blob: 3553fe062da928c242e193c3b5dc3911500ab59c [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,
Samuel Antao686c70c2016-05-26 17:30:50 +000041 OMPD_target_exit,
42 OMPD_update,
Dmitry Polukhin82478332016-02-13 06:53:38 +000043};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000044
45class ThreadprivateListParserHelper final {
46 SmallVector<Expr *, 4> Identifiers;
47 Parser *P;
48
49public:
50 ThreadprivateListParserHelper(Parser *P) : P(P) {}
51 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
52 ExprResult Res =
53 P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
54 if (Res.isUsable())
55 Identifiers.push_back(Res.get());
56 }
57 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
58};
Dmitry Polukhin82478332016-02-13 06:53:38 +000059} // namespace
60
61// Map token string to extended OMP token kind that are
62// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
63static unsigned getOpenMPDirectiveKindEx(StringRef S) {
64 auto DKind = getOpenMPDirectiveKind(S);
65 if (DKind != OMPD_unknown)
66 return DKind;
67
68 return llvm::StringSwitch<unsigned>(S)
69 .Case("cancellation", OMPD_cancellation)
70 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000071 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000072 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000073 .Case("enter", OMPD_enter)
74 .Case("exit", OMPD_exit)
75 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000076 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000077 .Case("update", OMPD_update)
Dmitry Polukhin82478332016-02-13 06:53:38 +000078 .Default(OMPD_unknown);
79}
80
Alexey Bataev4acb8592014-07-07 13:01:15 +000081static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000082 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
83 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
84 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 static const unsigned F[][3] = {
86 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000087 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000088 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000089 { OMPD_declare, OMPD_target, OMPD_declare_target },
90 { OMPD_end, OMPD_declare, OMPD_end_declare },
91 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 { OMPD_target, OMPD_data, OMPD_target_data },
93 { OMPD_target, OMPD_enter, OMPD_target_enter },
94 { OMPD_target, OMPD_exit, OMPD_target_exit },
Samuel Antao686c70c2016-05-26 17:30:50 +000095 { OMPD_target, OMPD_update, OMPD_target_update },
Dmitry Polukhin82478332016-02-13 06:53:38 +000096 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
97 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
98 { OMPD_for, OMPD_simd, OMPD_for_simd },
99 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
100 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
101 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
102 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
103 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
104 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
105 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000106 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +0000107 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +0000108 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000109 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000110 ? static_cast<unsigned>(OMPD_unknown)
111 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
112 if (DKind == OMPD_unknown)
113 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000114
Alexander Musmanf82886e2014-09-18 05:12:34 +0000115 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000116 if (DKind != F[i][0])
117 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000118
Dmitry Polukhin82478332016-02-13 06:53:38 +0000119 Tok = P.getPreprocessor().LookAhead(0);
120 unsigned SDKind =
121 Tok.isAnnotation()
122 ? static_cast<unsigned>(OMPD_unknown)
123 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
124 if (SDKind == OMPD_unknown)
125 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000126
Dmitry Polukhin82478332016-02-13 06:53:38 +0000127 if (SDKind == F[i][1]) {
128 P.ConsumeToken();
129 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000130 }
131 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000132 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
133 : OMPD_unknown;
134}
135
136static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000137 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000138 Sema &Actions = P.getActions();
139 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000140 // Allow to use 'operator' keyword for C++ operators
141 bool WithOperator = false;
142 if (Tok.is(tok::kw_operator)) {
143 P.ConsumeToken();
144 Tok = P.getCurToken();
145 WithOperator = true;
146 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000147 switch (Tok.getKind()) {
148 case tok::plus: // '+'
149 OOK = OO_Plus;
150 break;
151 case tok::minus: // '-'
152 OOK = OO_Minus;
153 break;
154 case tok::star: // '*'
155 OOK = OO_Star;
156 break;
157 case tok::amp: // '&'
158 OOK = OO_Amp;
159 break;
160 case tok::pipe: // '|'
161 OOK = OO_Pipe;
162 break;
163 case tok::caret: // '^'
164 OOK = OO_Caret;
165 break;
166 case tok::ampamp: // '&&'
167 OOK = OO_AmpAmp;
168 break;
169 case tok::pipepipe: // '||'
170 OOK = OO_PipePipe;
171 break;
172 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000173 if (!WithOperator)
174 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000175 default:
176 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
177 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
178 Parser::StopBeforeMatch);
179 return DeclarationName();
180 }
181 P.ConsumeToken();
182 auto &DeclNames = Actions.getASTContext().DeclarationNames;
183 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
184 : DeclNames.getCXXOperatorName(OOK);
185}
186
187/// \brief Parse 'omp declare reduction' construct.
188///
189/// declare-reduction-directive:
190/// annot_pragma_openmp 'declare' 'reduction'
191/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
192/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
193/// annot_pragma_openmp_end
194/// <reduction_id> is either a base language identifier or one of the following
195/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
196///
197Parser::DeclGroupPtrTy
198Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
199 // Parse '('.
200 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
201 if (T.expectAndConsume(diag::err_expected_lparen_after,
202 getOpenMPDirectiveName(OMPD_declare_reduction))) {
203 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
204 return DeclGroupPtrTy();
205 }
206
207 DeclarationName Name = parseOpenMPReductionId(*this);
208 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
209 return DeclGroupPtrTy();
210
211 // Consume ':'.
212 bool IsCorrect = !ExpectAndConsume(tok::colon);
213
214 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
215 return DeclGroupPtrTy();
216
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000217 IsCorrect = IsCorrect && !Name.isEmpty();
218
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000219 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
220 Diag(Tok.getLocation(), diag::err_expected_type);
221 IsCorrect = false;
222 }
223
224 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
225 return DeclGroupPtrTy();
226
227 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
228 // Parse list of types until ':' token.
229 do {
230 ColonProtectionRAIIObject ColonRAII(*this);
231 SourceRange Range;
232 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
233 if (TR.isUsable()) {
234 auto ReductionType =
235 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
236 if (!ReductionType.isNull()) {
237 ReductionTypes.push_back(
238 std::make_pair(ReductionType, Range.getBegin()));
239 }
240 } else {
241 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
242 StopBeforeMatch);
243 }
244
245 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
246 break;
247
248 // Consume ','.
249 if (ExpectAndConsume(tok::comma)) {
250 IsCorrect = false;
251 if (Tok.is(tok::annot_pragma_openmp_end)) {
252 Diag(Tok.getLocation(), diag::err_expected_type);
253 return DeclGroupPtrTy();
254 }
255 }
256 } while (Tok.isNot(tok::annot_pragma_openmp_end));
257
258 if (ReductionTypes.empty()) {
259 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
260 return DeclGroupPtrTy();
261 }
262
263 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
264 return DeclGroupPtrTy();
265
266 // Consume ':'.
267 if (ExpectAndConsume(tok::colon))
268 IsCorrect = false;
269
270 if (Tok.is(tok::annot_pragma_openmp_end)) {
271 Diag(Tok.getLocation(), diag::err_expected_expression);
272 return DeclGroupPtrTy();
273 }
274
275 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
276 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
277
278 // Parse <combiner> expression and then parse initializer if any for each
279 // correct type.
280 unsigned I = 0, E = ReductionTypes.size();
281 for (auto *D : DRD.get()) {
282 TentativeParsingAction TPA(*this);
283 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
284 Scope::OpenMPDirectiveScope);
285 // Parse <combiner> expression.
286 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
287 ExprResult CombinerResult =
288 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
289 D->getLocation(), /*DiscardedValue=*/true);
290 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
291
292 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
293 Tok.isNot(tok::annot_pragma_openmp_end)) {
294 TPA.Commit();
295 IsCorrect = false;
296 break;
297 }
298 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
299 ExprResult InitializerResult;
300 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
301 // Parse <initializer> expression.
302 if (Tok.is(tok::identifier) &&
303 Tok.getIdentifierInfo()->isStr("initializer"))
304 ConsumeToken();
305 else {
306 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
307 TPA.Commit();
308 IsCorrect = false;
309 break;
310 }
311 // Parse '('.
312 BalancedDelimiterTracker T(*this, tok::l_paren,
313 tok::annot_pragma_openmp_end);
314 IsCorrect =
315 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
316 IsCorrect;
317 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
318 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
319 Scope::OpenMPDirectiveScope);
320 // Parse expression.
321 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
322 InitializerResult = Actions.ActOnFinishFullExpr(
323 ParseAssignmentExpression().get(), D->getLocation(),
324 /*DiscardedValue=*/true);
325 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
326 D, InitializerResult.get());
327 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
328 Tok.isNot(tok::annot_pragma_openmp_end)) {
329 TPA.Commit();
330 IsCorrect = false;
331 break;
332 }
333 IsCorrect =
334 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
335 }
336 }
337
338 ++I;
339 // Revert parsing if not the last type, otherwise accept it, we're done with
340 // parsing.
341 if (I != E)
342 TPA.Revert();
343 else
344 TPA.Commit();
345 }
346 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
347 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000348}
349
Alexey Bataev2af33e32016-04-07 12:45:37 +0000350namespace {
351/// RAII that recreates function context for correct parsing of clauses of
352/// 'declare simd' construct.
353/// OpenMP, 2.8.2 declare simd Construct
354/// The expressions appearing in the clauses of this directive are evaluated in
355/// the scope of the arguments of the function declaration or definition.
356class FNContextRAII final {
357 Parser &P;
358 Sema::CXXThisScopeRAII *ThisScope;
359 Parser::ParseScope *TempScope;
360 Parser::ParseScope *FnScope;
361 bool HasTemplateScope = false;
362 bool HasFunScope = false;
363 FNContextRAII() = delete;
364 FNContextRAII(const FNContextRAII &) = delete;
365 FNContextRAII &operator=(const FNContextRAII &) = delete;
366
367public:
368 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
369 Decl *D = *Ptr.get().begin();
370 NamedDecl *ND = dyn_cast<NamedDecl>(D);
371 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
372 Sema &Actions = P.getActions();
373
374 // Allow 'this' within late-parsed attributes.
375 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
376 ND && ND->isCXXInstanceMember());
377
378 // If the Decl is templatized, add template parameters to scope.
379 HasTemplateScope = D->isTemplateDecl();
380 TempScope =
381 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
382 if (HasTemplateScope)
383 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
384
385 // If the Decl is on a function, add function parameters to the scope.
386 HasFunScope = D->isFunctionOrFunctionTemplate();
387 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
388 HasFunScope);
389 if (HasFunScope)
390 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
391 }
392 ~FNContextRAII() {
393 if (HasFunScope) {
394 P.getActions().ActOnExitFunctionContext();
395 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
396 }
397 if (HasTemplateScope)
398 TempScope->Exit();
399 delete FnScope;
400 delete TempScope;
401 delete ThisScope;
402 }
403};
404} // namespace
405
Alexey Bataevd93d3762016-04-12 09:35:56 +0000406/// Parses clauses for 'declare simd' directive.
407/// clause:
408/// 'inbranch' | 'notinbranch'
409/// 'simdlen' '(' <expr> ')'
410/// { 'uniform' '(' <argument_list> ')' }
411/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000412/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
413static bool parseDeclareSimdClauses(
414 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
415 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
416 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
417 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000418 SourceRange BSRange;
419 const Token &Tok = P.getCurToken();
420 bool IsError = false;
421 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
422 if (Tok.isNot(tok::identifier))
423 break;
424 OMPDeclareSimdDeclAttr::BranchStateTy Out;
425 IdentifierInfo *II = Tok.getIdentifierInfo();
426 StringRef ClauseName = II->getName();
427 // Parse 'inranch|notinbranch' clauses.
428 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
429 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
430 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
431 << ClauseName
432 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
433 IsError = true;
434 }
435 BS = Out;
436 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
437 P.ConsumeToken();
438 } else if (ClauseName.equals("simdlen")) {
439 if (SimdLen.isUsable()) {
440 P.Diag(Tok, diag::err_omp_more_one_clause)
441 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
442 IsError = true;
443 }
444 P.ConsumeToken();
445 SourceLocation RLoc;
446 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
447 if (SimdLen.isInvalid())
448 IsError = true;
449 } else {
450 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000451 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
452 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000453 Parser::OpenMPVarListDataTy Data;
454 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000455 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000456 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000457 else if (CKind == OMPC_linear)
458 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000459
460 P.ConsumeToken();
461 if (P.ParseOpenMPVarList(OMPD_declare_simd,
462 getOpenMPClauseKind(ClauseName), *Vars, Data))
463 IsError = true;
464 if (CKind == OMPC_aligned)
465 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000466 else if (CKind == OMPC_linear) {
467 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
468 Data.DepLinMapLoc))
469 Data.LinKind = OMPC_LINEAR_val;
470 LinModifiers.append(Linears.size() - LinModifiers.size(),
471 Data.LinKind);
472 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
473 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000474 } else
475 // TODO: add parsing of other clauses.
476 break;
477 }
478 // Skip ',' if any.
479 if (Tok.is(tok::comma))
480 P.ConsumeToken();
481 }
482 return IsError;
483}
484
Alexey Bataev2af33e32016-04-07 12:45:37 +0000485/// Parse clauses for '#pragma omp declare simd'.
486Parser::DeclGroupPtrTy
487Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
488 CachedTokens &Toks, SourceLocation Loc) {
489 PP.EnterToken(Tok);
490 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
491 // Consume the previously pushed token.
492 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
493
494 FNContextRAII FnContext(*this, Ptr);
495 OMPDeclareSimdDeclAttr::BranchStateTy BS =
496 OMPDeclareSimdDeclAttr::BS_Undefined;
497 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000498 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000499 SmallVector<Expr *, 4> Aligneds;
500 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000501 SmallVector<Expr *, 4> Linears;
502 SmallVector<unsigned, 4> LinModifiers;
503 SmallVector<Expr *, 4> Steps;
504 bool IsError =
505 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
506 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000507 // Need to check for extra tokens.
508 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
509 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
510 << getOpenMPDirectiveName(OMPD_declare_simd);
511 while (Tok.isNot(tok::annot_pragma_openmp_end))
512 ConsumeAnyToken();
513 }
514 // Skip the last annot_pragma_openmp_end.
515 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000516 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000517 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000518 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
519 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000520 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000521 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000522}
523
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000524/// \brief Parsing of declarative OpenMP directives.
525///
526/// threadprivate-directive:
527/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000528/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000529///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000530/// declare-reduction-directive:
531/// annot_pragma_openmp 'declare' 'reduction' [...]
532/// annot_pragma_openmp_end
533///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000534/// declare-simd-directive:
535/// annot_pragma_openmp 'declare simd' {<clause> [,]}
536/// annot_pragma_openmp_end
537/// <function declaration/definition>
538///
539Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
540 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
541 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000542 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000543 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000544
545 SourceLocation Loc = ConsumeToken();
Alexey Bataev4acb8592014-07-07 13:01:15 +0000546 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000547
548 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000549 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +0000550 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000551 ThreadprivateListParserHelper Helper(this);
552 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000553 // The last seen token is annot_pragma_openmp_end - need to check for
554 // extra tokens.
555 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
556 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000557 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000558 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000559 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000560 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000561 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000562 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
563 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +0000564 }
565 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000566 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000567 case OMPD_declare_reduction:
568 ConsumeToken();
569 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
570 // The last seen token is annot_pragma_openmp_end - need to check for
571 // extra tokens.
572 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
573 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
574 << getOpenMPDirectiveName(OMPD_declare_reduction);
575 while (Tok.isNot(tok::annot_pragma_openmp_end))
576 ConsumeAnyToken();
577 }
578 // Skip the last annot_pragma_openmp_end.
579 ConsumeToken();
580 return Res;
581 }
582 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000583 case OMPD_declare_simd: {
584 // The syntax is:
585 // { #pragma omp declare simd }
586 // <function-declaration-or-definition>
587 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000588 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000589 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000590 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
591 Toks.push_back(Tok);
592 ConsumeAnyToken();
593 }
594 Toks.push_back(Tok);
595 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000596
597 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000598 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000599 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000600 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000601 // Here we expect to see some function declaration.
602 if (AS == AS_none) {
603 assert(TagType == DeclSpec::TST_unspecified);
604 MaybeParseCXX11Attributes(Attrs);
605 MaybeParseMicrosoftAttributes(Attrs);
606 ParsingDeclSpec PDS(*this);
607 Ptr = ParseExternalDeclaration(Attrs, &PDS);
608 } else {
609 Ptr =
610 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
611 }
612 }
613 if (!Ptr) {
614 Diag(Loc, diag::err_omp_decl_in_declare_simd);
615 return DeclGroupPtrTy();
616 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000617 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000618 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000619 case OMPD_declare_target: {
620 SourceLocation DTLoc = ConsumeAnyToken();
621 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000622 // OpenMP 4.5 syntax with list of entities.
623 llvm::SmallSetVector<const NamedDecl*, 16> SameDirectiveDecls;
624 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
625 OMPDeclareTargetDeclAttr::MapTypeTy MT =
626 OMPDeclareTargetDeclAttr::MT_To;
627 if (Tok.is(tok::identifier)) {
628 IdentifierInfo *II = Tok.getIdentifierInfo();
629 StringRef ClauseName = II->getName();
630 // Parse 'to|link' clauses.
631 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
632 MT)) {
633 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
634 << ClauseName;
635 break;
636 }
637 ConsumeToken();
638 }
639 auto Callback = [this, MT, &SameDirectiveDecls](
640 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
641 Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
642 SameDirectiveDecls);
643 };
644 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback, true))
645 break;
646
647 // Consume optional ','.
648 if (Tok.is(tok::comma))
649 ConsumeToken();
650 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000651 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000652 ConsumeAnyToken();
653 return DeclGroupPtrTy();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000654 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000655
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000656 // Skip the last annot_pragma_openmp_end.
657 ConsumeAnyToken();
658
659 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
660 return DeclGroupPtrTy();
661
662 DKind = ParseOpenMPDirectiveKind(*this);
663 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
664 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
665 ParsedAttributesWithRange attrs(AttrFactory);
666 MaybeParseCXX11Attributes(attrs);
667 MaybeParseMicrosoftAttributes(attrs);
668 ParseExternalDeclaration(attrs);
669 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
670 TentativeParsingAction TPA(*this);
671 ConsumeToken();
672 DKind = ParseOpenMPDirectiveKind(*this);
673 if (DKind != OMPD_end_declare_target)
674 TPA.Revert();
675 else
676 TPA.Commit();
677 }
678 }
679
680 if (DKind == OMPD_end_declare_target) {
681 ConsumeAnyToken();
682 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
683 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
684 << getOpenMPDirectiveName(OMPD_end_declare_target);
685 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
686 }
687 // Skip the last annot_pragma_openmp_end.
688 ConsumeAnyToken();
689 } else {
690 Diag(Tok, diag::err_expected_end_declare_target);
691 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
692 }
693 Actions.ActOnFinishOpenMPDeclareTargetDirective();
694 return DeclGroupPtrTy();
695 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000696 case OMPD_unknown:
697 Diag(Tok, diag::err_omp_unknown_directive);
698 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000699 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000700 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000701 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000702 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000703 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000704 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000705 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000706 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000707 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000708 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000709 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000710 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000711 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000712 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000713 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000714 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000715 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000716 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000717 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000718 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000719 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000720 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000721 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000722 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000723 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000724 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000725 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000726 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000727 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000728 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000729 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000730 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000731 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +0000732 case OMPD_target_update:
Alexey Bataeva769e072013-03-22 06:34:35 +0000733 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000734 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000735 break;
736 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000737 while (Tok.isNot(tok::annot_pragma_openmp_end))
738 ConsumeAnyToken();
739 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000740 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000741}
742
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000743/// \brief Parsing of declarative or executable OpenMP directives.
744///
745/// threadprivate-directive:
746/// annot_pragma_openmp 'threadprivate' simple-variable-list
747/// annot_pragma_openmp_end
748///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000749/// declare-reduction-directive:
750/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
751/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
752/// ('omp_priv' '=' <expression>|<function_call>) ')']
753/// annot_pragma_openmp_end
754///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000755/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000756/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000757/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
758/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000759/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000760/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000761/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000762/// 'distribute' | 'target enter data' | 'target exit data' |
Samuel Antao686c70c2016-05-26 17:30:50 +0000763/// 'target parallel' | 'target parallel for' |
764/// 'target update' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000765/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000766///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000767StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
768 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000769 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000770 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000772 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000773 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000774 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000775 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000777 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000778 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000779 // Name of critical directive.
780 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000781 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000782 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000783 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000784
785 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000786 case OMPD_threadprivate: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000787 if (Allowed != ACK_Any) {
788 Diag(Tok, diag::err_omp_immediate_directive)
789 << getOpenMPDirectiveName(DKind) << 0;
790 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000791 ConsumeToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000792 ThreadprivateListParserHelper Helper(this);
793 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000794 // The last seen token is annot_pragma_openmp_end - need to check for
795 // extra tokens.
796 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
797 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000798 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000799 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000800 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000801 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
802 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000803 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
804 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000805 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000806 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000807 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000808 case OMPD_declare_reduction:
809 ConsumeToken();
810 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
811 // The last seen token is annot_pragma_openmp_end - need to check for
812 // extra tokens.
813 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
814 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
815 << getOpenMPDirectiveName(OMPD_declare_reduction);
816 while (Tok.isNot(tok::annot_pragma_openmp_end))
817 ConsumeAnyToken();
818 }
819 ConsumeAnyToken();
820 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
821 } else
822 SkipUntil(tok::annot_pragma_openmp_end);
823 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000824 case OMPD_flush:
825 if (PP.LookAhead(0).is(tok::l_paren)) {
826 FlushHasClause = true;
827 // Push copy of the current token back to stream to properly parse
828 // pseudo-clause OMPFlushClause.
829 PP.EnterToken(Tok);
830 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000831 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000832 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000833 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000834 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000835 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000836 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000837 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +0000838 case OMPD_target_update:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000839 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000840 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000841 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000842 }
843 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000844 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000845 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000846 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000847 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000848 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000849 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000850 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000851 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000852 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000853 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000854 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000855 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000856 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000857 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000858 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000859 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000860 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000861 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000862 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000863 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000864 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000865 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000866 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000867 case OMPD_taskloop_simd:
868 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000869 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000870 // Parse directive name of the 'critical' directive if any.
871 if (DKind == OMPD_critical) {
872 BalancedDelimiterTracker T(*this, tok::l_paren,
873 tok::annot_pragma_openmp_end);
874 if (!T.consumeOpen()) {
875 if (Tok.isAnyIdentifier()) {
876 DirName =
877 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
878 ConsumeAnyToken();
879 } else {
880 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
881 }
882 T.consumeClose();
883 }
Alexey Bataev80909872015-07-02 11:25:17 +0000884 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000885 CancelRegion = ParseOpenMPDirectiveKind(*this);
886 if (Tok.isNot(tok::annot_pragma_openmp_end))
887 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000888 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000889
Alexey Bataevf29276e2014-06-18 04:14:57 +0000890 if (isOpenMPLoopDirective(DKind))
891 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
892 if (isOpenMPSimdDirective(DKind))
893 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
894 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000895 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000896
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000897 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000898 OpenMPClauseKind CKind =
899 Tok.isAnnotation()
900 ? OMPC_unknown
901 : FlushHasClause ? OMPC_flush
902 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000903 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000904 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000905 OMPClause *Clause =
906 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000907 FirstClauses[CKind].setInt(true);
908 if (Clause) {
909 FirstClauses[CKind].setPointer(Clause);
910 Clauses.push_back(Clause);
911 }
912
913 // Skip ',' if any.
914 if (Tok.is(tok::comma))
915 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000916 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000917 }
918 // End location of the directive.
919 EndLoc = Tok.getLocation();
920 // Consume final annot_pragma_openmp_end.
921 ConsumeToken();
922
Alexey Bataeveb482352015-12-18 05:05:56 +0000923 // OpenMP [2.13.8, ordered Construct, Syntax]
924 // If the depend clause is specified, the ordered construct is a stand-alone
925 // directive.
926 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000927 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000928 Diag(Loc, diag::err_omp_immediate_directive)
929 << getOpenMPDirectiveName(DKind) << 1
930 << getOpenMPClauseName(OMPC_depend);
931 }
932 HasAssociatedStatement = false;
933 }
934
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000936 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000937 // The body is a block scope like in Lambdas and Blocks.
938 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000939 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940 Actions.ActOnStartOfCompoundStmt();
941 // Parse statement
942 AssociatedStmt = ParseStatement();
943 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000944 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000945 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000946 Directive = Actions.ActOnOpenMPExecutableDirective(
947 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
948 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949
950 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000951 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000952 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000954 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000955 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000956 case OMPD_declare_target:
957 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000958 Diag(Tok, diag::err_omp_unexpected_directive)
959 << getOpenMPDirectiveName(DKind);
960 SkipUntil(tok::annot_pragma_openmp_end);
961 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000962 case OMPD_unknown:
963 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000964 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000965 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000966 }
967 return Directive;
968}
969
Dmitry Polukhind69b5052016-05-09 14:59:13 +0000970// Parses simple list:
971// simple-variable-list:
972// '(' id-expression {, id-expression} ')'
973//
974bool Parser::ParseOpenMPSimpleVarList(
975 OpenMPDirectiveKind Kind,
976 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
977 Callback,
978 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000979 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000980 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981 if (T.expectAndConsume(diag::err_expected_lparen_after,
982 getOpenMPDirectiveName(Kind)))
983 return true;
984 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000985 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000986
987 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000988 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000989 CXXScopeSpec SS;
990 SourceLocation TemplateKWLoc;
991 UnqualifiedId Name;
992 // Read var name.
993 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000994 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000995
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000996 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000997 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000998 IsCorrect = false;
999 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001000 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +00001001 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001002 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001003 IsCorrect = false;
1004 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001005 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001006 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
1007 Tok.isNot(tok::annot_pragma_openmp_end)) {
1008 IsCorrect = false;
1009 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001010 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00001011 Diag(PrevTok.getLocation(), diag::err_expected)
1012 << tok::identifier
1013 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00001014 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001015 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00001016 }
1017 // Consume ','.
1018 if (Tok.is(tok::comma)) {
1019 ConsumeToken();
1020 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001021 }
1022
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001023 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00001024 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001025 IsCorrect = false;
1026 }
1027
1028 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001029 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001030
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001031 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00001032}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001033
1034/// \brief Parsing of OpenMP clauses.
1035///
1036/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00001037/// if-clause | final-clause | num_threads-clause | safelen-clause |
1038/// default-clause | private-clause | firstprivate-clause | shared-clause
1039/// | linear-clause | aligned-clause | collapse-clause |
1040/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001041/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00001042/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00001043/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001044/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001045/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +00001046/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001047///
1048OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1049 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001050 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001051 bool ErrorFound = false;
1052 // Check if clause is allowed for the given directive.
1053 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001054 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1055 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001056 ErrorFound = true;
1057 }
1058
1059 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001060 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001061 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001062 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001063 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001064 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001065 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001066 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001067 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001068 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001069 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001070 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001071 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001072 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001073 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001074 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001075 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001076 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001077 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001078 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001079 // OpenMP [2.9.1, target data construct, Restrictions]
1080 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001081 // OpenMP [2.11.1, task Construct, Restrictions]
1082 // At most one if clause can appear on the directive.
1083 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001084 // OpenMP [teams Construct, Restrictions]
1085 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001086 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001087 // OpenMP [2.9.1, task Construct, Restrictions]
1088 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001089 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1090 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001091 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1092 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001093 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001094 Diag(Tok, diag::err_omp_more_one_clause)
1095 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001096 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001097 }
1098
Alexey Bataev10e775f2015-07-30 11:36:16 +00001099 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1100 Clause = ParseOpenMPClause(CKind);
1101 else
1102 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001103 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001104 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001105 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001106 // OpenMP [2.14.3.1, Restrictions]
1107 // Only a single default clause may be specified on a parallel, task or
1108 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001109 // OpenMP [2.5, parallel Construct, Restrictions]
1110 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001111 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001112 Diag(Tok, diag::err_omp_more_one_clause)
1113 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001114 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001115 }
1116
1117 Clause = ParseOpenMPSimpleClause(CKind);
1118 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001119 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001120 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001121 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001122 // OpenMP [2.7.1, Restrictions, p. 3]
1123 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001124 // OpenMP [2.10.4, Restrictions, p. 106]
1125 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001126 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001127 Diag(Tok, diag::err_omp_more_one_clause)
1128 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001129 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001130 }
1131
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001132 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001133 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1134 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001135 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001136 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001137 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001138 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001139 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001140 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001141 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001142 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001143 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001144 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001145 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001146 // OpenMP [2.7.1, Restrictions, p. 9]
1147 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001148 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1149 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001150 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001151 Diag(Tok, diag::err_omp_more_one_clause)
1152 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001153 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001154 }
1155
1156 Clause = ParseOpenMPClause(CKind);
1157 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001158 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001159 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001160 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001161 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001162 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001163 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001164 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001165 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001166 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001167 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001168 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001169 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +00001170 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001171 break;
1172 case OMPC_unknown:
1173 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001174 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001175 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001176 break;
1177 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001178 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001179 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1180 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001181 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001182 break;
1183 }
Craig Topper161e4db2014-05-21 06:02:52 +00001184 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001185}
1186
Alexey Bataev2af33e32016-04-07 12:45:37 +00001187/// Parses simple expression in parens for single-expression clauses of OpenMP
1188/// constructs.
1189/// \param RLoc Returned location of right paren.
1190ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1191 SourceLocation &RLoc) {
1192 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1193 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1194 return ExprError();
1195
1196 SourceLocation ELoc = Tok.getLocation();
1197 ExprResult LHS(ParseCastExpression(
1198 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1199 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1200 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1201
1202 // Parse ')'.
1203 T.consumeClose();
1204
1205 RLoc = T.getCloseLocation();
1206 return Val;
1207}
1208
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001209/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001210/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001211/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001212///
Alexey Bataev3778b602014-07-17 07:32:53 +00001213/// final-clause:
1214/// 'final' '(' expression ')'
1215///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001216/// num_threads-clause:
1217/// 'num_threads' '(' expression ')'
1218///
1219/// safelen-clause:
1220/// 'safelen' '(' expression ')'
1221///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001222/// simdlen-clause:
1223/// 'simdlen' '(' expression ')'
1224///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001225/// collapse-clause:
1226/// 'collapse' '(' expression ')'
1227///
Alexey Bataeva0569352015-12-01 10:17:31 +00001228/// priority-clause:
1229/// 'priority' '(' expression ')'
1230///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001231/// grainsize-clause:
1232/// 'grainsize' '(' expression ')'
1233///
Alexey Bataev382967a2015-12-08 12:06:20 +00001234/// num_tasks-clause:
1235/// 'num_tasks' '(' expression ')'
1236///
Alexey Bataev28c75412015-12-15 08:19:24 +00001237/// hint-clause:
1238/// 'hint' '(' expression ')'
1239///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001240OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1241 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001242 SourceLocation LLoc = Tok.getLocation();
1243 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001244
Alexey Bataev2af33e32016-04-07 12:45:37 +00001245 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001246
1247 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001248 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001249
Alexey Bataev2af33e32016-04-07 12:45:37 +00001250 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001251}
1252
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001253/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001254///
1255/// default-clause:
1256/// 'default' '(' 'none' | 'shared' ')
1257///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001258/// proc_bind-clause:
1259/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1260///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001261OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1262 SourceLocation Loc = Tok.getLocation();
1263 SourceLocation LOpen = ConsumeToken();
1264 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001265 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001266 if (T.expectAndConsume(diag::err_expected_lparen_after,
1267 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001268 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001269
Alexey Bataeva55ed262014-05-28 06:15:33 +00001270 unsigned Type = getOpenMPSimpleClauseType(
1271 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001272 SourceLocation TypeLoc = Tok.getLocation();
1273 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1274 Tok.isNot(tok::annot_pragma_openmp_end))
1275 ConsumeAnyToken();
1276
1277 // Parse ')'.
1278 T.consumeClose();
1279
1280 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1281 Tok.getLocation());
1282}
1283
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001284/// \brief Parsing of OpenMP clauses like 'ordered'.
1285///
1286/// ordered-clause:
1287/// 'ordered'
1288///
Alexey Bataev236070f2014-06-20 11:19:47 +00001289/// nowait-clause:
1290/// 'nowait'
1291///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001292/// untied-clause:
1293/// 'untied'
1294///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001295/// mergeable-clause:
1296/// 'mergeable'
1297///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001298/// read-clause:
1299/// 'read'
1300///
Alexey Bataev346265e2015-09-25 10:37:12 +00001301/// threads-clause:
1302/// 'threads'
1303///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001304/// simd-clause:
1305/// 'simd'
1306///
Alexey Bataevb825de12015-12-07 10:51:44 +00001307/// nogroup-clause:
1308/// 'nogroup'
1309///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001310OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1311 SourceLocation Loc = Tok.getLocation();
1312 ConsumeAnyToken();
1313
1314 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1315}
1316
1317
Alexey Bataev56dafe82014-06-20 07:16:17 +00001318/// \brief Parsing of OpenMP clauses with single expressions and some additional
1319/// argument like 'schedule' or 'dist_schedule'.
1320///
1321/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001322/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1323/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001324///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001325/// if-clause:
1326/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1327///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001328/// defaultmap:
1329/// 'defaultmap' '(' modifier ':' kind ')'
1330///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001331OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1332 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001333 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001334 // Parse '('.
1335 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1336 if (T.expectAndConsume(diag::err_expected_lparen_after,
1337 getOpenMPClauseName(Kind)))
1338 return nullptr;
1339
1340 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001341 SmallVector<unsigned, 4> Arg;
1342 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001343 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001344 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1345 Arg.resize(NumberOfElements);
1346 KLoc.resize(NumberOfElements);
1347 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1348 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1349 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1350 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001351 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001352 if (KindModifier > OMPC_SCHEDULE_unknown) {
1353 // Parse 'modifier'
1354 Arg[Modifier1] = KindModifier;
1355 KLoc[Modifier1] = Tok.getLocation();
1356 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1357 Tok.isNot(tok::annot_pragma_openmp_end))
1358 ConsumeAnyToken();
1359 if (Tok.is(tok::comma)) {
1360 // Parse ',' 'modifier'
1361 ConsumeAnyToken();
1362 KindModifier = getOpenMPSimpleClauseType(
1363 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1364 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1365 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001366 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001367 KLoc[Modifier2] = Tok.getLocation();
1368 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1369 Tok.isNot(tok::annot_pragma_openmp_end))
1370 ConsumeAnyToken();
1371 }
1372 // Parse ':'
1373 if (Tok.is(tok::colon))
1374 ConsumeAnyToken();
1375 else
1376 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1377 KindModifier = getOpenMPSimpleClauseType(
1378 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1379 }
1380 Arg[ScheduleKind] = KindModifier;
1381 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001382 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1383 Tok.isNot(tok::annot_pragma_openmp_end))
1384 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001385 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1386 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1387 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001388 Tok.is(tok::comma))
1389 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001390 } else if (Kind == OMPC_dist_schedule) {
1391 Arg.push_back(getOpenMPSimpleClauseType(
1392 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1393 KLoc.push_back(Tok.getLocation());
1394 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1395 Tok.isNot(tok::annot_pragma_openmp_end))
1396 ConsumeAnyToken();
1397 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1398 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001399 } else if (Kind == OMPC_defaultmap) {
1400 // Get a defaultmap modifier
1401 Arg.push_back(getOpenMPSimpleClauseType(
1402 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1403 KLoc.push_back(Tok.getLocation());
1404 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1405 Tok.isNot(tok::annot_pragma_openmp_end))
1406 ConsumeAnyToken();
1407 // Parse ':'
1408 if (Tok.is(tok::colon))
1409 ConsumeAnyToken();
1410 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1411 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1412 // Get a defaultmap kind
1413 Arg.push_back(getOpenMPSimpleClauseType(
1414 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1415 KLoc.push_back(Tok.getLocation());
1416 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1417 Tok.isNot(tok::annot_pragma_openmp_end))
1418 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001419 } else {
1420 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001421 KLoc.push_back(Tok.getLocation());
1422 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1423 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001424 ConsumeToken();
1425 if (Tok.is(tok::colon))
1426 DelimLoc = ConsumeToken();
1427 else
1428 Diag(Tok, diag::warn_pragma_expected_colon)
1429 << "directive name modifier";
1430 }
1431 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001432
Carlo Bertollib4adf552016-01-15 18:50:31 +00001433 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1434 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1435 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001436 if (NeedAnExpression) {
1437 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001438 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1439 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001440 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001441 }
1442
1443 // Parse ')'.
1444 T.consumeClose();
1445
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001446 if (NeedAnExpression && Val.isInvalid())
1447 return nullptr;
1448
Alexey Bataev56dafe82014-06-20 07:16:17 +00001449 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001450 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001451 T.getCloseLocation());
1452}
1453
Alexey Bataevc5e02582014-06-16 07:08:35 +00001454static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1455 UnqualifiedId &ReductionId) {
1456 SourceLocation TemplateKWLoc;
1457 if (ReductionIdScopeSpec.isEmpty()) {
1458 auto OOK = OO_None;
1459 switch (P.getCurToken().getKind()) {
1460 case tok::plus:
1461 OOK = OO_Plus;
1462 break;
1463 case tok::minus:
1464 OOK = OO_Minus;
1465 break;
1466 case tok::star:
1467 OOK = OO_Star;
1468 break;
1469 case tok::amp:
1470 OOK = OO_Amp;
1471 break;
1472 case tok::pipe:
1473 OOK = OO_Pipe;
1474 break;
1475 case tok::caret:
1476 OOK = OO_Caret;
1477 break;
1478 case tok::ampamp:
1479 OOK = OO_AmpAmp;
1480 break;
1481 case tok::pipepipe:
1482 OOK = OO_PipePipe;
1483 break;
1484 default:
1485 break;
1486 }
1487 if (OOK != OO_None) {
1488 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001489 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001490 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1491 return false;
1492 }
1493 }
1494 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1495 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001496 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001497 TemplateKWLoc, ReductionId);
1498}
1499
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001500/// Parses clauses with list.
1501bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1502 OpenMPClauseKind Kind,
1503 SmallVectorImpl<Expr *> &Vars,
1504 OpenMPVarListDataTy &Data) {
1505 UnqualifiedId UnqualifiedReductionId;
1506 bool InvalidReductionId = false;
1507 bool MapTypeModifierSpecified = false;
1508
1509 // Parse '('.
1510 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1511 if (T.expectAndConsume(diag::err_expected_lparen_after,
1512 getOpenMPClauseName(Kind)))
1513 return true;
1514
1515 bool NeedRParenForLinear = false;
1516 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1517 tok::annot_pragma_openmp_end);
1518 // Handle reduction-identifier for reduction clause.
1519 if (Kind == OMPC_reduction) {
1520 ColonProtectionRAIIObject ColonRAII(*this);
1521 if (getLangOpts().CPlusPlus)
1522 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1523 /*ObjectType=*/nullptr,
1524 /*EnteringContext=*/false);
1525 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1526 UnqualifiedReductionId);
1527 if (InvalidReductionId) {
1528 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1529 StopBeforeMatch);
1530 }
1531 if (Tok.is(tok::colon))
1532 Data.ColonLoc = ConsumeToken();
1533 else
1534 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1535 if (!InvalidReductionId)
1536 Data.ReductionId =
1537 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1538 } else if (Kind == OMPC_depend) {
1539 // Handle dependency type for depend clause.
1540 ColonProtectionRAIIObject ColonRAII(*this);
1541 Data.DepKind =
1542 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1543 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1544 Data.DepLinMapLoc = Tok.getLocation();
1545
1546 if (Data.DepKind == OMPC_DEPEND_unknown) {
1547 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1548 StopBeforeMatch);
1549 } else {
1550 ConsumeToken();
1551 // Special processing for depend(source) clause.
1552 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1553 // Parse ')'.
1554 T.consumeClose();
1555 return false;
1556 }
1557 }
1558 if (Tok.is(tok::colon))
1559 Data.ColonLoc = ConsumeToken();
1560 else {
1561 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1562 : diag::warn_pragma_expected_colon)
1563 << "dependency type";
1564 }
1565 } else if (Kind == OMPC_linear) {
1566 // Try to parse modifier if any.
1567 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1568 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1569 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1570 Data.DepLinMapLoc = ConsumeToken();
1571 LinearT.consumeOpen();
1572 NeedRParenForLinear = true;
1573 }
1574 } else if (Kind == OMPC_map) {
1575 // Handle map type for map clause.
1576 ColonProtectionRAIIObject ColonRAII(*this);
1577
1578 /// The map clause modifier token can be either a identifier or the C++
1579 /// delete keyword.
1580 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1581 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1582 };
1583
1584 // The first identifier may be a list item, a map-type or a
1585 // map-type-modifier. The map modifier can also be delete which has the same
1586 // spelling of the C++ delete keyword.
1587 Data.MapType =
1588 IsMapClauseModifierToken(Tok)
1589 ? static_cast<OpenMPMapClauseKind>(
1590 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1591 : OMPC_MAP_unknown;
1592 Data.DepLinMapLoc = Tok.getLocation();
1593 bool ColonExpected = false;
1594
1595 if (IsMapClauseModifierToken(Tok)) {
1596 if (PP.LookAhead(0).is(tok::colon)) {
1597 if (Data.MapType == OMPC_MAP_unknown)
1598 Diag(Tok, diag::err_omp_unknown_map_type);
1599 else if (Data.MapType == OMPC_MAP_always)
1600 Diag(Tok, diag::err_omp_map_type_missing);
1601 ConsumeToken();
1602 } else if (PP.LookAhead(0).is(tok::comma)) {
1603 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1604 PP.LookAhead(2).is(tok::colon)) {
1605 Data.MapTypeModifier = Data.MapType;
1606 if (Data.MapTypeModifier != OMPC_MAP_always) {
1607 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1608 Data.MapTypeModifier = OMPC_MAP_unknown;
1609 } else
1610 MapTypeModifierSpecified = true;
1611
1612 ConsumeToken();
1613 ConsumeToken();
1614
1615 Data.MapType =
1616 IsMapClauseModifierToken(Tok)
1617 ? static_cast<OpenMPMapClauseKind>(
1618 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1619 : OMPC_MAP_unknown;
1620 if (Data.MapType == OMPC_MAP_unknown ||
1621 Data.MapType == OMPC_MAP_always)
1622 Diag(Tok, diag::err_omp_unknown_map_type);
1623 ConsumeToken();
1624 } else {
1625 Data.MapType = OMPC_MAP_tofrom;
1626 Data.IsMapTypeImplicit = true;
1627 }
1628 } else {
1629 Data.MapType = OMPC_MAP_tofrom;
1630 Data.IsMapTypeImplicit = true;
1631 }
1632 } else {
1633 Data.MapType = OMPC_MAP_tofrom;
1634 Data.IsMapTypeImplicit = true;
1635 }
1636
1637 if (Tok.is(tok::colon))
1638 Data.ColonLoc = ConsumeToken();
1639 else if (ColonExpected)
1640 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1641 }
1642
1643 bool IsComma =
1644 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1645 (Kind == OMPC_reduction && !InvalidReductionId) ||
1646 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1647 (!MapTypeModifierSpecified ||
1648 Data.MapTypeModifier == OMPC_MAP_always)) ||
1649 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1650 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1651 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1652 Tok.isNot(tok::annot_pragma_openmp_end))) {
1653 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1654 // Parse variable
1655 ExprResult VarExpr =
1656 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1657 if (VarExpr.isUsable())
1658 Vars.push_back(VarExpr.get());
1659 else {
1660 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1661 StopBeforeMatch);
1662 }
1663 // Skip ',' if any
1664 IsComma = Tok.is(tok::comma);
1665 if (IsComma)
1666 ConsumeToken();
1667 else if (Tok.isNot(tok::r_paren) &&
1668 Tok.isNot(tok::annot_pragma_openmp_end) &&
1669 (!MayHaveTail || Tok.isNot(tok::colon)))
1670 Diag(Tok, diag::err_omp_expected_punc)
1671 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1672 : getOpenMPClauseName(Kind))
1673 << (Kind == OMPC_flush);
1674 }
1675
1676 // Parse ')' for linear clause with modifier.
1677 if (NeedRParenForLinear)
1678 LinearT.consumeClose();
1679
1680 // Parse ':' linear-step (or ':' alignment).
1681 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1682 if (MustHaveTail) {
1683 Data.ColonLoc = Tok.getLocation();
1684 SourceLocation ELoc = ConsumeToken();
1685 ExprResult Tail = ParseAssignmentExpression();
1686 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1687 if (Tail.isUsable())
1688 Data.TailExpr = Tail.get();
1689 else
1690 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1691 StopBeforeMatch);
1692 }
1693
1694 // Parse ')'.
1695 T.consumeClose();
1696 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1697 Vars.empty()) ||
1698 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1699 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1700 return true;
1701 return false;
1702}
1703
Alexander Musman1bb328c2014-06-04 13:06:39 +00001704/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001705/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001706///
1707/// private-clause:
1708/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001709/// firstprivate-clause:
1710/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001711/// lastprivate-clause:
1712/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001713/// shared-clause:
1714/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001715/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001716/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001717/// aligned-clause:
1718/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001719/// reduction-clause:
1720/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001721/// copyprivate-clause:
1722/// 'copyprivate' '(' list ')'
1723/// flush-clause:
1724/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001725/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001726/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001727/// map-clause:
1728/// 'map' '(' [ [ always , ]
1729/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001730///
Alexey Bataev182227b2015-08-20 10:54:39 +00001731/// For 'linear' clause linear-list may have the following forms:
1732/// list
1733/// modifier(list)
1734/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001735OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1736 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001737 SourceLocation Loc = Tok.getLocation();
1738 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001739 SmallVector<Expr *, 4> Vars;
1740 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001741
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001742 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001743 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001744
Alexey Bataevc5e02582014-06-16 07:08:35 +00001745 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001746 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1747 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1748 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1749 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001750}
1751