blob: 9871e5bbf9fafe31dc0ac346bb92ae9c692e9abd [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);
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000425 CalledSignatureHelp = true;
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000426 Actions.CodeCompleteExpression(getCurScope(), PreferredType);
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000427 })) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000428 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) {
429 Actions.ProduceConstructorSignatureHelp(
430 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
431 OmpPrivParm->getLocation(), Exprs, LParLoc);
432 CalledSignatureHelp = true;
433 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000434 Actions.ActOnInitializerError(OmpPrivParm);
435 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
436 } else {
437 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000438 SourceLocation RLoc = Tok.getLocation();
439 if (!T.consumeClose())
440 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000441
442 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
443 "Unexpected number of commas!");
444
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000445 ExprResult Initializer =
446 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000447 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
448 /*DirectInit=*/true);
449 }
450 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
451 // Parse C++0x braced-init-list.
452 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
453
454 ExprResult Init(ParseBraceInitializer());
455
456 if (Init.isInvalid()) {
457 Actions.ActOnInitializerError(OmpPrivParm);
458 } else {
459 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
460 /*DirectInit=*/true);
461 }
462 } else {
463 Actions.ActOnUninitializedDecl(OmpPrivParm);
464 }
465}
466
Alexey Bataev2af33e32016-04-07 12:45:37 +0000467namespace {
468/// RAII that recreates function context for correct parsing of clauses of
469/// 'declare simd' construct.
470/// OpenMP, 2.8.2 declare simd Construct
471/// The expressions appearing in the clauses of this directive are evaluated in
472/// the scope of the arguments of the function declaration or definition.
473class FNContextRAII final {
474 Parser &P;
475 Sema::CXXThisScopeRAII *ThisScope;
476 Parser::ParseScope *TempScope;
477 Parser::ParseScope *FnScope;
478 bool HasTemplateScope = false;
479 bool HasFunScope = false;
480 FNContextRAII() = delete;
481 FNContextRAII(const FNContextRAII &) = delete;
482 FNContextRAII &operator=(const FNContextRAII &) = delete;
483
484public:
485 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
486 Decl *D = *Ptr.get().begin();
487 NamedDecl *ND = dyn_cast<NamedDecl>(D);
488 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
489 Sema &Actions = P.getActions();
490
491 // Allow 'this' within late-parsed attributes.
492 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
493 ND && ND->isCXXInstanceMember());
494
495 // If the Decl is templatized, add template parameters to scope.
496 HasTemplateScope = D->isTemplateDecl();
497 TempScope =
498 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
499 if (HasTemplateScope)
500 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
501
502 // If the Decl is on a function, add function parameters to the scope.
503 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000504 FnScope = new Parser::ParseScope(
505 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
506 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000507 if (HasFunScope)
508 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
509 }
510 ~FNContextRAII() {
511 if (HasFunScope) {
512 P.getActions().ActOnExitFunctionContext();
513 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
514 }
515 if (HasTemplateScope)
516 TempScope->Exit();
517 delete FnScope;
518 delete TempScope;
519 delete ThisScope;
520 }
521};
522} // namespace
523
Alexey Bataevd93d3762016-04-12 09:35:56 +0000524/// Parses clauses for 'declare simd' directive.
525/// clause:
526/// 'inbranch' | 'notinbranch'
527/// 'simdlen' '(' <expr> ')'
528/// { 'uniform' '(' <argument_list> ')' }
529/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000530/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
531static bool parseDeclareSimdClauses(
532 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
533 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
534 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
535 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000536 SourceRange BSRange;
537 const Token &Tok = P.getCurToken();
538 bool IsError = false;
539 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
540 if (Tok.isNot(tok::identifier))
541 break;
542 OMPDeclareSimdDeclAttr::BranchStateTy Out;
543 IdentifierInfo *II = Tok.getIdentifierInfo();
544 StringRef ClauseName = II->getName();
545 // Parse 'inranch|notinbranch' clauses.
546 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
547 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
548 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
549 << ClauseName
550 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
551 IsError = true;
552 }
553 BS = Out;
554 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
555 P.ConsumeToken();
556 } else if (ClauseName.equals("simdlen")) {
557 if (SimdLen.isUsable()) {
558 P.Diag(Tok, diag::err_omp_more_one_clause)
559 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
560 IsError = true;
561 }
562 P.ConsumeToken();
563 SourceLocation RLoc;
564 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
565 if (SimdLen.isInvalid())
566 IsError = true;
567 } else {
568 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000569 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
570 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000571 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000572 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000573 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000574 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000575 else if (CKind == OMPC_linear)
576 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000577
578 P.ConsumeToken();
579 if (P.ParseOpenMPVarList(OMPD_declare_simd,
580 getOpenMPClauseKind(ClauseName), *Vars, Data))
581 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000582 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000583 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000584 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000585 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
586 Data.DepLinMapLoc))
587 Data.LinKind = OMPC_LINEAR_val;
588 LinModifiers.append(Linears.size() - LinModifiers.size(),
589 Data.LinKind);
590 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
591 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000592 } else
593 // TODO: add parsing of other clauses.
594 break;
595 }
596 // Skip ',' if any.
597 if (Tok.is(tok::comma))
598 P.ConsumeToken();
599 }
600 return IsError;
601}
602
Alexey Bataev2af33e32016-04-07 12:45:37 +0000603/// Parse clauses for '#pragma omp declare simd'.
604Parser::DeclGroupPtrTy
605Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
606 CachedTokens &Toks, SourceLocation Loc) {
607 PP.EnterToken(Tok);
608 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
609 // Consume the previously pushed token.
610 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
611
612 FNContextRAII FnContext(*this, Ptr);
613 OMPDeclareSimdDeclAttr::BranchStateTy BS =
614 OMPDeclareSimdDeclAttr::BS_Undefined;
615 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000616 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000617 SmallVector<Expr *, 4> Aligneds;
618 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000619 SmallVector<Expr *, 4> Linears;
620 SmallVector<unsigned, 4> LinModifiers;
621 SmallVector<Expr *, 4> Steps;
622 bool IsError =
623 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
624 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000625 // Need to check for extra tokens.
626 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
627 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
628 << getOpenMPDirectiveName(OMPD_declare_simd);
629 while (Tok.isNot(tok::annot_pragma_openmp_end))
630 ConsumeAnyToken();
631 }
632 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000633 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000634 if (IsError)
635 return Ptr;
636 return Actions.ActOnOpenMPDeclareSimdDirective(
637 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
638 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000639}
640
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000641/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000642///
643/// threadprivate-directive:
644/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000645/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000646///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000647/// declare-reduction-directive:
648/// annot_pragma_openmp 'declare' 'reduction' [...]
649/// annot_pragma_openmp_end
650///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000651/// declare-simd-directive:
652/// annot_pragma_openmp 'declare simd' {<clause> [,]}
653/// annot_pragma_openmp_end
654/// <function declaration/definition>
655///
656Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
657 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
658 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000659 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000660 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000661
Richard Smithaf3b3252017-05-18 19:21:48 +0000662 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000663 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664
665 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000666 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000667 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000668 ThreadprivateListParserHelper Helper(this);
669 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000670 // The last seen token is annot_pragma_openmp_end - need to check for
671 // extra tokens.
672 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
673 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000674 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000675 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000676 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000677 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000678 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000679 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
680 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000681 }
682 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000683 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000684 case OMPD_declare_reduction:
685 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000686 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000687 // The last seen token is annot_pragma_openmp_end - need to check for
688 // extra tokens.
689 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
690 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
691 << getOpenMPDirectiveName(OMPD_declare_reduction);
692 while (Tok.isNot(tok::annot_pragma_openmp_end))
693 ConsumeAnyToken();
694 }
695 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000696 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000697 return Res;
698 }
699 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000700 case OMPD_declare_simd: {
701 // The syntax is:
702 // { #pragma omp declare simd }
703 // <function-declaration-or-definition>
704 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000705 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000706 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000707 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
708 Toks.push_back(Tok);
709 ConsumeAnyToken();
710 }
711 Toks.push_back(Tok);
712 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000713
714 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000715 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000716 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000717 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000718 // Here we expect to see some function declaration.
719 if (AS == AS_none) {
720 assert(TagType == DeclSpec::TST_unspecified);
721 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000722 ParsingDeclSpec PDS(*this);
723 Ptr = ParseExternalDeclaration(Attrs, &PDS);
724 } else {
725 Ptr =
726 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
727 }
728 }
729 if (!Ptr) {
730 Diag(Loc, diag::err_omp_decl_in_declare_simd);
731 return DeclGroupPtrTy();
732 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000733 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000734 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000735 case OMPD_declare_target: {
736 SourceLocation DTLoc = ConsumeAnyToken();
737 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000738 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000739 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000740 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
741 OMPDeclareTargetDeclAttr::MapTypeTy MT =
742 OMPDeclareTargetDeclAttr::MT_To;
743 if (Tok.is(tok::identifier)) {
744 IdentifierInfo *II = Tok.getIdentifierInfo();
745 StringRef ClauseName = II->getName();
746 // Parse 'to|link' clauses.
747 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
748 MT)) {
749 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
750 << ClauseName;
751 break;
752 }
753 ConsumeToken();
754 }
Alexey Bataev61908f652018-04-23 19:53:05 +0000755 auto &&Callback = [this, MT, &SameDirectiveDecls](
756 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000757 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
758 SameDirectiveDecls);
759 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000760 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
761 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000762 break;
763
764 // Consume optional ','.
765 if (Tok.is(tok::comma))
766 ConsumeToken();
767 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000768 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000769 ConsumeAnyToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000770 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
771 SameDirectiveDecls.end());
Alexey Bataev34f8a702018-03-28 14:28:54 +0000772 if (Decls.empty())
773 return DeclGroupPtrTy();
774 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000775 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000776
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000777 // Skip the last annot_pragma_openmp_end.
778 ConsumeAnyToken();
779
780 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
781 return DeclGroupPtrTy();
782
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000783 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000784 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +0000785 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
786 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000787 DeclGroupPtrTy Ptr;
788 // Here we expect to see some function declaration.
789 if (AS == AS_none) {
790 assert(TagType == DeclSpec::TST_unspecified);
791 MaybeParseCXX11Attributes(Attrs);
792 ParsingDeclSpec PDS(*this);
793 Ptr = ParseExternalDeclaration(Attrs, &PDS);
794 } else {
795 Ptr =
796 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
797 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000798 if (Ptr) {
799 DeclGroupRef Ref = Ptr.get();
800 Decls.append(Ref.begin(), Ref.end());
801 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000802 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
803 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000804 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000805 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000806 if (DKind != OMPD_end_declare_target)
807 TPA.Revert();
808 else
809 TPA.Commit();
810 }
811 }
812
813 if (DKind == OMPD_end_declare_target) {
814 ConsumeAnyToken();
815 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
816 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
817 << getOpenMPDirectiveName(OMPD_end_declare_target);
818 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
819 }
820 // Skip the last annot_pragma_openmp_end.
821 ConsumeAnyToken();
822 } else {
823 Diag(Tok, diag::err_expected_end_declare_target);
824 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
825 }
826 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000827 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000828 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000829 case OMPD_unknown:
830 Diag(Tok, diag::err_omp_unknown_directive);
831 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000832 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000833 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000834 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000835 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000836 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000837 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000838 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000839 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000840 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000841 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000842 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000843 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000844 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000845 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000846 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000847 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000848 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000849 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000850 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000851 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000852 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000853 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000854 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000855 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000856 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000857 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000858 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000859 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000860 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000861 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000862 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000863 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000864 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000865 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000866 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000867 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000868 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000869 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000870 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000871 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000872 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000873 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000874 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000875 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000876 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000877 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000878 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000879 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000880 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000881 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000882 break;
883 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000884 while (Tok.isNot(tok::annot_pragma_openmp_end))
885 ConsumeAnyToken();
886 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000887 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000888}
889
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000890/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000891///
892/// threadprivate-directive:
893/// annot_pragma_openmp 'threadprivate' simple-variable-list
894/// annot_pragma_openmp_end
895///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000896/// declare-reduction-directive:
897/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
898/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
899/// ('omp_priv' '=' <expression>|<function_call>) ')']
900/// annot_pragma_openmp_end
901///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000902/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000903/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000904/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
905/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000906/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000907/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000908/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000909/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000910/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000911/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000912/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000913/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000914/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000915/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000916/// 'teams distribute parallel for' | 'target teams' |
917/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000918/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000919/// 'target teams distribute parallel for simd' |
920/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000921/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000922///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000923StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000924 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000925 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000926 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000927 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000928 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000929 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000930 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
931 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000932 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000933 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000934 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000935 // Name of critical directive.
936 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000937 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000938 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000939 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940
941 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000942 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000943 if (Allowed != ACK_Any) {
944 Diag(Tok, diag::err_omp_immediate_directive)
945 << getOpenMPDirectiveName(DKind) << 0;
946 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000947 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000948 ThreadprivateListParserHelper Helper(this);
949 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000950 // The last seen token is annot_pragma_openmp_end - need to check for
951 // extra tokens.
952 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
953 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000954 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000955 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000956 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000957 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
958 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000959 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
960 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000961 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000962 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000963 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000964 case OMPD_declare_reduction:
965 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000966 if (DeclGroupPtrTy Res =
967 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000968 // The last seen token is annot_pragma_openmp_end - need to check for
969 // extra tokens.
970 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
971 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
972 << getOpenMPDirectiveName(OMPD_declare_reduction);
973 while (Tok.isNot(tok::annot_pragma_openmp_end))
974 ConsumeAnyToken();
975 }
976 ConsumeAnyToken();
977 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +0000978 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000979 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +0000980 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000981 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000982 case OMPD_flush:
983 if (PP.LookAhead(0).is(tok::l_paren)) {
984 FlushHasClause = true;
985 // Push copy of the current token back to stream to properly parse
986 // pseudo-clause OMPFlushClause.
987 PP.EnterToken(Tok);
988 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000989 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000990 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000991 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000992 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000993 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000994 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000995 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000996 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000997 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000998 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000999 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001000 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001001 }
1002 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001003 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001004 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001005 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001006 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001007 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001008 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001009 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001010 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001011 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001012 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001013 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001014 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001015 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001016 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001017 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001018 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001019 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001020 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001021 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001022 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001023 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001024 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001025 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001026 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001027 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001028 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001029 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001030 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001031 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001032 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001033 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001034 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001035 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001036 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001037 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001038 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001039 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001040 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001041 case OMPD_target_teams_distribute_parallel_for_simd:
1042 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001043 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001044 // Parse directive name of the 'critical' directive if any.
1045 if (DKind == OMPD_critical) {
1046 BalancedDelimiterTracker T(*this, tok::l_paren,
1047 tok::annot_pragma_openmp_end);
1048 if (!T.consumeOpen()) {
1049 if (Tok.isAnyIdentifier()) {
1050 DirName =
1051 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1052 ConsumeAnyToken();
1053 } else {
1054 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1055 }
1056 T.consumeClose();
1057 }
Alexey Bataev80909872015-07-02 11:25:17 +00001058 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001059 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001060 if (Tok.isNot(tok::annot_pragma_openmp_end))
1061 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001062 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001063
Alexey Bataevf29276e2014-06-18 04:14:57 +00001064 if (isOpenMPLoopDirective(DKind))
1065 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1066 if (isOpenMPSimdDirective(DKind))
1067 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1068 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001069 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001070
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001071 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001072 OpenMPClauseKind CKind =
1073 Tok.isAnnotation()
1074 ? OMPC_unknown
1075 : FlushHasClause ? OMPC_flush
1076 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001077 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001078 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001079 OMPClause *Clause =
1080 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001081 FirstClauses[CKind].setInt(true);
1082 if (Clause) {
1083 FirstClauses[CKind].setPointer(Clause);
1084 Clauses.push_back(Clause);
1085 }
1086
1087 // Skip ',' if any.
1088 if (Tok.is(tok::comma))
1089 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001090 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001091 }
1092 // End location of the directive.
1093 EndLoc = Tok.getLocation();
1094 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001095 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001096
Alexey Bataeveb482352015-12-18 05:05:56 +00001097 // OpenMP [2.13.8, ordered Construct, Syntax]
1098 // If the depend clause is specified, the ordered construct is a stand-alone
1099 // directive.
1100 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001101 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001102 Diag(Loc, diag::err_omp_immediate_directive)
1103 << getOpenMPDirectiveName(DKind) << 1
1104 << getOpenMPClauseName(OMPC_depend);
1105 }
1106 HasAssociatedStatement = false;
1107 }
1108
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001109 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001110 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001111 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001112 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001113 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1114 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1115 // should have at least one compound statement scope within it.
1116 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001117 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001118 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1119 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001120 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001121 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1122 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1123 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001124 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001125 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001126 Directive = Actions.ActOnOpenMPExecutableDirective(
1127 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1128 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001129
1130 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001131 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001132 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001133 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001134 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001135 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001136 case OMPD_declare_target:
1137 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001138 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001139 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001140 SkipUntil(tok::annot_pragma_openmp_end);
1141 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001142 case OMPD_unknown:
1143 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001144 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001145 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001146 }
1147 return Directive;
1148}
1149
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001150// Parses simple list:
1151// simple-variable-list:
1152// '(' id-expression {, id-expression} ')'
1153//
1154bool Parser::ParseOpenMPSimpleVarList(
1155 OpenMPDirectiveKind Kind,
1156 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1157 Callback,
1158 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001159 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001160 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001161 if (T.expectAndConsume(diag::err_expected_lparen_after,
1162 getOpenMPDirectiveName(Kind)))
1163 return true;
1164 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001165 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001166
1167 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001168 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001169 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001170 UnqualifiedId Name;
1171 // Read var name.
1172 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001173 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001174
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001175 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001176 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001177 IsCorrect = false;
1178 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001179 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001180 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001181 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001182 IsCorrect = false;
1183 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001184 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001185 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1186 Tok.isNot(tok::annot_pragma_openmp_end)) {
1187 IsCorrect = false;
1188 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001189 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001190 Diag(PrevTok.getLocation(), diag::err_expected)
1191 << tok::identifier
1192 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001193 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001194 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001195 }
1196 // Consume ','.
1197 if (Tok.is(tok::comma)) {
1198 ConsumeToken();
1199 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001200 }
1201
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001202 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001203 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001204 IsCorrect = false;
1205 }
1206
1207 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001208 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001209
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001210 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001211}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001213/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001214///
1215/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001216/// if-clause | final-clause | num_threads-clause | safelen-clause |
1217/// default-clause | private-clause | firstprivate-clause | shared-clause
1218/// | linear-clause | aligned-clause | collapse-clause |
1219/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001220/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001221/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001222/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001223/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001224/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001225/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001226/// from-clause | is_device_ptr-clause | task_reduction-clause |
1227/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001228///
1229OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1230 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001231 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001232 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001233 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001234 // Check if clause is allowed for the given directive.
1235 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001236 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1237 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001238 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001239 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001240 }
1241
1242 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001243 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001244 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001245 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001246 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001247 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001248 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001249 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001250 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001251 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001252 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001253 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001254 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001255 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001256 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001257 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001258 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001259 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001260 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001261 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001262 // OpenMP [2.9.1, target data construct, Restrictions]
1263 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001264 // OpenMP [2.11.1, task Construct, Restrictions]
1265 // At most one if clause can appear on the directive.
1266 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001267 // OpenMP [teams Construct, Restrictions]
1268 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001269 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001270 // OpenMP [2.9.1, task Construct, Restrictions]
1271 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001272 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1273 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001274 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1275 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001276 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001277 Diag(Tok, diag::err_omp_more_one_clause)
1278 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001279 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001280 }
1281
Alexey Bataev10e775f2015-07-30 11:36:16 +00001282 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001283 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001284 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001285 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001286 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001287 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001288 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001289 // OpenMP [2.14.3.1, Restrictions]
1290 // Only a single default clause may be specified on a parallel, task or
1291 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001292 // OpenMP [2.5, parallel Construct, Restrictions]
1293 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001294 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001295 Diag(Tok, diag::err_omp_more_one_clause)
1296 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001297 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001298 }
1299
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001300 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001301 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001302 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001303 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001304 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001305 // OpenMP [2.7.1, Restrictions, p. 3]
1306 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001307 // OpenMP [2.10.4, Restrictions, p. 106]
1308 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001309 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001310 Diag(Tok, diag::err_omp_more_one_clause)
1311 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001312 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001313 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001314 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001315
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001316 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001317 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001318 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001319 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001320 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001321 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001322 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001323 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001324 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001325 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001326 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001327 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001328 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001329 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001330 // OpenMP [2.7.1, Restrictions, p. 9]
1331 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001332 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1333 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001334 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001335 Diag(Tok, diag::err_omp_more_one_clause)
1336 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001337 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001338 }
1339
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001340 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001341 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001342 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001343 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001344 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001345 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001346 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001347 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001348 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001349 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001350 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001351 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001352 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001353 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001354 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001355 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001356 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001357 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001358 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001359 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001360 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001361 break;
1362 case OMPC_unknown:
1363 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001364 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001365 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001366 break;
1367 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001368 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001369 if (!WrongDirective)
1370 Diag(Tok, diag::err_omp_unexpected_clause)
1371 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001372 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001373 break;
1374 }
Craig Topper161e4db2014-05-21 06:02:52 +00001375 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001376}
1377
Alexey Bataev2af33e32016-04-07 12:45:37 +00001378/// Parses simple expression in parens for single-expression clauses of OpenMP
1379/// constructs.
1380/// \param RLoc Returned location of right paren.
1381ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1382 SourceLocation &RLoc) {
1383 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1384 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1385 return ExprError();
1386
1387 SourceLocation ELoc = Tok.getLocation();
1388 ExprResult LHS(ParseCastExpression(
1389 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1390 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1391 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1392
1393 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001394 RLoc = Tok.getLocation();
1395 if (!T.consumeClose())
1396 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001397
Alexey Bataev2af33e32016-04-07 12:45:37 +00001398 return Val;
1399}
1400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001401/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001402/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001403/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001404///
Alexey Bataev3778b602014-07-17 07:32:53 +00001405/// final-clause:
1406/// 'final' '(' expression ')'
1407///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001408/// num_threads-clause:
1409/// 'num_threads' '(' expression ')'
1410///
1411/// safelen-clause:
1412/// 'safelen' '(' expression ')'
1413///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001414/// simdlen-clause:
1415/// 'simdlen' '(' expression ')'
1416///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001417/// collapse-clause:
1418/// 'collapse' '(' expression ')'
1419///
Alexey Bataeva0569352015-12-01 10:17:31 +00001420/// priority-clause:
1421/// 'priority' '(' expression ')'
1422///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001423/// grainsize-clause:
1424/// 'grainsize' '(' expression ')'
1425///
Alexey Bataev382967a2015-12-08 12:06:20 +00001426/// num_tasks-clause:
1427/// 'num_tasks' '(' expression ')'
1428///
Alexey Bataev28c75412015-12-15 08:19:24 +00001429/// hint-clause:
1430/// 'hint' '(' expression ')'
1431///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001432OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1433 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001434 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001435 SourceLocation LLoc = Tok.getLocation();
1436 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001437
Alexey Bataev2af33e32016-04-07 12:45:37 +00001438 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001439
1440 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001441 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001442
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001443 if (ParseOnly)
1444 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001445 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001446}
1447
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001448/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001449///
1450/// default-clause:
1451/// 'default' '(' 'none' | 'shared' ')
1452///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001453/// proc_bind-clause:
1454/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1455///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001456OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1457 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001458 SourceLocation Loc = Tok.getLocation();
1459 SourceLocation LOpen = ConsumeToken();
1460 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001461 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001462 if (T.expectAndConsume(diag::err_expected_lparen_after,
1463 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001464 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001465
Alexey Bataeva55ed262014-05-28 06:15:33 +00001466 unsigned Type = getOpenMPSimpleClauseType(
1467 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001468 SourceLocation TypeLoc = Tok.getLocation();
1469 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1470 Tok.isNot(tok::annot_pragma_openmp_end))
1471 ConsumeAnyToken();
1472
1473 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001474 SourceLocation RLoc = Tok.getLocation();
1475 if (!T.consumeClose())
1476 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001477
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001478 if (ParseOnly)
1479 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001480 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001481}
1482
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001483/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001484///
1485/// ordered-clause:
1486/// 'ordered'
1487///
Alexey Bataev236070f2014-06-20 11:19:47 +00001488/// nowait-clause:
1489/// 'nowait'
1490///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001491/// untied-clause:
1492/// 'untied'
1493///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001494/// mergeable-clause:
1495/// 'mergeable'
1496///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001497/// read-clause:
1498/// 'read'
1499///
Alexey Bataev346265e2015-09-25 10:37:12 +00001500/// threads-clause:
1501/// 'threads'
1502///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001503/// simd-clause:
1504/// 'simd'
1505///
Alexey Bataevb825de12015-12-07 10:51:44 +00001506/// nogroup-clause:
1507/// 'nogroup'
1508///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001509OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001510 SourceLocation Loc = Tok.getLocation();
1511 ConsumeAnyToken();
1512
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001513 if (ParseOnly)
1514 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001515 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1516}
1517
1518
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001519/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001520/// argument like 'schedule' or 'dist_schedule'.
1521///
1522/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001523/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1524/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001525///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001526/// if-clause:
1527/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1528///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001529/// defaultmap:
1530/// 'defaultmap' '(' modifier ':' kind ')'
1531///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001532OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1533 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001534 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001535 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001536 // Parse '('.
1537 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1538 if (T.expectAndConsume(diag::err_expected_lparen_after,
1539 getOpenMPClauseName(Kind)))
1540 return nullptr;
1541
1542 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001543 SmallVector<unsigned, 4> Arg;
1544 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001545 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001546 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1547 Arg.resize(NumberOfElements);
1548 KLoc.resize(NumberOfElements);
1549 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1550 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1551 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001552 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001553 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001554 if (KindModifier > OMPC_SCHEDULE_unknown) {
1555 // Parse 'modifier'
1556 Arg[Modifier1] = KindModifier;
1557 KLoc[Modifier1] = Tok.getLocation();
1558 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1559 Tok.isNot(tok::annot_pragma_openmp_end))
1560 ConsumeAnyToken();
1561 if (Tok.is(tok::comma)) {
1562 // Parse ',' 'modifier'
1563 ConsumeAnyToken();
1564 KindModifier = getOpenMPSimpleClauseType(
1565 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1566 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1567 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001568 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001569 KLoc[Modifier2] = Tok.getLocation();
1570 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1571 Tok.isNot(tok::annot_pragma_openmp_end))
1572 ConsumeAnyToken();
1573 }
1574 // Parse ':'
1575 if (Tok.is(tok::colon))
1576 ConsumeAnyToken();
1577 else
1578 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1579 KindModifier = getOpenMPSimpleClauseType(
1580 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1581 }
1582 Arg[ScheduleKind] = KindModifier;
1583 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001584 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1585 Tok.isNot(tok::annot_pragma_openmp_end))
1586 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001587 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1588 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1589 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001590 Tok.is(tok::comma))
1591 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001592 } else if (Kind == OMPC_dist_schedule) {
1593 Arg.push_back(getOpenMPSimpleClauseType(
1594 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1595 KLoc.push_back(Tok.getLocation());
1596 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1597 Tok.isNot(tok::annot_pragma_openmp_end))
1598 ConsumeAnyToken();
1599 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1600 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001601 } else if (Kind == OMPC_defaultmap) {
1602 // Get a defaultmap modifier
1603 Arg.push_back(getOpenMPSimpleClauseType(
1604 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1605 KLoc.push_back(Tok.getLocation());
1606 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1607 Tok.isNot(tok::annot_pragma_openmp_end))
1608 ConsumeAnyToken();
1609 // Parse ':'
1610 if (Tok.is(tok::colon))
1611 ConsumeAnyToken();
1612 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1613 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1614 // Get a defaultmap kind
1615 Arg.push_back(getOpenMPSimpleClauseType(
1616 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1617 KLoc.push_back(Tok.getLocation());
1618 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1619 Tok.isNot(tok::annot_pragma_openmp_end))
1620 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001621 } else {
1622 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001623 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001624 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001625 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001626 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001627 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001628 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1629 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001630 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001631 } else {
1632 TPA.Revert();
1633 Arg.back() = OMPD_unknown;
1634 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001635 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001636 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001637 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001638 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001639
Carlo Bertollib4adf552016-01-15 18:50:31 +00001640 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1641 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1642 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001643 if (NeedAnExpression) {
1644 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001645 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1646 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001647 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001648 }
1649
1650 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001651 SourceLocation RLoc = Tok.getLocation();
1652 if (!T.consumeClose())
1653 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001654
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001655 if (NeedAnExpression && Val.isInvalid())
1656 return nullptr;
1657
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001658 if (ParseOnly)
1659 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001660 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001661 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001662}
1663
Alexey Bataevc5e02582014-06-16 07:08:35 +00001664static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1665 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001666 if (ReductionIdScopeSpec.isEmpty()) {
1667 auto OOK = OO_None;
1668 switch (P.getCurToken().getKind()) {
1669 case tok::plus:
1670 OOK = OO_Plus;
1671 break;
1672 case tok::minus:
1673 OOK = OO_Minus;
1674 break;
1675 case tok::star:
1676 OOK = OO_Star;
1677 break;
1678 case tok::amp:
1679 OOK = OO_Amp;
1680 break;
1681 case tok::pipe:
1682 OOK = OO_Pipe;
1683 break;
1684 case tok::caret:
1685 OOK = OO_Caret;
1686 break;
1687 case tok::ampamp:
1688 OOK = OO_AmpAmp;
1689 break;
1690 case tok::pipepipe:
1691 OOK = OO_PipePipe;
1692 break;
1693 default:
1694 break;
1695 }
1696 if (OOK != OO_None) {
1697 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001698 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001699 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1700 return false;
1701 }
1702 }
1703 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1704 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001705 /*AllowConstructorName*/ false,
1706 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001707 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001708}
1709
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001710/// Parses clauses with list.
1711bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1712 OpenMPClauseKind Kind,
1713 SmallVectorImpl<Expr *> &Vars,
1714 OpenMPVarListDataTy &Data) {
1715 UnqualifiedId UnqualifiedReductionId;
1716 bool InvalidReductionId = false;
1717 bool MapTypeModifierSpecified = false;
1718
1719 // Parse '('.
1720 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1721 if (T.expectAndConsume(diag::err_expected_lparen_after,
1722 getOpenMPClauseName(Kind)))
1723 return true;
1724
1725 bool NeedRParenForLinear = false;
1726 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1727 tok::annot_pragma_openmp_end);
1728 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001729 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1730 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001731 ColonProtectionRAIIObject ColonRAII(*this);
1732 if (getLangOpts().CPlusPlus)
1733 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1734 /*ObjectType=*/nullptr,
1735 /*EnteringContext=*/false);
1736 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1737 UnqualifiedReductionId);
1738 if (InvalidReductionId) {
1739 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1740 StopBeforeMatch);
1741 }
1742 if (Tok.is(tok::colon))
1743 Data.ColonLoc = ConsumeToken();
1744 else
1745 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1746 if (!InvalidReductionId)
1747 Data.ReductionId =
1748 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1749 } else if (Kind == OMPC_depend) {
1750 // Handle dependency type for depend clause.
1751 ColonProtectionRAIIObject ColonRAII(*this);
1752 Data.DepKind =
1753 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1754 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1755 Data.DepLinMapLoc = Tok.getLocation();
1756
1757 if (Data.DepKind == OMPC_DEPEND_unknown) {
1758 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1759 StopBeforeMatch);
1760 } else {
1761 ConsumeToken();
1762 // Special processing for depend(source) clause.
1763 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1764 // Parse ')'.
1765 T.consumeClose();
1766 return false;
1767 }
1768 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001769 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001770 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001771 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001772 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1773 : diag::warn_pragma_expected_colon)
1774 << "dependency type";
1775 }
1776 } else if (Kind == OMPC_linear) {
1777 // Try to parse modifier if any.
1778 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1779 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1780 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1781 Data.DepLinMapLoc = ConsumeToken();
1782 LinearT.consumeOpen();
1783 NeedRParenForLinear = true;
1784 }
1785 } else if (Kind == OMPC_map) {
1786 // Handle map type for map clause.
1787 ColonProtectionRAIIObject ColonRAII(*this);
1788
1789 /// The map clause modifier token can be either a identifier or the C++
1790 /// delete keyword.
1791 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1792 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1793 };
1794
1795 // The first identifier may be a list item, a map-type or a
1796 // map-type-modifier. The map modifier can also be delete which has the same
1797 // spelling of the C++ delete keyword.
1798 Data.MapType =
1799 IsMapClauseModifierToken(Tok)
1800 ? static_cast<OpenMPMapClauseKind>(
1801 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1802 : OMPC_MAP_unknown;
1803 Data.DepLinMapLoc = Tok.getLocation();
1804 bool ColonExpected = false;
1805
1806 if (IsMapClauseModifierToken(Tok)) {
1807 if (PP.LookAhead(0).is(tok::colon)) {
1808 if (Data.MapType == OMPC_MAP_unknown)
1809 Diag(Tok, diag::err_omp_unknown_map_type);
1810 else if (Data.MapType == OMPC_MAP_always)
1811 Diag(Tok, diag::err_omp_map_type_missing);
1812 ConsumeToken();
1813 } else if (PP.LookAhead(0).is(tok::comma)) {
1814 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1815 PP.LookAhead(2).is(tok::colon)) {
1816 Data.MapTypeModifier = Data.MapType;
1817 if (Data.MapTypeModifier != OMPC_MAP_always) {
1818 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1819 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001820 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001821 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001822 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001823
1824 ConsumeToken();
1825 ConsumeToken();
1826
1827 Data.MapType =
1828 IsMapClauseModifierToken(Tok)
1829 ? static_cast<OpenMPMapClauseKind>(
1830 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1831 : OMPC_MAP_unknown;
1832 if (Data.MapType == OMPC_MAP_unknown ||
1833 Data.MapType == OMPC_MAP_always)
1834 Diag(Tok, diag::err_omp_unknown_map_type);
1835 ConsumeToken();
1836 } else {
1837 Data.MapType = OMPC_MAP_tofrom;
1838 Data.IsMapTypeImplicit = true;
1839 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001840 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1841 if (PP.LookAhead(1).is(tok::colon)) {
1842 Data.MapTypeModifier = Data.MapType;
1843 if (Data.MapTypeModifier != OMPC_MAP_always) {
1844 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1845 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001846 } else {
Carlo Bertollid8844b92017-05-03 15:28:48 +00001847 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001848 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001849
1850 ConsumeToken();
1851
1852 Data.MapType =
1853 IsMapClauseModifierToken(Tok)
1854 ? static_cast<OpenMPMapClauseKind>(
1855 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1856 : OMPC_MAP_unknown;
1857 if (Data.MapType == OMPC_MAP_unknown ||
1858 Data.MapType == OMPC_MAP_always)
1859 Diag(Tok, diag::err_omp_unknown_map_type);
1860 ConsumeToken();
1861 } else {
1862 Data.MapType = OMPC_MAP_tofrom;
1863 Data.IsMapTypeImplicit = true;
1864 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001865 } else {
1866 Data.MapType = OMPC_MAP_tofrom;
1867 Data.IsMapTypeImplicit = true;
1868 }
1869 } else {
1870 Data.MapType = OMPC_MAP_tofrom;
1871 Data.IsMapTypeImplicit = true;
1872 }
1873
1874 if (Tok.is(tok::colon))
1875 Data.ColonLoc = ConsumeToken();
1876 else if (ColonExpected)
1877 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1878 }
1879
Alexey Bataevfa312f32017-07-21 18:48:21 +00001880 bool IsComma =
1881 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1882 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1883 (Kind == OMPC_reduction && !InvalidReductionId) ||
1884 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1885 (!MapTypeModifierSpecified ||
1886 Data.MapTypeModifier == OMPC_MAP_always)) ||
1887 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001888 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1889 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1890 Tok.isNot(tok::annot_pragma_openmp_end))) {
1891 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1892 // Parse variable
1893 ExprResult VarExpr =
1894 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001895 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001896 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001897 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001898 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1899 StopBeforeMatch);
1900 }
1901 // Skip ',' if any
1902 IsComma = Tok.is(tok::comma);
1903 if (IsComma)
1904 ConsumeToken();
1905 else if (Tok.isNot(tok::r_paren) &&
1906 Tok.isNot(tok::annot_pragma_openmp_end) &&
1907 (!MayHaveTail || Tok.isNot(tok::colon)))
1908 Diag(Tok, diag::err_omp_expected_punc)
1909 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1910 : getOpenMPClauseName(Kind))
1911 << (Kind == OMPC_flush);
1912 }
1913
1914 // Parse ')' for linear clause with modifier.
1915 if (NeedRParenForLinear)
1916 LinearT.consumeClose();
1917
1918 // Parse ':' linear-step (or ':' alignment).
1919 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1920 if (MustHaveTail) {
1921 Data.ColonLoc = Tok.getLocation();
1922 SourceLocation ELoc = ConsumeToken();
1923 ExprResult Tail = ParseAssignmentExpression();
1924 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1925 if (Tail.isUsable())
1926 Data.TailExpr = Tail.get();
1927 else
1928 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1929 StopBeforeMatch);
1930 }
1931
1932 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001933 Data.RLoc = Tok.getLocation();
1934 if (!T.consumeClose())
1935 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00001936 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1937 Vars.empty()) ||
1938 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1939 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001940}
1941
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001942/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001943/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1944/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945///
1946/// private-clause:
1947/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001948/// firstprivate-clause:
1949/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001950/// lastprivate-clause:
1951/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001952/// shared-clause:
1953/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001954/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001955/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001956/// aligned-clause:
1957/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001958/// reduction-clause:
1959/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001960/// task_reduction-clause:
1961/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001962/// in_reduction-clause:
1963/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001964/// copyprivate-clause:
1965/// 'copyprivate' '(' list ')'
1966/// flush-clause:
1967/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001968/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001969/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001970/// map-clause:
1971/// 'map' '(' [ [ always , ]
1972/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001973/// to-clause:
1974/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001975/// from-clause:
1976/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001977/// use_device_ptr-clause:
1978/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001979/// is_device_ptr-clause:
1980/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001981///
Alexey Bataev182227b2015-08-20 10:54:39 +00001982/// For 'linear' clause linear-list may have the following forms:
1983/// list
1984/// modifier(list)
1985/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001986OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001987 OpenMPClauseKind Kind,
1988 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001989 SourceLocation Loc = Tok.getLocation();
1990 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001991 SmallVector<Expr *, 4> Vars;
1992 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001993
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001994 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001995 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001996
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001997 if (ParseOnly)
1998 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001999 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002000 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002001 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
2002 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
2003 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002004}
2005