blob: a413e96a91e7cf6fc5d6b312a9caaef0530b82d6 [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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000011///
12//===----------------------------------------------------------------------===//
13
Alexey Bataev9959db52014-05-06 10:08:46 +000014#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000015#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000043 OMPD_teams_distribute_parallel,
44 OMPD_target_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
47class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
50
51public:
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
54 ExprResult Res =
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
56 if (Res.isUsable())
57 Identifiers.push_back(Res.get());
58 }
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
60};
Dmitry Polukhin82478332016-02-13 06:53:38 +000061} // namespace
62
63// Map token string to extended OMP token kind that are
64// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
68 return DKind;
69
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000073 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000074 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000079 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Default(OMPD_unknown);
81}
82
Alexey Bataev61908f652018-04-23 19:53:05 +000083static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000084 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
85 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
86 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000088 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
89 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
90 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
91 {OMPD_declare, OMPD_target, OMPD_declare_target},
92 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
93 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
94 {OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd},
96 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
97 {OMPD_end, OMPD_declare, OMPD_end_declare},
98 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
99 {OMPD_target, OMPD_data, OMPD_target_data},
100 {OMPD_target, OMPD_enter, OMPD_target_enter},
101 {OMPD_target, OMPD_exit, OMPD_target_exit},
102 {OMPD_target, OMPD_update, OMPD_target_update},
103 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
104 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
105 {OMPD_for, OMPD_simd, OMPD_for_simd},
106 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
107 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
108 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
109 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
110 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
111 {OMPD_target, OMPD_simd, OMPD_target_simd},
112 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
113 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
114 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
115 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
116 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
117 {OMPD_teams_distribute_parallel, OMPD_for,
118 OMPD_teams_distribute_parallel_for},
119 {OMPD_teams_distribute_parallel_for, OMPD_simd,
120 OMPD_teams_distribute_parallel_for_simd},
121 {OMPD_target, OMPD_teams, OMPD_target_teams},
122 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
123 {OMPD_target_teams_distribute, OMPD_parallel,
124 OMPD_target_teams_distribute_parallel},
125 {OMPD_target_teams_distribute, OMPD_simd,
126 OMPD_target_teams_distribute_simd},
127 {OMPD_target_teams_distribute_parallel, OMPD_for,
128 OMPD_target_teams_distribute_parallel_for},
129 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
130 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000131 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000132 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000133 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000134 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000135 ? static_cast<unsigned>(OMPD_unknown)
136 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
137 if (DKind == OMPD_unknown)
138 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000139
Alexey Bataev61908f652018-04-23 19:53:05 +0000140 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
141 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000142 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000143
Dmitry Polukhin82478332016-02-13 06:53:38 +0000144 Tok = P.getPreprocessor().LookAhead(0);
145 unsigned SDKind =
146 Tok.isAnnotation()
147 ? static_cast<unsigned>(OMPD_unknown)
148 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
149 if (SDKind == OMPD_unknown)
150 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000151
Alexey Bataev61908f652018-04-23 19:53:05 +0000152 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000153 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000154 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000155 }
156 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000157 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
158 : OMPD_unknown;
159}
160
161static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000162 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000163 Sema &Actions = P.getActions();
164 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000165 // Allow to use 'operator' keyword for C++ operators
166 bool WithOperator = false;
167 if (Tok.is(tok::kw_operator)) {
168 P.ConsumeToken();
169 Tok = P.getCurToken();
170 WithOperator = true;
171 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000172 switch (Tok.getKind()) {
173 case tok::plus: // '+'
174 OOK = OO_Plus;
175 break;
176 case tok::minus: // '-'
177 OOK = OO_Minus;
178 break;
179 case tok::star: // '*'
180 OOK = OO_Star;
181 break;
182 case tok::amp: // '&'
183 OOK = OO_Amp;
184 break;
185 case tok::pipe: // '|'
186 OOK = OO_Pipe;
187 break;
188 case tok::caret: // '^'
189 OOK = OO_Caret;
190 break;
191 case tok::ampamp: // '&&'
192 OOK = OO_AmpAmp;
193 break;
194 case tok::pipepipe: // '||'
195 OOK = OO_PipePipe;
196 break;
197 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000198 if (!WithOperator)
199 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000200 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 default:
202 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
203 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
204 Parser::StopBeforeMatch);
205 return DeclarationName();
206 }
207 P.ConsumeToken();
208 auto &DeclNames = Actions.getASTContext().DeclarationNames;
209 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
210 : DeclNames.getCXXOperatorName(OOK);
211}
212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000213/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000214///
215/// declare-reduction-directive:
216/// annot_pragma_openmp 'declare' 'reduction'
217/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
218/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
219/// annot_pragma_openmp_end
220/// <reduction_id> is either a base language identifier or one of the following
221/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
222///
223Parser::DeclGroupPtrTy
224Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
225 // Parse '('.
226 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
227 if (T.expectAndConsume(diag::err_expected_lparen_after,
228 getOpenMPDirectiveName(OMPD_declare_reduction))) {
229 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
230 return DeclGroupPtrTy();
231 }
232
233 DeclarationName Name = parseOpenMPReductionId(*this);
234 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
235 return DeclGroupPtrTy();
236
237 // Consume ':'.
238 bool IsCorrect = !ExpectAndConsume(tok::colon);
239
240 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
241 return DeclGroupPtrTy();
242
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000243 IsCorrect = IsCorrect && !Name.isEmpty();
244
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000245 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
246 Diag(Tok.getLocation(), diag::err_expected_type);
247 IsCorrect = false;
248 }
249
250 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
251 return DeclGroupPtrTy();
252
253 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
254 // Parse list of types until ':' token.
255 do {
256 ColonProtectionRAIIObject ColonRAII(*this);
257 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000258 TypeResult TR =
259 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000260 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000261 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000262 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
263 if (!ReductionType.isNull()) {
264 ReductionTypes.push_back(
265 std::make_pair(ReductionType, Range.getBegin()));
266 }
267 } else {
268 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
269 StopBeforeMatch);
270 }
271
272 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
273 break;
274
275 // Consume ','.
276 if (ExpectAndConsume(tok::comma)) {
277 IsCorrect = false;
278 if (Tok.is(tok::annot_pragma_openmp_end)) {
279 Diag(Tok.getLocation(), diag::err_expected_type);
280 return DeclGroupPtrTy();
281 }
282 }
283 } while (Tok.isNot(tok::annot_pragma_openmp_end));
284
285 if (ReductionTypes.empty()) {
286 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
287 return DeclGroupPtrTy();
288 }
289
290 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
291 return DeclGroupPtrTy();
292
293 // Consume ':'.
294 if (ExpectAndConsume(tok::colon))
295 IsCorrect = false;
296
297 if (Tok.is(tok::annot_pragma_openmp_end)) {
298 Diag(Tok.getLocation(), diag::err_expected_expression);
299 return DeclGroupPtrTy();
300 }
301
302 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
303 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
304
305 // Parse <combiner> expression and then parse initializer if any for each
306 // correct type.
307 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000308 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000309 TentativeParsingAction TPA(*this);
310 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000311 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000312 Scope::OpenMPDirectiveScope);
313 // Parse <combiner> expression.
314 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
315 ExprResult CombinerResult =
316 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
317 D->getLocation(), /*DiscardedValue=*/true);
318 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
319
320 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
321 Tok.isNot(tok::annot_pragma_openmp_end)) {
322 TPA.Commit();
323 IsCorrect = false;
324 break;
325 }
326 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
327 ExprResult InitializerResult;
328 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
329 // Parse <initializer> expression.
330 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000331 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000332 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000333 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000334 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
335 TPA.Commit();
336 IsCorrect = false;
337 break;
338 }
339 // Parse '('.
340 BalancedDelimiterTracker T(*this, tok::l_paren,
341 tok::annot_pragma_openmp_end);
342 IsCorrect =
343 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
344 IsCorrect;
345 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
346 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000347 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000348 Scope::OpenMPDirectiveScope);
349 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000350 VarDecl *OmpPrivParm =
351 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
352 D);
353 // Check if initializer is omp_priv <init_expr> or something else.
354 if (Tok.is(tok::identifier) &&
355 Tok.getIdentifierInfo()->isStr("omp_priv")) {
356 ConsumeToken();
357 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
358 } else {
359 InitializerResult = Actions.ActOnFinishFullExpr(
360 ParseAssignmentExpression().get(), D->getLocation(),
361 /*DiscardedValue=*/true);
362 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000363 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000364 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000365 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
366 Tok.isNot(tok::annot_pragma_openmp_end)) {
367 TPA.Commit();
368 IsCorrect = false;
369 break;
370 }
371 IsCorrect =
372 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
373 }
374 }
375
376 ++I;
377 // Revert parsing if not the last type, otherwise accept it, we're done with
378 // parsing.
379 if (I != E)
380 TPA.Revert();
381 else
382 TPA.Commit();
383 }
384 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
385 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000386}
387
Alexey Bataev070f43a2017-09-06 14:49:58 +0000388void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
389 // Parse declarator '=' initializer.
390 // If a '==' or '+=' is found, suggest a fixit to '='.
391 if (isTokenEqualOrEqualTypo()) {
392 ConsumeToken();
393
394 if (Tok.is(tok::code_completion)) {
395 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
396 Actions.FinalizeDeclaration(OmpPrivParm);
397 cutOffParsing();
398 return;
399 }
400
401 ExprResult Init(ParseInitializer());
402
403 if (Init.isInvalid()) {
404 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
405 Actions.ActOnInitializerError(OmpPrivParm);
406 } else {
407 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
408 /*DirectInit=*/false);
409 }
410 } else if (Tok.is(tok::l_paren)) {
411 // Parse C++ direct initializer: '(' expression-list ')'
412 BalancedDelimiterTracker T(*this, tok::l_paren);
413 T.consumeOpen();
414
415 ExprVector Exprs;
416 CommaLocsTy CommaLocs;
417
418 if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] {
419 Actions.CodeCompleteConstructor(
420 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
421 OmpPrivParm->getLocation(), Exprs);
422 })) {
423 Actions.ActOnInitializerError(OmpPrivParm);
424 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
425 } else {
426 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000427 SourceLocation RLoc = Tok.getLocation();
428 if (!T.consumeClose())
429 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000430
431 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
432 "Unexpected number of commas!");
433
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000434 ExprResult Initializer =
435 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000436 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
437 /*DirectInit=*/true);
438 }
439 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
440 // Parse C++0x braced-init-list.
441 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
442
443 ExprResult Init(ParseBraceInitializer());
444
445 if (Init.isInvalid()) {
446 Actions.ActOnInitializerError(OmpPrivParm);
447 } else {
448 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
449 /*DirectInit=*/true);
450 }
451 } else {
452 Actions.ActOnUninitializedDecl(OmpPrivParm);
453 }
454}
455
Alexey Bataev2af33e32016-04-07 12:45:37 +0000456namespace {
457/// RAII that recreates function context for correct parsing of clauses of
458/// 'declare simd' construct.
459/// OpenMP, 2.8.2 declare simd Construct
460/// The expressions appearing in the clauses of this directive are evaluated in
461/// the scope of the arguments of the function declaration or definition.
462class FNContextRAII final {
463 Parser &P;
464 Sema::CXXThisScopeRAII *ThisScope;
465 Parser::ParseScope *TempScope;
466 Parser::ParseScope *FnScope;
467 bool HasTemplateScope = false;
468 bool HasFunScope = false;
469 FNContextRAII() = delete;
470 FNContextRAII(const FNContextRAII &) = delete;
471 FNContextRAII &operator=(const FNContextRAII &) = delete;
472
473public:
474 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
475 Decl *D = *Ptr.get().begin();
476 NamedDecl *ND = dyn_cast<NamedDecl>(D);
477 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
478 Sema &Actions = P.getActions();
479
480 // Allow 'this' within late-parsed attributes.
481 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
482 ND && ND->isCXXInstanceMember());
483
484 // If the Decl is templatized, add template parameters to scope.
485 HasTemplateScope = D->isTemplateDecl();
486 TempScope =
487 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
488 if (HasTemplateScope)
489 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
490
491 // If the Decl is on a function, add function parameters to the scope.
492 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000493 FnScope = new Parser::ParseScope(
494 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
495 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000496 if (HasFunScope)
497 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
498 }
499 ~FNContextRAII() {
500 if (HasFunScope) {
501 P.getActions().ActOnExitFunctionContext();
502 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
503 }
504 if (HasTemplateScope)
505 TempScope->Exit();
506 delete FnScope;
507 delete TempScope;
508 delete ThisScope;
509 }
510};
511} // namespace
512
Alexey Bataevd93d3762016-04-12 09:35:56 +0000513/// Parses clauses for 'declare simd' directive.
514/// clause:
515/// 'inbranch' | 'notinbranch'
516/// 'simdlen' '(' <expr> ')'
517/// { 'uniform' '(' <argument_list> ')' }
518/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000519/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
520static bool parseDeclareSimdClauses(
521 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
522 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
523 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
524 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000525 SourceRange BSRange;
526 const Token &Tok = P.getCurToken();
527 bool IsError = false;
528 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
529 if (Tok.isNot(tok::identifier))
530 break;
531 OMPDeclareSimdDeclAttr::BranchStateTy Out;
532 IdentifierInfo *II = Tok.getIdentifierInfo();
533 StringRef ClauseName = II->getName();
534 // Parse 'inranch|notinbranch' clauses.
535 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
536 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
537 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
538 << ClauseName
539 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
540 IsError = true;
541 }
542 BS = Out;
543 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
544 P.ConsumeToken();
545 } else if (ClauseName.equals("simdlen")) {
546 if (SimdLen.isUsable()) {
547 P.Diag(Tok, diag::err_omp_more_one_clause)
548 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
549 IsError = true;
550 }
551 P.ConsumeToken();
552 SourceLocation RLoc;
553 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
554 if (SimdLen.isInvalid())
555 IsError = true;
556 } else {
557 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000558 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
559 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000560 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000561 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000562 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000563 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000564 else if (CKind == OMPC_linear)
565 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000566
567 P.ConsumeToken();
568 if (P.ParseOpenMPVarList(OMPD_declare_simd,
569 getOpenMPClauseKind(ClauseName), *Vars, Data))
570 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000571 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000572 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000573 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000574 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
575 Data.DepLinMapLoc))
576 Data.LinKind = OMPC_LINEAR_val;
577 LinModifiers.append(Linears.size() - LinModifiers.size(),
578 Data.LinKind);
579 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
580 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000581 } else
582 // TODO: add parsing of other clauses.
583 break;
584 }
585 // Skip ',' if any.
586 if (Tok.is(tok::comma))
587 P.ConsumeToken();
588 }
589 return IsError;
590}
591
Alexey Bataev2af33e32016-04-07 12:45:37 +0000592/// Parse clauses for '#pragma omp declare simd'.
593Parser::DeclGroupPtrTy
594Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
595 CachedTokens &Toks, SourceLocation Loc) {
596 PP.EnterToken(Tok);
597 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
598 // Consume the previously pushed token.
599 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
600
601 FNContextRAII FnContext(*this, Ptr);
602 OMPDeclareSimdDeclAttr::BranchStateTy BS =
603 OMPDeclareSimdDeclAttr::BS_Undefined;
604 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000605 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000606 SmallVector<Expr *, 4> Aligneds;
607 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000608 SmallVector<Expr *, 4> Linears;
609 SmallVector<unsigned, 4> LinModifiers;
610 SmallVector<Expr *, 4> Steps;
611 bool IsError =
612 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
613 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000614 // Need to check for extra tokens.
615 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
616 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
617 << getOpenMPDirectiveName(OMPD_declare_simd);
618 while (Tok.isNot(tok::annot_pragma_openmp_end))
619 ConsumeAnyToken();
620 }
621 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000622 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000623 if (IsError)
624 return Ptr;
625 return Actions.ActOnOpenMPDeclareSimdDirective(
626 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
627 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000628}
629
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000630/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000631///
632/// threadprivate-directive:
633/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000634/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000635///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000636/// declare-reduction-directive:
637/// annot_pragma_openmp 'declare' 'reduction' [...]
638/// annot_pragma_openmp_end
639///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000640/// declare-simd-directive:
641/// annot_pragma_openmp 'declare simd' {<clause> [,]}
642/// annot_pragma_openmp_end
643/// <function declaration/definition>
644///
645Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
646 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
647 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000648 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000649 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000650
Richard Smithaf3b3252017-05-18 19:21:48 +0000651 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000652 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000653
654 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000655 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000656 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000657 ThreadprivateListParserHelper Helper(this);
658 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000659 // The last seen token is annot_pragma_openmp_end - need to check for
660 // extra tokens.
661 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
662 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000663 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000664 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000665 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000666 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000667 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000668 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
669 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000670 }
671 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000672 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000673 case OMPD_declare_reduction:
674 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000675 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000676 // The last seen token is annot_pragma_openmp_end - need to check for
677 // extra tokens.
678 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
679 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
680 << getOpenMPDirectiveName(OMPD_declare_reduction);
681 while (Tok.isNot(tok::annot_pragma_openmp_end))
682 ConsumeAnyToken();
683 }
684 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000685 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000686 return Res;
687 }
688 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000689 case OMPD_declare_simd: {
690 // The syntax is:
691 // { #pragma omp declare simd }
692 // <function-declaration-or-definition>
693 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000694 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000695 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000696 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
697 Toks.push_back(Tok);
698 ConsumeAnyToken();
699 }
700 Toks.push_back(Tok);
701 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000702
703 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000704 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000705 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000706 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000707 // Here we expect to see some function declaration.
708 if (AS == AS_none) {
709 assert(TagType == DeclSpec::TST_unspecified);
710 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000711 ParsingDeclSpec PDS(*this);
712 Ptr = ParseExternalDeclaration(Attrs, &PDS);
713 } else {
714 Ptr =
715 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
716 }
717 }
718 if (!Ptr) {
719 Diag(Loc, diag::err_omp_decl_in_declare_simd);
720 return DeclGroupPtrTy();
721 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000722 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000723 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000724 case OMPD_declare_target: {
725 SourceLocation DTLoc = ConsumeAnyToken();
726 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000727 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000728 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000729 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
730 OMPDeclareTargetDeclAttr::MapTypeTy MT =
731 OMPDeclareTargetDeclAttr::MT_To;
732 if (Tok.is(tok::identifier)) {
733 IdentifierInfo *II = Tok.getIdentifierInfo();
734 StringRef ClauseName = II->getName();
735 // Parse 'to|link' clauses.
736 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
737 MT)) {
738 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
739 << ClauseName;
740 break;
741 }
742 ConsumeToken();
743 }
Alexey Bataev61908f652018-04-23 19:53:05 +0000744 auto &&Callback = [this, MT, &SameDirectiveDecls](
745 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000746 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
747 SameDirectiveDecls);
748 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000749 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
750 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000751 break;
752
753 // Consume optional ','.
754 if (Tok.is(tok::comma))
755 ConsumeToken();
756 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000757 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000758 ConsumeAnyToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000759 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
760 SameDirectiveDecls.end());
Alexey Bataev34f8a702018-03-28 14:28:54 +0000761 if (Decls.empty())
762 return DeclGroupPtrTy();
763 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000764 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000765
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000766 // Skip the last annot_pragma_openmp_end.
767 ConsumeAnyToken();
768
769 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
770 return DeclGroupPtrTy();
771
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000772 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000773 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000774 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
775 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000776 DeclGroupPtrTy Ptr;
777 // Here we expect to see some function declaration.
778 if (AS == AS_none) {
779 assert(TagType == DeclSpec::TST_unspecified);
780 MaybeParseCXX11Attributes(Attrs);
781 ParsingDeclSpec PDS(*this);
782 Ptr = ParseExternalDeclaration(Attrs, &PDS);
783 } else {
784 Ptr =
785 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
786 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000787 if (Ptr) {
788 DeclGroupRef Ref = Ptr.get();
789 Decls.append(Ref.begin(), Ref.end());
790 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000791 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
792 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000793 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000794 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000795 if (DKind != OMPD_end_declare_target)
796 TPA.Revert();
797 else
798 TPA.Commit();
799 }
800 }
801
802 if (DKind == OMPD_end_declare_target) {
803 ConsumeAnyToken();
804 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
805 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
806 << getOpenMPDirectiveName(OMPD_end_declare_target);
807 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
808 }
809 // Skip the last annot_pragma_openmp_end.
810 ConsumeAnyToken();
811 } else {
812 Diag(Tok, diag::err_expected_end_declare_target);
813 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
814 }
815 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000816 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000817 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000818 case OMPD_unknown:
819 Diag(Tok, diag::err_omp_unknown_directive);
820 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000822 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000823 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000824 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000825 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000826 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000827 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000828 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000829 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000830 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000831 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000832 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000833 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000834 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000835 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000836 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000837 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000838 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000839 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000840 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000841 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000842 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000843 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000844 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000845 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000846 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000847 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000848 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000849 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000850 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000851 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000852 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000853 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000854 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000855 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000856 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000857 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000858 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000859 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000860 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000861 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000862 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000863 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000864 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000865 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000866 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000867 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000868 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000869 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000870 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000871 break;
872 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000873 while (Tok.isNot(tok::annot_pragma_openmp_end))
874 ConsumeAnyToken();
875 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000876 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000877}
878
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000879/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000880///
881/// threadprivate-directive:
882/// annot_pragma_openmp 'threadprivate' simple-variable-list
883/// annot_pragma_openmp_end
884///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000885/// declare-reduction-directive:
886/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
887/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
888/// ('omp_priv' '=' <expression>|<function_call>) ')']
889/// annot_pragma_openmp_end
890///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000891/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000892/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000893/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
894/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000895/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000896/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000897/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000898/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000899/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000900/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000901/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000902/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000903/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000904/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000905/// 'teams distribute parallel for' | 'target teams' |
906/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000907/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000908/// 'target teams distribute parallel for simd' |
909/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000910/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000912StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000913 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000915 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000917 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000918 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000919 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
920 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000921 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000922 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000923 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000924 // Name of critical directive.
925 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000926 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000927 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000928 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929
930 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000931 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000932 if (Allowed != ACK_Any) {
933 Diag(Tok, diag::err_omp_immediate_directive)
934 << getOpenMPDirectiveName(DKind) << 0;
935 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000936 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000937 ThreadprivateListParserHelper Helper(this);
938 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000939 // The last seen token is annot_pragma_openmp_end - need to check for
940 // extra tokens.
941 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
942 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000943 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000944 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000945 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000946 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
947 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000948 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
949 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000950 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000951 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000952 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000953 case OMPD_declare_reduction:
954 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000955 if (DeclGroupPtrTy Res =
956 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000957 // The last seen token is annot_pragma_openmp_end - need to check for
958 // extra tokens.
959 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
960 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
961 << getOpenMPDirectiveName(OMPD_declare_reduction);
962 while (Tok.isNot(tok::annot_pragma_openmp_end))
963 ConsumeAnyToken();
964 }
965 ConsumeAnyToken();
966 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +0000967 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000968 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +0000969 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000970 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000971 case OMPD_flush:
972 if (PP.LookAhead(0).is(tok::l_paren)) {
973 FlushHasClause = true;
974 // Push copy of the current token back to stream to properly parse
975 // pseudo-clause OMPFlushClause.
976 PP.EnterToken(Tok);
977 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000978 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000979 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000980 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000981 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000982 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000983 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000984 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000985 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000986 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000987 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000988 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000989 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000990 }
991 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000992 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000993 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000994 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000995 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000996 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000997 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000998 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000999 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001000 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001001 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001002 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001003 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001004 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001005 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001006 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001007 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001008 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001009 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001010 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001011 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001012 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001013 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001014 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001015 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001016 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001017 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001018 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001019 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001020 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001021 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001022 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001023 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001024 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001025 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001026 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001027 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001028 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001029 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001030 case OMPD_target_teams_distribute_parallel_for_simd:
1031 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001032 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001033 // Parse directive name of the 'critical' directive if any.
1034 if (DKind == OMPD_critical) {
1035 BalancedDelimiterTracker T(*this, tok::l_paren,
1036 tok::annot_pragma_openmp_end);
1037 if (!T.consumeOpen()) {
1038 if (Tok.isAnyIdentifier()) {
1039 DirName =
1040 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1041 ConsumeAnyToken();
1042 } else {
1043 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1044 }
1045 T.consumeClose();
1046 }
Alexey Bataev80909872015-07-02 11:25:17 +00001047 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001048 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001049 if (Tok.isNot(tok::annot_pragma_openmp_end))
1050 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001051 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001052
Alexey Bataevf29276e2014-06-18 04:14:57 +00001053 if (isOpenMPLoopDirective(DKind))
1054 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1055 if (isOpenMPSimdDirective(DKind))
1056 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1057 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001058 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001059
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001060 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001061 OpenMPClauseKind CKind =
1062 Tok.isAnnotation()
1063 ? OMPC_unknown
1064 : FlushHasClause ? OMPC_flush
1065 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001066 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001067 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001068 OMPClause *Clause =
1069 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001070 FirstClauses[CKind].setInt(true);
1071 if (Clause) {
1072 FirstClauses[CKind].setPointer(Clause);
1073 Clauses.push_back(Clause);
1074 }
1075
1076 // Skip ',' if any.
1077 if (Tok.is(tok::comma))
1078 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001079 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 }
1081 // End location of the directive.
1082 EndLoc = Tok.getLocation();
1083 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001084 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085
Alexey Bataeveb482352015-12-18 05:05:56 +00001086 // OpenMP [2.13.8, ordered Construct, Syntax]
1087 // If the depend clause is specified, the ordered construct is a stand-alone
1088 // directive.
1089 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001090 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001091 Diag(Loc, diag::err_omp_immediate_directive)
1092 << getOpenMPDirectiveName(DKind) << 1
1093 << getOpenMPClauseName(OMPC_depend);
1094 }
1095 HasAssociatedStatement = false;
1096 }
1097
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001099 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001100 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001101 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001102 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1103 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1104 // should have at least one compound statement scope within it.
1105 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001106 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001107 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1108 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001109 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001110 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1111 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1112 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001113 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001114 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001115 Directive = Actions.ActOnOpenMPExecutableDirective(
1116 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1117 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001118
1119 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001120 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001121 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001123 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001124 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001125 case OMPD_declare_target:
1126 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001127 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001128 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001129 SkipUntil(tok::annot_pragma_openmp_end);
1130 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001131 case OMPD_unknown:
1132 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001133 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001134 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001135 }
1136 return Directive;
1137}
1138
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001139// Parses simple list:
1140// simple-variable-list:
1141// '(' id-expression {, id-expression} ')'
1142//
1143bool Parser::ParseOpenMPSimpleVarList(
1144 OpenMPDirectiveKind Kind,
1145 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1146 Callback,
1147 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001148 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001149 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001150 if (T.expectAndConsume(diag::err_expected_lparen_after,
1151 getOpenMPDirectiveName(Kind)))
1152 return true;
1153 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001154 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001155
1156 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001157 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001158 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001159 UnqualifiedId Name;
1160 // Read var name.
1161 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001162 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001163
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001164 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001165 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001166 IsCorrect = false;
1167 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001168 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001169 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001170 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001171 IsCorrect = false;
1172 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001173 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1175 Tok.isNot(tok::annot_pragma_openmp_end)) {
1176 IsCorrect = false;
1177 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001178 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001179 Diag(PrevTok.getLocation(), diag::err_expected)
1180 << tok::identifier
1181 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001182 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001183 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001184 }
1185 // Consume ','.
1186 if (Tok.is(tok::comma)) {
1187 ConsumeToken();
1188 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001189 }
1190
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001191 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001192 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 IsCorrect = false;
1194 }
1195
1196 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001197 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001198
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001199 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001200}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001201
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001202/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001203///
1204/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001205/// if-clause | final-clause | num_threads-clause | safelen-clause |
1206/// default-clause | private-clause | firstprivate-clause | shared-clause
1207/// | linear-clause | aligned-clause | collapse-clause |
1208/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001209/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001210/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001211/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001212/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001213/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001214/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001215/// from-clause | is_device_ptr-clause | task_reduction-clause |
1216/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217///
1218OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1219 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001220 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001221 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001222 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001223 // Check if clause is allowed for the given directive.
1224 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001225 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1226 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001227 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001228 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001229 }
1230
1231 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001232 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001233 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001234 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001235 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001236 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001237 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001238 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001239 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001240 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001241 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001242 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001243 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001244 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001245 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001246 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001247 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001248 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001249 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001250 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001251 // OpenMP [2.9.1, target data construct, Restrictions]
1252 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001253 // OpenMP [2.11.1, task Construct, Restrictions]
1254 // At most one if clause can appear on the directive.
1255 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001256 // OpenMP [teams Construct, Restrictions]
1257 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001258 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001259 // OpenMP [2.9.1, task Construct, Restrictions]
1260 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001261 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1262 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001263 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1264 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001265 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001266 Diag(Tok, diag::err_omp_more_one_clause)
1267 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001268 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001269 }
1270
Alexey Bataev10e775f2015-07-30 11:36:16 +00001271 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001272 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001273 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001274 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001275 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001276 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001277 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001278 // OpenMP [2.14.3.1, Restrictions]
1279 // Only a single default clause may be specified on a parallel, task or
1280 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001281 // OpenMP [2.5, parallel Construct, Restrictions]
1282 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001283 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001284 Diag(Tok, diag::err_omp_more_one_clause)
1285 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001286 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001287 }
1288
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001289 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001291 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001292 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001293 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001294 // OpenMP [2.7.1, Restrictions, p. 3]
1295 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001296 // OpenMP [2.10.4, Restrictions, p. 106]
1297 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001298 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001299 Diag(Tok, diag::err_omp_more_one_clause)
1300 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001301 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001302 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001303 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001304
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001305 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001306 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001307 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001308 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001309 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001310 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001311 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001312 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001313 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001314 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001315 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001316 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001317 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001318 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001319 // OpenMP [2.7.1, Restrictions, p. 9]
1320 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001321 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1322 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001323 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001324 Diag(Tok, diag::err_omp_more_one_clause)
1325 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001326 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001327 }
1328
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001329 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001330 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001331 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001332 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001333 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001334 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001335 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001336 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001337 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001338 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001339 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001340 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001341 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001342 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001343 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001344 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001345 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001346 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001347 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001348 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001349 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001350 break;
1351 case OMPC_unknown:
1352 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001353 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001354 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001355 break;
1356 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001357 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001358 if (!WrongDirective)
1359 Diag(Tok, diag::err_omp_unexpected_clause)
1360 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001361 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001362 break;
1363 }
Craig Topper161e4db2014-05-21 06:02:52 +00001364 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001365}
1366
Alexey Bataev2af33e32016-04-07 12:45:37 +00001367/// Parses simple expression in parens for single-expression clauses of OpenMP
1368/// constructs.
1369/// \param RLoc Returned location of right paren.
1370ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1371 SourceLocation &RLoc) {
1372 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1373 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1374 return ExprError();
1375
1376 SourceLocation ELoc = Tok.getLocation();
1377 ExprResult LHS(ParseCastExpression(
1378 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1379 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1380 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1381
1382 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001383 RLoc = Tok.getLocation();
1384 if (!T.consumeClose())
1385 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001386
Alexey Bataev2af33e32016-04-07 12:45:37 +00001387 return Val;
1388}
1389
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001390/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001391/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001392/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001393///
Alexey Bataev3778b602014-07-17 07:32:53 +00001394/// final-clause:
1395/// 'final' '(' expression ')'
1396///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001397/// num_threads-clause:
1398/// 'num_threads' '(' expression ')'
1399///
1400/// safelen-clause:
1401/// 'safelen' '(' expression ')'
1402///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001403/// simdlen-clause:
1404/// 'simdlen' '(' expression ')'
1405///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001406/// collapse-clause:
1407/// 'collapse' '(' expression ')'
1408///
Alexey Bataeva0569352015-12-01 10:17:31 +00001409/// priority-clause:
1410/// 'priority' '(' expression ')'
1411///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001412/// grainsize-clause:
1413/// 'grainsize' '(' expression ')'
1414///
Alexey Bataev382967a2015-12-08 12:06:20 +00001415/// num_tasks-clause:
1416/// 'num_tasks' '(' expression ')'
1417///
Alexey Bataev28c75412015-12-15 08:19:24 +00001418/// hint-clause:
1419/// 'hint' '(' expression ')'
1420///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001421OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1422 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001423 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001424 SourceLocation LLoc = Tok.getLocation();
1425 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001426
Alexey Bataev2af33e32016-04-07 12:45:37 +00001427 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001428
1429 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001430 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001431
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001432 if (ParseOnly)
1433 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001434 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001435}
1436
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001437/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001438///
1439/// default-clause:
1440/// 'default' '(' 'none' | 'shared' ')
1441///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001442/// proc_bind-clause:
1443/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1444///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001445OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1446 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001447 SourceLocation Loc = Tok.getLocation();
1448 SourceLocation LOpen = ConsumeToken();
1449 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001450 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001451 if (T.expectAndConsume(diag::err_expected_lparen_after,
1452 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001453 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001454
Alexey Bataeva55ed262014-05-28 06:15:33 +00001455 unsigned Type = getOpenMPSimpleClauseType(
1456 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001457 SourceLocation TypeLoc = Tok.getLocation();
1458 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1459 Tok.isNot(tok::annot_pragma_openmp_end))
1460 ConsumeAnyToken();
1461
1462 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001463 SourceLocation RLoc = Tok.getLocation();
1464 if (!T.consumeClose())
1465 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001466
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001467 if (ParseOnly)
1468 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001469 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001470}
1471
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001472/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001473///
1474/// ordered-clause:
1475/// 'ordered'
1476///
Alexey Bataev236070f2014-06-20 11:19:47 +00001477/// nowait-clause:
1478/// 'nowait'
1479///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001480/// untied-clause:
1481/// 'untied'
1482///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001483/// mergeable-clause:
1484/// 'mergeable'
1485///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001486/// read-clause:
1487/// 'read'
1488///
Alexey Bataev346265e2015-09-25 10:37:12 +00001489/// threads-clause:
1490/// 'threads'
1491///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001492/// simd-clause:
1493/// 'simd'
1494///
Alexey Bataevb825de12015-12-07 10:51:44 +00001495/// nogroup-clause:
1496/// 'nogroup'
1497///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001498OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001499 SourceLocation Loc = Tok.getLocation();
1500 ConsumeAnyToken();
1501
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001502 if (ParseOnly)
1503 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001504 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1505}
1506
1507
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001508/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001509/// argument like 'schedule' or 'dist_schedule'.
1510///
1511/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001512/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1513/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001514///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001515/// if-clause:
1516/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1517///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001518/// defaultmap:
1519/// 'defaultmap' '(' modifier ':' kind ')'
1520///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001521OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1522 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001523 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001524 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001525 // Parse '('.
1526 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1527 if (T.expectAndConsume(diag::err_expected_lparen_after,
1528 getOpenMPClauseName(Kind)))
1529 return nullptr;
1530
1531 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001532 SmallVector<unsigned, 4> Arg;
1533 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001534 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001535 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1536 Arg.resize(NumberOfElements);
1537 KLoc.resize(NumberOfElements);
1538 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1539 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1540 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001541 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001542 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001543 if (KindModifier > OMPC_SCHEDULE_unknown) {
1544 // Parse 'modifier'
1545 Arg[Modifier1] = KindModifier;
1546 KLoc[Modifier1] = Tok.getLocation();
1547 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1548 Tok.isNot(tok::annot_pragma_openmp_end))
1549 ConsumeAnyToken();
1550 if (Tok.is(tok::comma)) {
1551 // Parse ',' 'modifier'
1552 ConsumeAnyToken();
1553 KindModifier = getOpenMPSimpleClauseType(
1554 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1555 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1556 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001557 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001558 KLoc[Modifier2] = Tok.getLocation();
1559 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1560 Tok.isNot(tok::annot_pragma_openmp_end))
1561 ConsumeAnyToken();
1562 }
1563 // Parse ':'
1564 if (Tok.is(tok::colon))
1565 ConsumeAnyToken();
1566 else
1567 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1568 KindModifier = getOpenMPSimpleClauseType(
1569 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1570 }
1571 Arg[ScheduleKind] = KindModifier;
1572 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001573 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1574 Tok.isNot(tok::annot_pragma_openmp_end))
1575 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001576 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1577 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1578 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001579 Tok.is(tok::comma))
1580 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001581 } else if (Kind == OMPC_dist_schedule) {
1582 Arg.push_back(getOpenMPSimpleClauseType(
1583 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1584 KLoc.push_back(Tok.getLocation());
1585 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1586 Tok.isNot(tok::annot_pragma_openmp_end))
1587 ConsumeAnyToken();
1588 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1589 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001590 } else if (Kind == OMPC_defaultmap) {
1591 // Get a defaultmap modifier
1592 Arg.push_back(getOpenMPSimpleClauseType(
1593 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1594 KLoc.push_back(Tok.getLocation());
1595 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1596 Tok.isNot(tok::annot_pragma_openmp_end))
1597 ConsumeAnyToken();
1598 // Parse ':'
1599 if (Tok.is(tok::colon))
1600 ConsumeAnyToken();
1601 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1602 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1603 // Get a defaultmap kind
1604 Arg.push_back(getOpenMPSimpleClauseType(
1605 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1606 KLoc.push_back(Tok.getLocation());
1607 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1608 Tok.isNot(tok::annot_pragma_openmp_end))
1609 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001610 } else {
1611 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001612 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001613 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001614 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001615 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001616 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001617 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1618 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001619 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001620 } else {
1621 TPA.Revert();
1622 Arg.back() = OMPD_unknown;
1623 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001624 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001625 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001626 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001627 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001628
Carlo Bertollib4adf552016-01-15 18:50:31 +00001629 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1630 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1631 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001632 if (NeedAnExpression) {
1633 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001634 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1635 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001636 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001637 }
1638
1639 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001640 SourceLocation RLoc = Tok.getLocation();
1641 if (!T.consumeClose())
1642 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001643
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001644 if (NeedAnExpression && Val.isInvalid())
1645 return nullptr;
1646
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001647 if (ParseOnly)
1648 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001649 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001650 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001651}
1652
Alexey Bataevc5e02582014-06-16 07:08:35 +00001653static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1654 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001655 if (ReductionIdScopeSpec.isEmpty()) {
1656 auto OOK = OO_None;
1657 switch (P.getCurToken().getKind()) {
1658 case tok::plus:
1659 OOK = OO_Plus;
1660 break;
1661 case tok::minus:
1662 OOK = OO_Minus;
1663 break;
1664 case tok::star:
1665 OOK = OO_Star;
1666 break;
1667 case tok::amp:
1668 OOK = OO_Amp;
1669 break;
1670 case tok::pipe:
1671 OOK = OO_Pipe;
1672 break;
1673 case tok::caret:
1674 OOK = OO_Caret;
1675 break;
1676 case tok::ampamp:
1677 OOK = OO_AmpAmp;
1678 break;
1679 case tok::pipepipe:
1680 OOK = OO_PipePipe;
1681 break;
1682 default:
1683 break;
1684 }
1685 if (OOK != OO_None) {
1686 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001687 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001688 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1689 return false;
1690 }
1691 }
1692 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1693 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001694 /*AllowConstructorName*/ false,
1695 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001696 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001697}
1698
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001699/// Parses clauses with list.
1700bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1701 OpenMPClauseKind Kind,
1702 SmallVectorImpl<Expr *> &Vars,
1703 OpenMPVarListDataTy &Data) {
1704 UnqualifiedId UnqualifiedReductionId;
1705 bool InvalidReductionId = false;
1706 bool MapTypeModifierSpecified = false;
1707
1708 // Parse '('.
1709 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1710 if (T.expectAndConsume(diag::err_expected_lparen_after,
1711 getOpenMPClauseName(Kind)))
1712 return true;
1713
1714 bool NeedRParenForLinear = false;
1715 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1716 tok::annot_pragma_openmp_end);
1717 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001718 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1719 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001720 ColonProtectionRAIIObject ColonRAII(*this);
1721 if (getLangOpts().CPlusPlus)
1722 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1723 /*ObjectType=*/nullptr,
1724 /*EnteringContext=*/false);
1725 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1726 UnqualifiedReductionId);
1727 if (InvalidReductionId) {
1728 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1729 StopBeforeMatch);
1730 }
1731 if (Tok.is(tok::colon))
1732 Data.ColonLoc = ConsumeToken();
1733 else
1734 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1735 if (!InvalidReductionId)
1736 Data.ReductionId =
1737 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1738 } else if (Kind == OMPC_depend) {
1739 // Handle dependency type for depend clause.
1740 ColonProtectionRAIIObject ColonRAII(*this);
1741 Data.DepKind =
1742 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1743 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1744 Data.DepLinMapLoc = Tok.getLocation();
1745
1746 if (Data.DepKind == OMPC_DEPEND_unknown) {
1747 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1748 StopBeforeMatch);
1749 } else {
1750 ConsumeToken();
1751 // Special processing for depend(source) clause.
1752 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1753 // Parse ')'.
1754 T.consumeClose();
1755 return false;
1756 }
1757 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001758 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001759 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001760 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001761 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1762 : diag::warn_pragma_expected_colon)
1763 << "dependency type";
1764 }
1765 } else if (Kind == OMPC_linear) {
1766 // Try to parse modifier if any.
1767 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1768 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1769 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1770 Data.DepLinMapLoc = ConsumeToken();
1771 LinearT.consumeOpen();
1772 NeedRParenForLinear = true;
1773 }
1774 } else if (Kind == OMPC_map) {
1775 // Handle map type for map clause.
1776 ColonProtectionRAIIObject ColonRAII(*this);
1777
1778 /// The map clause modifier token can be either a identifier or the C++
1779 /// delete keyword.
1780 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1781 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1782 };
1783
1784 // The first identifier may be a list item, a map-type or a
1785 // map-type-modifier. The map modifier can also be delete which has the same
1786 // spelling of the C++ delete keyword.
1787 Data.MapType =
1788 IsMapClauseModifierToken(Tok)
1789 ? static_cast<OpenMPMapClauseKind>(
1790 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1791 : OMPC_MAP_unknown;
1792 Data.DepLinMapLoc = Tok.getLocation();
1793 bool ColonExpected = false;
1794
1795 if (IsMapClauseModifierToken(Tok)) {
1796 if (PP.LookAhead(0).is(tok::colon)) {
1797 if (Data.MapType == OMPC_MAP_unknown)
1798 Diag(Tok, diag::err_omp_unknown_map_type);
1799 else if (Data.MapType == OMPC_MAP_always)
1800 Diag(Tok, diag::err_omp_map_type_missing);
1801 ConsumeToken();
1802 } else if (PP.LookAhead(0).is(tok::comma)) {
1803 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1804 PP.LookAhead(2).is(tok::colon)) {
1805 Data.MapTypeModifier = Data.MapType;
1806 if (Data.MapTypeModifier != OMPC_MAP_always) {
1807 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1808 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001809 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001810 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001811 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001812
1813 ConsumeToken();
1814 ConsumeToken();
1815
1816 Data.MapType =
1817 IsMapClauseModifierToken(Tok)
1818 ? static_cast<OpenMPMapClauseKind>(
1819 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1820 : OMPC_MAP_unknown;
1821 if (Data.MapType == OMPC_MAP_unknown ||
1822 Data.MapType == OMPC_MAP_always)
1823 Diag(Tok, diag::err_omp_unknown_map_type);
1824 ConsumeToken();
1825 } else {
1826 Data.MapType = OMPC_MAP_tofrom;
1827 Data.IsMapTypeImplicit = true;
1828 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001829 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1830 if (PP.LookAhead(1).is(tok::colon)) {
1831 Data.MapTypeModifier = Data.MapType;
1832 if (Data.MapTypeModifier != OMPC_MAP_always) {
1833 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1834 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001835 } else {
Carlo Bertollid8844b92017-05-03 15:28:48 +00001836 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001837 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001838
1839 ConsumeToken();
1840
1841 Data.MapType =
1842 IsMapClauseModifierToken(Tok)
1843 ? static_cast<OpenMPMapClauseKind>(
1844 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1845 : OMPC_MAP_unknown;
1846 if (Data.MapType == OMPC_MAP_unknown ||
1847 Data.MapType == OMPC_MAP_always)
1848 Diag(Tok, diag::err_omp_unknown_map_type);
1849 ConsumeToken();
1850 } else {
1851 Data.MapType = OMPC_MAP_tofrom;
1852 Data.IsMapTypeImplicit = true;
1853 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001854 } else {
1855 Data.MapType = OMPC_MAP_tofrom;
1856 Data.IsMapTypeImplicit = true;
1857 }
1858 } else {
1859 Data.MapType = OMPC_MAP_tofrom;
1860 Data.IsMapTypeImplicit = true;
1861 }
1862
1863 if (Tok.is(tok::colon))
1864 Data.ColonLoc = ConsumeToken();
1865 else if (ColonExpected)
1866 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1867 }
1868
Alexey Bataevfa312f32017-07-21 18:48:21 +00001869 bool IsComma =
1870 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1871 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1872 (Kind == OMPC_reduction && !InvalidReductionId) ||
1873 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1874 (!MapTypeModifierSpecified ||
1875 Data.MapTypeModifier == OMPC_MAP_always)) ||
1876 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001877 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1878 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1879 Tok.isNot(tok::annot_pragma_openmp_end))) {
1880 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1881 // Parse variable
1882 ExprResult VarExpr =
1883 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001884 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001885 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001886 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001887 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1888 StopBeforeMatch);
1889 }
1890 // Skip ',' if any
1891 IsComma = Tok.is(tok::comma);
1892 if (IsComma)
1893 ConsumeToken();
1894 else if (Tok.isNot(tok::r_paren) &&
1895 Tok.isNot(tok::annot_pragma_openmp_end) &&
1896 (!MayHaveTail || Tok.isNot(tok::colon)))
1897 Diag(Tok, diag::err_omp_expected_punc)
1898 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1899 : getOpenMPClauseName(Kind))
1900 << (Kind == OMPC_flush);
1901 }
1902
1903 // Parse ')' for linear clause with modifier.
1904 if (NeedRParenForLinear)
1905 LinearT.consumeClose();
1906
1907 // Parse ':' linear-step (or ':' alignment).
1908 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1909 if (MustHaveTail) {
1910 Data.ColonLoc = Tok.getLocation();
1911 SourceLocation ELoc = ConsumeToken();
1912 ExprResult Tail = ParseAssignmentExpression();
1913 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1914 if (Tail.isUsable())
1915 Data.TailExpr = Tail.get();
1916 else
1917 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1918 StopBeforeMatch);
1919 }
1920
1921 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001922 Data.RLoc = Tok.getLocation();
1923 if (!T.consumeClose())
1924 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00001925 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1926 Vars.empty()) ||
1927 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1928 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001929}
1930
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001931/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001932/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1933/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001934///
1935/// private-clause:
1936/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001937/// firstprivate-clause:
1938/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001939/// lastprivate-clause:
1940/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001941/// shared-clause:
1942/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001943/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001944/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001945/// aligned-clause:
1946/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001947/// reduction-clause:
1948/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001949/// task_reduction-clause:
1950/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001951/// in_reduction-clause:
1952/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001953/// copyprivate-clause:
1954/// 'copyprivate' '(' list ')'
1955/// flush-clause:
1956/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001957/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001958/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001959/// map-clause:
1960/// 'map' '(' [ [ always , ]
1961/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001962/// to-clause:
1963/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001964/// from-clause:
1965/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001966/// use_device_ptr-clause:
1967/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001968/// is_device_ptr-clause:
1969/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001970///
Alexey Bataev182227b2015-08-20 10:54:39 +00001971/// For 'linear' clause linear-list may have the following forms:
1972/// list
1973/// modifier(list)
1974/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001975OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001976 OpenMPClauseKind Kind,
1977 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001978 SourceLocation Loc = Tok.getLocation();
1979 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001980 SmallVector<Expr *, 4> Vars;
1981 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001982
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001983 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001984 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001985
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001986 if (ParseOnly)
1987 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001988 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001989 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001990 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1991 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1992 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001993}
1994