blob: f7a6049d56ea179b35aec8f0d9ad538c2d3baefd [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 Li7ade93f2016-12-09 03:24:30 +000043 OMPD_teams_distribute_parallel
Dmitry Polukhin82478332016-02-13 06:53:38 +000044};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000045
46class ThreadprivateListParserHelper final {
47 SmallVector<Expr *, 4> Identifiers;
48 Parser *P;
49
50public:
51 ThreadprivateListParserHelper(Parser *P) : P(P) {}
52 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
53 ExprResult Res =
54 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
55 if (Res.isUsable())
56 Identifiers.push_back(Res.get());
57 }
58 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
59};
Dmitry Polukhin82478332016-02-13 06:53:38 +000060} // namespace
61
62// Map token string to extended OMP token kind that are
63// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
64static unsigned getOpenMPDirectiveKindEx(StringRef S) {
65 auto DKind = getOpenMPDirectiveKind(S);
66 if (DKind != OMPD_unknown)
67 return DKind;
68
69 return llvm::StringSwitch<unsigned>(S)
70 .Case("cancellation", OMPD_cancellation)
71 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000072 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000073 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000074 .Case("enter", OMPD_enter)
75 .Case("exit", OMPD_exit)
76 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000077 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000078 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000079 .Default(OMPD_unknown);
80}
81
Alexey Bataev4acb8592014-07-07 13:01:15 +000082static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000083 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
84 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
85 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 static const unsigned F[][3] = {
87 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000088 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000089 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000090 { OMPD_declare, OMPD_target, OMPD_declare_target },
Carlo Bertolli9925f152016-06-27 14:55:37 +000091 { OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel },
92 { OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for },
Kelvin Li4a39add2016-07-05 05:00:15 +000093 { OMPD_distribute_parallel_for, OMPD_simd,
94 OMPD_distribute_parallel_for_simd },
Kelvin Li787f3fc2016-07-06 04:45:38 +000095 { OMPD_distribute, OMPD_simd, OMPD_distribute_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000096 { OMPD_end, OMPD_declare, OMPD_end_declare },
97 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000098 { OMPD_target, OMPD_data, OMPD_target_data },
99 { OMPD_target, OMPD_enter, OMPD_target_enter },
100 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +0000101 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +0000102 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
103 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
104 { OMPD_for, OMPD_simd, OMPD_for_simd },
105 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
106 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
107 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
108 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
109 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
Kelvin Li986330c2016-07-20 22:57:10 +0000110 { OMPD_target, OMPD_simd, OMPD_target_simd },
Kelvin Lia579b912016-07-14 02:54:56 +0000111 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for },
Kelvin Li02532872016-08-05 14:37:37 +0000112 { OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd },
Kelvin Li4e325f72016-10-25 12:50:55 +0000113 { OMPD_teams, OMPD_distribute, OMPD_teams_distribute },
Kelvin Li579e41c2016-11-30 23:51:03 +0000114 { OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd },
115 { OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel },
116 { OMPD_teams_distribute_parallel, OMPD_for, OMPD_teams_distribute_parallel_for },
Kelvin Libf594a52016-12-17 05:48:59 +0000117 { OMPD_teams_distribute_parallel_for, OMPD_simd, OMPD_teams_distribute_parallel_for_simd },
Kelvin Li83c451e2016-12-25 04:52:54 +0000118 { OMPD_target, OMPD_teams, OMPD_target_teams },
119 { OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000120 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000121 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000122 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000123 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000124 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000125 ? static_cast<unsigned>(OMPD_unknown)
126 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
127 if (DKind == OMPD_unknown)
128 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000129
Alexander Musmanf82886e2014-09-18 05:12:34 +0000130 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000131 if (DKind != F[i][0])
132 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000133
Dmitry Polukhin82478332016-02-13 06:53:38 +0000134 Tok = P.getPreprocessor().LookAhead(0);
135 unsigned SDKind =
136 Tok.isAnnotation()
137 ? static_cast<unsigned>(OMPD_unknown)
138 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
139 if (SDKind == OMPD_unknown)
140 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000141
Dmitry Polukhin82478332016-02-13 06:53:38 +0000142 if (SDKind == F[i][1]) {
143 P.ConsumeToken();
144 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000145 }
146 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000147 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
148 : OMPD_unknown;
149}
150
151static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000152 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000153 Sema &Actions = P.getActions();
154 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000155 // Allow to use 'operator' keyword for C++ operators
156 bool WithOperator = false;
157 if (Tok.is(tok::kw_operator)) {
158 P.ConsumeToken();
159 Tok = P.getCurToken();
160 WithOperator = true;
161 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000162 switch (Tok.getKind()) {
163 case tok::plus: // '+'
164 OOK = OO_Plus;
165 break;
166 case tok::minus: // '-'
167 OOK = OO_Minus;
168 break;
169 case tok::star: // '*'
170 OOK = OO_Star;
171 break;
172 case tok::amp: // '&'
173 OOK = OO_Amp;
174 break;
175 case tok::pipe: // '|'
176 OOK = OO_Pipe;
177 break;
178 case tok::caret: // '^'
179 OOK = OO_Caret;
180 break;
181 case tok::ampamp: // '&&'
182 OOK = OO_AmpAmp;
183 break;
184 case tok::pipepipe: // '||'
185 OOK = OO_PipePipe;
186 break;
187 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000188 if (!WithOperator)
189 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000190 default:
191 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
192 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
193 Parser::StopBeforeMatch);
194 return DeclarationName();
195 }
196 P.ConsumeToken();
197 auto &DeclNames = Actions.getASTContext().DeclarationNames;
198 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
199 : DeclNames.getCXXOperatorName(OOK);
200}
201
202/// \brief Parse 'omp declare reduction' construct.
203///
204/// declare-reduction-directive:
205/// annot_pragma_openmp 'declare' 'reduction'
206/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
207/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
208/// annot_pragma_openmp_end
209/// <reduction_id> is either a base language identifier or one of the following
210/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
211///
212Parser::DeclGroupPtrTy
213Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
214 // Parse '('.
215 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
216 if (T.expectAndConsume(diag::err_expected_lparen_after,
217 getOpenMPDirectiveName(OMPD_declare_reduction))) {
218 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
219 return DeclGroupPtrTy();
220 }
221
222 DeclarationName Name = parseOpenMPReductionId(*this);
223 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
224 return DeclGroupPtrTy();
225
226 // Consume ':'.
227 bool IsCorrect = !ExpectAndConsume(tok::colon);
228
229 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
230 return DeclGroupPtrTy();
231
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000232 IsCorrect = IsCorrect && !Name.isEmpty();
233
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000234 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
235 Diag(Tok.getLocation(), diag::err_expected_type);
236 IsCorrect = false;
237 }
238
239 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
240 return DeclGroupPtrTy();
241
242 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
243 // Parse list of types until ':' token.
244 do {
245 ColonProtectionRAIIObject ColonRAII(*this);
246 SourceRange Range;
247 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
248 if (TR.isUsable()) {
249 auto ReductionType =
250 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
251 if (!ReductionType.isNull()) {
252 ReductionTypes.push_back(
253 std::make_pair(ReductionType, Range.getBegin()));
254 }
255 } else {
256 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
257 StopBeforeMatch);
258 }
259
260 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
261 break;
262
263 // Consume ','.
264 if (ExpectAndConsume(tok::comma)) {
265 IsCorrect = false;
266 if (Tok.is(tok::annot_pragma_openmp_end)) {
267 Diag(Tok.getLocation(), diag::err_expected_type);
268 return DeclGroupPtrTy();
269 }
270 }
271 } while (Tok.isNot(tok::annot_pragma_openmp_end));
272
273 if (ReductionTypes.empty()) {
274 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
275 return DeclGroupPtrTy();
276 }
277
278 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
279 return DeclGroupPtrTy();
280
281 // Consume ':'.
282 if (ExpectAndConsume(tok::colon))
283 IsCorrect = false;
284
285 if (Tok.is(tok::annot_pragma_openmp_end)) {
286 Diag(Tok.getLocation(), diag::err_expected_expression);
287 return DeclGroupPtrTy();
288 }
289
290 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
291 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
292
293 // Parse <combiner> expression and then parse initializer if any for each
294 // correct type.
295 unsigned I = 0, E = ReductionTypes.size();
296 for (auto *D : DRD.get()) {
297 TentativeParsingAction TPA(*this);
298 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
299 Scope::OpenMPDirectiveScope);
300 // Parse <combiner> expression.
301 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
302 ExprResult CombinerResult =
303 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
304 D->getLocation(), /*DiscardedValue=*/true);
305 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
306
307 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
308 Tok.isNot(tok::annot_pragma_openmp_end)) {
309 TPA.Commit();
310 IsCorrect = false;
311 break;
312 }
313 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
314 ExprResult InitializerResult;
315 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
316 // Parse <initializer> expression.
317 if (Tok.is(tok::identifier) &&
318 Tok.getIdentifierInfo()->isStr("initializer"))
319 ConsumeToken();
320 else {
321 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
322 TPA.Commit();
323 IsCorrect = false;
324 break;
325 }
326 // Parse '('.
327 BalancedDelimiterTracker T(*this, tok::l_paren,
328 tok::annot_pragma_openmp_end);
329 IsCorrect =
330 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
331 IsCorrect;
332 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
333 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
334 Scope::OpenMPDirectiveScope);
335 // Parse expression.
336 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
337 InitializerResult = Actions.ActOnFinishFullExpr(
338 ParseAssignmentExpression().get(), D->getLocation(),
339 /*DiscardedValue=*/true);
340 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
341 D, InitializerResult.get());
342 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
343 Tok.isNot(tok::annot_pragma_openmp_end)) {
344 TPA.Commit();
345 IsCorrect = false;
346 break;
347 }
348 IsCorrect =
349 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
350 }
351 }
352
353 ++I;
354 // Revert parsing if not the last type, otherwise accept it, we're done with
355 // parsing.
356 if (I != E)
357 TPA.Revert();
358 else
359 TPA.Commit();
360 }
361 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
362 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000363}
364
Alexey Bataev2af33e32016-04-07 12:45:37 +0000365namespace {
366/// RAII that recreates function context for correct parsing of clauses of
367/// 'declare simd' construct.
368/// OpenMP, 2.8.2 declare simd Construct
369/// The expressions appearing in the clauses of this directive are evaluated in
370/// the scope of the arguments of the function declaration or definition.
371class FNContextRAII final {
372 Parser &P;
373 Sema::CXXThisScopeRAII *ThisScope;
374 Parser::ParseScope *TempScope;
375 Parser::ParseScope *FnScope;
376 bool HasTemplateScope = false;
377 bool HasFunScope = false;
378 FNContextRAII() = delete;
379 FNContextRAII(const FNContextRAII &) = delete;
380 FNContextRAII &operator=(const FNContextRAII &) = delete;
381
382public:
383 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
384 Decl *D = *Ptr.get().begin();
385 NamedDecl *ND = dyn_cast<NamedDecl>(D);
386 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
387 Sema &Actions = P.getActions();
388
389 // Allow 'this' within late-parsed attributes.
390 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
391 ND && ND->isCXXInstanceMember());
392
393 // If the Decl is templatized, add template parameters to scope.
394 HasTemplateScope = D->isTemplateDecl();
395 TempScope =
396 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
397 if (HasTemplateScope)
398 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
399
400 // If the Decl is on a function, add function parameters to the scope.
401 HasFunScope = D->isFunctionOrFunctionTemplate();
402 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
403 HasFunScope);
404 if (HasFunScope)
405 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
406 }
407 ~FNContextRAII() {
408 if (HasFunScope) {
409 P.getActions().ActOnExitFunctionContext();
410 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
411 }
412 if (HasTemplateScope)
413 TempScope->Exit();
414 delete FnScope;
415 delete TempScope;
416 delete ThisScope;
417 }
418};
419} // namespace
420
Alexey Bataevd93d3762016-04-12 09:35:56 +0000421/// Parses clauses for 'declare simd' directive.
422/// clause:
423/// 'inbranch' | 'notinbranch'
424/// 'simdlen' '(' <expr> ')'
425/// { 'uniform' '(' <argument_list> ')' }
426/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000427/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
428static bool parseDeclareSimdClauses(
429 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
430 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
431 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
432 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000433 SourceRange BSRange;
434 const Token &Tok = P.getCurToken();
435 bool IsError = false;
436 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
437 if (Tok.isNot(tok::identifier))
438 break;
439 OMPDeclareSimdDeclAttr::BranchStateTy Out;
440 IdentifierInfo *II = Tok.getIdentifierInfo();
441 StringRef ClauseName = II->getName();
442 // Parse 'inranch|notinbranch' clauses.
443 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
444 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
445 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
446 << ClauseName
447 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
448 IsError = true;
449 }
450 BS = Out;
451 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
452 P.ConsumeToken();
453 } else if (ClauseName.equals("simdlen")) {
454 if (SimdLen.isUsable()) {
455 P.Diag(Tok, diag::err_omp_more_one_clause)
456 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
457 IsError = true;
458 }
459 P.ConsumeToken();
460 SourceLocation RLoc;
461 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
462 if (SimdLen.isInvalid())
463 IsError = true;
464 } else {
465 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000466 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
467 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000468 Parser::OpenMPVarListDataTy Data;
469 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000470 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000471 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000472 else if (CKind == OMPC_linear)
473 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000474
475 P.ConsumeToken();
476 if (P.ParseOpenMPVarList(OMPD_declare_simd,
477 getOpenMPClauseKind(ClauseName), *Vars, Data))
478 IsError = true;
479 if (CKind == OMPC_aligned)
480 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000481 else if (CKind == OMPC_linear) {
482 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
483 Data.DepLinMapLoc))
484 Data.LinKind = OMPC_LINEAR_val;
485 LinModifiers.append(Linears.size() - LinModifiers.size(),
486 Data.LinKind);
487 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
488 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000489 } else
490 // TODO: add parsing of other clauses.
491 break;
492 }
493 // Skip ',' if any.
494 if (Tok.is(tok::comma))
495 P.ConsumeToken();
496 }
497 return IsError;
498}
499
Alexey Bataev2af33e32016-04-07 12:45:37 +0000500/// Parse clauses for '#pragma omp declare simd'.
501Parser::DeclGroupPtrTy
502Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
503 CachedTokens &Toks, SourceLocation Loc) {
504 PP.EnterToken(Tok);
505 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
506 // Consume the previously pushed token.
507 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
508
509 FNContextRAII FnContext(*this, Ptr);
510 OMPDeclareSimdDeclAttr::BranchStateTy BS =
511 OMPDeclareSimdDeclAttr::BS_Undefined;
512 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000513 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000514 SmallVector<Expr *, 4> Aligneds;
515 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000516 SmallVector<Expr *, 4> Linears;
517 SmallVector<unsigned, 4> LinModifiers;
518 SmallVector<Expr *, 4> Steps;
519 bool IsError =
520 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
521 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000522 // Need to check for extra tokens.
523 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
524 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
525 << getOpenMPDirectiveName(OMPD_declare_simd);
526 while (Tok.isNot(tok::annot_pragma_openmp_end))
527 ConsumeAnyToken();
528 }
529 // Skip the last annot_pragma_openmp_end.
530 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000531 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000532 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000533 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
534 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000535 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000536 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000537}
538
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000539/// \brief Parsing of declarative OpenMP directives.
540///
541/// threadprivate-directive:
542/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000543/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000544///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000545/// declare-reduction-directive:
546/// annot_pragma_openmp 'declare' 'reduction' [...]
547/// annot_pragma_openmp_end
548///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000549/// declare-simd-directive:
550/// annot_pragma_openmp 'declare simd' {<clause> [,]}
551/// annot_pragma_openmp_end
552/// <function declaration/definition>
553///
554Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
555 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
556 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000557 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000558 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000559
560 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000561 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000562
563 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000564 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000565 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000566 ThreadprivateListParserHelper Helper(this);
567 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000568 // The last seen token is annot_pragma_openmp_end - need to check for
569 // extra tokens.
570 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
571 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000572 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000573 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000574 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000575 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000576 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000577 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
578 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000579 }
580 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000581 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000582 case OMPD_declare_reduction:
583 ConsumeToken();
584 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
585 // The last seen token is annot_pragma_openmp_end - need to check for
586 // extra tokens.
587 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
588 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
589 << getOpenMPDirectiveName(OMPD_declare_reduction);
590 while (Tok.isNot(tok::annot_pragma_openmp_end))
591 ConsumeAnyToken();
592 }
593 // Skip the last annot_pragma_openmp_end.
594 ConsumeToken();
595 return Res;
596 }
597 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000598 case OMPD_declare_simd: {
599 // The syntax is:
600 // { #pragma omp declare simd }
601 // <function-declaration-or-definition>
602 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000603 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000604 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000605 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
606 Toks.push_back(Tok);
607 ConsumeAnyToken();
608 }
609 Toks.push_back(Tok);
610 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000611
612 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000613 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000614 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000615 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000616 // Here we expect to see some function declaration.
617 if (AS == AS_none) {
618 assert(TagType == DeclSpec::TST_unspecified);
619 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000620 ParsingDeclSpec PDS(*this);
621 Ptr = ParseExternalDeclaration(Attrs, &PDS);
622 } else {
623 Ptr =
624 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
625 }
626 }
627 if (!Ptr) {
628 Diag(Loc, diag::err_omp_decl_in_declare_simd);
629 return DeclGroupPtrTy();
630 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000631 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000632 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000633 case OMPD_declare_target: {
634 SourceLocation DTLoc = ConsumeAnyToken();
635 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000636 // OpenMP 4.5 syntax with list of entities.
637 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
638 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
639 OMPDeclareTargetDeclAttr::MapTypeTy MT =
640 OMPDeclareTargetDeclAttr::MT_To;
641 if (Tok.is(tok::identifier)) {
642 IdentifierInfo *II = Tok.getIdentifierInfo();
643 StringRef ClauseName = II->getName();
644 // Parse 'to|link' clauses.
645 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
646 MT)) {
647 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
648 << ClauseName;
649 break;
650 }
651 ConsumeToken();
652 }
653 auto Callback = [this, MT, &SameDirectiveDecls](
654 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
655 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
656 SameDirectiveDecls);
657 };
658 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
659 break;
660
661 // Consume optional ','.
662 if (Tok.is(tok::comma))
663 ConsumeToken();
664 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000665 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000666 ConsumeAnyToken();
667 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000669
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000670 // Skip the last annot_pragma_openmp_end.
671 ConsumeAnyToken();
672
673 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
674 return DeclGroupPtrTy();
675
676 DKind = ParseOpenMPDirectiveKind(*this);
677 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
678 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
679 ParsedAttributesWithRange attrs(AttrFactory);
680 MaybeParseCXX11Attributes(attrs);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000681 ParseExternalDeclaration(attrs);
682 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
683 TentativeParsingAction TPA(*this);
684 ConsumeToken();
685 DKind = ParseOpenMPDirectiveKind(*this);
686 if (DKind != OMPD_end_declare_target)
687 TPA.Revert();
688 else
689 TPA.Commit();
690 }
691 }
692
693 if (DKind == OMPD_end_declare_target) {
694 ConsumeAnyToken();
695 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
696 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
697 << getOpenMPDirectiveName(OMPD_end_declare_target);
698 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
699 }
700 // Skip the last annot_pragma_openmp_end.
701 ConsumeAnyToken();
702 } else {
703 Diag(Tok, diag::err_expected_end_declare_target);
704 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
705 }
706 Actions.ActOnFinishOpenMPDeclareTargetDirective();
707 return DeclGroupPtrTy();
708 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000709 case OMPD_unknown:
710 Diag(Tok, diag::err_omp_unknown_directive);
711 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000712 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000713 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000714 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000715 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000716 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000717 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000718 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000719 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000720 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000721 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000722 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000723 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000724 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000725 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000726 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000727 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000728 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000729 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000730 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000731 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000732 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000733 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000734 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000735 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000736 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000737 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000738 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000739 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000740 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000741 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000742 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000743 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000744 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000745 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000746 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +0000747 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000748 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000749 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000750 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000751 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +0000752 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +0000753 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000754 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +0000755 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +0000756 case OMPD_target_teams_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000757 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000758 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000759 break;
760 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000761 while (Tok.isNot(tok::annot_pragma_openmp_end))
762 ConsumeAnyToken();
763 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000764 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000765}
766
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000767/// \brief Parsing of declarative or executable OpenMP directives.
768///
769/// threadprivate-directive:
770/// annot_pragma_openmp 'threadprivate' simple-variable-list
771/// annot_pragma_openmp_end
772///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000773/// declare-reduction-directive:
774/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
775/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
776/// ('omp_priv' '=' <expression>|<function_call>) ')']
777/// annot_pragma_openmp_end
778///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000779/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000780/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000781/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
782/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000783/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000784/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000785/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000786/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000787/// 'target parallel' | 'target parallel for' |
Kelvin Li4a39add2016-07-05 05:00:15 +0000788/// 'target update' | 'distribute parallel for' |
Kelvin Lia579b912016-07-14 02:54:56 +0000789/// 'distribute paralle for simd' | 'distribute simd' |
Kelvin Li02532872016-08-05 14:37:37 +0000790/// 'target parallel for simd' | 'target simd' |
Kelvin Li579e41c2016-11-30 23:51:03 +0000791/// 'teams distribute' | 'teams distribute simd' |
Kelvin Li7ade93f2016-12-09 03:24:30 +0000792/// 'teams distribute parallel for simd' |
Kelvin Li83c451e2016-12-25 04:52:54 +0000793/// 'teams distribute parallel for' | 'target teams'
794/// 'target teams distribute' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000795/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000796///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000797StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
798 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000799 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000800 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000802 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000803 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000804 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000805 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000806 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000807 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000808 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000809 // Name of critical directive.
810 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000812 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000813 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000814
815 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000816 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000817 if (Allowed != ACK_Any) {
818 Diag(Tok, diag::err_omp_immediate_directive)
819 << getOpenMPDirectiveName(DKind) << 0;
820 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000822 ThreadprivateListParserHelper Helper(this);
823 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000824 // The last seen token is annot_pragma_openmp_end - need to check for
825 // extra tokens.
826 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
827 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000828 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000829 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000830 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000831 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
832 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000833 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
834 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000835 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000836 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000837 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000838 case OMPD_declare_reduction:
839 ConsumeToken();
840 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
841 // The last seen token is annot_pragma_openmp_end - need to check for
842 // extra tokens.
843 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
844 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
845 << getOpenMPDirectiveName(OMPD_declare_reduction);
846 while (Tok.isNot(tok::annot_pragma_openmp_end))
847 ConsumeAnyToken();
848 }
849 ConsumeAnyToken();
850 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
851 } else
852 SkipUntil(tok::annot_pragma_openmp_end);
853 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000854 case OMPD_flush:
855 if (PP.LookAhead(0).is(tok::l_paren)) {
856 FlushHasClause = true;
857 // Push copy of the current token back to stream to properly parse
858 // pseudo-clause OMPFlushClause.
859 PP.EnterToken(Tok);
860 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000861 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000862 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000863 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000864 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000865 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000866 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000867 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000868 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000869 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000870 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000871 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000872 }
873 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000874 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000875 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000876 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000877 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000878 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000879 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000880 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000881 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000882 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000883 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000884 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000885 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000886 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000887 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000888 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000889 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000890 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000891 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000892 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000893 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000894 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000895 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000896 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000897 case OMPD_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +0000898 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +0000899 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +0000900 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +0000901 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +0000902 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +0000903 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +0000904 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +0000905 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +0000906 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +0000907 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +0000908 case OMPD_target_teams:
909 case OMPD_target_teams_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000910 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000911 // Parse directive name of the 'critical' directive if any.
912 if (DKind == OMPD_critical) {
913 BalancedDelimiterTracker T(*this, tok::l_paren,
914 tok::annot_pragma_openmp_end);
915 if (!T.consumeOpen()) {
916 if (Tok.isAnyIdentifier()) {
917 DirName =
918 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
919 ConsumeAnyToken();
920 } else {
921 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
922 }
923 T.consumeClose();
924 }
Alexey Bataev80909872015-07-02 11:25:17 +0000925 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000926 CancelRegion = ParseOpenMPDirectiveKind(*this);
927 if (Tok.isNot(tok::annot_pragma_openmp_end))
928 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000929 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930
Alexey Bataevf29276e2014-06-18 04:14:57 +0000931 if (isOpenMPLoopDirective(DKind))
932 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
933 if (isOpenMPSimdDirective(DKind))
934 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
935 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000936 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000937
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000938 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000939 OpenMPClauseKind CKind =
940 Tok.isAnnotation()
941 ? OMPC_unknown
942 : FlushHasClause ? OMPC_flush
943 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000944 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000945 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000946 OMPClause *Clause =
947 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000948 FirstClauses[CKind].setInt(true);
949 if (Clause) {
950 FirstClauses[CKind].setPointer(Clause);
951 Clauses.push_back(Clause);
952 }
953
954 // Skip ',' if any.
955 if (Tok.is(tok::comma))
956 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000957 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000958 }
959 // End location of the directive.
960 EndLoc = Tok.getLocation();
961 // Consume final annot_pragma_openmp_end.
962 ConsumeToken();
963
Alexey Bataeveb482352015-12-18 05:05:56 +0000964 // OpenMP [2.13.8, ordered Construct, Syntax]
965 // If the depend clause is specified, the ordered construct is a stand-alone
966 // directive.
967 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000968 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000969 Diag(Loc, diag::err_omp_immediate_directive)
970 << getOpenMPDirectiveName(DKind) << 1
971 << getOpenMPClauseName(OMPC_depend);
972 }
973 HasAssociatedStatement = false;
974 }
975
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000976 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000977 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000978 // The body is a block scope like in Lambdas and Blocks.
979 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000980 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981 Actions.ActOnStartOfCompoundStmt();
982 // Parse statement
983 AssociatedStmt = ParseStatement();
984 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000985 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000987 Directive = Actions.ActOnOpenMPExecutableDirective(
988 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
989 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000990
991 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000992 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000993 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000994 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000995 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000996 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000997 case OMPD_declare_target:
998 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000999 Diag(Tok, diag::err_omp_unexpected_directive)
1000 << getOpenMPDirectiveName(DKind);
1001 SkipUntil(tok::annot_pragma_openmp_end);
1002 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001003 case OMPD_unknown:
1004 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001005 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001006 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001007 }
1008 return Directive;
1009}
1010
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001011// Parses simple list:
1012// simple-variable-list:
1013// '(' id-expression {, id-expression} ')'
1014//
1015bool Parser::ParseOpenMPSimpleVarList(
1016 OpenMPDirectiveKind Kind,
1017 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1018 Callback,
1019 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001020 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001021 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001022 if (T.expectAndConsume(diag::err_expected_lparen_after,
1023 getOpenMPDirectiveName(Kind)))
1024 return true;
1025 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001026 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001027
1028 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001029 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001030 CXXScopeSpec SS;
1031 SourceLocation TemplateKWLoc;
1032 UnqualifiedId Name;
1033 // Read var name.
1034 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001036
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001037 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00001038 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001039 IsCorrect = false;
1040 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001041 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001042 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001044 IsCorrect = false;
1045 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001046 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001047 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1048 Tok.isNot(tok::annot_pragma_openmp_end)) {
1049 IsCorrect = false;
1050 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001051 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001052 Diag(PrevTok.getLocation(), diag::err_expected)
1053 << tok::identifier
1054 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001055 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001056 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001057 }
1058 // Consume ','.
1059 if (Tok.is(tok::comma)) {
1060 ConsumeToken();
1061 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001062 }
1063
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001064 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001065 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001066 IsCorrect = false;
1067 }
1068
1069 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001070 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001071
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001072 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001073}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001074
1075/// \brief Parsing of OpenMP clauses.
1076///
1077/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001078/// if-clause | final-clause | num_threads-clause | safelen-clause |
1079/// default-clause | private-clause | firstprivate-clause | shared-clause
1080/// | linear-clause | aligned-clause | collapse-clause |
1081/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001082/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001083/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001084/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001085/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001086/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00001087/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Carlo Bertolli70594e92016-07-13 17:16:49 +00001088/// from-clause | is_device_ptr-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089///
1090OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1091 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001092 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001093 bool ErrorFound = false;
1094 // Check if clause is allowed for the given directive.
1095 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001096 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1097 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098 ErrorFound = true;
1099 }
1100
1101 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001102 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001103 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001104 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001105 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001106 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001107 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001108 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001109 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001110 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001111 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001112 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001113 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001114 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001115 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001116 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001117 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001118 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001119 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001120 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001121 // OpenMP [2.9.1, target data construct, Restrictions]
1122 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001123 // OpenMP [2.11.1, task Construct, Restrictions]
1124 // At most one if clause can appear on the directive.
1125 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001126 // OpenMP [teams Construct, Restrictions]
1127 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001128 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001129 // OpenMP [2.9.1, task Construct, Restrictions]
1130 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001131 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1132 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001133 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1134 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001135 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001136 Diag(Tok, diag::err_omp_more_one_clause)
1137 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001138 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001139 }
1140
Alexey Bataev10e775f2015-07-30 11:36:16 +00001141 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1142 Clause = ParseOpenMPClause(CKind);
1143 else
1144 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001145 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001146 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001147 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001148 // OpenMP [2.14.3.1, Restrictions]
1149 // Only a single default clause may be specified on a parallel, task or
1150 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001151 // OpenMP [2.5, parallel Construct, Restrictions]
1152 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001153 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001154 Diag(Tok, diag::err_omp_more_one_clause)
1155 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001156 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001157 }
1158
1159 Clause = ParseOpenMPSimpleClause(CKind);
1160 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001161 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001162 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001163 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001164 // OpenMP [2.7.1, Restrictions, p. 3]
1165 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001166 // OpenMP [2.10.4, Restrictions, p. 106]
1167 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001168 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001169 Diag(Tok, diag::err_omp_more_one_clause)
1170 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001171 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001172 }
1173
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001174 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001175 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1176 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001177 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001178 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001179 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001180 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001181 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001182 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001183 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001184 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001185 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001186 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001187 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001188 // OpenMP [2.7.1, Restrictions, p. 9]
1189 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001190 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1191 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001192 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001193 Diag(Tok, diag::err_omp_more_one_clause)
1194 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001195 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001196 }
1197
1198 Clause = ParseOpenMPClause(CKind);
1199 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001201 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001202 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001203 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001204 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001205 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001206 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001207 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001208 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001209 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001210 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001211 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00001212 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00001213 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00001214 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00001215 case OMPC_is_device_ptr:
Alexey Bataeveb482352015-12-18 05:05:56 +00001216 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217 break;
1218 case OMPC_unknown:
1219 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001220 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001221 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001222 break;
1223 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001224 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001225 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1226 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001227 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001228 break;
1229 }
Craig Topper161e4db2014-05-21 06:02:52 +00001230 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001231}
1232
Alexey Bataev2af33e32016-04-07 12:45:37 +00001233/// Parses simple expression in parens for single-expression clauses of OpenMP
1234/// constructs.
1235/// \param RLoc Returned location of right paren.
1236ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1237 SourceLocation &RLoc) {
1238 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1239 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1240 return ExprError();
1241
1242 SourceLocation ELoc = Tok.getLocation();
1243 ExprResult LHS(ParseCastExpression(
1244 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1245 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1246 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1247
1248 // Parse ')'.
1249 T.consumeClose();
1250
1251 RLoc = T.getCloseLocation();
1252 return Val;
1253}
1254
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001255/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001256/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001257/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001258///
Alexey Bataev3778b602014-07-17 07:32:53 +00001259/// final-clause:
1260/// 'final' '(' expression ')'
1261///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001262/// num_threads-clause:
1263/// 'num_threads' '(' expression ')'
1264///
1265/// safelen-clause:
1266/// 'safelen' '(' expression ')'
1267///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001268/// simdlen-clause:
1269/// 'simdlen' '(' expression ')'
1270///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001271/// collapse-clause:
1272/// 'collapse' '(' expression ')'
1273///
Alexey Bataeva0569352015-12-01 10:17:31 +00001274/// priority-clause:
1275/// 'priority' '(' expression ')'
1276///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001277/// grainsize-clause:
1278/// 'grainsize' '(' expression ')'
1279///
Alexey Bataev382967a2015-12-08 12:06:20 +00001280/// num_tasks-clause:
1281/// 'num_tasks' '(' expression ')'
1282///
Alexey Bataev28c75412015-12-15 08:19:24 +00001283/// hint-clause:
1284/// 'hint' '(' expression ')'
1285///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001286OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1287 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001288 SourceLocation LLoc = Tok.getLocation();
1289 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001290
Alexey Bataev2af33e32016-04-07 12:45:37 +00001291 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001292
1293 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001294 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001295
Alexey Bataev2af33e32016-04-07 12:45:37 +00001296 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001297}
1298
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001299/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001300///
1301/// default-clause:
1302/// 'default' '(' 'none' | 'shared' ')
1303///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001304/// proc_bind-clause:
1305/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1306///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001307OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1308 SourceLocation Loc = Tok.getLocation();
1309 SourceLocation LOpen = ConsumeToken();
1310 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001311 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001312 if (T.expectAndConsume(diag::err_expected_lparen_after,
1313 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001314 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001315
Alexey Bataeva55ed262014-05-28 06:15:33 +00001316 unsigned Type = getOpenMPSimpleClauseType(
1317 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001318 SourceLocation TypeLoc = Tok.getLocation();
1319 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1320 Tok.isNot(tok::annot_pragma_openmp_end))
1321 ConsumeAnyToken();
1322
1323 // Parse ')'.
1324 T.consumeClose();
1325
1326 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1327 Tok.getLocation());
1328}
1329
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001330/// \brief Parsing of OpenMP clauses like 'ordered'.
1331///
1332/// ordered-clause:
1333/// 'ordered'
1334///
Alexey Bataev236070f2014-06-20 11:19:47 +00001335/// nowait-clause:
1336/// 'nowait'
1337///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001338/// untied-clause:
1339/// 'untied'
1340///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001341/// mergeable-clause:
1342/// 'mergeable'
1343///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001344/// read-clause:
1345/// 'read'
1346///
Alexey Bataev346265e2015-09-25 10:37:12 +00001347/// threads-clause:
1348/// 'threads'
1349///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001350/// simd-clause:
1351/// 'simd'
1352///
Alexey Bataevb825de12015-12-07 10:51:44 +00001353/// nogroup-clause:
1354/// 'nogroup'
1355///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001356OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1357 SourceLocation Loc = Tok.getLocation();
1358 ConsumeAnyToken();
1359
1360 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1361}
1362
1363
Alexey Bataev56dafe82014-06-20 07:16:17 +00001364/// \brief Parsing of OpenMP clauses with single expressions and some additional
1365/// argument like 'schedule' or 'dist_schedule'.
1366///
1367/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001368/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1369/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001370///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001371/// if-clause:
1372/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1373///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001374/// defaultmap:
1375/// 'defaultmap' '(' modifier ':' kind ')'
1376///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001377OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1378 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001379 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001380 // Parse '('.
1381 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1382 if (T.expectAndConsume(diag::err_expected_lparen_after,
1383 getOpenMPClauseName(Kind)))
1384 return nullptr;
1385
1386 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001387 SmallVector<unsigned, 4> Arg;
1388 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001389 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001390 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1391 Arg.resize(NumberOfElements);
1392 KLoc.resize(NumberOfElements);
1393 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1394 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1395 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1396 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001397 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001398 if (KindModifier > OMPC_SCHEDULE_unknown) {
1399 // Parse 'modifier'
1400 Arg[Modifier1] = KindModifier;
1401 KLoc[Modifier1] = Tok.getLocation();
1402 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1403 Tok.isNot(tok::annot_pragma_openmp_end))
1404 ConsumeAnyToken();
1405 if (Tok.is(tok::comma)) {
1406 // Parse ',' 'modifier'
1407 ConsumeAnyToken();
1408 KindModifier = getOpenMPSimpleClauseType(
1409 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1410 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1411 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001412 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001413 KLoc[Modifier2] = Tok.getLocation();
1414 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1415 Tok.isNot(tok::annot_pragma_openmp_end))
1416 ConsumeAnyToken();
1417 }
1418 // Parse ':'
1419 if (Tok.is(tok::colon))
1420 ConsumeAnyToken();
1421 else
1422 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1423 KindModifier = getOpenMPSimpleClauseType(
1424 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1425 }
1426 Arg[ScheduleKind] = KindModifier;
1427 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001428 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1429 Tok.isNot(tok::annot_pragma_openmp_end))
1430 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001431 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1432 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1433 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001434 Tok.is(tok::comma))
1435 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001436 } else if (Kind == OMPC_dist_schedule) {
1437 Arg.push_back(getOpenMPSimpleClauseType(
1438 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1439 KLoc.push_back(Tok.getLocation());
1440 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1441 Tok.isNot(tok::annot_pragma_openmp_end))
1442 ConsumeAnyToken();
1443 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1444 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001445 } else if (Kind == OMPC_defaultmap) {
1446 // Get a defaultmap modifier
1447 Arg.push_back(getOpenMPSimpleClauseType(
1448 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1449 KLoc.push_back(Tok.getLocation());
1450 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1451 Tok.isNot(tok::annot_pragma_openmp_end))
1452 ConsumeAnyToken();
1453 // Parse ':'
1454 if (Tok.is(tok::colon))
1455 ConsumeAnyToken();
1456 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1457 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1458 // Get a defaultmap kind
1459 Arg.push_back(getOpenMPSimpleClauseType(
1460 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1461 KLoc.push_back(Tok.getLocation());
1462 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1463 Tok.isNot(tok::annot_pragma_openmp_end))
1464 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001465 } else {
1466 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001467 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001468 TentativeParsingAction TPA(*this);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001469 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1470 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001471 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001472 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
1473 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001474 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00001475 } else {
1476 TPA.Revert();
1477 Arg.back() = OMPD_unknown;
1478 }
1479 } else
1480 TPA.Revert();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001481 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001482
Carlo Bertollib4adf552016-01-15 18:50:31 +00001483 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1484 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1485 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001486 if (NeedAnExpression) {
1487 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001488 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1489 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001490 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001491 }
1492
1493 // Parse ')'.
1494 T.consumeClose();
1495
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001496 if (NeedAnExpression && Val.isInvalid())
1497 return nullptr;
1498
Alexey Bataev56dafe82014-06-20 07:16:17 +00001499 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001500 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001501 T.getCloseLocation());
1502}
1503
Alexey Bataevc5e02582014-06-16 07:08:35 +00001504static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1505 UnqualifiedId &ReductionId) {
1506 SourceLocation TemplateKWLoc;
1507 if (ReductionIdScopeSpec.isEmpty()) {
1508 auto OOK = OO_None;
1509 switch (P.getCurToken().getKind()) {
1510 case tok::plus:
1511 OOK = OO_Plus;
1512 break;
1513 case tok::minus:
1514 OOK = OO_Minus;
1515 break;
1516 case tok::star:
1517 OOK = OO_Star;
1518 break;
1519 case tok::amp:
1520 OOK = OO_Amp;
1521 break;
1522 case tok::pipe:
1523 OOK = OO_Pipe;
1524 break;
1525 case tok::caret:
1526 OOK = OO_Caret;
1527 break;
1528 case tok::ampamp:
1529 OOK = OO_AmpAmp;
1530 break;
1531 case tok::pipepipe:
1532 OOK = OO_PipePipe;
1533 break;
1534 default:
1535 break;
1536 }
1537 if (OOK != OO_None) {
1538 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001539 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001540 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1541 return false;
1542 }
1543 }
1544 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1545 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001546 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001547 TemplateKWLoc, ReductionId);
1548}
1549
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001550/// Parses clauses with list.
1551bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1552 OpenMPClauseKind Kind,
1553 SmallVectorImpl<Expr *> &Vars,
1554 OpenMPVarListDataTy &Data) {
1555 UnqualifiedId UnqualifiedReductionId;
1556 bool InvalidReductionId = false;
1557 bool MapTypeModifierSpecified = false;
1558
1559 // Parse '('.
1560 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1561 if (T.expectAndConsume(diag::err_expected_lparen_after,
1562 getOpenMPClauseName(Kind)))
1563 return true;
1564
1565 bool NeedRParenForLinear = false;
1566 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1567 tok::annot_pragma_openmp_end);
1568 // Handle reduction-identifier for reduction clause.
1569 if (Kind == OMPC_reduction) {
1570 ColonProtectionRAIIObject ColonRAII(*this);
1571 if (getLangOpts().CPlusPlus)
1572 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1573 /*ObjectType=*/nullptr,
1574 /*EnteringContext=*/false);
1575 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1576 UnqualifiedReductionId);
1577 if (InvalidReductionId) {
1578 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1579 StopBeforeMatch);
1580 }
1581 if (Tok.is(tok::colon))
1582 Data.ColonLoc = ConsumeToken();
1583 else
1584 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1585 if (!InvalidReductionId)
1586 Data.ReductionId =
1587 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1588 } else if (Kind == OMPC_depend) {
1589 // Handle dependency type for depend clause.
1590 ColonProtectionRAIIObject ColonRAII(*this);
1591 Data.DepKind =
1592 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1593 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1594 Data.DepLinMapLoc = Tok.getLocation();
1595
1596 if (Data.DepKind == OMPC_DEPEND_unknown) {
1597 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1598 StopBeforeMatch);
1599 } else {
1600 ConsumeToken();
1601 // Special processing for depend(source) clause.
1602 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1603 // Parse ')'.
1604 T.consumeClose();
1605 return false;
1606 }
1607 }
1608 if (Tok.is(tok::colon))
1609 Data.ColonLoc = ConsumeToken();
1610 else {
1611 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1612 : diag::warn_pragma_expected_colon)
1613 << "dependency type";
1614 }
1615 } else if (Kind == OMPC_linear) {
1616 // Try to parse modifier if any.
1617 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1618 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1619 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1620 Data.DepLinMapLoc = ConsumeToken();
1621 LinearT.consumeOpen();
1622 NeedRParenForLinear = true;
1623 }
1624 } else if (Kind == OMPC_map) {
1625 // Handle map type for map clause.
1626 ColonProtectionRAIIObject ColonRAII(*this);
1627
1628 /// The map clause modifier token can be either a identifier or the C++
1629 /// delete keyword.
1630 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1631 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1632 };
1633
1634 // The first identifier may be a list item, a map-type or a
1635 // map-type-modifier. The map modifier can also be delete which has the same
1636 // spelling of the C++ delete keyword.
1637 Data.MapType =
1638 IsMapClauseModifierToken(Tok)
1639 ? static_cast<OpenMPMapClauseKind>(
1640 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1641 : OMPC_MAP_unknown;
1642 Data.DepLinMapLoc = Tok.getLocation();
1643 bool ColonExpected = false;
1644
1645 if (IsMapClauseModifierToken(Tok)) {
1646 if (PP.LookAhead(0).is(tok::colon)) {
1647 if (Data.MapType == OMPC_MAP_unknown)
1648 Diag(Tok, diag::err_omp_unknown_map_type);
1649 else if (Data.MapType == OMPC_MAP_always)
1650 Diag(Tok, diag::err_omp_map_type_missing);
1651 ConsumeToken();
1652 } else if (PP.LookAhead(0).is(tok::comma)) {
1653 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1654 PP.LookAhead(2).is(tok::colon)) {
1655 Data.MapTypeModifier = Data.MapType;
1656 if (Data.MapTypeModifier != OMPC_MAP_always) {
1657 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1658 Data.MapTypeModifier = OMPC_MAP_unknown;
1659 } else
1660 MapTypeModifierSpecified = true;
1661
1662 ConsumeToken();
1663 ConsumeToken();
1664
1665 Data.MapType =
1666 IsMapClauseModifierToken(Tok)
1667 ? static_cast<OpenMPMapClauseKind>(
1668 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1669 : OMPC_MAP_unknown;
1670 if (Data.MapType == OMPC_MAP_unknown ||
1671 Data.MapType == OMPC_MAP_always)
1672 Diag(Tok, diag::err_omp_unknown_map_type);
1673 ConsumeToken();
1674 } else {
1675 Data.MapType = OMPC_MAP_tofrom;
1676 Data.IsMapTypeImplicit = true;
1677 }
1678 } else {
1679 Data.MapType = OMPC_MAP_tofrom;
1680 Data.IsMapTypeImplicit = true;
1681 }
1682 } else {
1683 Data.MapType = OMPC_MAP_tofrom;
1684 Data.IsMapTypeImplicit = true;
1685 }
1686
1687 if (Tok.is(tok::colon))
1688 Data.ColonLoc = ConsumeToken();
1689 else if (ColonExpected)
1690 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1691 }
1692
1693 bool IsComma =
1694 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1695 (Kind == OMPC_reduction && !InvalidReductionId) ||
1696 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1697 (!MapTypeModifierSpecified ||
1698 Data.MapTypeModifier == OMPC_MAP_always)) ||
1699 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1700 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1701 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1702 Tok.isNot(tok::annot_pragma_openmp_end))) {
1703 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1704 // Parse variable
1705 ExprResult VarExpr =
1706 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1707 if (VarExpr.isUsable())
1708 Vars.push_back(VarExpr.get());
1709 else {
1710 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1711 StopBeforeMatch);
1712 }
1713 // Skip ',' if any
1714 IsComma = Tok.is(tok::comma);
1715 if (IsComma)
1716 ConsumeToken();
1717 else if (Tok.isNot(tok::r_paren) &&
1718 Tok.isNot(tok::annot_pragma_openmp_end) &&
1719 (!MayHaveTail || Tok.isNot(tok::colon)))
1720 Diag(Tok, diag::err_omp_expected_punc)
1721 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1722 : getOpenMPClauseName(Kind))
1723 << (Kind == OMPC_flush);
1724 }
1725
1726 // Parse ')' for linear clause with modifier.
1727 if (NeedRParenForLinear)
1728 LinearT.consumeClose();
1729
1730 // Parse ':' linear-step (or ':' alignment).
1731 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1732 if (MustHaveTail) {
1733 Data.ColonLoc = Tok.getLocation();
1734 SourceLocation ELoc = ConsumeToken();
1735 ExprResult Tail = ParseAssignmentExpression();
1736 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1737 if (Tail.isUsable())
1738 Data.TailExpr = Tail.get();
1739 else
1740 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1741 StopBeforeMatch);
1742 }
1743
1744 // Parse ')'.
1745 T.consumeClose();
1746 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1747 Vars.empty()) ||
1748 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1749 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1750 return true;
1751 return false;
1752}
1753
Alexander Musman1bb328c2014-06-04 13:06:39 +00001754/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001755/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001756///
1757/// private-clause:
1758/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001759/// firstprivate-clause:
1760/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001761/// lastprivate-clause:
1762/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001763/// shared-clause:
1764/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001765/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001766/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001767/// aligned-clause:
1768/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001769/// reduction-clause:
1770/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001771/// copyprivate-clause:
1772/// 'copyprivate' '(' list ')'
1773/// flush-clause:
1774/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001775/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001776/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001777/// map-clause:
1778/// 'map' '(' [ [ always , ]
1779/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00001780/// to-clause:
1781/// 'to' '(' list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00001782/// from-clause:
1783/// 'from' '(' list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00001784/// use_device_ptr-clause:
1785/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00001786/// is_device_ptr-clause:
1787/// 'is_device_ptr' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001788///
Alexey Bataev182227b2015-08-20 10:54:39 +00001789/// For 'linear' clause linear-list may have the following forms:
1790/// list
1791/// modifier(list)
1792/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001793OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1794 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001795 SourceLocation Loc = Tok.getLocation();
1796 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001797 SmallVector<Expr *, 4> Vars;
1798 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001799
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001800 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001801 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001802
Alexey Bataevc5e02582014-06-16 07:08:35 +00001803 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001804 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1805 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1806 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1807 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001808}
1809