blob: 2308e6e3da9a8a465e242a2c44fee50331bfe2c1 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000015#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000016#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000020
Alexey Bataeva769e072013-03-22 06:34:35 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// OpenMP declarative directives.
25//===----------------------------------------------------------------------===//
26
Dmitry Polukhin82478332016-02-13 06:53:38 +000027namespace {
28enum OpenMPDirectiveKindEx {
29 OMPD_cancellation = OMPD_unknown + 1,
30 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000031 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000032 OMPD_end,
33 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000034 OMPD_enter,
35 OMPD_exit,
36 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000037 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000038 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000039 OMPD_target_exit,
40 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000041 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000042 OMPD_teams_distribute_parallel,
43 OMPD_target_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000044};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000045
46class ThreadprivateListParserHelper final {
47 SmallVector<Expr *, 4> Identifiers;
48 Parser *P;
49
50public:
51 ThreadprivateListParserHelper(Parser *P) : P(P) {}
52 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
53 ExprResult Res =
54 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
55 if (Res.isUsable())
56 Identifiers.push_back(Res.get());
57 }
58 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
59};
Dmitry Polukhin82478332016-02-13 06:53:38 +000060} // namespace
61
62// Map token string to extended OMP token kind that are
63// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
64static unsigned getOpenMPDirectiveKindEx(StringRef S) {
65 auto DKind = getOpenMPDirectiveKind(S);
66 if (DKind != OMPD_unknown)
67 return DKind;
68
69 return llvm::StringSwitch<unsigned>(S)
70 .Case("cancellation", OMPD_cancellation)
71 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000072 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000073 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000074 .Case("enter", OMPD_enter)
75 .Case("exit", OMPD_exit)
76 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000077 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000078 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000079 .Default(OMPD_unknown);
80}
81
Alexey Bataev61908f652018-04-23 19:53:05 +000082static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000083 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
84 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
85 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000087 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
88 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
89 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
90 {OMPD_declare, OMPD_target, OMPD_declare_target},
91 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
92 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
93 {OMPD_distribute_parallel_for, OMPD_simd,
94 OMPD_distribute_parallel_for_simd},
95 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
96 {OMPD_end, OMPD_declare, OMPD_end_declare},
97 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
98 {OMPD_target, OMPD_data, OMPD_target_data},
99 {OMPD_target, OMPD_enter, OMPD_target_enter},
100 {OMPD_target, OMPD_exit, OMPD_target_exit},
101 {OMPD_target, OMPD_update, OMPD_target_update},
102 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
103 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
104 {OMPD_for, OMPD_simd, OMPD_for_simd},
105 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
106 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
107 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
108 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
109 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
110 {OMPD_target, OMPD_simd, OMPD_target_simd},
111 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
112 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
113 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
114 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
115 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
116 {OMPD_teams_distribute_parallel, OMPD_for,
117 OMPD_teams_distribute_parallel_for},
118 {OMPD_teams_distribute_parallel_for, OMPD_simd,
119 OMPD_teams_distribute_parallel_for_simd},
120 {OMPD_target, OMPD_teams, OMPD_target_teams},
121 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
122 {OMPD_target_teams_distribute, OMPD_parallel,
123 OMPD_target_teams_distribute_parallel},
124 {OMPD_target_teams_distribute, OMPD_simd,
125 OMPD_target_teams_distribute_simd},
126 {OMPD_target_teams_distribute_parallel, OMPD_for,
127 OMPD_target_teams_distribute_parallel_for},
128 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
129 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000130 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000131 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000132 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000133 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000134 ? static_cast<unsigned>(OMPD_unknown)
135 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
136 if (DKind == OMPD_unknown)
137 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000138
Alexey Bataev61908f652018-04-23 19:53:05 +0000139 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
140 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000141 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000142
Dmitry Polukhin82478332016-02-13 06:53:38 +0000143 Tok = P.getPreprocessor().LookAhead(0);
144 unsigned SDKind =
145 Tok.isAnnotation()
146 ? static_cast<unsigned>(OMPD_unknown)
147 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
148 if (SDKind == OMPD_unknown)
149 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000150
Alexey Bataev61908f652018-04-23 19:53:05 +0000151 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000152 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000153 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000154 }
155 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000156 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
157 : OMPD_unknown;
158}
159
160static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000161 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000162 Sema &Actions = P.getActions();
163 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000164 // Allow to use 'operator' keyword for C++ operators
165 bool WithOperator = false;
166 if (Tok.is(tok::kw_operator)) {
167 P.ConsumeToken();
168 Tok = P.getCurToken();
169 WithOperator = true;
170 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000171 switch (Tok.getKind()) {
172 case tok::plus: // '+'
173 OOK = OO_Plus;
174 break;
175 case tok::minus: // '-'
176 OOK = OO_Minus;
177 break;
178 case tok::star: // '*'
179 OOK = OO_Star;
180 break;
181 case tok::amp: // '&'
182 OOK = OO_Amp;
183 break;
184 case tok::pipe: // '|'
185 OOK = OO_Pipe;
186 break;
187 case tok::caret: // '^'
188 OOK = OO_Caret;
189 break;
190 case tok::ampamp: // '&&'
191 OOK = OO_AmpAmp;
192 break;
193 case tok::pipepipe: // '||'
194 OOK = OO_PipePipe;
195 break;
196 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000197 if (!WithOperator)
198 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000199 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000200 default:
201 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
202 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
203 Parser::StopBeforeMatch);
204 return DeclarationName();
205 }
206 P.ConsumeToken();
207 auto &DeclNames = Actions.getASTContext().DeclarationNames;
208 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
209 : DeclNames.getCXXOperatorName(OOK);
210}
211
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000212/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000213///
214/// declare-reduction-directive:
215/// annot_pragma_openmp 'declare' 'reduction'
216/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
217/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
218/// annot_pragma_openmp_end
219/// <reduction_id> is either a base language identifier or one of the following
220/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
221///
222Parser::DeclGroupPtrTy
223Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
224 // Parse '('.
225 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
226 if (T.expectAndConsume(diag::err_expected_lparen_after,
227 getOpenMPDirectiveName(OMPD_declare_reduction))) {
228 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
229 return DeclGroupPtrTy();
230 }
231
232 DeclarationName Name = parseOpenMPReductionId(*this);
233 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
234 return DeclGroupPtrTy();
235
236 // Consume ':'.
237 bool IsCorrect = !ExpectAndConsume(tok::colon);
238
239 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
240 return DeclGroupPtrTy();
241
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000242 IsCorrect = IsCorrect && !Name.isEmpty();
243
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000244 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
245 Diag(Tok.getLocation(), diag::err_expected_type);
246 IsCorrect = false;
247 }
248
249 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
250 return DeclGroupPtrTy();
251
252 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
253 // Parse list of types until ':' token.
254 do {
255 ColonProtectionRAIIObject ColonRAII(*this);
256 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000257 TypeResult TR =
258 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000259 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000260 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000261 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
262 if (!ReductionType.isNull()) {
263 ReductionTypes.push_back(
264 std::make_pair(ReductionType, Range.getBegin()));
265 }
266 } else {
267 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
268 StopBeforeMatch);
269 }
270
271 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
272 break;
273
274 // Consume ','.
275 if (ExpectAndConsume(tok::comma)) {
276 IsCorrect = false;
277 if (Tok.is(tok::annot_pragma_openmp_end)) {
278 Diag(Tok.getLocation(), diag::err_expected_type);
279 return DeclGroupPtrTy();
280 }
281 }
282 } while (Tok.isNot(tok::annot_pragma_openmp_end));
283
284 if (ReductionTypes.empty()) {
285 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
286 return DeclGroupPtrTy();
287 }
288
289 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
290 return DeclGroupPtrTy();
291
292 // Consume ':'.
293 if (ExpectAndConsume(tok::colon))
294 IsCorrect = false;
295
296 if (Tok.is(tok::annot_pragma_openmp_end)) {
297 Diag(Tok.getLocation(), diag::err_expected_expression);
298 return DeclGroupPtrTy();
299 }
300
301 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
302 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
303
304 // Parse <combiner> expression and then parse initializer if any for each
305 // correct type.
306 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000307 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000308 TentativeParsingAction TPA(*this);
309 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000310 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000311 Scope::OpenMPDirectiveScope);
312 // Parse <combiner> expression.
313 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
314 ExprResult CombinerResult =
315 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000316 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000317 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
318
319 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
320 Tok.isNot(tok::annot_pragma_openmp_end)) {
321 TPA.Commit();
322 IsCorrect = false;
323 break;
324 }
325 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
326 ExprResult InitializerResult;
327 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
328 // Parse <initializer> expression.
329 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000330 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000331 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000332 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000333 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
334 TPA.Commit();
335 IsCorrect = false;
336 break;
337 }
338 // Parse '('.
339 BalancedDelimiterTracker T(*this, tok::l_paren,
340 tok::annot_pragma_openmp_end);
341 IsCorrect =
342 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
343 IsCorrect;
344 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
345 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000346 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000347 Scope::OpenMPDirectiveScope);
348 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000349 VarDecl *OmpPrivParm =
350 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
351 D);
352 // Check if initializer is omp_priv <init_expr> or something else.
353 if (Tok.is(tok::identifier) &&
354 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000355 if (Actions.getLangOpts().CPlusPlus) {
356 InitializerResult = Actions.ActOnFinishFullExpr(
357 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000358 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000359 } else {
360 ConsumeToken();
361 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
362 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000363 } else {
364 InitializerResult = Actions.ActOnFinishFullExpr(
365 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000366 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000367 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000368 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000369 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000370 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
371 Tok.isNot(tok::annot_pragma_openmp_end)) {
372 TPA.Commit();
373 IsCorrect = false;
374 break;
375 }
376 IsCorrect =
377 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
378 }
379 }
380
381 ++I;
382 // Revert parsing if not the last type, otherwise accept it, we're done with
383 // parsing.
384 if (I != E)
385 TPA.Revert();
386 else
387 TPA.Commit();
388 }
389 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
390 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000391}
392
Alexey Bataev070f43a2017-09-06 14:49:58 +0000393void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
394 // Parse declarator '=' initializer.
395 // If a '==' or '+=' is found, suggest a fixit to '='.
396 if (isTokenEqualOrEqualTypo()) {
397 ConsumeToken();
398
399 if (Tok.is(tok::code_completion)) {
400 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
401 Actions.FinalizeDeclaration(OmpPrivParm);
402 cutOffParsing();
403 return;
404 }
405
406 ExprResult Init(ParseInitializer());
407
408 if (Init.isInvalid()) {
409 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
410 Actions.ActOnInitializerError(OmpPrivParm);
411 } else {
412 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
413 /*DirectInit=*/false);
414 }
415 } else if (Tok.is(tok::l_paren)) {
416 // Parse C++ direct initializer: '(' expression-list ')'
417 BalancedDelimiterTracker T(*this, tok::l_paren);
418 T.consumeOpen();
419
420 ExprVector Exprs;
421 CommaLocsTy CommaLocs;
422
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000423 SourceLocation LParLoc = T.getOpenLocation();
424 if (ParseExpressionList(
425 Exprs, CommaLocs, [this, OmpPrivParm, LParLoc, &Exprs] {
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000426 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000427 getCurScope(),
428 OmpPrivParm->getType()->getCanonicalTypeInternal(),
429 OmpPrivParm->getLocation(), Exprs, LParLoc);
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000430 CalledSignatureHelp = true;
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000431 Actions.CodeCompleteExpression(getCurScope(), PreferredType);
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000432 })) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000433 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) {
434 Actions.ProduceConstructorSignatureHelp(
435 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
436 OmpPrivParm->getLocation(), Exprs, LParLoc);
437 CalledSignatureHelp = true;
438 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000439 Actions.ActOnInitializerError(OmpPrivParm);
440 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
441 } else {
442 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000443 SourceLocation RLoc = Tok.getLocation();
444 if (!T.consumeClose())
445 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000446
447 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
448 "Unexpected number of commas!");
449
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000450 ExprResult Initializer =
451 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000452 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
453 /*DirectInit=*/true);
454 }
455 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
456 // Parse C++0x braced-init-list.
457 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
458
459 ExprResult Init(ParseBraceInitializer());
460
461 if (Init.isInvalid()) {
462 Actions.ActOnInitializerError(OmpPrivParm);
463 } else {
464 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
465 /*DirectInit=*/true);
466 }
467 } else {
468 Actions.ActOnUninitializedDecl(OmpPrivParm);
469 }
470}
471
Alexey Bataev2af33e32016-04-07 12:45:37 +0000472namespace {
473/// RAII that recreates function context for correct parsing of clauses of
474/// 'declare simd' construct.
475/// OpenMP, 2.8.2 declare simd Construct
476/// The expressions appearing in the clauses of this directive are evaluated in
477/// the scope of the arguments of the function declaration or definition.
478class FNContextRAII final {
479 Parser &P;
480 Sema::CXXThisScopeRAII *ThisScope;
481 Parser::ParseScope *TempScope;
482 Parser::ParseScope *FnScope;
483 bool HasTemplateScope = false;
484 bool HasFunScope = false;
485 FNContextRAII() = delete;
486 FNContextRAII(const FNContextRAII &) = delete;
487 FNContextRAII &operator=(const FNContextRAII &) = delete;
488
489public:
490 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
491 Decl *D = *Ptr.get().begin();
492 NamedDecl *ND = dyn_cast<NamedDecl>(D);
493 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
494 Sema &Actions = P.getActions();
495
496 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000497 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000498 ND && ND->isCXXInstanceMember());
499
500 // If the Decl is templatized, add template parameters to scope.
501 HasTemplateScope = D->isTemplateDecl();
502 TempScope =
503 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
504 if (HasTemplateScope)
505 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
506
507 // If the Decl is on a function, add function parameters to the scope.
508 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000509 FnScope = new Parser::ParseScope(
510 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
511 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000512 if (HasFunScope)
513 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
514 }
515 ~FNContextRAII() {
516 if (HasFunScope) {
517 P.getActions().ActOnExitFunctionContext();
518 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
519 }
520 if (HasTemplateScope)
521 TempScope->Exit();
522 delete FnScope;
523 delete TempScope;
524 delete ThisScope;
525 }
526};
527} // namespace
528
Alexey Bataevd93d3762016-04-12 09:35:56 +0000529/// Parses clauses for 'declare simd' directive.
530/// clause:
531/// 'inbranch' | 'notinbranch'
532/// 'simdlen' '(' <expr> ')'
533/// { 'uniform' '(' <argument_list> ')' }
534/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000535/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
536static bool parseDeclareSimdClauses(
537 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
538 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
539 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
540 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000541 SourceRange BSRange;
542 const Token &Tok = P.getCurToken();
543 bool IsError = false;
544 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
545 if (Tok.isNot(tok::identifier))
546 break;
547 OMPDeclareSimdDeclAttr::BranchStateTy Out;
548 IdentifierInfo *II = Tok.getIdentifierInfo();
549 StringRef ClauseName = II->getName();
550 // Parse 'inranch|notinbranch' clauses.
551 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
552 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
553 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
554 << ClauseName
555 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
556 IsError = true;
557 }
558 BS = Out;
559 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
560 P.ConsumeToken();
561 } else if (ClauseName.equals("simdlen")) {
562 if (SimdLen.isUsable()) {
563 P.Diag(Tok, diag::err_omp_more_one_clause)
564 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
565 IsError = true;
566 }
567 P.ConsumeToken();
568 SourceLocation RLoc;
569 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
570 if (SimdLen.isInvalid())
571 IsError = true;
572 } else {
573 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000574 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
575 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000576 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000577 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000578 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000579 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000580 else if (CKind == OMPC_linear)
581 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000582
583 P.ConsumeToken();
584 if (P.ParseOpenMPVarList(OMPD_declare_simd,
585 getOpenMPClauseKind(ClauseName), *Vars, Data))
586 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000587 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000588 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000589 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000590 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
591 Data.DepLinMapLoc))
592 Data.LinKind = OMPC_LINEAR_val;
593 LinModifiers.append(Linears.size() - LinModifiers.size(),
594 Data.LinKind);
595 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
596 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000597 } else
598 // TODO: add parsing of other clauses.
599 break;
600 }
601 // Skip ',' if any.
602 if (Tok.is(tok::comma))
603 P.ConsumeToken();
604 }
605 return IsError;
606}
607
Alexey Bataev2af33e32016-04-07 12:45:37 +0000608/// Parse clauses for '#pragma omp declare simd'.
609Parser::DeclGroupPtrTy
610Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
611 CachedTokens &Toks, SourceLocation Loc) {
612 PP.EnterToken(Tok);
613 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
614 // Consume the previously pushed token.
615 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
616
617 FNContextRAII FnContext(*this, Ptr);
618 OMPDeclareSimdDeclAttr::BranchStateTy BS =
619 OMPDeclareSimdDeclAttr::BS_Undefined;
620 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000621 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000622 SmallVector<Expr *, 4> Aligneds;
623 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000624 SmallVector<Expr *, 4> Linears;
625 SmallVector<unsigned, 4> LinModifiers;
626 SmallVector<Expr *, 4> Steps;
627 bool IsError =
628 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
629 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000630 // Need to check for extra tokens.
631 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
632 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
633 << getOpenMPDirectiveName(OMPD_declare_simd);
634 while (Tok.isNot(tok::annot_pragma_openmp_end))
635 ConsumeAnyToken();
636 }
637 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000638 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000639 if (IsError)
640 return Ptr;
641 return Actions.ActOnOpenMPDeclareSimdDirective(
642 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
643 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000644}
645
Kelvin Lie0502752018-11-21 20:15:57 +0000646Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
647 // OpenMP 4.5 syntax with list of entities.
648 Sema::NamedDeclSetType SameDirectiveDecls;
649 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
650 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
651 if (Tok.is(tok::identifier)) {
652 IdentifierInfo *II = Tok.getIdentifierInfo();
653 StringRef ClauseName = II->getName();
654 // Parse 'to|link' clauses.
655 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT)) {
656 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) << ClauseName;
657 break;
658 }
659 ConsumeToken();
660 }
661 auto &&Callback = [this, MT, &SameDirectiveDecls](
662 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
663 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
664 SameDirectiveDecls);
665 };
666 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
667 /*AllowScopeSpecifier=*/true))
668 break;
669
670 // Consume optional ','.
671 if (Tok.is(tok::comma))
672 ConsumeToken();
673 }
674 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
675 ConsumeAnyToken();
676 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
677 SameDirectiveDecls.end());
678 if (Decls.empty())
679 return DeclGroupPtrTy();
680 return Actions.BuildDeclaratorGroup(Decls);
681}
682
683void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
684 SourceLocation DTLoc) {
685 if (DKind != OMPD_end_declare_target) {
686 Diag(Tok, diag::err_expected_end_declare_target);
687 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
688 return;
689 }
690 ConsumeAnyToken();
691 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
692 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
693 << getOpenMPDirectiveName(OMPD_end_declare_target);
694 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
695 }
696 // Skip the last annot_pragma_openmp_end.
697 ConsumeAnyToken();
698}
699
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000700/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000701///
702/// threadprivate-directive:
703/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000704/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000705///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000706/// declare-reduction-directive:
707/// annot_pragma_openmp 'declare' 'reduction' [...]
708/// annot_pragma_openmp_end
709///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000710/// declare-simd-directive:
711/// annot_pragma_openmp 'declare simd' {<clause> [,]}
712/// annot_pragma_openmp_end
713/// <function declaration/definition>
714///
Kelvin Li1408f912018-09-26 04:28:39 +0000715/// requires directive:
716/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
717/// annot_pragma_openmp_end
718///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000719Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
720 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
721 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000722 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000723 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000724
Richard Smithaf3b3252017-05-18 19:21:48 +0000725 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000726 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000727
728 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000729 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000730 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000731 ThreadprivateListParserHelper Helper(this);
732 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000733 // The last seen token is annot_pragma_openmp_end - need to check for
734 // extra tokens.
735 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
736 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000737 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000738 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000739 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000740 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000741 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000742 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
743 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000744 }
745 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000746 }
Kelvin Li1408f912018-09-26 04:28:39 +0000747 case OMPD_requires: {
748 SourceLocation StartLoc = ConsumeToken();
749 SmallVector<OMPClause *, 5> Clauses;
750 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
751 FirstClauses(OMPC_unknown + 1);
752 if (Tok.is(tok::annot_pragma_openmp_end)) {
753 Diag(Tok, diag::err_omp_expected_clause)
754 << getOpenMPDirectiveName(OMPD_requires);
755 break;
756 }
757 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
758 OpenMPClauseKind CKind = Tok.isAnnotation()
759 ? OMPC_unknown
760 : getOpenMPClauseKind(PP.getSpelling(Tok));
761 Actions.StartOpenMPClause(CKind);
762 OMPClause *Clause =
763 ParseOpenMPClause(OMPD_requires, CKind, !FirstClauses[CKind].getInt());
764 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, StopBeforeMatch);
765 FirstClauses[CKind].setInt(true);
766 if (Clause != nullptr)
767 Clauses.push_back(Clause);
768 if (Tok.is(tok::annot_pragma_openmp_end)) {
769 Actions.EndOpenMPClause();
770 break;
771 }
772 // Skip ',' if any.
773 if (Tok.is(tok::comma))
774 ConsumeToken();
775 Actions.EndOpenMPClause();
776 }
777 // Consume final annot_pragma_openmp_end
778 if (Clauses.size() == 0) {
779 Diag(Tok, diag::err_omp_expected_clause)
780 << getOpenMPDirectiveName(OMPD_requires);
781 ConsumeAnnotationToken();
782 return nullptr;
783 }
784 ConsumeAnnotationToken();
785 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
786 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000787 case OMPD_declare_reduction:
788 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000789 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000790 // The last seen token is annot_pragma_openmp_end - need to check for
791 // extra tokens.
792 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
793 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
794 << getOpenMPDirectiveName(OMPD_declare_reduction);
795 while (Tok.isNot(tok::annot_pragma_openmp_end))
796 ConsumeAnyToken();
797 }
798 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000799 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000800 return Res;
801 }
802 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000803 case OMPD_declare_simd: {
804 // The syntax is:
805 // { #pragma omp declare simd }
806 // <function-declaration-or-definition>
807 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000808 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000809 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000810 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
811 Toks.push_back(Tok);
812 ConsumeAnyToken();
813 }
814 Toks.push_back(Tok);
815 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000816
817 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000818 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000819 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000820 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000821 // Here we expect to see some function declaration.
822 if (AS == AS_none) {
823 assert(TagType == DeclSpec::TST_unspecified);
824 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000825 ParsingDeclSpec PDS(*this);
826 Ptr = ParseExternalDeclaration(Attrs, &PDS);
827 } else {
828 Ptr =
829 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
830 }
831 }
832 if (!Ptr) {
833 Diag(Loc, diag::err_omp_decl_in_declare_simd);
834 return DeclGroupPtrTy();
835 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000836 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000837 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000838 case OMPD_declare_target: {
839 SourceLocation DTLoc = ConsumeAnyToken();
840 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +0000841 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000842 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000843
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000844 // Skip the last annot_pragma_openmp_end.
845 ConsumeAnyToken();
846
847 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
848 return DeclGroupPtrTy();
849
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000850 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000851 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +0000852 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
853 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000854 DeclGroupPtrTy Ptr;
855 // Here we expect to see some function declaration.
856 if (AS == AS_none) {
857 assert(TagType == DeclSpec::TST_unspecified);
858 MaybeParseCXX11Attributes(Attrs);
859 ParsingDeclSpec PDS(*this);
860 Ptr = ParseExternalDeclaration(Attrs, &PDS);
861 } else {
862 Ptr =
863 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
864 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000865 if (Ptr) {
866 DeclGroupRef Ref = Ptr.get();
867 Decls.append(Ref.begin(), Ref.end());
868 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000869 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
870 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000871 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000872 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000873 if (DKind != OMPD_end_declare_target)
874 TPA.Revert();
875 else
876 TPA.Commit();
877 }
878 }
879
Kelvin Lie0502752018-11-21 20:15:57 +0000880 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000881 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000882 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000883 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000884 case OMPD_unknown:
885 Diag(Tok, diag::err_omp_unknown_directive);
886 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000887 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000888 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000889 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000890 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000891 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000892 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000893 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000894 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000895 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000896 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000897 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000898 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000899 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000900 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000901 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000902 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000903 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000904 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000905 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000906 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000907 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000908 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000909 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000910 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000911 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000912 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000913 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000914 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000915 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000916 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000917 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000918 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000919 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000920 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000921 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000922 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000923 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000924 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000925 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000926 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000927 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000928 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000929 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000930 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000931 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000932 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000933 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000934 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000935 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000936 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000937 break;
938 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000939 while (Tok.isNot(tok::annot_pragma_openmp_end))
940 ConsumeAnyToken();
941 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000942 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000943}
944
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000945/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000946///
947/// threadprivate-directive:
948/// annot_pragma_openmp 'threadprivate' simple-variable-list
949/// annot_pragma_openmp_end
950///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000951/// declare-reduction-directive:
952/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
953/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
954/// ('omp_priv' '=' <expression>|<function_call>) ')']
955/// annot_pragma_openmp_end
956///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000957/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000958/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000959/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
960/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000961/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000962/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000963/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000964/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000965/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000966/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000967/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000968/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000969/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000970/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000971/// 'teams distribute parallel for' | 'target teams' |
972/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000973/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000974/// 'target teams distribute parallel for simd' |
975/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000976/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000978StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000979 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000980 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000981 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000983 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000984 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000985 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
986 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000987 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000988 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000989 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000990 // Name of critical directive.
991 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000993 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000994 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000995
996 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000997 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000998 if (Allowed != ACK_Any) {
999 Diag(Tok, diag::err_omp_immediate_directive)
1000 << getOpenMPDirectiveName(DKind) << 0;
1001 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001002 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001003 ThreadprivateListParserHelper Helper(this);
1004 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001005 // The last seen token is annot_pragma_openmp_end - need to check for
1006 // extra tokens.
1007 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1008 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001009 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +00001010 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001011 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001012 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1013 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001014 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1015 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001016 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001018 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001019 case OMPD_declare_reduction:
1020 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001021 if (DeclGroupPtrTy Res =
1022 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001023 // The last seen token is annot_pragma_openmp_end - need to check for
1024 // extra tokens.
1025 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1026 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1027 << getOpenMPDirectiveName(OMPD_declare_reduction);
1028 while (Tok.isNot(tok::annot_pragma_openmp_end))
1029 ConsumeAnyToken();
1030 }
1031 ConsumeAnyToken();
1032 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001033 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001034 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001035 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001036 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001037 case OMPD_flush:
1038 if (PP.LookAhead(0).is(tok::l_paren)) {
1039 FlushHasClause = true;
1040 // Push copy of the current token back to stream to properly parse
1041 // pseudo-clause OMPFlushClause.
1042 PP.EnterToken(Tok);
1043 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001044 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001045 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001046 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001047 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001048 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001049 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001050 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001051 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001052 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +00001053 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001054 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001055 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001056 }
1057 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001058 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001059 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001060 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001061 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001062 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001063 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001064 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001065 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001066 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001067 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001068 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001069 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001070 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001071 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001072 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001073 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001074 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001075 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001076 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001077 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001078 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001079 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001080 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001081 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001082 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001083 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001084 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001085 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001086 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001087 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001088 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001089 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001090 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001091 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001092 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001093 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001094 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001095 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001096 case OMPD_target_teams_distribute_parallel_for_simd:
1097 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001099 // Parse directive name of the 'critical' directive if any.
1100 if (DKind == OMPD_critical) {
1101 BalancedDelimiterTracker T(*this, tok::l_paren,
1102 tok::annot_pragma_openmp_end);
1103 if (!T.consumeOpen()) {
1104 if (Tok.isAnyIdentifier()) {
1105 DirName =
1106 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1107 ConsumeAnyToken();
1108 } else {
1109 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1110 }
1111 T.consumeClose();
1112 }
Alexey Bataev80909872015-07-02 11:25:17 +00001113 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001114 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001115 if (Tok.isNot(tok::annot_pragma_openmp_end))
1116 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001117 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001118
Alexey Bataevf29276e2014-06-18 04:14:57 +00001119 if (isOpenMPLoopDirective(DKind))
1120 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1121 if (isOpenMPSimdDirective(DKind))
1122 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1123 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001124 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001125
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001126 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001127 OpenMPClauseKind CKind =
1128 Tok.isAnnotation()
1129 ? OMPC_unknown
1130 : FlushHasClause ? OMPC_flush
1131 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001132 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001133 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001134 OMPClause *Clause =
1135 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001136 FirstClauses[CKind].setInt(true);
1137 if (Clause) {
1138 FirstClauses[CKind].setPointer(Clause);
1139 Clauses.push_back(Clause);
1140 }
1141
1142 // Skip ',' if any.
1143 if (Tok.is(tok::comma))
1144 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001145 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001146 }
1147 // End location of the directive.
1148 EndLoc = Tok.getLocation();
1149 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001150 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001151
Alexey Bataeveb482352015-12-18 05:05:56 +00001152 // OpenMP [2.13.8, ordered Construct, Syntax]
1153 // If the depend clause is specified, the ordered construct is a stand-alone
1154 // directive.
1155 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001156 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001157 Diag(Loc, diag::err_omp_immediate_directive)
1158 << getOpenMPDirectiveName(DKind) << 1
1159 << getOpenMPClauseName(OMPC_depend);
1160 }
1161 HasAssociatedStatement = false;
1162 }
1163
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001164 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001165 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001166 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001167 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001168 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1169 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1170 // should have at least one compound statement scope within it.
1171 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001172 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001173 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1174 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001175 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001176 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1177 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1178 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001179 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001180 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001181 Directive = Actions.ActOnOpenMPExecutableDirective(
1182 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1183 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001184
1185 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001186 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001187 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001188 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001189 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001190 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001191 case OMPD_declare_target:
1192 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001193 case OMPD_requires:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001194 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001195 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001196 SkipUntil(tok::annot_pragma_openmp_end);
1197 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001198 case OMPD_unknown:
1199 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001200 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001201 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001202 }
1203 return Directive;
1204}
1205
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001206// Parses simple list:
1207// simple-variable-list:
1208// '(' id-expression {, id-expression} ')'
1209//
1210bool Parser::ParseOpenMPSimpleVarList(
1211 OpenMPDirectiveKind Kind,
1212 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1213 Callback,
1214 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001215 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001216 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217 if (T.expectAndConsume(diag::err_expected_lparen_after,
1218 getOpenMPDirectiveName(Kind)))
1219 return true;
1220 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001221 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001222
1223 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001224 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001225 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001226 UnqualifiedId Name;
1227 // Read var name.
1228 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001229 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001230
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001231 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001232 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001233 IsCorrect = false;
1234 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001235 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001236 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001237 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001238 IsCorrect = false;
1239 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001240 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001241 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1242 Tok.isNot(tok::annot_pragma_openmp_end)) {
1243 IsCorrect = false;
1244 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001245 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001246 Diag(PrevTok.getLocation(), diag::err_expected)
1247 << tok::identifier
1248 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001249 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001250 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001251 }
1252 // Consume ','.
1253 if (Tok.is(tok::comma)) {
1254 ConsumeToken();
1255 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001256 }
1257
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001258 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001259 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001260 IsCorrect = false;
1261 }
1262
1263 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001264 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001265
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001266 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001267}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001268
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001269/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001270///
1271/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001272/// if-clause | final-clause | num_threads-clause | safelen-clause |
1273/// default-clause | private-clause | firstprivate-clause | shared-clause
1274/// | linear-clause | aligned-clause | collapse-clause |
1275/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001276/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001277/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001278/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001279/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001280/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001281/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001282/// from-clause | is_device_ptr-clause | task_reduction-clause |
1283/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001284///
1285OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1286 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001287 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001288 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001289 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001290 // Check if clause is allowed for the given directive.
1291 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001292 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1293 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001294 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001295 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001296 }
1297
1298 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001299 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001300 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001301 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001302 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001303 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001304 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001305 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001306 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001307 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001308 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001309 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001310 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001311 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001312 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001313 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001314 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001315 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001316 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001317 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001318 // OpenMP [2.9.1, target data construct, Restrictions]
1319 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001320 // OpenMP [2.11.1, task Construct, Restrictions]
1321 // At most one if clause can appear on the directive.
1322 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001323 // OpenMP [teams Construct, Restrictions]
1324 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001325 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001326 // OpenMP [2.9.1, task Construct, Restrictions]
1327 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001328 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1329 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001330 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1331 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001332 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001333 Diag(Tok, diag::err_omp_more_one_clause)
1334 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001335 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001336 }
1337
Alexey Bataev10e775f2015-07-30 11:36:16 +00001338 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001339 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001340 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001341 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001342 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001343 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001344 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001345 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001346 // OpenMP [2.14.3.1, Restrictions]
1347 // Only a single default clause may be specified on a parallel, task or
1348 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001349 // OpenMP [2.5, parallel Construct, Restrictions]
1350 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001351 // OpenMP [5.0, Requires directive, Restrictions]
1352 // At most one atomic_default_mem_order clause can appear
1353 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001354 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001355 Diag(Tok, diag::err_omp_more_one_clause)
1356 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001357 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001358 }
1359
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001360 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001361 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001362 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001363 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001364 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001365 // OpenMP [2.7.1, Restrictions, p. 3]
1366 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001367 // OpenMP [2.10.4, Restrictions, p. 106]
1368 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001369 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001370 Diag(Tok, diag::err_omp_more_one_clause)
1371 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001372 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001373 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001374 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001375
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001376 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001377 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001378 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001379 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001380 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001381 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001382 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001383 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001384 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001385 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001386 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001387 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001388 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001389 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001390 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001391 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001392 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001393 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001394 // OpenMP [2.7.1, Restrictions, p. 9]
1395 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001396 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1397 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001398 // OpenMP [5.0, Requires directive, Restrictions]
1399 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001400 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001401 Diag(Tok, diag::err_omp_more_one_clause)
1402 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001403 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001404 }
1405
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001406 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001407 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001408 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001409 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001410 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001411 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001412 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001413 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001414 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001415 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001416 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001417 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001418 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001419 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001420 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001421 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001422 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001423 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001424 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001425 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001426 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001427 break;
1428 case OMPC_unknown:
1429 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001430 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001431 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001432 break;
1433 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001434 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001435 if (!WrongDirective)
1436 Diag(Tok, diag::err_omp_unexpected_clause)
1437 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001438 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001439 break;
1440 }
Craig Topper161e4db2014-05-21 06:02:52 +00001441 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001442}
1443
Alexey Bataev2af33e32016-04-07 12:45:37 +00001444/// Parses simple expression in parens for single-expression clauses of OpenMP
1445/// constructs.
1446/// \param RLoc Returned location of right paren.
1447ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1448 SourceLocation &RLoc) {
1449 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1450 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1451 return ExprError();
1452
1453 SourceLocation ELoc = Tok.getLocation();
1454 ExprResult LHS(ParseCastExpression(
1455 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1456 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001457 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001458
1459 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001460 RLoc = Tok.getLocation();
1461 if (!T.consumeClose())
1462 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001463
Alexey Bataev2af33e32016-04-07 12:45:37 +00001464 return Val;
1465}
1466
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001467/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001468/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001469/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001470///
Alexey Bataev3778b602014-07-17 07:32:53 +00001471/// final-clause:
1472/// 'final' '(' expression ')'
1473///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001474/// num_threads-clause:
1475/// 'num_threads' '(' expression ')'
1476///
1477/// safelen-clause:
1478/// 'safelen' '(' expression ')'
1479///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001480/// simdlen-clause:
1481/// 'simdlen' '(' expression ')'
1482///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001483/// collapse-clause:
1484/// 'collapse' '(' expression ')'
1485///
Alexey Bataeva0569352015-12-01 10:17:31 +00001486/// priority-clause:
1487/// 'priority' '(' expression ')'
1488///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001489/// grainsize-clause:
1490/// 'grainsize' '(' expression ')'
1491///
Alexey Bataev382967a2015-12-08 12:06:20 +00001492/// num_tasks-clause:
1493/// 'num_tasks' '(' expression ')'
1494///
Alexey Bataev28c75412015-12-15 08:19:24 +00001495/// hint-clause:
1496/// 'hint' '(' expression ')'
1497///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001498OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1499 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001500 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001501 SourceLocation LLoc = Tok.getLocation();
1502 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001503
Alexey Bataev2af33e32016-04-07 12:45:37 +00001504 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001505
1506 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001507 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001508
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001509 if (ParseOnly)
1510 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001511 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001512}
1513
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001514/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001515///
1516/// default-clause:
1517/// 'default' '(' 'none' | 'shared' ')
1518///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001519/// proc_bind-clause:
1520/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1521///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001522OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1523 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001524 SourceLocation Loc = Tok.getLocation();
1525 SourceLocation LOpen = ConsumeToken();
1526 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001527 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001528 if (T.expectAndConsume(diag::err_expected_lparen_after,
1529 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001530 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001531
Alexey Bataeva55ed262014-05-28 06:15:33 +00001532 unsigned Type = getOpenMPSimpleClauseType(
1533 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001534 SourceLocation TypeLoc = Tok.getLocation();
1535 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1536 Tok.isNot(tok::annot_pragma_openmp_end))
1537 ConsumeAnyToken();
1538
1539 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001540 SourceLocation RLoc = Tok.getLocation();
1541 if (!T.consumeClose())
1542 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001543
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001544 if (ParseOnly)
1545 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001546 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001547}
1548
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001549/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001550///
1551/// ordered-clause:
1552/// 'ordered'
1553///
Alexey Bataev236070f2014-06-20 11:19:47 +00001554/// nowait-clause:
1555/// 'nowait'
1556///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001557/// untied-clause:
1558/// 'untied'
1559///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001560/// mergeable-clause:
1561/// 'mergeable'
1562///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001563/// read-clause:
1564/// 'read'
1565///
Alexey Bataev346265e2015-09-25 10:37:12 +00001566/// threads-clause:
1567/// 'threads'
1568///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001569/// simd-clause:
1570/// 'simd'
1571///
Alexey Bataevb825de12015-12-07 10:51:44 +00001572/// nogroup-clause:
1573/// 'nogroup'
1574///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001575OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001576 SourceLocation Loc = Tok.getLocation();
1577 ConsumeAnyToken();
1578
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001579 if (ParseOnly)
1580 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001581 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1582}
1583
1584
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001585/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001586/// argument like 'schedule' or 'dist_schedule'.
1587///
1588/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001589/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1590/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001591///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001592/// if-clause:
1593/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1594///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001595/// defaultmap:
1596/// 'defaultmap' '(' modifier ':' kind ')'
1597///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001598OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1599 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001600 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001601 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001602 // Parse '('.
1603 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1604 if (T.expectAndConsume(diag::err_expected_lparen_after,
1605 getOpenMPClauseName(Kind)))
1606 return nullptr;
1607
1608 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001609 SmallVector<unsigned, 4> Arg;
1610 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001611 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001612 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1613 Arg.resize(NumberOfElements);
1614 KLoc.resize(NumberOfElements);
1615 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1616 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1617 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001618 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001619 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001620 if (KindModifier > OMPC_SCHEDULE_unknown) {
1621 // Parse 'modifier'
1622 Arg[Modifier1] = KindModifier;
1623 KLoc[Modifier1] = Tok.getLocation();
1624 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1625 Tok.isNot(tok::annot_pragma_openmp_end))
1626 ConsumeAnyToken();
1627 if (Tok.is(tok::comma)) {
1628 // Parse ',' 'modifier'
1629 ConsumeAnyToken();
1630 KindModifier = getOpenMPSimpleClauseType(
1631 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1632 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1633 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001634 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001635 KLoc[Modifier2] = Tok.getLocation();
1636 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1637 Tok.isNot(tok::annot_pragma_openmp_end))
1638 ConsumeAnyToken();
1639 }
1640 // Parse ':'
1641 if (Tok.is(tok::colon))
1642 ConsumeAnyToken();
1643 else
1644 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1645 KindModifier = getOpenMPSimpleClauseType(
1646 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1647 }
1648 Arg[ScheduleKind] = KindModifier;
1649 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001650 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1651 Tok.isNot(tok::annot_pragma_openmp_end))
1652 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001653 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1654 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1655 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001656 Tok.is(tok::comma))
1657 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001658 } else if (Kind == OMPC_dist_schedule) {
1659 Arg.push_back(getOpenMPSimpleClauseType(
1660 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1661 KLoc.push_back(Tok.getLocation());
1662 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1663 Tok.isNot(tok::annot_pragma_openmp_end))
1664 ConsumeAnyToken();
1665 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1666 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001667 } else if (Kind == OMPC_defaultmap) {
1668 // Get a defaultmap modifier
1669 Arg.push_back(getOpenMPSimpleClauseType(
1670 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1671 KLoc.push_back(Tok.getLocation());
1672 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1673 Tok.isNot(tok::annot_pragma_openmp_end))
1674 ConsumeAnyToken();
1675 // Parse ':'
1676 if (Tok.is(tok::colon))
1677 ConsumeAnyToken();
1678 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1679 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1680 // Get a defaultmap kind
1681 Arg.push_back(getOpenMPSimpleClauseType(
1682 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1683 KLoc.push_back(Tok.getLocation());
1684 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1685 Tok.isNot(tok::annot_pragma_openmp_end))
1686 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001687 } else {
1688 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001689 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001690 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001691 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001692 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001693 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001694 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1695 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001696 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001697 } else {
1698 TPA.Revert();
1699 Arg.back() = OMPD_unknown;
1700 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001701 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001702 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001703 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001704 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001705
Carlo Bertollib4adf552016-01-15 18:50:31 +00001706 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1707 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1708 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001709 if (NeedAnExpression) {
1710 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001711 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1712 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001713 Val =
1714 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001715 }
1716
1717 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001718 SourceLocation RLoc = Tok.getLocation();
1719 if (!T.consumeClose())
1720 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001721
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001722 if (NeedAnExpression && Val.isInvalid())
1723 return nullptr;
1724
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001725 if (ParseOnly)
1726 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001727 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001728 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001729}
1730
Alexey Bataevc5e02582014-06-16 07:08:35 +00001731static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1732 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001733 if (ReductionIdScopeSpec.isEmpty()) {
1734 auto OOK = OO_None;
1735 switch (P.getCurToken().getKind()) {
1736 case tok::plus:
1737 OOK = OO_Plus;
1738 break;
1739 case tok::minus:
1740 OOK = OO_Minus;
1741 break;
1742 case tok::star:
1743 OOK = OO_Star;
1744 break;
1745 case tok::amp:
1746 OOK = OO_Amp;
1747 break;
1748 case tok::pipe:
1749 OOK = OO_Pipe;
1750 break;
1751 case tok::caret:
1752 OOK = OO_Caret;
1753 break;
1754 case tok::ampamp:
1755 OOK = OO_AmpAmp;
1756 break;
1757 case tok::pipepipe:
1758 OOK = OO_PipePipe;
1759 break;
1760 default:
1761 break;
1762 }
1763 if (OOK != OO_None) {
1764 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001765 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001766 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1767 return false;
1768 }
1769 }
1770 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1771 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001772 /*AllowConstructorName*/ false,
1773 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001774 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001775}
1776
Kelvin Lief579432018-12-18 22:18:41 +00001777/// Checks if the token is a valid map-type-modifier.
1778static OpenMPMapModifierKind isMapModifier(Parser &P) {
1779 Token Tok = P.getCurToken();
1780 if (!Tok.is(tok::identifier))
1781 return OMPC_MAP_MODIFIER_unknown;
1782
1783 Preprocessor &PP = P.getPreprocessor();
1784 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
1785 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1786 return TypeModifier;
1787}
1788
1789/// Parse map-type-modifiers in map clause.
1790/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1791/// where, map-type-modifier ::= always | close
1792static void parseMapTypeModifiers(Parser &P,
1793 Parser::OpenMPVarListDataTy &Data) {
1794 Preprocessor &PP = P.getPreprocessor();
1795 while (P.getCurToken().isNot(tok::colon)) {
1796 Token Tok = P.getCurToken();
1797 OpenMPMapModifierKind TypeModifier = isMapModifier(P);
1798 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
1799 TypeModifier == OMPC_MAP_MODIFIER_close) {
1800 Data.MapTypeModifiers.push_back(TypeModifier);
1801 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
1802 P.ConsumeToken();
1803 } else {
1804 // For the case of unknown map-type-modifier or a map-type.
1805 // Map-type is followed by a colon; the function returns when it
1806 // encounters a token followed by a colon.
1807 if (Tok.is(tok::comma)) {
1808 P.Diag(Tok, diag::err_omp_map_type_modifier_missing);
1809 P.ConsumeToken();
1810 continue;
1811 }
1812 // Potential map-type token as it is followed by a colon.
1813 if (PP.LookAhead(0).is(tok::colon))
1814 return;
1815 P.Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1816 P.ConsumeToken();
1817 }
1818 if (P.getCurToken().is(tok::comma))
1819 P.ConsumeToken();
1820 }
1821}
1822
1823/// Checks if the token is a valid map-type.
1824static OpenMPMapClauseKind isMapType(Parser &P) {
1825 Token Tok = P.getCurToken();
1826 // The map-type token can be either an identifier or the C++ delete keyword.
1827 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
1828 return OMPC_MAP_unknown;
1829 Preprocessor &PP = P.getPreprocessor();
1830 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
1831 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1832 return MapType;
1833}
1834
1835/// Parse map-type in map clause.
1836/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1837/// where, map-type ::= to | from | tofrom | alloc | release | delete
1838static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
1839 Token Tok = P.getCurToken();
1840 if (Tok.is(tok::colon)) {
1841 P.Diag(Tok, diag::err_omp_map_type_missing);
1842 return;
1843 }
1844 Data.MapType = isMapType(P);
1845 if (Data.MapType == OMPC_MAP_unknown)
1846 P.Diag(Tok, diag::err_omp_unknown_map_type);
1847 P.ConsumeToken();
1848}
1849
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001850/// Parses clauses with list.
1851bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1852 OpenMPClauseKind Kind,
1853 SmallVectorImpl<Expr *> &Vars,
1854 OpenMPVarListDataTy &Data) {
1855 UnqualifiedId UnqualifiedReductionId;
1856 bool InvalidReductionId = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001857
1858 // Parse '('.
1859 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1860 if (T.expectAndConsume(diag::err_expected_lparen_after,
1861 getOpenMPClauseName(Kind)))
1862 return true;
1863
1864 bool NeedRParenForLinear = false;
1865 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1866 tok::annot_pragma_openmp_end);
1867 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001868 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1869 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001870 ColonProtectionRAIIObject ColonRAII(*this);
1871 if (getLangOpts().CPlusPlus)
1872 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1873 /*ObjectType=*/nullptr,
1874 /*EnteringContext=*/false);
1875 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1876 UnqualifiedReductionId);
1877 if (InvalidReductionId) {
1878 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1879 StopBeforeMatch);
1880 }
1881 if (Tok.is(tok::colon))
1882 Data.ColonLoc = ConsumeToken();
1883 else
1884 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1885 if (!InvalidReductionId)
1886 Data.ReductionId =
1887 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1888 } else if (Kind == OMPC_depend) {
1889 // Handle dependency type for depend clause.
1890 ColonProtectionRAIIObject ColonRAII(*this);
1891 Data.DepKind =
1892 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1893 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1894 Data.DepLinMapLoc = Tok.getLocation();
1895
1896 if (Data.DepKind == OMPC_DEPEND_unknown) {
1897 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1898 StopBeforeMatch);
1899 } else {
1900 ConsumeToken();
1901 // Special processing for depend(source) clause.
1902 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1903 // Parse ')'.
1904 T.consumeClose();
1905 return false;
1906 }
1907 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001908 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001909 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001910 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001911 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1912 : diag::warn_pragma_expected_colon)
1913 << "dependency type";
1914 }
1915 } else if (Kind == OMPC_linear) {
1916 // Try to parse modifier if any.
1917 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1918 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1919 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1920 Data.DepLinMapLoc = ConsumeToken();
1921 LinearT.consumeOpen();
1922 NeedRParenForLinear = true;
1923 }
1924 } else if (Kind == OMPC_map) {
1925 // Handle map type for map clause.
1926 ColonProtectionRAIIObject ColonRAII(*this);
1927
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001928 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00001929 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001930 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001931 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001932
Kelvin Lief579432018-12-18 22:18:41 +00001933 // Check for presence of a colon in the map clause.
1934 TentativeParsingAction TPA(*this);
1935 bool ColonPresent = false;
1936 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1937 StopBeforeMatch)) {
1938 if (Tok.is(tok::colon))
1939 ColonPresent = true;
1940 }
1941 TPA.Revert();
1942 // Only parse map-type-modifier[s] and map-type if a colon is present in
1943 // the map clause.
1944 if (ColonPresent) {
1945 parseMapTypeModifiers(*this, Data);
1946 parseMapType(*this, Data);
1947 }
1948 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001949 Data.MapType = OMPC_MAP_tofrom;
1950 Data.IsMapTypeImplicit = true;
1951 }
1952
1953 if (Tok.is(tok::colon))
1954 Data.ColonLoc = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001955 }
1956
Alexey Bataevfa312f32017-07-21 18:48:21 +00001957 bool IsComma =
1958 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1959 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1960 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00001961 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00001962 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001963 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1964 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1965 Tok.isNot(tok::annot_pragma_openmp_end))) {
1966 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1967 // Parse variable
1968 ExprResult VarExpr =
1969 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001970 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001971 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001972 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001973 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1974 StopBeforeMatch);
1975 }
1976 // Skip ',' if any
1977 IsComma = Tok.is(tok::comma);
1978 if (IsComma)
1979 ConsumeToken();
1980 else if (Tok.isNot(tok::r_paren) &&
1981 Tok.isNot(tok::annot_pragma_openmp_end) &&
1982 (!MayHaveTail || Tok.isNot(tok::colon)))
1983 Diag(Tok, diag::err_omp_expected_punc)
1984 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1985 : getOpenMPClauseName(Kind))
1986 << (Kind == OMPC_flush);
1987 }
1988
1989 // Parse ')' for linear clause with modifier.
1990 if (NeedRParenForLinear)
1991 LinearT.consumeClose();
1992
1993 // Parse ':' linear-step (or ':' alignment).
1994 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1995 if (MustHaveTail) {
1996 Data.ColonLoc = Tok.getLocation();
1997 SourceLocation ELoc = ConsumeToken();
1998 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001999 Tail =
2000 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002001 if (Tail.isUsable())
2002 Data.TailExpr = Tail.get();
2003 else
2004 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2005 StopBeforeMatch);
2006 }
2007
2008 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002009 Data.RLoc = Tok.getLocation();
2010 if (!T.consumeClose())
2011 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002012 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2013 Vars.empty()) ||
2014 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
2015 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002016}
2017
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002018/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002019/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2020/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002021///
2022/// private-clause:
2023/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002024/// firstprivate-clause:
2025/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002026/// lastprivate-clause:
2027/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002028/// shared-clause:
2029/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002030/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002031/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002032/// aligned-clause:
2033/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002034/// reduction-clause:
2035/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002036/// task_reduction-clause:
2037/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002038/// in_reduction-clause:
2039/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002040/// copyprivate-clause:
2041/// 'copyprivate' '(' list ')'
2042/// flush-clause:
2043/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002044/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002045/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002046/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002047/// 'map' '(' [ [ always [,] ] [ close [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002048/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002049/// to-clause:
2050/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002051/// from-clause:
2052/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002053/// use_device_ptr-clause:
2054/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002055/// is_device_ptr-clause:
2056/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002057///
Alexey Bataev182227b2015-08-20 10:54:39 +00002058/// For 'linear' clause linear-list may have the following forms:
2059/// list
2060/// modifier(list)
2061/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002062OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002063 OpenMPClauseKind Kind,
2064 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002065 SourceLocation Loc = Tok.getLocation();
2066 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002067 SmallVector<Expr *, 4> Vars;
2068 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002069
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002070 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002071 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002072
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002073 if (ParseOnly)
2074 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002075 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002076 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002077 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
Kelvin Lief579432018-12-18 22:18:41 +00002078 Data.MapTypeModifiers, Data.MapTypeModifiersLoc, Data.MapType,
2079 Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002080}
2081