blob: cab7d3432db32ebd05ecb69ea97d9bf36670dd5a [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 Lida681182017-01-10 18:08:18 +0000122 { OMPD_target_teams_distribute, OMPD_simd, OMPD_target_teams_distribute_simd },
Kelvin Li1851df52017-01-03 05:23:48 +0000123 { OMPD_target_teams_distribute_parallel, OMPD_for, OMPD_target_teams_distribute_parallel_for },
124 { OMPD_target_teams_distribute_parallel_for, OMPD_simd, OMPD_target_teams_distribute_parallel_for_simd }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000125 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000126 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000127 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000128 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000129 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000130 ? static_cast<unsigned>(OMPD_unknown)
131 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
132 if (DKind == OMPD_unknown)
133 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000134
Alexander Musmanf82886e2014-09-18 05:12:34 +0000135 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000136 if (DKind != F[i][0])
137 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000138
Dmitry Polukhin82478332016-02-13 06:53:38 +0000139 Tok = P.getPreprocessor().LookAhead(0);
140 unsigned SDKind =
141 Tok.isAnnotation()
142 ? static_cast<unsigned>(OMPD_unknown)
143 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
144 if (SDKind == OMPD_unknown)
145 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000146
Dmitry Polukhin82478332016-02-13 06:53:38 +0000147 if (SDKind == F[i][1]) {
148 P.ConsumeToken();
149 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000150 }
151 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000152 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
153 : OMPD_unknown;
154}
155
156static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000157 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000158 Sema &Actions = P.getActions();
159 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000160 // Allow to use 'operator' keyword for C++ operators
161 bool WithOperator = false;
162 if (Tok.is(tok::kw_operator)) {
163 P.ConsumeToken();
164 Tok = P.getCurToken();
165 WithOperator = true;
166 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000167 switch (Tok.getKind()) {
168 case tok::plus: // '+'
169 OOK = OO_Plus;
170 break;
171 case tok::minus: // '-'
172 OOK = OO_Minus;
173 break;
174 case tok::star: // '*'
175 OOK = OO_Star;
176 break;
177 case tok::amp: // '&'
178 OOK = OO_Amp;
179 break;
180 case tok::pipe: // '|'
181 OOK = OO_Pipe;
182 break;
183 case tok::caret: // '^'
184 OOK = OO_Caret;
185 break;
186 case tok::ampamp: // '&&'
187 OOK = OO_AmpAmp;
188 break;
189 case tok::pipepipe: // '||'
190 OOK = OO_PipePipe;
191 break;
192 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000193 if (!WithOperator)
194 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000195 default:
196 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
197 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
198 Parser::StopBeforeMatch);
199 return DeclarationName();
200 }
201 P.ConsumeToken();
202 auto &DeclNames = Actions.getASTContext().DeclarationNames;
203 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
204 : DeclNames.getCXXOperatorName(OOK);
205}
206
207/// \brief Parse 'omp declare reduction' construct.
208///
209/// declare-reduction-directive:
210/// annot_pragma_openmp 'declare' 'reduction'
211/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
212/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
213/// annot_pragma_openmp_end
214/// <reduction_id> is either a base language identifier or one of the following
215/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
216///
217Parser::DeclGroupPtrTy
218Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
219 // Parse '('.
220 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
221 if (T.expectAndConsume(diag::err_expected_lparen_after,
222 getOpenMPDirectiveName(OMPD_declare_reduction))) {
223 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
224 return DeclGroupPtrTy();
225 }
226
227 DeclarationName Name = parseOpenMPReductionId(*this);
228 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
229 return DeclGroupPtrTy();
230
231 // Consume ':'.
232 bool IsCorrect = !ExpectAndConsume(tok::colon);
233
234 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
235 return DeclGroupPtrTy();
236
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000237 IsCorrect = IsCorrect && !Name.isEmpty();
238
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000239 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
240 Diag(Tok.getLocation(), diag::err_expected_type);
241 IsCorrect = false;
242 }
243
244 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
245 return DeclGroupPtrTy();
246
247 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
248 // Parse list of types until ':' token.
249 do {
250 ColonProtectionRAIIObject ColonRAII(*this);
251 SourceRange Range;
252 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
253 if (TR.isUsable()) {
254 auto ReductionType =
255 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
256 if (!ReductionType.isNull()) {
257 ReductionTypes.push_back(
258 std::make_pair(ReductionType, Range.getBegin()));
259 }
260 } else {
261 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
262 StopBeforeMatch);
263 }
264
265 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
266 break;
267
268 // Consume ','.
269 if (ExpectAndConsume(tok::comma)) {
270 IsCorrect = false;
271 if (Tok.is(tok::annot_pragma_openmp_end)) {
272 Diag(Tok.getLocation(), diag::err_expected_type);
273 return DeclGroupPtrTy();
274 }
275 }
276 } while (Tok.isNot(tok::annot_pragma_openmp_end));
277
278 if (ReductionTypes.empty()) {
279 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
280 return DeclGroupPtrTy();
281 }
282
283 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
284 return DeclGroupPtrTy();
285
286 // Consume ':'.
287 if (ExpectAndConsume(tok::colon))
288 IsCorrect = false;
289
290 if (Tok.is(tok::annot_pragma_openmp_end)) {
291 Diag(Tok.getLocation(), diag::err_expected_expression);
292 return DeclGroupPtrTy();
293 }
294
295 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
296 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
297
298 // Parse <combiner> expression and then parse initializer if any for each
299 // correct type.
300 unsigned I = 0, E = ReductionTypes.size();
301 for (auto *D : DRD.get()) {
302 TentativeParsingAction TPA(*this);
303 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
304 Scope::OpenMPDirectiveScope);
305 // Parse <combiner> expression.
306 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
307 ExprResult CombinerResult =
308 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
309 D->getLocation(), /*DiscardedValue=*/true);
310 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
311
312 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
313 Tok.isNot(tok::annot_pragma_openmp_end)) {
314 TPA.Commit();
315 IsCorrect = false;
316 break;
317 }
318 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
319 ExprResult InitializerResult;
320 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
321 // Parse <initializer> expression.
322 if (Tok.is(tok::identifier) &&
323 Tok.getIdentifierInfo()->isStr("initializer"))
324 ConsumeToken();
325 else {
326 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
327 TPA.Commit();
328 IsCorrect = false;
329 break;
330 }
331 // Parse '('.
332 BalancedDelimiterTracker T(*this, tok::l_paren,
333 tok::annot_pragma_openmp_end);
334 IsCorrect =
335 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
336 IsCorrect;
337 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
338 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
339 Scope::OpenMPDirectiveScope);
340 // Parse expression.
341 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
342 InitializerResult = Actions.ActOnFinishFullExpr(
343 ParseAssignmentExpression().get(), D->getLocation(),
344 /*DiscardedValue=*/true);
345 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
346 D, InitializerResult.get());
347 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
348 Tok.isNot(tok::annot_pragma_openmp_end)) {
349 TPA.Commit();
350 IsCorrect = false;
351 break;
352 }
353 IsCorrect =
354 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
355 }
356 }
357
358 ++I;
359 // Revert parsing if not the last type, otherwise accept it, we're done with
360 // parsing.
361 if (I != E)
362 TPA.Revert();
363 else
364 TPA.Commit();
365 }
366 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
367 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000368}
369
Alexey Bataev2af33e32016-04-07 12:45:37 +0000370namespace {
371/// RAII that recreates function context for correct parsing of clauses of
372/// 'declare simd' construct.
373/// OpenMP, 2.8.2 declare simd Construct
374/// The expressions appearing in the clauses of this directive are evaluated in
375/// the scope of the arguments of the function declaration or definition.
376class FNContextRAII final {
377 Parser &P;
378 Sema::CXXThisScopeRAII *ThisScope;
379 Parser::ParseScope *TempScope;
380 Parser::ParseScope *FnScope;
381 bool HasTemplateScope = false;
382 bool HasFunScope = false;
383 FNContextRAII() = delete;
384 FNContextRAII(const FNContextRAII &) = delete;
385 FNContextRAII &operator=(const FNContextRAII &) = delete;
386
387public:
388 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
389 Decl *D = *Ptr.get().begin();
390 NamedDecl *ND = dyn_cast<NamedDecl>(D);
391 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
392 Sema &Actions = P.getActions();
393
394 // Allow 'this' within late-parsed attributes.
395 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
396 ND && ND->isCXXInstanceMember());
397
398 // If the Decl is templatized, add template parameters to scope.
399 HasTemplateScope = D->isTemplateDecl();
400 TempScope =
401 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
402 if (HasTemplateScope)
403 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
404
405 // If the Decl is on a function, add function parameters to the scope.
406 HasFunScope = D->isFunctionOrFunctionTemplate();
407 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
408 HasFunScope);
409 if (HasFunScope)
410 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
411 }
412 ~FNContextRAII() {
413 if (HasFunScope) {
414 P.getActions().ActOnExitFunctionContext();
415 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
416 }
417 if (HasTemplateScope)
418 TempScope->Exit();
419 delete FnScope;
420 delete TempScope;
421 delete ThisScope;
422 }
423};
424} // namespace
425
Alexey Bataevd93d3762016-04-12 09:35:56 +0000426/// Parses clauses for 'declare simd' directive.
427/// clause:
428/// 'inbranch' | 'notinbranch'
429/// 'simdlen' '(' <expr> ')'
430/// { 'uniform' '(' <argument_list> ')' }
431/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000432/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
433static bool parseDeclareSimdClauses(
434 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
435 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
436 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
437 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000438 SourceRange BSRange;
439 const Token &Tok = P.getCurToken();
440 bool IsError = false;
441 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
442 if (Tok.isNot(tok::identifier))
443 break;
444 OMPDeclareSimdDeclAttr::BranchStateTy Out;
445 IdentifierInfo *II = Tok.getIdentifierInfo();
446 StringRef ClauseName = II->getName();
447 // Parse 'inranch|notinbranch' clauses.
448 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
449 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
450 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
451 << ClauseName
452 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
453 IsError = true;
454 }
455 BS = Out;
456 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
457 P.ConsumeToken();
458 } else if (ClauseName.equals("simdlen")) {
459 if (SimdLen.isUsable()) {
460 P.Diag(Tok, diag::err_omp_more_one_clause)
461 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
462 IsError = true;
463 }
464 P.ConsumeToken();
465 SourceLocation RLoc;
466 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
467 if (SimdLen.isInvalid())
468 IsError = true;
469 } else {
470 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000471 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
472 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000473 Parser::OpenMPVarListDataTy Data;
474 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000475 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000476 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000477 else if (CKind == OMPC_linear)
478 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000479
480 P.ConsumeToken();
481 if (P.ParseOpenMPVarList(OMPD_declare_simd,
482 getOpenMPClauseKind(ClauseName), *Vars, Data))
483 IsError = true;
484 if (CKind == OMPC_aligned)
485 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000486 else if (CKind == OMPC_linear) {
487 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
488 Data.DepLinMapLoc))
489 Data.LinKind = OMPC_LINEAR_val;
490 LinModifiers.append(Linears.size() - LinModifiers.size(),
491 Data.LinKind);
492 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
493 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000494 } else
495 // TODO: add parsing of other clauses.
496 break;
497 }
498 // Skip ',' if any.
499 if (Tok.is(tok::comma))
500 P.ConsumeToken();
501 }
502 return IsError;
503}
504
Alexey Bataev2af33e32016-04-07 12:45:37 +0000505/// Parse clauses for '#pragma omp declare simd'.
506Parser::DeclGroupPtrTy
507Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
508 CachedTokens &Toks, SourceLocation Loc) {
509 PP.EnterToken(Tok);
510 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
511 // Consume the previously pushed token.
512 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
513
514 FNContextRAII FnContext(*this, Ptr);
515 OMPDeclareSimdDeclAttr::BranchStateTy BS =
516 OMPDeclareSimdDeclAttr::BS_Undefined;
517 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000518 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000519 SmallVector<Expr *, 4> Aligneds;
520 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000521 SmallVector<Expr *, 4> Linears;
522 SmallVector<unsigned, 4> LinModifiers;
523 SmallVector<Expr *, 4> Steps;
524 bool IsError =
525 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
526 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000527 // Need to check for extra tokens.
528 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
529 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
530 << getOpenMPDirectiveName(OMPD_declare_simd);
531 while (Tok.isNot(tok::annot_pragma_openmp_end))
532 ConsumeAnyToken();
533 }
534 // Skip the last annot_pragma_openmp_end.
535 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000536 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000537 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000538 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
539 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000540 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000541 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000542}
543
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000544/// \brief Parsing of declarative OpenMP directives.
545///
546/// threadprivate-directive:
547/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000548/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000549///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000550/// declare-reduction-directive:
551/// annot_pragma_openmp 'declare' 'reduction' [...]
552/// annot_pragma_openmp_end
553///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000554/// declare-simd-directive:
555/// annot_pragma_openmp 'declare simd' {<clause> [,]}
556/// annot_pragma_openmp_end
557/// <function declaration/definition>
558///
559Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
560 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
561 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000562 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000563 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000564
565 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000566 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000567
568 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000569 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000570 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000571 ThreadprivateListParserHelper Helper(this);
572 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000573 // The last seen token is annot_pragma_openmp_end - need to check for
574 // extra tokens.
575 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
576 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000577 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000578 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000579 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000580 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000582 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
583 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000584 }
585 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000586 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000587 case OMPD_declare_reduction:
588 ConsumeToken();
589 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
590 // The last seen token is annot_pragma_openmp_end - need to check for
591 // extra tokens.
592 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
593 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
594 << getOpenMPDirectiveName(OMPD_declare_reduction);
595 while (Tok.isNot(tok::annot_pragma_openmp_end))
596 ConsumeAnyToken();
597 }
598 // Skip the last annot_pragma_openmp_end.
599 ConsumeToken();
600 return Res;
601 }
602 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000603 case OMPD_declare_simd: {
604 // The syntax is:
605 // { #pragma omp declare simd }
606 // <function-declaration-or-definition>
607 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000608 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000609 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000610 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
611 Toks.push_back(Tok);
612 ConsumeAnyToken();
613 }
614 Toks.push_back(Tok);
615 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000616
617 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000618 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000619 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000620 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000621 // Here we expect to see some function declaration.
622 if (AS == AS_none) {
623 assert(TagType == DeclSpec::TST_unspecified);
624 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000625 ParsingDeclSpec PDS(*this);
626 Ptr = ParseExternalDeclaration(Attrs, &PDS);
627 } else {
628 Ptr =
629 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
630 }
631 }
632 if (!Ptr) {
633 Diag(Loc, diag::err_omp_decl_in_declare_simd);
634 return DeclGroupPtrTy();
635 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000636 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000637 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000638 case OMPD_declare_target: {
639 SourceLocation DTLoc = ConsumeAnyToken();
640 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000641 // OpenMP 4.5 syntax with list of entities.
642 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
643 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
644 OMPDeclareTargetDeclAttr::MapTypeTy MT =
645 OMPDeclareTargetDeclAttr::MT_To;
646 if (Tok.is(tok::identifier)) {
647 IdentifierInfo *II = Tok.getIdentifierInfo();
648 StringRef ClauseName = II->getName();
649 // Parse 'to|link' clauses.
650 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
651 MT)) {
652 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
653 << ClauseName;
654 break;
655 }
656 ConsumeToken();
657 }
658 auto Callback = [this, MT, &SameDirectiveDecls](
659 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
660 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
661 SameDirectiveDecls);
662 };
663 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
664 break;
665
666 // Consume optional ','.
667 if (Tok.is(tok::comma))
668 ConsumeToken();
669 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000670 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000671 ConsumeAnyToken();
672 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000673 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000674
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000675 // Skip the last annot_pragma_openmp_end.
676 ConsumeAnyToken();
677
678 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
679 return DeclGroupPtrTy();
680
681 DKind = ParseOpenMPDirectiveKind(*this);
682 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
683 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
684 ParsedAttributesWithRange attrs(AttrFactory);
685 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000686 ParseExternalDeclaration(attrs);
687 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
688 TentativeParsingAction TPA(*this);
689 ConsumeToken();
690 DKind = ParseOpenMPDirectiveKind(*this);
691 if (DKind != OMPD_end_declare_target)
692 TPA.Revert();
693 else
694 TPA.Commit();
695 }
696 }
697
698 if (DKind == OMPD_end_declare_target) {
699 ConsumeAnyToken();
700 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
701 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
702 << getOpenMPDirectiveName(OMPD_end_declare_target);
703 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
704 }
705 // Skip the last annot_pragma_openmp_end.
706 ConsumeAnyToken();
707 } else {
708 Diag(Tok, diag::err_expected_end_declare_target);
709 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
710 }
711 Actions.ActOnFinishOpenMPDeclareTargetDirective();
712 return DeclGroupPtrTy();
713 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000714 case OMPD_unknown:
715 Diag(Tok, diag::err_omp_unknown_directive);
716 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000718 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000720 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000721 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000722 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000723 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000724 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000725 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000726 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000727 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000728 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000729 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000730 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000731 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000732 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000733 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000734 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000735 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000736 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000737 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000738 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000739 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000740 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000741 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000742 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000743 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000744 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000745 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000746 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000747 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000748 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000749 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000750 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000751 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000752 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000753 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000754 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000755 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000756 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000757 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000758 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000759 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000760 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000761 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000762 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +0000763 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +0000764 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +0000765 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000766 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000767 break;
768 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000769 while (Tok.isNot(tok::annot_pragma_openmp_end))
770 ConsumeAnyToken();
771 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000772 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000773}
774
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000775/// \brief Parsing of declarative or executable OpenMP directives.
776///
777/// threadprivate-directive:
778/// annot_pragma_openmp 'threadprivate' simple-variable-list
779/// annot_pragma_openmp_end
780///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000781/// declare-reduction-directive:
782/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
783/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
784/// ('omp_priv' '=' <expression>|<function_call>) ')']
785/// annot_pragma_openmp_end
786///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000787/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000788/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000789/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
790/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000791/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000792/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000793/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000794/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000795/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000796/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000797/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000798/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000799/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000800/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000801/// 'teams distribute parallel for' | 'target teams' |
802/// 'target teams distribute' |
Kelvin Li1851df52017-01-03 05:23:48 +0000803/// 'target teams distribute parallel for' |
Kelvin Lida681182017-01-10 18:08:18 +0000804/// 'target teams distribute parallel for simd' |
805/// 'target teams distribute simd' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000806/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000807///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000808StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
809 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000810 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000811 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000812 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000813 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000814 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000815 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000816 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000818 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000819 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000820 // Name of critical directive.
821 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000822 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000823 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000824 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825
826 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000827 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000828 if (Allowed != ACK_Any) {
829 Diag(Tok, diag::err_omp_immediate_directive)
830 << getOpenMPDirectiveName(DKind) << 0;
831 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000832 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000833 ThreadprivateListParserHelper Helper(this);
834 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000835 // The last seen token is annot_pragma_openmp_end - need to check for
836 // extra tokens.
837 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
838 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000839 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000840 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000841 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000842 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
843 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000844 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
845 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000846 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000847 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000848 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000849 case OMPD_declare_reduction:
850 ConsumeToken();
851 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
852 // The last seen token is annot_pragma_openmp_end - need to check for
853 // extra tokens.
854 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
855 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
856 << getOpenMPDirectiveName(OMPD_declare_reduction);
857 while (Tok.isNot(tok::annot_pragma_openmp_end))
858 ConsumeAnyToken();
859 }
860 ConsumeAnyToken();
861 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
862 } else
863 SkipUntil(tok::annot_pragma_openmp_end);
864 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000865 case OMPD_flush:
866 if (PP.LookAhead(0).is(tok::l_paren)) {
867 FlushHasClause = true;
868 // Push copy of the current token back to stream to properly parse
869 // pseudo-clause OMPFlushClause.
870 PP.EnterToken(Tok);
871 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000872 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000873 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000874 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000875 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000876 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000877 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000878 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000879 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000880 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000881 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000882 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000883 }
884 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000885 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000886 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000887 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000888 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000889 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000890 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000891 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000892 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000893 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000894 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000895 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000896 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000897 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000898 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000899 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000900 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000901 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000902 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000903 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000904 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000905 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000906 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000907 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000908 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000909 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000910 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000911 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000912 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000913 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000914 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000915 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000916 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000917 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +0000918 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +0000919 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +0000920 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +0000921 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +0000922 case OMPD_target_teams_distribute_parallel_for_simd:
923 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000924 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000925 // Parse directive name of the 'critical' directive if any.
926 if (DKind == OMPD_critical) {
927 BalancedDelimiterTracker T(*this, tok::l_paren,
928 tok::annot_pragma_openmp_end);
929 if (!T.consumeOpen()) {
930 if (Tok.isAnyIdentifier()) {
931 DirName =
932 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
933 ConsumeAnyToken();
934 } else {
935 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
936 }
937 T.consumeClose();
938 }
Alexey Bataev80909872015-07-02 11:25:17 +0000939 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000940 CancelRegion = ParseOpenMPDirectiveKind(*this);
941 if (Tok.isNot(tok::annot_pragma_openmp_end))
942 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000943 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000944
Alexey Bataevf29276e2014-06-18 04:14:57 +0000945 if (isOpenMPLoopDirective(DKind))
946 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
947 if (isOpenMPSimdDirective(DKind))
948 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
949 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000950 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000951
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000952 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000953 OpenMPClauseKind CKind =
954 Tok.isAnnotation()
955 ? OMPC_unknown
956 : FlushHasClause ? OMPC_flush
957 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000958 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000959 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000960 OMPClause *Clause =
961 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000962 FirstClauses[CKind].setInt(true);
963 if (Clause) {
964 FirstClauses[CKind].setPointer(Clause);
965 Clauses.push_back(Clause);
966 }
967
968 // Skip ',' if any.
969 if (Tok.is(tok::comma))
970 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000971 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000972 }
973 // End location of the directive.
974 EndLoc = Tok.getLocation();
975 // Consume final annot_pragma_openmp_end.
976 ConsumeToken();
977
Alexey Bataeveb482352015-12-18 05:05:56 +0000978 // OpenMP [2.13.8, ordered Construct, Syntax]
979 // If the depend clause is specified, the ordered construct is a stand-alone
980 // directive.
981 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000982 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000983 Diag(Loc, diag::err_omp_immediate_directive)
984 << getOpenMPDirectiveName(DKind) << 1
985 << getOpenMPClauseName(OMPC_depend);
986 }
987 HasAssociatedStatement = false;
988 }
989
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000990 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000991 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992 // The body is a block scope like in Lambdas and Blocks.
993 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000994 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000995 Actions.ActOnStartOfCompoundStmt();
996 // Parse statement
997 AssociatedStmt = ParseStatement();
998 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000999 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001001 Directive = Actions.ActOnOpenMPExecutableDirective(
1002 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1003 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001004
1005 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001006 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001007 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001008 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001009 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001010 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001011 case OMPD_declare_target:
1012 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001013 Diag(Tok, diag::err_omp_unexpected_directive)
1014 << getOpenMPDirectiveName(DKind);
1015 SkipUntil(tok::annot_pragma_openmp_end);
1016 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017 case OMPD_unknown:
1018 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001019 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001020 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001021 }
1022 return Directive;
1023}
1024
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001025// Parses simple list:
1026// simple-variable-list:
1027// '(' id-expression {, id-expression} ')'
1028//
1029bool Parser::ParseOpenMPSimpleVarList(
1030 OpenMPDirectiveKind Kind,
1031 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1032 Callback,
1033 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001034 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001035 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001036 if (T.expectAndConsume(diag::err_expected_lparen_after,
1037 getOpenMPDirectiveName(Kind)))
1038 return true;
1039 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001040 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001041
1042 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001044 CXXScopeSpec SS;
1045 SourceLocation TemplateKWLoc;
1046 UnqualifiedId Name;
1047 // Read var name.
1048 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001050
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001051 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001052 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001053 IsCorrect = false;
1054 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001055 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001056 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001058 IsCorrect = false;
1059 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001060 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001061 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1062 Tok.isNot(tok::annot_pragma_openmp_end)) {
1063 IsCorrect = false;
1064 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001065 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001066 Diag(PrevTok.getLocation(), diag::err_expected)
1067 << tok::identifier
1068 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001069 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001070 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001071 }
1072 // Consume ','.
1073 if (Tok.is(tok::comma)) {
1074 ConsumeToken();
1075 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001076 }
1077
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001079 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001080 IsCorrect = false;
1081 }
1082
1083 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001084 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001085
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001086 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001087}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001088
1089/// \brief Parsing of OpenMP clauses.
1090///
1091/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001092/// if-clause | final-clause | num_threads-clause | safelen-clause |
1093/// default-clause | private-clause | firstprivate-clause | shared-clause
1094/// | linear-clause | aligned-clause | collapse-clause |
1095/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001096/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001097/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001098/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001099/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001100/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001101/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001102/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001103///
1104OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1105 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001106 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001107 bool ErrorFound = false;
1108 // Check if clause is allowed for the given directive.
1109 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001110 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1111 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001112 ErrorFound = true;
1113 }
1114
1115 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001116 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001117 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001118 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001119 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001120 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001121 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001122 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001123 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001124 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001125 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001126 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001127 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001128 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001129 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001130 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001131 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001132 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001133 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001134 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001135 // OpenMP [2.9.1, target data construct, Restrictions]
1136 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001137 // OpenMP [2.11.1, task Construct, Restrictions]
1138 // At most one if clause can appear on the directive.
1139 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001140 // OpenMP [teams Construct, Restrictions]
1141 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001142 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001143 // OpenMP [2.9.1, task Construct, Restrictions]
1144 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001145 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1146 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001147 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1148 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001149 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001150 Diag(Tok, diag::err_omp_more_one_clause)
1151 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001152 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001153 }
1154
Alexey Bataev10e775f2015-07-30 11:36:16 +00001155 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1156 Clause = ParseOpenMPClause(CKind);
1157 else
1158 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001159 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001160 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001161 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001162 // OpenMP [2.14.3.1, Restrictions]
1163 // Only a single default clause may be specified on a parallel, task or
1164 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001165 // OpenMP [2.5, parallel Construct, Restrictions]
1166 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001167 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001168 Diag(Tok, diag::err_omp_more_one_clause)
1169 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001170 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001171 }
1172
1173 Clause = ParseOpenMPSimpleClause(CKind);
1174 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001175 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001176 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001177 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001178 // OpenMP [2.7.1, Restrictions, p. 3]
1179 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001180 // OpenMP [2.10.4, Restrictions, p. 106]
1181 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001182 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001183 Diag(Tok, diag::err_omp_more_one_clause)
1184 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001185 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001186 }
1187
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001188 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001189 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1190 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001191 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001192 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001193 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001194 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001195 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001196 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001197 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001198 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001199 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001200 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001201 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001202 // OpenMP [2.7.1, Restrictions, p. 9]
1203 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001204 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1205 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001206 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001207 Diag(Tok, diag::err_omp_more_one_clause)
1208 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001209 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001210 }
1211
1212 Clause = ParseOpenMPClause(CKind);
1213 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001214 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001215 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001216 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001217 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001218 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001219 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001220 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001221 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001222 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001223 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001224 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001225 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001226 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001227 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001228 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001229 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001230 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001231 break;
1232 case OMPC_unknown:
1233 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001234 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001235 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001236 break;
1237 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001238 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001239 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1240 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001241 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001242 break;
1243 }
Craig Topper161e4db2014-05-21 06:02:52 +00001244 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001245}
1246
Alexey Bataev2af33e32016-04-07 12:45:37 +00001247/// Parses simple expression in parens for single-expression clauses of OpenMP
1248/// constructs.
1249/// \param RLoc Returned location of right paren.
1250ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1251 SourceLocation &RLoc) {
1252 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1253 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1254 return ExprError();
1255
1256 SourceLocation ELoc = Tok.getLocation();
1257 ExprResult LHS(ParseCastExpression(
1258 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1259 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1260 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1261
1262 // Parse ')'.
1263 T.consumeClose();
1264
1265 RLoc = T.getCloseLocation();
1266 return Val;
1267}
1268
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001269/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001270/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001271/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001272///
Alexey Bataev3778b602014-07-17 07:32:53 +00001273/// final-clause:
1274/// 'final' '(' expression ')'
1275///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001276/// num_threads-clause:
1277/// 'num_threads' '(' expression ')'
1278///
1279/// safelen-clause:
1280/// 'safelen' '(' expression ')'
1281///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001282/// simdlen-clause:
1283/// 'simdlen' '(' expression ')'
1284///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001285/// collapse-clause:
1286/// 'collapse' '(' expression ')'
1287///
Alexey Bataeva0569352015-12-01 10:17:31 +00001288/// priority-clause:
1289/// 'priority' '(' expression ')'
1290///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001291/// grainsize-clause:
1292/// 'grainsize' '(' expression ')'
1293///
Alexey Bataev382967a2015-12-08 12:06:20 +00001294/// num_tasks-clause:
1295/// 'num_tasks' '(' expression ')'
1296///
Alexey Bataev28c75412015-12-15 08:19:24 +00001297/// hint-clause:
1298/// 'hint' '(' expression ')'
1299///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001300OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1301 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001302 SourceLocation LLoc = Tok.getLocation();
1303 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001304
Alexey Bataev2af33e32016-04-07 12:45:37 +00001305 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001306
1307 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001308 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001309
Alexey Bataev2af33e32016-04-07 12:45:37 +00001310 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001311}
1312
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001313/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001314///
1315/// default-clause:
1316/// 'default' '(' 'none' | 'shared' ')
1317///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001318/// proc_bind-clause:
1319/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1320///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001321OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1322 SourceLocation Loc = Tok.getLocation();
1323 SourceLocation LOpen = ConsumeToken();
1324 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001325 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001326 if (T.expectAndConsume(diag::err_expected_lparen_after,
1327 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001328 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001329
Alexey Bataeva55ed262014-05-28 06:15:33 +00001330 unsigned Type = getOpenMPSimpleClauseType(
1331 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001332 SourceLocation TypeLoc = Tok.getLocation();
1333 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1334 Tok.isNot(tok::annot_pragma_openmp_end))
1335 ConsumeAnyToken();
1336
1337 // Parse ')'.
1338 T.consumeClose();
1339
1340 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1341 Tok.getLocation());
1342}
1343
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001344/// \brief Parsing of OpenMP clauses like 'ordered'.
1345///
1346/// ordered-clause:
1347/// 'ordered'
1348///
Alexey Bataev236070f2014-06-20 11:19:47 +00001349/// nowait-clause:
1350/// 'nowait'
1351///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001352/// untied-clause:
1353/// 'untied'
1354///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001355/// mergeable-clause:
1356/// 'mergeable'
1357///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001358/// read-clause:
1359/// 'read'
1360///
Alexey Bataev346265e2015-09-25 10:37:12 +00001361/// threads-clause:
1362/// 'threads'
1363///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001364/// simd-clause:
1365/// 'simd'
1366///
Alexey Bataevb825de12015-12-07 10:51:44 +00001367/// nogroup-clause:
1368/// 'nogroup'
1369///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001370OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1371 SourceLocation Loc = Tok.getLocation();
1372 ConsumeAnyToken();
1373
1374 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1375}
1376
1377
Alexey Bataev56dafe82014-06-20 07:16:17 +00001378/// \brief Parsing of OpenMP clauses with single expressions and some additional
1379/// argument like 'schedule' or 'dist_schedule'.
1380///
1381/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001382/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1383/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001384///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001385/// if-clause:
1386/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1387///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001388/// defaultmap:
1389/// 'defaultmap' '(' modifier ':' kind ')'
1390///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001391OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1392 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001393 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001394 // Parse '('.
1395 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1396 if (T.expectAndConsume(diag::err_expected_lparen_after,
1397 getOpenMPClauseName(Kind)))
1398 return nullptr;
1399
1400 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001401 SmallVector<unsigned, 4> Arg;
1402 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001403 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001404 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1405 Arg.resize(NumberOfElements);
1406 KLoc.resize(NumberOfElements);
1407 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1408 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1409 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1410 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001411 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001412 if (KindModifier > OMPC_SCHEDULE_unknown) {
1413 // Parse 'modifier'
1414 Arg[Modifier1] = KindModifier;
1415 KLoc[Modifier1] = Tok.getLocation();
1416 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1417 Tok.isNot(tok::annot_pragma_openmp_end))
1418 ConsumeAnyToken();
1419 if (Tok.is(tok::comma)) {
1420 // Parse ',' 'modifier'
1421 ConsumeAnyToken();
1422 KindModifier = getOpenMPSimpleClauseType(
1423 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1424 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1425 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001426 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001427 KLoc[Modifier2] = Tok.getLocation();
1428 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1429 Tok.isNot(tok::annot_pragma_openmp_end))
1430 ConsumeAnyToken();
1431 }
1432 // Parse ':'
1433 if (Tok.is(tok::colon))
1434 ConsumeAnyToken();
1435 else
1436 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1437 KindModifier = getOpenMPSimpleClauseType(
1438 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1439 }
1440 Arg[ScheduleKind] = KindModifier;
1441 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001442 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1443 Tok.isNot(tok::annot_pragma_openmp_end))
1444 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001445 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1446 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1447 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001448 Tok.is(tok::comma))
1449 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001450 } else if (Kind == OMPC_dist_schedule) {
1451 Arg.push_back(getOpenMPSimpleClauseType(
1452 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1453 KLoc.push_back(Tok.getLocation());
1454 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1455 Tok.isNot(tok::annot_pragma_openmp_end))
1456 ConsumeAnyToken();
1457 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1458 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001459 } else if (Kind == OMPC_defaultmap) {
1460 // Get a defaultmap modifier
1461 Arg.push_back(getOpenMPSimpleClauseType(
1462 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1463 KLoc.push_back(Tok.getLocation());
1464 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1465 Tok.isNot(tok::annot_pragma_openmp_end))
1466 ConsumeAnyToken();
1467 // Parse ':'
1468 if (Tok.is(tok::colon))
1469 ConsumeAnyToken();
1470 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1471 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1472 // Get a defaultmap kind
1473 Arg.push_back(getOpenMPSimpleClauseType(
1474 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1475 KLoc.push_back(Tok.getLocation());
1476 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1477 Tok.isNot(tok::annot_pragma_openmp_end))
1478 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001479 } else {
1480 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001481 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001482 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001483 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1484 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001485 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001486 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1487 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001488 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001489 } else {
1490 TPA.Revert();
1491 Arg.back() = OMPD_unknown;
1492 }
1493 } else
1494 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001495 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001496
Carlo Bertollib4adf552016-01-15 18:50:31 +00001497 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1498 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1499 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001500 if (NeedAnExpression) {
1501 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001502 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1503 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001504 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001505 }
1506
1507 // Parse ')'.
1508 T.consumeClose();
1509
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001510 if (NeedAnExpression && Val.isInvalid())
1511 return nullptr;
1512
Alexey Bataev56dafe82014-06-20 07:16:17 +00001513 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001514 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001515 T.getCloseLocation());
1516}
1517
Alexey Bataevc5e02582014-06-16 07:08:35 +00001518static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1519 UnqualifiedId &ReductionId) {
1520 SourceLocation TemplateKWLoc;
1521 if (ReductionIdScopeSpec.isEmpty()) {
1522 auto OOK = OO_None;
1523 switch (P.getCurToken().getKind()) {
1524 case tok::plus:
1525 OOK = OO_Plus;
1526 break;
1527 case tok::minus:
1528 OOK = OO_Minus;
1529 break;
1530 case tok::star:
1531 OOK = OO_Star;
1532 break;
1533 case tok::amp:
1534 OOK = OO_Amp;
1535 break;
1536 case tok::pipe:
1537 OOK = OO_Pipe;
1538 break;
1539 case tok::caret:
1540 OOK = OO_Caret;
1541 break;
1542 case tok::ampamp:
1543 OOK = OO_AmpAmp;
1544 break;
1545 case tok::pipepipe:
1546 OOK = OO_PipePipe;
1547 break;
1548 default:
1549 break;
1550 }
1551 if (OOK != OO_None) {
1552 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001553 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001554 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1555 return false;
1556 }
1557 }
1558 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1559 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001560 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001561 TemplateKWLoc, ReductionId);
1562}
1563
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001564/// Parses clauses with list.
1565bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1566 OpenMPClauseKind Kind,
1567 SmallVectorImpl<Expr *> &Vars,
1568 OpenMPVarListDataTy &Data) {
1569 UnqualifiedId UnqualifiedReductionId;
1570 bool InvalidReductionId = false;
1571 bool MapTypeModifierSpecified = false;
1572
1573 // Parse '('.
1574 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1575 if (T.expectAndConsume(diag::err_expected_lparen_after,
1576 getOpenMPClauseName(Kind)))
1577 return true;
1578
1579 bool NeedRParenForLinear = false;
1580 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1581 tok::annot_pragma_openmp_end);
1582 // Handle reduction-identifier for reduction clause.
1583 if (Kind == OMPC_reduction) {
1584 ColonProtectionRAIIObject ColonRAII(*this);
1585 if (getLangOpts().CPlusPlus)
1586 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1587 /*ObjectType=*/nullptr,
1588 /*EnteringContext=*/false);
1589 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1590 UnqualifiedReductionId);
1591 if (InvalidReductionId) {
1592 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1593 StopBeforeMatch);
1594 }
1595 if (Tok.is(tok::colon))
1596 Data.ColonLoc = ConsumeToken();
1597 else
1598 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1599 if (!InvalidReductionId)
1600 Data.ReductionId =
1601 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1602 } else if (Kind == OMPC_depend) {
1603 // Handle dependency type for depend clause.
1604 ColonProtectionRAIIObject ColonRAII(*this);
1605 Data.DepKind =
1606 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1607 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1608 Data.DepLinMapLoc = Tok.getLocation();
1609
1610 if (Data.DepKind == OMPC_DEPEND_unknown) {
1611 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1612 StopBeforeMatch);
1613 } else {
1614 ConsumeToken();
1615 // Special processing for depend(source) clause.
1616 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1617 // Parse ')'.
1618 T.consumeClose();
1619 return false;
1620 }
1621 }
1622 if (Tok.is(tok::colon))
1623 Data.ColonLoc = ConsumeToken();
1624 else {
1625 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1626 : diag::warn_pragma_expected_colon)
1627 << "dependency type";
1628 }
1629 } else if (Kind == OMPC_linear) {
1630 // Try to parse modifier if any.
1631 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1632 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1633 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1634 Data.DepLinMapLoc = ConsumeToken();
1635 LinearT.consumeOpen();
1636 NeedRParenForLinear = true;
1637 }
1638 } else if (Kind == OMPC_map) {
1639 // Handle map type for map clause.
1640 ColonProtectionRAIIObject ColonRAII(*this);
1641
1642 /// The map clause modifier token can be either a identifier or the C++
1643 /// delete keyword.
1644 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1645 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1646 };
1647
1648 // The first identifier may be a list item, a map-type or a
1649 // map-type-modifier. The map modifier can also be delete which has the same
1650 // spelling of the C++ delete keyword.
1651 Data.MapType =
1652 IsMapClauseModifierToken(Tok)
1653 ? static_cast<OpenMPMapClauseKind>(
1654 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1655 : OMPC_MAP_unknown;
1656 Data.DepLinMapLoc = Tok.getLocation();
1657 bool ColonExpected = false;
1658
1659 if (IsMapClauseModifierToken(Tok)) {
1660 if (PP.LookAhead(0).is(tok::colon)) {
1661 if (Data.MapType == OMPC_MAP_unknown)
1662 Diag(Tok, diag::err_omp_unknown_map_type);
1663 else if (Data.MapType == OMPC_MAP_always)
1664 Diag(Tok, diag::err_omp_map_type_missing);
1665 ConsumeToken();
1666 } else if (PP.LookAhead(0).is(tok::comma)) {
1667 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1668 PP.LookAhead(2).is(tok::colon)) {
1669 Data.MapTypeModifier = Data.MapType;
1670 if (Data.MapTypeModifier != OMPC_MAP_always) {
1671 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1672 Data.MapTypeModifier = OMPC_MAP_unknown;
1673 } else
1674 MapTypeModifierSpecified = true;
1675
1676 ConsumeToken();
1677 ConsumeToken();
1678
1679 Data.MapType =
1680 IsMapClauseModifierToken(Tok)
1681 ? static_cast<OpenMPMapClauseKind>(
1682 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1683 : OMPC_MAP_unknown;
1684 if (Data.MapType == OMPC_MAP_unknown ||
1685 Data.MapType == OMPC_MAP_always)
1686 Diag(Tok, diag::err_omp_unknown_map_type);
1687 ConsumeToken();
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 } else {
1697 Data.MapType = OMPC_MAP_tofrom;
1698 Data.IsMapTypeImplicit = true;
1699 }
1700
1701 if (Tok.is(tok::colon))
1702 Data.ColonLoc = ConsumeToken();
1703 else if (ColonExpected)
1704 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1705 }
1706
1707 bool IsComma =
1708 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1709 (Kind == OMPC_reduction && !InvalidReductionId) ||
1710 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1711 (!MapTypeModifierSpecified ||
1712 Data.MapTypeModifier == OMPC_MAP_always)) ||
1713 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1714 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1715 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1716 Tok.isNot(tok::annot_pragma_openmp_end))) {
1717 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1718 // Parse variable
1719 ExprResult VarExpr =
1720 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1721 if (VarExpr.isUsable())
1722 Vars.push_back(VarExpr.get());
1723 else {
1724 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1725 StopBeforeMatch);
1726 }
1727 // Skip ',' if any
1728 IsComma = Tok.is(tok::comma);
1729 if (IsComma)
1730 ConsumeToken();
1731 else if (Tok.isNot(tok::r_paren) &&
1732 Tok.isNot(tok::annot_pragma_openmp_end) &&
1733 (!MayHaveTail || Tok.isNot(tok::colon)))
1734 Diag(Tok, diag::err_omp_expected_punc)
1735 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1736 : getOpenMPClauseName(Kind))
1737 << (Kind == OMPC_flush);
1738 }
1739
1740 // Parse ')' for linear clause with modifier.
1741 if (NeedRParenForLinear)
1742 LinearT.consumeClose();
1743
1744 // Parse ':' linear-step (or ':' alignment).
1745 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1746 if (MustHaveTail) {
1747 Data.ColonLoc = Tok.getLocation();
1748 SourceLocation ELoc = ConsumeToken();
1749 ExprResult Tail = ParseAssignmentExpression();
1750 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1751 if (Tail.isUsable())
1752 Data.TailExpr = Tail.get();
1753 else
1754 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1755 StopBeforeMatch);
1756 }
1757
1758 // Parse ')'.
1759 T.consumeClose();
1760 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1761 Vars.empty()) ||
1762 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1763 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1764 return true;
1765 return false;
1766}
1767
Alexander Musman1bb328c2014-06-04 13:06:39 +00001768/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001769/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001770///
1771/// private-clause:
1772/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001773/// firstprivate-clause:
1774/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001775/// lastprivate-clause:
1776/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001777/// shared-clause:
1778/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001779/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001780/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001781/// aligned-clause:
1782/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001783/// reduction-clause:
1784/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001785/// copyprivate-clause:
1786/// 'copyprivate' '(' list ')'
1787/// flush-clause:
1788/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001789/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001790/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001791/// map-clause:
1792/// 'map' '(' [ [ always , ]
1793/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001794/// to-clause:
1795/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001796/// from-clause:
1797/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001798/// use_device_ptr-clause:
1799/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001800/// is_device_ptr-clause:
1801/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001802///
Alexey Bataev182227b2015-08-20 10:54:39 +00001803/// For 'linear' clause linear-list may have the following forms:
1804/// list
1805/// modifier(list)
1806/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001807OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1808 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001809 SourceLocation Loc = Tok.getLocation();
1810 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001811 SmallVector<Expr *, 4> Vars;
1812 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001813
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001814 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001815 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001816
Alexey Bataevc5e02582014-06-16 07:08:35 +00001817 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001818 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1819 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1820 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1821 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001822}
1823