blob: 94eabf4797de2ed4e5aa8885ba1e590a2a6af69d [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000011///
12//===----------------------------------------------------------------------===//
13
Alexey Bataev9959db52014-05-06 10:08:46 +000014#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000015#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000043 OMPD_teams_distribute_parallel,
44 OMPD_target_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
47class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
50
51public:
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
54 ExprResult Res =
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
56 if (Res.isUsable())
57 Identifiers.push_back(Res.get());
58 }
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
60};
Dmitry Polukhin82478332016-02-13 06:53:38 +000061} // namespace
62
63// Map token string to extended OMP token kind that are
64// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
68 return DKind;
69
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000073 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000074 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000079 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Default(OMPD_unknown);
81}
82
Alexey Bataev61908f652018-04-23 19:53:05 +000083static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000084 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
85 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
86 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 static const unsigned F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +000088 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
89 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
90 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
91 {OMPD_declare, OMPD_target, OMPD_declare_target},
92 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
93 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
94 {OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd},
96 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
97 {OMPD_end, OMPD_declare, OMPD_end_declare},
98 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
99 {OMPD_target, OMPD_data, OMPD_target_data},
100 {OMPD_target, OMPD_enter, OMPD_target_enter},
101 {OMPD_target, OMPD_exit, OMPD_target_exit},
102 {OMPD_target, OMPD_update, OMPD_target_update},
103 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
104 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
105 {OMPD_for, OMPD_simd, OMPD_for_simd},
106 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
107 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
108 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
109 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
110 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
111 {OMPD_target, OMPD_simd, OMPD_target_simd},
112 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
113 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
114 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
115 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
116 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
117 {OMPD_teams_distribute_parallel, OMPD_for,
118 OMPD_teams_distribute_parallel_for},
119 {OMPD_teams_distribute_parallel_for, OMPD_simd,
120 OMPD_teams_distribute_parallel_for_simd},
121 {OMPD_target, OMPD_teams, OMPD_target_teams},
122 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
123 {OMPD_target_teams_distribute, OMPD_parallel,
124 OMPD_target_teams_distribute_parallel},
125 {OMPD_target_teams_distribute, OMPD_simd,
126 OMPD_target_teams_distribute_simd},
127 {OMPD_target_teams_distribute_parallel, OMPD_for,
128 OMPD_target_teams_distribute_parallel_for},
129 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
130 OMPD_target_teams_distribute_parallel_for_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000131 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000132 Token Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000133 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000134 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000135 ? static_cast<unsigned>(OMPD_unknown)
136 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
137 if (DKind == OMPD_unknown)
138 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000139
Alexey Bataev61908f652018-04-23 19:53:05 +0000140 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
141 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000142 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000143
Dmitry Polukhin82478332016-02-13 06:53:38 +0000144 Tok = P.getPreprocessor().LookAhead(0);
145 unsigned SDKind =
146 Tok.isAnnotation()
147 ? static_cast<unsigned>(OMPD_unknown)
148 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
149 if (SDKind == OMPD_unknown)
150 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000151
Alexey Bataev61908f652018-04-23 19:53:05 +0000152 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000153 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000154 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000155 }
156 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000157 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
158 : OMPD_unknown;
159}
160
161static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000162 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000163 Sema &Actions = P.getActions();
164 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000165 // Allow to use 'operator' keyword for C++ operators
166 bool WithOperator = false;
167 if (Tok.is(tok::kw_operator)) {
168 P.ConsumeToken();
169 Tok = P.getCurToken();
170 WithOperator = true;
171 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000172 switch (Tok.getKind()) {
173 case tok::plus: // '+'
174 OOK = OO_Plus;
175 break;
176 case tok::minus: // '-'
177 OOK = OO_Minus;
178 break;
179 case tok::star: // '*'
180 OOK = OO_Star;
181 break;
182 case tok::amp: // '&'
183 OOK = OO_Amp;
184 break;
185 case tok::pipe: // '|'
186 OOK = OO_Pipe;
187 break;
188 case tok::caret: // '^'
189 OOK = OO_Caret;
190 break;
191 case tok::ampamp: // '&&'
192 OOK = OO_AmpAmp;
193 break;
194 case tok::pipepipe: // '||'
195 OOK = OO_PipePipe;
196 break;
197 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000198 if (!WithOperator)
199 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000200 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 default:
202 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
203 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
204 Parser::StopBeforeMatch);
205 return DeclarationName();
206 }
207 P.ConsumeToken();
208 auto &DeclNames = Actions.getASTContext().DeclarationNames;
209 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
210 : DeclNames.getCXXOperatorName(OOK);
211}
212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000213/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000214///
215/// declare-reduction-directive:
216/// annot_pragma_openmp 'declare' 'reduction'
217/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
218/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
219/// annot_pragma_openmp_end
220/// <reduction_id> is either a base language identifier or one of the following
221/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
222///
223Parser::DeclGroupPtrTy
224Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
225 // Parse '('.
226 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
227 if (T.expectAndConsume(diag::err_expected_lparen_after,
228 getOpenMPDirectiveName(OMPD_declare_reduction))) {
229 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
230 return DeclGroupPtrTy();
231 }
232
233 DeclarationName Name = parseOpenMPReductionId(*this);
234 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
235 return DeclGroupPtrTy();
236
237 // Consume ':'.
238 bool IsCorrect = !ExpectAndConsume(tok::colon);
239
240 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
241 return DeclGroupPtrTy();
242
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000243 IsCorrect = IsCorrect && !Name.isEmpty();
244
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000245 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
246 Diag(Tok.getLocation(), diag::err_expected_type);
247 IsCorrect = false;
248 }
249
250 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
251 return DeclGroupPtrTy();
252
253 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
254 // Parse list of types until ':' token.
255 do {
256 ColonProtectionRAIIObject ColonRAII(*this);
257 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000258 TypeResult TR =
259 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000260 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000261 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000262 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
263 if (!ReductionType.isNull()) {
264 ReductionTypes.push_back(
265 std::make_pair(ReductionType, Range.getBegin()));
266 }
267 } else {
268 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
269 StopBeforeMatch);
270 }
271
272 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
273 break;
274
275 // Consume ','.
276 if (ExpectAndConsume(tok::comma)) {
277 IsCorrect = false;
278 if (Tok.is(tok::annot_pragma_openmp_end)) {
279 Diag(Tok.getLocation(), diag::err_expected_type);
280 return DeclGroupPtrTy();
281 }
282 }
283 } while (Tok.isNot(tok::annot_pragma_openmp_end));
284
285 if (ReductionTypes.empty()) {
286 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
287 return DeclGroupPtrTy();
288 }
289
290 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
291 return DeclGroupPtrTy();
292
293 // Consume ':'.
294 if (ExpectAndConsume(tok::colon))
295 IsCorrect = false;
296
297 if (Tok.is(tok::annot_pragma_openmp_end)) {
298 Diag(Tok.getLocation(), diag::err_expected_expression);
299 return DeclGroupPtrTy();
300 }
301
302 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
303 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
304
305 // Parse <combiner> expression and then parse initializer if any for each
306 // correct type.
307 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000308 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000309 TentativeParsingAction TPA(*this);
310 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000311 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000312 Scope::OpenMPDirectiveScope);
313 // Parse <combiner> expression.
314 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
315 ExprResult CombinerResult =
316 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
317 D->getLocation(), /*DiscardedValue=*/true);
318 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
319
320 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
321 Tok.isNot(tok::annot_pragma_openmp_end)) {
322 TPA.Commit();
323 IsCorrect = false;
324 break;
325 }
326 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
327 ExprResult InitializerResult;
328 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
329 // Parse <initializer> expression.
330 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000331 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000332 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000333 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000334 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
335 TPA.Commit();
336 IsCorrect = false;
337 break;
338 }
339 // Parse '('.
340 BalancedDelimiterTracker T(*this, tok::l_paren,
341 tok::annot_pragma_openmp_end);
342 IsCorrect =
343 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
344 IsCorrect;
345 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
346 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000347 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000348 Scope::OpenMPDirectiveScope);
349 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000350 VarDecl *OmpPrivParm =
351 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
352 D);
353 // Check if initializer is omp_priv <init_expr> or something else.
354 if (Tok.is(tok::identifier) &&
355 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000356 if (Actions.getLangOpts().CPlusPlus) {
357 InitializerResult = Actions.ActOnFinishFullExpr(
358 ParseAssignmentExpression().get(), D->getLocation(),
359 /*DiscardedValue=*/true);
360 } 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(),
367 /*DiscardedValue=*/true);
368 }
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.
498 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
499 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000647/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000648///
649/// threadprivate-directive:
650/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000651/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000652///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000653/// declare-reduction-directive:
654/// annot_pragma_openmp 'declare' 'reduction' [...]
655/// annot_pragma_openmp_end
656///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000657/// declare-simd-directive:
658/// annot_pragma_openmp 'declare simd' {<clause> [,]}
659/// annot_pragma_openmp_end
660/// <function declaration/definition>
661///
Kelvin Li1408f912018-09-26 04:28:39 +0000662/// requires directive:
663/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
664/// annot_pragma_openmp_end
665///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000666Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
667 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
668 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000669 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000670 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000671
Richard Smithaf3b3252017-05-18 19:21:48 +0000672 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000673 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000674
675 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000676 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000677 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000678 ThreadprivateListParserHelper Helper(this);
679 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000680 // The last seen token is annot_pragma_openmp_end - need to check for
681 // extra tokens.
682 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
683 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000684 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000685 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000686 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000687 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000688 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000689 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
690 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000691 }
692 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000693 }
Kelvin Li1408f912018-09-26 04:28:39 +0000694 case OMPD_requires: {
695 SourceLocation StartLoc = ConsumeToken();
696 SmallVector<OMPClause *, 5> Clauses;
697 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
698 FirstClauses(OMPC_unknown + 1);
699 if (Tok.is(tok::annot_pragma_openmp_end)) {
700 Diag(Tok, diag::err_omp_expected_clause)
701 << getOpenMPDirectiveName(OMPD_requires);
702 break;
703 }
704 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
705 OpenMPClauseKind CKind = Tok.isAnnotation()
706 ? OMPC_unknown
707 : getOpenMPClauseKind(PP.getSpelling(Tok));
708 Actions.StartOpenMPClause(CKind);
709 OMPClause *Clause =
710 ParseOpenMPClause(OMPD_requires, CKind, !FirstClauses[CKind].getInt());
711 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end, StopBeforeMatch);
712 FirstClauses[CKind].setInt(true);
713 if (Clause != nullptr)
714 Clauses.push_back(Clause);
715 if (Tok.is(tok::annot_pragma_openmp_end)) {
716 Actions.EndOpenMPClause();
717 break;
718 }
719 // Skip ',' if any.
720 if (Tok.is(tok::comma))
721 ConsumeToken();
722 Actions.EndOpenMPClause();
723 }
724 // Consume final annot_pragma_openmp_end
725 if (Clauses.size() == 0) {
726 Diag(Tok, diag::err_omp_expected_clause)
727 << getOpenMPDirectiveName(OMPD_requires);
728 ConsumeAnnotationToken();
729 return nullptr;
730 }
731 ConsumeAnnotationToken();
732 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
733 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000734 case OMPD_declare_reduction:
735 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000736 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000737 // The last seen token is annot_pragma_openmp_end - need to check for
738 // extra tokens.
739 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
740 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
741 << getOpenMPDirectiveName(OMPD_declare_reduction);
742 while (Tok.isNot(tok::annot_pragma_openmp_end))
743 ConsumeAnyToken();
744 }
745 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000746 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000747 return Res;
748 }
749 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000750 case OMPD_declare_simd: {
751 // The syntax is:
752 // { #pragma omp declare simd }
753 // <function-declaration-or-definition>
754 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000755 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000756 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000757 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
758 Toks.push_back(Tok);
759 ConsumeAnyToken();
760 }
761 Toks.push_back(Tok);
762 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000763
764 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +0000765 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000766 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +0000767 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000768 // Here we expect to see some function declaration.
769 if (AS == AS_none) {
770 assert(TagType == DeclSpec::TST_unspecified);
771 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000772 ParsingDeclSpec PDS(*this);
773 Ptr = ParseExternalDeclaration(Attrs, &PDS);
774 } else {
775 Ptr =
776 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
777 }
778 }
779 if (!Ptr) {
780 Diag(Loc, diag::err_omp_decl_in_declare_simd);
781 return DeclGroupPtrTy();
782 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000783 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000784 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000785 case OMPD_declare_target: {
786 SourceLocation DTLoc = ConsumeAnyToken();
787 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000788 // OpenMP 4.5 syntax with list of entities.
Alexey Bataev34f8a702018-03-28 14:28:54 +0000789 Sema::NamedDeclSetType SameDirectiveDecls;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000790 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
791 OMPDeclareTargetDeclAttr::MapTypeTy MT =
792 OMPDeclareTargetDeclAttr::MT_To;
793 if (Tok.is(tok::identifier)) {
794 IdentifierInfo *II = Tok.getIdentifierInfo();
795 StringRef ClauseName = II->getName();
796 // Parse 'to|link' clauses.
797 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
798 MT)) {
799 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
800 << ClauseName;
801 break;
802 }
803 ConsumeToken();
804 }
Alexey Bataev61908f652018-04-23 19:53:05 +0000805 auto &&Callback = [this, MT, &SameDirectiveDecls](
806 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000807 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
808 SameDirectiveDecls);
809 };
Alexey Bataev34f8a702018-03-28 14:28:54 +0000810 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
811 /*AllowScopeSpecifier=*/true))
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000812 break;
813
814 // Consume optional ','.
815 if (Tok.is(tok::comma))
816 ConsumeToken();
817 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000818 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000819 ConsumeAnyToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000820 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
821 SameDirectiveDecls.end());
Alexey Bataev34f8a702018-03-28 14:28:54 +0000822 if (Decls.empty())
823 return DeclGroupPtrTy();
824 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000825 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000826
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000827 // Skip the last annot_pragma_openmp_end.
828 ConsumeAnyToken();
829
830 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
831 return DeclGroupPtrTy();
832
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000833 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +0000834 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +0000835 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
836 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +0000837 DeclGroupPtrTy Ptr;
838 // Here we expect to see some function declaration.
839 if (AS == AS_none) {
840 assert(TagType == DeclSpec::TST_unspecified);
841 MaybeParseCXX11Attributes(Attrs);
842 ParsingDeclSpec PDS(*this);
843 Ptr = ParseExternalDeclaration(Attrs, &PDS);
844 } else {
845 Ptr =
846 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
847 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000848 if (Ptr) {
849 DeclGroupRef Ref = Ptr.get();
850 Decls.append(Ref.begin(), Ref.end());
851 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000852 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
853 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +0000854 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000855 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000856 if (DKind != OMPD_end_declare_target)
857 TPA.Revert();
858 else
859 TPA.Commit();
860 }
861 }
862
863 if (DKind == OMPD_end_declare_target) {
864 ConsumeAnyToken();
865 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
866 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
867 << getOpenMPDirectiveName(OMPD_end_declare_target);
868 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
869 }
870 // Skip the last annot_pragma_openmp_end.
871 ConsumeAnyToken();
872 } else {
873 Diag(Tok, diag::err_expected_end_declare_target);
874 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
875 }
876 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +0000877 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000878 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000879 case OMPD_unknown:
880 Diag(Tok, diag::err_omp_unknown_directive);
881 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000882 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000883 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000884 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000885 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000886 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000887 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000888 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000889 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000890 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000891 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000892 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000893 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000894 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000895 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000896 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000897 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000898 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000899 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000900 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000901 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000902 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000903 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000904 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000905 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000906 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000907 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000908 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000909 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000910 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000911 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000912 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000913 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000914 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000915 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000916 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000917 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000918 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000919 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000920 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000921 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000922 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000923 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000924 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000925 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000926 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000927 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000928 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000929 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000930 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +0000931 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000932 break;
933 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000934 while (Tok.isNot(tok::annot_pragma_openmp_end))
935 ConsumeAnyToken();
936 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000937 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000938}
939
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000940/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941///
942/// threadprivate-directive:
943/// annot_pragma_openmp 'threadprivate' simple-variable-list
944/// annot_pragma_openmp_end
945///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000946/// declare-reduction-directive:
947/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
948/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
949/// ('omp_priv' '=' <expression>|<function_call>) ')']
950/// annot_pragma_openmp_end
951///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000952/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000953/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000954/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
955/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000956/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000957/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000958/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000959/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000960/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000961/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000962/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000963/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000964/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000965/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000966/// 'teams distribute parallel for' | 'target teams' |
967/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000968/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000969/// 'target teams distribute parallel for simd' |
970/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000971/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000973StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000974 AllowedConstructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000975 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000976 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000978 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000979 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +0000980 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
981 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +0000982 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +0000983 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000984 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000985 // Name of critical directive.
986 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000987 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000988 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000989 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000990
991 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000992 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000993 if (Allowed != ACK_Any) {
994 Diag(Tok, diag::err_omp_immediate_directive)
995 << getOpenMPDirectiveName(DKind) << 0;
996 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000997 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000998 ThreadprivateListParserHelper Helper(this);
999 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000 // The last seen token is annot_pragma_openmp_end - need to check for
1001 // extra tokens.
1002 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1003 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001004 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +00001005 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001006 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001007 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1008 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001009 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1010 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001011 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001012 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001013 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001014 case OMPD_declare_reduction:
1015 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001016 if (DeclGroupPtrTy Res =
1017 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001018 // The last seen token is annot_pragma_openmp_end - need to check for
1019 // extra tokens.
1020 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1021 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1022 << getOpenMPDirectiveName(OMPD_declare_reduction);
1023 while (Tok.isNot(tok::annot_pragma_openmp_end))
1024 ConsumeAnyToken();
1025 }
1026 ConsumeAnyToken();
1027 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001028 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001029 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001030 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001031 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001032 case OMPD_flush:
1033 if (PP.LookAhead(0).is(tok::l_paren)) {
1034 FlushHasClause = true;
1035 // Push copy of the current token back to stream to properly parse
1036 // pseudo-clause OMPFlushClause.
1037 PP.EnterToken(Tok);
1038 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001039 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001040 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001041 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001042 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001043 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001044 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001045 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001046 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001047 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +00001048 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001049 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001050 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001051 }
1052 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001053 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001054 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001055 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001056 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001057 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001058 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001059 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001060 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001061 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001062 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001063 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001064 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001065 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001066 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001067 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001068 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001069 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001070 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001071 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001072 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001073 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001074 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001075 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001076 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001077 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001078 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001079 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001080 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001081 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001082 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001083 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001084 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001085 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001086 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001087 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001088 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001089 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001090 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001091 case OMPD_target_teams_distribute_parallel_for_simd:
1092 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001093 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001094 // Parse directive name of the 'critical' directive if any.
1095 if (DKind == OMPD_critical) {
1096 BalancedDelimiterTracker T(*this, tok::l_paren,
1097 tok::annot_pragma_openmp_end);
1098 if (!T.consumeOpen()) {
1099 if (Tok.isAnyIdentifier()) {
1100 DirName =
1101 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1102 ConsumeAnyToken();
1103 } else {
1104 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1105 }
1106 T.consumeClose();
1107 }
Alexey Bataev80909872015-07-02 11:25:17 +00001108 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001109 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001110 if (Tok.isNot(tok::annot_pragma_openmp_end))
1111 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001112 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001113
Alexey Bataevf29276e2014-06-18 04:14:57 +00001114 if (isOpenMPLoopDirective(DKind))
1115 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1116 if (isOpenMPSimdDirective(DKind))
1117 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1118 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001119 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001120
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001121 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001122 OpenMPClauseKind CKind =
1123 Tok.isAnnotation()
1124 ? OMPC_unknown
1125 : FlushHasClause ? OMPC_flush
1126 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001127 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001128 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001129 OMPClause *Clause =
1130 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001131 FirstClauses[CKind].setInt(true);
1132 if (Clause) {
1133 FirstClauses[CKind].setPointer(Clause);
1134 Clauses.push_back(Clause);
1135 }
1136
1137 // Skip ',' if any.
1138 if (Tok.is(tok::comma))
1139 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001140 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001141 }
1142 // End location of the directive.
1143 EndLoc = Tok.getLocation();
1144 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001145 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001146
Alexey Bataeveb482352015-12-18 05:05:56 +00001147 // OpenMP [2.13.8, ordered Construct, Syntax]
1148 // If the depend clause is specified, the ordered construct is a stand-alone
1149 // directive.
1150 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001151 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001152 Diag(Loc, diag::err_omp_immediate_directive)
1153 << getOpenMPDirectiveName(DKind) << 1
1154 << getOpenMPClauseName(OMPC_depend);
1155 }
1156 HasAssociatedStatement = false;
1157 }
1158
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001159 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001160 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001161 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001162 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001163 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1164 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1165 // should have at least one compound statement scope within it.
1166 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001167 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001168 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1169 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001170 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001171 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1172 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1173 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001174 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001175 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001176 Directive = Actions.ActOnOpenMPExecutableDirective(
1177 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1178 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001179
1180 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001181 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001182 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001183 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001184 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001185 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001186 case OMPD_declare_target:
1187 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001188 case OMPD_requires:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001189 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001190 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001191 SkipUntil(tok::annot_pragma_openmp_end);
1192 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001193 case OMPD_unknown:
1194 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001195 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001196 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001197 }
1198 return Directive;
1199}
1200
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001201// Parses simple list:
1202// simple-variable-list:
1203// '(' id-expression {, id-expression} ')'
1204//
1205bool Parser::ParseOpenMPSimpleVarList(
1206 OpenMPDirectiveKind Kind,
1207 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1208 Callback,
1209 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001210 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001211 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001212 if (T.expectAndConsume(diag::err_expected_lparen_after,
1213 getOpenMPDirectiveName(Kind)))
1214 return true;
1215 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001216 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001217
1218 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001219 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001220 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001221 UnqualifiedId Name;
1222 // Read var name.
1223 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001224 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001225
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001226 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001227 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001228 IsCorrect = false;
1229 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001230 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00001231 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00001232 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001233 IsCorrect = false;
1234 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001235 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001236 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1237 Tok.isNot(tok::annot_pragma_openmp_end)) {
1238 IsCorrect = false;
1239 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001240 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001241 Diag(PrevTok.getLocation(), diag::err_expected)
1242 << tok::identifier
1243 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001244 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001245 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001246 }
1247 // Consume ','.
1248 if (Tok.is(tok::comma)) {
1249 ConsumeToken();
1250 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001251 }
1252
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001253 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001254 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001255 IsCorrect = false;
1256 }
1257
1258 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001259 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001260
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001261 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001262}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001263
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001264/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001265///
1266/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001267/// if-clause | final-clause | num_threads-clause | safelen-clause |
1268/// default-clause | private-clause | firstprivate-clause | shared-clause
1269/// | linear-clause | aligned-clause | collapse-clause |
1270/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001271/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001272/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001273/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001274/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001275/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001276/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00001277/// from-clause | is_device_ptr-clause | task_reduction-clause |
1278/// in_reduction-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001279///
1280OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1281 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001282 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001283 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001284 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001285 // Check if clause is allowed for the given directive.
1286 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001287 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1288 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001289 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001290 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001291 }
1292
1293 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001294 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001295 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001296 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001297 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001298 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001299 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001300 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001301 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001302 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001303 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001304 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001305 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001306 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001307 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001308 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001309 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001310 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001311 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001312 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001313 // OpenMP [2.9.1, target data construct, Restrictions]
1314 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001315 // OpenMP [2.11.1, task Construct, Restrictions]
1316 // At most one if clause can appear on the directive.
1317 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001318 // OpenMP [teams Construct, Restrictions]
1319 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001320 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001321 // OpenMP [2.9.1, task Construct, Restrictions]
1322 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001323 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1324 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001325 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1326 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001327 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001328 Diag(Tok, diag::err_omp_more_one_clause)
1329 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001330 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001331 }
1332
Alexey Bataev10e775f2015-07-30 11:36:16 +00001333 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001334 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00001335 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001336 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001337 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001338 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001339 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001340 // OpenMP [2.14.3.1, Restrictions]
1341 // Only a single default clause may be specified on a parallel, task or
1342 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001343 // OpenMP [2.5, parallel Construct, Restrictions]
1344 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001345 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001346 Diag(Tok, diag::err_omp_more_one_clause)
1347 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001348 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001349 }
1350
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001351 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001352 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001353 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001354 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001355 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001356 // OpenMP [2.7.1, Restrictions, p. 3]
1357 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001358 // OpenMP [2.10.4, Restrictions, p. 106]
1359 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001360 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001361 Diag(Tok, diag::err_omp_more_one_clause)
1362 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001363 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001364 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001365 LLVM_FALLTHROUGH;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001366
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001367 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001368 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001369 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001370 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001371 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001372 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001373 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001374 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001375 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001376 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001377 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001378 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001379 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001380 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00001381 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00001382 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00001383 case OMPC_reverse_offload:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001384 // OpenMP [2.7.1, Restrictions, p. 9]
1385 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001386 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1387 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00001388 // OpenMP [5.0, Requires directive, Restrictions]
1389 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001390 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001391 Diag(Tok, diag::err_omp_more_one_clause)
1392 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001393 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001394 }
1395
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001396 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001397 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001398 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001399 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001400 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001401 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001402 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00001403 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00001404 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001405 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001406 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001407 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001408 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001409 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001410 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001411 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001412 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001413 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001414 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001415 case OMPC_is_device_ptr:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001416 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001417 break;
1418 case OMPC_unknown:
1419 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001420 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001421 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001422 break;
1423 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001424 case OMPC_uniform:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001425 if (!WrongDirective)
1426 Diag(Tok, diag::err_omp_unexpected_clause)
1427 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001428 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001429 break;
1430 }
Craig Topper161e4db2014-05-21 06:02:52 +00001431 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001432}
1433
Alexey Bataev2af33e32016-04-07 12:45:37 +00001434/// Parses simple expression in parens for single-expression clauses of OpenMP
1435/// constructs.
1436/// \param RLoc Returned location of right paren.
1437ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1438 SourceLocation &RLoc) {
1439 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1440 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1441 return ExprError();
1442
1443 SourceLocation ELoc = Tok.getLocation();
1444 ExprResult LHS(ParseCastExpression(
1445 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1446 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1447 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1448
1449 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001450 RLoc = Tok.getLocation();
1451 if (!T.consumeClose())
1452 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001453
Alexey Bataev2af33e32016-04-07 12:45:37 +00001454 return Val;
1455}
1456
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001457/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001458/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001459/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001460///
Alexey Bataev3778b602014-07-17 07:32:53 +00001461/// final-clause:
1462/// 'final' '(' expression ')'
1463///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001464/// num_threads-clause:
1465/// 'num_threads' '(' expression ')'
1466///
1467/// safelen-clause:
1468/// 'safelen' '(' expression ')'
1469///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001470/// simdlen-clause:
1471/// 'simdlen' '(' expression ')'
1472///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001473/// collapse-clause:
1474/// 'collapse' '(' expression ')'
1475///
Alexey Bataeva0569352015-12-01 10:17:31 +00001476/// priority-clause:
1477/// 'priority' '(' expression ')'
1478///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001479/// grainsize-clause:
1480/// 'grainsize' '(' expression ')'
1481///
Alexey Bataev382967a2015-12-08 12:06:20 +00001482/// num_tasks-clause:
1483/// 'num_tasks' '(' expression ')'
1484///
Alexey Bataev28c75412015-12-15 08:19:24 +00001485/// hint-clause:
1486/// 'hint' '(' expression ')'
1487///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001488OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
1489 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001490 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001491 SourceLocation LLoc = Tok.getLocation();
1492 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001493
Alexey Bataev2af33e32016-04-07 12:45:37 +00001494 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001495
1496 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001497 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001498
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001499 if (ParseOnly)
1500 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00001501 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001502}
1503
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001504/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001505///
1506/// default-clause:
1507/// 'default' '(' 'none' | 'shared' ')
1508///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001509/// proc_bind-clause:
1510/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1511///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001512OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
1513 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001514 SourceLocation Loc = Tok.getLocation();
1515 SourceLocation LOpen = ConsumeToken();
1516 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001517 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001518 if (T.expectAndConsume(diag::err_expected_lparen_after,
1519 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001520 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001521
Alexey Bataeva55ed262014-05-28 06:15:33 +00001522 unsigned Type = getOpenMPSimpleClauseType(
1523 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001524 SourceLocation TypeLoc = Tok.getLocation();
1525 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1526 Tok.isNot(tok::annot_pragma_openmp_end))
1527 ConsumeAnyToken();
1528
1529 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001530 SourceLocation RLoc = Tok.getLocation();
1531 if (!T.consumeClose())
1532 RLoc = T.getCloseLocation();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001533
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001534 if (ParseOnly)
1535 return nullptr;
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001536 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001537}
1538
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001539/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001540///
1541/// ordered-clause:
1542/// 'ordered'
1543///
Alexey Bataev236070f2014-06-20 11:19:47 +00001544/// nowait-clause:
1545/// 'nowait'
1546///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001547/// untied-clause:
1548/// 'untied'
1549///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001550/// mergeable-clause:
1551/// 'mergeable'
1552///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001553/// read-clause:
1554/// 'read'
1555///
Alexey Bataev346265e2015-09-25 10:37:12 +00001556/// threads-clause:
1557/// 'threads'
1558///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001559/// simd-clause:
1560/// 'simd'
1561///
Alexey Bataevb825de12015-12-07 10:51:44 +00001562/// nogroup-clause:
1563/// 'nogroup'
1564///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001565OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001566 SourceLocation Loc = Tok.getLocation();
1567 ConsumeAnyToken();
1568
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001569 if (ParseOnly)
1570 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001571 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1572}
1573
1574
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001575/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00001576/// argument like 'schedule' or 'dist_schedule'.
1577///
1578/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001579/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1580/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001581///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001582/// if-clause:
1583/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1584///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001585/// defaultmap:
1586/// 'defaultmap' '(' modifier ':' kind ')'
1587///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001588OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
1589 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00001590 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001591 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001592 // Parse '('.
1593 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1594 if (T.expectAndConsume(diag::err_expected_lparen_after,
1595 getOpenMPClauseName(Kind)))
1596 return nullptr;
1597
1598 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001599 SmallVector<unsigned, 4> Arg;
1600 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001601 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001602 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1603 Arg.resize(NumberOfElements);
1604 KLoc.resize(NumberOfElements);
1605 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1606 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1607 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001608 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001609 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001610 if (KindModifier > OMPC_SCHEDULE_unknown) {
1611 // Parse 'modifier'
1612 Arg[Modifier1] = KindModifier;
1613 KLoc[Modifier1] = Tok.getLocation();
1614 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1615 Tok.isNot(tok::annot_pragma_openmp_end))
1616 ConsumeAnyToken();
1617 if (Tok.is(tok::comma)) {
1618 // Parse ',' 'modifier'
1619 ConsumeAnyToken();
1620 KindModifier = getOpenMPSimpleClauseType(
1621 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1622 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1623 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001624 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001625 KLoc[Modifier2] = Tok.getLocation();
1626 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1627 Tok.isNot(tok::annot_pragma_openmp_end))
1628 ConsumeAnyToken();
1629 }
1630 // Parse ':'
1631 if (Tok.is(tok::colon))
1632 ConsumeAnyToken();
1633 else
1634 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1635 KindModifier = getOpenMPSimpleClauseType(
1636 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1637 }
1638 Arg[ScheduleKind] = KindModifier;
1639 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001640 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1641 Tok.isNot(tok::annot_pragma_openmp_end))
1642 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001643 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1644 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1645 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001646 Tok.is(tok::comma))
1647 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001648 } else if (Kind == OMPC_dist_schedule) {
1649 Arg.push_back(getOpenMPSimpleClauseType(
1650 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1651 KLoc.push_back(Tok.getLocation());
1652 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1653 Tok.isNot(tok::annot_pragma_openmp_end))
1654 ConsumeAnyToken();
1655 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1656 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001657 } else if (Kind == OMPC_defaultmap) {
1658 // Get a defaultmap modifier
1659 Arg.push_back(getOpenMPSimpleClauseType(
1660 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1661 KLoc.push_back(Tok.getLocation());
1662 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1663 Tok.isNot(tok::annot_pragma_openmp_end))
1664 ConsumeAnyToken();
1665 // Parse ':'
1666 if (Tok.is(tok::colon))
1667 ConsumeAnyToken();
1668 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1669 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1670 // Get a defaultmap kind
1671 Arg.push_back(getOpenMPSimpleClauseType(
1672 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1673 KLoc.push_back(Tok.getLocation());
1674 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1675 Tok.isNot(tok::annot_pragma_openmp_end))
1676 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001677 } else {
1678 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001679 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001680 TentativeParsingAction TPA(*this);
Alexey Bataev61908f652018-04-23 19:53:05 +00001681 Arg.push_back(parseOpenMPDirectiveKind(*this));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001682 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001683 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001684 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1685 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001686 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001687 } else {
1688 TPA.Revert();
1689 Arg.back() = OMPD_unknown;
1690 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001691 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001692 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00001693 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001694 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001695
Carlo Bertollib4adf552016-01-15 18:50:31 +00001696 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1697 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1698 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001699 if (NeedAnExpression) {
1700 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001701 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1702 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001703 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001704 }
1705
1706 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001707 SourceLocation RLoc = Tok.getLocation();
1708 if (!T.consumeClose())
1709 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001710
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001711 if (NeedAnExpression && Val.isInvalid())
1712 return nullptr;
1713
Alexey Bataevf3c832a2018-01-09 19:21:04 +00001714 if (ParseOnly)
1715 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001716 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001717 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001718}
1719
Alexey Bataevc5e02582014-06-16 07:08:35 +00001720static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1721 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001722 if (ReductionIdScopeSpec.isEmpty()) {
1723 auto OOK = OO_None;
1724 switch (P.getCurToken().getKind()) {
1725 case tok::plus:
1726 OOK = OO_Plus;
1727 break;
1728 case tok::minus:
1729 OOK = OO_Minus;
1730 break;
1731 case tok::star:
1732 OOK = OO_Star;
1733 break;
1734 case tok::amp:
1735 OOK = OO_Amp;
1736 break;
1737 case tok::pipe:
1738 OOK = OO_Pipe;
1739 break;
1740 case tok::caret:
1741 OOK = OO_Caret;
1742 break;
1743 case tok::ampamp:
1744 OOK = OO_AmpAmp;
1745 break;
1746 case tok::pipepipe:
1747 OOK = OO_PipePipe;
1748 break;
1749 default:
1750 break;
1751 }
1752 if (OOK != OO_None) {
1753 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001754 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001755 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1756 return false;
1757 }
1758 }
1759 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1760 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00001761 /*AllowConstructorName*/ false,
1762 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00001763 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001764}
1765
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001766/// Parses clauses with list.
1767bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1768 OpenMPClauseKind Kind,
1769 SmallVectorImpl<Expr *> &Vars,
1770 OpenMPVarListDataTy &Data) {
1771 UnqualifiedId UnqualifiedReductionId;
1772 bool InvalidReductionId = false;
1773 bool MapTypeModifierSpecified = false;
1774
1775 // Parse '('.
1776 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1777 if (T.expectAndConsume(diag::err_expected_lparen_after,
1778 getOpenMPClauseName(Kind)))
1779 return true;
1780
1781 bool NeedRParenForLinear = false;
1782 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1783 tok::annot_pragma_openmp_end);
1784 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00001785 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
1786 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001787 ColonProtectionRAIIObject ColonRAII(*this);
1788 if (getLangOpts().CPlusPlus)
1789 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1790 /*ObjectType=*/nullptr,
1791 /*EnteringContext=*/false);
1792 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1793 UnqualifiedReductionId);
1794 if (InvalidReductionId) {
1795 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1796 StopBeforeMatch);
1797 }
1798 if (Tok.is(tok::colon))
1799 Data.ColonLoc = ConsumeToken();
1800 else
1801 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1802 if (!InvalidReductionId)
1803 Data.ReductionId =
1804 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1805 } else if (Kind == OMPC_depend) {
1806 // Handle dependency type for depend clause.
1807 ColonProtectionRAIIObject ColonRAII(*this);
1808 Data.DepKind =
1809 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1810 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1811 Data.DepLinMapLoc = Tok.getLocation();
1812
1813 if (Data.DepKind == OMPC_DEPEND_unknown) {
1814 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1815 StopBeforeMatch);
1816 } else {
1817 ConsumeToken();
1818 // Special processing for depend(source) clause.
1819 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1820 // Parse ')'.
1821 T.consumeClose();
1822 return false;
1823 }
1824 }
Alexey Bataev61908f652018-04-23 19:53:05 +00001825 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001826 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001827 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001828 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1829 : diag::warn_pragma_expected_colon)
1830 << "dependency type";
1831 }
1832 } else if (Kind == OMPC_linear) {
1833 // Try to parse modifier if any.
1834 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1835 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1836 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1837 Data.DepLinMapLoc = ConsumeToken();
1838 LinearT.consumeOpen();
1839 NeedRParenForLinear = true;
1840 }
1841 } else if (Kind == OMPC_map) {
1842 // Handle map type for map clause.
1843 ColonProtectionRAIIObject ColonRAII(*this);
1844
1845 /// The map clause modifier token can be either a identifier or the C++
1846 /// delete keyword.
1847 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1848 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1849 };
1850
1851 // The first identifier may be a list item, a map-type or a
1852 // map-type-modifier. The map modifier can also be delete which has the same
1853 // spelling of the C++ delete keyword.
1854 Data.MapType =
1855 IsMapClauseModifierToken(Tok)
1856 ? static_cast<OpenMPMapClauseKind>(
1857 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1858 : OMPC_MAP_unknown;
1859 Data.DepLinMapLoc = Tok.getLocation();
1860 bool ColonExpected = false;
1861
1862 if (IsMapClauseModifierToken(Tok)) {
1863 if (PP.LookAhead(0).is(tok::colon)) {
1864 if (Data.MapType == OMPC_MAP_unknown)
1865 Diag(Tok, diag::err_omp_unknown_map_type);
1866 else if (Data.MapType == OMPC_MAP_always)
1867 Diag(Tok, diag::err_omp_map_type_missing);
1868 ConsumeToken();
1869 } else if (PP.LookAhead(0).is(tok::comma)) {
1870 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1871 PP.LookAhead(2).is(tok::colon)) {
1872 Data.MapTypeModifier = Data.MapType;
1873 if (Data.MapTypeModifier != OMPC_MAP_always) {
1874 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1875 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001876 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001877 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001878 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001879
1880 ConsumeToken();
1881 ConsumeToken();
1882
1883 Data.MapType =
1884 IsMapClauseModifierToken(Tok)
1885 ? static_cast<OpenMPMapClauseKind>(
1886 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1887 : OMPC_MAP_unknown;
1888 if (Data.MapType == OMPC_MAP_unknown ||
1889 Data.MapType == OMPC_MAP_always)
1890 Diag(Tok, diag::err_omp_unknown_map_type);
1891 ConsumeToken();
1892 } else {
1893 Data.MapType = OMPC_MAP_tofrom;
1894 Data.IsMapTypeImplicit = true;
1895 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001896 } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
1897 if (PP.LookAhead(1).is(tok::colon)) {
1898 Data.MapTypeModifier = Data.MapType;
1899 if (Data.MapTypeModifier != OMPC_MAP_always) {
1900 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1901 Data.MapTypeModifier = OMPC_MAP_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00001902 } else {
Carlo Bertollid8844b92017-05-03 15:28:48 +00001903 MapTypeModifierSpecified = true;
Alexey Bataev61908f652018-04-23 19:53:05 +00001904 }
Carlo Bertollid8844b92017-05-03 15:28:48 +00001905
1906 ConsumeToken();
1907
1908 Data.MapType =
1909 IsMapClauseModifierToken(Tok)
1910 ? static_cast<OpenMPMapClauseKind>(
1911 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1912 : OMPC_MAP_unknown;
1913 if (Data.MapType == OMPC_MAP_unknown ||
1914 Data.MapType == OMPC_MAP_always)
1915 Diag(Tok, diag::err_omp_unknown_map_type);
1916 ConsumeToken();
1917 } else {
1918 Data.MapType = OMPC_MAP_tofrom;
1919 Data.IsMapTypeImplicit = true;
1920 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001921 } else {
1922 Data.MapType = OMPC_MAP_tofrom;
1923 Data.IsMapTypeImplicit = true;
1924 }
1925 } else {
1926 Data.MapType = OMPC_MAP_tofrom;
1927 Data.IsMapTypeImplicit = true;
1928 }
1929
1930 if (Tok.is(tok::colon))
1931 Data.ColonLoc = ConsumeToken();
1932 else if (ColonExpected)
1933 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1934 }
1935
Alexey Bataevfa312f32017-07-21 18:48:21 +00001936 bool IsComma =
1937 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
1938 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1939 (Kind == OMPC_reduction && !InvalidReductionId) ||
1940 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1941 (!MapTypeModifierSpecified ||
1942 Data.MapTypeModifier == OMPC_MAP_always)) ||
1943 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001944 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1945 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1946 Tok.isNot(tok::annot_pragma_openmp_end))) {
1947 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1948 // Parse variable
1949 ExprResult VarExpr =
1950 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00001951 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001952 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00001953 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001954 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1955 StopBeforeMatch);
1956 }
1957 // Skip ',' if any
1958 IsComma = Tok.is(tok::comma);
1959 if (IsComma)
1960 ConsumeToken();
1961 else if (Tok.isNot(tok::r_paren) &&
1962 Tok.isNot(tok::annot_pragma_openmp_end) &&
1963 (!MayHaveTail || Tok.isNot(tok::colon)))
1964 Diag(Tok, diag::err_omp_expected_punc)
1965 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1966 : getOpenMPClauseName(Kind))
1967 << (Kind == OMPC_flush);
1968 }
1969
1970 // Parse ')' for linear clause with modifier.
1971 if (NeedRParenForLinear)
1972 LinearT.consumeClose();
1973
1974 // Parse ':' linear-step (or ':' alignment).
1975 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1976 if (MustHaveTail) {
1977 Data.ColonLoc = Tok.getLocation();
1978 SourceLocation ELoc = ConsumeToken();
1979 ExprResult Tail = ParseAssignmentExpression();
1980 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1981 if (Tail.isUsable())
1982 Data.TailExpr = Tail.get();
1983 else
1984 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1985 StopBeforeMatch);
1986 }
1987
1988 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00001989 Data.RLoc = Tok.getLocation();
1990 if (!T.consumeClose())
1991 Data.RLoc = T.getCloseLocation();
Alexey Bataev61908f652018-04-23 19:53:05 +00001992 return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1993 Vars.empty()) ||
1994 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1995 (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001996}
1997
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001998/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00001999/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2000/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002001///
2002/// private-clause:
2003/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002004/// firstprivate-clause:
2005/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002006/// lastprivate-clause:
2007/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002008/// shared-clause:
2009/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002010/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002011/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002012/// aligned-clause:
2013/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002014/// reduction-clause:
2015/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002016/// task_reduction-clause:
2017/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002018/// in_reduction-clause:
2019/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002020/// copyprivate-clause:
2021/// 'copyprivate' '(' list ')'
2022/// flush-clause:
2023/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002024/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002025/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002026/// map-clause:
2027/// 'map' '(' [ [ always , ]
2028/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002029/// to-clause:
2030/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002031/// from-clause:
2032/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002033/// use_device_ptr-clause:
2034/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002035/// is_device_ptr-clause:
2036/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002037///
Alexey Bataev182227b2015-08-20 10:54:39 +00002038/// For 'linear' clause linear-list may have the following forms:
2039/// list
2040/// modifier(list)
2041/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002042OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002043 OpenMPClauseKind Kind,
2044 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002045 SourceLocation Loc = Tok.getLocation();
2046 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002047 SmallVector<Expr *, 4> Vars;
2048 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002049
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002050 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002051 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002052
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002053 if (ParseOnly)
2054 return nullptr;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002055 return Actions.ActOnOpenMPVarListClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002056 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Data.RLoc,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002057 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
2058 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
2059 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002060}
2061