blob: d06e1014232fd0a8d51a5be6b80c7338072542e6 [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 ')'.
427 T.consumeClose();
428
429 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
430 "Unexpected number of commas!");
431
432 ExprResult Initializer = Actions.ActOnParenListExpr(
433 T.getOpenLocation(), T.getCloseLocation(), Exprs);
434 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
435 /*DirectInit=*/true);
436 }
437 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
438 // Parse C++0x braced-init-list.
439 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
440
441 ExprResult Init(ParseBraceInitializer());
442
443 if (Init.isInvalid()) {
444 Actions.ActOnInitializerError(OmpPrivParm);
445 } else {
446 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
447 /*DirectInit=*/true);
448 }
449 } else {
450 Actions.ActOnUninitializedDecl(OmpPrivParm);
451 }
452}
453
Alexey Bataev2af33e32016-04-07 12:45:37 +0000454namespace {
455/// RAII that recreates function context for correct parsing of clauses of
456/// 'declare simd' construct.
457/// OpenMP, 2.8.2 declare simd Construct
458/// The expressions appearing in the clauses of this directive are evaluated in
459/// the scope of the arguments of the function declaration or definition.
460class FNContextRAII final {
461 Parser &P;
462 Sema::CXXThisScopeRAII *ThisScope;
463 Parser::ParseScope *TempScope;
464 Parser::ParseScope *FnScope;
465 bool HasTemplateScope = false;
466 bool HasFunScope = false;
467 FNContextRAII() = delete;
468 FNContextRAII(const FNContextRAII &) = delete;
469 FNContextRAII &operator=(const FNContextRAII &) = delete;
470
471public:
472 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
473 Decl *D = *Ptr.get().begin();
474 NamedDecl *ND = dyn_cast<NamedDecl>(D);
475 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
476 Sema &Actions = P.getActions();
477
478 // Allow 'this' within late-parsed attributes.
479 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
480 ND && ND->isCXXInstanceMember());
481
482 // If the Decl is templatized, add template parameters to scope.
483 HasTemplateScope = D->isTemplateDecl();
484 TempScope =
485 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
486 if (HasTemplateScope)
487 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
488
489 // If the Decl is on a function, add function parameters to the scope.
490 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000491 FnScope = new Parser::ParseScope(
492 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
493 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000494 if (HasFunScope)
495 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
496 }
497 ~FNContextRAII() {
498 if (HasFunScope) {
499 P.getActions().ActOnExitFunctionContext();
500 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
501 }
502 if (HasTemplateScope)
503 TempScope->Exit();
504 delete FnScope;
505 delete TempScope;
506 delete ThisScope;
507 }
508};
509} // namespace
510
Alexey Bataevd93d3762016-04-12 09:35:56 +0000511/// Parses clauses for 'declare simd' directive.
512/// clause:
513/// 'inbranch' | 'notinbranch'
514/// 'simdlen' '(' <expr> ')'
515/// { 'uniform' '(' <argument_list> ')' }
516/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000517/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
518static bool parseDeclareSimdClauses(
519 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
520 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
521 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
522 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000523 SourceRange BSRange;
524 const Token &Tok = P.getCurToken();
525 bool IsError = false;
526 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
527 if (Tok.isNot(tok::identifier))
528 break;
529 OMPDeclareSimdDeclAttr::BranchStateTy Out;
530 IdentifierInfo *II = Tok.getIdentifierInfo();
531 StringRef ClauseName = II->getName();
532 // Parse 'inranch|notinbranch' clauses.
533 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
534 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
535 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
536 << ClauseName
537 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
538 IsError = true;
539 }
540 BS = Out;
541 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
542 P.ConsumeToken();
543 } else if (ClauseName.equals("simdlen")) {
544 if (SimdLen.isUsable()) {
545 P.Diag(Tok, diag::err_omp_more_one_clause)
546 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
547 IsError = true;
548 }
549 P.ConsumeToken();
550 SourceLocation RLoc;
551 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
552 if (SimdLen.isInvalid())
553 IsError = true;
554 } else {
555 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000556 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
557 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000558 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000559 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000560 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000561 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000562 else if (CKind == OMPC_linear)
563 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000564
565 P.ConsumeToken();
566 if (P.ParseOpenMPVarList(OMPD_declare_simd,
567 getOpenMPClauseKind(ClauseName), *Vars, Data))
568 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000569 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000570 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000571 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000572 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
573 Data.DepLinMapLoc))
574 Data.LinKind = OMPC_LINEAR_val;
575 LinModifiers.append(Linears.size() - LinModifiers.size(),
576 Data.LinKind);
577 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
578 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000579 } else
580 // TODO: add parsing of other clauses.
581 break;
582 }
583 // Skip ',' if any.
584 if (Tok.is(tok::comma))
585 P.ConsumeToken();
586 }
587 return IsError;
588}
589
Alexey Bataev2af33e32016-04-07 12:45:37 +0000590/// Parse clauses for '#pragma omp declare simd'.
591Parser::DeclGroupPtrTy
592Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
593 CachedTokens &Toks, SourceLocation Loc) {
594 PP.EnterToken(Tok);
595 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
596 // Consume the previously pushed token.
597 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
598
599 FNContextRAII FnContext(*this, Ptr);
600 OMPDeclareSimdDeclAttr::BranchStateTy BS =
601 OMPDeclareSimdDeclAttr::BS_Undefined;
602 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000603 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000604 SmallVector<Expr *, 4> Aligneds;
605 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000606 SmallVector<Expr *, 4> Linears;
607 SmallVector<unsigned, 4> LinModifiers;
608 SmallVector<Expr *, 4> Steps;
609 bool IsError =
610 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
611 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000612 // Need to check for extra tokens.
613 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
614 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
615 << getOpenMPDirectiveName(OMPD_declare_simd);
616 while (Tok.isNot(tok::annot_pragma_openmp_end))
617 ConsumeAnyToken();
618 }
619 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000620 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000621 if (IsError)
622 return Ptr;
623 return Actions.ActOnOpenMPDeclareSimdDirective(
624 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
625 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000626}
627
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000628/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000629///
630/// threadprivate-directive:
631/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000632/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000633///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000634/// declare-reduction-directive:
635/// annot_pragma_openmp 'declare' 'reduction' [...]
636/// annot_pragma_openmp_end
637///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000638/// declare-simd-directive:
639/// annot_pragma_openmp 'declare simd' {<clause> [,]}
640/// annot_pragma_openmp_end
641/// <function declaration/definition>
642///
643Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
644 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
645 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000646 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000647 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000648
Richard Smithaf3b3252017-05-18 19:21:48 +0000649 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000650 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000651
652 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000653 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000654 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000655 ThreadprivateListParserHelper Helper(this);
656 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000657 // The last seen token is annot_pragma_openmp_end - need to check for
658 // extra tokens.
659 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
660 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000661 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000662 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000663 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000665 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000666 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
667 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000668 }
669 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000670 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000671 case OMPD_declare_reduction:
672 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000673 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000674 // The last seen token is annot_pragma_openmp_end - need to check for
675 // extra tokens.
676 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
677 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
678 << getOpenMPDirectiveName(OMPD_declare_reduction);
679 while (Tok.isNot(tok::annot_pragma_openmp_end))
680 ConsumeAnyToken();
681 }
682 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000683 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000684 return Res;
685 }
686 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000687 case OMPD_declare_simd: {
688 // The syntax is:
689 // { #pragma omp declare simd }
690 // <function-declaration-or-definition>
691 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000692 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000693 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000694 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
695 Toks.push_back(Tok);
696 ConsumeAnyToken();
697 }
698 Toks.push_back(Tok);
699 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000700
701 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000702 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000703 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000704 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000705 // Here we expect to see some function declaration.
706 if (AS == AS_none) {
707 assert(TagType == DeclSpec::TST_unspecified);
708 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000709 ParsingDeclSpec PDS(*this);
710 Ptr = ParseExternalDeclaration(Attrs, &PDS);
711 } else {
712 Ptr =
713 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
714 }
715 }
716 if (!Ptr) {
717 Diag(Loc, diag::err_omp_decl_in_declare_simd);
718 return DeclGroupPtrTy();
719 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000720 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000721 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000722 case OMPD_declare_target: {
723 SourceLocation DTLoc = ConsumeAnyToken();
724 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000725 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000726 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000727 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
728 OMPDeclareTargetDeclAttr::MapTypeTy MT =
729 OMPDeclareTargetDeclAttr::MT_To;
730 if (Tok.is(tok::identifier)) {
731 IdentifierInfo *II = Tok.getIdentifierInfo();
732 StringRef ClauseName = II->getName();
733 // Parse 'to|link' clauses.
734 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
735 MT)) {
736 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
737 << ClauseName;
738 break;
739 }
740 ConsumeToken();
741 }
Alexey Bataev61908f652018-04-23 19:53:05 +0000742 auto &&Callback = [this, MT, &SameDirectiveDecls](
743 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000744 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
745 SameDirectiveDecls);
746 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000747 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
748 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000749 break;
750
751 // Consume optional ','.
752 if (Tok.is(tok::comma))
753 ConsumeToken();
754 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000755 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000756 ConsumeAnyToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000757 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
758 SameDirectiveDecls.end());
Alexey Bataev34f8a702018-03-28 14:28:54 +0000759 if (Decls.empty())
760 return DeclGroupPtrTy();
761 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000762 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000763
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000764 // Skip the last annot_pragma_openmp_end.
765 ConsumeAnyToken();
766
767 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
768 return DeclGroupPtrTy();
769
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000770 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000771 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000772 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
773 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000774 DeclGroupPtrTy Ptr;
775 // Here we expect to see some function declaration.
776 if (AS == AS_none) {
777 assert(TagType == DeclSpec::TST_unspecified);
778 MaybeParseCXX11Attributes(Attrs);
779 ParsingDeclSpec PDS(*this);
780 Ptr = ParseExternalDeclaration(Attrs, &PDS);
781 } else {
782 Ptr =
783 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
784 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000785 if (Ptr) {
786 DeclGroupRef Ref = Ptr.get();
787 Decls.append(Ref.begin(), Ref.end());
788 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000789 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
790 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000791 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000792 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000793 if (DKind != OMPD_end_declare_target)
794 TPA.Revert();
795 else
796 TPA.Commit();
797 }
798 }
799
800 if (DKind == OMPD_end_declare_target) {
801 ConsumeAnyToken();
802 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
803 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
804 << getOpenMPDirectiveName(OMPD_end_declare_target);
805 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
806 }
807 // Skip the last annot_pragma_openmp_end.
808 ConsumeAnyToken();
809 } else {
810 Diag(Tok, diag::err_expected_end_declare_target);
811 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
812 }
813 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000814 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000815 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000816 case OMPD_unknown:
817 Diag(Tok, diag::err_omp_unknown_directive);
818 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000819 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000820 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000822 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000823 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000824 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000825 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000826 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000827 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000828 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000829 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000830 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000831 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000832 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000833 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000834 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000835 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000836 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000837 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000838 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000839 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000840 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000841 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000842 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000843 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000844 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000845 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000846 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000847 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000848 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000849 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000850 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000851 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000852 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000853 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000854 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000855 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000856 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000857 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000858 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000859 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000860 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000861 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000862 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000863 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000864 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000865 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000866 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000867 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000868 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000869 break;
870 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000871 while (Tok.isNot(tok::annot_pragma_openmp_end))
872 ConsumeAnyToken();
873 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000874 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000875}
876
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000877/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000878///
879/// threadprivate-directive:
880/// annot_pragma_openmp 'threadprivate' simple-variable-list
881/// annot_pragma_openmp_end
882///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000883/// declare-reduction-directive:
884/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
885/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
886/// ('omp_priv' '=' <expression>|<function_call>) ')']
887/// annot_pragma_openmp_end
888///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000889/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000890/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000891/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
892/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000893/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000894/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000895/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000896/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000897/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000898/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000899/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000900/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000901/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000902/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000903/// 'teams distribute parallel for' | 'target teams' |
904/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000905/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000906/// 'target teams distribute parallel for simd' |
907/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000908/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000909///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000910StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000911 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000912 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000913 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000915 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000916 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000917 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
918 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000919 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000920 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000921 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000922 // Name of critical directive.
923 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000924 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000925 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000926 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000927
928 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000929 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000930 if (Allowed != ACK_Any) {
931 Diag(Tok, diag::err_omp_immediate_directive)
932 << getOpenMPDirectiveName(DKind) << 0;
933 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000934 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000935 ThreadprivateListParserHelper Helper(this);
936 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000937 // The last seen token is annot_pragma_openmp_end - need to check for
938 // extra tokens.
939 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
940 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000941 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000942 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000943 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000944 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
945 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000946 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
947 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000948 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000950 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000951 case OMPD_declare_reduction:
952 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000953 if (DeclGroupPtrTy Res =
954 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000955 // The last seen token is annot_pragma_openmp_end - need to check for
956 // extra tokens.
957 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
958 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
959 << getOpenMPDirectiveName(OMPD_declare_reduction);
960 while (Tok.isNot(tok::annot_pragma_openmp_end))
961 ConsumeAnyToken();
962 }
963 ConsumeAnyToken();
964 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +0000965 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000966 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +0000967 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000968 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000969 case OMPD_flush:
970 if (PP.LookAhead(0).is(tok::l_paren)) {
971 FlushHasClause = true;
972 // Push copy of the current token back to stream to properly parse
973 // pseudo-clause OMPFlushClause.
974 PP.EnterToken(Tok);
975 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000976 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000977 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000978 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000979 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000980 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000981 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000982 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000983 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000984 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000985 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000986 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000987 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000988 }
989 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000990 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000991 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000992 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000993 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000994 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000995 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000996 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000997 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000998 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000999 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001000 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001001 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001002 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001003 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001004 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001005 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001006 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001007 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001008 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001009 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001010 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001011 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001012 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001013 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001014 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001015 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001016 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001017 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001018 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001019 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001020 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001021 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001022 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001023 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001024 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001025 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001026 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001027 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001028 case OMPD_target_teams_distribute_parallel_for_simd:
1029 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001030 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001031 // Parse directive name of the 'critical' directive if any.
1032 if (DKind == OMPD_critical) {
1033 BalancedDelimiterTracker T(*this, tok::l_paren,
1034 tok::annot_pragma_openmp_end);
1035 if (!T.consumeOpen()) {
1036 if (Tok.isAnyIdentifier()) {
1037 DirName =
1038 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1039 ConsumeAnyToken();
1040 } else {
1041 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1042 }
1043 T.consumeClose();
1044 }
Alexey Bataev80909872015-07-02 11:25:17 +00001045 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001046 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001047 if (Tok.isNot(tok::annot_pragma_openmp_end))
1048 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001049 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001050
Alexey Bataevf29276e2014-06-18 04:14:57 +00001051 if (isOpenMPLoopDirective(DKind))
1052 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1053 if (isOpenMPSimdDirective(DKind))
1054 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1055 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001056 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001057
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001058 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001059 OpenMPClauseKind CKind =
1060 Tok.isAnnotation()
1061 ? OMPC_unknown
1062 : FlushHasClause ? OMPC_flush
1063 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001064 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001065 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001066 OMPClause *Clause =
1067 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001068 FirstClauses[CKind].setInt(true);
1069 if (Clause) {
1070 FirstClauses[CKind].setPointer(Clause);
1071 Clauses.push_back(Clause);
1072 }
1073
1074 // Skip ',' if any.
1075 if (Tok.is(tok::comma))
1076 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001077 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001078 }
1079 // End location of the directive.
1080 EndLoc = Tok.getLocation();
1081 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001082 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001083
Alexey Bataeveb482352015-12-18 05:05:56 +00001084 // OpenMP [2.13.8, ordered Construct, Syntax]
1085 // If the depend clause is specified, the ordered construct is a stand-alone
1086 // directive.
1087 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001088 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001089 Diag(Loc, diag::err_omp_immediate_directive)
1090 << getOpenMPDirectiveName(DKind) << 1
1091 << getOpenMPClauseName(OMPC_depend);
1092 }
1093 HasAssociatedStatement = false;
1094 }
1095
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001096 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001097 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001099 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001100 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1101 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1102 // should have at least one compound statement scope within it.
1103 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001104 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001105 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1106 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001107 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001108 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1109 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1110 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001111 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001112 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001113 Directive = Actions.ActOnOpenMPExecutableDirective(
1114 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1115 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001116
1117 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001118 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001119 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001120 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001121 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001122 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001123 case OMPD_declare_target:
1124 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001125 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001126 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001127 SkipUntil(tok::annot_pragma_openmp_end);
1128 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001129 case OMPD_unknown:
1130 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001131 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001132 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001133 }
1134 return Directive;
1135}
1136
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001137// Parses simple list:
1138// simple-variable-list:
1139// '(' id-expression {, id-expression} ')'
1140//
1141bool Parser::ParseOpenMPSimpleVarList(
1142 OpenMPDirectiveKind Kind,
1143 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1144 Callback,
1145 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001146 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001147 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001148 if (T.expectAndConsume(diag::err_expected_lparen_after,
1149 getOpenMPDirectiveName(Kind)))
1150 return true;
1151 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001152 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001153
1154 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001156 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001157 UnqualifiedId Name;
1158 // Read var name.
1159 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001161
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001162 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001163 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001164 IsCorrect = false;
1165 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001166 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001167 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001168 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001169 IsCorrect = false;
1170 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001171 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001172 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1173 Tok.isNot(tok::annot_pragma_openmp_end)) {
1174 IsCorrect = false;
1175 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001176 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001177 Diag(PrevTok.getLocation(), diag::err_expected)
1178 << tok::identifier
1179 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001180 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001181 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001182 }
1183 // Consume ','.
1184 if (Tok.is(tok::comma)) {
1185 ConsumeToken();
1186 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001187 }
1188
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001189 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001190 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001191 IsCorrect = false;
1192 }
1193
1194 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001195 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001196
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001197 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001198}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001200/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001201///
1202/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001203/// if-clause | final-clause | num_threads-clause | safelen-clause |
1204/// default-clause | private-clause | firstprivate-clause | shared-clause
1205/// | linear-clause | aligned-clause | collapse-clause |
1206/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001207/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001208/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001209/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001210/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001211/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001212/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001213/// from-clause | is_device_ptr-clause | task_reduction-clause |
1214/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001215///
1216OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1217 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001218 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001219 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001220 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001221 // Check if clause is allowed for the given directive.
1222 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001223 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1224 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001225 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001226 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001227 }
1228
1229 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001230 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001231 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001232 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001233 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001234 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001235 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001236 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001237 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001238 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001239 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001240 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001241 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001242 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001243 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001244 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001245 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001246 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001247 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001248 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001249 // OpenMP [2.9.1, target data construct, Restrictions]
1250 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001251 // OpenMP [2.11.1, task Construct, Restrictions]
1252 // At most one if clause can appear on the directive.
1253 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001254 // OpenMP [teams Construct, Restrictions]
1255 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001256 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001257 // OpenMP [2.9.1, task Construct, Restrictions]
1258 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001259 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1260 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001261 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1262 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001263 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001264 Diag(Tok, diag::err_omp_more_one_clause)
1265 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001266 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001267 }
1268
Alexey Bataev10e775f2015-07-30 11:36:16 +00001269 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001270 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001271 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001272 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001273 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001274 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001275 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001276 // OpenMP [2.14.3.1, Restrictions]
1277 // Only a single default clause may be specified on a parallel, task or
1278 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001279 // OpenMP [2.5, parallel Construct, Restrictions]
1280 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001281 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001282 Diag(Tok, diag::err_omp_more_one_clause)
1283 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001284 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001285 }
1286
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001287 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001288 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001289 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001290 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001291 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001292 // OpenMP [2.7.1, Restrictions, p. 3]
1293 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001294 // OpenMP [2.10.4, Restrictions, p. 106]
1295 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001296 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001297 Diag(Tok, diag::err_omp_more_one_clause)
1298 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001299 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001300 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001301 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001302
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001303 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001304 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001305 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001306 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001307 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001308 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001309 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001310 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001311 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001312 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001313 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001314 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001315 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001316 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001317 // OpenMP [2.7.1, Restrictions, p. 9]
1318 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001319 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1320 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001321 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001322 Diag(Tok, diag::err_omp_more_one_clause)
1323 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001324 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001325 }
1326
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001327 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001328 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001329 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001330 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001331 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001332 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001333 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001334 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001335 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001336 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001337 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001338 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001339 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001340 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001341 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001342 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001343 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001344 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001345 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001346 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001347 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001348 break;
1349 case OMPC_unknown:
1350 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001351 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001352 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001353 break;
1354 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001355 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001356 if (!WrongDirective)
1357 Diag(Tok, diag::err_omp_unexpected_clause)
1358 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001359 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001360 break;
1361 }
Craig Topper161e4db2014-05-21 06:02:52 +00001362 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001363}
1364
Alexey Bataev2af33e32016-04-07 12:45:37 +00001365/// Parses simple expression in parens for single-expression clauses of OpenMP
1366/// constructs.
1367/// \param RLoc Returned location of right paren.
1368ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1369 SourceLocation &RLoc) {
1370 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1371 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1372 return ExprError();
1373
1374 SourceLocation ELoc = Tok.getLocation();
1375 ExprResult LHS(ParseCastExpression(
1376 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1377 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1378 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1379
1380 // Parse ')'.
1381 T.consumeClose();
1382
1383 RLoc = T.getCloseLocation();
1384 return Val;
1385}
1386
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001387/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001388/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001389/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001390///
Alexey Bataev3778b602014-07-17 07:32:53 +00001391/// final-clause:
1392/// 'final' '(' expression ')'
1393///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001394/// num_threads-clause:
1395/// 'num_threads' '(' expression ')'
1396///
1397/// safelen-clause:
1398/// 'safelen' '(' expression ')'
1399///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001400/// simdlen-clause:
1401/// 'simdlen' '(' expression ')'
1402///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001403/// collapse-clause:
1404/// 'collapse' '(' expression ')'
1405///
Alexey Bataeva0569352015-12-01 10:17:31 +00001406/// priority-clause:
1407/// 'priority' '(' expression ')'
1408///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001409/// grainsize-clause:
1410/// 'grainsize' '(' expression ')'
1411///
Alexey Bataev382967a2015-12-08 12:06:20 +00001412/// num_tasks-clause:
1413/// 'num_tasks' '(' expression ')'
1414///
Alexey Bataev28c75412015-12-15 08:19:24 +00001415/// hint-clause:
1416/// 'hint' '(' expression ')'
1417///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001418OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1419 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001420 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001421 SourceLocation LLoc = Tok.getLocation();
1422 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001423
Alexey Bataev2af33e32016-04-07 12:45:37 +00001424 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001425
1426 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001427 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001428
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001429 if (ParseOnly)
1430 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001431 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001432}
1433
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001434/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001435///
1436/// default-clause:
1437/// 'default' '(' 'none' | 'shared' ')
1438///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001439/// proc_bind-clause:
1440/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1441///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001442OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1443 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001444 SourceLocation Loc = Tok.getLocation();
1445 SourceLocation LOpen = ConsumeToken();
1446 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001447 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001448 if (T.expectAndConsume(diag::err_expected_lparen_after,
1449 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001450 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001451
Alexey Bataeva55ed262014-05-28 06:15:33 +00001452 unsigned Type = getOpenMPSimpleClauseType(
1453 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001454 SourceLocation TypeLoc = Tok.getLocation();
1455 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1456 Tok.isNot(tok::annot_pragma_openmp_end))
1457 ConsumeAnyToken();
1458
1459 // Parse ')'.
1460 T.consumeClose();
1461
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001462 if (ParseOnly)
1463 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001464 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1465 Tok.getLocation());
1466}
1467
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001468/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001469///
1470/// ordered-clause:
1471/// 'ordered'
1472///
Alexey Bataev236070f2014-06-20 11:19:47 +00001473/// nowait-clause:
1474/// 'nowait'
1475///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001476/// untied-clause:
1477/// 'untied'
1478///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001479/// mergeable-clause:
1480/// 'mergeable'
1481///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001482/// read-clause:
1483/// 'read'
1484///
Alexey Bataev346265e2015-09-25 10:37:12 +00001485/// threads-clause:
1486/// 'threads'
1487///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001488/// simd-clause:
1489/// 'simd'
1490///
Alexey Bataevb825de12015-12-07 10:51:44 +00001491/// nogroup-clause:
1492/// 'nogroup'
1493///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001494OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001495 SourceLocation Loc = Tok.getLocation();
1496 ConsumeAnyToken();
1497
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001498 if (ParseOnly)
1499 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001500 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1501}
1502
1503
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001504/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001505/// argument like 'schedule' or 'dist_schedule'.
1506///
1507/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001508/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1509/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001510///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001511/// if-clause:
1512/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1513///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001514/// defaultmap:
1515/// 'defaultmap' '(' modifier ':' kind ')'
1516///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001517OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1518 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001519 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001520 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001521 // Parse '('.
1522 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1523 if (T.expectAndConsume(diag::err_expected_lparen_after,
1524 getOpenMPClauseName(Kind)))
1525 return nullptr;
1526
1527 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001528 SmallVector<unsigned, 4> Arg;
1529 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001530 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001531 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1532 Arg.resize(NumberOfElements);
1533 KLoc.resize(NumberOfElements);
1534 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1535 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1536 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001537 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001538 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001539 if (KindModifier > OMPC_SCHEDULE_unknown) {
1540 // Parse 'modifier'
1541 Arg[Modifier1] = KindModifier;
1542 KLoc[Modifier1] = Tok.getLocation();
1543 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1544 Tok.isNot(tok::annot_pragma_openmp_end))
1545 ConsumeAnyToken();
1546 if (Tok.is(tok::comma)) {
1547 // Parse ',' 'modifier'
1548 ConsumeAnyToken();
1549 KindModifier = getOpenMPSimpleClauseType(
1550 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1551 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1552 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001553 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001554 KLoc[Modifier2] = Tok.getLocation();
1555 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1556 Tok.isNot(tok::annot_pragma_openmp_end))
1557 ConsumeAnyToken();
1558 }
1559 // Parse ':'
1560 if (Tok.is(tok::colon))
1561 ConsumeAnyToken();
1562 else
1563 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1564 KindModifier = getOpenMPSimpleClauseType(
1565 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1566 }
1567 Arg[ScheduleKind] = KindModifier;
1568 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001569 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1570 Tok.isNot(tok::annot_pragma_openmp_end))
1571 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001572 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1573 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1574 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001575 Tok.is(tok::comma))
1576 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001577 } else if (Kind == OMPC_dist_schedule) {
1578 Arg.push_back(getOpenMPSimpleClauseType(
1579 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1580 KLoc.push_back(Tok.getLocation());
1581 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1582 Tok.isNot(tok::annot_pragma_openmp_end))
1583 ConsumeAnyToken();
1584 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1585 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001586 } else if (Kind == OMPC_defaultmap) {
1587 // Get a defaultmap modifier
1588 Arg.push_back(getOpenMPSimpleClauseType(
1589 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1590 KLoc.push_back(Tok.getLocation());
1591 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1592 Tok.isNot(tok::annot_pragma_openmp_end))
1593 ConsumeAnyToken();
1594 // Parse ':'
1595 if (Tok.is(tok::colon))
1596 ConsumeAnyToken();
1597 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1598 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1599 // Get a defaultmap kind
1600 Arg.push_back(getOpenMPSimpleClauseType(
1601 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1602 KLoc.push_back(Tok.getLocation());
1603 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1604 Tok.isNot(tok::annot_pragma_openmp_end))
1605 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001606 } else {
1607 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001608 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001609 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001610 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001611 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001612 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001613 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1614 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001615 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001616 } else {
1617 TPA.Revert();
1618 Arg.back() = OMPD_unknown;
1619 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001620 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001621 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001622 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001623 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001624
Carlo Bertollib4adf552016-01-15 18:50:31 +00001625 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1626 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1627 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001628 if (NeedAnExpression) {
1629 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001630 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1631 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001632 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001633 }
1634
1635 // Parse ')'.
1636 T.consumeClose();
1637
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001638 if (NeedAnExpression && Val.isInvalid())
1639 return nullptr;
1640
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001641 if (ParseOnly)
1642 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001643 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001644 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001645 T.getCloseLocation());
1646}
1647
Alexey Bataevc5e02582014-06-16 07:08:35 +00001648static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1649 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001650 if (ReductionIdScopeSpec.isEmpty()) {
1651 auto OOK = OO_None;
1652 switch (P.getCurToken().getKind()) {
1653 case tok::plus:
1654 OOK = OO_Plus;
1655 break;
1656 case tok::minus:
1657 OOK = OO_Minus;
1658 break;
1659 case tok::star:
1660 OOK = OO_Star;
1661 break;
1662 case tok::amp:
1663 OOK = OO_Amp;
1664 break;
1665 case tok::pipe:
1666 OOK = OO_Pipe;
1667 break;
1668 case tok::caret:
1669 OOK = OO_Caret;
1670 break;
1671 case tok::ampamp:
1672 OOK = OO_AmpAmp;
1673 break;
1674 case tok::pipepipe:
1675 OOK = OO_PipePipe;
1676 break;
1677 default:
1678 break;
1679 }
1680 if (OOK != OO_None) {
1681 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001682 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001683 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1684 return false;
1685 }
1686 }
1687 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1688 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001689 /*AllowConstructorName*/ false,
1690 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001691 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001692}
1693
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001694/// Parses clauses with list.
1695bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1696 OpenMPClauseKind Kind,
1697 SmallVectorImpl<Expr *> &Vars,
1698 OpenMPVarListDataTy &Data) {
1699 UnqualifiedId UnqualifiedReductionId;
1700 bool InvalidReductionId = false;
1701 bool MapTypeModifierSpecified = false;
1702
1703 // Parse '('.
1704 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1705 if (T.expectAndConsume(diag::err_expected_lparen_after,
1706 getOpenMPClauseName(Kind)))
1707 return true;
1708
1709 bool NeedRParenForLinear = false;
1710 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1711 tok::annot_pragma_openmp_end);
1712 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001713 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1714 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001715 ColonProtectionRAIIObject ColonRAII(*this);
1716 if (getLangOpts().CPlusPlus)
1717 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1718 /*ObjectType=*/nullptr,
1719 /*EnteringContext=*/false);
1720 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1721 UnqualifiedReductionId);
1722 if (InvalidReductionId) {
1723 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1724 StopBeforeMatch);
1725 }
1726 if (Tok.is(tok::colon))
1727 Data.ColonLoc = ConsumeToken();
1728 else
1729 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1730 if (!InvalidReductionId)
1731 Data.ReductionId =
1732 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1733 } else if (Kind == OMPC_depend) {
1734 // Handle dependency type for depend clause.
1735 ColonProtectionRAIIObject ColonRAII(*this);
1736 Data.DepKind =
1737 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1738 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1739 Data.DepLinMapLoc = Tok.getLocation();
1740
1741 if (Data.DepKind == OMPC_DEPEND_unknown) {
1742 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1743 StopBeforeMatch);
1744 } else {
1745 ConsumeToken();
1746 // Special processing for depend(source) clause.
1747 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1748 // Parse ')'.
1749 T.consumeClose();
1750 return false;
1751 }
1752 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001753 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001754 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001755 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001756 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1757 : diag::warn_pragma_expected_colon)
1758 << "dependency type";
1759 }
1760 } else if (Kind == OMPC_linear) {
1761 // Try to parse modifier if any.
1762 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1763 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1764 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1765 Data.DepLinMapLoc = ConsumeToken();
1766 LinearT.consumeOpen();
1767 NeedRParenForLinear = true;
1768 }
1769 } else if (Kind == OMPC_map) {
1770 // Handle map type for map clause.
1771 ColonProtectionRAIIObject ColonRAII(*this);
1772
1773 /// The map clause modifier token can be either a identifier or the C++
1774 /// delete keyword.
1775 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1776 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1777 };
1778
1779 // The first identifier may be a list item, a map-type or a
1780 // map-type-modifier. The map modifier can also be delete which has the same
1781 // spelling of the C++ delete keyword.
1782 Data.MapType =
1783 IsMapClauseModifierToken(Tok)
1784 ? static_cast<OpenMPMapClauseKind>(
1785 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1786 : OMPC_MAP_unknown;
1787 Data.DepLinMapLoc = Tok.getLocation();
1788 bool ColonExpected = false;
1789
1790 if (IsMapClauseModifierToken(Tok)) {
1791 if (PP.LookAhead(0).is(tok::colon)) {
1792 if (Data.MapType == OMPC_MAP_unknown)
1793 Diag(Tok, diag::err_omp_unknown_map_type);
1794 else if (Data.MapType == OMPC_MAP_always)
1795 Diag(Tok, diag::err_omp_map_type_missing);
1796 ConsumeToken();
1797 } else if (PP.LookAhead(0).is(tok::comma)) {
1798 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1799 PP.LookAhead(2).is(tok::colon)) {
1800 Data.MapTypeModifier = Data.MapType;
1801 if (Data.MapTypeModifier != OMPC_MAP_always) {
1802 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1803 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001804 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001805 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001806 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001807
1808 ConsumeToken();
1809 ConsumeToken();
1810
1811 Data.MapType =
1812 IsMapClauseModifierToken(Tok)
1813 ? static_cast<OpenMPMapClauseKind>(
1814 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1815 : OMPC_MAP_unknown;
1816 if (Data.MapType == OMPC_MAP_unknown ||
1817 Data.MapType == OMPC_MAP_always)
1818 Diag(Tok, diag::err_omp_unknown_map_type);
1819 ConsumeToken();
1820 } else {
1821 Data.MapType = OMPC_MAP_tofrom;
1822 Data.IsMapTypeImplicit = true;
1823 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001824 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1825 if (PP.LookAhead(1).is(tok::colon)) {
1826 Data.MapTypeModifier = Data.MapType;
1827 if (Data.MapTypeModifier != OMPC_MAP_always) {
1828 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1829 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001830 } else {
Carlo Bertollid8844b92017-05-03 15:28:48 +00001831 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001832 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001833
1834 ConsumeToken();
1835
1836 Data.MapType =
1837 IsMapClauseModifierToken(Tok)
1838 ? static_cast<OpenMPMapClauseKind>(
1839 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1840 : OMPC_MAP_unknown;
1841 if (Data.MapType == OMPC_MAP_unknown ||
1842 Data.MapType == OMPC_MAP_always)
1843 Diag(Tok, diag::err_omp_unknown_map_type);
1844 ConsumeToken();
1845 } else {
1846 Data.MapType = OMPC_MAP_tofrom;
1847 Data.IsMapTypeImplicit = true;
1848 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001849 } else {
1850 Data.MapType = OMPC_MAP_tofrom;
1851 Data.IsMapTypeImplicit = true;
1852 }
1853 } else {
1854 Data.MapType = OMPC_MAP_tofrom;
1855 Data.IsMapTypeImplicit = true;
1856 }
1857
1858 if (Tok.is(tok::colon))
1859 Data.ColonLoc = ConsumeToken();
1860 else if (ColonExpected)
1861 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1862 }
1863
Alexey Bataevfa312f32017-07-21 18:48:21 +00001864 bool IsComma =
1865 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1866 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1867 (Kind == OMPC_reduction && !InvalidReductionId) ||
1868 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1869 (!MapTypeModifierSpecified ||
1870 Data.MapTypeModifier == OMPC_MAP_always)) ||
1871 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001872 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1873 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1874 Tok.isNot(tok::annot_pragma_openmp_end))) {
1875 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1876 // Parse variable
1877 ExprResult VarExpr =
1878 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001879 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001880 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001881 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001882 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1883 StopBeforeMatch);
1884 }
1885 // Skip ',' if any
1886 IsComma = Tok.is(tok::comma);
1887 if (IsComma)
1888 ConsumeToken();
1889 else if (Tok.isNot(tok::r_paren) &&
1890 Tok.isNot(tok::annot_pragma_openmp_end) &&
1891 (!MayHaveTail || Tok.isNot(tok::colon)))
1892 Diag(Tok, diag::err_omp_expected_punc)
1893 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1894 : getOpenMPClauseName(Kind))
1895 << (Kind == OMPC_flush);
1896 }
1897
1898 // Parse ')' for linear clause with modifier.
1899 if (NeedRParenForLinear)
1900 LinearT.consumeClose();
1901
1902 // Parse ':' linear-step (or ':' alignment).
1903 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1904 if (MustHaveTail) {
1905 Data.ColonLoc = Tok.getLocation();
1906 SourceLocation ELoc = ConsumeToken();
1907 ExprResult Tail = ParseAssignmentExpression();
1908 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1909 if (Tail.isUsable())
1910 Data.TailExpr = Tail.get();
1911 else
1912 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1913 StopBeforeMatch);
1914 }
1915
1916 // Parse ')'.
1917 T.consumeClose();
Alexey Bataev61908f652018-04-23 19:53:05 +00001918 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1919 Vars.empty()) ||
1920 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1921 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001922}
1923
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001924/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001925/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1926/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001927///
1928/// private-clause:
1929/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001930/// firstprivate-clause:
1931/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001932/// lastprivate-clause:
1933/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001934/// shared-clause:
1935/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001936/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001937/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001938/// aligned-clause:
1939/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001940/// reduction-clause:
1941/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001942/// task_reduction-clause:
1943/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001944/// in_reduction-clause:
1945/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001946/// copyprivate-clause:
1947/// 'copyprivate' '(' list ')'
1948/// flush-clause:
1949/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001950/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001951/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001952/// map-clause:
1953/// 'map' '(' [ [ always , ]
1954/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001955/// to-clause:
1956/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001957/// from-clause:
1958/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001959/// use_device_ptr-clause:
1960/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001961/// is_device_ptr-clause:
1962/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001963///
Alexey Bataev182227b2015-08-20 10:54:39 +00001964/// For 'linear' clause linear-list may have the following forms:
1965/// list
1966/// modifier(list)
1967/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001968OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001969 OpenMPClauseKind Kind,
1970 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001971 SourceLocation Loc = Tok.getLocation();
1972 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001973 SmallVector<Expr *, 4> Vars;
1974 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001975
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001976 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001977 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001978
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001979 if (ParseOnly)
1980 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001981 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001982 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1983 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1984 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1985 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001986}
1987