blob: b3c7e3a63d094dd5c91fbaf13ced3635e4b0b196 [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] {
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000421 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000422 getCurScope(),
423 OmpPrivParm->getType()->getCanonicalTypeInternal(),
424 OmpPrivParm->getLocation(), Exprs, LParLoc);
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000425 Actions.CodeCompleteExpression(getCurScope(), PreferredType);
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000426 })) {
Alexey Bataev070f43a2017-09-06 14:49:58 +0000427 Actions.ActOnInitializerError(OmpPrivParm);
428 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
429 } else {
430 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000431 SourceLocation RLoc = Tok.getLocation();
432 if (!T.consumeClose())
433 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000434
435 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
436 "Unexpected number of commas!");
437
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000438 ExprResult Initializer =
439 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000440 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
441 /*DirectInit=*/true);
442 }
443 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
444 // Parse C++0x braced-init-list.
445 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
446
447 ExprResult Init(ParseBraceInitializer());
448
449 if (Init.isInvalid()) {
450 Actions.ActOnInitializerError(OmpPrivParm);
451 } else {
452 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
453 /*DirectInit=*/true);
454 }
455 } else {
456 Actions.ActOnUninitializedDecl(OmpPrivParm);
457 }
458}
459
Alexey Bataev2af33e32016-04-07 12:45:37 +0000460namespace {
461/// RAII that recreates function context for correct parsing of clauses of
462/// 'declare simd' construct.
463/// OpenMP, 2.8.2 declare simd Construct
464/// The expressions appearing in the clauses of this directive are evaluated in
465/// the scope of the arguments of the function declaration or definition.
466class FNContextRAII final {
467 Parser &P;
468 Sema::CXXThisScopeRAII *ThisScope;
469 Parser::ParseScope *TempScope;
470 Parser::ParseScope *FnScope;
471 bool HasTemplateScope = false;
472 bool HasFunScope = false;
473 FNContextRAII() = delete;
474 FNContextRAII(const FNContextRAII &) = delete;
475 FNContextRAII &operator=(const FNContextRAII &) = delete;
476
477public:
478 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
479 Decl *D = *Ptr.get().begin();
480 NamedDecl *ND = dyn_cast<NamedDecl>(D);
481 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
482 Sema &Actions = P.getActions();
483
484 // Allow 'this' within late-parsed attributes.
485 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
486 ND && ND->isCXXInstanceMember());
487
488 // If the Decl is templatized, add template parameters to scope.
489 HasTemplateScope = D->isTemplateDecl();
490 TempScope =
491 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
492 if (HasTemplateScope)
493 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
494
495 // If the Decl is on a function, add function parameters to the scope.
496 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000497 FnScope = new Parser::ParseScope(
498 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
499 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000500 if (HasFunScope)
501 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
502 }
503 ~FNContextRAII() {
504 if (HasFunScope) {
505 P.getActions().ActOnExitFunctionContext();
506 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
507 }
508 if (HasTemplateScope)
509 TempScope->Exit();
510 delete FnScope;
511 delete TempScope;
512 delete ThisScope;
513 }
514};
515} // namespace
516
Alexey Bataevd93d3762016-04-12 09:35:56 +0000517/// Parses clauses for 'declare simd' directive.
518/// clause:
519/// 'inbranch' | 'notinbranch'
520/// 'simdlen' '(' <expr> ')'
521/// { 'uniform' '(' <argument_list> ')' }
522/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000523/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
524static bool parseDeclareSimdClauses(
525 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
526 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
527 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
528 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000529 SourceRange BSRange;
530 const Token &Tok = P.getCurToken();
531 bool IsError = false;
532 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
533 if (Tok.isNot(tok::identifier))
534 break;
535 OMPDeclareSimdDeclAttr::BranchStateTy Out;
536 IdentifierInfo *II = Tok.getIdentifierInfo();
537 StringRef ClauseName = II->getName();
538 // Parse 'inranch|notinbranch' clauses.
539 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
540 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
541 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
542 << ClauseName
543 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
544 IsError = true;
545 }
546 BS = Out;
547 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
548 P.ConsumeToken();
549 } else if (ClauseName.equals("simdlen")) {
550 if (SimdLen.isUsable()) {
551 P.Diag(Tok, diag::err_omp_more_one_clause)
552 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
553 IsError = true;
554 }
555 P.ConsumeToken();
556 SourceLocation RLoc;
557 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
558 if (SimdLen.isInvalid())
559 IsError = true;
560 } else {
561 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000562 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
563 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000564 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000565 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000566 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000567 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000568 else if (CKind == OMPC_linear)
569 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000570
571 P.ConsumeToken();
572 if (P.ParseOpenMPVarList(OMPD_declare_simd,
573 getOpenMPClauseKind(ClauseName), *Vars, Data))
574 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000575 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000576 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000577 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000578 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
579 Data.DepLinMapLoc))
580 Data.LinKind = OMPC_LINEAR_val;
581 LinModifiers.append(Linears.size() - LinModifiers.size(),
582 Data.LinKind);
583 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
584 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000585 } else
586 // TODO: add parsing of other clauses.
587 break;
588 }
589 // Skip ',' if any.
590 if (Tok.is(tok::comma))
591 P.ConsumeToken();
592 }
593 return IsError;
594}
595
Alexey Bataev2af33e32016-04-07 12:45:37 +0000596/// Parse clauses for '#pragma omp declare simd'.
597Parser::DeclGroupPtrTy
598Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
599 CachedTokens &Toks, SourceLocation Loc) {
600 PP.EnterToken(Tok);
601 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
602 // Consume the previously pushed token.
603 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
604
605 FNContextRAII FnContext(*this, Ptr);
606 OMPDeclareSimdDeclAttr::BranchStateTy BS =
607 OMPDeclareSimdDeclAttr::BS_Undefined;
608 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000609 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000610 SmallVector<Expr *, 4> Aligneds;
611 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000612 SmallVector<Expr *, 4> Linears;
613 SmallVector<unsigned, 4> LinModifiers;
614 SmallVector<Expr *, 4> Steps;
615 bool IsError =
616 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
617 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000618 // Need to check for extra tokens.
619 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
620 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
621 << getOpenMPDirectiveName(OMPD_declare_simd);
622 while (Tok.isNot(tok::annot_pragma_openmp_end))
623 ConsumeAnyToken();
624 }
625 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000626 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000627 if (IsError)
628 return Ptr;
629 return Actions.ActOnOpenMPDeclareSimdDirective(
630 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
631 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000632}
633
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000634/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000635///
636/// threadprivate-directive:
637/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000638/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000639///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000640/// declare-reduction-directive:
641/// annot_pragma_openmp 'declare' 'reduction' [...]
642/// annot_pragma_openmp_end
643///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000644/// declare-simd-directive:
645/// annot_pragma_openmp 'declare simd' {<clause> [,]}
646/// annot_pragma_openmp_end
647/// <function declaration/definition>
648///
649Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
650 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
651 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000652 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000653 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000654
Richard Smithaf3b3252017-05-18 19:21:48 +0000655 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000656 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000657
658 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000659 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000660 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000661 ThreadprivateListParserHelper Helper(this);
662 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000663 // The last seen token is annot_pragma_openmp_end - need to check for
664 // extra tokens.
665 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
666 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000667 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000668 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000669 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000670 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000671 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000672 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
673 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000674 }
675 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000676 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000677 case OMPD_declare_reduction:
678 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000679 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000680 // The last seen token is annot_pragma_openmp_end - need to check for
681 // extra tokens.
682 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
683 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
684 << getOpenMPDirectiveName(OMPD_declare_reduction);
685 while (Tok.isNot(tok::annot_pragma_openmp_end))
686 ConsumeAnyToken();
687 }
688 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000689 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000690 return Res;
691 }
692 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000693 case OMPD_declare_simd: {
694 // The syntax is:
695 // { #pragma omp declare simd }
696 // <function-declaration-or-definition>
697 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000698 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000699 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000700 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
701 Toks.push_back(Tok);
702 ConsumeAnyToken();
703 }
704 Toks.push_back(Tok);
705 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000706
707 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000708 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000709 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000710 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000711 // Here we expect to see some function declaration.
712 if (AS == AS_none) {
713 assert(TagType == DeclSpec::TST_unspecified);
714 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000715 ParsingDeclSpec PDS(*this);
716 Ptr = ParseExternalDeclaration(Attrs, &PDS);
717 } else {
718 Ptr =
719 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
720 }
721 }
722 if (!Ptr) {
723 Diag(Loc, diag::err_omp_decl_in_declare_simd);
724 return DeclGroupPtrTy();
725 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000726 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000727 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000728 case OMPD_declare_target: {
729 SourceLocation DTLoc = ConsumeAnyToken();
730 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000731 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000732 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000733 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
734 OMPDeclareTargetDeclAttr::MapTypeTy MT =
735 OMPDeclareTargetDeclAttr::MT_To;
736 if (Tok.is(tok::identifier)) {
737 IdentifierInfo *II = Tok.getIdentifierInfo();
738 StringRef ClauseName = II->getName();
739 // Parse 'to|link' clauses.
740 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
741 MT)) {
742 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
743 << ClauseName;
744 break;
745 }
746 ConsumeToken();
747 }
Alexey Bataev61908f652018-04-23 19:53:05 +0000748 auto &&Callback = [this, MT, &SameDirectiveDecls](
749 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000750 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
751 SameDirectiveDecls);
752 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000753 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
754 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000755 break;
756
757 // Consume optional ','.
758 if (Tok.is(tok::comma))
759 ConsumeToken();
760 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000761 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000762 ConsumeAnyToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000763 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
764 SameDirectiveDecls.end());
Alexey Bataev34f8a702018-03-28 14:28:54 +0000765 if (Decls.empty())
766 return DeclGroupPtrTy();
767 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000768 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000769
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000770 // Skip the last annot_pragma_openmp_end.
771 ConsumeAnyToken();
772
773 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
774 return DeclGroupPtrTy();
775
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000776 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000777 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000778 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
779 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000780 DeclGroupPtrTy Ptr;
781 // Here we expect to see some function declaration.
782 if (AS == AS_none) {
783 assert(TagType == DeclSpec::TST_unspecified);
784 MaybeParseCXX11Attributes(Attrs);
785 ParsingDeclSpec PDS(*this);
786 Ptr = ParseExternalDeclaration(Attrs, &PDS);
787 } else {
788 Ptr =
789 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
790 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000791 if (Ptr) {
792 DeclGroupRef Ref = Ptr.get();
793 Decls.append(Ref.begin(), Ref.end());
794 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000795 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
796 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000797 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000798 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000799 if (DKind != OMPD_end_declare_target)
800 TPA.Revert();
801 else
802 TPA.Commit();
803 }
804 }
805
806 if (DKind == OMPD_end_declare_target) {
807 ConsumeAnyToken();
808 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
809 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
810 << getOpenMPDirectiveName(OMPD_end_declare_target);
811 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
812 }
813 // Skip the last annot_pragma_openmp_end.
814 ConsumeAnyToken();
815 } else {
816 Diag(Tok, diag::err_expected_end_declare_target);
817 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
818 }
819 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000820 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000821 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000822 case OMPD_unknown:
823 Diag(Tok, diag::err_omp_unknown_directive);
824 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000826 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000827 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000828 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000829 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000830 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000831 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000832 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000833 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000834 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000835 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000836 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000837 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000838 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000839 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000840 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000841 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000842 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000843 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000844 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000845 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000846 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000847 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000848 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000849 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000850 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000851 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000852 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000853 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000854 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000855 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000856 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000857 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000858 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000859 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000860 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000861 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000862 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000863 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000864 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000865 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000866 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000867 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000868 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000869 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000870 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000871 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000872 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000873 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000874 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000875 break;
876 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000877 while (Tok.isNot(tok::annot_pragma_openmp_end))
878 ConsumeAnyToken();
879 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000880 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000881}
882
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000883/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000884///
885/// threadprivate-directive:
886/// annot_pragma_openmp 'threadprivate' simple-variable-list
887/// annot_pragma_openmp_end
888///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000889/// declare-reduction-directive:
890/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
891/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
892/// ('omp_priv' '=' <expression>|<function_call>) ')']
893/// annot_pragma_openmp_end
894///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000895/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000896/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000897/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
898/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000899/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000900/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000901/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000902/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000903/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000904/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000905/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000906/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000907/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000908/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000909/// 'teams distribute parallel for' | 'target teams' |
910/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000911/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000912/// 'target teams distribute parallel for simd' |
913/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000914/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000915///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000916StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000917 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000918 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000919 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000921 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000922 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000923 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
924 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000925 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000926 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000927 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000928 // Name of critical directive.
929 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000930 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000931 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000932 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000933
934 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000935 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000936 if (Allowed != ACK_Any) {
937 Diag(Tok, diag::err_omp_immediate_directive)
938 << getOpenMPDirectiveName(DKind) << 0;
939 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000941 ThreadprivateListParserHelper Helper(this);
942 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000943 // The last seen token is annot_pragma_openmp_end - need to check for
944 // extra tokens.
945 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
946 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000947 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000948 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000950 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
951 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000952 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
953 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000954 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000955 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000956 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000957 case OMPD_declare_reduction:
958 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000959 if (DeclGroupPtrTy Res =
960 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000961 // The last seen token is annot_pragma_openmp_end - need to check for
962 // extra tokens.
963 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
964 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
965 << getOpenMPDirectiveName(OMPD_declare_reduction);
966 while (Tok.isNot(tok::annot_pragma_openmp_end))
967 ConsumeAnyToken();
968 }
969 ConsumeAnyToken();
970 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +0000971 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000972 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +0000973 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000974 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000975 case OMPD_flush:
976 if (PP.LookAhead(0).is(tok::l_paren)) {
977 FlushHasClause = true;
978 // Push copy of the current token back to stream to properly parse
979 // pseudo-clause OMPFlushClause.
980 PP.EnterToken(Tok);
981 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000982 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000983 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000984 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000985 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000986 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000987 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000988 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000989 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000990 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000991 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000992 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000993 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000994 }
995 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000996 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000997 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000998 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000999 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001000 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001001 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001002 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001003 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001004 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001005 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001006 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001007 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001008 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001009 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001010 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001011 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001012 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001013 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001014 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001015 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001016 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001017 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001018 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001019 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001020 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001021 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001022 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001023 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001024 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001025 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001026 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001027 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001028 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001029 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001030 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001031 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001032 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001033 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001034 case OMPD_target_teams_distribute_parallel_for_simd:
1035 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001036 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001037 // Parse directive name of the 'critical' directive if any.
1038 if (DKind == OMPD_critical) {
1039 BalancedDelimiterTracker T(*this, tok::l_paren,
1040 tok::annot_pragma_openmp_end);
1041 if (!T.consumeOpen()) {
1042 if (Tok.isAnyIdentifier()) {
1043 DirName =
1044 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1045 ConsumeAnyToken();
1046 } else {
1047 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1048 }
1049 T.consumeClose();
1050 }
Alexey Bataev80909872015-07-02 11:25:17 +00001051 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001052 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001053 if (Tok.isNot(tok::annot_pragma_openmp_end))
1054 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001055 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001056
Alexey Bataevf29276e2014-06-18 04:14:57 +00001057 if (isOpenMPLoopDirective(DKind))
1058 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1059 if (isOpenMPSimdDirective(DKind))
1060 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1061 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001062 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001063
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001064 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001065 OpenMPClauseKind CKind =
1066 Tok.isAnnotation()
1067 ? OMPC_unknown
1068 : FlushHasClause ? OMPC_flush
1069 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001070 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001071 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001072 OMPClause *Clause =
1073 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001074 FirstClauses[CKind].setInt(true);
1075 if (Clause) {
1076 FirstClauses[CKind].setPointer(Clause);
1077 Clauses.push_back(Clause);
1078 }
1079
1080 // Skip ',' if any.
1081 if (Tok.is(tok::comma))
1082 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001083 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001084 }
1085 // End location of the directive.
1086 EndLoc = Tok.getLocation();
1087 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001088 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089
Alexey Bataeveb482352015-12-18 05:05:56 +00001090 // OpenMP [2.13.8, ordered Construct, Syntax]
1091 // If the depend clause is specified, the ordered construct is a stand-alone
1092 // directive.
1093 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001094 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001095 Diag(Loc, diag::err_omp_immediate_directive)
1096 << getOpenMPDirectiveName(DKind) << 1
1097 << getOpenMPClauseName(OMPC_depend);
1098 }
1099 HasAssociatedStatement = false;
1100 }
1101
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001103 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001104 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001105 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001106 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1107 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1108 // should have at least one compound statement scope within it.
1109 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001110 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001111 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1112 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001113 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001114 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1115 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1116 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001117 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001118 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001119 Directive = Actions.ActOnOpenMPExecutableDirective(
1120 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1121 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122
1123 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001124 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001125 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001126 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001127 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001128 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001129 case OMPD_declare_target:
1130 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001131 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001132 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001133 SkipUntil(tok::annot_pragma_openmp_end);
1134 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001135 case OMPD_unknown:
1136 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001137 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001138 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001139 }
1140 return Directive;
1141}
1142
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001143// Parses simple list:
1144// simple-variable-list:
1145// '(' id-expression {, id-expression} ')'
1146//
1147bool Parser::ParseOpenMPSimpleVarList(
1148 OpenMPDirectiveKind Kind,
1149 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1150 Callback,
1151 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001152 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001153 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001154 if (T.expectAndConsume(diag::err_expected_lparen_after,
1155 getOpenMPDirectiveName(Kind)))
1156 return true;
1157 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001158 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001159
1160 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001161 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001162 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001163 UnqualifiedId Name;
1164 // Read var name.
1165 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001166 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001167
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001168 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001169 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001170 IsCorrect = false;
1171 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001172 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001173 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001174 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001175 IsCorrect = false;
1176 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001177 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1179 Tok.isNot(tok::annot_pragma_openmp_end)) {
1180 IsCorrect = false;
1181 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001182 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001183 Diag(PrevTok.getLocation(), diag::err_expected)
1184 << tok::identifier
1185 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001186 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001187 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001188 }
1189 // Consume ','.
1190 if (Tok.is(tok::comma)) {
1191 ConsumeToken();
1192 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001193 }
1194
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001195 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001196 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001197 IsCorrect = false;
1198 }
1199
1200 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001201 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001202
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001203 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001204}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001205
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001206/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001207///
1208/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001209/// if-clause | final-clause | num_threads-clause | safelen-clause |
1210/// default-clause | private-clause | firstprivate-clause | shared-clause
1211/// | linear-clause | aligned-clause | collapse-clause |
1212/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001213/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001214/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001215/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001216/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001217/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001218/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001219/// from-clause | is_device_ptr-clause | task_reduction-clause |
1220/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001221///
1222OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1223 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001224 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001225 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001226 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001227 // Check if clause is allowed for the given directive.
1228 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001229 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1230 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001231 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001232 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001233 }
1234
1235 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001236 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001237 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001238 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001239 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001240 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001241 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001242 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001243 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001244 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001245 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001246 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001247 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001248 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001249 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001250 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001251 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001252 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001253 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001254 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001255 // OpenMP [2.9.1, target data construct, Restrictions]
1256 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001257 // OpenMP [2.11.1, task Construct, Restrictions]
1258 // At most one if clause can appear on the directive.
1259 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001260 // OpenMP [teams Construct, Restrictions]
1261 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001262 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001263 // OpenMP [2.9.1, task Construct, Restrictions]
1264 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001265 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1266 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001267 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1268 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001269 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001270 Diag(Tok, diag::err_omp_more_one_clause)
1271 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001272 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001273 }
1274
Alexey Bataev10e775f2015-07-30 11:36:16 +00001275 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001276 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001277 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001278 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001279 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001280 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001281 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001282 // OpenMP [2.14.3.1, Restrictions]
1283 // Only a single default clause may be specified on a parallel, task or
1284 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001285 // OpenMP [2.5, parallel Construct, Restrictions]
1286 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001287 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001288 Diag(Tok, diag::err_omp_more_one_clause)
1289 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001290 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001291 }
1292
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001293 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001294 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001295 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001296 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001297 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001298 // OpenMP [2.7.1, Restrictions, p. 3]
1299 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001300 // OpenMP [2.10.4, Restrictions, p. 106]
1301 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001302 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001303 Diag(Tok, diag::err_omp_more_one_clause)
1304 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001305 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001306 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001307 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001308
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001309 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001310 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001311 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001312 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001313 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001314 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001315 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001316 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001317 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001318 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001319 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001320 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001321 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001322 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001323 // OpenMP [2.7.1, Restrictions, p. 9]
1324 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001325 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1326 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001327 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001328 Diag(Tok, diag::err_omp_more_one_clause)
1329 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001330 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001331 }
1332
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001333 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001334 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001335 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001336 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001337 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001338 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001339 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001340 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001341 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001342 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001343 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001344 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001345 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001346 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001347 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001348 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001349 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001350 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001351 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001352 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001353 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001354 break;
1355 case OMPC_unknown:
1356 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001357 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001358 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001359 break;
1360 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001361 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001362 if (!WrongDirective)
1363 Diag(Tok, diag::err_omp_unexpected_clause)
1364 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001365 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001366 break;
1367 }
Craig Topper161e4db2014-05-21 06:02:52 +00001368 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001369}
1370
Alexey Bataev2af33e32016-04-07 12:45:37 +00001371/// Parses simple expression in parens for single-expression clauses of OpenMP
1372/// constructs.
1373/// \param RLoc Returned location of right paren.
1374ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1375 SourceLocation &RLoc) {
1376 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1377 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1378 return ExprError();
1379
1380 SourceLocation ELoc = Tok.getLocation();
1381 ExprResult LHS(ParseCastExpression(
1382 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1383 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1384 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1385
1386 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001387 RLoc = Tok.getLocation();
1388 if (!T.consumeClose())
1389 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001390
Alexey Bataev2af33e32016-04-07 12:45:37 +00001391 return Val;
1392}
1393
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001394/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001395/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001396/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001397///
Alexey Bataev3778b602014-07-17 07:32:53 +00001398/// final-clause:
1399/// 'final' '(' expression ')'
1400///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001401/// num_threads-clause:
1402/// 'num_threads' '(' expression ')'
1403///
1404/// safelen-clause:
1405/// 'safelen' '(' expression ')'
1406///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001407/// simdlen-clause:
1408/// 'simdlen' '(' expression ')'
1409///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001410/// collapse-clause:
1411/// 'collapse' '(' expression ')'
1412///
Alexey Bataeva0569352015-12-01 10:17:31 +00001413/// priority-clause:
1414/// 'priority' '(' expression ')'
1415///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001416/// grainsize-clause:
1417/// 'grainsize' '(' expression ')'
1418///
Alexey Bataev382967a2015-12-08 12:06:20 +00001419/// num_tasks-clause:
1420/// 'num_tasks' '(' expression ')'
1421///
Alexey Bataev28c75412015-12-15 08:19:24 +00001422/// hint-clause:
1423/// 'hint' '(' expression ')'
1424///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001425OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1426 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001427 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001428 SourceLocation LLoc = Tok.getLocation();
1429 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001430
Alexey Bataev2af33e32016-04-07 12:45:37 +00001431 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001432
1433 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001434 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001435
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001436 if (ParseOnly)
1437 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001438 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001439}
1440
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001441/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001442///
1443/// default-clause:
1444/// 'default' '(' 'none' | 'shared' ')
1445///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001446/// proc_bind-clause:
1447/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1448///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001449OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1450 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001451 SourceLocation Loc = Tok.getLocation();
1452 SourceLocation LOpen = ConsumeToken();
1453 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001454 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001455 if (T.expectAndConsume(diag::err_expected_lparen_after,
1456 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001457 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001458
Alexey Bataeva55ed262014-05-28 06:15:33 +00001459 unsigned Type = getOpenMPSimpleClauseType(
1460 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001461 SourceLocation TypeLoc = Tok.getLocation();
1462 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1463 Tok.isNot(tok::annot_pragma_openmp_end))
1464 ConsumeAnyToken();
1465
1466 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001467 SourceLocation RLoc = Tok.getLocation();
1468 if (!T.consumeClose())
1469 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001470
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001471 if (ParseOnly)
1472 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001473 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001474}
1475
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001476/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001477///
1478/// ordered-clause:
1479/// 'ordered'
1480///
Alexey Bataev236070f2014-06-20 11:19:47 +00001481/// nowait-clause:
1482/// 'nowait'
1483///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001484/// untied-clause:
1485/// 'untied'
1486///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001487/// mergeable-clause:
1488/// 'mergeable'
1489///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001490/// read-clause:
1491/// 'read'
1492///
Alexey Bataev346265e2015-09-25 10:37:12 +00001493/// threads-clause:
1494/// 'threads'
1495///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001496/// simd-clause:
1497/// 'simd'
1498///
Alexey Bataevb825de12015-12-07 10:51:44 +00001499/// nogroup-clause:
1500/// 'nogroup'
1501///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001502OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001503 SourceLocation Loc = Tok.getLocation();
1504 ConsumeAnyToken();
1505
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001506 if (ParseOnly)
1507 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001508 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1509}
1510
1511
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001512/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001513/// argument like 'schedule' or 'dist_schedule'.
1514///
1515/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001516/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1517/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001518///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001519/// if-clause:
1520/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1521///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001522/// defaultmap:
1523/// 'defaultmap' '(' modifier ':' kind ')'
1524///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001525OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1526 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001527 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001528 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001529 // Parse '('.
1530 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1531 if (T.expectAndConsume(diag::err_expected_lparen_after,
1532 getOpenMPClauseName(Kind)))
1533 return nullptr;
1534
1535 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001536 SmallVector<unsigned, 4> Arg;
1537 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001538 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001539 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1540 Arg.resize(NumberOfElements);
1541 KLoc.resize(NumberOfElements);
1542 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1543 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1544 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001545 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001546 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001547 if (KindModifier > OMPC_SCHEDULE_unknown) {
1548 // Parse 'modifier'
1549 Arg[Modifier1] = KindModifier;
1550 KLoc[Modifier1] = Tok.getLocation();
1551 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1552 Tok.isNot(tok::annot_pragma_openmp_end))
1553 ConsumeAnyToken();
1554 if (Tok.is(tok::comma)) {
1555 // Parse ',' 'modifier'
1556 ConsumeAnyToken();
1557 KindModifier = getOpenMPSimpleClauseType(
1558 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1559 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1560 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001561 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001562 KLoc[Modifier2] = Tok.getLocation();
1563 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1564 Tok.isNot(tok::annot_pragma_openmp_end))
1565 ConsumeAnyToken();
1566 }
1567 // Parse ':'
1568 if (Tok.is(tok::colon))
1569 ConsumeAnyToken();
1570 else
1571 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1572 KindModifier = getOpenMPSimpleClauseType(
1573 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1574 }
1575 Arg[ScheduleKind] = KindModifier;
1576 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001577 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1578 Tok.isNot(tok::annot_pragma_openmp_end))
1579 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001580 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1581 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1582 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001583 Tok.is(tok::comma))
1584 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001585 } else if (Kind == OMPC_dist_schedule) {
1586 Arg.push_back(getOpenMPSimpleClauseType(
1587 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1588 KLoc.push_back(Tok.getLocation());
1589 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1590 Tok.isNot(tok::annot_pragma_openmp_end))
1591 ConsumeAnyToken();
1592 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1593 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001594 } else if (Kind == OMPC_defaultmap) {
1595 // Get a defaultmap modifier
1596 Arg.push_back(getOpenMPSimpleClauseType(
1597 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1598 KLoc.push_back(Tok.getLocation());
1599 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1600 Tok.isNot(tok::annot_pragma_openmp_end))
1601 ConsumeAnyToken();
1602 // Parse ':'
1603 if (Tok.is(tok::colon))
1604 ConsumeAnyToken();
1605 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1606 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1607 // Get a defaultmap kind
1608 Arg.push_back(getOpenMPSimpleClauseType(
1609 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1610 KLoc.push_back(Tok.getLocation());
1611 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1612 Tok.isNot(tok::annot_pragma_openmp_end))
1613 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001614 } else {
1615 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001616 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001617 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001618 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001619 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001620 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001621 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1622 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001623 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001624 } else {
1625 TPA.Revert();
1626 Arg.back() = OMPD_unknown;
1627 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001628 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001629 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001630 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001631 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001632
Carlo Bertollib4adf552016-01-15 18:50:31 +00001633 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1634 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1635 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001636 if (NeedAnExpression) {
1637 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001638 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1639 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001640 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001641 }
1642
1643 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001644 SourceLocation RLoc = Tok.getLocation();
1645 if (!T.consumeClose())
1646 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001647
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001648 if (NeedAnExpression && Val.isInvalid())
1649 return nullptr;
1650
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001651 if (ParseOnly)
1652 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001653 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001654 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001655}
1656
Alexey Bataevc5e02582014-06-16 07:08:35 +00001657static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1658 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001659 if (ReductionIdScopeSpec.isEmpty()) {
1660 auto OOK = OO_None;
1661 switch (P.getCurToken().getKind()) {
1662 case tok::plus:
1663 OOK = OO_Plus;
1664 break;
1665 case tok::minus:
1666 OOK = OO_Minus;
1667 break;
1668 case tok::star:
1669 OOK = OO_Star;
1670 break;
1671 case tok::amp:
1672 OOK = OO_Amp;
1673 break;
1674 case tok::pipe:
1675 OOK = OO_Pipe;
1676 break;
1677 case tok::caret:
1678 OOK = OO_Caret;
1679 break;
1680 case tok::ampamp:
1681 OOK = OO_AmpAmp;
1682 break;
1683 case tok::pipepipe:
1684 OOK = OO_PipePipe;
1685 break;
1686 default:
1687 break;
1688 }
1689 if (OOK != OO_None) {
1690 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001691 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001692 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1693 return false;
1694 }
1695 }
1696 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1697 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001698 /*AllowConstructorName*/ false,
1699 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001700 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001701}
1702
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001703/// Parses clauses with list.
1704bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1705 OpenMPClauseKind Kind,
1706 SmallVectorImpl<Expr *> &Vars,
1707 OpenMPVarListDataTy &Data) {
1708 UnqualifiedId UnqualifiedReductionId;
1709 bool InvalidReductionId = false;
1710 bool MapTypeModifierSpecified = false;
1711
1712 // Parse '('.
1713 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1714 if (T.expectAndConsume(diag::err_expected_lparen_after,
1715 getOpenMPClauseName(Kind)))
1716 return true;
1717
1718 bool NeedRParenForLinear = false;
1719 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1720 tok::annot_pragma_openmp_end);
1721 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001722 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1723 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001724 ColonProtectionRAIIObject ColonRAII(*this);
1725 if (getLangOpts().CPlusPlus)
1726 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1727 /*ObjectType=*/nullptr,
1728 /*EnteringContext=*/false);
1729 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1730 UnqualifiedReductionId);
1731 if (InvalidReductionId) {
1732 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1733 StopBeforeMatch);
1734 }
1735 if (Tok.is(tok::colon))
1736 Data.ColonLoc = ConsumeToken();
1737 else
1738 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1739 if (!InvalidReductionId)
1740 Data.ReductionId =
1741 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1742 } else if (Kind == OMPC_depend) {
1743 // Handle dependency type for depend clause.
1744 ColonProtectionRAIIObject ColonRAII(*this);
1745 Data.DepKind =
1746 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1747 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1748 Data.DepLinMapLoc = Tok.getLocation();
1749
1750 if (Data.DepKind == OMPC_DEPEND_unknown) {
1751 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1752 StopBeforeMatch);
1753 } else {
1754 ConsumeToken();
1755 // Special processing for depend(source) clause.
1756 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1757 // Parse ')'.
1758 T.consumeClose();
1759 return false;
1760 }
1761 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001762 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001763 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001764 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001765 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1766 : diag::warn_pragma_expected_colon)
1767 << "dependency type";
1768 }
1769 } else if (Kind == OMPC_linear) {
1770 // Try to parse modifier if any.
1771 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1772 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1773 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1774 Data.DepLinMapLoc = ConsumeToken();
1775 LinearT.consumeOpen();
1776 NeedRParenForLinear = true;
1777 }
1778 } else if (Kind == OMPC_map) {
1779 // Handle map type for map clause.
1780 ColonProtectionRAIIObject ColonRAII(*this);
1781
1782 /// The map clause modifier token can be either a identifier or the C++
1783 /// delete keyword.
1784 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1785 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1786 };
1787
1788 // The first identifier may be a list item, a map-type or a
1789 // map-type-modifier. The map modifier can also be delete which has the same
1790 // spelling of the C++ delete keyword.
1791 Data.MapType =
1792 IsMapClauseModifierToken(Tok)
1793 ? static_cast<OpenMPMapClauseKind>(
1794 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1795 : OMPC_MAP_unknown;
1796 Data.DepLinMapLoc = Tok.getLocation();
1797 bool ColonExpected = false;
1798
1799 if (IsMapClauseModifierToken(Tok)) {
1800 if (PP.LookAhead(0).is(tok::colon)) {
1801 if (Data.MapType == OMPC_MAP_unknown)
1802 Diag(Tok, diag::err_omp_unknown_map_type);
1803 else if (Data.MapType == OMPC_MAP_always)
1804 Diag(Tok, diag::err_omp_map_type_missing);
1805 ConsumeToken();
1806 } else if (PP.LookAhead(0).is(tok::comma)) {
1807 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1808 PP.LookAhead(2).is(tok::colon)) {
1809 Data.MapTypeModifier = Data.MapType;
1810 if (Data.MapTypeModifier != OMPC_MAP_always) {
1811 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1812 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001813 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001814 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001815 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001816
1817 ConsumeToken();
1818 ConsumeToken();
1819
1820 Data.MapType =
1821 IsMapClauseModifierToken(Tok)
1822 ? static_cast<OpenMPMapClauseKind>(
1823 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1824 : OMPC_MAP_unknown;
1825 if (Data.MapType == OMPC_MAP_unknown ||
1826 Data.MapType == OMPC_MAP_always)
1827 Diag(Tok, diag::err_omp_unknown_map_type);
1828 ConsumeToken();
1829 } else {
1830 Data.MapType = OMPC_MAP_tofrom;
1831 Data.IsMapTypeImplicit = true;
1832 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001833 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1834 if (PP.LookAhead(1).is(tok::colon)) {
1835 Data.MapTypeModifier = Data.MapType;
1836 if (Data.MapTypeModifier != OMPC_MAP_always) {
1837 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1838 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001839 } else {
Carlo Bertollid8844b92017-05-03 15:28:48 +00001840 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001841 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001842
1843 ConsumeToken();
1844
1845 Data.MapType =
1846 IsMapClauseModifierToken(Tok)
1847 ? static_cast<OpenMPMapClauseKind>(
1848 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1849 : OMPC_MAP_unknown;
1850 if (Data.MapType == OMPC_MAP_unknown ||
1851 Data.MapType == OMPC_MAP_always)
1852 Diag(Tok, diag::err_omp_unknown_map_type);
1853 ConsumeToken();
1854 } else {
1855 Data.MapType = OMPC_MAP_tofrom;
1856 Data.IsMapTypeImplicit = true;
1857 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001858 } else {
1859 Data.MapType = OMPC_MAP_tofrom;
1860 Data.IsMapTypeImplicit = true;
1861 }
1862 } else {
1863 Data.MapType = OMPC_MAP_tofrom;
1864 Data.IsMapTypeImplicit = true;
1865 }
1866
1867 if (Tok.is(tok::colon))
1868 Data.ColonLoc = ConsumeToken();
1869 else if (ColonExpected)
1870 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1871 }
1872
Alexey Bataevfa312f32017-07-21 18:48:21 +00001873 bool IsComma =
1874 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1875 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1876 (Kind == OMPC_reduction && !InvalidReductionId) ||
1877 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1878 (!MapTypeModifierSpecified ||
1879 Data.MapTypeModifier == OMPC_MAP_always)) ||
1880 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001881 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1882 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1883 Tok.isNot(tok::annot_pragma_openmp_end))) {
1884 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1885 // Parse variable
1886 ExprResult VarExpr =
1887 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001888 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001889 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001890 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001891 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1892 StopBeforeMatch);
1893 }
1894 // Skip ',' if any
1895 IsComma = Tok.is(tok::comma);
1896 if (IsComma)
1897 ConsumeToken();
1898 else if (Tok.isNot(tok::r_paren) &&
1899 Tok.isNot(tok::annot_pragma_openmp_end) &&
1900 (!MayHaveTail || Tok.isNot(tok::colon)))
1901 Diag(Tok, diag::err_omp_expected_punc)
1902 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1903 : getOpenMPClauseName(Kind))
1904 << (Kind == OMPC_flush);
1905 }
1906
1907 // Parse ')' for linear clause with modifier.
1908 if (NeedRParenForLinear)
1909 LinearT.consumeClose();
1910
1911 // Parse ':' linear-step (or ':' alignment).
1912 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1913 if (MustHaveTail) {
1914 Data.ColonLoc = Tok.getLocation();
1915 SourceLocation ELoc = ConsumeToken();
1916 ExprResult Tail = ParseAssignmentExpression();
1917 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1918 if (Tail.isUsable())
1919 Data.TailExpr = Tail.get();
1920 else
1921 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1922 StopBeforeMatch);
1923 }
1924
1925 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001926 Data.RLoc = Tok.getLocation();
1927 if (!T.consumeClose())
1928 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00001929 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1930 Vars.empty()) ||
1931 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1932 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001933}
1934
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001935/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001936/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1937/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001938///
1939/// private-clause:
1940/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001941/// firstprivate-clause:
1942/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001943/// lastprivate-clause:
1944/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001945/// shared-clause:
1946/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001947/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001948/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001949/// aligned-clause:
1950/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001951/// reduction-clause:
1952/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001953/// task_reduction-clause:
1954/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001955/// in_reduction-clause:
1956/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001957/// copyprivate-clause:
1958/// 'copyprivate' '(' list ')'
1959/// flush-clause:
1960/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001961/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001962/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001963/// map-clause:
1964/// 'map' '(' [ [ always , ]
1965/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001966/// to-clause:
1967/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001968/// from-clause:
1969/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001970/// use_device_ptr-clause:
1971/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001972/// is_device_ptr-clause:
1973/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001974///
Alexey Bataev182227b2015-08-20 10:54:39 +00001975/// For 'linear' clause linear-list may have the following forms:
1976/// list
1977/// modifier(list)
1978/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001979OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001980 OpenMPClauseKind Kind,
1981 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001982 SourceLocation Loc = Tok.getLocation();
1983 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001984 SmallVector<Expr *, 4> Vars;
1985 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001986
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001987 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001988 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001989
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001990 if (ParseOnly)
1991 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001992 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001993 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001994 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1995 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1996 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001997}
1998