blob: f3edf7ca51f40b2751326e4cd800b0ea86ce136d [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
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
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 Bataev4acb8592014-07-07 13:01:15 +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] = {
88 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000089 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000090 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000091 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000092 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
93 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000094 { OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000096 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000097 { OMPD_end, OMPD_declare, OMPD_end_declare },
98 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000099 { OMPD_target, OMPD_data, OMPD_target_data },
100 { OMPD_target, OMPD_enter, OMPD_target_enter },
101 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000102 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000103 { 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 },
Kelvin Li986330c2016-07-20 22:57:10 +0000111 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000112 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000113 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000114 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
Kelvin Li579e41c2016-11-30 23:51:03 +0000115 { 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, OMPD_teams_distribute_parallel_for },
Kelvin Libf594a52016-12-17 05:48:59 +0000118 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd },
Kelvin Li83c451e2016-12-25 04:52:54 +0000119 { OMPD_target, OMPD_teams, OMPD_target_teams },
Kelvin Li80e8f562016-12-29 22:16:30 +0000120 { OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute },
121 { OMPD_target_teams_distribute, OMPD_parallel, OMPD_target_teams_distribute_parallel },
Kelvin Lida681182017-01-10 18:08:18 +0000122 { OMPD_target_teams_distribute, OMPD_simd, OMPD_target_teams_distribute_simd },
Kelvin Li1851df52017-01-03 05:23:48 +0000123 { OMPD_target_teams_distribute_parallel, OMPD_for, OMPD_target_teams_distribute_parallel_for },
124 { OMPD_target_teams_distribute_parallel_for, OMPD_simd, OMPD_target_teams_distribute_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000125 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000126 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000127 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000128 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000129 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000130 ? static_cast<unsigned>(OMPD_unknown)
131 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
132 if (DKind == OMPD_unknown)
133 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000134
Alexander Musmanf82886e2014-09-18 05:12:34 +0000135 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000136 if (DKind != F[i][0])
137 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000138
Dmitry Polukhin82478332016-02-13 06:53:38 +0000139 Tok = P.getPreprocessor().LookAhead(0);
140 unsigned SDKind =
141 Tok.isAnnotation()
142 ? static_cast<unsigned>(OMPD_unknown)
143 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
144 if (SDKind == OMPD_unknown)
145 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000146
Dmitry Polukhin82478332016-02-13 06:53:38 +0000147 if (SDKind == F[i][1]) {
148 P.ConsumeToken();
149 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000150 }
151 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000152 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
153 : OMPD_unknown;
154}
155
156static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000157 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000158 Sema &Actions = P.getActions();
159 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000160 // Allow to use 'operator' keyword for C++ operators
161 bool WithOperator = false;
162 if (Tok.is(tok::kw_operator)) {
163 P.ConsumeToken();
164 Tok = P.getCurToken();
165 WithOperator = true;
166 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000167 switch (Tok.getKind()) {
168 case tok::plus: // '+'
169 OOK = OO_Plus;
170 break;
171 case tok::minus: // '-'
172 OOK = OO_Minus;
173 break;
174 case tok::star: // '*'
175 OOK = OO_Star;
176 break;
177 case tok::amp: // '&'
178 OOK = OO_Amp;
179 break;
180 case tok::pipe: // '|'
181 OOK = OO_Pipe;
182 break;
183 case tok::caret: // '^'
184 OOK = OO_Caret;
185 break;
186 case tok::ampamp: // '&&'
187 OOK = OO_AmpAmp;
188 break;
189 case tok::pipepipe: // '||'
190 OOK = OO_PipePipe;
191 break;
192 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000193 if (!WithOperator)
194 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000195 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000196 default:
197 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
198 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
199 Parser::StopBeforeMatch);
200 return DeclarationName();
201 }
202 P.ConsumeToken();
203 auto &DeclNames = Actions.getASTContext().DeclarationNames;
204 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
205 : DeclNames.getCXXOperatorName(OOK);
206}
207
208/// \brief Parse 'omp declare reduction' construct.
209///
210/// declare-reduction-directive:
211/// annot_pragma_openmp 'declare' 'reduction'
212/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
213/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
214/// annot_pragma_openmp_end
215/// <reduction_id> is either a base language identifier or one of the following
216/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
217///
218Parser::DeclGroupPtrTy
219Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
220 // Parse '('.
221 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
222 if (T.expectAndConsume(diag::err_expected_lparen_after,
223 getOpenMPDirectiveName(OMPD_declare_reduction))) {
224 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
225 return DeclGroupPtrTy();
226 }
227
228 DeclarationName Name = parseOpenMPReductionId(*this);
229 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
230 return DeclGroupPtrTy();
231
232 // Consume ':'.
233 bool IsCorrect = !ExpectAndConsume(tok::colon);
234
235 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
236 return DeclGroupPtrTy();
237
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000238 IsCorrect = IsCorrect && !Name.isEmpty();
239
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000240 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
241 Diag(Tok.getLocation(), diag::err_expected_type);
242 IsCorrect = false;
243 }
244
245 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
246 return DeclGroupPtrTy();
247
248 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
249 // Parse list of types until ':' token.
250 do {
251 ColonProtectionRAIIObject ColonRAII(*this);
252 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000253 TypeResult TR =
254 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000255 if (TR.isUsable()) {
256 auto ReductionType =
257 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
258 if (!ReductionType.isNull()) {
259 ReductionTypes.push_back(
260 std::make_pair(ReductionType, Range.getBegin()));
261 }
262 } else {
263 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
264 StopBeforeMatch);
265 }
266
267 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
268 break;
269
270 // Consume ','.
271 if (ExpectAndConsume(tok::comma)) {
272 IsCorrect = false;
273 if (Tok.is(tok::annot_pragma_openmp_end)) {
274 Diag(Tok.getLocation(), diag::err_expected_type);
275 return DeclGroupPtrTy();
276 }
277 }
278 } while (Tok.isNot(tok::annot_pragma_openmp_end));
279
280 if (ReductionTypes.empty()) {
281 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
282 return DeclGroupPtrTy();
283 }
284
285 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
286 return DeclGroupPtrTy();
287
288 // Consume ':'.
289 if (ExpectAndConsume(tok::colon))
290 IsCorrect = false;
291
292 if (Tok.is(tok::annot_pragma_openmp_end)) {
293 Diag(Tok.getLocation(), diag::err_expected_expression);
294 return DeclGroupPtrTy();
295 }
296
297 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
298 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
299
300 // Parse <combiner> expression and then parse initializer if any for each
301 // correct type.
302 unsigned I = 0, E = ReductionTypes.size();
303 for (auto *D : DRD.get()) {
304 TentativeParsingAction TPA(*this);
305 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000306 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000307 Scope::OpenMPDirectiveScope);
308 // Parse <combiner> expression.
309 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
310 ExprResult CombinerResult =
311 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
312 D->getLocation(), /*DiscardedValue=*/true);
313 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
314
315 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
316 Tok.isNot(tok::annot_pragma_openmp_end)) {
317 TPA.Commit();
318 IsCorrect = false;
319 break;
320 }
321 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
322 ExprResult InitializerResult;
323 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
324 // Parse <initializer> expression.
325 if (Tok.is(tok::identifier) &&
326 Tok.getIdentifierInfo()->isStr("initializer"))
327 ConsumeToken();
328 else {
329 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
330 TPA.Commit();
331 IsCorrect = false;
332 break;
333 }
334 // Parse '('.
335 BalancedDelimiterTracker T(*this, tok::l_paren,
336 tok::annot_pragma_openmp_end);
337 IsCorrect =
338 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
339 IsCorrect;
340 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
341 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000342 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000343 Scope::OpenMPDirectiveScope);
344 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000345 VarDecl *OmpPrivParm =
346 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
347 D);
348 // Check if initializer is omp_priv <init_expr> or something else.
349 if (Tok.is(tok::identifier) &&
350 Tok.getIdentifierInfo()->isStr("omp_priv")) {
351 ConsumeToken();
352 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
353 } else {
354 InitializerResult = Actions.ActOnFinishFullExpr(
355 ParseAssignmentExpression().get(), D->getLocation(),
356 /*DiscardedValue=*/true);
357 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000358 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000359 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000360 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
361 Tok.isNot(tok::annot_pragma_openmp_end)) {
362 TPA.Commit();
363 IsCorrect = false;
364 break;
365 }
366 IsCorrect =
367 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
368 }
369 }
370
371 ++I;
372 // Revert parsing if not the last type, otherwise accept it, we're done with
373 // parsing.
374 if (I != E)
375 TPA.Revert();
376 else
377 TPA.Commit();
378 }
379 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
380 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000381}
382
Alexey Bataev070f43a2017-09-06 14:49:58 +0000383void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
384 // Parse declarator '=' initializer.
385 // If a '==' or '+=' is found, suggest a fixit to '='.
386 if (isTokenEqualOrEqualTypo()) {
387 ConsumeToken();
388
389 if (Tok.is(tok::code_completion)) {
390 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
391 Actions.FinalizeDeclaration(OmpPrivParm);
392 cutOffParsing();
393 return;
394 }
395
396 ExprResult Init(ParseInitializer());
397
398 if (Init.isInvalid()) {
399 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
400 Actions.ActOnInitializerError(OmpPrivParm);
401 } else {
402 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
403 /*DirectInit=*/false);
404 }
405 } else if (Tok.is(tok::l_paren)) {
406 // Parse C++ direct initializer: '(' expression-list ')'
407 BalancedDelimiterTracker T(*this, tok::l_paren);
408 T.consumeOpen();
409
410 ExprVector Exprs;
411 CommaLocsTy CommaLocs;
412
413 if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] {
414 Actions.CodeCompleteConstructor(
415 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
416 OmpPrivParm->getLocation(), Exprs);
417 })) {
418 Actions.ActOnInitializerError(OmpPrivParm);
419 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
420 } else {
421 // Match the ')'.
422 T.consumeClose();
423
424 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
425 "Unexpected number of commas!");
426
427 ExprResult Initializer = Actions.ActOnParenListExpr(
428 T.getOpenLocation(), T.getCloseLocation(), Exprs);
429 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
430 /*DirectInit=*/true);
431 }
432 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
433 // Parse C++0x braced-init-list.
434 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
435
436 ExprResult Init(ParseBraceInitializer());
437
438 if (Init.isInvalid()) {
439 Actions.ActOnInitializerError(OmpPrivParm);
440 } else {
441 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
442 /*DirectInit=*/true);
443 }
444 } else {
445 Actions.ActOnUninitializedDecl(OmpPrivParm);
446 }
447}
448
Alexey Bataev2af33e32016-04-07 12:45:37 +0000449namespace {
450/// RAII that recreates function context for correct parsing of clauses of
451/// 'declare simd' construct.
452/// OpenMP, 2.8.2 declare simd Construct
453/// The expressions appearing in the clauses of this directive are evaluated in
454/// the scope of the arguments of the function declaration or definition.
455class FNContextRAII final {
456 Parser &P;
457 Sema::CXXThisScopeRAII *ThisScope;
458 Parser::ParseScope *TempScope;
459 Parser::ParseScope *FnScope;
460 bool HasTemplateScope = false;
461 bool HasFunScope = false;
462 FNContextRAII() = delete;
463 FNContextRAII(const FNContextRAII &) = delete;
464 FNContextRAII &operator=(const FNContextRAII &) = delete;
465
466public:
467 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
468 Decl *D = *Ptr.get().begin();
469 NamedDecl *ND = dyn_cast<NamedDecl>(D);
470 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
471 Sema &Actions = P.getActions();
472
473 // Allow 'this' within late-parsed attributes.
474 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
475 ND && ND->isCXXInstanceMember());
476
477 // If the Decl is templatized, add template parameters to scope.
478 HasTemplateScope = D->isTemplateDecl();
479 TempScope =
480 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
481 if (HasTemplateScope)
482 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
483
484 // If the Decl is on a function, add function parameters to the scope.
485 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000486 FnScope = new Parser::ParseScope(
487 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
488 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000489 if (HasFunScope)
490 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
491 }
492 ~FNContextRAII() {
493 if (HasFunScope) {
494 P.getActions().ActOnExitFunctionContext();
495 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
496 }
497 if (HasTemplateScope)
498 TempScope->Exit();
499 delete FnScope;
500 delete TempScope;
501 delete ThisScope;
502 }
503};
504} // namespace
505
Alexey Bataevd93d3762016-04-12 09:35:56 +0000506/// Parses clauses for 'declare simd' directive.
507/// clause:
508/// 'inbranch' | 'notinbranch'
509/// 'simdlen' '(' <expr> ')'
510/// { 'uniform' '(' <argument_list> ')' }
511/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000512/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
513static bool parseDeclareSimdClauses(
514 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
515 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
516 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
517 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000518 SourceRange BSRange;
519 const Token &Tok = P.getCurToken();
520 bool IsError = false;
521 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
522 if (Tok.isNot(tok::identifier))
523 break;
524 OMPDeclareSimdDeclAttr::BranchStateTy Out;
525 IdentifierInfo *II = Tok.getIdentifierInfo();
526 StringRef ClauseName = II->getName();
527 // Parse 'inranch|notinbranch' clauses.
528 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
529 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
530 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
531 << ClauseName
532 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
533 IsError = true;
534 }
535 BS = Out;
536 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
537 P.ConsumeToken();
538 } else if (ClauseName.equals("simdlen")) {
539 if (SimdLen.isUsable()) {
540 P.Diag(Tok, diag::err_omp_more_one_clause)
541 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
542 IsError = true;
543 }
544 P.ConsumeToken();
545 SourceLocation RLoc;
546 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
547 if (SimdLen.isInvalid())
548 IsError = true;
549 } else {
550 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000551 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
552 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000553 Parser::OpenMPVarListDataTy Data;
554 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000555 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000556 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000557 else if (CKind == OMPC_linear)
558 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000559
560 P.ConsumeToken();
561 if (P.ParseOpenMPVarList(OMPD_declare_simd,
562 getOpenMPClauseKind(ClauseName), *Vars, Data))
563 IsError = true;
564 if (CKind == OMPC_aligned)
565 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000566 else if (CKind == OMPC_linear) {
567 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
568 Data.DepLinMapLoc))
569 Data.LinKind = OMPC_LINEAR_val;
570 LinModifiers.append(Linears.size() - LinModifiers.size(),
571 Data.LinKind);
572 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
573 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000574 } else
575 // TODO: add parsing of other clauses.
576 break;
577 }
578 // Skip ',' if any.
579 if (Tok.is(tok::comma))
580 P.ConsumeToken();
581 }
582 return IsError;
583}
584
Alexey Bataev2af33e32016-04-07 12:45:37 +0000585/// Parse clauses for '#pragma omp declare simd'.
586Parser::DeclGroupPtrTy
587Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
588 CachedTokens &Toks, SourceLocation Loc) {
589 PP.EnterToken(Tok);
590 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
591 // Consume the previously pushed token.
592 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
593
594 FNContextRAII FnContext(*this, Ptr);
595 OMPDeclareSimdDeclAttr::BranchStateTy BS =
596 OMPDeclareSimdDeclAttr::BS_Undefined;
597 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000598 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000599 SmallVector<Expr *, 4> Aligneds;
600 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000601 SmallVector<Expr *, 4> Linears;
602 SmallVector<unsigned, 4> LinModifiers;
603 SmallVector<Expr *, 4> Steps;
604 bool IsError =
605 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
606 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000607 // Need to check for extra tokens.
608 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
609 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
610 << getOpenMPDirectiveName(OMPD_declare_simd);
611 while (Tok.isNot(tok::annot_pragma_openmp_end))
612 ConsumeAnyToken();
613 }
614 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000615 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000616 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000617 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000618 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
619 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000620 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000621 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000622}
623
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000624/// \brief Parsing of declarative OpenMP directives.
625///
626/// threadprivate-directive:
627/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000628/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000629///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000630/// declare-reduction-directive:
631/// annot_pragma_openmp 'declare' 'reduction' [...]
632/// annot_pragma_openmp_end
633///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000634/// declare-simd-directive:
635/// annot_pragma_openmp 'declare simd' {<clause> [,]}
636/// annot_pragma_openmp_end
637/// <function declaration/definition>
638///
639Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
640 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
641 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000642 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000643 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000644
Richard Smithaf3b3252017-05-18 19:21:48 +0000645 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000646 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000647
648 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000649 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000650 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000651 ThreadprivateListParserHelper Helper(this);
652 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000653 // The last seen token is annot_pragma_openmp_end - need to check for
654 // extra tokens.
655 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
656 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000657 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000658 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000659 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000660 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000661 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000662 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
663 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000664 }
665 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000666 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000667 case OMPD_declare_reduction:
668 ConsumeToken();
669 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
670 // 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)
674 << getOpenMPDirectiveName(OMPD_declare_reduction);
675 while (Tok.isNot(tok::annot_pragma_openmp_end))
676 ConsumeAnyToken();
677 }
678 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000679 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000680 return Res;
681 }
682 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000683 case OMPD_declare_simd: {
684 // The syntax is:
685 // { #pragma omp declare simd }
686 // <function-declaration-or-definition>
687 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000688 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000689 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000690 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
691 Toks.push_back(Tok);
692 ConsumeAnyToken();
693 }
694 Toks.push_back(Tok);
695 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000696
697 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000698 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000699 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000700 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000701 // Here we expect to see some function declaration.
702 if (AS == AS_none) {
703 assert(TagType == DeclSpec::TST_unspecified);
704 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000705 ParsingDeclSpec PDS(*this);
706 Ptr = ParseExternalDeclaration(Attrs, &PDS);
707 } else {
708 Ptr =
709 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
710 }
711 }
712 if (!Ptr) {
713 Diag(Loc, diag::err_omp_decl_in_declare_simd);
714 return DeclGroupPtrTy();
715 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000716 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000717 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000718 case OMPD_declare_target: {
719 SourceLocation DTLoc = ConsumeAnyToken();
720 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000721 // OpenMP 4.5 syntax with list of entities.
722 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
723 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
724 OMPDeclareTargetDeclAttr::MapTypeTy MT =
725 OMPDeclareTargetDeclAttr::MT_To;
726 if (Tok.is(tok::identifier)) {
727 IdentifierInfo *II = Tok.getIdentifierInfo();
728 StringRef ClauseName = II->getName();
729 // Parse 'to|link' clauses.
730 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
731 MT)) {
732 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
733 << ClauseName;
734 break;
735 }
736 ConsumeToken();
737 }
738 auto Callback = [this, MT, &SameDirectiveDecls](
739 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
740 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
741 SameDirectiveDecls);
742 };
743 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
744 break;
745
746 // Consume optional ','.
747 if (Tok.is(tok::comma))
748 ConsumeToken();
749 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000750 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000751 ConsumeAnyToken();
752 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000753 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000754
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000755 // Skip the last annot_pragma_openmp_end.
756 ConsumeAnyToken();
757
758 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
759 return DeclGroupPtrTy();
760
761 DKind = ParseOpenMPDirectiveKind(*this);
762 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
763 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000764 DeclGroupPtrTy Ptr;
765 // Here we expect to see some function declaration.
766 if (AS == AS_none) {
767 assert(TagType == DeclSpec::TST_unspecified);
768 MaybeParseCXX11Attributes(Attrs);
769 ParsingDeclSpec PDS(*this);
770 Ptr = ParseExternalDeclaration(Attrs, &PDS);
771 } else {
772 Ptr =
773 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
774 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000775 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
776 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000777 ConsumeAnnotationToken();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000778 DKind = ParseOpenMPDirectiveKind(*this);
779 if (DKind != OMPD_end_declare_target)
780 TPA.Revert();
781 else
782 TPA.Commit();
783 }
784 }
785
786 if (DKind == OMPD_end_declare_target) {
787 ConsumeAnyToken();
788 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
789 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
790 << getOpenMPDirectiveName(OMPD_end_declare_target);
791 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
792 }
793 // Skip the last annot_pragma_openmp_end.
794 ConsumeAnyToken();
795 } else {
796 Diag(Tok, diag::err_expected_end_declare_target);
797 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
798 }
799 Actions.ActOnFinishOpenMPDeclareTargetDirective();
800 return DeclGroupPtrTy();
801 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000802 case OMPD_unknown:
803 Diag(Tok, diag::err_omp_unknown_directive);
804 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000805 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000806 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000807 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000808 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000809 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000810 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000811 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000812 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000813 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000814 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000815 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000816 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000817 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000818 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000819 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000820 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000821 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000822 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000823 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000824 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000825 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000826 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000827 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000828 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000829 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000830 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000831 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000832 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000833 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000834 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000835 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000836 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000837 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000838 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000839 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000840 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000841 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000842 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000843 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000844 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000845 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000846 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000847 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000848 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000849 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000850 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000851 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000852 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000853 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000854 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000855 break;
856 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000857 while (Tok.isNot(tok::annot_pragma_openmp_end))
858 ConsumeAnyToken();
859 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000860 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000861}
862
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000863/// \brief Parsing of declarative or executable OpenMP directives.
864///
865/// threadprivate-directive:
866/// annot_pragma_openmp 'threadprivate' simple-variable-list
867/// annot_pragma_openmp_end
868///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000869/// declare-reduction-directive:
870/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
871/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
872/// ('omp_priv' '=' <expression>|<function_call>) ')']
873/// annot_pragma_openmp_end
874///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000875/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000876/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000877/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
878/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000879/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000880/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000881/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000882/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000883/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000884/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000885/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000886/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000887/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000888/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000889/// 'teams distribute parallel for' | 'target teams' |
890/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000891/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000892/// 'target teams distribute parallel for simd' |
893/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000894/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000895///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000896StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000897 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000898 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000899 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000900 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000901 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000902 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000903 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
904 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000905 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000906 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000907 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908 // Name of critical directive.
909 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000910 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000911 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000912 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000913
914 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000915 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000916 if (Allowed != ACK_Any) {
917 Diag(Tok, diag::err_omp_immediate_directive)
918 << getOpenMPDirectiveName(DKind) << 0;
919 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000921 ThreadprivateListParserHelper Helper(this);
922 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923 // The last seen token is annot_pragma_openmp_end - need to check for
924 // extra tokens.
925 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
926 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000927 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000928 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000930 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
931 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000932 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
933 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000934 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000936 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000937 case OMPD_declare_reduction:
938 ConsumeToken();
939 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
940 // The last seen token is annot_pragma_openmp_end - need to check for
941 // extra tokens.
942 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
943 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
944 << getOpenMPDirectiveName(OMPD_declare_reduction);
945 while (Tok.isNot(tok::annot_pragma_openmp_end))
946 ConsumeAnyToken();
947 }
948 ConsumeAnyToken();
949 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
950 } else
951 SkipUntil(tok::annot_pragma_openmp_end);
952 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000953 case OMPD_flush:
954 if (PP.LookAhead(0).is(tok::l_paren)) {
955 FlushHasClause = true;
956 // Push copy of the current token back to stream to properly parse
957 // pseudo-clause OMPFlushClause.
958 PP.EnterToken(Tok);
959 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000960 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +0000961 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000962 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000963 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000964 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000965 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000966 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000967 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000968 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000969 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000970 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000971 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000972 }
973 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000974 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000975 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000976 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000977 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000978 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000979 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000980 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000981 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000982 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000983 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000984 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000985 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000986 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000987 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000988 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000989 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000990 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000991 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000992 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000993 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000994 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000995 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000996 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000997 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000998 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000999 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001000 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001001 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001002 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001003 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001004 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001005 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001006 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001007 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001008 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001009 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001010 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001011 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001012 case OMPD_target_teams_distribute_parallel_for_simd:
1013 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001014 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001015 // Parse directive name of the 'critical' directive if any.
1016 if (DKind == OMPD_critical) {
1017 BalancedDelimiterTracker T(*this, tok::l_paren,
1018 tok::annot_pragma_openmp_end);
1019 if (!T.consumeOpen()) {
1020 if (Tok.isAnyIdentifier()) {
1021 DirName =
1022 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1023 ConsumeAnyToken();
1024 } else {
1025 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1026 }
1027 T.consumeClose();
1028 }
Alexey Bataev80909872015-07-02 11:25:17 +00001029 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001030 CancelRegion = ParseOpenMPDirectiveKind(*this);
1031 if (Tok.isNot(tok::annot_pragma_openmp_end))
1032 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001033 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034
Alexey Bataevf29276e2014-06-18 04:14:57 +00001035 if (isOpenMPLoopDirective(DKind))
1036 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1037 if (isOpenMPSimdDirective(DKind))
1038 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1039 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001041
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001042 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001043 OpenMPClauseKind CKind =
1044 Tok.isAnnotation()
1045 ? OMPC_unknown
1046 : FlushHasClause ? OMPC_flush
1047 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001048 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001049 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001050 OMPClause *Clause =
1051 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001052 FirstClauses[CKind].setInt(true);
1053 if (Clause) {
1054 FirstClauses[CKind].setPointer(Clause);
1055 Clauses.push_back(Clause);
1056 }
1057
1058 // Skip ',' if any.
1059 if (Tok.is(tok::comma))
1060 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001061 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001062 }
1063 // End location of the directive.
1064 EndLoc = Tok.getLocation();
1065 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001066 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001067
Alexey Bataeveb482352015-12-18 05:05:56 +00001068 // OpenMP [2.13.8, ordered Construct, Syntax]
1069 // If the depend clause is specified, the ordered construct is a stand-alone
1070 // directive.
1071 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001072 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001073 Diag(Loc, diag::err_omp_immediate_directive)
1074 << getOpenMPDirectiveName(DKind) << 1
1075 << getOpenMPClauseName(OMPC_depend);
1076 }
1077 HasAssociatedStatement = false;
1078 }
1079
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001081 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082 // The body is a block scope like in Lambdas and Blocks.
1083 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001084 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085 Actions.ActOnStartOfCompoundStmt();
1086 // Parse statement
1087 AssociatedStmt = ParseStatement();
1088 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001089 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001090 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1091 DKind == OMPD_target_exit_data) {
1092 Sema::CompoundScopeRAII CompoundScope(Actions);
1093 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
1094 Actions.ActOnStartOfCompoundStmt();
1095 AssociatedStmt =
1096 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None, /*isStmtExpr=*/false);
1097 Actions.ActOnFinishOfCompoundStmt();
1098 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001099 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001100 Directive = Actions.ActOnOpenMPExecutableDirective(
1101 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1102 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001103
1104 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001105 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001106 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001107 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001108 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001109 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001110 case OMPD_declare_target:
1111 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001112 Diag(Tok, diag::err_omp_unexpected_directive)
1113 << getOpenMPDirectiveName(DKind);
1114 SkipUntil(tok::annot_pragma_openmp_end);
1115 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001116 case OMPD_unknown:
1117 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001118 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001119 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001120 }
1121 return Directive;
1122}
1123
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001124// Parses simple list:
1125// simple-variable-list:
1126// '(' id-expression {, id-expression} ')'
1127//
1128bool Parser::ParseOpenMPSimpleVarList(
1129 OpenMPDirectiveKind Kind,
1130 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1131 Callback,
1132 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001133 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001134 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001135 if (T.expectAndConsume(diag::err_expected_lparen_after,
1136 getOpenMPDirectiveName(Kind)))
1137 return true;
1138 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001139 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001140
1141 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001142 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001143 CXXScopeSpec SS;
1144 SourceLocation TemplateKWLoc;
1145 UnqualifiedId Name;
1146 // Read var name.
1147 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001149
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001151 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001152 IsCorrect = false;
1153 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001154 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001155 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001156 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001157 IsCorrect = false;
1158 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001159 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1161 Tok.isNot(tok::annot_pragma_openmp_end)) {
1162 IsCorrect = false;
1163 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001164 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001165 Diag(PrevTok.getLocation(), diag::err_expected)
1166 << tok::identifier
1167 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001168 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001169 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001170 }
1171 // Consume ','.
1172 if (Tok.is(tok::comma)) {
1173 ConsumeToken();
1174 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001175 }
1176
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001178 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179 IsCorrect = false;
1180 }
1181
1182 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001183 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001184
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001185 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001186}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001187
1188/// \brief Parsing of OpenMP clauses.
1189///
1190/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001191/// if-clause | final-clause | num_threads-clause | safelen-clause |
1192/// default-clause | private-clause | firstprivate-clause | shared-clause
1193/// | linear-clause | aligned-clause | collapse-clause |
1194/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001195/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001196/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001197/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001198/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001199/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001200/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001201/// from-clause | is_device_ptr-clause | task_reduction-clause |
1202/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001203///
1204OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1205 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001206 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001207 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001208 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001209 // Check if clause is allowed for the given directive.
1210 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001211 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1212 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001213 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001214 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001215 }
1216
1217 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001218 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001219 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001220 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001221 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001222 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001223 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001224 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001225 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001226 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001227 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001228 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001229 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001230 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001231 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001232 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001233 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001234 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001235 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001236 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001237 // OpenMP [2.9.1, target data construct, Restrictions]
1238 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001239 // OpenMP [2.11.1, task Construct, Restrictions]
1240 // At most one if clause can appear on the directive.
1241 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001242 // OpenMP [teams Construct, Restrictions]
1243 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001244 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001245 // OpenMP [2.9.1, task Construct, Restrictions]
1246 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001247 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1248 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001249 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1250 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001251 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001252 Diag(Tok, diag::err_omp_more_one_clause)
1253 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001254 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001255 }
1256
Alexey Bataev10e775f2015-07-30 11:36:16 +00001257 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001258 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001259 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001260 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001261 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001262 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001263 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001264 // OpenMP [2.14.3.1, Restrictions]
1265 // Only a single default clause may be specified on a parallel, task or
1266 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001267 // OpenMP [2.5, parallel Construct, Restrictions]
1268 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001269 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001270 Diag(Tok, diag::err_omp_more_one_clause)
1271 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001272 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001273 }
1274
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001275 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001276 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001277 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001278 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001279 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001280 // OpenMP [2.7.1, Restrictions, p. 3]
1281 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001282 // OpenMP [2.10.4, Restrictions, p. 106]
1283 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001284 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001285 Diag(Tok, diag::err_omp_more_one_clause)
1286 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001287 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001288 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001289 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001290
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001291 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001292 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001293 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001294 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001295 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001296 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001297 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001298 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001299 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001300 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001301 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001302 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001303 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001304 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001305 // OpenMP [2.7.1, Restrictions, p. 9]
1306 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001307 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1308 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +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 Bataev142e1fc2014-06-20 09:44:06 +00001313 }
1314
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001315 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001316 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001317 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001318 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001319 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001320 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001321 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001322 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001323 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001324 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001325 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001326 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001327 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001328 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001329 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001330 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001331 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001332 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001333 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001334 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001335 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001336 break;
1337 case OMPC_unknown:
1338 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001339 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001340 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001341 break;
1342 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001343 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001344 if (!WrongDirective)
1345 Diag(Tok, diag::err_omp_unexpected_clause)
1346 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001347 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001348 break;
1349 }
Craig Topper161e4db2014-05-21 06:02:52 +00001350 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001351}
1352
Alexey Bataev2af33e32016-04-07 12:45:37 +00001353/// Parses simple expression in parens for single-expression clauses of OpenMP
1354/// constructs.
1355/// \param RLoc Returned location of right paren.
1356ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1357 SourceLocation &RLoc) {
1358 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1359 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1360 return ExprError();
1361
1362 SourceLocation ELoc = Tok.getLocation();
1363 ExprResult LHS(ParseCastExpression(
1364 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1365 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1366 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1367
1368 // Parse ')'.
1369 T.consumeClose();
1370
1371 RLoc = T.getCloseLocation();
1372 return Val;
1373}
1374
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001375/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001376/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001377/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001378///
Alexey Bataev3778b602014-07-17 07:32:53 +00001379/// final-clause:
1380/// 'final' '(' expression ')'
1381///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001382/// num_threads-clause:
1383/// 'num_threads' '(' expression ')'
1384///
1385/// safelen-clause:
1386/// 'safelen' '(' expression ')'
1387///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001388/// simdlen-clause:
1389/// 'simdlen' '(' expression ')'
1390///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001391/// collapse-clause:
1392/// 'collapse' '(' expression ')'
1393///
Alexey Bataeva0569352015-12-01 10:17:31 +00001394/// priority-clause:
1395/// 'priority' '(' expression ')'
1396///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001397/// grainsize-clause:
1398/// 'grainsize' '(' expression ')'
1399///
Alexey Bataev382967a2015-12-08 12:06:20 +00001400/// num_tasks-clause:
1401/// 'num_tasks' '(' expression ')'
1402///
Alexey Bataev28c75412015-12-15 08:19:24 +00001403/// hint-clause:
1404/// 'hint' '(' expression ')'
1405///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001406OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1407 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001408 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001409 SourceLocation LLoc = Tok.getLocation();
1410 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001411
Alexey Bataev2af33e32016-04-07 12:45:37 +00001412 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001413
1414 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001415 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001416
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001417 if (ParseOnly)
1418 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001419 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001420}
1421
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001422/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001423///
1424/// default-clause:
1425/// 'default' '(' 'none' | 'shared' ')
1426///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001427/// proc_bind-clause:
1428/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1429///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001430OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1431 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001432 SourceLocation Loc = Tok.getLocation();
1433 SourceLocation LOpen = ConsumeToken();
1434 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001435 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001436 if (T.expectAndConsume(diag::err_expected_lparen_after,
1437 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001438 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001439
Alexey Bataeva55ed262014-05-28 06:15:33 +00001440 unsigned Type = getOpenMPSimpleClauseType(
1441 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001442 SourceLocation TypeLoc = Tok.getLocation();
1443 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1444 Tok.isNot(tok::annot_pragma_openmp_end))
1445 ConsumeAnyToken();
1446
1447 // Parse ')'.
1448 T.consumeClose();
1449
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001450 if (ParseOnly)
1451 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001452 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1453 Tok.getLocation());
1454}
1455
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001456/// \brief Parsing of OpenMP clauses like 'ordered'.
1457///
1458/// ordered-clause:
1459/// 'ordered'
1460///
Alexey Bataev236070f2014-06-20 11:19:47 +00001461/// nowait-clause:
1462/// 'nowait'
1463///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001464/// untied-clause:
1465/// 'untied'
1466///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001467/// mergeable-clause:
1468/// 'mergeable'
1469///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001470/// read-clause:
1471/// 'read'
1472///
Alexey Bataev346265e2015-09-25 10:37:12 +00001473/// threads-clause:
1474/// 'threads'
1475///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001476/// simd-clause:
1477/// 'simd'
1478///
Alexey Bataevb825de12015-12-07 10:51:44 +00001479/// nogroup-clause:
1480/// 'nogroup'
1481///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001482OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001483 SourceLocation Loc = Tok.getLocation();
1484 ConsumeAnyToken();
1485
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001486 if (ParseOnly)
1487 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001488 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1489}
1490
1491
Alexey Bataev56dafe82014-06-20 07:16:17 +00001492/// \brief Parsing of OpenMP clauses with single expressions and some additional
1493/// argument like 'schedule' or 'dist_schedule'.
1494///
1495/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001496/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1497/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001498///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001499/// if-clause:
1500/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1501///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001502/// defaultmap:
1503/// 'defaultmap' '(' modifier ':' kind ')'
1504///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001505OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1506 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001507 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001508 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001509 // Parse '('.
1510 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1511 if (T.expectAndConsume(diag::err_expected_lparen_after,
1512 getOpenMPClauseName(Kind)))
1513 return nullptr;
1514
1515 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001516 SmallVector<unsigned, 4> Arg;
1517 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001518 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001519 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1520 Arg.resize(NumberOfElements);
1521 KLoc.resize(NumberOfElements);
1522 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1523 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1524 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1525 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001526 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001527 if (KindModifier > OMPC_SCHEDULE_unknown) {
1528 // Parse 'modifier'
1529 Arg[Modifier1] = KindModifier;
1530 KLoc[Modifier1] = Tok.getLocation();
1531 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1532 Tok.isNot(tok::annot_pragma_openmp_end))
1533 ConsumeAnyToken();
1534 if (Tok.is(tok::comma)) {
1535 // Parse ',' 'modifier'
1536 ConsumeAnyToken();
1537 KindModifier = getOpenMPSimpleClauseType(
1538 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1539 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1540 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001541 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001542 KLoc[Modifier2] = Tok.getLocation();
1543 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1544 Tok.isNot(tok::annot_pragma_openmp_end))
1545 ConsumeAnyToken();
1546 }
1547 // Parse ':'
1548 if (Tok.is(tok::colon))
1549 ConsumeAnyToken();
1550 else
1551 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1552 KindModifier = getOpenMPSimpleClauseType(
1553 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1554 }
1555 Arg[ScheduleKind] = KindModifier;
1556 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001557 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1558 Tok.isNot(tok::annot_pragma_openmp_end))
1559 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001560 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1561 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1562 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001563 Tok.is(tok::comma))
1564 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001565 } else if (Kind == OMPC_dist_schedule) {
1566 Arg.push_back(getOpenMPSimpleClauseType(
1567 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1568 KLoc.push_back(Tok.getLocation());
1569 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1570 Tok.isNot(tok::annot_pragma_openmp_end))
1571 ConsumeAnyToken();
1572 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1573 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001574 } else if (Kind == OMPC_defaultmap) {
1575 // Get a defaultmap modifier
1576 Arg.push_back(getOpenMPSimpleClauseType(
1577 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1578 KLoc.push_back(Tok.getLocation());
1579 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1580 Tok.isNot(tok::annot_pragma_openmp_end))
1581 ConsumeAnyToken();
1582 // Parse ':'
1583 if (Tok.is(tok::colon))
1584 ConsumeAnyToken();
1585 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1586 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1587 // Get a defaultmap kind
1588 Arg.push_back(getOpenMPSimpleClauseType(
1589 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1590 KLoc.push_back(Tok.getLocation());
1591 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1592 Tok.isNot(tok::annot_pragma_openmp_end))
1593 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001594 } else {
1595 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001596 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001597 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001598 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1599 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001600 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001601 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1602 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001603 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001604 } else {
1605 TPA.Revert();
1606 Arg.back() = OMPD_unknown;
1607 }
1608 } else
1609 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001610 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001611
Carlo Bertollib4adf552016-01-15 18:50:31 +00001612 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1613 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1614 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001615 if (NeedAnExpression) {
1616 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001617 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1618 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001619 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001620 }
1621
1622 // Parse ')'.
1623 T.consumeClose();
1624
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001625 if (NeedAnExpression && Val.isInvalid())
1626 return nullptr;
1627
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001628 if (ParseOnly)
1629 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001630 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001631 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001632 T.getCloseLocation());
1633}
1634
Alexey Bataevc5e02582014-06-16 07:08:35 +00001635static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1636 UnqualifiedId &ReductionId) {
1637 SourceLocation TemplateKWLoc;
1638 if (ReductionIdScopeSpec.isEmpty()) {
1639 auto OOK = OO_None;
1640 switch (P.getCurToken().getKind()) {
1641 case tok::plus:
1642 OOK = OO_Plus;
1643 break;
1644 case tok::minus:
1645 OOK = OO_Minus;
1646 break;
1647 case tok::star:
1648 OOK = OO_Star;
1649 break;
1650 case tok::amp:
1651 OOK = OO_Amp;
1652 break;
1653 case tok::pipe:
1654 OOK = OO_Pipe;
1655 break;
1656 case tok::caret:
1657 OOK = OO_Caret;
1658 break;
1659 case tok::ampamp:
1660 OOK = OO_AmpAmp;
1661 break;
1662 case tok::pipepipe:
1663 OOK = OO_PipePipe;
1664 break;
1665 default:
1666 break;
1667 }
1668 if (OOK != OO_None) {
1669 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001670 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001671 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1672 return false;
1673 }
1674 }
1675 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1676 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001677 /*AllowConstructorName*/ false,
1678 /*AllowDeductionGuide*/ false,
1679 nullptr, TemplateKWLoc, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001680}
1681
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001682/// Parses clauses with list.
1683bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1684 OpenMPClauseKind Kind,
1685 SmallVectorImpl<Expr *> &Vars,
1686 OpenMPVarListDataTy &Data) {
1687 UnqualifiedId UnqualifiedReductionId;
1688 bool InvalidReductionId = false;
1689 bool MapTypeModifierSpecified = false;
1690
1691 // Parse '('.
1692 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1693 if (T.expectAndConsume(diag::err_expected_lparen_after,
1694 getOpenMPClauseName(Kind)))
1695 return true;
1696
1697 bool NeedRParenForLinear = false;
1698 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1699 tok::annot_pragma_openmp_end);
1700 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001701 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1702 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001703 ColonProtectionRAIIObject ColonRAII(*this);
1704 if (getLangOpts().CPlusPlus)
1705 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1706 /*ObjectType=*/nullptr,
1707 /*EnteringContext=*/false);
1708 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1709 UnqualifiedReductionId);
1710 if (InvalidReductionId) {
1711 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1712 StopBeforeMatch);
1713 }
1714 if (Tok.is(tok::colon))
1715 Data.ColonLoc = ConsumeToken();
1716 else
1717 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1718 if (!InvalidReductionId)
1719 Data.ReductionId =
1720 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1721 } else if (Kind == OMPC_depend) {
1722 // Handle dependency type for depend clause.
1723 ColonProtectionRAIIObject ColonRAII(*this);
1724 Data.DepKind =
1725 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1726 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1727 Data.DepLinMapLoc = Tok.getLocation();
1728
1729 if (Data.DepKind == OMPC_DEPEND_unknown) {
1730 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1731 StopBeforeMatch);
1732 } else {
1733 ConsumeToken();
1734 // Special processing for depend(source) clause.
1735 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1736 // Parse ')'.
1737 T.consumeClose();
1738 return false;
1739 }
1740 }
1741 if (Tok.is(tok::colon))
1742 Data.ColonLoc = ConsumeToken();
1743 else {
1744 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1745 : diag::warn_pragma_expected_colon)
1746 << "dependency type";
1747 }
1748 } else if (Kind == OMPC_linear) {
1749 // Try to parse modifier if any.
1750 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1751 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1752 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1753 Data.DepLinMapLoc = ConsumeToken();
1754 LinearT.consumeOpen();
1755 NeedRParenForLinear = true;
1756 }
1757 } else if (Kind == OMPC_map) {
1758 // Handle map type for map clause.
1759 ColonProtectionRAIIObject ColonRAII(*this);
1760
1761 /// The map clause modifier token can be either a identifier or the C++
1762 /// delete keyword.
1763 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1764 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1765 };
1766
1767 // The first identifier may be a list item, a map-type or a
1768 // map-type-modifier. The map modifier can also be delete which has the same
1769 // spelling of the C++ delete keyword.
1770 Data.MapType =
1771 IsMapClauseModifierToken(Tok)
1772 ? static_cast<OpenMPMapClauseKind>(
1773 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1774 : OMPC_MAP_unknown;
1775 Data.DepLinMapLoc = Tok.getLocation();
1776 bool ColonExpected = false;
1777
1778 if (IsMapClauseModifierToken(Tok)) {
1779 if (PP.LookAhead(0).is(tok::colon)) {
1780 if (Data.MapType == OMPC_MAP_unknown)
1781 Diag(Tok, diag::err_omp_unknown_map_type);
1782 else if (Data.MapType == OMPC_MAP_always)
1783 Diag(Tok, diag::err_omp_map_type_missing);
1784 ConsumeToken();
1785 } else if (PP.LookAhead(0).is(tok::comma)) {
1786 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1787 PP.LookAhead(2).is(tok::colon)) {
1788 Data.MapTypeModifier = Data.MapType;
1789 if (Data.MapTypeModifier != OMPC_MAP_always) {
1790 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1791 Data.MapTypeModifier = OMPC_MAP_unknown;
1792 } else
1793 MapTypeModifierSpecified = true;
1794
1795 ConsumeToken();
1796 ConsumeToken();
1797
1798 Data.MapType =
1799 IsMapClauseModifierToken(Tok)
1800 ? static_cast<OpenMPMapClauseKind>(
1801 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1802 : OMPC_MAP_unknown;
1803 if (Data.MapType == OMPC_MAP_unknown ||
1804 Data.MapType == OMPC_MAP_always)
1805 Diag(Tok, diag::err_omp_unknown_map_type);
1806 ConsumeToken();
1807 } else {
1808 Data.MapType = OMPC_MAP_tofrom;
1809 Data.IsMapTypeImplicit = true;
1810 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001811 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1812 if (PP.LookAhead(1).is(tok::colon)) {
1813 Data.MapTypeModifier = Data.MapType;
1814 if (Data.MapTypeModifier != OMPC_MAP_always) {
1815 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1816 Data.MapTypeModifier = OMPC_MAP_unknown;
1817 } else
1818 MapTypeModifierSpecified = true;
1819
1820 ConsumeToken();
1821
1822 Data.MapType =
1823 IsMapClauseModifierToken(Tok)
1824 ? static_cast<OpenMPMapClauseKind>(
1825 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1826 : OMPC_MAP_unknown;
1827 if (Data.MapType == OMPC_MAP_unknown ||
1828 Data.MapType == OMPC_MAP_always)
1829 Diag(Tok, diag::err_omp_unknown_map_type);
1830 ConsumeToken();
1831 } else {
1832 Data.MapType = OMPC_MAP_tofrom;
1833 Data.IsMapTypeImplicit = true;
1834 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001835 } else {
1836 Data.MapType = OMPC_MAP_tofrom;
1837 Data.IsMapTypeImplicit = true;
1838 }
1839 } else {
1840 Data.MapType = OMPC_MAP_tofrom;
1841 Data.IsMapTypeImplicit = true;
1842 }
1843
1844 if (Tok.is(tok::colon))
1845 Data.ColonLoc = ConsumeToken();
1846 else if (ColonExpected)
1847 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1848 }
1849
Alexey Bataevfa312f32017-07-21 18:48:21 +00001850 bool IsComma =
1851 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1852 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1853 (Kind == OMPC_reduction && !InvalidReductionId) ||
1854 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1855 (!MapTypeModifierSpecified ||
1856 Data.MapTypeModifier == OMPC_MAP_always)) ||
1857 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001858 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1859 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1860 Tok.isNot(tok::annot_pragma_openmp_end))) {
1861 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1862 // Parse variable
1863 ExprResult VarExpr =
1864 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1865 if (VarExpr.isUsable())
1866 Vars.push_back(VarExpr.get());
1867 else {
1868 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1869 StopBeforeMatch);
1870 }
1871 // Skip ',' if any
1872 IsComma = Tok.is(tok::comma);
1873 if (IsComma)
1874 ConsumeToken();
1875 else if (Tok.isNot(tok::r_paren) &&
1876 Tok.isNot(tok::annot_pragma_openmp_end) &&
1877 (!MayHaveTail || Tok.isNot(tok::colon)))
1878 Diag(Tok, diag::err_omp_expected_punc)
1879 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1880 : getOpenMPClauseName(Kind))
1881 << (Kind == OMPC_flush);
1882 }
1883
1884 // Parse ')' for linear clause with modifier.
1885 if (NeedRParenForLinear)
1886 LinearT.consumeClose();
1887
1888 // Parse ':' linear-step (or ':' alignment).
1889 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1890 if (MustHaveTail) {
1891 Data.ColonLoc = Tok.getLocation();
1892 SourceLocation ELoc = ConsumeToken();
1893 ExprResult Tail = ParseAssignmentExpression();
1894 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1895 if (Tail.isUsable())
1896 Data.TailExpr = Tail.get();
1897 else
1898 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1899 StopBeforeMatch);
1900 }
1901
1902 // Parse ')'.
1903 T.consumeClose();
1904 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1905 Vars.empty()) ||
1906 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1907 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1908 return true;
1909 return false;
1910}
1911
Alexander Musman1bb328c2014-06-04 13:06:39 +00001912/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001913/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
1914/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001915///
1916/// private-clause:
1917/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001918/// firstprivate-clause:
1919/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001920/// lastprivate-clause:
1921/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001922/// shared-clause:
1923/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001924/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001925/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001926/// aligned-clause:
1927/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001928/// reduction-clause:
1929/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00001930/// task_reduction-clause:
1931/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00001932/// in_reduction-clause:
1933/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001934/// copyprivate-clause:
1935/// 'copyprivate' '(' list ')'
1936/// flush-clause:
1937/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001938/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001939/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001940/// map-clause:
1941/// 'map' '(' [ [ always , ]
1942/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001943/// to-clause:
1944/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001945/// from-clause:
1946/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001947/// use_device_ptr-clause:
1948/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001949/// is_device_ptr-clause:
1950/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001951///
Alexey Bataev182227b2015-08-20 10:54:39 +00001952/// For 'linear' clause linear-list may have the following forms:
1953/// list
1954/// modifier(list)
1955/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001956OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001957 OpenMPClauseKind Kind,
1958 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001959 SourceLocation Loc = Tok.getLocation();
1960 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001961 SmallVector<Expr *, 4> Vars;
1962 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001963
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001964 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001965 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001966
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001967 if (ParseOnly)
1968 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001969 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001970 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1971 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1972 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1973 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001974}
1975