blob: dd2a8aae9f2fb6ac0b14e1a17b5c81718ecadfce [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000011///
12//===----------------------------------------------------------------------===//
13
Alexey Bataev9959db52014-05-06 10:08:46 +000014#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000015#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000043 OMPD_teams_distribute_parallel,
44 OMPD_target_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
47class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
50
51public:
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
54 ExprResult Res =
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
56 if (Res.isUsable())
57 Identifiers.push_back(Res.get());
58 }
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
60};
Dmitry Polukhin82478332016-02-13 06:53:38 +000061} // namespace
62
63// Map token string to extended OMP token kind that are
64// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
68 return DKind;
69
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000073 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000074 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000079 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Default(OMPD_unknown);
81}
82
Alexey Bataev61908f652018-04-23 19:53:05 +000083static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000084 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
85 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
86 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000088 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
89 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
90 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
91 {OMPD_declare, OMPD_target, OMPD_declare_target},
92 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
93 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
94 {OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd},
96 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
97 {OMPD_end, OMPD_declare, OMPD_end_declare},
98 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
99 {OMPD_target, OMPD_data, OMPD_target_data},
100 {OMPD_target, OMPD_enter, OMPD_target_enter},
101 {OMPD_target, OMPD_exit, OMPD_target_exit},
102 {OMPD_target, OMPD_update, OMPD_target_update},
103 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
104 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
105 {OMPD_for, OMPD_simd, OMPD_for_simd},
106 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
107 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
108 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
109 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
110 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
111 {OMPD_target, OMPD_simd, OMPD_target_simd},
112 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
113 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
114 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
115 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
116 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
117 {OMPD_teams_distribute_parallel, OMPD_for,
118 OMPD_teams_distribute_parallel_for},
119 {OMPD_teams_distribute_parallel_for, OMPD_simd,
120 OMPD_teams_distribute_parallel_for_simd},
121 {OMPD_target, OMPD_teams, OMPD_target_teams},
122 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
123 {OMPD_target_teams_distribute, OMPD_parallel,
124 OMPD_target_teams_distribute_parallel},
125 {OMPD_target_teams_distribute, OMPD_simd,
126 OMPD_target_teams_distribute_simd},
127 {OMPD_target_teams_distribute_parallel, OMPD_for,
128 OMPD_target_teams_distribute_parallel_for},
129 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
130 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000131 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000132 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000133 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000134 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000135 ? static_cast<unsigned>(OMPD_unknown)
136 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
137 if (DKind == OMPD_unknown)
138 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000139
Alexey Bataev61908f652018-04-23 19:53:05 +0000140 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
141 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000142 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000143
Dmitry Polukhin82478332016-02-13 06:53:38 +0000144 Tok = P.getPreprocessor().LookAhead(0);
145 unsigned SDKind =
146 Tok.isAnnotation()
147 ? static_cast<unsigned>(OMPD_unknown)
148 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
149 if (SDKind == OMPD_unknown)
150 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000151
Alexey Bataev61908f652018-04-23 19:53:05 +0000152 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000153 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000154 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000155 }
156 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000157 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
158 : OMPD_unknown;
159}
160
161static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000162 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000163 Sema &Actions = P.getActions();
164 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000165 // Allow to use 'operator' keyword for C++ operators
166 bool WithOperator = false;
167 if (Tok.is(tok::kw_operator)) {
168 P.ConsumeToken();
169 Tok = P.getCurToken();
170 WithOperator = true;
171 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000172 switch (Tok.getKind()) {
173 case tok::plus: // '+'
174 OOK = OO_Plus;
175 break;
176 case tok::minus: // '-'
177 OOK = OO_Minus;
178 break;
179 case tok::star: // '*'
180 OOK = OO_Star;
181 break;
182 case tok::amp: // '&'
183 OOK = OO_Amp;
184 break;
185 case tok::pipe: // '|'
186 OOK = OO_Pipe;
187 break;
188 case tok::caret: // '^'
189 OOK = OO_Caret;
190 break;
191 case tok::ampamp: // '&&'
192 OOK = OO_AmpAmp;
193 break;
194 case tok::pipepipe: // '||'
195 OOK = OO_PipePipe;
196 break;
197 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000198 if (!WithOperator)
199 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000200 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 default:
202 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
203 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
204 Parser::StopBeforeMatch);
205 return DeclarationName();
206 }
207 P.ConsumeToken();
208 auto &DeclNames = Actions.getASTContext().DeclarationNames;
209 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
210 : DeclNames.getCXXOperatorName(OOK);
211}
212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000213/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000214///
215/// declare-reduction-directive:
216/// annot_pragma_openmp 'declare' 'reduction'
217/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
218/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
219/// annot_pragma_openmp_end
220/// <reduction_id> is either a base language identifier or one of the following
221/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
222///
223Parser::DeclGroupPtrTy
224Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
225 // Parse '('.
226 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
227 if (T.expectAndConsume(diag::err_expected_lparen_after,
228 getOpenMPDirectiveName(OMPD_declare_reduction))) {
229 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
230 return DeclGroupPtrTy();
231 }
232
233 DeclarationName Name = parseOpenMPReductionId(*this);
234 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
235 return DeclGroupPtrTy();
236
237 // Consume ':'.
238 bool IsCorrect = !ExpectAndConsume(tok::colon);
239
240 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
241 return DeclGroupPtrTy();
242
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000243 IsCorrect = IsCorrect && !Name.isEmpty();
244
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000245 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
246 Diag(Tok.getLocation(), diag::err_expected_type);
247 IsCorrect = false;
248 }
249
250 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
251 return DeclGroupPtrTy();
252
253 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
254 // Parse list of types until ':' token.
255 do {
256 ColonProtectionRAIIObject ColonRAII(*this);
257 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000258 TypeResult TR =
259 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000260 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000261 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000262 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
263 if (!ReductionType.isNull()) {
264 ReductionTypes.push_back(
265 std::make_pair(ReductionType, Range.getBegin()));
266 }
267 } else {
268 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
269 StopBeforeMatch);
270 }
271
272 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
273 break;
274
275 // Consume ','.
276 if (ExpectAndConsume(tok::comma)) {
277 IsCorrect = false;
278 if (Tok.is(tok::annot_pragma_openmp_end)) {
279 Diag(Tok.getLocation(), diag::err_expected_type);
280 return DeclGroupPtrTy();
281 }
282 }
283 } while (Tok.isNot(tok::annot_pragma_openmp_end));
284
285 if (ReductionTypes.empty()) {
286 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
287 return DeclGroupPtrTy();
288 }
289
290 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
291 return DeclGroupPtrTy();
292
293 // Consume ':'.
294 if (ExpectAndConsume(tok::colon))
295 IsCorrect = false;
296
297 if (Tok.is(tok::annot_pragma_openmp_end)) {
298 Diag(Tok.getLocation(), diag::err_expected_expression);
299 return DeclGroupPtrTy();
300 }
301
302 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
303 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
304
305 // Parse <combiner> expression and then parse initializer if any for each
306 // correct type.
307 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000308 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000309 TentativeParsingAction TPA(*this);
310 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000311 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000312 Scope::OpenMPDirectiveScope);
313 // Parse <combiner> expression.
314 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
315 ExprResult CombinerResult =
316 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000317 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000318 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
319
320 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
321 Tok.isNot(tok::annot_pragma_openmp_end)) {
322 TPA.Commit();
323 IsCorrect = false;
324 break;
325 }
326 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
327 ExprResult InitializerResult;
328 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
329 // Parse <initializer> expression.
330 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000331 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000332 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000333 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000334 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
335 TPA.Commit();
336 IsCorrect = false;
337 break;
338 }
339 // Parse '('.
340 BalancedDelimiterTracker T(*this, tok::l_paren,
341 tok::annot_pragma_openmp_end);
342 IsCorrect =
343 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
344 IsCorrect;
345 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
346 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000347 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000348 Scope::OpenMPDirectiveScope);
349 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000350 VarDecl *OmpPrivParm =
351 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
352 D);
353 // Check if initializer is omp_priv <init_expr> or something else.
354 if (Tok.is(tok::identifier) &&
355 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000356 if (Actions.getLangOpts().CPlusPlus) {
357 InitializerResult = Actions.ActOnFinishFullExpr(
358 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000359 /*DiscardedValue*/ false);
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000360 } else {
361 ConsumeToken();
362 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
363 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000364 } else {
365 InitializerResult = Actions.ActOnFinishFullExpr(
366 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000367 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000368 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000369 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000370 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000371 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
372 Tok.isNot(tok::annot_pragma_openmp_end)) {
373 TPA.Commit();
374 IsCorrect = false;
375 break;
376 }
377 IsCorrect =
378 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
379 }
380 }
381
382 ++I;
383 // Revert parsing if not the last type, otherwise accept it, we're done with
384 // parsing.
385 if (I != E)
386 TPA.Revert();
387 else
388 TPA.Commit();
389 }
390 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
391 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000392}
393
Alexey Bataev070f43a2017-09-06 14:49:58 +0000394void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
395 // Parse declarator '=' initializer.
396 // If a '==' or '+=' is found, suggest a fixit to '='.
397 if (isTokenEqualOrEqualTypo()) {
398 ConsumeToken();
399
400 if (Tok.is(tok::code_completion)) {
401 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
402 Actions.FinalizeDeclaration(OmpPrivParm);
403 cutOffParsing();
404 return;
405 }
406
407 ExprResult Init(ParseInitializer());
408
409 if (Init.isInvalid()) {
410 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
411 Actions.ActOnInitializerError(OmpPrivParm);
412 } else {
413 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
414 /*DirectInit=*/false);
415 }
416 } else if (Tok.is(tok::l_paren)) {
417 // Parse C++ direct initializer: '(' expression-list ')'
418 BalancedDelimiterTracker T(*this, tok::l_paren);
419 T.consumeOpen();
420
421 ExprVector Exprs;
422 CommaLocsTy CommaLocs;
423
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000424 SourceLocation LParLoc = T.getOpenLocation();
425 if (ParseExpressionList(
426 Exprs, CommaLocs, [this, OmpPrivParm, LParLoc, &Exprs] {
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000427 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000428 getCurScope(),
429 OmpPrivParm->getType()->getCanonicalTypeInternal(),
430 OmpPrivParm->getLocation(), Exprs, LParLoc);
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000431 CalledSignatureHelp = true;
Ilya Biryukov832c4af2018-09-07 14:04:39 +0000432 Actions.CodeCompleteExpression(getCurScope(), PreferredType);
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000433 })) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +0000434 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) {
435 Actions.ProduceConstructorSignatureHelp(
436 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
437 OmpPrivParm->getLocation(), Exprs, LParLoc);
438 CalledSignatureHelp = true;
439 }
Alexey Bataev070f43a2017-09-06 14:49:58 +0000440 Actions.ActOnInitializerError(OmpPrivParm);
441 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
442 } else {
443 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000444 SourceLocation RLoc = Tok.getLocation();
445 if (!T.consumeClose())
446 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000447
448 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
449 "Unexpected number of commas!");
450
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000451 ExprResult Initializer =
452 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000453 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
454 /*DirectInit=*/true);
455 }
456 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
457 // Parse C++0x braced-init-list.
458 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
459
460 ExprResult Init(ParseBraceInitializer());
461
462 if (Init.isInvalid()) {
463 Actions.ActOnInitializerError(OmpPrivParm);
464 } else {
465 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
466 /*DirectInit=*/true);
467 }
468 } else {
469 Actions.ActOnUninitializedDecl(OmpPrivParm);
470 }
471}
472
Alexey Bataev2af33e32016-04-07 12:45:37 +0000473namespace {
474/// RAII that recreates function context for correct parsing of clauses of
475/// 'declare simd' construct.
476/// OpenMP, 2.8.2 declare simd Construct
477/// The expressions appearing in the clauses of this directive are evaluated in
478/// the scope of the arguments of the function declaration or definition.
479class FNContextRAII final {
480 Parser &P;
481 Sema::CXXThisScopeRAII *ThisScope;
482 Parser::ParseScope *TempScope;
483 Parser::ParseScope *FnScope;
484 bool HasTemplateScope = false;
485 bool HasFunScope = false;
486 FNContextRAII() = delete;
487 FNContextRAII(const FNContextRAII &) = delete;
488 FNContextRAII &operator=(const FNContextRAII &) = delete;
489
490public:
491 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
492 Decl *D = *Ptr.get().begin();
493 NamedDecl *ND = dyn_cast<NamedDecl>(D);
494 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
495 Sema &Actions = P.getActions();
496
497 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000498 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000499 ND && ND->isCXXInstanceMember());
500
501 // If the Decl is templatized, add template parameters to scope.
502 HasTemplateScope = D->isTemplateDecl();
503 TempScope =
504 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
505 if (HasTemplateScope)
506 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
507
508 // If the Decl is on a function, add function parameters to the scope.
509 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000510 FnScope = new Parser::ParseScope(
511 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
512 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000513 if (HasFunScope)
514 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
515 }
516 ~FNContextRAII() {
517 if (HasFunScope) {
518 P.getActions().ActOnExitFunctionContext();
519 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
520 }
521 if (HasTemplateScope)
522 TempScope->Exit();
523 delete FnScope;
524 delete TempScope;
525 delete ThisScope;
526 }
527};
528} // namespace
529
Alexey Bataevd93d3762016-04-12 09:35:56 +0000530/// Parses clauses for 'declare simd' directive.
531/// clause:
532/// 'inbranch' | 'notinbranch'
533/// 'simdlen' '(' <expr> ')'
534/// { 'uniform' '(' <argument_list> ')' }
535/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000536/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
537static bool parseDeclareSimdClauses(
538 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
539 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
540 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
541 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000542 SourceRange BSRange;
543 const Token &Tok = P.getCurToken();
544 bool IsError = false;
545 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
546 if (Tok.isNot(tok::identifier))
547 break;
548 OMPDeclareSimdDeclAttr::BranchStateTy Out;
549 IdentifierInfo *II = Tok.getIdentifierInfo();
550 StringRef ClauseName = II->getName();
551 // Parse 'inranch|notinbranch' clauses.
552 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
553 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
554 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
555 << ClauseName
556 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
557 IsError = true;
558 }
559 BS = Out;
560 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
561 P.ConsumeToken();
562 } else if (ClauseName.equals("simdlen")) {
563 if (SimdLen.isUsable()) {
564 P.Diag(Tok, diag::err_omp_more_one_clause)
565 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
566 IsError = true;
567 }
568 P.ConsumeToken();
569 SourceLocation RLoc;
570 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
571 if (SimdLen.isInvalid())
572 IsError = true;
573 } else {
574 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000575 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
576 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000577 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000578 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000579 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000580 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000581 else if (CKind == OMPC_linear)
582 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000583
584 P.ConsumeToken();
585 if (P.ParseOpenMPVarList(OMPD_declare_simd,
586 getOpenMPClauseKind(ClauseName), *Vars, Data))
587 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000588 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000589 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000590 } else if (CKind == OMPC_linear) {
Alexey Bataevecba70f2016-04-12 11:02:11 +0000591 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
592 Data.DepLinMapLoc))
593 Data.LinKind = OMPC_LINEAR_val;
594 LinModifiers.append(Linears.size() - LinModifiers.size(),
595 Data.LinKind);
596 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
597 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000598 } else
599 // TODO: add parsing of other clauses.
600 break;
601 }
602 // Skip ',' if any.
603 if (Tok.is(tok::comma))
604 P.ConsumeToken();
605 }
606 return IsError;
607}
608
Alexey Bataev2af33e32016-04-07 12:45:37 +0000609/// Parse clauses for '#pragma omp declare simd'.
610Parser::DeclGroupPtrTy
611Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
612 CachedTokens &Toks, SourceLocation Loc) {
613 PP.EnterToken(Tok);
614 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
615 // Consume the previously pushed token.
616 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
617
618 FNContextRAII FnContext(*this, Ptr);
619 OMPDeclareSimdDeclAttr::BranchStateTy BS =
620 OMPDeclareSimdDeclAttr::BS_Undefined;
621 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000622 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000623 SmallVector<Expr *, 4> Aligneds;
624 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000625 SmallVector<Expr *, 4> Linears;
626 SmallVector<unsigned, 4> LinModifiers;
627 SmallVector<Expr *, 4> Steps;
628 bool IsError =
629 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
630 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000631 // Need to check for extra tokens.
632 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
633 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
634 << getOpenMPDirectiveName(OMPD_declare_simd);
635 while (Tok.isNot(tok::annot_pragma_openmp_end))
636 ConsumeAnyToken();
637 }
638 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000639 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000640 if (IsError)
641 return Ptr;
642 return Actions.ActOnOpenMPDeclareSimdDirective(
643 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
644 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000645}
646
Kelvin Lie0502752018-11-21 20:15:57 +0000647Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
648 // OpenMP 4.5 syntax with list of entities.
649 Sema::NamedDeclSetType SameDirectiveDecls;
650 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
651 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
652 if (Tok.is(tok::identifier)) {
653 IdentifierInfo *II = Tok.getIdentifierInfo();
654 StringRef ClauseName = II->getName();
655 // Parse 'to|link' clauses.
656 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT)) {
657 Diag(Tok, diag::err_omp_declare_target_unexpected_clause) << ClauseName;
658 break;
659 }
660 ConsumeToken();
661 }
662 auto &&Callback = [this, MT, &SameDirectiveDecls](
663 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
664 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
665 SameDirectiveDecls);
666 };
667 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
668 /*AllowScopeSpecifier=*/true))
669 break;
670
671 // Consume optional ','.
672 if (Tok.is(tok::comma))
673 ConsumeToken();
674 }
675 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
676 ConsumeAnyToken();
677 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
678 SameDirectiveDecls.end());
679 if (Decls.empty())
680 return DeclGroupPtrTy();
681 return Actions.BuildDeclaratorGroup(Decls);
682}
683
684void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
685 SourceLocation DTLoc) {
686 if (DKind != OMPD_end_declare_target) {
687 Diag(Tok, diag::err_expected_end_declare_target);
688 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
689 return;
690 }
691 ConsumeAnyToken();
692 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
693 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
694 << getOpenMPDirectiveName(OMPD_end_declare_target);
695 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
696 }
697 // Skip the last annot_pragma_openmp_end.
698 ConsumeAnyToken();
699}
700
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000701/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000702///
703/// threadprivate-directive:
704/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000705/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000706///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000707/// declare-reduction-directive:
708/// annot_pragma_openmp 'declare' 'reduction' [...]
709/// annot_pragma_openmp_end
710///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000711/// declare-simd-directive:
712/// annot_pragma_openmp 'declare simd' {<clause> [,]}
713/// annot_pragma_openmp_end
714/// <function declaration/definition>
715///
Kelvin Li1408f912018-09-26 04:28:39 +0000716/// requires directive:
717/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
718/// annot_pragma_openmp_end
719///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000720Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
721 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
722 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000723 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000724 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000725
Richard Smithaf3b3252017-05-18 19:21:48 +0000726 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000727 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000728
729 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000730 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000731 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000732 ThreadprivateListParserHelper Helper(this);
733 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000734 // The last seen token is annot_pragma_openmp_end - need to check for
735 // extra tokens.
736 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
737 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000738 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000739 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000740 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000741 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000742 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000743 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
744 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000745 }
746 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000747 }
Kelvin Li1408f912018-09-26 04:28:39 +0000748 case OMPD_requires: {
749 SourceLocation StartLoc = ConsumeToken();
750 SmallVector<OMPClause *, 5> Clauses;
751 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
752 FirstClauses(OMPC_unknown + 1);
753 if (Tok.is(tok::annot_pragma_openmp_end)) {
754 Diag(Tok, diag::err_omp_expected_clause)
755 << getOpenMPDirectiveName(OMPD_requires);
756 break;
757 }
758 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
759 OpenMPClauseKind CKind = Tok.isAnnotation()
760 ? OMPC_unknown
761 : getOpenMPClauseKind(PP.getSpelling(Tok));
762 Actions.StartOpenMPClause(CKind);
763 OMPClause *Clause =
764 ParseOpenMPClause(OMPD_requires, CKind, !FirstClauses[CKind].getInt());
765 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, StopBeforeMatch);
766 FirstClauses[CKind].setInt(true);
767 if (Clause != nullptr)
768 Clauses.push_back(Clause);
769 if (Tok.is(tok::annot_pragma_openmp_end)) {
770 Actions.EndOpenMPClause();
771 break;
772 }
773 // Skip ',' if any.
774 if (Tok.is(tok::comma))
775 ConsumeToken();
776 Actions.EndOpenMPClause();
777 }
778 // Consume final annot_pragma_openmp_end
779 if (Clauses.size() == 0) {
780 Diag(Tok, diag::err_omp_expected_clause)
781 << getOpenMPDirectiveName(OMPD_requires);
782 ConsumeAnnotationToken();
783 return nullptr;
784 }
785 ConsumeAnnotationToken();
786 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
787 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000788 case OMPD_declare_reduction:
789 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000790 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000791 // The last seen token is annot_pragma_openmp_end - need to check for
792 // extra tokens.
793 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
794 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
795 << getOpenMPDirectiveName(OMPD_declare_reduction);
796 while (Tok.isNot(tok::annot_pragma_openmp_end))
797 ConsumeAnyToken();
798 }
799 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000800 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000801 return Res;
802 }
803 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000804 case OMPD_declare_simd: {
805 // The syntax is:
806 // { #pragma omp declare simd }
807 // <function-declaration-or-definition>
808 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000809 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000810 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000811 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
812 Toks.push_back(Tok);
813 ConsumeAnyToken();
814 }
815 Toks.push_back(Tok);
816 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000817
818 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000819 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000820 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000821 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000822 // Here we expect to see some function declaration.
823 if (AS == AS_none) {
824 assert(TagType == DeclSpec::TST_unspecified);
825 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000826 ParsingDeclSpec PDS(*this);
827 Ptr = ParseExternalDeclaration(Attrs, &PDS);
828 } else {
829 Ptr =
830 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
831 }
832 }
833 if (!Ptr) {
834 Diag(Loc, diag::err_omp_decl_in_declare_simd);
835 return DeclGroupPtrTy();
836 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000837 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000838 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000839 case OMPD_declare_target: {
840 SourceLocation DTLoc = ConsumeAnyToken();
841 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +0000842 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000843 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000844
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000845 // Skip the last annot_pragma_openmp_end.
846 ConsumeAnyToken();
847
848 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
849 return DeclGroupPtrTy();
850
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000851 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000852 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +0000853 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
854 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000855 DeclGroupPtrTy Ptr;
856 // Here we expect to see some function declaration.
857 if (AS == AS_none) {
858 assert(TagType == DeclSpec::TST_unspecified);
859 MaybeParseCXX11Attributes(Attrs);
860 ParsingDeclSpec PDS(*this);
861 Ptr = ParseExternalDeclaration(Attrs, &PDS);
862 } else {
863 Ptr =
864 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
865 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000866 if (Ptr) {
867 DeclGroupRef Ref = Ptr.get();
868 Decls.append(Ref.begin(), Ref.end());
869 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000870 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
871 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000872 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000873 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000874 if (DKind != OMPD_end_declare_target)
875 TPA.Revert();
876 else
877 TPA.Commit();
878 }
879 }
880
Kelvin Lie0502752018-11-21 20:15:57 +0000881 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000882 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000883 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000884 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000885 case OMPD_unknown:
886 Diag(Tok, diag::err_omp_unknown_directive);
887 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000888 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000889 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000890 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000891 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000892 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000893 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000894 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000895 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000896 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000897 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000898 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000899 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000900 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000901 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000902 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000903 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000904 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000905 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000906 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000907 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000908 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000909 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000910 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000911 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000912 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000913 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000914 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000915 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000916 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000917 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000918 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000919 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000920 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000921 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000922 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000923 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000924 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000925 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000926 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000927 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000928 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000929 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000930 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000931 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000932 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000933 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000934 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000935 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000936 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000937 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000938 break;
939 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000940 while (Tok.isNot(tok::annot_pragma_openmp_end))
941 ConsumeAnyToken();
942 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000943 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000944}
945
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000946/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000947///
948/// threadprivate-directive:
949/// annot_pragma_openmp 'threadprivate' simple-variable-list
950/// annot_pragma_openmp_end
951///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000952/// declare-reduction-directive:
953/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
954/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
955/// ('omp_priv' '=' <expression>|<function_call>) ')']
956/// annot_pragma_openmp_end
957///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000958/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000959/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000960/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
961/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000962/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000963/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000964/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000965/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000966/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000967/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000968/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000969/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000970/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000971/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000972/// 'teams distribute parallel for' | 'target teams' |
973/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000974/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000975/// 'target teams distribute parallel for simd' |
976/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000977/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000978///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000979StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000980 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000982 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000983 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000984 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000985 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000986 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
987 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000988 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000989 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000990 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000991 // Name of critical directive.
992 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000993 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000994 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000995 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000996
997 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000998 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000999 if (Allowed != ACK_Any) {
1000 Diag(Tok, diag::err_omp_immediate_directive)
1001 << getOpenMPDirectiveName(DKind) << 0;
1002 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001003 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001004 ThreadprivateListParserHelper Helper(this);
1005 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001006 // The last seen token is annot_pragma_openmp_end - need to check for
1007 // extra tokens.
1008 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1009 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001010 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +00001011 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001012 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001013 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1014 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001015 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1016 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001017 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001018 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001019 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001020 case OMPD_declare_reduction:
1021 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001022 if (DeclGroupPtrTy Res =
1023 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001024 // The last seen token is annot_pragma_openmp_end - need to check for
1025 // extra tokens.
1026 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1027 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1028 << getOpenMPDirectiveName(OMPD_declare_reduction);
1029 while (Tok.isNot(tok::annot_pragma_openmp_end))
1030 ConsumeAnyToken();
1031 }
1032 ConsumeAnyToken();
1033 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001034 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001035 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001036 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001037 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001038 case OMPD_flush:
1039 if (PP.LookAhead(0).is(tok::l_paren)) {
1040 FlushHasClause = true;
1041 // Push copy of the current token back to stream to properly parse
1042 // pseudo-clause OMPFlushClause.
1043 PP.EnterToken(Tok);
1044 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001045 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001046 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001047 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001048 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001049 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001050 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001051 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001052 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001053 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +00001054 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001055 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001056 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001057 }
1058 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001059 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001060 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001061 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001062 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001063 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001064 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001065 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001066 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001067 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001068 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001069 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001070 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001071 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001072 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001073 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001074 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001075 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001076 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001077 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001078 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001079 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001080 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001081 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001082 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001083 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001084 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001085 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001086 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001087 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001088 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001089 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001090 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001091 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001092 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001093 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001094 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001095 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001096 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001097 case OMPD_target_teams_distribute_parallel_for_simd:
1098 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001099 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001100 // Parse directive name of the 'critical' directive if any.
1101 if (DKind == OMPD_critical) {
1102 BalancedDelimiterTracker T(*this, tok::l_paren,
1103 tok::annot_pragma_openmp_end);
1104 if (!T.consumeOpen()) {
1105 if (Tok.isAnyIdentifier()) {
1106 DirName =
1107 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1108 ConsumeAnyToken();
1109 } else {
1110 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1111 }
1112 T.consumeClose();
1113 }
Alexey Bataev80909872015-07-02 11:25:17 +00001114 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001115 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001116 if (Tok.isNot(tok::annot_pragma_openmp_end))
1117 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001118 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001119
Alexey Bataevf29276e2014-06-18 04:14:57 +00001120 if (isOpenMPLoopDirective(DKind))
1121 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1122 if (isOpenMPSimdDirective(DKind))
1123 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1124 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001125 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001126
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001127 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001128 OpenMPClauseKind CKind =
1129 Tok.isAnnotation()
1130 ? OMPC_unknown
1131 : FlushHasClause ? OMPC_flush
1132 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001133 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001134 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001135 OMPClause *Clause =
1136 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001137 FirstClauses[CKind].setInt(true);
1138 if (Clause) {
1139 FirstClauses[CKind].setPointer(Clause);
1140 Clauses.push_back(Clause);
1141 }
1142
1143 // Skip ',' if any.
1144 if (Tok.is(tok::comma))
1145 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001146 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001147 }
1148 // End location of the directive.
1149 EndLoc = Tok.getLocation();
1150 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001151 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001152
Alexey Bataeveb482352015-12-18 05:05:56 +00001153 // OpenMP [2.13.8, ordered Construct, Syntax]
1154 // If the depend clause is specified, the ordered construct is a stand-alone
1155 // directive.
1156 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001157 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001158 Diag(Loc, diag::err_omp_immediate_directive)
1159 << getOpenMPDirectiveName(DKind) << 1
1160 << getOpenMPClauseName(OMPC_depend);
1161 }
1162 HasAssociatedStatement = false;
1163 }
1164
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001165 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001166 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001167 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001168 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001169 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1170 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1171 // should have at least one compound statement scope within it.
1172 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001173 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001174 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1175 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001176 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001177 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1178 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1179 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001180 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001181 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001182 Directive = Actions.ActOnOpenMPExecutableDirective(
1183 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1184 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001185
1186 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001187 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001188 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001190 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001191 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001192 case OMPD_declare_target:
1193 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001194 case OMPD_requires:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001195 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001196 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001197 SkipUntil(tok::annot_pragma_openmp_end);
1198 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001199 case OMPD_unknown:
1200 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001201 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001202 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001203 }
1204 return Directive;
1205}
1206
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001207// Parses simple list:
1208// simple-variable-list:
1209// '(' id-expression {, id-expression} ')'
1210//
1211bool Parser::ParseOpenMPSimpleVarList(
1212 OpenMPDirectiveKind Kind,
1213 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1214 Callback,
1215 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001216 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001217 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001218 if (T.expectAndConsume(diag::err_expected_lparen_after,
1219 getOpenMPDirectiveName(Kind)))
1220 return true;
1221 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001222 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001223
1224 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001225 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001226 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001227 UnqualifiedId Name;
1228 // Read var name.
1229 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001231
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001232 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001233 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001234 IsCorrect = false;
1235 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001236 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001237 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001238 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001239 IsCorrect = false;
1240 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001241 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001242 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1243 Tok.isNot(tok::annot_pragma_openmp_end)) {
1244 IsCorrect = false;
1245 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001246 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001247 Diag(PrevTok.getLocation(), diag::err_expected)
1248 << tok::identifier
1249 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001250 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001251 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001252 }
1253 // Consume ','.
1254 if (Tok.is(tok::comma)) {
1255 ConsumeToken();
1256 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001257 }
1258
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001259 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001260 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001261 IsCorrect = false;
1262 }
1263
1264 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001265 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001266
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001267 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001268}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001269
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001270/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001271///
1272/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001273/// if-clause | final-clause | num_threads-clause | safelen-clause |
1274/// default-clause | private-clause | firstprivate-clause | shared-clause
1275/// | linear-clause | aligned-clause | collapse-clause |
1276/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001277/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001278/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001279/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001280/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001281/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001282/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001283/// from-clause | is_device_ptr-clause | task_reduction-clause |
1284/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001285///
1286OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1287 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001288 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001289 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001290 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001291 // Check if clause is allowed for the given directive.
1292 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001293 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1294 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001295 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001296 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001297 }
1298
1299 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001300 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001301 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001302 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001303 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001304 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001305 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001306 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001307 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001308 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001309 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001310 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001311 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001312 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001313 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001314 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001315 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001316 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001317 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001318 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001319 // OpenMP [2.9.1, target data construct, Restrictions]
1320 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001321 // OpenMP [2.11.1, task Construct, Restrictions]
1322 // At most one if clause can appear on the directive.
1323 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001324 // OpenMP [teams Construct, Restrictions]
1325 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001326 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001327 // OpenMP [2.9.1, task Construct, Restrictions]
1328 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001329 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1330 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001331 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1332 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001333 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001334 Diag(Tok, diag::err_omp_more_one_clause)
1335 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001336 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001337 }
1338
Alexey Bataev10e775f2015-07-30 11:36:16 +00001339 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001340 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001341 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001342 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001343 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001344 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001345 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001346 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001347 // OpenMP [2.14.3.1, Restrictions]
1348 // Only a single default clause may be specified on a parallel, task or
1349 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001350 // OpenMP [2.5, parallel Construct, Restrictions]
1351 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001352 // OpenMP [5.0, Requires directive, Restrictions]
1353 // At most one atomic_default_mem_order clause can appear
1354 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001355 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001356 Diag(Tok, diag::err_omp_more_one_clause)
1357 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001358 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001359 }
1360
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001361 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001362 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001363 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001364 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001365 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001366 // OpenMP [2.7.1, Restrictions, p. 3]
1367 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001368 // OpenMP [2.10.4, Restrictions, p. 106]
1369 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001370 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001371 Diag(Tok, diag::err_omp_more_one_clause)
1372 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001373 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001374 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001375 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001376
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001377 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001378 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001379 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001380 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001381 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001382 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001383 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001384 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001385 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001386 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001387 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001388 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001389 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001390 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001391 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001392 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001393 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00001394 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001395 // OpenMP [2.7.1, Restrictions, p. 9]
1396 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001397 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1398 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001399 // OpenMP [5.0, Requires directive, Restrictions]
1400 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001401 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001402 Diag(Tok, diag::err_omp_more_one_clause)
1403 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001404 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001405 }
1406
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001407 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001408 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001409 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001410 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001411 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001412 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001413 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001414 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001415 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001416 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001417 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001418 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001419 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001420 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001421 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001422 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001423 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001424 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001425 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001426 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001427 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001428 break;
1429 case OMPC_unknown:
1430 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001431 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001432 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001433 break;
1434 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001435 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001436 if (!WrongDirective)
1437 Diag(Tok, diag::err_omp_unexpected_clause)
1438 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001439 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001440 break;
1441 }
Craig Topper161e4db2014-05-21 06:02:52 +00001442 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001443}
1444
Alexey Bataev2af33e32016-04-07 12:45:37 +00001445/// Parses simple expression in parens for single-expression clauses of OpenMP
1446/// constructs.
1447/// \param RLoc Returned location of right paren.
1448ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1449 SourceLocation &RLoc) {
1450 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1451 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1452 return ExprError();
1453
1454 SourceLocation ELoc = Tok.getLocation();
1455 ExprResult LHS(ParseCastExpression(
1456 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1457 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001458 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00001459
1460 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001461 RLoc = Tok.getLocation();
1462 if (!T.consumeClose())
1463 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001464
Alexey Bataev2af33e32016-04-07 12:45:37 +00001465 return Val;
1466}
1467
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001468/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001469/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001470/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001471///
Alexey Bataev3778b602014-07-17 07:32:53 +00001472/// final-clause:
1473/// 'final' '(' expression ')'
1474///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001475/// num_threads-clause:
1476/// 'num_threads' '(' expression ')'
1477///
1478/// safelen-clause:
1479/// 'safelen' '(' expression ')'
1480///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001481/// simdlen-clause:
1482/// 'simdlen' '(' expression ')'
1483///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001484/// collapse-clause:
1485/// 'collapse' '(' expression ')'
1486///
Alexey Bataeva0569352015-12-01 10:17:31 +00001487/// priority-clause:
1488/// 'priority' '(' expression ')'
1489///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001490/// grainsize-clause:
1491/// 'grainsize' '(' expression ')'
1492///
Alexey Bataev382967a2015-12-08 12:06:20 +00001493/// num_tasks-clause:
1494/// 'num_tasks' '(' expression ')'
1495///
Alexey Bataev28c75412015-12-15 08:19:24 +00001496/// hint-clause:
1497/// 'hint' '(' expression ')'
1498///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001499OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1500 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001501 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001502 SourceLocation LLoc = Tok.getLocation();
1503 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001504
Alexey Bataev2af33e32016-04-07 12:45:37 +00001505 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001506
1507 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001508 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001509
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001510 if (ParseOnly)
1511 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001512 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001513}
1514
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001515/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001516///
1517/// default-clause:
1518/// 'default' '(' 'none' | 'shared' ')
1519///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001520/// proc_bind-clause:
1521/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1522///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001523OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1524 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001525 SourceLocation Loc = Tok.getLocation();
1526 SourceLocation LOpen = ConsumeToken();
1527 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001528 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001529 if (T.expectAndConsume(diag::err_expected_lparen_after,
1530 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001531 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001532
Alexey Bataeva55ed262014-05-28 06:15:33 +00001533 unsigned Type = getOpenMPSimpleClauseType(
1534 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001535 SourceLocation TypeLoc = Tok.getLocation();
1536 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1537 Tok.isNot(tok::annot_pragma_openmp_end))
1538 ConsumeAnyToken();
1539
1540 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001541 SourceLocation RLoc = Tok.getLocation();
1542 if (!T.consumeClose())
1543 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001544
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001545 if (ParseOnly)
1546 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001547 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001548}
1549
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001550/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001551///
1552/// ordered-clause:
1553/// 'ordered'
1554///
Alexey Bataev236070f2014-06-20 11:19:47 +00001555/// nowait-clause:
1556/// 'nowait'
1557///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001558/// untied-clause:
1559/// 'untied'
1560///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001561/// mergeable-clause:
1562/// 'mergeable'
1563///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001564/// read-clause:
1565/// 'read'
1566///
Alexey Bataev346265e2015-09-25 10:37:12 +00001567/// threads-clause:
1568/// 'threads'
1569///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001570/// simd-clause:
1571/// 'simd'
1572///
Alexey Bataevb825de12015-12-07 10:51:44 +00001573/// nogroup-clause:
1574/// 'nogroup'
1575///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001576OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001577 SourceLocation Loc = Tok.getLocation();
1578 ConsumeAnyToken();
1579
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001580 if (ParseOnly)
1581 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001582 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1583}
1584
1585
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001586/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001587/// argument like 'schedule' or 'dist_schedule'.
1588///
1589/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001590/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1591/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001592///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001593/// if-clause:
1594/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1595///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001596/// defaultmap:
1597/// 'defaultmap' '(' modifier ':' kind ')'
1598///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001599OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1600 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001601 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001602 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001603 // Parse '('.
1604 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1605 if (T.expectAndConsume(diag::err_expected_lparen_after,
1606 getOpenMPClauseName(Kind)))
1607 return nullptr;
1608
1609 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001610 SmallVector<unsigned, 4> Arg;
1611 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001612 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001613 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1614 Arg.resize(NumberOfElements);
1615 KLoc.resize(NumberOfElements);
1616 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1617 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1618 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001619 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001620 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001621 if (KindModifier > OMPC_SCHEDULE_unknown) {
1622 // Parse 'modifier'
1623 Arg[Modifier1] = KindModifier;
1624 KLoc[Modifier1] = Tok.getLocation();
1625 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1626 Tok.isNot(tok::annot_pragma_openmp_end))
1627 ConsumeAnyToken();
1628 if (Tok.is(tok::comma)) {
1629 // Parse ',' 'modifier'
1630 ConsumeAnyToken();
1631 KindModifier = getOpenMPSimpleClauseType(
1632 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1633 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1634 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001635 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001636 KLoc[Modifier2] = Tok.getLocation();
1637 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1638 Tok.isNot(tok::annot_pragma_openmp_end))
1639 ConsumeAnyToken();
1640 }
1641 // Parse ':'
1642 if (Tok.is(tok::colon))
1643 ConsumeAnyToken();
1644 else
1645 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1646 KindModifier = getOpenMPSimpleClauseType(
1647 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1648 }
1649 Arg[ScheduleKind] = KindModifier;
1650 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001651 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1652 Tok.isNot(tok::annot_pragma_openmp_end))
1653 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001654 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1655 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1656 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001657 Tok.is(tok::comma))
1658 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001659 } else if (Kind == OMPC_dist_schedule) {
1660 Arg.push_back(getOpenMPSimpleClauseType(
1661 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1662 KLoc.push_back(Tok.getLocation());
1663 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1664 Tok.isNot(tok::annot_pragma_openmp_end))
1665 ConsumeAnyToken();
1666 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1667 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001668 } else if (Kind == OMPC_defaultmap) {
1669 // Get a defaultmap modifier
1670 Arg.push_back(getOpenMPSimpleClauseType(
1671 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1672 KLoc.push_back(Tok.getLocation());
1673 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1674 Tok.isNot(tok::annot_pragma_openmp_end))
1675 ConsumeAnyToken();
1676 // Parse ':'
1677 if (Tok.is(tok::colon))
1678 ConsumeAnyToken();
1679 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1680 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1681 // Get a defaultmap kind
1682 Arg.push_back(getOpenMPSimpleClauseType(
1683 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1684 KLoc.push_back(Tok.getLocation());
1685 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1686 Tok.isNot(tok::annot_pragma_openmp_end))
1687 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001688 } else {
1689 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001690 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001691 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001692 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001693 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001694 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001695 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1696 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001697 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001698 } else {
1699 TPA.Revert();
1700 Arg.back() = OMPD_unknown;
1701 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001702 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001703 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001704 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001705 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001706
Carlo Bertollib4adf552016-01-15 18:50:31 +00001707 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1708 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1709 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001710 if (NeedAnExpression) {
1711 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001712 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1713 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001714 Val =
1715 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001716 }
1717
1718 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001719 SourceLocation RLoc = Tok.getLocation();
1720 if (!T.consumeClose())
1721 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001722
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001723 if (NeedAnExpression && Val.isInvalid())
1724 return nullptr;
1725
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001726 if (ParseOnly)
1727 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001728 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001729 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001730}
1731
Alexey Bataevc5e02582014-06-16 07:08:35 +00001732static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1733 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001734 if (ReductionIdScopeSpec.isEmpty()) {
1735 auto OOK = OO_None;
1736 switch (P.getCurToken().getKind()) {
1737 case tok::plus:
1738 OOK = OO_Plus;
1739 break;
1740 case tok::minus:
1741 OOK = OO_Minus;
1742 break;
1743 case tok::star:
1744 OOK = OO_Star;
1745 break;
1746 case tok::amp:
1747 OOK = OO_Amp;
1748 break;
1749 case tok::pipe:
1750 OOK = OO_Pipe;
1751 break;
1752 case tok::caret:
1753 OOK = OO_Caret;
1754 break;
1755 case tok::ampamp:
1756 OOK = OO_AmpAmp;
1757 break;
1758 case tok::pipepipe:
1759 OOK = OO_PipePipe;
1760 break;
1761 default:
1762 break;
1763 }
1764 if (OOK != OO_None) {
1765 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001766 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001767 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1768 return false;
1769 }
1770 }
1771 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1772 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001773 /*AllowConstructorName*/ false,
1774 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001775 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001776}
1777
Kelvin Lief579432018-12-18 22:18:41 +00001778/// Checks if the token is a valid map-type-modifier.
1779static OpenMPMapModifierKind isMapModifier(Parser &P) {
1780 Token Tok = P.getCurToken();
1781 if (!Tok.is(tok::identifier))
1782 return OMPC_MAP_MODIFIER_unknown;
1783
1784 Preprocessor &PP = P.getPreprocessor();
1785 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
1786 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1787 return TypeModifier;
1788}
1789
1790/// Parse map-type-modifiers in map clause.
1791/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1792/// where, map-type-modifier ::= always | close
1793static void parseMapTypeModifiers(Parser &P,
1794 Parser::OpenMPVarListDataTy &Data) {
1795 Preprocessor &PP = P.getPreprocessor();
1796 while (P.getCurToken().isNot(tok::colon)) {
1797 Token Tok = P.getCurToken();
1798 OpenMPMapModifierKind TypeModifier = isMapModifier(P);
1799 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
1800 TypeModifier == OMPC_MAP_MODIFIER_close) {
1801 Data.MapTypeModifiers.push_back(TypeModifier);
1802 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
1803 P.ConsumeToken();
1804 } else {
1805 // For the case of unknown map-type-modifier or a map-type.
1806 // Map-type is followed by a colon; the function returns when it
1807 // encounters a token followed by a colon.
1808 if (Tok.is(tok::comma)) {
1809 P.Diag(Tok, diag::err_omp_map_type_modifier_missing);
1810 P.ConsumeToken();
1811 continue;
1812 }
1813 // Potential map-type token as it is followed by a colon.
1814 if (PP.LookAhead(0).is(tok::colon))
1815 return;
1816 P.Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1817 P.ConsumeToken();
1818 }
1819 if (P.getCurToken().is(tok::comma))
1820 P.ConsumeToken();
1821 }
1822}
1823
1824/// Checks if the token is a valid map-type.
1825static OpenMPMapClauseKind isMapType(Parser &P) {
1826 Token Tok = P.getCurToken();
1827 // The map-type token can be either an identifier or the C++ delete keyword.
1828 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
1829 return OMPC_MAP_unknown;
1830 Preprocessor &PP = P.getPreprocessor();
1831 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
1832 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
1833 return MapType;
1834}
1835
1836/// Parse map-type in map clause.
1837/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
1838/// where, map-type ::= to | from | tofrom | alloc | release | delete
1839static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
1840 Token Tok = P.getCurToken();
1841 if (Tok.is(tok::colon)) {
1842 P.Diag(Tok, diag::err_omp_map_type_missing);
1843 return;
1844 }
1845 Data.MapType = isMapType(P);
1846 if (Data.MapType == OMPC_MAP_unknown)
1847 P.Diag(Tok, diag::err_omp_unknown_map_type);
1848 P.ConsumeToken();
1849}
1850
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001851/// Parses clauses with list.
1852bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1853 OpenMPClauseKind Kind,
1854 SmallVectorImpl<Expr *> &Vars,
1855 OpenMPVarListDataTy &Data) {
1856 UnqualifiedId UnqualifiedReductionId;
1857 bool InvalidReductionId = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001858
1859 // Parse '('.
1860 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1861 if (T.expectAndConsume(diag::err_expected_lparen_after,
1862 getOpenMPClauseName(Kind)))
1863 return true;
1864
1865 bool NeedRParenForLinear = false;
1866 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1867 tok::annot_pragma_openmp_end);
1868 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001869 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1870 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001871 ColonProtectionRAIIObject ColonRAII(*this);
1872 if (getLangOpts().CPlusPlus)
1873 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1874 /*ObjectType=*/nullptr,
1875 /*EnteringContext=*/false);
1876 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1877 UnqualifiedReductionId);
1878 if (InvalidReductionId) {
1879 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1880 StopBeforeMatch);
1881 }
1882 if (Tok.is(tok::colon))
1883 Data.ColonLoc = ConsumeToken();
1884 else
1885 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1886 if (!InvalidReductionId)
1887 Data.ReductionId =
1888 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1889 } else if (Kind == OMPC_depend) {
1890 // Handle dependency type for depend clause.
1891 ColonProtectionRAIIObject ColonRAII(*this);
1892 Data.DepKind =
1893 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1894 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1895 Data.DepLinMapLoc = Tok.getLocation();
1896
1897 if (Data.DepKind == OMPC_DEPEND_unknown) {
1898 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1899 StopBeforeMatch);
1900 } else {
1901 ConsumeToken();
1902 // Special processing for depend(source) clause.
1903 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1904 // Parse ')'.
1905 T.consumeClose();
1906 return false;
1907 }
1908 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001909 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001910 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001911 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001912 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1913 : diag::warn_pragma_expected_colon)
1914 << "dependency type";
1915 }
1916 } else if (Kind == OMPC_linear) {
1917 // Try to parse modifier if any.
1918 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1919 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1920 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1921 Data.DepLinMapLoc = ConsumeToken();
1922 LinearT.consumeOpen();
1923 NeedRParenForLinear = true;
1924 }
1925 } else if (Kind == OMPC_map) {
1926 // Handle map type for map clause.
1927 ColonProtectionRAIIObject ColonRAII(*this);
1928
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001929 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00001930 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001931 // spelling of the C++ delete keyword.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001932 Data.DepLinMapLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001933
Kelvin Lief579432018-12-18 22:18:41 +00001934 // Check for presence of a colon in the map clause.
1935 TentativeParsingAction TPA(*this);
1936 bool ColonPresent = false;
1937 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1938 StopBeforeMatch)) {
1939 if (Tok.is(tok::colon))
1940 ColonPresent = true;
1941 }
1942 TPA.Revert();
1943 // Only parse map-type-modifier[s] and map-type if a colon is present in
1944 // the map clause.
1945 if (ColonPresent) {
1946 parseMapTypeModifiers(*this, Data);
1947 parseMapType(*this, Data);
1948 }
1949 if (Data.MapType == OMPC_MAP_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001950 Data.MapType = OMPC_MAP_tofrom;
1951 Data.IsMapTypeImplicit = true;
1952 }
1953
1954 if (Tok.is(tok::colon))
1955 Data.ColonLoc = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001956 }
1957
Alexey Bataevfa312f32017-07-21 18:48:21 +00001958 bool IsComma =
1959 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1960 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1961 (Kind == OMPC_reduction && !InvalidReductionId) ||
Kelvin Lida6bc702018-11-21 19:38:53 +00001962 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown) ||
Alexey Bataevfa312f32017-07-21 18:48:21 +00001963 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001964 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1965 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1966 Tok.isNot(tok::annot_pragma_openmp_end))) {
1967 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1968 // Parse variable
1969 ExprResult VarExpr =
1970 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001971 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001972 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001973 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001974 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1975 StopBeforeMatch);
1976 }
1977 // Skip ',' if any
1978 IsComma = Tok.is(tok::comma);
1979 if (IsComma)
1980 ConsumeToken();
1981 else if (Tok.isNot(tok::r_paren) &&
1982 Tok.isNot(tok::annot_pragma_openmp_end) &&
1983 (!MayHaveTail || Tok.isNot(tok::colon)))
1984 Diag(Tok, diag::err_omp_expected_punc)
1985 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1986 : getOpenMPClauseName(Kind))
1987 << (Kind == OMPC_flush);
1988 }
1989
1990 // Parse ')' for linear clause with modifier.
1991 if (NeedRParenForLinear)
1992 LinearT.consumeClose();
1993
1994 // Parse ':' linear-step (or ':' alignment).
1995 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1996 if (MustHaveTail) {
1997 Data.ColonLoc = Tok.getLocation();
1998 SourceLocation ELoc = ConsumeToken();
1999 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002000 Tail =
2001 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002002 if (Tail.isUsable())
2003 Data.TailExpr = Tail.get();
2004 else
2005 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2006 StopBeforeMatch);
2007 }
2008
2009 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002010 Data.RLoc = Tok.getLocation();
2011 if (!T.consumeClose())
2012 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00002013 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
2014 Vars.empty()) ||
2015 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
2016 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002017}
2018
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002019/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002020/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2021/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002022///
2023/// private-clause:
2024/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002025/// firstprivate-clause:
2026/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002027/// lastprivate-clause:
2028/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002029/// shared-clause:
2030/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002031/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002032/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002033/// aligned-clause:
2034/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002035/// reduction-clause:
2036/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002037/// task_reduction-clause:
2038/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002039/// in_reduction-clause:
2040/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002041/// copyprivate-clause:
2042/// 'copyprivate' '(' list ')'
2043/// flush-clause:
2044/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002045/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002046/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002047/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002048/// 'map' '(' [ [ always [,] ] [ close [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002049/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002050/// to-clause:
2051/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002052/// from-clause:
2053/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002054/// use_device_ptr-clause:
2055/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002056/// is_device_ptr-clause:
2057/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002058///
Alexey Bataev182227b2015-08-20 10:54:39 +00002059/// For 'linear' clause linear-list may have the following forms:
2060/// list
2061/// modifier(list)
2062/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002063OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002064 OpenMPClauseKind Kind,
2065 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002066 SourceLocation Loc = Tok.getLocation();
2067 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002068 SmallVector<Expr *, 4> Vars;
2069 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002070
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002071 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002072 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002073
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002074 if (ParseOnly)
2075 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002076 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002077 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002078 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
Kelvin Lief579432018-12-18 22:18:41 +00002079 Data.MapTypeModifiers, Data.MapTypeModifiersLoc, Data.MapType,
2080 Data.IsMapTypeImplicit, Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002081}
2082