blob: f9ea8af00f50a5a0a64a3c7f4124802097996cc7 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000018#include "clang/Parse/Parser.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000021
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Dmitry Polukhin82478332016-02-13 06:53:38 +000028namespace {
29enum OpenMPDirectiveKindEx {
30 OMPD_cancellation = OMPD_unknown + 1,
31 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000032 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000033 OMPD_end,
34 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000035 OMPD_enter,
36 OMPD_exit,
37 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000038 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000039 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000040 OMPD_target_exit,
41 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000042 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000043 OMPD_teams_distribute_parallel,
44 OMPD_target_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000045};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000046
47class ThreadprivateListParserHelper final {
48 SmallVector<Expr *, 4> Identifiers;
49 Parser *P;
50
51public:
52 ThreadprivateListParserHelper(Parser *P) : P(P) {}
53 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
54 ExprResult Res =
55 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
56 if (Res.isUsable())
57 Identifiers.push_back(Res.get());
58 }
59 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
60};
Dmitry Polukhin82478332016-02-13 06:53:38 +000061} // namespace
62
63// Map token string to extended OMP token kind that are
64// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
65static unsigned getOpenMPDirectiveKindEx(StringRef S) {
66 auto DKind = getOpenMPDirectiveKind(S);
67 if (DKind != OMPD_unknown)
68 return DKind;
69
70 return llvm::StringSwitch<unsigned>(S)
71 .Case("cancellation", OMPD_cancellation)
72 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000073 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000074 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 .Case("enter", OMPD_enter)
76 .Case("exit", OMPD_exit)
77 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000078 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000079 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000080 .Default(OMPD_unknown);
81}
82
Alexey Bataev4acb8592014-07-07 13:01:15 +000083static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000084 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
85 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
86 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 static const unsigned F[][3] = {
88 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000089 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000090 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000091 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000092 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
93 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000094 { OMPD_distribute_parallel_for, OMPD_simd,
95 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000096 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000097 { OMPD_end, OMPD_declare, OMPD_end_declare },
98 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000099 { OMPD_target, OMPD_data, OMPD_target_data },
100 { OMPD_target, OMPD_enter, OMPD_target_enter },
101 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000102 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000103 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
104 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
105 { OMPD_for, OMPD_simd, OMPD_for_simd },
106 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
107 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
108 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
109 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
110 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000111 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000112 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000113 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000114 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
Kelvin Li579e41c2016-11-30 23:51:03 +0000115 { OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd },
116 { OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel },
117 { OMPD_teams_distribute_parallel, OMPD_for, OMPD_teams_distribute_parallel_for },
Kelvin Libf594a52016-12-17 05:48:59 +0000118 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd },
Kelvin Li83c451e2016-12-25 04:52:54 +0000119 { OMPD_target, OMPD_teams, OMPD_target_teams },
Kelvin Li80e8f562016-12-29 22:16:30 +0000120 { OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute },
121 { OMPD_target_teams_distribute, OMPD_parallel, OMPD_target_teams_distribute_parallel },
Kelvin Li1851df52017-01-03 05:23:48 +0000122 { OMPD_target_teams_distribute_parallel, OMPD_for, OMPD_target_teams_distribute_parallel_for },
123 { OMPD_target_teams_distribute_parallel_for, OMPD_simd, OMPD_target_teams_distribute_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000124 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000125 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000126 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000127 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000128 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000129 ? static_cast<unsigned>(OMPD_unknown)
130 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
131 if (DKind == OMPD_unknown)
132 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000133
Alexander Musmanf82886e2014-09-18 05:12:34 +0000134 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000135 if (DKind != F[i][0])
136 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000137
Dmitry Polukhin82478332016-02-13 06:53:38 +0000138 Tok = P.getPreprocessor().LookAhead(0);
139 unsigned SDKind =
140 Tok.isAnnotation()
141 ? static_cast<unsigned>(OMPD_unknown)
142 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
143 if (SDKind == OMPD_unknown)
144 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000145
Dmitry Polukhin82478332016-02-13 06:53:38 +0000146 if (SDKind == F[i][1]) {
147 P.ConsumeToken();
148 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000149 }
150 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000151 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
152 : OMPD_unknown;
153}
154
155static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000156 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000157 Sema &Actions = P.getActions();
158 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000159 // Allow to use 'operator' keyword for C++ operators
160 bool WithOperator = false;
161 if (Tok.is(tok::kw_operator)) {
162 P.ConsumeToken();
163 Tok = P.getCurToken();
164 WithOperator = true;
165 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000166 switch (Tok.getKind()) {
167 case tok::plus: // '+'
168 OOK = OO_Plus;
169 break;
170 case tok::minus: // '-'
171 OOK = OO_Minus;
172 break;
173 case tok::star: // '*'
174 OOK = OO_Star;
175 break;
176 case tok::amp: // '&'
177 OOK = OO_Amp;
178 break;
179 case tok::pipe: // '|'
180 OOK = OO_Pipe;
181 break;
182 case tok::caret: // '^'
183 OOK = OO_Caret;
184 break;
185 case tok::ampamp: // '&&'
186 OOK = OO_AmpAmp;
187 break;
188 case tok::pipepipe: // '||'
189 OOK = OO_PipePipe;
190 break;
191 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000192 if (!WithOperator)
193 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000194 default:
195 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
196 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
197 Parser::StopBeforeMatch);
198 return DeclarationName();
199 }
200 P.ConsumeToken();
201 auto &DeclNames = Actions.getASTContext().DeclarationNames;
202 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
203 : DeclNames.getCXXOperatorName(OOK);
204}
205
206/// \brief Parse 'omp declare reduction' construct.
207///
208/// declare-reduction-directive:
209/// annot_pragma_openmp 'declare' 'reduction'
210/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
211/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
212/// annot_pragma_openmp_end
213/// <reduction_id> is either a base language identifier or one of the following
214/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
215///
216Parser::DeclGroupPtrTy
217Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
218 // Parse '('.
219 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
220 if (T.expectAndConsume(diag::err_expected_lparen_after,
221 getOpenMPDirectiveName(OMPD_declare_reduction))) {
222 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
223 return DeclGroupPtrTy();
224 }
225
226 DeclarationName Name = parseOpenMPReductionId(*this);
227 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
228 return DeclGroupPtrTy();
229
230 // Consume ':'.
231 bool IsCorrect = !ExpectAndConsume(tok::colon);
232
233 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
234 return DeclGroupPtrTy();
235
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000236 IsCorrect = IsCorrect && !Name.isEmpty();
237
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000238 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
239 Diag(Tok.getLocation(), diag::err_expected_type);
240 IsCorrect = false;
241 }
242
243 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
244 return DeclGroupPtrTy();
245
246 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
247 // Parse list of types until ':' token.
248 do {
249 ColonProtectionRAIIObject ColonRAII(*this);
250 SourceRange Range;
251 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
252 if (TR.isUsable()) {
253 auto ReductionType =
254 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
255 if (!ReductionType.isNull()) {
256 ReductionTypes.push_back(
257 std::make_pair(ReductionType, Range.getBegin()));
258 }
259 } else {
260 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
261 StopBeforeMatch);
262 }
263
264 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
265 break;
266
267 // Consume ','.
268 if (ExpectAndConsume(tok::comma)) {
269 IsCorrect = false;
270 if (Tok.is(tok::annot_pragma_openmp_end)) {
271 Diag(Tok.getLocation(), diag::err_expected_type);
272 return DeclGroupPtrTy();
273 }
274 }
275 } while (Tok.isNot(tok::annot_pragma_openmp_end));
276
277 if (ReductionTypes.empty()) {
278 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
279 return DeclGroupPtrTy();
280 }
281
282 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
283 return DeclGroupPtrTy();
284
285 // Consume ':'.
286 if (ExpectAndConsume(tok::colon))
287 IsCorrect = false;
288
289 if (Tok.is(tok::annot_pragma_openmp_end)) {
290 Diag(Tok.getLocation(), diag::err_expected_expression);
291 return DeclGroupPtrTy();
292 }
293
294 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
295 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
296
297 // Parse <combiner> expression and then parse initializer if any for each
298 // correct type.
299 unsigned I = 0, E = ReductionTypes.size();
300 for (auto *D : DRD.get()) {
301 TentativeParsingAction TPA(*this);
302 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
303 Scope::OpenMPDirectiveScope);
304 // Parse <combiner> expression.
305 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
306 ExprResult CombinerResult =
307 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
308 D->getLocation(), /*DiscardedValue=*/true);
309 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
310
311 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
312 Tok.isNot(tok::annot_pragma_openmp_end)) {
313 TPA.Commit();
314 IsCorrect = false;
315 break;
316 }
317 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
318 ExprResult InitializerResult;
319 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
320 // Parse <initializer> expression.
321 if (Tok.is(tok::identifier) &&
322 Tok.getIdentifierInfo()->isStr("initializer"))
323 ConsumeToken();
324 else {
325 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
326 TPA.Commit();
327 IsCorrect = false;
328 break;
329 }
330 // Parse '('.
331 BalancedDelimiterTracker T(*this, tok::l_paren,
332 tok::annot_pragma_openmp_end);
333 IsCorrect =
334 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
335 IsCorrect;
336 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
337 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
338 Scope::OpenMPDirectiveScope);
339 // Parse expression.
340 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
341 InitializerResult = Actions.ActOnFinishFullExpr(
342 ParseAssignmentExpression().get(), D->getLocation(),
343 /*DiscardedValue=*/true);
344 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
345 D, InitializerResult.get());
346 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
347 Tok.isNot(tok::annot_pragma_openmp_end)) {
348 TPA.Commit();
349 IsCorrect = false;
350 break;
351 }
352 IsCorrect =
353 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
354 }
355 }
356
357 ++I;
358 // Revert parsing if not the last type, otherwise accept it, we're done with
359 // parsing.
360 if (I != E)
361 TPA.Revert();
362 else
363 TPA.Commit();
364 }
365 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
366 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000367}
368
Alexey Bataev2af33e32016-04-07 12:45:37 +0000369namespace {
370/// RAII that recreates function context for correct parsing of clauses of
371/// 'declare simd' construct.
372/// OpenMP, 2.8.2 declare simd Construct
373/// The expressions appearing in the clauses of this directive are evaluated in
374/// the scope of the arguments of the function declaration or definition.
375class FNContextRAII final {
376 Parser &P;
377 Sema::CXXThisScopeRAII *ThisScope;
378 Parser::ParseScope *TempScope;
379 Parser::ParseScope *FnScope;
380 bool HasTemplateScope = false;
381 bool HasFunScope = false;
382 FNContextRAII() = delete;
383 FNContextRAII(const FNContextRAII &) = delete;
384 FNContextRAII &operator=(const FNContextRAII &) = delete;
385
386public:
387 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
388 Decl *D = *Ptr.get().begin();
389 NamedDecl *ND = dyn_cast<NamedDecl>(D);
390 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
391 Sema &Actions = P.getActions();
392
393 // Allow 'this' within late-parsed attributes.
394 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
395 ND && ND->isCXXInstanceMember());
396
397 // If the Decl is templatized, add template parameters to scope.
398 HasTemplateScope = D->isTemplateDecl();
399 TempScope =
400 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
401 if (HasTemplateScope)
402 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
403
404 // If the Decl is on a function, add function parameters to the scope.
405 HasFunScope = D->isFunctionOrFunctionTemplate();
406 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
407 HasFunScope);
408 if (HasFunScope)
409 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
410 }
411 ~FNContextRAII() {
412 if (HasFunScope) {
413 P.getActions().ActOnExitFunctionContext();
414 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
415 }
416 if (HasTemplateScope)
417 TempScope->Exit();
418 delete FnScope;
419 delete TempScope;
420 delete ThisScope;
421 }
422};
423} // namespace
424
Alexey Bataevd93d3762016-04-12 09:35:56 +0000425/// Parses clauses for 'declare simd' directive.
426/// clause:
427/// 'inbranch' | 'notinbranch'
428/// 'simdlen' '(' <expr> ')'
429/// { 'uniform' '(' <argument_list> ')' }
430/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000431/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
432static bool parseDeclareSimdClauses(
433 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
434 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
435 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
436 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000437 SourceRange BSRange;
438 const Token &Tok = P.getCurToken();
439 bool IsError = false;
440 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
441 if (Tok.isNot(tok::identifier))
442 break;
443 OMPDeclareSimdDeclAttr::BranchStateTy Out;
444 IdentifierInfo *II = Tok.getIdentifierInfo();
445 StringRef ClauseName = II->getName();
446 // Parse 'inranch|notinbranch' clauses.
447 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
448 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
449 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
450 << ClauseName
451 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
452 IsError = true;
453 }
454 BS = Out;
455 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
456 P.ConsumeToken();
457 } else if (ClauseName.equals("simdlen")) {
458 if (SimdLen.isUsable()) {
459 P.Diag(Tok, diag::err_omp_more_one_clause)
460 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
461 IsError = true;
462 }
463 P.ConsumeToken();
464 SourceLocation RLoc;
465 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
466 if (SimdLen.isInvalid())
467 IsError = true;
468 } else {
469 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000470 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
471 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000472 Parser::OpenMPVarListDataTy Data;
473 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000474 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000475 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000476 else if (CKind == OMPC_linear)
477 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000478
479 P.ConsumeToken();
480 if (P.ParseOpenMPVarList(OMPD_declare_simd,
481 getOpenMPClauseKind(ClauseName), *Vars, Data))
482 IsError = true;
483 if (CKind == OMPC_aligned)
484 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000485 else if (CKind == OMPC_linear) {
486 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
487 Data.DepLinMapLoc))
488 Data.LinKind = OMPC_LINEAR_val;
489 LinModifiers.append(Linears.size() - LinModifiers.size(),
490 Data.LinKind);
491 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
492 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000493 } else
494 // TODO: add parsing of other clauses.
495 break;
496 }
497 // Skip ',' if any.
498 if (Tok.is(tok::comma))
499 P.ConsumeToken();
500 }
501 return IsError;
502}
503
Alexey Bataev2af33e32016-04-07 12:45:37 +0000504/// Parse clauses for '#pragma omp declare simd'.
505Parser::DeclGroupPtrTy
506Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
507 CachedTokens &Toks, SourceLocation Loc) {
508 PP.EnterToken(Tok);
509 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
510 // Consume the previously pushed token.
511 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
512
513 FNContextRAII FnContext(*this, Ptr);
514 OMPDeclareSimdDeclAttr::BranchStateTy BS =
515 OMPDeclareSimdDeclAttr::BS_Undefined;
516 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000517 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000518 SmallVector<Expr *, 4> Aligneds;
519 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000520 SmallVector<Expr *, 4> Linears;
521 SmallVector<unsigned, 4> LinModifiers;
522 SmallVector<Expr *, 4> Steps;
523 bool IsError =
524 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
525 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000526 // Need to check for extra tokens.
527 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
528 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
529 << getOpenMPDirectiveName(OMPD_declare_simd);
530 while (Tok.isNot(tok::annot_pragma_openmp_end))
531 ConsumeAnyToken();
532 }
533 // Skip the last annot_pragma_openmp_end.
534 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000535 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000536 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000537 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
538 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000539 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000540 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000541}
542
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000543/// \brief Parsing of declarative OpenMP directives.
544///
545/// threadprivate-directive:
546/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000547/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000548///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000549/// declare-reduction-directive:
550/// annot_pragma_openmp 'declare' 'reduction' [...]
551/// annot_pragma_openmp_end
552///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000553/// declare-simd-directive:
554/// annot_pragma_openmp 'declare simd' {<clause> [,]}
555/// annot_pragma_openmp_end
556/// <function declaration/definition>
557///
558Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
559 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
560 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000561 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000562 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000563
564 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000565 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000566
567 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000568 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000569 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000570 ThreadprivateListParserHelper Helper(this);
571 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000572 // The last seen token is annot_pragma_openmp_end - need to check for
573 // extra tokens.
574 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
575 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000576 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000577 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000578 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000579 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000580 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000581 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
582 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000583 }
584 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000585 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000586 case OMPD_declare_reduction:
587 ConsumeToken();
588 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
589 // The last seen token is annot_pragma_openmp_end - need to check for
590 // extra tokens.
591 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
592 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
593 << getOpenMPDirectiveName(OMPD_declare_reduction);
594 while (Tok.isNot(tok::annot_pragma_openmp_end))
595 ConsumeAnyToken();
596 }
597 // Skip the last annot_pragma_openmp_end.
598 ConsumeToken();
599 return Res;
600 }
601 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000602 case OMPD_declare_simd: {
603 // The syntax is:
604 // { #pragma omp declare simd }
605 // <function-declaration-or-definition>
606 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000607 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000608 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000609 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
610 Toks.push_back(Tok);
611 ConsumeAnyToken();
612 }
613 Toks.push_back(Tok);
614 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000615
616 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000617 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000618 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000619 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000620 // Here we expect to see some function declaration.
621 if (AS == AS_none) {
622 assert(TagType == DeclSpec::TST_unspecified);
623 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000624 ParsingDeclSpec PDS(*this);
625 Ptr = ParseExternalDeclaration(Attrs, &PDS);
626 } else {
627 Ptr =
628 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
629 }
630 }
631 if (!Ptr) {
632 Diag(Loc, diag::err_omp_decl_in_declare_simd);
633 return DeclGroupPtrTy();
634 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000635 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000636 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000637 case OMPD_declare_target: {
638 SourceLocation DTLoc = ConsumeAnyToken();
639 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000640 // OpenMP 4.5 syntax with list of entities.
641 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
642 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
643 OMPDeclareTargetDeclAttr::MapTypeTy MT =
644 OMPDeclareTargetDeclAttr::MT_To;
645 if (Tok.is(tok::identifier)) {
646 IdentifierInfo *II = Tok.getIdentifierInfo();
647 StringRef ClauseName = II->getName();
648 // Parse 'to|link' clauses.
649 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
650 MT)) {
651 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
652 << ClauseName;
653 break;
654 }
655 ConsumeToken();
656 }
657 auto Callback = [this, MT, &SameDirectiveDecls](
658 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
659 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
660 SameDirectiveDecls);
661 };
662 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
663 break;
664
665 // Consume optional ','.
666 if (Tok.is(tok::comma))
667 ConsumeToken();
668 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000669 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000670 ConsumeAnyToken();
671 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000672 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000673
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000674 // Skip the last annot_pragma_openmp_end.
675 ConsumeAnyToken();
676
677 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
678 return DeclGroupPtrTy();
679
680 DKind = ParseOpenMPDirectiveKind(*this);
681 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
682 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
683 ParsedAttributesWithRange attrs(AttrFactory);
684 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000685 ParseExternalDeclaration(attrs);
686 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
687 TentativeParsingAction TPA(*this);
688 ConsumeToken();
689 DKind = ParseOpenMPDirectiveKind(*this);
690 if (DKind != OMPD_end_declare_target)
691 TPA.Revert();
692 else
693 TPA.Commit();
694 }
695 }
696
697 if (DKind == OMPD_end_declare_target) {
698 ConsumeAnyToken();
699 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
700 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
701 << getOpenMPDirectiveName(OMPD_end_declare_target);
702 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
703 }
704 // Skip the last annot_pragma_openmp_end.
705 ConsumeAnyToken();
706 } else {
707 Diag(Tok, diag::err_expected_end_declare_target);
708 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
709 }
710 Actions.ActOnFinishOpenMPDeclareTargetDirective();
711 return DeclGroupPtrTy();
712 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000713 case OMPD_unknown:
714 Diag(Tok, diag::err_omp_unknown_directive);
715 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000716 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000717 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000718 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000719 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000720 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000721 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000722 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000723 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000724 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000725 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000726 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000727 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000728 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000729 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000730 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000731 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000732 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000733 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000734 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000735 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000736 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000737 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000738 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000739 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000740 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000741 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000742 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000743 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000744 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000745 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000746 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000747 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000748 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000749 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000750 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000751 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000752 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000753 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000754 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000755 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000756 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000757 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000758 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000759 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000760 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000761 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000762 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000763 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000764 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000765 break;
766 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000767 while (Tok.isNot(tok::annot_pragma_openmp_end))
768 ConsumeAnyToken();
769 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000770 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000771}
772
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000773/// \brief Parsing of declarative or executable OpenMP directives.
774///
775/// threadprivate-directive:
776/// annot_pragma_openmp 'threadprivate' simple-variable-list
777/// annot_pragma_openmp_end
778///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000779/// declare-reduction-directive:
780/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
781/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
782/// ('omp_priv' '=' <expression>|<function_call>) ')']
783/// annot_pragma_openmp_end
784///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000785/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000786/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000787/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
788/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000789/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000790/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000791/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000792/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000793/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000794/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000795/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000796/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000797/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000798/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000799/// 'teams distribute parallel for' | 'target teams' |
800/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000801/// 'target teams distribute parallel for' |
802/// 'target teams distribute parallel for simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000803/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000804///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000805StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
806 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000807 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000808 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000809 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000810 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000811 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000812 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000813 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000814 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000815 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000816 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000817 // Name of critical directive.
818 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000819 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000820 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000821 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000822
823 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000824 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000825 if (Allowed != ACK_Any) {
826 Diag(Tok, diag::err_omp_immediate_directive)
827 << getOpenMPDirectiveName(DKind) << 0;
828 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000829 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000830 ThreadprivateListParserHelper Helper(this);
831 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000832 // The last seen token is annot_pragma_openmp_end - need to check for
833 // extra tokens.
834 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
835 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000836 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000837 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000838 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000839 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
840 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000841 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
842 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000843 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000844 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000845 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000846 case OMPD_declare_reduction:
847 ConsumeToken();
848 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
849 // The last seen token is annot_pragma_openmp_end - need to check for
850 // extra tokens.
851 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
852 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
853 << getOpenMPDirectiveName(OMPD_declare_reduction);
854 while (Tok.isNot(tok::annot_pragma_openmp_end))
855 ConsumeAnyToken();
856 }
857 ConsumeAnyToken();
858 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
859 } else
860 SkipUntil(tok::annot_pragma_openmp_end);
861 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000862 case OMPD_flush:
863 if (PP.LookAhead(0).is(tok::l_paren)) {
864 FlushHasClause = true;
865 // Push copy of the current token back to stream to properly parse
866 // pseudo-clause OMPFlushClause.
867 PP.EnterToken(Tok);
868 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000869 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000870 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000871 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000872 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000873 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000874 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000875 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000876 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000877 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000878 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000879 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000880 }
881 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000882 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000883 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000884 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000885 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000886 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000887 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000888 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000889 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000890 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000891 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000892 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000893 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000894 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000895 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000896 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000897 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000898 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000899 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000900 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000901 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000902 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000903 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000904 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000905 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000906 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000907 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000908 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000909 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000910 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000911 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000912 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000913 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000914 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +0000915 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +0000916 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +0000917 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +0000918 case OMPD_target_teams_distribute_parallel_for:
919 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000921 // Parse directive name of the 'critical' directive if any.
922 if (DKind == OMPD_critical) {
923 BalancedDelimiterTracker T(*this, tok::l_paren,
924 tok::annot_pragma_openmp_end);
925 if (!T.consumeOpen()) {
926 if (Tok.isAnyIdentifier()) {
927 DirName =
928 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
929 ConsumeAnyToken();
930 } else {
931 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
932 }
933 T.consumeClose();
934 }
Alexey Bataev80909872015-07-02 11:25:17 +0000935 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000936 CancelRegion = ParseOpenMPDirectiveKind(*this);
937 if (Tok.isNot(tok::annot_pragma_openmp_end))
938 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000939 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000940
Alexey Bataevf29276e2014-06-18 04:14:57 +0000941 if (isOpenMPLoopDirective(DKind))
942 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
943 if (isOpenMPSimdDirective(DKind))
944 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
945 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000946 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000947
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000948 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000949 OpenMPClauseKind CKind =
950 Tok.isAnnotation()
951 ? OMPC_unknown
952 : FlushHasClause ? OMPC_flush
953 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000954 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000955 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000956 OMPClause *Clause =
957 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000958 FirstClauses[CKind].setInt(true);
959 if (Clause) {
960 FirstClauses[CKind].setPointer(Clause);
961 Clauses.push_back(Clause);
962 }
963
964 // Skip ',' if any.
965 if (Tok.is(tok::comma))
966 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000967 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000968 }
969 // End location of the directive.
970 EndLoc = Tok.getLocation();
971 // Consume final annot_pragma_openmp_end.
972 ConsumeToken();
973
Alexey Bataeveb482352015-12-18 05:05:56 +0000974 // OpenMP [2.13.8, ordered Construct, Syntax]
975 // If the depend clause is specified, the ordered construct is a stand-alone
976 // directive.
977 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000978 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000979 Diag(Loc, diag::err_omp_immediate_directive)
980 << getOpenMPDirectiveName(DKind) << 1
981 << getOpenMPClauseName(OMPC_depend);
982 }
983 HasAssociatedStatement = false;
984 }
985
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000987 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000988 // The body is a block scope like in Lambdas and Blocks.
989 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000990 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000991 Actions.ActOnStartOfCompoundStmt();
992 // Parse statement
993 AssociatedStmt = ParseStatement();
994 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000995 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000996 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000997 Directive = Actions.ActOnOpenMPExecutableDirective(
998 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
999 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000
1001 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001002 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001003 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001004 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001005 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001006 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001007 case OMPD_declare_target:
1008 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001009 Diag(Tok, diag::err_omp_unexpected_directive)
1010 << getOpenMPDirectiveName(DKind);
1011 SkipUntil(tok::annot_pragma_openmp_end);
1012 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001013 case OMPD_unknown:
1014 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001015 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001016 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017 }
1018 return Directive;
1019}
1020
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001021// Parses simple list:
1022// simple-variable-list:
1023// '(' id-expression {, id-expression} ')'
1024//
1025bool Parser::ParseOpenMPSimpleVarList(
1026 OpenMPDirectiveKind Kind,
1027 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1028 Callback,
1029 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001030 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001031 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001032 if (T.expectAndConsume(diag::err_expected_lparen_after,
1033 getOpenMPDirectiveName(Kind)))
1034 return true;
1035 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001037
1038 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001040 CXXScopeSpec SS;
1041 SourceLocation TemplateKWLoc;
1042 UnqualifiedId Name;
1043 // Read var name.
1044 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001045 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001046
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001047 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001048 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001049 IsCorrect = false;
1050 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001051 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001052 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001054 IsCorrect = false;
1055 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001056 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1058 Tok.isNot(tok::annot_pragma_openmp_end)) {
1059 IsCorrect = false;
1060 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001061 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001062 Diag(PrevTok.getLocation(), diag::err_expected)
1063 << tok::identifier
1064 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001065 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001066 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001067 }
1068 // Consume ','.
1069 if (Tok.is(tok::comma)) {
1070 ConsumeToken();
1071 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001072 }
1073
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001074 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001075 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001076 IsCorrect = false;
1077 }
1078
1079 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001082 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001083}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001084
1085/// \brief Parsing of OpenMP clauses.
1086///
1087/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001088/// if-clause | final-clause | num_threads-clause | safelen-clause |
1089/// default-clause | private-clause | firstprivate-clause | shared-clause
1090/// | linear-clause | aligned-clause | collapse-clause |
1091/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001092/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001093/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001094/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001095/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001096/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001097/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001098/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001099///
1100OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1101 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001102 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001103 bool ErrorFound = false;
1104 // Check if clause is allowed for the given directive.
1105 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001106 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1107 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001108 ErrorFound = true;
1109 }
1110
1111 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001112 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001113 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001114 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001115 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001116 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001117 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001118 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001119 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001120 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001121 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001122 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001123 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001124 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001125 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001126 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001127 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001128 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001129 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001130 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001131 // OpenMP [2.9.1, target data construct, Restrictions]
1132 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001133 // OpenMP [2.11.1, task Construct, Restrictions]
1134 // At most one if clause can appear on the directive.
1135 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001136 // OpenMP [teams Construct, Restrictions]
1137 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001138 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001139 // OpenMP [2.9.1, task Construct, Restrictions]
1140 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001141 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1142 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001143 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1144 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001145 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001146 Diag(Tok, diag::err_omp_more_one_clause)
1147 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001148 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001149 }
1150
Alexey Bataev10e775f2015-07-30 11:36:16 +00001151 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1152 Clause = ParseOpenMPClause(CKind);
1153 else
1154 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001155 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001156 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001157 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001158 // OpenMP [2.14.3.1, Restrictions]
1159 // Only a single default clause may be specified on a parallel, task or
1160 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001161 // OpenMP [2.5, parallel Construct, Restrictions]
1162 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001163 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001164 Diag(Tok, diag::err_omp_more_one_clause)
1165 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001166 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001167 }
1168
1169 Clause = ParseOpenMPSimpleClause(CKind);
1170 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001171 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001172 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001173 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001174 // OpenMP [2.7.1, Restrictions, p. 3]
1175 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001176 // OpenMP [2.10.4, Restrictions, p. 106]
1177 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001178 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001179 Diag(Tok, diag::err_omp_more_one_clause)
1180 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001181 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001182 }
1183
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001184 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001185 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1186 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001187 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001188 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001189 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001190 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001191 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001192 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001193 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001194 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001195 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001196 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001197 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001198 // OpenMP [2.7.1, Restrictions, p. 9]
1199 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001200 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1201 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001202 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001203 Diag(Tok, diag::err_omp_more_one_clause)
1204 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001205 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001206 }
1207
1208 Clause = ParseOpenMPClause(CKind);
1209 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001210 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001211 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001212 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001213 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001214 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001215 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001216 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001217 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001218 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001219 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001220 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001221 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001222 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001223 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001224 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001225 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001226 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001227 break;
1228 case OMPC_unknown:
1229 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001230 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001231 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001232 break;
1233 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001234 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001235 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1236 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001237 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001238 break;
1239 }
Craig Topper161e4db2014-05-21 06:02:52 +00001240 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001241}
1242
Alexey Bataev2af33e32016-04-07 12:45:37 +00001243/// Parses simple expression in parens for single-expression clauses of OpenMP
1244/// constructs.
1245/// \param RLoc Returned location of right paren.
1246ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1247 SourceLocation &RLoc) {
1248 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1249 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1250 return ExprError();
1251
1252 SourceLocation ELoc = Tok.getLocation();
1253 ExprResult LHS(ParseCastExpression(
1254 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1255 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1256 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1257
1258 // Parse ')'.
1259 T.consumeClose();
1260
1261 RLoc = T.getCloseLocation();
1262 return Val;
1263}
1264
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001265/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001266/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001267/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001268///
Alexey Bataev3778b602014-07-17 07:32:53 +00001269/// final-clause:
1270/// 'final' '(' expression ')'
1271///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001272/// num_threads-clause:
1273/// 'num_threads' '(' expression ')'
1274///
1275/// safelen-clause:
1276/// 'safelen' '(' expression ')'
1277///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001278/// simdlen-clause:
1279/// 'simdlen' '(' expression ')'
1280///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001281/// collapse-clause:
1282/// 'collapse' '(' expression ')'
1283///
Alexey Bataeva0569352015-12-01 10:17:31 +00001284/// priority-clause:
1285/// 'priority' '(' expression ')'
1286///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001287/// grainsize-clause:
1288/// 'grainsize' '(' expression ')'
1289///
Alexey Bataev382967a2015-12-08 12:06:20 +00001290/// num_tasks-clause:
1291/// 'num_tasks' '(' expression ')'
1292///
Alexey Bataev28c75412015-12-15 08:19:24 +00001293/// hint-clause:
1294/// 'hint' '(' expression ')'
1295///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001296OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1297 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001298 SourceLocation LLoc = Tok.getLocation();
1299 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001300
Alexey Bataev2af33e32016-04-07 12:45:37 +00001301 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001302
1303 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001304 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001305
Alexey Bataev2af33e32016-04-07 12:45:37 +00001306 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001307}
1308
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001309/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001310///
1311/// default-clause:
1312/// 'default' '(' 'none' | 'shared' ')
1313///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001314/// proc_bind-clause:
1315/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1316///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001317OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1318 SourceLocation Loc = Tok.getLocation();
1319 SourceLocation LOpen = ConsumeToken();
1320 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001321 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001322 if (T.expectAndConsume(diag::err_expected_lparen_after,
1323 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001324 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001325
Alexey Bataeva55ed262014-05-28 06:15:33 +00001326 unsigned Type = getOpenMPSimpleClauseType(
1327 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001328 SourceLocation TypeLoc = Tok.getLocation();
1329 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1330 Tok.isNot(tok::annot_pragma_openmp_end))
1331 ConsumeAnyToken();
1332
1333 // Parse ')'.
1334 T.consumeClose();
1335
1336 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1337 Tok.getLocation());
1338}
1339
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001340/// \brief Parsing of OpenMP clauses like 'ordered'.
1341///
1342/// ordered-clause:
1343/// 'ordered'
1344///
Alexey Bataev236070f2014-06-20 11:19:47 +00001345/// nowait-clause:
1346/// 'nowait'
1347///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001348/// untied-clause:
1349/// 'untied'
1350///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001351/// mergeable-clause:
1352/// 'mergeable'
1353///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001354/// read-clause:
1355/// 'read'
1356///
Alexey Bataev346265e2015-09-25 10:37:12 +00001357/// threads-clause:
1358/// 'threads'
1359///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001360/// simd-clause:
1361/// 'simd'
1362///
Alexey Bataevb825de12015-12-07 10:51:44 +00001363/// nogroup-clause:
1364/// 'nogroup'
1365///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001366OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1367 SourceLocation Loc = Tok.getLocation();
1368 ConsumeAnyToken();
1369
1370 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1371}
1372
1373
Alexey Bataev56dafe82014-06-20 07:16:17 +00001374/// \brief Parsing of OpenMP clauses with single expressions and some additional
1375/// argument like 'schedule' or 'dist_schedule'.
1376///
1377/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001378/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1379/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001380///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001381/// if-clause:
1382/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1383///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001384/// defaultmap:
1385/// 'defaultmap' '(' modifier ':' kind ')'
1386///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001387OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1388 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001389 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001390 // Parse '('.
1391 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1392 if (T.expectAndConsume(diag::err_expected_lparen_after,
1393 getOpenMPClauseName(Kind)))
1394 return nullptr;
1395
1396 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001397 SmallVector<unsigned, 4> Arg;
1398 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001399 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001400 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1401 Arg.resize(NumberOfElements);
1402 KLoc.resize(NumberOfElements);
1403 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1404 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1405 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1406 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001407 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001408 if (KindModifier > OMPC_SCHEDULE_unknown) {
1409 // Parse 'modifier'
1410 Arg[Modifier1] = KindModifier;
1411 KLoc[Modifier1] = Tok.getLocation();
1412 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1413 Tok.isNot(tok::annot_pragma_openmp_end))
1414 ConsumeAnyToken();
1415 if (Tok.is(tok::comma)) {
1416 // Parse ',' 'modifier'
1417 ConsumeAnyToken();
1418 KindModifier = getOpenMPSimpleClauseType(
1419 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1420 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1421 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001422 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001423 KLoc[Modifier2] = Tok.getLocation();
1424 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1425 Tok.isNot(tok::annot_pragma_openmp_end))
1426 ConsumeAnyToken();
1427 }
1428 // Parse ':'
1429 if (Tok.is(tok::colon))
1430 ConsumeAnyToken();
1431 else
1432 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1433 KindModifier = getOpenMPSimpleClauseType(
1434 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1435 }
1436 Arg[ScheduleKind] = KindModifier;
1437 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001438 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1439 Tok.isNot(tok::annot_pragma_openmp_end))
1440 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001441 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1442 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1443 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001444 Tok.is(tok::comma))
1445 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001446 } else if (Kind == OMPC_dist_schedule) {
1447 Arg.push_back(getOpenMPSimpleClauseType(
1448 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1449 KLoc.push_back(Tok.getLocation());
1450 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1451 Tok.isNot(tok::annot_pragma_openmp_end))
1452 ConsumeAnyToken();
1453 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1454 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001455 } else if (Kind == OMPC_defaultmap) {
1456 // Get a defaultmap modifier
1457 Arg.push_back(getOpenMPSimpleClauseType(
1458 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1459 KLoc.push_back(Tok.getLocation());
1460 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1461 Tok.isNot(tok::annot_pragma_openmp_end))
1462 ConsumeAnyToken();
1463 // Parse ':'
1464 if (Tok.is(tok::colon))
1465 ConsumeAnyToken();
1466 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1467 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1468 // Get a defaultmap kind
1469 Arg.push_back(getOpenMPSimpleClauseType(
1470 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1471 KLoc.push_back(Tok.getLocation());
1472 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1473 Tok.isNot(tok::annot_pragma_openmp_end))
1474 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001475 } else {
1476 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001477 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001478 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001479 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1480 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001481 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001482 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1483 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001484 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001485 } else {
1486 TPA.Revert();
1487 Arg.back() = OMPD_unknown;
1488 }
1489 } else
1490 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001491 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001492
Carlo Bertollib4adf552016-01-15 18:50:31 +00001493 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1494 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1495 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001496 if (NeedAnExpression) {
1497 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001498 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1499 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001500 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001501 }
1502
1503 // Parse ')'.
1504 T.consumeClose();
1505
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001506 if (NeedAnExpression && Val.isInvalid())
1507 return nullptr;
1508
Alexey Bataev56dafe82014-06-20 07:16:17 +00001509 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001510 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001511 T.getCloseLocation());
1512}
1513
Alexey Bataevc5e02582014-06-16 07:08:35 +00001514static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1515 UnqualifiedId &ReductionId) {
1516 SourceLocation TemplateKWLoc;
1517 if (ReductionIdScopeSpec.isEmpty()) {
1518 auto OOK = OO_None;
1519 switch (P.getCurToken().getKind()) {
1520 case tok::plus:
1521 OOK = OO_Plus;
1522 break;
1523 case tok::minus:
1524 OOK = OO_Minus;
1525 break;
1526 case tok::star:
1527 OOK = OO_Star;
1528 break;
1529 case tok::amp:
1530 OOK = OO_Amp;
1531 break;
1532 case tok::pipe:
1533 OOK = OO_Pipe;
1534 break;
1535 case tok::caret:
1536 OOK = OO_Caret;
1537 break;
1538 case tok::ampamp:
1539 OOK = OO_AmpAmp;
1540 break;
1541 case tok::pipepipe:
1542 OOK = OO_PipePipe;
1543 break;
1544 default:
1545 break;
1546 }
1547 if (OOK != OO_None) {
1548 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001549 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001550 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1551 return false;
1552 }
1553 }
1554 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1555 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001556 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001557 TemplateKWLoc, ReductionId);
1558}
1559
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001560/// Parses clauses with list.
1561bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1562 OpenMPClauseKind Kind,
1563 SmallVectorImpl<Expr *> &Vars,
1564 OpenMPVarListDataTy &Data) {
1565 UnqualifiedId UnqualifiedReductionId;
1566 bool InvalidReductionId = false;
1567 bool MapTypeModifierSpecified = false;
1568
1569 // Parse '('.
1570 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1571 if (T.expectAndConsume(diag::err_expected_lparen_after,
1572 getOpenMPClauseName(Kind)))
1573 return true;
1574
1575 bool NeedRParenForLinear = false;
1576 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1577 tok::annot_pragma_openmp_end);
1578 // Handle reduction-identifier for reduction clause.
1579 if (Kind == OMPC_reduction) {
1580 ColonProtectionRAIIObject ColonRAII(*this);
1581 if (getLangOpts().CPlusPlus)
1582 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1583 /*ObjectType=*/nullptr,
1584 /*EnteringContext=*/false);
1585 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1586 UnqualifiedReductionId);
1587 if (InvalidReductionId) {
1588 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1589 StopBeforeMatch);
1590 }
1591 if (Tok.is(tok::colon))
1592 Data.ColonLoc = ConsumeToken();
1593 else
1594 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1595 if (!InvalidReductionId)
1596 Data.ReductionId =
1597 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1598 } else if (Kind == OMPC_depend) {
1599 // Handle dependency type for depend clause.
1600 ColonProtectionRAIIObject ColonRAII(*this);
1601 Data.DepKind =
1602 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1603 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1604 Data.DepLinMapLoc = Tok.getLocation();
1605
1606 if (Data.DepKind == OMPC_DEPEND_unknown) {
1607 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1608 StopBeforeMatch);
1609 } else {
1610 ConsumeToken();
1611 // Special processing for depend(source) clause.
1612 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1613 // Parse ')'.
1614 T.consumeClose();
1615 return false;
1616 }
1617 }
1618 if (Tok.is(tok::colon))
1619 Data.ColonLoc = ConsumeToken();
1620 else {
1621 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1622 : diag::warn_pragma_expected_colon)
1623 << "dependency type";
1624 }
1625 } else if (Kind == OMPC_linear) {
1626 // Try to parse modifier if any.
1627 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1628 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1629 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1630 Data.DepLinMapLoc = ConsumeToken();
1631 LinearT.consumeOpen();
1632 NeedRParenForLinear = true;
1633 }
1634 } else if (Kind == OMPC_map) {
1635 // Handle map type for map clause.
1636 ColonProtectionRAIIObject ColonRAII(*this);
1637
1638 /// The map clause modifier token can be either a identifier or the C++
1639 /// delete keyword.
1640 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1641 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1642 };
1643
1644 // The first identifier may be a list item, a map-type or a
1645 // map-type-modifier. The map modifier can also be delete which has the same
1646 // spelling of the C++ delete keyword.
1647 Data.MapType =
1648 IsMapClauseModifierToken(Tok)
1649 ? static_cast<OpenMPMapClauseKind>(
1650 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1651 : OMPC_MAP_unknown;
1652 Data.DepLinMapLoc = Tok.getLocation();
1653 bool ColonExpected = false;
1654
1655 if (IsMapClauseModifierToken(Tok)) {
1656 if (PP.LookAhead(0).is(tok::colon)) {
1657 if (Data.MapType == OMPC_MAP_unknown)
1658 Diag(Tok, diag::err_omp_unknown_map_type);
1659 else if (Data.MapType == OMPC_MAP_always)
1660 Diag(Tok, diag::err_omp_map_type_missing);
1661 ConsumeToken();
1662 } else if (PP.LookAhead(0).is(tok::comma)) {
1663 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1664 PP.LookAhead(2).is(tok::colon)) {
1665 Data.MapTypeModifier = Data.MapType;
1666 if (Data.MapTypeModifier != OMPC_MAP_always) {
1667 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1668 Data.MapTypeModifier = OMPC_MAP_unknown;
1669 } else
1670 MapTypeModifierSpecified = true;
1671
1672 ConsumeToken();
1673 ConsumeToken();
1674
1675 Data.MapType =
1676 IsMapClauseModifierToken(Tok)
1677 ? static_cast<OpenMPMapClauseKind>(
1678 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1679 : OMPC_MAP_unknown;
1680 if (Data.MapType == OMPC_MAP_unknown ||
1681 Data.MapType == OMPC_MAP_always)
1682 Diag(Tok, diag::err_omp_unknown_map_type);
1683 ConsumeToken();
1684 } else {
1685 Data.MapType = OMPC_MAP_tofrom;
1686 Data.IsMapTypeImplicit = true;
1687 }
1688 } else {
1689 Data.MapType = OMPC_MAP_tofrom;
1690 Data.IsMapTypeImplicit = true;
1691 }
1692 } else {
1693 Data.MapType = OMPC_MAP_tofrom;
1694 Data.IsMapTypeImplicit = true;
1695 }
1696
1697 if (Tok.is(tok::colon))
1698 Data.ColonLoc = ConsumeToken();
1699 else if (ColonExpected)
1700 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1701 }
1702
1703 bool IsComma =
1704 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1705 (Kind == OMPC_reduction && !InvalidReductionId) ||
1706 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1707 (!MapTypeModifierSpecified ||
1708 Data.MapTypeModifier == OMPC_MAP_always)) ||
1709 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1710 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1711 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1712 Tok.isNot(tok::annot_pragma_openmp_end))) {
1713 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1714 // Parse variable
1715 ExprResult VarExpr =
1716 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1717 if (VarExpr.isUsable())
1718 Vars.push_back(VarExpr.get());
1719 else {
1720 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1721 StopBeforeMatch);
1722 }
1723 // Skip ',' if any
1724 IsComma = Tok.is(tok::comma);
1725 if (IsComma)
1726 ConsumeToken();
1727 else if (Tok.isNot(tok::r_paren) &&
1728 Tok.isNot(tok::annot_pragma_openmp_end) &&
1729 (!MayHaveTail || Tok.isNot(tok::colon)))
1730 Diag(Tok, diag::err_omp_expected_punc)
1731 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1732 : getOpenMPClauseName(Kind))
1733 << (Kind == OMPC_flush);
1734 }
1735
1736 // Parse ')' for linear clause with modifier.
1737 if (NeedRParenForLinear)
1738 LinearT.consumeClose();
1739
1740 // Parse ':' linear-step (or ':' alignment).
1741 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1742 if (MustHaveTail) {
1743 Data.ColonLoc = Tok.getLocation();
1744 SourceLocation ELoc = ConsumeToken();
1745 ExprResult Tail = ParseAssignmentExpression();
1746 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1747 if (Tail.isUsable())
1748 Data.TailExpr = Tail.get();
1749 else
1750 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1751 StopBeforeMatch);
1752 }
1753
1754 // Parse ')'.
1755 T.consumeClose();
1756 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1757 Vars.empty()) ||
1758 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1759 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1760 return true;
1761 return false;
1762}
1763
Alexander Musman1bb328c2014-06-04 13:06:39 +00001764/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001765/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001766///
1767/// private-clause:
1768/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001769/// firstprivate-clause:
1770/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001771/// lastprivate-clause:
1772/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001773/// shared-clause:
1774/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001775/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001776/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001777/// aligned-clause:
1778/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001779/// reduction-clause:
1780/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001781/// copyprivate-clause:
1782/// 'copyprivate' '(' list ')'
1783/// flush-clause:
1784/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001785/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001786/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001787/// map-clause:
1788/// 'map' '(' [ [ always , ]
1789/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001790/// to-clause:
1791/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001792/// from-clause:
1793/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001794/// use_device_ptr-clause:
1795/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001796/// is_device_ptr-clause:
1797/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001798///
Alexey Bataev182227b2015-08-20 10:54:39 +00001799/// For 'linear' clause linear-list may have the following forms:
1800/// list
1801/// modifier(list)
1802/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001803OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1804 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001805 SourceLocation Loc = Tok.getLocation();
1806 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001807 SmallVector<Expr *, 4> Vars;
1808 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001809
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001810 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001811 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001812
Alexey Bataevc5e02582014-06-16 07:08:35 +00001813 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001814 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1815 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1816 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1817 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001818}
1819