blob: c53eae3067e223cd2cf85b6bf86cd0756bc27b18 [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
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000418 SourceLocation LParLoc = T.getOpenLocation();
419 if (ParseExpressionList(
420 Exprs, CommaLocs, [this, OmpPrivParm, LParLoc, &Exprs] {
421 Actions.CodeCompleteConstructor(
422 getCurScope(),
423 OmpPrivParm->getType()->getCanonicalTypeInternal(),
424 OmpPrivParm->getLocation(), Exprs, LParLoc);
425 })) {
Alexey Bataev070f43a2017-09-06 14:49:58 +0000426 Actions.ActOnInitializerError(OmpPrivParm);
427 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
428 } else {
429 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000430 SourceLocation RLoc = Tok.getLocation();
431 if (!T.consumeClose())
432 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000433
434 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
435 "Unexpected number of commas!");
436
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000437 ExprResult Initializer =
438 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000439 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
440 /*DirectInit=*/true);
441 }
442 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
443 // Parse C++0x braced-init-list.
444 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
445
446 ExprResult Init(ParseBraceInitializer());
447
448 if (Init.isInvalid()) {
449 Actions.ActOnInitializerError(OmpPrivParm);
450 } else {
451 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
452 /*DirectInit=*/true);
453 }
454 } else {
455 Actions.ActOnUninitializedDecl(OmpPrivParm);
456 }
457}
458
Alexey Bataev2af33e32016-04-07 12:45:37 +0000459namespace {
460/// RAII that recreates function context for correct parsing of clauses of
461/// 'declare simd' construct.
462/// OpenMP, 2.8.2 declare simd Construct
463/// The expressions appearing in the clauses of this directive are evaluated in
464/// the scope of the arguments of the function declaration or definition.
465class FNContextRAII final {
466 Parser &P;
467 Sema::CXXThisScopeRAII *ThisScope;
468 Parser::ParseScope *TempScope;
469 Parser::ParseScope *FnScope;
470 bool HasTemplateScope = false;
471 bool HasFunScope = false;
472 FNContextRAII() = delete;
473 FNContextRAII(const FNContextRAII &) = delete;
474 FNContextRAII &operator=(const FNContextRAII &) = delete;
475
476public:
477 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
478 Decl *D = *Ptr.get().begin();
479 NamedDecl *ND = dyn_cast<NamedDecl>(D);
480 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
481 Sema &Actions = P.getActions();
482
483 // Allow 'this' within late-parsed attributes.
484 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
485 ND && ND->isCXXInstanceMember());
486
487 // If the Decl is templatized, add template parameters to scope.
488 HasTemplateScope = D->isTemplateDecl();
489 TempScope =
490 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
491 if (HasTemplateScope)
492 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
493
494 // If the Decl is on a function, add function parameters to the scope.
495 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000496 FnScope = new Parser::ParseScope(
497 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
498 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000499 if (HasFunScope)
500 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
501 }
502 ~FNContextRAII() {
503 if (HasFunScope) {
504 P.getActions().ActOnExitFunctionContext();
505 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
506 }
507 if (HasTemplateScope)
508 TempScope->Exit();
509 delete FnScope;
510 delete TempScope;
511 delete ThisScope;
512 }
513};
514} // namespace
515
Alexey Bataevd93d3762016-04-12 09:35:56 +0000516/// Parses clauses for 'declare simd' directive.
517/// clause:
518/// 'inbranch' | 'notinbranch'
519/// 'simdlen' '(' <expr> ')'
520/// { 'uniform' '(' <argument_list> ')' }
521/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000522/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
523static bool parseDeclareSimdClauses(
524 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
525 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
526 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
527 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000528 SourceRange BSRange;
529 const Token &Tok = P.getCurToken();
530 bool IsError = false;
531 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
532 if (Tok.isNot(tok::identifier))
533 break;
534 OMPDeclareSimdDeclAttr::BranchStateTy Out;
535 IdentifierInfo *II = Tok.getIdentifierInfo();
536 StringRef ClauseName = II->getName();
537 // Parse 'inranch|notinbranch' clauses.
538 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
539 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
540 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
541 << ClauseName
542 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
543 IsError = true;
544 }
545 BS = Out;
546 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
547 P.ConsumeToken();
548 } else if (ClauseName.equals("simdlen")) {
549 if (SimdLen.isUsable()) {
550 P.Diag(Tok, diag::err_omp_more_one_clause)
551 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
552 IsError = true;
553 }
554 P.ConsumeToken();
555 SourceLocation RLoc;
556 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
557 if (SimdLen.isInvalid())
558 IsError = true;
559 } else {
560 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000561 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
562 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000563 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000564 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000565 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000566 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000567 else if (CKind == OMPC_linear)
568 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000569
570 P.ConsumeToken();
571 if (P.ParseOpenMPVarList(OMPD_declare_simd,
572 getOpenMPClauseKind(ClauseName), *Vars, Data))
573 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000574 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000575 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000576 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000577 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
578 Data.DepLinMapLoc))
579 Data.LinKind = OMPC_LINEAR_val;
580 LinModifiers.append(Linears.size() - LinModifiers.size(),
581 Data.LinKind);
582 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
583 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000584 } else
585 // TODO: add parsing of other clauses.
586 break;
587 }
588 // Skip ',' if any.
589 if (Tok.is(tok::comma))
590 P.ConsumeToken();
591 }
592 return IsError;
593}
594
Alexey Bataev2af33e32016-04-07 12:45:37 +0000595/// Parse clauses for '#pragma omp declare simd'.
596Parser::DeclGroupPtrTy
597Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
598 CachedTokens &Toks, SourceLocation Loc) {
599 PP.EnterToken(Tok);
600 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
601 // Consume the previously pushed token.
602 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
603
604 FNContextRAII FnContext(*this, Ptr);
605 OMPDeclareSimdDeclAttr::BranchStateTy BS =
606 OMPDeclareSimdDeclAttr::BS_Undefined;
607 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000608 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000609 SmallVector<Expr *, 4> Aligneds;
610 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000611 SmallVector<Expr *, 4> Linears;
612 SmallVector<unsigned, 4> LinModifiers;
613 SmallVector<Expr *, 4> Steps;
614 bool IsError =
615 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
616 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000617 // Need to check for extra tokens.
618 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
619 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
620 << getOpenMPDirectiveName(OMPD_declare_simd);
621 while (Tok.isNot(tok::annot_pragma_openmp_end))
622 ConsumeAnyToken();
623 }
624 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000625 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000626 if (IsError)
627 return Ptr;
628 return Actions.ActOnOpenMPDeclareSimdDirective(
629 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
630 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000631}
632
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000633/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000634///
635/// threadprivate-directive:
636/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000637/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000638///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000639/// declare-reduction-directive:
640/// annot_pragma_openmp 'declare' 'reduction' [...]
641/// annot_pragma_openmp_end
642///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000643/// declare-simd-directive:
644/// annot_pragma_openmp 'declare simd' {<clause> [,]}
645/// annot_pragma_openmp_end
646/// <function declaration/definition>
647///
648Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
649 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
650 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000651 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000652 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000653
Richard Smithaf3b3252017-05-18 19:21:48 +0000654 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000655 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000656
657 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000658 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000659 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000660 ThreadprivateListParserHelper Helper(this);
661 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000662 // The last seen token is annot_pragma_openmp_end - need to check for
663 // extra tokens.
664 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
665 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000666 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000667 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000668 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000669 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000670 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000671 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
672 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000673 }
674 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000675 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000676 case OMPD_declare_reduction:
677 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000678 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000679 // The last seen token is annot_pragma_openmp_end - need to check for
680 // extra tokens.
681 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
682 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
683 << getOpenMPDirectiveName(OMPD_declare_reduction);
684 while (Tok.isNot(tok::annot_pragma_openmp_end))
685 ConsumeAnyToken();
686 }
687 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000688 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000689 return Res;
690 }
691 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000692 case OMPD_declare_simd: {
693 // The syntax is:
694 // { #pragma omp declare simd }
695 // <function-declaration-or-definition>
696 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000697 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000698 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000699 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
700 Toks.push_back(Tok);
701 ConsumeAnyToken();
702 }
703 Toks.push_back(Tok);
704 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000705
706 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000707 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000708 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000709 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000710 // Here we expect to see some function declaration.
711 if (AS == AS_none) {
712 assert(TagType == DeclSpec::TST_unspecified);
713 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000714 ParsingDeclSpec PDS(*this);
715 Ptr = ParseExternalDeclaration(Attrs, &PDS);
716 } else {
717 Ptr =
718 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
719 }
720 }
721 if (!Ptr) {
722 Diag(Loc, diag::err_omp_decl_in_declare_simd);
723 return DeclGroupPtrTy();
724 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000725 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000726 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000727 case OMPD_declare_target: {
728 SourceLocation DTLoc = ConsumeAnyToken();
729 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000730 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000731 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000732 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
733 OMPDeclareTargetDeclAttr::MapTypeTy MT =
734 OMPDeclareTargetDeclAttr::MT_To;
735 if (Tok.is(tok::identifier)) {
736 IdentifierInfo *II = Tok.getIdentifierInfo();
737 StringRef ClauseName = II->getName();
738 // Parse 'to|link' clauses.
739 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
740 MT)) {
741 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
742 << ClauseName;
743 break;
744 }
745 ConsumeToken();
746 }
Alexey Bataev61908f652018-04-23 19:53:05 +0000747 auto &&Callback = [this, MT, &SameDirectiveDecls](
748 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000749 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
750 SameDirectiveDecls);
751 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000752 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
753 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000754 break;
755
756 // Consume optional ','.
757 if (Tok.is(tok::comma))
758 ConsumeToken();
759 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000760 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000761 ConsumeAnyToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000762 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
763 SameDirectiveDecls.end());
Alexey Bataev34f8a702018-03-28 14:28:54 +0000764 if (Decls.empty())
765 return DeclGroupPtrTy();
766 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000767 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000768
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000769 // Skip the last annot_pragma_openmp_end.
770 ConsumeAnyToken();
771
772 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
773 return DeclGroupPtrTy();
774
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000775 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000776 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000777 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
778 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000779 DeclGroupPtrTy Ptr;
780 // Here we expect to see some function declaration.
781 if (AS == AS_none) {
782 assert(TagType == DeclSpec::TST_unspecified);
783 MaybeParseCXX11Attributes(Attrs);
784 ParsingDeclSpec PDS(*this);
785 Ptr = ParseExternalDeclaration(Attrs, &PDS);
786 } else {
787 Ptr =
788 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
789 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000790 if (Ptr) {
791 DeclGroupRef Ref = Ptr.get();
792 Decls.append(Ref.begin(), Ref.end());
793 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000794 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
795 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000796 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000797 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000798 if (DKind != OMPD_end_declare_target)
799 TPA.Revert();
800 else
801 TPA.Commit();
802 }
803 }
804
805 if (DKind == OMPD_end_declare_target) {
806 ConsumeAnyToken();
807 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
808 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
809 << getOpenMPDirectiveName(OMPD_end_declare_target);
810 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
811 }
812 // Skip the last annot_pragma_openmp_end.
813 ConsumeAnyToken();
814 } else {
815 Diag(Tok, diag::err_expected_end_declare_target);
816 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
817 }
818 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000819 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000820 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000821 case OMPD_unknown:
822 Diag(Tok, diag::err_omp_unknown_directive);
823 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000824 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000825 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000826 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000827 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000828 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000829 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000830 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000831 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000832 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000833 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000834 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000835 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000836 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000837 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000838 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000839 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000840 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000841 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000842 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000843 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000844 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000845 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000846 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000847 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000848 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000849 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000850 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000851 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000852 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000853 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000854 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000855 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000856 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000857 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000858 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000859 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000860 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000861 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000862 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000863 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000864 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000865 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000866 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000867 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000868 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000869 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000870 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000871 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000872 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000873 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000874 break;
875 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000876 while (Tok.isNot(tok::annot_pragma_openmp_end))
877 ConsumeAnyToken();
878 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000879 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000880}
881
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000882/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000883///
884/// threadprivate-directive:
885/// annot_pragma_openmp 'threadprivate' simple-variable-list
886/// annot_pragma_openmp_end
887///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000888/// declare-reduction-directive:
889/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
890/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
891/// ('omp_priv' '=' <expression>|<function_call>) ')']
892/// annot_pragma_openmp_end
893///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000894/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000895/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000896/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
897/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000898/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000899/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000900/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000901/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000902/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000903/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000904/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000905/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000906/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000907/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000908/// 'teams distribute parallel for' | 'target teams' |
909/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000910/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000911/// 'target teams distribute parallel for simd' |
912/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000913/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000915StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000916 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000917 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000918 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000919 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000920 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000921 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000922 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
923 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000924 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000925 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000926 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000927 // Name of critical directive.
928 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000930 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000931 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000932
933 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000934 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000935 if (Allowed != ACK_Any) {
936 Diag(Tok, diag::err_omp_immediate_directive)
937 << getOpenMPDirectiveName(DKind) << 0;
938 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000939 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000940 ThreadprivateListParserHelper Helper(this);
941 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000942 // The last seen token is annot_pragma_openmp_end - need to check for
943 // extra tokens.
944 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
945 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000946 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000947 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000948 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000949 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
950 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000951 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
952 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000953 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000955 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000956 case OMPD_declare_reduction:
957 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000958 if (DeclGroupPtrTy Res =
959 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000960 // The last seen token is annot_pragma_openmp_end - need to check for
961 // extra tokens.
962 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
963 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
964 << getOpenMPDirectiveName(OMPD_declare_reduction);
965 while (Tok.isNot(tok::annot_pragma_openmp_end))
966 ConsumeAnyToken();
967 }
968 ConsumeAnyToken();
969 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +0000970 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000971 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +0000972 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000973 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000974 case OMPD_flush:
975 if (PP.LookAhead(0).is(tok::l_paren)) {
976 FlushHasClause = true;
977 // Push copy of the current token back to stream to properly parse
978 // pseudo-clause OMPFlushClause.
979 PP.EnterToken(Tok);
980 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000981 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000982 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000983 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000984 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000985 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000986 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000987 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000988 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000989 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000990 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000991 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000992 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000993 }
994 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000995 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000996 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000997 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000998 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000999 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001000 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001001 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001002 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001003 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001004 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001005 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001006 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001007 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001008 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001009 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001010 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001011 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001012 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001013 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001014 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001015 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001016 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001017 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001018 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001019 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001020 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001021 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001022 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001023 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001024 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001025 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001026 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001027 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001028 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001029 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001030 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001031 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001032 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001033 case OMPD_target_teams_distribute_parallel_for_simd:
1034 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001035 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001036 // Parse directive name of the 'critical' directive if any.
1037 if (DKind == OMPD_critical) {
1038 BalancedDelimiterTracker T(*this, tok::l_paren,
1039 tok::annot_pragma_openmp_end);
1040 if (!T.consumeOpen()) {
1041 if (Tok.isAnyIdentifier()) {
1042 DirName =
1043 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1044 ConsumeAnyToken();
1045 } else {
1046 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1047 }
1048 T.consumeClose();
1049 }
Alexey Bataev80909872015-07-02 11:25:17 +00001050 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001051 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001052 if (Tok.isNot(tok::annot_pragma_openmp_end))
1053 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001054 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001055
Alexey Bataevf29276e2014-06-18 04:14:57 +00001056 if (isOpenMPLoopDirective(DKind))
1057 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1058 if (isOpenMPSimdDirective(DKind))
1059 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1060 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001061 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001062
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001063 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001064 OpenMPClauseKind CKind =
1065 Tok.isAnnotation()
1066 ? OMPC_unknown
1067 : FlushHasClause ? OMPC_flush
1068 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001069 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001070 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001071 OMPClause *Clause =
1072 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073 FirstClauses[CKind].setInt(true);
1074 if (Clause) {
1075 FirstClauses[CKind].setPointer(Clause);
1076 Clauses.push_back(Clause);
1077 }
1078
1079 // Skip ',' if any.
1080 if (Tok.is(tok::comma))
1081 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001082 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001083 }
1084 // End location of the directive.
1085 EndLoc = Tok.getLocation();
1086 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001087 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001088
Alexey Bataeveb482352015-12-18 05:05:56 +00001089 // OpenMP [2.13.8, ordered Construct, Syntax]
1090 // If the depend clause is specified, the ordered construct is a stand-alone
1091 // directive.
1092 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001093 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001094 Diag(Loc, diag::err_omp_immediate_directive)
1095 << getOpenMPDirectiveName(DKind) << 1
1096 << getOpenMPClauseName(OMPC_depend);
1097 }
1098 HasAssociatedStatement = false;
1099 }
1100
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001101 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001102 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001103 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001104 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001105 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1106 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1107 // should have at least one compound statement scope within it.
1108 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001109 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001110 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1111 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001112 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001113 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1114 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1115 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001116 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001117 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001118 Directive = Actions.ActOnOpenMPExecutableDirective(
1119 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1120 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001121
1122 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001123 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001124 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001125 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001126 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001127 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001128 case OMPD_declare_target:
1129 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001130 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001131 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001132 SkipUntil(tok::annot_pragma_openmp_end);
1133 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001134 case OMPD_unknown:
1135 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001136 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001137 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001138 }
1139 return Directive;
1140}
1141
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001142// Parses simple list:
1143// simple-variable-list:
1144// '(' id-expression {, id-expression} ')'
1145//
1146bool Parser::ParseOpenMPSimpleVarList(
1147 OpenMPDirectiveKind Kind,
1148 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1149 Callback,
1150 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001151 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001152 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001153 if (T.expectAndConsume(diag::err_expected_lparen_after,
1154 getOpenMPDirectiveName(Kind)))
1155 return true;
1156 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001157 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001158
1159 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001161 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001162 UnqualifiedId Name;
1163 // Read var name.
1164 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001165 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001166
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001168 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
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);
Richard Smith35845152017-02-07 01:37:30 +00001172 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001173 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001174 IsCorrect = false;
1175 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001176 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1178 Tok.isNot(tok::annot_pragma_openmp_end)) {
1179 IsCorrect = false;
1180 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001181 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001182 Diag(PrevTok.getLocation(), diag::err_expected)
1183 << tok::identifier
1184 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001185 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001186 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001187 }
1188 // Consume ','.
1189 if (Tok.is(tok::comma)) {
1190 ConsumeToken();
1191 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001192 }
1193
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001194 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001195 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001196 IsCorrect = false;
1197 }
1198
1199 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001202 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001203}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001204
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001205/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206///
1207/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001208/// if-clause | final-clause | num_threads-clause | safelen-clause |
1209/// default-clause | private-clause | firstprivate-clause | shared-clause
1210/// | linear-clause | aligned-clause | collapse-clause |
1211/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001212/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001213/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001214/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001215/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001216/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001217/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001218/// from-clause | is_device_ptr-clause | task_reduction-clause |
1219/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001220///
1221OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1222 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001223 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001225 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001226 // Check if clause is allowed for the given directive.
1227 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001228 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1229 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001230 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001231 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001232 }
1233
1234 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001235 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001236 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001237 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001238 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001239 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001240 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001241 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001242 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001243 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001244 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001245 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001246 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001247 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001248 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001249 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001250 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001251 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001252 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001253 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001254 // OpenMP [2.9.1, target data construct, Restrictions]
1255 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001256 // OpenMP [2.11.1, task Construct, Restrictions]
1257 // At most one if clause can appear on the directive.
1258 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001259 // OpenMP [teams Construct, Restrictions]
1260 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001261 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001262 // OpenMP [2.9.1, task Construct, Restrictions]
1263 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001264 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1265 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001266 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1267 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001268 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001269 Diag(Tok, diag::err_omp_more_one_clause)
1270 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001271 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001272 }
1273
Alexey Bataev10e775f2015-07-30 11:36:16 +00001274 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001275 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001276 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001277 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001278 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001279 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001280 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001281 // OpenMP [2.14.3.1, Restrictions]
1282 // Only a single default clause may be specified on a parallel, task or
1283 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001284 // OpenMP [2.5, parallel Construct, Restrictions]
1285 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001286 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001287 Diag(Tok, diag::err_omp_more_one_clause)
1288 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001289 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290 }
1291
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001292 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001293 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001294 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001295 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001296 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001297 // OpenMP [2.7.1, Restrictions, p. 3]
1298 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001299 // OpenMP [2.10.4, Restrictions, p. 106]
1300 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001301 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001302 Diag(Tok, diag::err_omp_more_one_clause)
1303 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001304 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001305 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001306 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001307
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001308 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001309 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001310 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001311 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001312 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001313 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001314 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001315 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001316 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001317 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001318 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001319 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001320 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001321 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001322 // OpenMP [2.7.1, Restrictions, p. 9]
1323 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001324 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1325 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001326 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001327 Diag(Tok, diag::err_omp_more_one_clause)
1328 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001329 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001330 }
1331
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001332 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001333 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001334 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001335 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001336 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001337 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001338 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001339 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001340 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001341 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001342 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001343 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001344 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001345 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001346 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001347 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001348 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001349 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001350 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001351 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001352 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001353 break;
1354 case OMPC_unknown:
1355 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001356 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001357 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001358 break;
1359 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001360 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001361 if (!WrongDirective)
1362 Diag(Tok, diag::err_omp_unexpected_clause)
1363 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001364 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001365 break;
1366 }
Craig Topper161e4db2014-05-21 06:02:52 +00001367 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001368}
1369
Alexey Bataev2af33e32016-04-07 12:45:37 +00001370/// Parses simple expression in parens for single-expression clauses of OpenMP
1371/// constructs.
1372/// \param RLoc Returned location of right paren.
1373ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1374 SourceLocation &RLoc) {
1375 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1376 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1377 return ExprError();
1378
1379 SourceLocation ELoc = Tok.getLocation();
1380 ExprResult LHS(ParseCastExpression(
1381 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1382 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1383 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1384
1385 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001386 RLoc = Tok.getLocation();
1387 if (!T.consumeClose())
1388 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001389
Alexey Bataev2af33e32016-04-07 12:45:37 +00001390 return Val;
1391}
1392
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001393/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001394/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001395/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001396///
Alexey Bataev3778b602014-07-17 07:32:53 +00001397/// final-clause:
1398/// 'final' '(' expression ')'
1399///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001400/// num_threads-clause:
1401/// 'num_threads' '(' expression ')'
1402///
1403/// safelen-clause:
1404/// 'safelen' '(' expression ')'
1405///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001406/// simdlen-clause:
1407/// 'simdlen' '(' expression ')'
1408///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001409/// collapse-clause:
1410/// 'collapse' '(' expression ')'
1411///
Alexey Bataeva0569352015-12-01 10:17:31 +00001412/// priority-clause:
1413/// 'priority' '(' expression ')'
1414///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001415/// grainsize-clause:
1416/// 'grainsize' '(' expression ')'
1417///
Alexey Bataev382967a2015-12-08 12:06:20 +00001418/// num_tasks-clause:
1419/// 'num_tasks' '(' expression ')'
1420///
Alexey Bataev28c75412015-12-15 08:19:24 +00001421/// hint-clause:
1422/// 'hint' '(' expression ')'
1423///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001424OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1425 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001426 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001427 SourceLocation LLoc = Tok.getLocation();
1428 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001429
Alexey Bataev2af33e32016-04-07 12:45:37 +00001430 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001431
1432 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001433 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001434
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001435 if (ParseOnly)
1436 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001437 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001438}
1439
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001440/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001441///
1442/// default-clause:
1443/// 'default' '(' 'none' | 'shared' ')
1444///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001445/// proc_bind-clause:
1446/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1447///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001448OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1449 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001450 SourceLocation Loc = Tok.getLocation();
1451 SourceLocation LOpen = ConsumeToken();
1452 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001453 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001454 if (T.expectAndConsume(diag::err_expected_lparen_after,
1455 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001456 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001457
Alexey Bataeva55ed262014-05-28 06:15:33 +00001458 unsigned Type = getOpenMPSimpleClauseType(
1459 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001460 SourceLocation TypeLoc = Tok.getLocation();
1461 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1462 Tok.isNot(tok::annot_pragma_openmp_end))
1463 ConsumeAnyToken();
1464
1465 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001466 SourceLocation RLoc = Tok.getLocation();
1467 if (!T.consumeClose())
1468 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001469
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001470 if (ParseOnly)
1471 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001472 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001473}
1474
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001475/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001476///
1477/// ordered-clause:
1478/// 'ordered'
1479///
Alexey Bataev236070f2014-06-20 11:19:47 +00001480/// nowait-clause:
1481/// 'nowait'
1482///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001483/// untied-clause:
1484/// 'untied'
1485///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001486/// mergeable-clause:
1487/// 'mergeable'
1488///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001489/// read-clause:
1490/// 'read'
1491///
Alexey Bataev346265e2015-09-25 10:37:12 +00001492/// threads-clause:
1493/// 'threads'
1494///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001495/// simd-clause:
1496/// 'simd'
1497///
Alexey Bataevb825de12015-12-07 10:51:44 +00001498/// nogroup-clause:
1499/// 'nogroup'
1500///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001501OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001502 SourceLocation Loc = Tok.getLocation();
1503 ConsumeAnyToken();
1504
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001505 if (ParseOnly)
1506 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001507 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1508}
1509
1510
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001511/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001512/// argument like 'schedule' or 'dist_schedule'.
1513///
1514/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001515/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1516/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001517///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001518/// if-clause:
1519/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1520///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001521/// defaultmap:
1522/// 'defaultmap' '(' modifier ':' kind ')'
1523///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001524OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1525 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001526 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001527 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001528 // Parse '('.
1529 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1530 if (T.expectAndConsume(diag::err_expected_lparen_after,
1531 getOpenMPClauseName(Kind)))
1532 return nullptr;
1533
1534 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001535 SmallVector<unsigned, 4> Arg;
1536 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001537 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001538 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1539 Arg.resize(NumberOfElements);
1540 KLoc.resize(NumberOfElements);
1541 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1542 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1543 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001544 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001545 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001546 if (KindModifier > OMPC_SCHEDULE_unknown) {
1547 // Parse 'modifier'
1548 Arg[Modifier1] = KindModifier;
1549 KLoc[Modifier1] = Tok.getLocation();
1550 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1551 Tok.isNot(tok::annot_pragma_openmp_end))
1552 ConsumeAnyToken();
1553 if (Tok.is(tok::comma)) {
1554 // Parse ',' 'modifier'
1555 ConsumeAnyToken();
1556 KindModifier = getOpenMPSimpleClauseType(
1557 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1558 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1559 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001560 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001561 KLoc[Modifier2] = Tok.getLocation();
1562 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1563 Tok.isNot(tok::annot_pragma_openmp_end))
1564 ConsumeAnyToken();
1565 }
1566 // Parse ':'
1567 if (Tok.is(tok::colon))
1568 ConsumeAnyToken();
1569 else
1570 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1571 KindModifier = getOpenMPSimpleClauseType(
1572 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1573 }
1574 Arg[ScheduleKind] = KindModifier;
1575 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001576 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1577 Tok.isNot(tok::annot_pragma_openmp_end))
1578 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001579 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1580 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1581 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001582 Tok.is(tok::comma))
1583 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001584 } else if (Kind == OMPC_dist_schedule) {
1585 Arg.push_back(getOpenMPSimpleClauseType(
1586 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1587 KLoc.push_back(Tok.getLocation());
1588 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1589 Tok.isNot(tok::annot_pragma_openmp_end))
1590 ConsumeAnyToken();
1591 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1592 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001593 } else if (Kind == OMPC_defaultmap) {
1594 // Get a defaultmap modifier
1595 Arg.push_back(getOpenMPSimpleClauseType(
1596 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1597 KLoc.push_back(Tok.getLocation());
1598 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1599 Tok.isNot(tok::annot_pragma_openmp_end))
1600 ConsumeAnyToken();
1601 // Parse ':'
1602 if (Tok.is(tok::colon))
1603 ConsumeAnyToken();
1604 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1605 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1606 // Get a defaultmap kind
1607 Arg.push_back(getOpenMPSimpleClauseType(
1608 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1609 KLoc.push_back(Tok.getLocation());
1610 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1611 Tok.isNot(tok::annot_pragma_openmp_end))
1612 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001613 } else {
1614 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001615 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001616 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001617 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001618 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001619 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001620 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1621 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001622 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001623 } else {
1624 TPA.Revert();
1625 Arg.back() = OMPD_unknown;
1626 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001627 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001628 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001629 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001630 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001631
Carlo Bertollib4adf552016-01-15 18:50:31 +00001632 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1633 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1634 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001635 if (NeedAnExpression) {
1636 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001637 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1638 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001639 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001640 }
1641
1642 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001643 SourceLocation RLoc = Tok.getLocation();
1644 if (!T.consumeClose())
1645 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001646
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001647 if (NeedAnExpression && Val.isInvalid())
1648 return nullptr;
1649
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001650 if (ParseOnly)
1651 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001652 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001653 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001654}
1655
Alexey Bataevc5e02582014-06-16 07:08:35 +00001656static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1657 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001658 if (ReductionIdScopeSpec.isEmpty()) {
1659 auto OOK = OO_None;
1660 switch (P.getCurToken().getKind()) {
1661 case tok::plus:
1662 OOK = OO_Plus;
1663 break;
1664 case tok::minus:
1665 OOK = OO_Minus;
1666 break;
1667 case tok::star:
1668 OOK = OO_Star;
1669 break;
1670 case tok::amp:
1671 OOK = OO_Amp;
1672 break;
1673 case tok::pipe:
1674 OOK = OO_Pipe;
1675 break;
1676 case tok::caret:
1677 OOK = OO_Caret;
1678 break;
1679 case tok::ampamp:
1680 OOK = OO_AmpAmp;
1681 break;
1682 case tok::pipepipe:
1683 OOK = OO_PipePipe;
1684 break;
1685 default:
1686 break;
1687 }
1688 if (OOK != OO_None) {
1689 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001690 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001691 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1692 return false;
1693 }
1694 }
1695 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1696 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001697 /*AllowConstructorName*/ false,
1698 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001699 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001700}
1701
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001702/// Parses clauses with list.
1703bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1704 OpenMPClauseKind Kind,
1705 SmallVectorImpl<Expr *> &Vars,
1706 OpenMPVarListDataTy &Data) {
1707 UnqualifiedId UnqualifiedReductionId;
1708 bool InvalidReductionId = false;
1709 bool MapTypeModifierSpecified = false;
1710
1711 // Parse '('.
1712 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1713 if (T.expectAndConsume(diag::err_expected_lparen_after,
1714 getOpenMPClauseName(Kind)))
1715 return true;
1716
1717 bool NeedRParenForLinear = false;
1718 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1719 tok::annot_pragma_openmp_end);
1720 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001721 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1722 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001723 ColonProtectionRAIIObject ColonRAII(*this);
1724 if (getLangOpts().CPlusPlus)
1725 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1726 /*ObjectType=*/nullptr,
1727 /*EnteringContext=*/false);
1728 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1729 UnqualifiedReductionId);
1730 if (InvalidReductionId) {
1731 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1732 StopBeforeMatch);
1733 }
1734 if (Tok.is(tok::colon))
1735 Data.ColonLoc = ConsumeToken();
1736 else
1737 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1738 if (!InvalidReductionId)
1739 Data.ReductionId =
1740 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1741 } else if (Kind == OMPC_depend) {
1742 // Handle dependency type for depend clause.
1743 ColonProtectionRAIIObject ColonRAII(*this);
1744 Data.DepKind =
1745 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1746 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1747 Data.DepLinMapLoc = Tok.getLocation();
1748
1749 if (Data.DepKind == OMPC_DEPEND_unknown) {
1750 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1751 StopBeforeMatch);
1752 } else {
1753 ConsumeToken();
1754 // Special processing for depend(source) clause.
1755 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1756 // Parse ')'.
1757 T.consumeClose();
1758 return false;
1759 }
1760 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001761 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001762 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001763 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001764 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1765 : diag::warn_pragma_expected_colon)
1766 << "dependency type";
1767 }
1768 } else if (Kind == OMPC_linear) {
1769 // Try to parse modifier if any.
1770 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1771 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1772 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1773 Data.DepLinMapLoc = ConsumeToken();
1774 LinearT.consumeOpen();
1775 NeedRParenForLinear = true;
1776 }
1777 } else if (Kind == OMPC_map) {
1778 // Handle map type for map clause.
1779 ColonProtectionRAIIObject ColonRAII(*this);
1780
1781 /// The map clause modifier token can be either a identifier or the C++
1782 /// delete keyword.
1783 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1784 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1785 };
1786
1787 // The first identifier may be a list item, a map-type or a
1788 // map-type-modifier. The map modifier can also be delete which has the same
1789 // spelling of the C++ delete keyword.
1790 Data.MapType =
1791 IsMapClauseModifierToken(Tok)
1792 ? static_cast<OpenMPMapClauseKind>(
1793 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1794 : OMPC_MAP_unknown;
1795 Data.DepLinMapLoc = Tok.getLocation();
1796 bool ColonExpected = false;
1797
1798 if (IsMapClauseModifierToken(Tok)) {
1799 if (PP.LookAhead(0).is(tok::colon)) {
1800 if (Data.MapType == OMPC_MAP_unknown)
1801 Diag(Tok, diag::err_omp_unknown_map_type);
1802 else if (Data.MapType == OMPC_MAP_always)
1803 Diag(Tok, diag::err_omp_map_type_missing);
1804 ConsumeToken();
1805 } else if (PP.LookAhead(0).is(tok::comma)) {
1806 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1807 PP.LookAhead(2).is(tok::colon)) {
1808 Data.MapTypeModifier = Data.MapType;
1809 if (Data.MapTypeModifier != OMPC_MAP_always) {
1810 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1811 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001812 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001813 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001814 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001815
1816 ConsumeToken();
1817 ConsumeToken();
1818
1819 Data.MapType =
1820 IsMapClauseModifierToken(Tok)
1821 ? static_cast<OpenMPMapClauseKind>(
1822 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1823 : OMPC_MAP_unknown;
1824 if (Data.MapType == OMPC_MAP_unknown ||
1825 Data.MapType == OMPC_MAP_always)
1826 Diag(Tok, diag::err_omp_unknown_map_type);
1827 ConsumeToken();
1828 } else {
1829 Data.MapType = OMPC_MAP_tofrom;
1830 Data.IsMapTypeImplicit = true;
1831 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001832 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1833 if (PP.LookAhead(1).is(tok::colon)) {
1834 Data.MapTypeModifier = Data.MapType;
1835 if (Data.MapTypeModifier != OMPC_MAP_always) {
1836 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1837 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001838 } else {
Carlo Bertollid8844b92017-05-03 15:28:48 +00001839 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001840 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001841
1842 ConsumeToken();
1843
1844 Data.MapType =
1845 IsMapClauseModifierToken(Tok)
1846 ? static_cast<OpenMPMapClauseKind>(
1847 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1848 : OMPC_MAP_unknown;
1849 if (Data.MapType == OMPC_MAP_unknown ||
1850 Data.MapType == OMPC_MAP_always)
1851 Diag(Tok, diag::err_omp_unknown_map_type);
1852 ConsumeToken();
1853 } else {
1854 Data.MapType = OMPC_MAP_tofrom;
1855 Data.IsMapTypeImplicit = true;
1856 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001857 } else {
1858 Data.MapType = OMPC_MAP_tofrom;
1859 Data.IsMapTypeImplicit = true;
1860 }
1861 } else {
1862 Data.MapType = OMPC_MAP_tofrom;
1863 Data.IsMapTypeImplicit = true;
1864 }
1865
1866 if (Tok.is(tok::colon))
1867 Data.ColonLoc = ConsumeToken();
1868 else if (ColonExpected)
1869 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1870 }
1871
Alexey Bataevfa312f32017-07-21 18:48:21 +00001872 bool IsComma =
1873 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1874 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1875 (Kind == OMPC_reduction && !InvalidReductionId) ||
1876 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1877 (!MapTypeModifierSpecified ||
1878 Data.MapTypeModifier == OMPC_MAP_always)) ||
1879 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001880 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1881 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1882 Tok.isNot(tok::annot_pragma_openmp_end))) {
1883 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1884 // Parse variable
1885 ExprResult VarExpr =
1886 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001887 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001888 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001889 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001890 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1891 StopBeforeMatch);
1892 }
1893 // Skip ',' if any
1894 IsComma = Tok.is(tok::comma);
1895 if (IsComma)
1896 ConsumeToken();
1897 else if (Tok.isNot(tok::r_paren) &&
1898 Tok.isNot(tok::annot_pragma_openmp_end) &&
1899 (!MayHaveTail || Tok.isNot(tok::colon)))
1900 Diag(Tok, diag::err_omp_expected_punc)
1901 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1902 : getOpenMPClauseName(Kind))
1903 << (Kind == OMPC_flush);
1904 }
1905
1906 // Parse ')' for linear clause with modifier.
1907 if (NeedRParenForLinear)
1908 LinearT.consumeClose();
1909
1910 // Parse ':' linear-step (or ':' alignment).
1911 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1912 if (MustHaveTail) {
1913 Data.ColonLoc = Tok.getLocation();
1914 SourceLocation ELoc = ConsumeToken();
1915 ExprResult Tail = ParseAssignmentExpression();
1916 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1917 if (Tail.isUsable())
1918 Data.TailExpr = Tail.get();
1919 else
1920 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1921 StopBeforeMatch);
1922 }
1923
1924 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001925 Data.RLoc = Tok.getLocation();
1926 if (!T.consumeClose())
1927 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00001928 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1929 Vars.empty()) ||
1930 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1931 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001932}
1933
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001934/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001935/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1936/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001937///
1938/// private-clause:
1939/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001940/// firstprivate-clause:
1941/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001942/// lastprivate-clause:
1943/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001944/// shared-clause:
1945/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001946/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001947/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001948/// aligned-clause:
1949/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001950/// reduction-clause:
1951/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001952/// task_reduction-clause:
1953/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001954/// in_reduction-clause:
1955/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001956/// copyprivate-clause:
1957/// 'copyprivate' '(' list ')'
1958/// flush-clause:
1959/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001960/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001961/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001962/// map-clause:
1963/// 'map' '(' [ [ always , ]
1964/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001965/// to-clause:
1966/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001967/// from-clause:
1968/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001969/// use_device_ptr-clause:
1970/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001971/// is_device_ptr-clause:
1972/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001973///
Alexey Bataev182227b2015-08-20 10:54:39 +00001974/// For 'linear' clause linear-list may have the following forms:
1975/// list
1976/// modifier(list)
1977/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001978OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001979 OpenMPClauseKind Kind,
1980 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001981 SourceLocation Loc = Tok.getLocation();
1982 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001983 SmallVector<Expr *, 4> Vars;
1984 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001985
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001986 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001987 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001988
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001989 if (ParseOnly)
1990 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001991 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001992 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001993 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1994 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1995 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001996}
1997