blob: 5a3b4ac825c1778ea8c259f53b1e43f1f64ddb93 [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/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Dmitry Polukhin82478332016-02-13 06:53:38 +000029namespace {
30enum OpenMPDirectiveKindEx {
31 OMPD_cancellation = OMPD_unknown + 1,
32 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000033 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000034 OMPD_end,
35 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000036 OMPD_enter,
37 OMPD_exit,
38 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000039 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000040 OMPD_target_enter,
41 OMPD_target_exit
42};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000043
44class ThreadprivateListParserHelper final {
45 SmallVector<Expr *, 4> Identifiers;
46 Parser *P;
47
48public:
49 ThreadprivateListParserHelper(Parser *P) : P(P) {}
50 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
51 ExprResult Res =
52 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
53 if (Res.isUsable())
54 Identifiers.push_back(Res.get());
55 }
56 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
57};
Dmitry Polukhin82478332016-02-13 06:53:38 +000058} // namespace
59
60// Map token string to extended OMP token kind that are
61// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
62static unsigned getOpenMPDirectiveKindEx(StringRef S) {
63 auto DKind = getOpenMPDirectiveKind(S);
64 if (DKind != OMPD_unknown)
65 return DKind;
66
67 return llvm::StringSwitch<unsigned>(S)
68 .Case("cancellation", OMPD_cancellation)
69 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000070 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000071 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000072 .Case("enter", OMPD_enter)
73 .Case("exit", OMPD_exit)
74 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000075 .Case("reduction", OMPD_reduction)
Dmitry Polukhin82478332016-02-13 06:53:38 +000076 .Default(OMPD_unknown);
77}
78
Alexey Bataev4acb8592014-07-07 13:01:15 +000079static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000080 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
81 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
82 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000083 static const unsigned F[][3] = {
84 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000085 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000086 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000087 { OMPD_declare, OMPD_target, OMPD_declare_target },
88 { OMPD_end, OMPD_declare, OMPD_end_declare },
89 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000090 { OMPD_target, OMPD_data, OMPD_target_data },
91 { OMPD_target, OMPD_enter, OMPD_target_enter },
92 { OMPD_target, OMPD_exit, OMPD_target_exit },
93 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
94 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
95 { OMPD_for, OMPD_simd, OMPD_for_simd },
96 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
97 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
98 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
99 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
100 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
101 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
102 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000103 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000104 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000105 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000106 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000107 ? static_cast<unsigned>(OMPD_unknown)
108 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
109 if (DKind == OMPD_unknown)
110 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000111
Alexander Musmanf82886e2014-09-18 05:12:34 +0000112 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000113 if (DKind != F[i][0])
114 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000115
Dmitry Polukhin82478332016-02-13 06:53:38 +0000116 Tok = P.getPreprocessor().LookAhead(0);
117 unsigned SDKind =
118 Tok.isAnnotation()
119 ? static_cast<unsigned>(OMPD_unknown)
120 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
121 if (SDKind == OMPD_unknown)
122 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000123
Dmitry Polukhin82478332016-02-13 06:53:38 +0000124 if (SDKind == F[i][1]) {
125 P.ConsumeToken();
126 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000127 }
128 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000129 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
130 : OMPD_unknown;
131}
132
133static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000134 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000135 Sema &Actions = P.getActions();
136 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000137 // Allow to use 'operator' keyword for C++ operators
138 bool WithOperator = false;
139 if (Tok.is(tok::kw_operator)) {
140 P.ConsumeToken();
141 Tok = P.getCurToken();
142 WithOperator = true;
143 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000144 switch (Tok.getKind()) {
145 case tok::plus: // '+'
146 OOK = OO_Plus;
147 break;
148 case tok::minus: // '-'
149 OOK = OO_Minus;
150 break;
151 case tok::star: // '*'
152 OOK = OO_Star;
153 break;
154 case tok::amp: // '&'
155 OOK = OO_Amp;
156 break;
157 case tok::pipe: // '|'
158 OOK = OO_Pipe;
159 break;
160 case tok::caret: // '^'
161 OOK = OO_Caret;
162 break;
163 case tok::ampamp: // '&&'
164 OOK = OO_AmpAmp;
165 break;
166 case tok::pipepipe: // '||'
167 OOK = OO_PipePipe;
168 break;
169 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000170 if (!WithOperator)
171 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000172 default:
173 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
174 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
175 Parser::StopBeforeMatch);
176 return DeclarationName();
177 }
178 P.ConsumeToken();
179 auto &DeclNames = Actions.getASTContext().DeclarationNames;
180 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
181 : DeclNames.getCXXOperatorName(OOK);
182}
183
184/// \brief Parse 'omp declare reduction' construct.
185///
186/// declare-reduction-directive:
187/// annot_pragma_openmp 'declare' 'reduction'
188/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
189/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
190/// annot_pragma_openmp_end
191/// <reduction_id> is either a base language identifier or one of the following
192/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
193///
194Parser::DeclGroupPtrTy
195Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
196 // Parse '('.
197 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
198 if (T.expectAndConsume(diag::err_expected_lparen_after,
199 getOpenMPDirectiveName(OMPD_declare_reduction))) {
200 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
201 return DeclGroupPtrTy();
202 }
203
204 DeclarationName Name = parseOpenMPReductionId(*this);
205 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
206 return DeclGroupPtrTy();
207
208 // Consume ':'.
209 bool IsCorrect = !ExpectAndConsume(tok::colon);
210
211 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
212 return DeclGroupPtrTy();
213
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000214 IsCorrect = IsCorrect && !Name.isEmpty();
215
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000216 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
217 Diag(Tok.getLocation(), diag::err_expected_type);
218 IsCorrect = false;
219 }
220
221 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
222 return DeclGroupPtrTy();
223
224 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
225 // Parse list of types until ':' token.
226 do {
227 ColonProtectionRAIIObject ColonRAII(*this);
228 SourceRange Range;
229 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
230 if (TR.isUsable()) {
231 auto ReductionType =
232 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
233 if (!ReductionType.isNull()) {
234 ReductionTypes.push_back(
235 std::make_pair(ReductionType, Range.getBegin()));
236 }
237 } else {
238 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
239 StopBeforeMatch);
240 }
241
242 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
243 break;
244
245 // Consume ','.
246 if (ExpectAndConsume(tok::comma)) {
247 IsCorrect = false;
248 if (Tok.is(tok::annot_pragma_openmp_end)) {
249 Diag(Tok.getLocation(), diag::err_expected_type);
250 return DeclGroupPtrTy();
251 }
252 }
253 } while (Tok.isNot(tok::annot_pragma_openmp_end));
254
255 if (ReductionTypes.empty()) {
256 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
257 return DeclGroupPtrTy();
258 }
259
260 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
261 return DeclGroupPtrTy();
262
263 // Consume ':'.
264 if (ExpectAndConsume(tok::colon))
265 IsCorrect = false;
266
267 if (Tok.is(tok::annot_pragma_openmp_end)) {
268 Diag(Tok.getLocation(), diag::err_expected_expression);
269 return DeclGroupPtrTy();
270 }
271
272 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
273 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
274
275 // Parse <combiner> expression and then parse initializer if any for each
276 // correct type.
277 unsigned I = 0, E = ReductionTypes.size();
278 for (auto *D : DRD.get()) {
279 TentativeParsingAction TPA(*this);
280 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
281 Scope::OpenMPDirectiveScope);
282 // Parse <combiner> expression.
283 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
284 ExprResult CombinerResult =
285 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
286 D->getLocation(), /*DiscardedValue=*/true);
287 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
288
289 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
290 Tok.isNot(tok::annot_pragma_openmp_end)) {
291 TPA.Commit();
292 IsCorrect = false;
293 break;
294 }
295 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
296 ExprResult InitializerResult;
297 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
298 // Parse <initializer> expression.
299 if (Tok.is(tok::identifier) &&
300 Tok.getIdentifierInfo()->isStr("initializer"))
301 ConsumeToken();
302 else {
303 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
304 TPA.Commit();
305 IsCorrect = false;
306 break;
307 }
308 // Parse '('.
309 BalancedDelimiterTracker T(*this, tok::l_paren,
310 tok::annot_pragma_openmp_end);
311 IsCorrect =
312 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
313 IsCorrect;
314 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
315 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
316 Scope::OpenMPDirectiveScope);
317 // Parse expression.
318 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
319 InitializerResult = Actions.ActOnFinishFullExpr(
320 ParseAssignmentExpression().get(), D->getLocation(),
321 /*DiscardedValue=*/true);
322 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
323 D, InitializerResult.get());
324 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
325 Tok.isNot(tok::annot_pragma_openmp_end)) {
326 TPA.Commit();
327 IsCorrect = false;
328 break;
329 }
330 IsCorrect =
331 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
332 }
333 }
334
335 ++I;
336 // Revert parsing if not the last type, otherwise accept it, we're done with
337 // parsing.
338 if (I != E)
339 TPA.Revert();
340 else
341 TPA.Commit();
342 }
343 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
344 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000345}
346
Alexey Bataev2af33e32016-04-07 12:45:37 +0000347namespace {
348/// RAII that recreates function context for correct parsing of clauses of
349/// 'declare simd' construct.
350/// OpenMP, 2.8.2 declare simd Construct
351/// The expressions appearing in the clauses of this directive are evaluated in
352/// the scope of the arguments of the function declaration or definition.
353class FNContextRAII final {
354 Parser &P;
355 Sema::CXXThisScopeRAII *ThisScope;
356 Parser::ParseScope *TempScope;
357 Parser::ParseScope *FnScope;
358 bool HasTemplateScope = false;
359 bool HasFunScope = false;
360 FNContextRAII() = delete;
361 FNContextRAII(const FNContextRAII &) = delete;
362 FNContextRAII &operator=(const FNContextRAII &) = delete;
363
364public:
365 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
366 Decl *D = *Ptr.get().begin();
367 NamedDecl *ND = dyn_cast<NamedDecl>(D);
368 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
369 Sema &Actions = P.getActions();
370
371 // Allow 'this' within late-parsed attributes.
372 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
373 ND && ND->isCXXInstanceMember());
374
375 // If the Decl is templatized, add template parameters to scope.
376 HasTemplateScope = D->isTemplateDecl();
377 TempScope =
378 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
379 if (HasTemplateScope)
380 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
381
382 // If the Decl is on a function, add function parameters to the scope.
383 HasFunScope = D->isFunctionOrFunctionTemplate();
384 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
385 HasFunScope);
386 if (HasFunScope)
387 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
388 }
389 ~FNContextRAII() {
390 if (HasFunScope) {
391 P.getActions().ActOnExitFunctionContext();
392 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
393 }
394 if (HasTemplateScope)
395 TempScope->Exit();
396 delete FnScope;
397 delete TempScope;
398 delete ThisScope;
399 }
400};
401} // namespace
402
Alexey Bataevd93d3762016-04-12 09:35:56 +0000403/// Parses clauses for 'declare simd' directive.
404/// clause:
405/// 'inbranch' | 'notinbranch'
406/// 'simdlen' '(' <expr> ')'
407/// { 'uniform' '(' <argument_list> ')' }
408/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000409/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
410static bool parseDeclareSimdClauses(
411 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
412 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
413 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
414 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000415 SourceRange BSRange;
416 const Token &Tok = P.getCurToken();
417 bool IsError = false;
418 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
419 if (Tok.isNot(tok::identifier))
420 break;
421 OMPDeclareSimdDeclAttr::BranchStateTy Out;
422 IdentifierInfo *II = Tok.getIdentifierInfo();
423 StringRef ClauseName = II->getName();
424 // Parse 'inranch|notinbranch' clauses.
425 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
426 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
427 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
428 << ClauseName
429 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
430 IsError = true;
431 }
432 BS = Out;
433 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
434 P.ConsumeToken();
435 } else if (ClauseName.equals("simdlen")) {
436 if (SimdLen.isUsable()) {
437 P.Diag(Tok, diag::err_omp_more_one_clause)
438 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
439 IsError = true;
440 }
441 P.ConsumeToken();
442 SourceLocation RLoc;
443 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
444 if (SimdLen.isInvalid())
445 IsError = true;
446 } else {
447 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000448 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
449 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000450 Parser::OpenMPVarListDataTy Data;
451 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000452 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000453 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000454 else if (CKind == OMPC_linear)
455 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000456
457 P.ConsumeToken();
458 if (P.ParseOpenMPVarList(OMPD_declare_simd,
459 getOpenMPClauseKind(ClauseName), *Vars, Data))
460 IsError = true;
461 if (CKind == OMPC_aligned)
462 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000463 else if (CKind == OMPC_linear) {
464 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
465 Data.DepLinMapLoc))
466 Data.LinKind = OMPC_LINEAR_val;
467 LinModifiers.append(Linears.size() - LinModifiers.size(),
468 Data.LinKind);
469 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
470 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000471 } else
472 // TODO: add parsing of other clauses.
473 break;
474 }
475 // Skip ',' if any.
476 if (Tok.is(tok::comma))
477 P.ConsumeToken();
478 }
479 return IsError;
480}
481
Alexey Bataev2af33e32016-04-07 12:45:37 +0000482/// Parse clauses for '#pragma omp declare simd'.
483Parser::DeclGroupPtrTy
484Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
485 CachedTokens &Toks, SourceLocation Loc) {
486 PP.EnterToken(Tok);
487 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
488 // Consume the previously pushed token.
489 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
490
491 FNContextRAII FnContext(*this, Ptr);
492 OMPDeclareSimdDeclAttr::BranchStateTy BS =
493 OMPDeclareSimdDeclAttr::BS_Undefined;
494 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000495 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000496 SmallVector<Expr *, 4> Aligneds;
497 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000498 SmallVector<Expr *, 4> Linears;
499 SmallVector<unsigned, 4> LinModifiers;
500 SmallVector<Expr *, 4> Steps;
501 bool IsError =
502 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
503 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000504 // Need to check for extra tokens.
505 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
506 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
507 << getOpenMPDirectiveName(OMPD_declare_simd);
508 while (Tok.isNot(tok::annot_pragma_openmp_end))
509 ConsumeAnyToken();
510 }
511 // Skip the last annot_pragma_openmp_end.
512 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000513 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000514 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000515 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
516 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000517 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000518 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000519}
520
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000521/// \brief Parsing of declarative OpenMP directives.
522///
523/// threadprivate-directive:
524/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000525/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000526///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000527/// declare-reduction-directive:
528/// annot_pragma_openmp 'declare' 'reduction' [...]
529/// annot_pragma_openmp_end
530///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000531/// declare-simd-directive:
532/// annot_pragma_openmp 'declare simd' {<clause> [,]}
533/// annot_pragma_openmp_end
534/// <function declaration/definition>
535///
536Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
537 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
538 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000539 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000540 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000541
542 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000543 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000544
545 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000546 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000547 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000548 ThreadprivateListParserHelper Helper(this);
549 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000550 // The last seen token is annot_pragma_openmp_end - need to check for
551 // extra tokens.
552 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
553 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000554 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000555 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000556 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000557 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000558 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000559 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
560 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000561 }
562 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000563 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000564 case OMPD_declare_reduction:
565 ConsumeToken();
566 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
567 // The last seen token is annot_pragma_openmp_end - need to check for
568 // extra tokens.
569 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
570 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
571 << getOpenMPDirectiveName(OMPD_declare_reduction);
572 while (Tok.isNot(tok::annot_pragma_openmp_end))
573 ConsumeAnyToken();
574 }
575 // Skip the last annot_pragma_openmp_end.
576 ConsumeToken();
577 return Res;
578 }
579 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000580 case OMPD_declare_simd: {
581 // The syntax is:
582 // { #pragma omp declare simd }
583 // <function-declaration-or-definition>
584 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000585 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000586 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000587 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
588 Toks.push_back(Tok);
589 ConsumeAnyToken();
590 }
591 Toks.push_back(Tok);
592 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000593
594 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000595 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000596 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000597 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000598 // Here we expect to see some function declaration.
599 if (AS == AS_none) {
600 assert(TagType == DeclSpec::TST_unspecified);
601 MaybeParseCXX11Attributes(Attrs);
602 MaybeParseMicrosoftAttributes(Attrs);
603 ParsingDeclSpec PDS(*this);
604 Ptr = ParseExternalDeclaration(Attrs, &PDS);
605 } else {
606 Ptr =
607 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
608 }
609 }
610 if (!Ptr) {
611 Diag(Loc, diag::err_omp_decl_in_declare_simd);
612 return DeclGroupPtrTy();
613 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000614 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000615 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000616 case OMPD_declare_target: {
617 SourceLocation DTLoc = ConsumeAnyToken();
618 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000619 // OpenMP 4.5 syntax with list of entities.
620 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
621 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
622 OMPDeclareTargetDeclAttr::MapTypeTy MT =
623 OMPDeclareTargetDeclAttr::MT_To;
624 if (Tok.is(tok::identifier)) {
625 IdentifierInfo *II = Tok.getIdentifierInfo();
626 StringRef ClauseName = II->getName();
627 // Parse 'to|link' clauses.
628 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
629 MT)) {
630 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
631 << ClauseName;
632 break;
633 }
634 ConsumeToken();
635 }
636 auto Callback = [this, MT, &SameDirectiveDecls](
637 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
638 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
639 SameDirectiveDecls);
640 };
641 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
642 break;
643
644 // Consume optional ','.
645 if (Tok.is(tok::comma))
646 ConsumeToken();
647 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000648 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000649 ConsumeAnyToken();
650 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000651 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000652
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000653 // Skip the last annot_pragma_openmp_end.
654 ConsumeAnyToken();
655
656 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
657 return DeclGroupPtrTy();
658
659 DKind = ParseOpenMPDirectiveKind(*this);
660 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
661 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
662 ParsedAttributesWithRange attrs(AttrFactory);
663 MaybeParseCXX11Attributes(attrs);
664 MaybeParseMicrosoftAttributes(attrs);
665 ParseExternalDeclaration(attrs);
666 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
667 TentativeParsingAction TPA(*this);
668 ConsumeToken();
669 DKind = ParseOpenMPDirectiveKind(*this);
670 if (DKind != OMPD_end_declare_target)
671 TPA.Revert();
672 else
673 TPA.Commit();
674 }
675 }
676
677 if (DKind == OMPD_end_declare_target) {
678 ConsumeAnyToken();
679 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
680 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
681 << getOpenMPDirectiveName(OMPD_end_declare_target);
682 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
683 }
684 // Skip the last annot_pragma_openmp_end.
685 ConsumeAnyToken();
686 } else {
687 Diag(Tok, diag::err_expected_end_declare_target);
688 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
689 }
690 Actions.ActOnFinishOpenMPDeclareTargetDirective();
691 return DeclGroupPtrTy();
692 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000693 case OMPD_unknown:
694 Diag(Tok, diag::err_omp_unknown_directive);
695 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000696 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000697 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000698 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000699 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000700 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000701 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000702 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000703 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000704 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000705 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000706 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000707 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000708 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000709 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000710 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000711 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000712 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000713 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000714 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000715 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000716 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000717 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000718 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000719 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000720 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000721 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000722 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000723 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000724 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000725 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000726 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000727 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000728 case OMPD_end_declare_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000729 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000730 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000731 break;
732 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000733 while (Tok.isNot(tok::annot_pragma_openmp_end))
734 ConsumeAnyToken();
735 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000736 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000737}
738
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000739/// \brief Parsing of declarative or executable OpenMP directives.
740///
741/// threadprivate-directive:
742/// annot_pragma_openmp 'threadprivate' simple-variable-list
743/// annot_pragma_openmp_end
744///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000745/// declare-reduction-directive:
746/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
747/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
748/// ('omp_priv' '=' <expression>|<function_call>) ')']
749/// annot_pragma_openmp_end
750///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000751/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000752/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000753/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
754/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000755/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000756/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000757/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000758/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000759/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000760/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000761///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000762StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
763 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000764 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000765 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000766 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000767 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000768 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000769 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000770 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000772 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000773 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000774 // Name of critical directive.
775 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000777 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000778 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000779
780 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000781 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000782 if (Allowed != ACK_Any) {
783 Diag(Tok, diag::err_omp_immediate_directive)
784 << getOpenMPDirectiveName(DKind) << 0;
785 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000787 ThreadprivateListParserHelper Helper(this);
788 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000789 // The last seen token is annot_pragma_openmp_end - need to check for
790 // extra tokens.
791 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
792 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000793 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000794 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000796 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
797 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
799 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000800 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000802 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000803 case OMPD_declare_reduction:
804 ConsumeToken();
805 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
806 // The last seen token is annot_pragma_openmp_end - need to check for
807 // extra tokens.
808 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
809 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
810 << getOpenMPDirectiveName(OMPD_declare_reduction);
811 while (Tok.isNot(tok::annot_pragma_openmp_end))
812 ConsumeAnyToken();
813 }
814 ConsumeAnyToken();
815 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
816 } else
817 SkipUntil(tok::annot_pragma_openmp_end);
818 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000819 case OMPD_flush:
820 if (PP.LookAhead(0).is(tok::l_paren)) {
821 FlushHasClause = true;
822 // Push copy of the current token back to stream to properly parse
823 // pseudo-clause OMPFlushClause.
824 PP.EnterToken(Tok);
825 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000826 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000827 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000828 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000829 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000830 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000831 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000832 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000833 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000834 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000835 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000836 }
837 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000838 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000839 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000840 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000841 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000842 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000843 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000844 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000845 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000846 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000847 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000848 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000849 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000850 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000851 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000852 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000853 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000854 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000855 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000856 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000857 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000858 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000859 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000860 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000861 case OMPD_taskloop_simd:
862 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000863 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000864 // Parse directive name of the 'critical' directive if any.
865 if (DKind == OMPD_critical) {
866 BalancedDelimiterTracker T(*this, tok::l_paren,
867 tok::annot_pragma_openmp_end);
868 if (!T.consumeOpen()) {
869 if (Tok.isAnyIdentifier()) {
870 DirName =
871 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
872 ConsumeAnyToken();
873 } else {
874 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
875 }
876 T.consumeClose();
877 }
Alexey Bataev80909872015-07-02 11:25:17 +0000878 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000879 CancelRegion = ParseOpenMPDirectiveKind(*this);
880 if (Tok.isNot(tok::annot_pragma_openmp_end))
881 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000882 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000883
Alexey Bataevf29276e2014-06-18 04:14:57 +0000884 if (isOpenMPLoopDirective(DKind))
885 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
886 if (isOpenMPSimdDirective(DKind))
887 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
888 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000889 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000890
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000891 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000892 OpenMPClauseKind CKind =
893 Tok.isAnnotation()
894 ? OMPC_unknown
895 : FlushHasClause ? OMPC_flush
896 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000897 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000898 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000899 OMPClause *Clause =
900 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000901 FirstClauses[CKind].setInt(true);
902 if (Clause) {
903 FirstClauses[CKind].setPointer(Clause);
904 Clauses.push_back(Clause);
905 }
906
907 // Skip ',' if any.
908 if (Tok.is(tok::comma))
909 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000910 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911 }
912 // End location of the directive.
913 EndLoc = Tok.getLocation();
914 // Consume final annot_pragma_openmp_end.
915 ConsumeToken();
916
Alexey Bataeveb482352015-12-18 05:05:56 +0000917 // OpenMP [2.13.8, ordered Construct, Syntax]
918 // If the depend clause is specified, the ordered construct is a stand-alone
919 // directive.
920 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000921 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000922 Diag(Loc, diag::err_omp_immediate_directive)
923 << getOpenMPDirectiveName(DKind) << 1
924 << getOpenMPClauseName(OMPC_depend);
925 }
926 HasAssociatedStatement = false;
927 }
928
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000929 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000930 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000931 // The body is a block scope like in Lambdas and Blocks.
932 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000933 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000934 Actions.ActOnStartOfCompoundStmt();
935 // Parse statement
936 AssociatedStmt = ParseStatement();
937 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000938 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000939 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000940 Directive = Actions.ActOnOpenMPExecutableDirective(
941 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
942 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000943
944 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000945 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000946 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000947 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000948 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000949 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000950 case OMPD_declare_target:
951 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000952 Diag(Tok, diag::err_omp_unexpected_directive)
953 << getOpenMPDirectiveName(DKind);
954 SkipUntil(tok::annot_pragma_openmp_end);
955 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000956 case OMPD_unknown:
957 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000958 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000959 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000960 }
961 return Directive;
962}
963
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000964// Parses simple list:
965// simple-variable-list:
966// '(' id-expression {, id-expression} ')'
967//
968bool Parser::ParseOpenMPSimpleVarList(
969 OpenMPDirectiveKind Kind,
970 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
971 Callback,
972 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000973 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000974 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000975 if (T.expectAndConsume(diag::err_expected_lparen_after,
976 getOpenMPDirectiveName(Kind)))
977 return true;
978 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000979 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000980
981 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000982 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000983 CXXScopeSpec SS;
984 SourceLocation TemplateKWLoc;
985 UnqualifiedId Name;
986 // Read var name.
987 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000988 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000989
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000990 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000991 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000992 IsCorrect = false;
993 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000994 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000995 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000996 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000997 IsCorrect = false;
998 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000999 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001000 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1001 Tok.isNot(tok::annot_pragma_openmp_end)) {
1002 IsCorrect = false;
1003 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001004 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001005 Diag(PrevTok.getLocation(), diag::err_expected)
1006 << tok::identifier
1007 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001008 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001009 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001010 }
1011 // Consume ','.
1012 if (Tok.is(tok::comma)) {
1013 ConsumeToken();
1014 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001015 }
1016
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001017 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001018 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001019 IsCorrect = false;
1020 }
1021
1022 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001023 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001024
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001025 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001026}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001027
1028/// \brief Parsing of OpenMP clauses.
1029///
1030/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001031/// if-clause | final-clause | num_threads-clause | safelen-clause |
1032/// default-clause | private-clause | firstprivate-clause | shared-clause
1033/// | linear-clause | aligned-clause | collapse-clause |
1034/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001035/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001036/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001037/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001038/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001039/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +00001040/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001041///
1042OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1043 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001044 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001045 bool ErrorFound = false;
1046 // Check if clause is allowed for the given directive.
1047 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001048 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1049 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001050 ErrorFound = true;
1051 }
1052
1053 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001054 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001055 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001056 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001057 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001058 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001059 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001060 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001061 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001062 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001063 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001064 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001065 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001066 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001067 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001068 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001069 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001070 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001071 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001072 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001073 // OpenMP [2.9.1, target data construct, Restrictions]
1074 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001075 // OpenMP [2.11.1, task Construct, Restrictions]
1076 // At most one if clause can appear on the directive.
1077 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001078 // OpenMP [teams Construct, Restrictions]
1079 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001080 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001081 // OpenMP [2.9.1, task Construct, Restrictions]
1082 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001083 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1084 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001085 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1086 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001087 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001088 Diag(Tok, diag::err_omp_more_one_clause)
1089 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001090 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001091 }
1092
Alexey Bataev10e775f2015-07-30 11:36:16 +00001093 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1094 Clause = ParseOpenMPClause(CKind);
1095 else
1096 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001097 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001098 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001099 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001100 // OpenMP [2.14.3.1, Restrictions]
1101 // Only a single default clause may be specified on a parallel, task or
1102 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001103 // OpenMP [2.5, parallel Construct, Restrictions]
1104 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001105 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001106 Diag(Tok, diag::err_omp_more_one_clause)
1107 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001108 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001109 }
1110
1111 Clause = ParseOpenMPSimpleClause(CKind);
1112 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001113 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001114 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001115 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001116 // OpenMP [2.7.1, Restrictions, p. 3]
1117 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001118 // OpenMP [2.10.4, Restrictions, p. 106]
1119 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001120 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001121 Diag(Tok, diag::err_omp_more_one_clause)
1122 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001123 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001124 }
1125
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001126 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001127 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1128 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001129 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001130 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001131 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001132 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001133 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001134 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001135 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001136 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001137 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001138 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001139 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001140 // OpenMP [2.7.1, Restrictions, p. 9]
1141 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001142 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1143 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001144 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001145 Diag(Tok, diag::err_omp_more_one_clause)
1146 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001147 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001148 }
1149
1150 Clause = ParseOpenMPClause(CKind);
1151 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001152 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001153 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001154 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001155 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001156 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001157 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001158 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001159 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001160 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001161 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001162 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001163 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +00001164 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001165 break;
1166 case OMPC_unknown:
1167 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001168 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001169 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001170 break;
1171 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001172 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001173 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1174 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001175 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001176 break;
1177 }
Craig Topper161e4db2014-05-21 06:02:52 +00001178 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001179}
1180
Alexey Bataev2af33e32016-04-07 12:45:37 +00001181/// Parses simple expression in parens for single-expression clauses of OpenMP
1182/// constructs.
1183/// \param RLoc Returned location of right paren.
1184ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1185 SourceLocation &RLoc) {
1186 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1187 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1188 return ExprError();
1189
1190 SourceLocation ELoc = Tok.getLocation();
1191 ExprResult LHS(ParseCastExpression(
1192 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1193 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1194 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1195
1196 // Parse ')'.
1197 T.consumeClose();
1198
1199 RLoc = T.getCloseLocation();
1200 return Val;
1201}
1202
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001203/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001204/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001205/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001206///
Alexey Bataev3778b602014-07-17 07:32:53 +00001207/// final-clause:
1208/// 'final' '(' expression ')'
1209///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001210/// num_threads-clause:
1211/// 'num_threads' '(' expression ')'
1212///
1213/// safelen-clause:
1214/// 'safelen' '(' expression ')'
1215///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001216/// simdlen-clause:
1217/// 'simdlen' '(' expression ')'
1218///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001219/// collapse-clause:
1220/// 'collapse' '(' expression ')'
1221///
Alexey Bataeva0569352015-12-01 10:17:31 +00001222/// priority-clause:
1223/// 'priority' '(' expression ')'
1224///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001225/// grainsize-clause:
1226/// 'grainsize' '(' expression ')'
1227///
Alexey Bataev382967a2015-12-08 12:06:20 +00001228/// num_tasks-clause:
1229/// 'num_tasks' '(' expression ')'
1230///
Alexey Bataev28c75412015-12-15 08:19:24 +00001231/// hint-clause:
1232/// 'hint' '(' expression ')'
1233///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001234OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1235 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001236 SourceLocation LLoc = Tok.getLocation();
1237 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001238
Alexey Bataev2af33e32016-04-07 12:45:37 +00001239 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001240
1241 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001242 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001243
Alexey Bataev2af33e32016-04-07 12:45:37 +00001244 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001245}
1246
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001247/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001248///
1249/// default-clause:
1250/// 'default' '(' 'none' | 'shared' ')
1251///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001252/// proc_bind-clause:
1253/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1254///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001255OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1256 SourceLocation Loc = Tok.getLocation();
1257 SourceLocation LOpen = ConsumeToken();
1258 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001259 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001260 if (T.expectAndConsume(diag::err_expected_lparen_after,
1261 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001262 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001263
Alexey Bataeva55ed262014-05-28 06:15:33 +00001264 unsigned Type = getOpenMPSimpleClauseType(
1265 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001266 SourceLocation TypeLoc = Tok.getLocation();
1267 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1268 Tok.isNot(tok::annot_pragma_openmp_end))
1269 ConsumeAnyToken();
1270
1271 // Parse ')'.
1272 T.consumeClose();
1273
1274 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1275 Tok.getLocation());
1276}
1277
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001278/// \brief Parsing of OpenMP clauses like 'ordered'.
1279///
1280/// ordered-clause:
1281/// 'ordered'
1282///
Alexey Bataev236070f2014-06-20 11:19:47 +00001283/// nowait-clause:
1284/// 'nowait'
1285///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001286/// untied-clause:
1287/// 'untied'
1288///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001289/// mergeable-clause:
1290/// 'mergeable'
1291///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001292/// read-clause:
1293/// 'read'
1294///
Alexey Bataev346265e2015-09-25 10:37:12 +00001295/// threads-clause:
1296/// 'threads'
1297///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001298/// simd-clause:
1299/// 'simd'
1300///
Alexey Bataevb825de12015-12-07 10:51:44 +00001301/// nogroup-clause:
1302/// 'nogroup'
1303///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001304OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1305 SourceLocation Loc = Tok.getLocation();
1306 ConsumeAnyToken();
1307
1308 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1309}
1310
1311
Alexey Bataev56dafe82014-06-20 07:16:17 +00001312/// \brief Parsing of OpenMP clauses with single expressions and some additional
1313/// argument like 'schedule' or 'dist_schedule'.
1314///
1315/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001316/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1317/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001318///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001319/// if-clause:
1320/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1321///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001322/// defaultmap:
1323/// 'defaultmap' '(' modifier ':' kind ')'
1324///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001325OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1326 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001327 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001328 // Parse '('.
1329 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1330 if (T.expectAndConsume(diag::err_expected_lparen_after,
1331 getOpenMPClauseName(Kind)))
1332 return nullptr;
1333
1334 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001335 SmallVector<unsigned, 4> Arg;
1336 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001337 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001338 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1339 Arg.resize(NumberOfElements);
1340 KLoc.resize(NumberOfElements);
1341 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1342 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1343 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1344 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001345 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001346 if (KindModifier > OMPC_SCHEDULE_unknown) {
1347 // Parse 'modifier'
1348 Arg[Modifier1] = KindModifier;
1349 KLoc[Modifier1] = Tok.getLocation();
1350 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1351 Tok.isNot(tok::annot_pragma_openmp_end))
1352 ConsumeAnyToken();
1353 if (Tok.is(tok::comma)) {
1354 // Parse ',' 'modifier'
1355 ConsumeAnyToken();
1356 KindModifier = getOpenMPSimpleClauseType(
1357 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1358 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1359 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001360 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001361 KLoc[Modifier2] = Tok.getLocation();
1362 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1363 Tok.isNot(tok::annot_pragma_openmp_end))
1364 ConsumeAnyToken();
1365 }
1366 // Parse ':'
1367 if (Tok.is(tok::colon))
1368 ConsumeAnyToken();
1369 else
1370 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1371 KindModifier = getOpenMPSimpleClauseType(
1372 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1373 }
1374 Arg[ScheduleKind] = KindModifier;
1375 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001376 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1377 Tok.isNot(tok::annot_pragma_openmp_end))
1378 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001379 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1380 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1381 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001382 Tok.is(tok::comma))
1383 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001384 } else if (Kind == OMPC_dist_schedule) {
1385 Arg.push_back(getOpenMPSimpleClauseType(
1386 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1387 KLoc.push_back(Tok.getLocation());
1388 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1389 Tok.isNot(tok::annot_pragma_openmp_end))
1390 ConsumeAnyToken();
1391 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1392 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001393 } else if (Kind == OMPC_defaultmap) {
1394 // Get a defaultmap modifier
1395 Arg.push_back(getOpenMPSimpleClauseType(
1396 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1397 KLoc.push_back(Tok.getLocation());
1398 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1399 Tok.isNot(tok::annot_pragma_openmp_end))
1400 ConsumeAnyToken();
1401 // Parse ':'
1402 if (Tok.is(tok::colon))
1403 ConsumeAnyToken();
1404 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1405 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1406 // Get a defaultmap kind
1407 Arg.push_back(getOpenMPSimpleClauseType(
1408 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1409 KLoc.push_back(Tok.getLocation());
1410 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1411 Tok.isNot(tok::annot_pragma_openmp_end))
1412 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001413 } else {
1414 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001415 KLoc.push_back(Tok.getLocation());
1416 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1417 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001418 ConsumeToken();
1419 if (Tok.is(tok::colon))
1420 DelimLoc = ConsumeToken();
1421 else
1422 Diag(Tok, diag::warn_pragma_expected_colon)
1423 << "directive name modifier";
1424 }
1425 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001426
Carlo Bertollib4adf552016-01-15 18:50:31 +00001427 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1428 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1429 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001430 if (NeedAnExpression) {
1431 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001432 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1433 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001434 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001435 }
1436
1437 // Parse ')'.
1438 T.consumeClose();
1439
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001440 if (NeedAnExpression && Val.isInvalid())
1441 return nullptr;
1442
Alexey Bataev56dafe82014-06-20 07:16:17 +00001443 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001444 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001445 T.getCloseLocation());
1446}
1447
Alexey Bataevc5e02582014-06-16 07:08:35 +00001448static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1449 UnqualifiedId &ReductionId) {
1450 SourceLocation TemplateKWLoc;
1451 if (ReductionIdScopeSpec.isEmpty()) {
1452 auto OOK = OO_None;
1453 switch (P.getCurToken().getKind()) {
1454 case tok::plus:
1455 OOK = OO_Plus;
1456 break;
1457 case tok::minus:
1458 OOK = OO_Minus;
1459 break;
1460 case tok::star:
1461 OOK = OO_Star;
1462 break;
1463 case tok::amp:
1464 OOK = OO_Amp;
1465 break;
1466 case tok::pipe:
1467 OOK = OO_Pipe;
1468 break;
1469 case tok::caret:
1470 OOK = OO_Caret;
1471 break;
1472 case tok::ampamp:
1473 OOK = OO_AmpAmp;
1474 break;
1475 case tok::pipepipe:
1476 OOK = OO_PipePipe;
1477 break;
1478 default:
1479 break;
1480 }
1481 if (OOK != OO_None) {
1482 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001483 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001484 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1485 return false;
1486 }
1487 }
1488 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1489 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001490 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001491 TemplateKWLoc, ReductionId);
1492}
1493
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001494/// Parses clauses with list.
1495bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1496 OpenMPClauseKind Kind,
1497 SmallVectorImpl<Expr *> &Vars,
1498 OpenMPVarListDataTy &Data) {
1499 UnqualifiedId UnqualifiedReductionId;
1500 bool InvalidReductionId = false;
1501 bool MapTypeModifierSpecified = false;
1502
1503 // Parse '('.
1504 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1505 if (T.expectAndConsume(diag::err_expected_lparen_after,
1506 getOpenMPClauseName(Kind)))
1507 return true;
1508
1509 bool NeedRParenForLinear = false;
1510 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1511 tok::annot_pragma_openmp_end);
1512 // Handle reduction-identifier for reduction clause.
1513 if (Kind == OMPC_reduction) {
1514 ColonProtectionRAIIObject ColonRAII(*this);
1515 if (getLangOpts().CPlusPlus)
1516 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1517 /*ObjectType=*/nullptr,
1518 /*EnteringContext=*/false);
1519 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1520 UnqualifiedReductionId);
1521 if (InvalidReductionId) {
1522 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1523 StopBeforeMatch);
1524 }
1525 if (Tok.is(tok::colon))
1526 Data.ColonLoc = ConsumeToken();
1527 else
1528 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1529 if (!InvalidReductionId)
1530 Data.ReductionId =
1531 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1532 } else if (Kind == OMPC_depend) {
1533 // Handle dependency type for depend clause.
1534 ColonProtectionRAIIObject ColonRAII(*this);
1535 Data.DepKind =
1536 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1537 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1538 Data.DepLinMapLoc = Tok.getLocation();
1539
1540 if (Data.DepKind == OMPC_DEPEND_unknown) {
1541 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1542 StopBeforeMatch);
1543 } else {
1544 ConsumeToken();
1545 // Special processing for depend(source) clause.
1546 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1547 // Parse ')'.
1548 T.consumeClose();
1549 return false;
1550 }
1551 }
1552 if (Tok.is(tok::colon))
1553 Data.ColonLoc = ConsumeToken();
1554 else {
1555 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1556 : diag::warn_pragma_expected_colon)
1557 << "dependency type";
1558 }
1559 } else if (Kind == OMPC_linear) {
1560 // Try to parse modifier if any.
1561 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1562 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1563 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1564 Data.DepLinMapLoc = ConsumeToken();
1565 LinearT.consumeOpen();
1566 NeedRParenForLinear = true;
1567 }
1568 } else if (Kind == OMPC_map) {
1569 // Handle map type for map clause.
1570 ColonProtectionRAIIObject ColonRAII(*this);
1571
1572 /// The map clause modifier token can be either a identifier or the C++
1573 /// delete keyword.
1574 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1575 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1576 };
1577
1578 // The first identifier may be a list item, a map-type or a
1579 // map-type-modifier. The map modifier can also be delete which has the same
1580 // spelling of the C++ delete keyword.
1581 Data.MapType =
1582 IsMapClauseModifierToken(Tok)
1583 ? static_cast<OpenMPMapClauseKind>(
1584 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1585 : OMPC_MAP_unknown;
1586 Data.DepLinMapLoc = Tok.getLocation();
1587 bool ColonExpected = false;
1588
1589 if (IsMapClauseModifierToken(Tok)) {
1590 if (PP.LookAhead(0).is(tok::colon)) {
1591 if (Data.MapType == OMPC_MAP_unknown)
1592 Diag(Tok, diag::err_omp_unknown_map_type);
1593 else if (Data.MapType == OMPC_MAP_always)
1594 Diag(Tok, diag::err_omp_map_type_missing);
1595 ConsumeToken();
1596 } else if (PP.LookAhead(0).is(tok::comma)) {
1597 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1598 PP.LookAhead(2).is(tok::colon)) {
1599 Data.MapTypeModifier = Data.MapType;
1600 if (Data.MapTypeModifier != OMPC_MAP_always) {
1601 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1602 Data.MapTypeModifier = OMPC_MAP_unknown;
1603 } else
1604 MapTypeModifierSpecified = true;
1605
1606 ConsumeToken();
1607 ConsumeToken();
1608
1609 Data.MapType =
1610 IsMapClauseModifierToken(Tok)
1611 ? static_cast<OpenMPMapClauseKind>(
1612 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1613 : OMPC_MAP_unknown;
1614 if (Data.MapType == OMPC_MAP_unknown ||
1615 Data.MapType == OMPC_MAP_always)
1616 Diag(Tok, diag::err_omp_unknown_map_type);
1617 ConsumeToken();
1618 } else {
1619 Data.MapType = OMPC_MAP_tofrom;
1620 Data.IsMapTypeImplicit = true;
1621 }
1622 } else {
1623 Data.MapType = OMPC_MAP_tofrom;
1624 Data.IsMapTypeImplicit = true;
1625 }
1626 } else {
1627 Data.MapType = OMPC_MAP_tofrom;
1628 Data.IsMapTypeImplicit = true;
1629 }
1630
1631 if (Tok.is(tok::colon))
1632 Data.ColonLoc = ConsumeToken();
1633 else if (ColonExpected)
1634 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1635 }
1636
1637 bool IsComma =
1638 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1639 (Kind == OMPC_reduction && !InvalidReductionId) ||
1640 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1641 (!MapTypeModifierSpecified ||
1642 Data.MapTypeModifier == OMPC_MAP_always)) ||
1643 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1644 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1645 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1646 Tok.isNot(tok::annot_pragma_openmp_end))) {
1647 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1648 // Parse variable
1649 ExprResult VarExpr =
1650 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1651 if (VarExpr.isUsable())
1652 Vars.push_back(VarExpr.get());
1653 else {
1654 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1655 StopBeforeMatch);
1656 }
1657 // Skip ',' if any
1658 IsComma = Tok.is(tok::comma);
1659 if (IsComma)
1660 ConsumeToken();
1661 else if (Tok.isNot(tok::r_paren) &&
1662 Tok.isNot(tok::annot_pragma_openmp_end) &&
1663 (!MayHaveTail || Tok.isNot(tok::colon)))
1664 Diag(Tok, diag::err_omp_expected_punc)
1665 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1666 : getOpenMPClauseName(Kind))
1667 << (Kind == OMPC_flush);
1668 }
1669
1670 // Parse ')' for linear clause with modifier.
1671 if (NeedRParenForLinear)
1672 LinearT.consumeClose();
1673
1674 // Parse ':' linear-step (or ':' alignment).
1675 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1676 if (MustHaveTail) {
1677 Data.ColonLoc = Tok.getLocation();
1678 SourceLocation ELoc = ConsumeToken();
1679 ExprResult Tail = ParseAssignmentExpression();
1680 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1681 if (Tail.isUsable())
1682 Data.TailExpr = Tail.get();
1683 else
1684 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1685 StopBeforeMatch);
1686 }
1687
1688 // Parse ')'.
1689 T.consumeClose();
1690 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1691 Vars.empty()) ||
1692 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1693 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1694 return true;
1695 return false;
1696}
1697
Alexander Musman1bb328c2014-06-04 13:06:39 +00001698/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001699/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001700///
1701/// private-clause:
1702/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001703/// firstprivate-clause:
1704/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001705/// lastprivate-clause:
1706/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001707/// shared-clause:
1708/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001709/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001710/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001711/// aligned-clause:
1712/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001713/// reduction-clause:
1714/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001715/// copyprivate-clause:
1716/// 'copyprivate' '(' list ')'
1717/// flush-clause:
1718/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001719/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001720/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001721/// map-clause:
1722/// 'map' '(' [ [ always , ]
1723/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001724///
Alexey Bataev182227b2015-08-20 10:54:39 +00001725/// For 'linear' clause linear-list may have the following forms:
1726/// list
1727/// modifier(list)
1728/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001729OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1730 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001731 SourceLocation Loc = Tok.getLocation();
1732 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001733 SmallVector<Expr *, 4> Vars;
1734 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001735
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001736 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001737 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001738
Alexey Bataevc5e02582014-06-16 07:08:35 +00001739 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001740 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1741 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1742 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1743 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001744}
1745