blob: 061721dfb8da2acdcc9c4ed6829b29d054400530 [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 },
122 { OMPD_target_teams_distribute_parallel, OMPD_for, OMPD_target_teams_distribute_parallel_for }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000123 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000124 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000125 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000126 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000127 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000128 ? static_cast<unsigned>(OMPD_unknown)
129 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
130 if (DKind == OMPD_unknown)
131 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000132
Alexander Musmanf82886e2014-09-18 05:12:34 +0000133 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000134 if (DKind != F[i][0])
135 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000136
Dmitry Polukhin82478332016-02-13 06:53:38 +0000137 Tok = P.getPreprocessor().LookAhead(0);
138 unsigned SDKind =
139 Tok.isAnnotation()
140 ? static_cast<unsigned>(OMPD_unknown)
141 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
142 if (SDKind == OMPD_unknown)
143 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000144
Dmitry Polukhin82478332016-02-13 06:53:38 +0000145 if (SDKind == F[i][1]) {
146 P.ConsumeToken();
147 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000148 }
149 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000150 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
151 : OMPD_unknown;
152}
153
154static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000155 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000156 Sema &Actions = P.getActions();
157 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000158 // Allow to use 'operator' keyword for C++ operators
159 bool WithOperator = false;
160 if (Tok.is(tok::kw_operator)) {
161 P.ConsumeToken();
162 Tok = P.getCurToken();
163 WithOperator = true;
164 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000165 switch (Tok.getKind()) {
166 case tok::plus: // '+'
167 OOK = OO_Plus;
168 break;
169 case tok::minus: // '-'
170 OOK = OO_Minus;
171 break;
172 case tok::star: // '*'
173 OOK = OO_Star;
174 break;
175 case tok::amp: // '&'
176 OOK = OO_Amp;
177 break;
178 case tok::pipe: // '|'
179 OOK = OO_Pipe;
180 break;
181 case tok::caret: // '^'
182 OOK = OO_Caret;
183 break;
184 case tok::ampamp: // '&&'
185 OOK = OO_AmpAmp;
186 break;
187 case tok::pipepipe: // '||'
188 OOK = OO_PipePipe;
189 break;
190 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000191 if (!WithOperator)
192 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000193 default:
194 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
195 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
196 Parser::StopBeforeMatch);
197 return DeclarationName();
198 }
199 P.ConsumeToken();
200 auto &DeclNames = Actions.getASTContext().DeclarationNames;
201 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
202 : DeclNames.getCXXOperatorName(OOK);
203}
204
205/// \brief Parse 'omp declare reduction' construct.
206///
207/// declare-reduction-directive:
208/// annot_pragma_openmp 'declare' 'reduction'
209/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
210/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
211/// annot_pragma_openmp_end
212/// <reduction_id> is either a base language identifier or one of the following
213/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
214///
215Parser::DeclGroupPtrTy
216Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
217 // Parse '('.
218 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
219 if (T.expectAndConsume(diag::err_expected_lparen_after,
220 getOpenMPDirectiveName(OMPD_declare_reduction))) {
221 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
222 return DeclGroupPtrTy();
223 }
224
225 DeclarationName Name = parseOpenMPReductionId(*this);
226 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
227 return DeclGroupPtrTy();
228
229 // Consume ':'.
230 bool IsCorrect = !ExpectAndConsume(tok::colon);
231
232 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
233 return DeclGroupPtrTy();
234
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000235 IsCorrect = IsCorrect && !Name.isEmpty();
236
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000237 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
238 Diag(Tok.getLocation(), diag::err_expected_type);
239 IsCorrect = false;
240 }
241
242 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
243 return DeclGroupPtrTy();
244
245 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
246 // Parse list of types until ':' token.
247 do {
248 ColonProtectionRAIIObject ColonRAII(*this);
249 SourceRange Range;
250 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
251 if (TR.isUsable()) {
252 auto ReductionType =
253 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
254 if (!ReductionType.isNull()) {
255 ReductionTypes.push_back(
256 std::make_pair(ReductionType, Range.getBegin()));
257 }
258 } else {
259 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
260 StopBeforeMatch);
261 }
262
263 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
264 break;
265
266 // Consume ','.
267 if (ExpectAndConsume(tok::comma)) {
268 IsCorrect = false;
269 if (Tok.is(tok::annot_pragma_openmp_end)) {
270 Diag(Tok.getLocation(), diag::err_expected_type);
271 return DeclGroupPtrTy();
272 }
273 }
274 } while (Tok.isNot(tok::annot_pragma_openmp_end));
275
276 if (ReductionTypes.empty()) {
277 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
278 return DeclGroupPtrTy();
279 }
280
281 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
282 return DeclGroupPtrTy();
283
284 // Consume ':'.
285 if (ExpectAndConsume(tok::colon))
286 IsCorrect = false;
287
288 if (Tok.is(tok::annot_pragma_openmp_end)) {
289 Diag(Tok.getLocation(), diag::err_expected_expression);
290 return DeclGroupPtrTy();
291 }
292
293 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
294 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
295
296 // Parse <combiner> expression and then parse initializer if any for each
297 // correct type.
298 unsigned I = 0, E = ReductionTypes.size();
299 for (auto *D : DRD.get()) {
300 TentativeParsingAction TPA(*this);
301 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
302 Scope::OpenMPDirectiveScope);
303 // Parse <combiner> expression.
304 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
305 ExprResult CombinerResult =
306 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
307 D->getLocation(), /*DiscardedValue=*/true);
308 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
309
310 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
311 Tok.isNot(tok::annot_pragma_openmp_end)) {
312 TPA.Commit();
313 IsCorrect = false;
314 break;
315 }
316 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
317 ExprResult InitializerResult;
318 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
319 // Parse <initializer> expression.
320 if (Tok.is(tok::identifier) &&
321 Tok.getIdentifierInfo()->isStr("initializer"))
322 ConsumeToken();
323 else {
324 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
325 TPA.Commit();
326 IsCorrect = false;
327 break;
328 }
329 // Parse '('.
330 BalancedDelimiterTracker T(*this, tok::l_paren,
331 tok::annot_pragma_openmp_end);
332 IsCorrect =
333 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
334 IsCorrect;
335 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
336 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
337 Scope::OpenMPDirectiveScope);
338 // Parse expression.
339 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
340 InitializerResult = Actions.ActOnFinishFullExpr(
341 ParseAssignmentExpression().get(), D->getLocation(),
342 /*DiscardedValue=*/true);
343 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
344 D, InitializerResult.get());
345 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
346 Tok.isNot(tok::annot_pragma_openmp_end)) {
347 TPA.Commit();
348 IsCorrect = false;
349 break;
350 }
351 IsCorrect =
352 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
353 }
354 }
355
356 ++I;
357 // Revert parsing if not the last type, otherwise accept it, we're done with
358 // parsing.
359 if (I != E)
360 TPA.Revert();
361 else
362 TPA.Commit();
363 }
364 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
365 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000366}
367
Alexey Bataev2af33e32016-04-07 12:45:37 +0000368namespace {
369/// RAII that recreates function context for correct parsing of clauses of
370/// 'declare simd' construct.
371/// OpenMP, 2.8.2 declare simd Construct
372/// The expressions appearing in the clauses of this directive are evaluated in
373/// the scope of the arguments of the function declaration or definition.
374class FNContextRAII final {
375 Parser &P;
376 Sema::CXXThisScopeRAII *ThisScope;
377 Parser::ParseScope *TempScope;
378 Parser::ParseScope *FnScope;
379 bool HasTemplateScope = false;
380 bool HasFunScope = false;
381 FNContextRAII() = delete;
382 FNContextRAII(const FNContextRAII &) = delete;
383 FNContextRAII &operator=(const FNContextRAII &) = delete;
384
385public:
386 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
387 Decl *D = *Ptr.get().begin();
388 NamedDecl *ND = dyn_cast<NamedDecl>(D);
389 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
390 Sema &Actions = P.getActions();
391
392 // Allow 'this' within late-parsed attributes.
393 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
394 ND && ND->isCXXInstanceMember());
395
396 // If the Decl is templatized, add template parameters to scope.
397 HasTemplateScope = D->isTemplateDecl();
398 TempScope =
399 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
400 if (HasTemplateScope)
401 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
402
403 // If the Decl is on a function, add function parameters to the scope.
404 HasFunScope = D->isFunctionOrFunctionTemplate();
405 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
406 HasFunScope);
407 if (HasFunScope)
408 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
409 }
410 ~FNContextRAII() {
411 if (HasFunScope) {
412 P.getActions().ActOnExitFunctionContext();
413 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
414 }
415 if (HasTemplateScope)
416 TempScope->Exit();
417 delete FnScope;
418 delete TempScope;
419 delete ThisScope;
420 }
421};
422} // namespace
423
Alexey Bataevd93d3762016-04-12 09:35:56 +0000424/// Parses clauses for 'declare simd' directive.
425/// clause:
426/// 'inbranch' | 'notinbranch'
427/// 'simdlen' '(' <expr> ')'
428/// { 'uniform' '(' <argument_list> ')' }
429/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000430/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
431static bool parseDeclareSimdClauses(
432 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
433 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
434 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
435 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000436 SourceRange BSRange;
437 const Token &Tok = P.getCurToken();
438 bool IsError = false;
439 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
440 if (Tok.isNot(tok::identifier))
441 break;
442 OMPDeclareSimdDeclAttr::BranchStateTy Out;
443 IdentifierInfo *II = Tok.getIdentifierInfo();
444 StringRef ClauseName = II->getName();
445 // Parse 'inranch|notinbranch' clauses.
446 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
447 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
448 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
449 << ClauseName
450 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
451 IsError = true;
452 }
453 BS = Out;
454 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
455 P.ConsumeToken();
456 } else if (ClauseName.equals("simdlen")) {
457 if (SimdLen.isUsable()) {
458 P.Diag(Tok, diag::err_omp_more_one_clause)
459 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
460 IsError = true;
461 }
462 P.ConsumeToken();
463 SourceLocation RLoc;
464 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
465 if (SimdLen.isInvalid())
466 IsError = true;
467 } else {
468 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000469 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
470 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000471 Parser::OpenMPVarListDataTy Data;
472 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000473 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000474 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000475 else if (CKind == OMPC_linear)
476 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000477
478 P.ConsumeToken();
479 if (P.ParseOpenMPVarList(OMPD_declare_simd,
480 getOpenMPClauseKind(ClauseName), *Vars, Data))
481 IsError = true;
482 if (CKind == OMPC_aligned)
483 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000484 else if (CKind == OMPC_linear) {
485 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
486 Data.DepLinMapLoc))
487 Data.LinKind = OMPC_LINEAR_val;
488 LinModifiers.append(Linears.size() - LinModifiers.size(),
489 Data.LinKind);
490 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
491 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000492 } else
493 // TODO: add parsing of other clauses.
494 break;
495 }
496 // Skip ',' if any.
497 if (Tok.is(tok::comma))
498 P.ConsumeToken();
499 }
500 return IsError;
501}
502
Alexey Bataev2af33e32016-04-07 12:45:37 +0000503/// Parse clauses for '#pragma omp declare simd'.
504Parser::DeclGroupPtrTy
505Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
506 CachedTokens &Toks, SourceLocation Loc) {
507 PP.EnterToken(Tok);
508 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
509 // Consume the previously pushed token.
510 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
511
512 FNContextRAII FnContext(*this, Ptr);
513 OMPDeclareSimdDeclAttr::BranchStateTy BS =
514 OMPDeclareSimdDeclAttr::BS_Undefined;
515 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000516 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000517 SmallVector<Expr *, 4> Aligneds;
518 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000519 SmallVector<Expr *, 4> Linears;
520 SmallVector<unsigned, 4> LinModifiers;
521 SmallVector<Expr *, 4> Steps;
522 bool IsError =
523 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
524 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000525 // Need to check for extra tokens.
526 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
527 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
528 << getOpenMPDirectiveName(OMPD_declare_simd);
529 while (Tok.isNot(tok::annot_pragma_openmp_end))
530 ConsumeAnyToken();
531 }
532 // Skip the last annot_pragma_openmp_end.
533 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000534 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000535 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000536 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
537 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000538 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000539 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000540}
541
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000542/// \brief Parsing of declarative OpenMP directives.
543///
544/// threadprivate-directive:
545/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000546/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000547///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000548/// declare-reduction-directive:
549/// annot_pragma_openmp 'declare' 'reduction' [...]
550/// annot_pragma_openmp_end
551///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000552/// declare-simd-directive:
553/// annot_pragma_openmp 'declare simd' {<clause> [,]}
554/// annot_pragma_openmp_end
555/// <function declaration/definition>
556///
557Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
558 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
559 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000560 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000561 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000562
563 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000564 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000565
566 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000567 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000568 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000569 ThreadprivateListParserHelper Helper(this);
570 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000571 // The last seen token is annot_pragma_openmp_end - need to check for
572 // extra tokens.
573 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
574 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000575 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000576 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000577 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000578 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000579 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000580 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
581 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000582 }
583 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000584 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000585 case OMPD_declare_reduction:
586 ConsumeToken();
587 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
588 // The last seen token is annot_pragma_openmp_end - need to check for
589 // extra tokens.
590 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
591 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
592 << getOpenMPDirectiveName(OMPD_declare_reduction);
593 while (Tok.isNot(tok::annot_pragma_openmp_end))
594 ConsumeAnyToken();
595 }
596 // Skip the last annot_pragma_openmp_end.
597 ConsumeToken();
598 return Res;
599 }
600 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000601 case OMPD_declare_simd: {
602 // The syntax is:
603 // { #pragma omp declare simd }
604 // <function-declaration-or-definition>
605 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000606 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000607 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000608 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
609 Toks.push_back(Tok);
610 ConsumeAnyToken();
611 }
612 Toks.push_back(Tok);
613 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000614
615 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000616 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000617 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000618 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000619 // Here we expect to see some function declaration.
620 if (AS == AS_none) {
621 assert(TagType == DeclSpec::TST_unspecified);
622 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000623 ParsingDeclSpec PDS(*this);
624 Ptr = ParseExternalDeclaration(Attrs, &PDS);
625 } else {
626 Ptr =
627 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
628 }
629 }
630 if (!Ptr) {
631 Diag(Loc, diag::err_omp_decl_in_declare_simd);
632 return DeclGroupPtrTy();
633 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000634 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000635 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000636 case OMPD_declare_target: {
637 SourceLocation DTLoc = ConsumeAnyToken();
638 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000639 // OpenMP 4.5 syntax with list of entities.
640 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
641 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
642 OMPDeclareTargetDeclAttr::MapTypeTy MT =
643 OMPDeclareTargetDeclAttr::MT_To;
644 if (Tok.is(tok::identifier)) {
645 IdentifierInfo *II = Tok.getIdentifierInfo();
646 StringRef ClauseName = II->getName();
647 // Parse 'to|link' clauses.
648 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
649 MT)) {
650 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
651 << ClauseName;
652 break;
653 }
654 ConsumeToken();
655 }
656 auto Callback = [this, MT, &SameDirectiveDecls](
657 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
658 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
659 SameDirectiveDecls);
660 };
661 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
662 break;
663
664 // Consume optional ','.
665 if (Tok.is(tok::comma))
666 ConsumeToken();
667 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000669 ConsumeAnyToken();
670 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000671 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000672
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000673 // Skip the last annot_pragma_openmp_end.
674 ConsumeAnyToken();
675
676 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
677 return DeclGroupPtrTy();
678
679 DKind = ParseOpenMPDirectiveKind(*this);
680 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
681 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
682 ParsedAttributesWithRange attrs(AttrFactory);
683 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000684 ParseExternalDeclaration(attrs);
685 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
686 TentativeParsingAction TPA(*this);
687 ConsumeToken();
688 DKind = ParseOpenMPDirectiveKind(*this);
689 if (DKind != OMPD_end_declare_target)
690 TPA.Revert();
691 else
692 TPA.Commit();
693 }
694 }
695
696 if (DKind == OMPD_end_declare_target) {
697 ConsumeAnyToken();
698 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
699 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
700 << getOpenMPDirectiveName(OMPD_end_declare_target);
701 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
702 }
703 // Skip the last annot_pragma_openmp_end.
704 ConsumeAnyToken();
705 } else {
706 Diag(Tok, diag::err_expected_end_declare_target);
707 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
708 }
709 Actions.ActOnFinishOpenMPDeclareTargetDirective();
710 return DeclGroupPtrTy();
711 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000712 case OMPD_unknown:
713 Diag(Tok, diag::err_omp_unknown_directive);
714 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000715 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000716 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000718 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000719 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000720 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000721 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000722 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000723 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000724 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000725 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000726 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000727 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000728 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000729 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000730 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000731 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000732 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000733 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000734 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000735 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000736 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000737 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000738 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000739 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000740 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000741 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000742 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000743 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000744 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000745 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000746 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000747 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000748 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000749 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000750 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000751 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000752 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000753 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000754 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000755 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000756 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000757 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000758 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000759 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +0000760 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataeva769e072013-03-22 06:34:35 +0000761 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000762 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000763 break;
764 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000765 while (Tok.isNot(tok::annot_pragma_openmp_end))
766 ConsumeAnyToken();
767 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000768 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000769}
770
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771/// \brief Parsing of declarative or executable OpenMP directives.
772///
773/// threadprivate-directive:
774/// annot_pragma_openmp 'threadprivate' simple-variable-list
775/// annot_pragma_openmp_end
776///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000777/// declare-reduction-directive:
778/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
779/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
780/// ('omp_priv' '=' <expression>|<function_call>) ')']
781/// annot_pragma_openmp_end
782///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000783/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000784/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
786/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000787/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000788/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000789/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000790/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000791/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000792/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000793/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000794/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000795/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000796/// 'teams distribute parallel for simd' |
Kelvin Li80e8f562016-12-29 22:16:30 +0000797/// 'teams distribute parallel for' | 'target teams' |
798/// 'target teams distribute' |
799/// 'target teams distribute parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000800/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000802StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
803 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000804 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000805 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000806 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000807 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000808 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000809 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000810 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000812 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000813 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000814 // Name of critical directive.
815 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000816 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000817 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000818 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000819
820 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000821 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000822 if (Allowed != ACK_Any) {
823 Diag(Tok, diag::err_omp_immediate_directive)
824 << getOpenMPDirectiveName(DKind) << 0;
825 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000826 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000827 ThreadprivateListParserHelper Helper(this);
828 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000829 // The last seen token is annot_pragma_openmp_end - need to check for
830 // extra tokens.
831 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
832 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000833 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000834 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000835 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000836 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
837 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000838 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
839 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000840 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000841 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000842 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000843 case OMPD_declare_reduction:
844 ConsumeToken();
845 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
846 // The last seen token is annot_pragma_openmp_end - need to check for
847 // extra tokens.
848 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
849 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
850 << getOpenMPDirectiveName(OMPD_declare_reduction);
851 while (Tok.isNot(tok::annot_pragma_openmp_end))
852 ConsumeAnyToken();
853 }
854 ConsumeAnyToken();
855 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
856 } else
857 SkipUntil(tok::annot_pragma_openmp_end);
858 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000859 case OMPD_flush:
860 if (PP.LookAhead(0).is(tok::l_paren)) {
861 FlushHasClause = true;
862 // Push copy of the current token back to stream to properly parse
863 // pseudo-clause OMPFlushClause.
864 PP.EnterToken(Tok);
865 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000866 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000867 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000868 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000869 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000870 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000871 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000872 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000873 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000874 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000875 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000876 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000877 }
878 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000879 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000880 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000881 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000882 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000883 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000884 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000885 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000886 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000887 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000888 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000889 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000890 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000891 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000892 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000893 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000894 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000895 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000896 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000897 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000898 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000899 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000900 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000901 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000902 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000903 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000904 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000905 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000906 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000907 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000908 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000909 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000910 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000911 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +0000912 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +0000913 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +0000914 case OMPD_target_teams_distribute:
915 case OMPD_target_teams_distribute_parallel_for: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000917 // Parse directive name of the 'critical' directive if any.
918 if (DKind == OMPD_critical) {
919 BalancedDelimiterTracker T(*this, tok::l_paren,
920 tok::annot_pragma_openmp_end);
921 if (!T.consumeOpen()) {
922 if (Tok.isAnyIdentifier()) {
923 DirName =
924 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
925 ConsumeAnyToken();
926 } else {
927 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
928 }
929 T.consumeClose();
930 }
Alexey Bataev80909872015-07-02 11:25:17 +0000931 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000932 CancelRegion = ParseOpenMPDirectiveKind(*this);
933 if (Tok.isNot(tok::annot_pragma_openmp_end))
934 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000935 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000936
Alexey Bataevf29276e2014-06-18 04:14:57 +0000937 if (isOpenMPLoopDirective(DKind))
938 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
939 if (isOpenMPSimdDirective(DKind))
940 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
941 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000942 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000944 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000945 OpenMPClauseKind CKind =
946 Tok.isAnnotation()
947 ? OMPC_unknown
948 : FlushHasClause ? OMPC_flush
949 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000950 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000951 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000952 OMPClause *Clause =
953 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954 FirstClauses[CKind].setInt(true);
955 if (Clause) {
956 FirstClauses[CKind].setPointer(Clause);
957 Clauses.push_back(Clause);
958 }
959
960 // Skip ',' if any.
961 if (Tok.is(tok::comma))
962 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000963 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000964 }
965 // End location of the directive.
966 EndLoc = Tok.getLocation();
967 // Consume final annot_pragma_openmp_end.
968 ConsumeToken();
969
Alexey Bataeveb482352015-12-18 05:05:56 +0000970 // OpenMP [2.13.8, ordered Construct, Syntax]
971 // If the depend clause is specified, the ordered construct is a stand-alone
972 // directive.
973 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000974 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000975 Diag(Loc, diag::err_omp_immediate_directive)
976 << getOpenMPDirectiveName(DKind) << 1
977 << getOpenMPClauseName(OMPC_depend);
978 }
979 HasAssociatedStatement = false;
980 }
981
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000983 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000984 // The body is a block scope like in Lambdas and Blocks.
985 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000986 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000987 Actions.ActOnStartOfCompoundStmt();
988 // Parse statement
989 AssociatedStmt = ParseStatement();
990 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000991 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000993 Directive = Actions.ActOnOpenMPExecutableDirective(
994 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
995 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000996
997 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000998 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000999 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001001 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001002 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001003 case OMPD_declare_target:
1004 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001005 Diag(Tok, diag::err_omp_unexpected_directive)
1006 << getOpenMPDirectiveName(DKind);
1007 SkipUntil(tok::annot_pragma_openmp_end);
1008 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001009 case OMPD_unknown:
1010 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001011 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001012 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001013 }
1014 return Directive;
1015}
1016
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001017// Parses simple list:
1018// simple-variable-list:
1019// '(' id-expression {, id-expression} ')'
1020//
1021bool Parser::ParseOpenMPSimpleVarList(
1022 OpenMPDirectiveKind Kind,
1023 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1024 Callback,
1025 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001026 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001027 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001028 if (T.expectAndConsume(diag::err_expected_lparen_after,
1029 getOpenMPDirectiveName(Kind)))
1030 return true;
1031 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001032 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001033
1034 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001036 CXXScopeSpec SS;
1037 SourceLocation TemplateKWLoc;
1038 UnqualifiedId Name;
1039 // Read var name.
1040 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001042
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001044 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001045 IsCorrect = false;
1046 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001047 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001048 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001050 IsCorrect = false;
1051 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001052 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1054 Tok.isNot(tok::annot_pragma_openmp_end)) {
1055 IsCorrect = false;
1056 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001057 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001058 Diag(PrevTok.getLocation(), diag::err_expected)
1059 << tok::identifier
1060 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001061 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001062 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001063 }
1064 // Consume ','.
1065 if (Tok.is(tok::comma)) {
1066 ConsumeToken();
1067 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001068 }
1069
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001070 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001071 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001072 IsCorrect = false;
1073 }
1074
1075 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001077
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001078 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001079}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080
1081/// \brief Parsing of OpenMP clauses.
1082///
1083/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001084/// if-clause | final-clause | num_threads-clause | safelen-clause |
1085/// default-clause | private-clause | firstprivate-clause | shared-clause
1086/// | linear-clause | aligned-clause | collapse-clause |
1087/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001088/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001089/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001090/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001091/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001092/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001093/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001094/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001095///
1096OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1097 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001098 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001099 bool ErrorFound = false;
1100 // Check if clause is allowed for the given directive.
1101 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001102 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1103 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001104 ErrorFound = true;
1105 }
1106
1107 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001108 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001109 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001110 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001111 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001112 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001113 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001114 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001115 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001116 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001117 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001118 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001119 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001120 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001121 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001122 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001123 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001124 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001125 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001126 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001127 // OpenMP [2.9.1, target data construct, Restrictions]
1128 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001129 // OpenMP [2.11.1, task Construct, Restrictions]
1130 // At most one if clause can appear on the directive.
1131 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001132 // OpenMP [teams Construct, Restrictions]
1133 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001134 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001135 // OpenMP [2.9.1, task Construct, Restrictions]
1136 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001137 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1138 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001139 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1140 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001141 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001142 Diag(Tok, diag::err_omp_more_one_clause)
1143 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001144 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001145 }
1146
Alexey Bataev10e775f2015-07-30 11:36:16 +00001147 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1148 Clause = ParseOpenMPClause(CKind);
1149 else
1150 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001151 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001152 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001153 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001154 // OpenMP [2.14.3.1, Restrictions]
1155 // Only a single default clause may be specified on a parallel, task or
1156 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001157 // OpenMP [2.5, parallel Construct, Restrictions]
1158 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001159 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001160 Diag(Tok, diag::err_omp_more_one_clause)
1161 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001162 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001163 }
1164
1165 Clause = ParseOpenMPSimpleClause(CKind);
1166 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001167 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001168 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001169 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001170 // OpenMP [2.7.1, Restrictions, p. 3]
1171 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001172 // OpenMP [2.10.4, Restrictions, p. 106]
1173 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001174 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001175 Diag(Tok, diag::err_omp_more_one_clause)
1176 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001177 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001178 }
1179
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001180 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001181 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1182 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001183 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001184 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001185 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001186 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001187 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001188 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001189 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001190 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001191 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001192 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001193 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001194 // OpenMP [2.7.1, Restrictions, p. 9]
1195 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001196 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1197 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001198 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001199 Diag(Tok, diag::err_omp_more_one_clause)
1200 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001201 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001202 }
1203
1204 Clause = ParseOpenMPClause(CKind);
1205 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001207 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001208 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001209 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001210 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001211 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001212 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001213 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001214 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001215 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001216 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001217 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001218 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001219 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001220 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001221 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001222 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001223 break;
1224 case OMPC_unknown:
1225 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001226 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001227 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001228 break;
1229 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001230 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001231 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1232 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001233 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001234 break;
1235 }
Craig Topper161e4db2014-05-21 06:02:52 +00001236 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001237}
1238
Alexey Bataev2af33e32016-04-07 12:45:37 +00001239/// Parses simple expression in parens for single-expression clauses of OpenMP
1240/// constructs.
1241/// \param RLoc Returned location of right paren.
1242ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1243 SourceLocation &RLoc) {
1244 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1245 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1246 return ExprError();
1247
1248 SourceLocation ELoc = Tok.getLocation();
1249 ExprResult LHS(ParseCastExpression(
1250 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1251 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1252 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1253
1254 // Parse ')'.
1255 T.consumeClose();
1256
1257 RLoc = T.getCloseLocation();
1258 return Val;
1259}
1260
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001261/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001262/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001263/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001264///
Alexey Bataev3778b602014-07-17 07:32:53 +00001265/// final-clause:
1266/// 'final' '(' expression ')'
1267///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001268/// num_threads-clause:
1269/// 'num_threads' '(' expression ')'
1270///
1271/// safelen-clause:
1272/// 'safelen' '(' expression ')'
1273///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001274/// simdlen-clause:
1275/// 'simdlen' '(' expression ')'
1276///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001277/// collapse-clause:
1278/// 'collapse' '(' expression ')'
1279///
Alexey Bataeva0569352015-12-01 10:17:31 +00001280/// priority-clause:
1281/// 'priority' '(' expression ')'
1282///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001283/// grainsize-clause:
1284/// 'grainsize' '(' expression ')'
1285///
Alexey Bataev382967a2015-12-08 12:06:20 +00001286/// num_tasks-clause:
1287/// 'num_tasks' '(' expression ')'
1288///
Alexey Bataev28c75412015-12-15 08:19:24 +00001289/// hint-clause:
1290/// 'hint' '(' expression ')'
1291///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001292OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1293 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001294 SourceLocation LLoc = Tok.getLocation();
1295 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001296
Alexey Bataev2af33e32016-04-07 12:45:37 +00001297 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001298
1299 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001300 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001301
Alexey Bataev2af33e32016-04-07 12:45:37 +00001302 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001303}
1304
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001305/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001306///
1307/// default-clause:
1308/// 'default' '(' 'none' | 'shared' ')
1309///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001310/// proc_bind-clause:
1311/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1312///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001313OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1314 SourceLocation Loc = Tok.getLocation();
1315 SourceLocation LOpen = ConsumeToken();
1316 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001317 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001318 if (T.expectAndConsume(diag::err_expected_lparen_after,
1319 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001320 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001321
Alexey Bataeva55ed262014-05-28 06:15:33 +00001322 unsigned Type = getOpenMPSimpleClauseType(
1323 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001324 SourceLocation TypeLoc = Tok.getLocation();
1325 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1326 Tok.isNot(tok::annot_pragma_openmp_end))
1327 ConsumeAnyToken();
1328
1329 // Parse ')'.
1330 T.consumeClose();
1331
1332 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1333 Tok.getLocation());
1334}
1335
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001336/// \brief Parsing of OpenMP clauses like 'ordered'.
1337///
1338/// ordered-clause:
1339/// 'ordered'
1340///
Alexey Bataev236070f2014-06-20 11:19:47 +00001341/// nowait-clause:
1342/// 'nowait'
1343///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001344/// untied-clause:
1345/// 'untied'
1346///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001347/// mergeable-clause:
1348/// 'mergeable'
1349///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001350/// read-clause:
1351/// 'read'
1352///
Alexey Bataev346265e2015-09-25 10:37:12 +00001353/// threads-clause:
1354/// 'threads'
1355///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001356/// simd-clause:
1357/// 'simd'
1358///
Alexey Bataevb825de12015-12-07 10:51:44 +00001359/// nogroup-clause:
1360/// 'nogroup'
1361///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001362OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1363 SourceLocation Loc = Tok.getLocation();
1364 ConsumeAnyToken();
1365
1366 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1367}
1368
1369
Alexey Bataev56dafe82014-06-20 07:16:17 +00001370/// \brief Parsing of OpenMP clauses with single expressions and some additional
1371/// argument like 'schedule' or 'dist_schedule'.
1372///
1373/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001374/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1375/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001376///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001377/// if-clause:
1378/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1379///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001380/// defaultmap:
1381/// 'defaultmap' '(' modifier ':' kind ')'
1382///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001383OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1384 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001385 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001386 // Parse '('.
1387 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1388 if (T.expectAndConsume(diag::err_expected_lparen_after,
1389 getOpenMPClauseName(Kind)))
1390 return nullptr;
1391
1392 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001393 SmallVector<unsigned, 4> Arg;
1394 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001395 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001396 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1397 Arg.resize(NumberOfElements);
1398 KLoc.resize(NumberOfElements);
1399 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1400 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1401 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1402 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001403 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001404 if (KindModifier > OMPC_SCHEDULE_unknown) {
1405 // Parse 'modifier'
1406 Arg[Modifier1] = KindModifier;
1407 KLoc[Modifier1] = Tok.getLocation();
1408 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1409 Tok.isNot(tok::annot_pragma_openmp_end))
1410 ConsumeAnyToken();
1411 if (Tok.is(tok::comma)) {
1412 // Parse ',' 'modifier'
1413 ConsumeAnyToken();
1414 KindModifier = getOpenMPSimpleClauseType(
1415 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1416 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1417 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001418 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001419 KLoc[Modifier2] = Tok.getLocation();
1420 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1421 Tok.isNot(tok::annot_pragma_openmp_end))
1422 ConsumeAnyToken();
1423 }
1424 // Parse ':'
1425 if (Tok.is(tok::colon))
1426 ConsumeAnyToken();
1427 else
1428 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1429 KindModifier = getOpenMPSimpleClauseType(
1430 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1431 }
1432 Arg[ScheduleKind] = KindModifier;
1433 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001434 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1435 Tok.isNot(tok::annot_pragma_openmp_end))
1436 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001437 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1438 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1439 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001440 Tok.is(tok::comma))
1441 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001442 } else if (Kind == OMPC_dist_schedule) {
1443 Arg.push_back(getOpenMPSimpleClauseType(
1444 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1445 KLoc.push_back(Tok.getLocation());
1446 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1447 Tok.isNot(tok::annot_pragma_openmp_end))
1448 ConsumeAnyToken();
1449 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1450 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001451 } else if (Kind == OMPC_defaultmap) {
1452 // Get a defaultmap modifier
1453 Arg.push_back(getOpenMPSimpleClauseType(
1454 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1455 KLoc.push_back(Tok.getLocation());
1456 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1457 Tok.isNot(tok::annot_pragma_openmp_end))
1458 ConsumeAnyToken();
1459 // Parse ':'
1460 if (Tok.is(tok::colon))
1461 ConsumeAnyToken();
1462 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1463 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1464 // Get a defaultmap kind
1465 Arg.push_back(getOpenMPSimpleClauseType(
1466 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1467 KLoc.push_back(Tok.getLocation());
1468 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1469 Tok.isNot(tok::annot_pragma_openmp_end))
1470 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001471 } else {
1472 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001473 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001474 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001475 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1476 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001477 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001478 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1479 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001480 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001481 } else {
1482 TPA.Revert();
1483 Arg.back() = OMPD_unknown;
1484 }
1485 } else
1486 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001487 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001488
Carlo Bertollib4adf552016-01-15 18:50:31 +00001489 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1490 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1491 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001492 if (NeedAnExpression) {
1493 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001494 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1495 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001496 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001497 }
1498
1499 // Parse ')'.
1500 T.consumeClose();
1501
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001502 if (NeedAnExpression && Val.isInvalid())
1503 return nullptr;
1504
Alexey Bataev56dafe82014-06-20 07:16:17 +00001505 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001506 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001507 T.getCloseLocation());
1508}
1509
Alexey Bataevc5e02582014-06-16 07:08:35 +00001510static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1511 UnqualifiedId &ReductionId) {
1512 SourceLocation TemplateKWLoc;
1513 if (ReductionIdScopeSpec.isEmpty()) {
1514 auto OOK = OO_None;
1515 switch (P.getCurToken().getKind()) {
1516 case tok::plus:
1517 OOK = OO_Plus;
1518 break;
1519 case tok::minus:
1520 OOK = OO_Minus;
1521 break;
1522 case tok::star:
1523 OOK = OO_Star;
1524 break;
1525 case tok::amp:
1526 OOK = OO_Amp;
1527 break;
1528 case tok::pipe:
1529 OOK = OO_Pipe;
1530 break;
1531 case tok::caret:
1532 OOK = OO_Caret;
1533 break;
1534 case tok::ampamp:
1535 OOK = OO_AmpAmp;
1536 break;
1537 case tok::pipepipe:
1538 OOK = OO_PipePipe;
1539 break;
1540 default:
1541 break;
1542 }
1543 if (OOK != OO_None) {
1544 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001545 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001546 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1547 return false;
1548 }
1549 }
1550 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1551 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001552 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001553 TemplateKWLoc, ReductionId);
1554}
1555
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001556/// Parses clauses with list.
1557bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1558 OpenMPClauseKind Kind,
1559 SmallVectorImpl<Expr *> &Vars,
1560 OpenMPVarListDataTy &Data) {
1561 UnqualifiedId UnqualifiedReductionId;
1562 bool InvalidReductionId = false;
1563 bool MapTypeModifierSpecified = false;
1564
1565 // Parse '('.
1566 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1567 if (T.expectAndConsume(diag::err_expected_lparen_after,
1568 getOpenMPClauseName(Kind)))
1569 return true;
1570
1571 bool NeedRParenForLinear = false;
1572 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1573 tok::annot_pragma_openmp_end);
1574 // Handle reduction-identifier for reduction clause.
1575 if (Kind == OMPC_reduction) {
1576 ColonProtectionRAIIObject ColonRAII(*this);
1577 if (getLangOpts().CPlusPlus)
1578 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1579 /*ObjectType=*/nullptr,
1580 /*EnteringContext=*/false);
1581 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1582 UnqualifiedReductionId);
1583 if (InvalidReductionId) {
1584 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1585 StopBeforeMatch);
1586 }
1587 if (Tok.is(tok::colon))
1588 Data.ColonLoc = ConsumeToken();
1589 else
1590 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1591 if (!InvalidReductionId)
1592 Data.ReductionId =
1593 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1594 } else if (Kind == OMPC_depend) {
1595 // Handle dependency type for depend clause.
1596 ColonProtectionRAIIObject ColonRAII(*this);
1597 Data.DepKind =
1598 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1599 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1600 Data.DepLinMapLoc = Tok.getLocation();
1601
1602 if (Data.DepKind == OMPC_DEPEND_unknown) {
1603 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1604 StopBeforeMatch);
1605 } else {
1606 ConsumeToken();
1607 // Special processing for depend(source) clause.
1608 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1609 // Parse ')'.
1610 T.consumeClose();
1611 return false;
1612 }
1613 }
1614 if (Tok.is(tok::colon))
1615 Data.ColonLoc = ConsumeToken();
1616 else {
1617 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1618 : diag::warn_pragma_expected_colon)
1619 << "dependency type";
1620 }
1621 } else if (Kind == OMPC_linear) {
1622 // Try to parse modifier if any.
1623 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1624 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1625 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1626 Data.DepLinMapLoc = ConsumeToken();
1627 LinearT.consumeOpen();
1628 NeedRParenForLinear = true;
1629 }
1630 } else if (Kind == OMPC_map) {
1631 // Handle map type for map clause.
1632 ColonProtectionRAIIObject ColonRAII(*this);
1633
1634 /// The map clause modifier token can be either a identifier or the C++
1635 /// delete keyword.
1636 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1637 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1638 };
1639
1640 // The first identifier may be a list item, a map-type or a
1641 // map-type-modifier. The map modifier can also be delete which has the same
1642 // spelling of the C++ delete keyword.
1643 Data.MapType =
1644 IsMapClauseModifierToken(Tok)
1645 ? static_cast<OpenMPMapClauseKind>(
1646 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1647 : OMPC_MAP_unknown;
1648 Data.DepLinMapLoc = Tok.getLocation();
1649 bool ColonExpected = false;
1650
1651 if (IsMapClauseModifierToken(Tok)) {
1652 if (PP.LookAhead(0).is(tok::colon)) {
1653 if (Data.MapType == OMPC_MAP_unknown)
1654 Diag(Tok, diag::err_omp_unknown_map_type);
1655 else if (Data.MapType == OMPC_MAP_always)
1656 Diag(Tok, diag::err_omp_map_type_missing);
1657 ConsumeToken();
1658 } else if (PP.LookAhead(0).is(tok::comma)) {
1659 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1660 PP.LookAhead(2).is(tok::colon)) {
1661 Data.MapTypeModifier = Data.MapType;
1662 if (Data.MapTypeModifier != OMPC_MAP_always) {
1663 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1664 Data.MapTypeModifier = OMPC_MAP_unknown;
1665 } else
1666 MapTypeModifierSpecified = true;
1667
1668 ConsumeToken();
1669 ConsumeToken();
1670
1671 Data.MapType =
1672 IsMapClauseModifierToken(Tok)
1673 ? static_cast<OpenMPMapClauseKind>(
1674 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1675 : OMPC_MAP_unknown;
1676 if (Data.MapType == OMPC_MAP_unknown ||
1677 Data.MapType == OMPC_MAP_always)
1678 Diag(Tok, diag::err_omp_unknown_map_type);
1679 ConsumeToken();
1680 } else {
1681 Data.MapType = OMPC_MAP_tofrom;
1682 Data.IsMapTypeImplicit = true;
1683 }
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
1693 if (Tok.is(tok::colon))
1694 Data.ColonLoc = ConsumeToken();
1695 else if (ColonExpected)
1696 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1697 }
1698
1699 bool IsComma =
1700 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1701 (Kind == OMPC_reduction && !InvalidReductionId) ||
1702 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1703 (!MapTypeModifierSpecified ||
1704 Data.MapTypeModifier == OMPC_MAP_always)) ||
1705 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1706 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1707 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1708 Tok.isNot(tok::annot_pragma_openmp_end))) {
1709 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1710 // Parse variable
1711 ExprResult VarExpr =
1712 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1713 if (VarExpr.isUsable())
1714 Vars.push_back(VarExpr.get());
1715 else {
1716 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1717 StopBeforeMatch);
1718 }
1719 // Skip ',' if any
1720 IsComma = Tok.is(tok::comma);
1721 if (IsComma)
1722 ConsumeToken();
1723 else if (Tok.isNot(tok::r_paren) &&
1724 Tok.isNot(tok::annot_pragma_openmp_end) &&
1725 (!MayHaveTail || Tok.isNot(tok::colon)))
1726 Diag(Tok, diag::err_omp_expected_punc)
1727 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1728 : getOpenMPClauseName(Kind))
1729 << (Kind == OMPC_flush);
1730 }
1731
1732 // Parse ')' for linear clause with modifier.
1733 if (NeedRParenForLinear)
1734 LinearT.consumeClose();
1735
1736 // Parse ':' linear-step (or ':' alignment).
1737 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1738 if (MustHaveTail) {
1739 Data.ColonLoc = Tok.getLocation();
1740 SourceLocation ELoc = ConsumeToken();
1741 ExprResult Tail = ParseAssignmentExpression();
1742 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1743 if (Tail.isUsable())
1744 Data.TailExpr = Tail.get();
1745 else
1746 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1747 StopBeforeMatch);
1748 }
1749
1750 // Parse ')'.
1751 T.consumeClose();
1752 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1753 Vars.empty()) ||
1754 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1755 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1756 return true;
1757 return false;
1758}
1759
Alexander Musman1bb328c2014-06-04 13:06:39 +00001760/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001761/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001762///
1763/// private-clause:
1764/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001765/// firstprivate-clause:
1766/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001767/// lastprivate-clause:
1768/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001769/// shared-clause:
1770/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001771/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001772/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001773/// aligned-clause:
1774/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001775/// reduction-clause:
1776/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001777/// copyprivate-clause:
1778/// 'copyprivate' '(' list ')'
1779/// flush-clause:
1780/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001781/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001782/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001783/// map-clause:
1784/// 'map' '(' [ [ always , ]
1785/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001786/// to-clause:
1787/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001788/// from-clause:
1789/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001790/// use_device_ptr-clause:
1791/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001792/// is_device_ptr-clause:
1793/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001794///
Alexey Bataev182227b2015-08-20 10:54:39 +00001795/// For 'linear' clause linear-list may have the following forms:
1796/// list
1797/// modifier(list)
1798/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001799OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1800 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001801 SourceLocation Loc = Tok.getLocation();
1802 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001803 SmallVector<Expr *, 4> Vars;
1804 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001805
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001806 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001807 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001808
Alexey Bataevc5e02582014-06-16 07:08:35 +00001809 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001810 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1811 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1812 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1813 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001814}
1815