blob: b41a2a86f5149ff9e4f119a5913510bd2ab57092 [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};
43} // namespace
44
45// Map token string to extended OMP token kind that are
46// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
47static unsigned getOpenMPDirectiveKindEx(StringRef S) {
48 auto DKind = getOpenMPDirectiveKind(S);
49 if (DKind != OMPD_unknown)
50 return DKind;
51
52 return llvm::StringSwitch<unsigned>(S)
53 .Case("cancellation", OMPD_cancellation)
54 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000055 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000056 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000057 .Case("enter", OMPD_enter)
58 .Case("exit", OMPD_exit)
59 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000060 .Case("reduction", OMPD_reduction)
Dmitry Polukhin82478332016-02-13 06:53:38 +000061 .Default(OMPD_unknown);
62}
63
Alexey Bataev4acb8592014-07-07 13:01:15 +000064static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000065 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
66 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
67 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000068 static const unsigned F[][3] = {
69 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000070 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000071 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000072 { OMPD_declare, OMPD_target, OMPD_declare_target },
73 { OMPD_end, OMPD_declare, OMPD_end_declare },
74 { OMPD_end_declare, OMPD_target, OMPD_end_declare_target },
Dmitry Polukhin82478332016-02-13 06:53:38 +000075 { OMPD_target, OMPD_data, OMPD_target_data },
76 { OMPD_target, OMPD_enter, OMPD_target_enter },
77 { OMPD_target, OMPD_exit, OMPD_target_exit },
78 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
79 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
80 { OMPD_for, OMPD_simd, OMPD_for_simd },
81 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
82 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
83 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
84 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
85 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
86 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
87 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000088 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000089 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +000090 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +000091 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 ? static_cast<unsigned>(OMPD_unknown)
93 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
94 if (DKind == OMPD_unknown)
95 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +000096
Alexander Musmanf82886e2014-09-18 05:12:34 +000097 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +000098 if (DKind != F[i][0])
99 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000100
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 Tok = P.getPreprocessor().LookAhead(0);
102 unsigned SDKind =
103 Tok.isAnnotation()
104 ? static_cast<unsigned>(OMPD_unknown)
105 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
106 if (SDKind == OMPD_unknown)
107 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000108
Dmitry Polukhin82478332016-02-13 06:53:38 +0000109 if (SDKind == F[i][1]) {
110 P.ConsumeToken();
111 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000112 }
113 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000114 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
115 : OMPD_unknown;
116}
117
118static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000119 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000120 Sema &Actions = P.getActions();
121 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000122 // Allow to use 'operator' keyword for C++ operators
123 bool WithOperator = false;
124 if (Tok.is(tok::kw_operator)) {
125 P.ConsumeToken();
126 Tok = P.getCurToken();
127 WithOperator = true;
128 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000129 switch (Tok.getKind()) {
130 case tok::plus: // '+'
131 OOK = OO_Plus;
132 break;
133 case tok::minus: // '-'
134 OOK = OO_Minus;
135 break;
136 case tok::star: // '*'
137 OOK = OO_Star;
138 break;
139 case tok::amp: // '&'
140 OOK = OO_Amp;
141 break;
142 case tok::pipe: // '|'
143 OOK = OO_Pipe;
144 break;
145 case tok::caret: // '^'
146 OOK = OO_Caret;
147 break;
148 case tok::ampamp: // '&&'
149 OOK = OO_AmpAmp;
150 break;
151 case tok::pipepipe: // '||'
152 OOK = OO_PipePipe;
153 break;
154 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000155 if (!WithOperator)
156 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000157 default:
158 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
159 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
160 Parser::StopBeforeMatch);
161 return DeclarationName();
162 }
163 P.ConsumeToken();
164 auto &DeclNames = Actions.getASTContext().DeclarationNames;
165 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
166 : DeclNames.getCXXOperatorName(OOK);
167}
168
169/// \brief Parse 'omp declare reduction' construct.
170///
171/// declare-reduction-directive:
172/// annot_pragma_openmp 'declare' 'reduction'
173/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
174/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
175/// annot_pragma_openmp_end
176/// <reduction_id> is either a base language identifier or one of the following
177/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
178///
179Parser::DeclGroupPtrTy
180Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
181 // Parse '('.
182 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
183 if (T.expectAndConsume(diag::err_expected_lparen_after,
184 getOpenMPDirectiveName(OMPD_declare_reduction))) {
185 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
186 return DeclGroupPtrTy();
187 }
188
189 DeclarationName Name = parseOpenMPReductionId(*this);
190 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
191 return DeclGroupPtrTy();
192
193 // Consume ':'.
194 bool IsCorrect = !ExpectAndConsume(tok::colon);
195
196 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
197 return DeclGroupPtrTy();
198
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000199 IsCorrect = IsCorrect && !Name.isEmpty();
200
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000201 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
202 Diag(Tok.getLocation(), diag::err_expected_type);
203 IsCorrect = false;
204 }
205
206 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
207 return DeclGroupPtrTy();
208
209 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
210 // Parse list of types until ':' token.
211 do {
212 ColonProtectionRAIIObject ColonRAII(*this);
213 SourceRange Range;
214 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
215 if (TR.isUsable()) {
216 auto ReductionType =
217 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
218 if (!ReductionType.isNull()) {
219 ReductionTypes.push_back(
220 std::make_pair(ReductionType, Range.getBegin()));
221 }
222 } else {
223 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
224 StopBeforeMatch);
225 }
226
227 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
228 break;
229
230 // Consume ','.
231 if (ExpectAndConsume(tok::comma)) {
232 IsCorrect = false;
233 if (Tok.is(tok::annot_pragma_openmp_end)) {
234 Diag(Tok.getLocation(), diag::err_expected_type);
235 return DeclGroupPtrTy();
236 }
237 }
238 } while (Tok.isNot(tok::annot_pragma_openmp_end));
239
240 if (ReductionTypes.empty()) {
241 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
242 return DeclGroupPtrTy();
243 }
244
245 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
246 return DeclGroupPtrTy();
247
248 // Consume ':'.
249 if (ExpectAndConsume(tok::colon))
250 IsCorrect = false;
251
252 if (Tok.is(tok::annot_pragma_openmp_end)) {
253 Diag(Tok.getLocation(), diag::err_expected_expression);
254 return DeclGroupPtrTy();
255 }
256
257 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
258 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
259
260 // Parse <combiner> expression and then parse initializer if any for each
261 // correct type.
262 unsigned I = 0, E = ReductionTypes.size();
263 for (auto *D : DRD.get()) {
264 TentativeParsingAction TPA(*this);
265 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
266 Scope::OpenMPDirectiveScope);
267 // Parse <combiner> expression.
268 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
269 ExprResult CombinerResult =
270 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
271 D->getLocation(), /*DiscardedValue=*/true);
272 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
273
274 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
275 Tok.isNot(tok::annot_pragma_openmp_end)) {
276 TPA.Commit();
277 IsCorrect = false;
278 break;
279 }
280 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
281 ExprResult InitializerResult;
282 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
283 // Parse <initializer> expression.
284 if (Tok.is(tok::identifier) &&
285 Tok.getIdentifierInfo()->isStr("initializer"))
286 ConsumeToken();
287 else {
288 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
289 TPA.Commit();
290 IsCorrect = false;
291 break;
292 }
293 // Parse '('.
294 BalancedDelimiterTracker T(*this, tok::l_paren,
295 tok::annot_pragma_openmp_end);
296 IsCorrect =
297 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
298 IsCorrect;
299 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
300 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
301 Scope::OpenMPDirectiveScope);
302 // Parse expression.
303 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
304 InitializerResult = Actions.ActOnFinishFullExpr(
305 ParseAssignmentExpression().get(), D->getLocation(),
306 /*DiscardedValue=*/true);
307 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
308 D, InitializerResult.get());
309 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
310 Tok.isNot(tok::annot_pragma_openmp_end)) {
311 TPA.Commit();
312 IsCorrect = false;
313 break;
314 }
315 IsCorrect =
316 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
317 }
318 }
319
320 ++I;
321 // Revert parsing if not the last type, otherwise accept it, we're done with
322 // parsing.
323 if (I != E)
324 TPA.Revert();
325 else
326 TPA.Commit();
327 }
328 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
329 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000330}
331
Alexey Bataev2af33e32016-04-07 12:45:37 +0000332namespace {
333/// RAII that recreates function context for correct parsing of clauses of
334/// 'declare simd' construct.
335/// OpenMP, 2.8.2 declare simd Construct
336/// The expressions appearing in the clauses of this directive are evaluated in
337/// the scope of the arguments of the function declaration or definition.
338class FNContextRAII final {
339 Parser &P;
340 Sema::CXXThisScopeRAII *ThisScope;
341 Parser::ParseScope *TempScope;
342 Parser::ParseScope *FnScope;
343 bool HasTemplateScope = false;
344 bool HasFunScope = false;
345 FNContextRAII() = delete;
346 FNContextRAII(const FNContextRAII &) = delete;
347 FNContextRAII &operator=(const FNContextRAII &) = delete;
348
349public:
350 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
351 Decl *D = *Ptr.get().begin();
352 NamedDecl *ND = dyn_cast<NamedDecl>(D);
353 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
354 Sema &Actions = P.getActions();
355
356 // Allow 'this' within late-parsed attributes.
357 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
358 ND && ND->isCXXInstanceMember());
359
360 // If the Decl is templatized, add template parameters to scope.
361 HasTemplateScope = D->isTemplateDecl();
362 TempScope =
363 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
364 if (HasTemplateScope)
365 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
366
367 // If the Decl is on a function, add function parameters to the scope.
368 HasFunScope = D->isFunctionOrFunctionTemplate();
369 FnScope = new Parser::ParseScope(&P, Scope::FnScope | Scope::DeclScope,
370 HasFunScope);
371 if (HasFunScope)
372 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
373 }
374 ~FNContextRAII() {
375 if (HasFunScope) {
376 P.getActions().ActOnExitFunctionContext();
377 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
378 }
379 if (HasTemplateScope)
380 TempScope->Exit();
381 delete FnScope;
382 delete TempScope;
383 delete ThisScope;
384 }
385};
386} // namespace
387
Alexey Bataevd93d3762016-04-12 09:35:56 +0000388/// Parses clauses for 'declare simd' directive.
389/// clause:
390/// 'inbranch' | 'notinbranch'
391/// 'simdlen' '(' <expr> ')'
392/// { 'uniform' '(' <argument_list> ')' }
393/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
394static bool parseDeclareSimdClauses(Parser &P,
395 OMPDeclareSimdDeclAttr::BranchStateTy &BS,
396 ExprResult &SimdLen,
397 SmallVectorImpl<Expr *> &Uniforms,
398 SmallVectorImpl<Expr *> &Aligneds,
399 SmallVectorImpl<Expr *> &Alignments) {
400 SourceRange BSRange;
401 const Token &Tok = P.getCurToken();
402 bool IsError = false;
403 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
404 if (Tok.isNot(tok::identifier))
405 break;
406 OMPDeclareSimdDeclAttr::BranchStateTy Out;
407 IdentifierInfo *II = Tok.getIdentifierInfo();
408 StringRef ClauseName = II->getName();
409 // Parse 'inranch|notinbranch' clauses.
410 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
411 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
412 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
413 << ClauseName
414 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
415 IsError = true;
416 }
417 BS = Out;
418 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
419 P.ConsumeToken();
420 } else if (ClauseName.equals("simdlen")) {
421 if (SimdLen.isUsable()) {
422 P.Diag(Tok, diag::err_omp_more_one_clause)
423 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
424 IsError = true;
425 }
426 P.ConsumeToken();
427 SourceLocation RLoc;
428 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
429 if (SimdLen.isInvalid())
430 IsError = true;
431 } else {
432 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
433 if (CKind == OMPC_uniform || CKind == OMPC_aligned) {
434 Parser::OpenMPVarListDataTy Data;
435 auto *Vars = &Uniforms;
436 if (CKind == OMPC_aligned) {
437 Vars = &Aligneds;
438 }
439
440 P.ConsumeToken();
441 if (P.ParseOpenMPVarList(OMPD_declare_simd,
442 getOpenMPClauseKind(ClauseName), *Vars, Data))
443 IsError = true;
444 if (CKind == OMPC_aligned)
445 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
446 } else
447 // TODO: add parsing of other clauses.
448 break;
449 }
450 // Skip ',' if any.
451 if (Tok.is(tok::comma))
452 P.ConsumeToken();
453 }
454 return IsError;
455}
456
Alexey Bataev2af33e32016-04-07 12:45:37 +0000457/// Parse clauses for '#pragma omp declare simd'.
458Parser::DeclGroupPtrTy
459Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
460 CachedTokens &Toks, SourceLocation Loc) {
461 PP.EnterToken(Tok);
462 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
463 // Consume the previously pushed token.
464 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
465
466 FNContextRAII FnContext(*this, Ptr);
467 OMPDeclareSimdDeclAttr::BranchStateTy BS =
468 OMPDeclareSimdDeclAttr::BS_Undefined;
469 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000470 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000471 SmallVector<Expr *, 4> Aligneds;
472 SmallVector<Expr *, 4> Alignments;
473 bool IsError = parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
474 Alignments);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000475 // Need to check for extra tokens.
476 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
477 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
478 << getOpenMPDirectiveName(OMPD_declare_simd);
479 while (Tok.isNot(tok::annot_pragma_openmp_end))
480 ConsumeAnyToken();
481 }
482 // Skip the last annot_pragma_openmp_end.
483 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000484 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000485 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevd93d3762016-04-12 09:35:56 +0000486 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments,
487 SourceRange(Loc, EndLoc));
488 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000489 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000490}
491
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000492/// \brief Parsing of declarative OpenMP directives.
493///
494/// threadprivate-directive:
495/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000496/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000497///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000498/// declare-reduction-directive:
499/// annot_pragma_openmp 'declare' 'reduction' [...]
500/// annot_pragma_openmp_end
501///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000502/// declare-simd-directive:
503/// annot_pragma_openmp 'declare simd' {<clause> [,]}
504/// annot_pragma_openmp_end
505/// <function declaration/definition>
506///
507Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
508 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
509 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000510 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000511 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000512
513 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000514 SmallVector<Expr *, 4> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000515 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000516
517 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000518 case OMPD_threadprivate:
519 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000520 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000521 // The last seen token is annot_pragma_openmp_end - need to check for
522 // extra tokens.
523 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
524 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000525 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000526 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000527 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000528 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000529 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000530 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000531 }
532 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000533 case OMPD_declare_reduction:
534 ConsumeToken();
535 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
536 // The last seen token is annot_pragma_openmp_end - need to check for
537 // extra tokens.
538 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
539 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
540 << getOpenMPDirectiveName(OMPD_declare_reduction);
541 while (Tok.isNot(tok::annot_pragma_openmp_end))
542 ConsumeAnyToken();
543 }
544 // Skip the last annot_pragma_openmp_end.
545 ConsumeToken();
546 return Res;
547 }
548 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000549 case OMPD_declare_simd: {
550 // The syntax is:
551 // { #pragma omp declare simd }
552 // <function-declaration-or-definition>
553 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000554 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000555 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000556 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
557 Toks.push_back(Tok);
558 ConsumeAnyToken();
559 }
560 Toks.push_back(Tok);
561 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000562
563 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000564 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000565 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000566 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000567 // Here we expect to see some function declaration.
568 if (AS == AS_none) {
569 assert(TagType == DeclSpec::TST_unspecified);
570 MaybeParseCXX11Attributes(Attrs);
571 MaybeParseMicrosoftAttributes(Attrs);
572 ParsingDeclSpec PDS(*this);
573 Ptr = ParseExternalDeclaration(Attrs, &PDS);
574 } else {
575 Ptr =
576 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
577 }
578 }
579 if (!Ptr) {
580 Diag(Loc, diag::err_omp_decl_in_declare_simd);
581 return DeclGroupPtrTy();
582 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000583 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000584 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000585 case OMPD_declare_target: {
586 SourceLocation DTLoc = ConsumeAnyToken();
587 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
588 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
589 << getOpenMPDirectiveName(OMPD_declare_target);
590 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
591 }
592 // Skip the last annot_pragma_openmp_end.
593 ConsumeAnyToken();
594
595 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
596 return DeclGroupPtrTy();
597
598 DKind = ParseOpenMPDirectiveKind(*this);
599 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
600 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
601 ParsedAttributesWithRange attrs(AttrFactory);
602 MaybeParseCXX11Attributes(attrs);
603 MaybeParseMicrosoftAttributes(attrs);
604 ParseExternalDeclaration(attrs);
605 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
606 TentativeParsingAction TPA(*this);
607 ConsumeToken();
608 DKind = ParseOpenMPDirectiveKind(*this);
609 if (DKind != OMPD_end_declare_target)
610 TPA.Revert();
611 else
612 TPA.Commit();
613 }
614 }
615
616 if (DKind == OMPD_end_declare_target) {
617 ConsumeAnyToken();
618 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
619 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
620 << getOpenMPDirectiveName(OMPD_end_declare_target);
621 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
622 }
623 // Skip the last annot_pragma_openmp_end.
624 ConsumeAnyToken();
625 } else {
626 Diag(Tok, diag::err_expected_end_declare_target);
627 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
628 }
629 Actions.ActOnFinishOpenMPDeclareTargetDirective();
630 return DeclGroupPtrTy();
631 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000632 case OMPD_unknown:
633 Diag(Tok, diag::err_omp_unknown_directive);
634 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000635 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000636 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000637 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000638 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000639 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000640 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000641 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000642 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000643 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000644 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000645 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000646 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000647 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000648 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000649 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000650 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000651 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000652 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000653 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000654 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000655 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000656 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000657 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000658 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000659 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000660 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000661 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000662 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000663 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000664 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000665 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000666 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000667 case OMPD_end_declare_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000668 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000669 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000670 break;
671 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000672 while (Tok.isNot(tok::annot_pragma_openmp_end))
673 ConsumeAnyToken();
674 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000675 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000676}
677
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000678/// \brief Parsing of declarative or executable OpenMP directives.
679///
680/// threadprivate-directive:
681/// annot_pragma_openmp 'threadprivate' simple-variable-list
682/// annot_pragma_openmp_end
683///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000684/// declare-reduction-directive:
685/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
686/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
687/// ('omp_priv' '=' <expression>|<function_call>) ')']
688/// annot_pragma_openmp_end
689///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000690/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000691/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000692/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
693/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000694/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000695/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000696/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000697/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000698/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000699/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000700///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000701StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
702 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000704 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000705 SmallVector<Expr *, 5> Identifiers;
706 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000707 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000708 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000709 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000710 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000711 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000712 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000713 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000714 // Name of critical directive.
715 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000716 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000717 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000718 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719
720 switch (DKind) {
721 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000722 if (Allowed != ACK_Any) {
723 Diag(Tok, diag::err_omp_immediate_directive)
724 << getOpenMPDirectiveName(DKind) << 0;
725 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000726 ConsumeToken();
727 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
728 // The last seen token is annot_pragma_openmp_end - need to check for
729 // extra tokens.
730 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
731 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000732 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000733 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000734 }
735 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000736 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000737 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
738 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000739 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000740 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000741 case OMPD_declare_reduction:
742 ConsumeToken();
743 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
744 // The last seen token is annot_pragma_openmp_end - need to check for
745 // extra tokens.
746 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
747 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
748 << getOpenMPDirectiveName(OMPD_declare_reduction);
749 while (Tok.isNot(tok::annot_pragma_openmp_end))
750 ConsumeAnyToken();
751 }
752 ConsumeAnyToken();
753 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
754 } else
755 SkipUntil(tok::annot_pragma_openmp_end);
756 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000757 case OMPD_flush:
758 if (PP.LookAhead(0).is(tok::l_paren)) {
759 FlushHasClause = true;
760 // Push copy of the current token back to stream to properly parse
761 // pseudo-clause OMPFlushClause.
762 PP.EnterToken(Tok);
763 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000764 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000765 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000766 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000767 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000768 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000769 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000770 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000771 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000772 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000773 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000774 }
775 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000776 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000777 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000778 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000779 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000780 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000781 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000782 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000783 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000784 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000786 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000787 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000788 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000789 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000790 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000791 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000792 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000793 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000794 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000795 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000796 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000797 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000798 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000799 case OMPD_taskloop_simd:
800 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000801 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000802 // Parse directive name of the 'critical' directive if any.
803 if (DKind == OMPD_critical) {
804 BalancedDelimiterTracker T(*this, tok::l_paren,
805 tok::annot_pragma_openmp_end);
806 if (!T.consumeOpen()) {
807 if (Tok.isAnyIdentifier()) {
808 DirName =
809 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
810 ConsumeAnyToken();
811 } else {
812 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
813 }
814 T.consumeClose();
815 }
Alexey Bataev80909872015-07-02 11:25:17 +0000816 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000817 CancelRegion = ParseOpenMPDirectiveKind(*this);
818 if (Tok.isNot(tok::annot_pragma_openmp_end))
819 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000820 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000821
Alexey Bataevf29276e2014-06-18 04:14:57 +0000822 if (isOpenMPLoopDirective(DKind))
823 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
824 if (isOpenMPSimdDirective(DKind))
825 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
826 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000827 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000828
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000829 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000830 OpenMPClauseKind CKind =
831 Tok.isAnnotation()
832 ? OMPC_unknown
833 : FlushHasClause ? OMPC_flush
834 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000835 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000836 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000837 OMPClause *Clause =
838 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000839 FirstClauses[CKind].setInt(true);
840 if (Clause) {
841 FirstClauses[CKind].setPointer(Clause);
842 Clauses.push_back(Clause);
843 }
844
845 // Skip ',' if any.
846 if (Tok.is(tok::comma))
847 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000848 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000849 }
850 // End location of the directive.
851 EndLoc = Tok.getLocation();
852 // Consume final annot_pragma_openmp_end.
853 ConsumeToken();
854
Alexey Bataeveb482352015-12-18 05:05:56 +0000855 // OpenMP [2.13.8, ordered Construct, Syntax]
856 // If the depend clause is specified, the ordered construct is a stand-alone
857 // directive.
858 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000859 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000860 Diag(Loc, diag::err_omp_immediate_directive)
861 << getOpenMPDirectiveName(DKind) << 1
862 << getOpenMPClauseName(OMPC_depend);
863 }
864 HasAssociatedStatement = false;
865 }
866
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000867 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000868 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000869 // The body is a block scope like in Lambdas and Blocks.
870 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000871 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000872 Actions.ActOnStartOfCompoundStmt();
873 // Parse statement
874 AssociatedStmt = ParseStatement();
875 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000876 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000877 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000878 Directive = Actions.ActOnOpenMPExecutableDirective(
879 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
880 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000881
882 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000883 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000884 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000885 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000886 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000887 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000888 case OMPD_declare_target:
889 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000890 Diag(Tok, diag::err_omp_unexpected_directive)
891 << getOpenMPDirectiveName(DKind);
892 SkipUntil(tok::annot_pragma_openmp_end);
893 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000894 case OMPD_unknown:
895 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000896 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000897 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000898 }
899 return Directive;
900}
901
Alexey Bataeva769e072013-03-22 06:34:35 +0000902/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000903/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000904///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000905/// simple-variable-list:
906/// '(' id-expression {, id-expression} ')'
907///
908bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
909 SmallVectorImpl<Expr *> &VarList,
910 bool AllowScopeSpecifier) {
911 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000912 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000913 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 if (T.expectAndConsume(diag::err_expected_lparen_after,
915 getOpenMPDirectiveName(Kind)))
916 return true;
917 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000918 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000919
920 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000921 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000922 CXXScopeSpec SS;
923 SourceLocation TemplateKWLoc;
924 UnqualifiedId Name;
925 // Read var name.
926 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000927 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000928
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000929 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000930 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000931 IsCorrect = false;
932 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000933 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000934 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000935 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000936 IsCorrect = false;
937 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000938 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000939 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
940 Tok.isNot(tok::annot_pragma_openmp_end)) {
941 IsCorrect = false;
942 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000943 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000944 Diag(PrevTok.getLocation(), diag::err_expected)
945 << tok::identifier
946 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000947 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000948 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000949 ExprResult Res =
950 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000951 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000952 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000953 }
954 // Consume ','.
955 if (Tok.is(tok::comma)) {
956 ConsumeToken();
957 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000958 }
959
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000960 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000961 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000962 IsCorrect = false;
963 }
964
965 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000966 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000967
968 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000969}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000970
971/// \brief Parsing of OpenMP clauses.
972///
973/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000974/// if-clause | final-clause | num_threads-clause | safelen-clause |
975/// default-clause | private-clause | firstprivate-clause | shared-clause
976/// | linear-clause | aligned-clause | collapse-clause |
977/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000978/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000979/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000980/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000981/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000982/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000983/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000984///
985OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
986 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000987 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000988 bool ErrorFound = false;
989 // Check if clause is allowed for the given directive.
990 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000991 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
992 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000993 ErrorFound = true;
994 }
995
996 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000997 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000998 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000999 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001000 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001001 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001002 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001003 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001004 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001005 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001006 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001007 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001008 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001009 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001010 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001011 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001012 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001013 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001014 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001015 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001016 // OpenMP [2.9.1, target data construct, Restrictions]
1017 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001018 // OpenMP [2.11.1, task Construct, Restrictions]
1019 // At most one if clause can appear on the directive.
1020 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001021 // OpenMP [teams Construct, Restrictions]
1022 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001023 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001024 // OpenMP [2.9.1, task Construct, Restrictions]
1025 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001026 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1027 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001028 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1029 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001030 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001031 Diag(Tok, diag::err_omp_more_one_clause)
1032 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001033 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001034 }
1035
Alexey Bataev10e775f2015-07-30 11:36:16 +00001036 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1037 Clause = ParseOpenMPClause(CKind);
1038 else
1039 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001040 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001041 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001042 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001043 // OpenMP [2.14.3.1, Restrictions]
1044 // Only a single default clause may be specified on a parallel, task or
1045 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001046 // OpenMP [2.5, parallel Construct, Restrictions]
1047 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001048 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001049 Diag(Tok, diag::err_omp_more_one_clause)
1050 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001051 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001052 }
1053
1054 Clause = ParseOpenMPSimpleClause(CKind);
1055 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001056 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001057 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001058 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001059 // OpenMP [2.7.1, Restrictions, p. 3]
1060 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001061 // OpenMP [2.10.4, Restrictions, p. 106]
1062 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001063 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001064 Diag(Tok, diag::err_omp_more_one_clause)
1065 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001066 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001067 }
1068
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001069 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001070 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1071 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001072 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001073 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001074 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001075 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001076 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001077 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001078 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001079 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001080 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001081 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001082 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001083 // OpenMP [2.7.1, Restrictions, p. 9]
1084 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001085 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1086 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +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 Bataev142e1fc2014-06-20 09:44:06 +00001091 }
1092
1093 Clause = ParseOpenMPClause(CKind);
1094 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001095 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001096 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001097 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001098 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001099 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001100 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001101 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001102 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001103 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001104 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001105 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001106 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +00001107 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001108 break;
1109 case OMPC_unknown:
1110 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001111 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001112 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001113 break;
1114 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001115 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001116 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1117 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001118 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001119 break;
1120 }
Craig Topper161e4db2014-05-21 06:02:52 +00001121 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122}
1123
Alexey Bataev2af33e32016-04-07 12:45:37 +00001124/// Parses simple expression in parens for single-expression clauses of OpenMP
1125/// constructs.
1126/// \param RLoc Returned location of right paren.
1127ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1128 SourceLocation &RLoc) {
1129 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1130 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1131 return ExprError();
1132
1133 SourceLocation ELoc = Tok.getLocation();
1134 ExprResult LHS(ParseCastExpression(
1135 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1136 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1137 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1138
1139 // Parse ')'.
1140 T.consumeClose();
1141
1142 RLoc = T.getCloseLocation();
1143 return Val;
1144}
1145
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001146/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001147/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001148/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001149///
Alexey Bataev3778b602014-07-17 07:32:53 +00001150/// final-clause:
1151/// 'final' '(' expression ')'
1152///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001153/// num_threads-clause:
1154/// 'num_threads' '(' expression ')'
1155///
1156/// safelen-clause:
1157/// 'safelen' '(' expression ')'
1158///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001159/// simdlen-clause:
1160/// 'simdlen' '(' expression ')'
1161///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001162/// collapse-clause:
1163/// 'collapse' '(' expression ')'
1164///
Alexey Bataeva0569352015-12-01 10:17:31 +00001165/// priority-clause:
1166/// 'priority' '(' expression ')'
1167///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001168/// grainsize-clause:
1169/// 'grainsize' '(' expression ')'
1170///
Alexey Bataev382967a2015-12-08 12:06:20 +00001171/// num_tasks-clause:
1172/// 'num_tasks' '(' expression ')'
1173///
Alexey Bataev28c75412015-12-15 08:19:24 +00001174/// hint-clause:
1175/// 'hint' '(' expression ')'
1176///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001177OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1178 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001179 SourceLocation LLoc = Tok.getLocation();
1180 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001181
Alexey Bataev2af33e32016-04-07 12:45:37 +00001182 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001183
1184 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001185 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001186
Alexey Bataev2af33e32016-04-07 12:45:37 +00001187 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001188}
1189
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001190/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001191///
1192/// default-clause:
1193/// 'default' '(' 'none' | 'shared' ')
1194///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001195/// proc_bind-clause:
1196/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1197///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001198OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1199 SourceLocation Loc = Tok.getLocation();
1200 SourceLocation LOpen = ConsumeToken();
1201 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001202 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001203 if (T.expectAndConsume(diag::err_expected_lparen_after,
1204 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001205 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206
Alexey Bataeva55ed262014-05-28 06:15:33 +00001207 unsigned Type = getOpenMPSimpleClauseType(
1208 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001209 SourceLocation TypeLoc = Tok.getLocation();
1210 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1211 Tok.isNot(tok::annot_pragma_openmp_end))
1212 ConsumeAnyToken();
1213
1214 // Parse ')'.
1215 T.consumeClose();
1216
1217 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1218 Tok.getLocation());
1219}
1220
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001221/// \brief Parsing of OpenMP clauses like 'ordered'.
1222///
1223/// ordered-clause:
1224/// 'ordered'
1225///
Alexey Bataev236070f2014-06-20 11:19:47 +00001226/// nowait-clause:
1227/// 'nowait'
1228///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001229/// untied-clause:
1230/// 'untied'
1231///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001232/// mergeable-clause:
1233/// 'mergeable'
1234///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001235/// read-clause:
1236/// 'read'
1237///
Alexey Bataev346265e2015-09-25 10:37:12 +00001238/// threads-clause:
1239/// 'threads'
1240///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001241/// simd-clause:
1242/// 'simd'
1243///
Alexey Bataevb825de12015-12-07 10:51:44 +00001244/// nogroup-clause:
1245/// 'nogroup'
1246///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001247OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1248 SourceLocation Loc = Tok.getLocation();
1249 ConsumeAnyToken();
1250
1251 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1252}
1253
1254
Alexey Bataev56dafe82014-06-20 07:16:17 +00001255/// \brief Parsing of OpenMP clauses with single expressions and some additional
1256/// argument like 'schedule' or 'dist_schedule'.
1257///
1258/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001259/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1260/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001261///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001262/// if-clause:
1263/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1264///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001265/// defaultmap:
1266/// 'defaultmap' '(' modifier ':' kind ')'
1267///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001268OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1269 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001270 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001271 // Parse '('.
1272 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1273 if (T.expectAndConsume(diag::err_expected_lparen_after,
1274 getOpenMPClauseName(Kind)))
1275 return nullptr;
1276
1277 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001278 SmallVector<unsigned, 4> Arg;
1279 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001280 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001281 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1282 Arg.resize(NumberOfElements);
1283 KLoc.resize(NumberOfElements);
1284 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1285 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1286 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1287 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001288 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001289 if (KindModifier > OMPC_SCHEDULE_unknown) {
1290 // Parse 'modifier'
1291 Arg[Modifier1] = KindModifier;
1292 KLoc[Modifier1] = Tok.getLocation();
1293 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1294 Tok.isNot(tok::annot_pragma_openmp_end))
1295 ConsumeAnyToken();
1296 if (Tok.is(tok::comma)) {
1297 // Parse ',' 'modifier'
1298 ConsumeAnyToken();
1299 KindModifier = getOpenMPSimpleClauseType(
1300 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1301 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1302 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001303 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001304 KLoc[Modifier2] = Tok.getLocation();
1305 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1306 Tok.isNot(tok::annot_pragma_openmp_end))
1307 ConsumeAnyToken();
1308 }
1309 // Parse ':'
1310 if (Tok.is(tok::colon))
1311 ConsumeAnyToken();
1312 else
1313 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1314 KindModifier = getOpenMPSimpleClauseType(
1315 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1316 }
1317 Arg[ScheduleKind] = KindModifier;
1318 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001319 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1320 Tok.isNot(tok::annot_pragma_openmp_end))
1321 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001322 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1323 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1324 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001325 Tok.is(tok::comma))
1326 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001327 } else if (Kind == OMPC_dist_schedule) {
1328 Arg.push_back(getOpenMPSimpleClauseType(
1329 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1330 KLoc.push_back(Tok.getLocation());
1331 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1332 Tok.isNot(tok::annot_pragma_openmp_end))
1333 ConsumeAnyToken();
1334 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1335 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001336 } else if (Kind == OMPC_defaultmap) {
1337 // Get a defaultmap modifier
1338 Arg.push_back(getOpenMPSimpleClauseType(
1339 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1340 KLoc.push_back(Tok.getLocation());
1341 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1342 Tok.isNot(tok::annot_pragma_openmp_end))
1343 ConsumeAnyToken();
1344 // Parse ':'
1345 if (Tok.is(tok::colon))
1346 ConsumeAnyToken();
1347 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1348 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1349 // Get a defaultmap kind
1350 Arg.push_back(getOpenMPSimpleClauseType(
1351 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1352 KLoc.push_back(Tok.getLocation());
1353 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1354 Tok.isNot(tok::annot_pragma_openmp_end))
1355 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001356 } else {
1357 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001358 KLoc.push_back(Tok.getLocation());
1359 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1360 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001361 ConsumeToken();
1362 if (Tok.is(tok::colon))
1363 DelimLoc = ConsumeToken();
1364 else
1365 Diag(Tok, diag::warn_pragma_expected_colon)
1366 << "directive name modifier";
1367 }
1368 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001369
Carlo Bertollib4adf552016-01-15 18:50:31 +00001370 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1371 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1372 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001373 if (NeedAnExpression) {
1374 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001375 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1376 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001377 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001378 }
1379
1380 // Parse ')'.
1381 T.consumeClose();
1382
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001383 if (NeedAnExpression && Val.isInvalid())
1384 return nullptr;
1385
Alexey Bataev56dafe82014-06-20 07:16:17 +00001386 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001387 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001388 T.getCloseLocation());
1389}
1390
Alexey Bataevc5e02582014-06-16 07:08:35 +00001391static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1392 UnqualifiedId &ReductionId) {
1393 SourceLocation TemplateKWLoc;
1394 if (ReductionIdScopeSpec.isEmpty()) {
1395 auto OOK = OO_None;
1396 switch (P.getCurToken().getKind()) {
1397 case tok::plus:
1398 OOK = OO_Plus;
1399 break;
1400 case tok::minus:
1401 OOK = OO_Minus;
1402 break;
1403 case tok::star:
1404 OOK = OO_Star;
1405 break;
1406 case tok::amp:
1407 OOK = OO_Amp;
1408 break;
1409 case tok::pipe:
1410 OOK = OO_Pipe;
1411 break;
1412 case tok::caret:
1413 OOK = OO_Caret;
1414 break;
1415 case tok::ampamp:
1416 OOK = OO_AmpAmp;
1417 break;
1418 case tok::pipepipe:
1419 OOK = OO_PipePipe;
1420 break;
1421 default:
1422 break;
1423 }
1424 if (OOK != OO_None) {
1425 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001426 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001427 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1428 return false;
1429 }
1430 }
1431 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1432 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001433 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001434 TemplateKWLoc, ReductionId);
1435}
1436
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001437/// Parses clauses with list.
1438bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1439 OpenMPClauseKind Kind,
1440 SmallVectorImpl<Expr *> &Vars,
1441 OpenMPVarListDataTy &Data) {
1442 UnqualifiedId UnqualifiedReductionId;
1443 bool InvalidReductionId = false;
1444 bool MapTypeModifierSpecified = false;
1445
1446 // Parse '('.
1447 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1448 if (T.expectAndConsume(diag::err_expected_lparen_after,
1449 getOpenMPClauseName(Kind)))
1450 return true;
1451
1452 bool NeedRParenForLinear = false;
1453 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1454 tok::annot_pragma_openmp_end);
1455 // Handle reduction-identifier for reduction clause.
1456 if (Kind == OMPC_reduction) {
1457 ColonProtectionRAIIObject ColonRAII(*this);
1458 if (getLangOpts().CPlusPlus)
1459 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1460 /*ObjectType=*/nullptr,
1461 /*EnteringContext=*/false);
1462 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1463 UnqualifiedReductionId);
1464 if (InvalidReductionId) {
1465 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1466 StopBeforeMatch);
1467 }
1468 if (Tok.is(tok::colon))
1469 Data.ColonLoc = ConsumeToken();
1470 else
1471 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1472 if (!InvalidReductionId)
1473 Data.ReductionId =
1474 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1475 } else if (Kind == OMPC_depend) {
1476 // Handle dependency type for depend clause.
1477 ColonProtectionRAIIObject ColonRAII(*this);
1478 Data.DepKind =
1479 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1480 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1481 Data.DepLinMapLoc = Tok.getLocation();
1482
1483 if (Data.DepKind == OMPC_DEPEND_unknown) {
1484 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1485 StopBeforeMatch);
1486 } else {
1487 ConsumeToken();
1488 // Special processing for depend(source) clause.
1489 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1490 // Parse ')'.
1491 T.consumeClose();
1492 return false;
1493 }
1494 }
1495 if (Tok.is(tok::colon))
1496 Data.ColonLoc = ConsumeToken();
1497 else {
1498 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1499 : diag::warn_pragma_expected_colon)
1500 << "dependency type";
1501 }
1502 } else if (Kind == OMPC_linear) {
1503 // Try to parse modifier if any.
1504 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1505 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1506 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1507 Data.DepLinMapLoc = ConsumeToken();
1508 LinearT.consumeOpen();
1509 NeedRParenForLinear = true;
1510 }
1511 } else if (Kind == OMPC_map) {
1512 // Handle map type for map clause.
1513 ColonProtectionRAIIObject ColonRAII(*this);
1514
1515 /// The map clause modifier token can be either a identifier or the C++
1516 /// delete keyword.
1517 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1518 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1519 };
1520
1521 // The first identifier may be a list item, a map-type or a
1522 // map-type-modifier. The map modifier can also be delete which has the same
1523 // spelling of the C++ delete keyword.
1524 Data.MapType =
1525 IsMapClauseModifierToken(Tok)
1526 ? static_cast<OpenMPMapClauseKind>(
1527 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1528 : OMPC_MAP_unknown;
1529 Data.DepLinMapLoc = Tok.getLocation();
1530 bool ColonExpected = false;
1531
1532 if (IsMapClauseModifierToken(Tok)) {
1533 if (PP.LookAhead(0).is(tok::colon)) {
1534 if (Data.MapType == OMPC_MAP_unknown)
1535 Diag(Tok, diag::err_omp_unknown_map_type);
1536 else if (Data.MapType == OMPC_MAP_always)
1537 Diag(Tok, diag::err_omp_map_type_missing);
1538 ConsumeToken();
1539 } else if (PP.LookAhead(0).is(tok::comma)) {
1540 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1541 PP.LookAhead(2).is(tok::colon)) {
1542 Data.MapTypeModifier = Data.MapType;
1543 if (Data.MapTypeModifier != OMPC_MAP_always) {
1544 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1545 Data.MapTypeModifier = OMPC_MAP_unknown;
1546 } else
1547 MapTypeModifierSpecified = true;
1548
1549 ConsumeToken();
1550 ConsumeToken();
1551
1552 Data.MapType =
1553 IsMapClauseModifierToken(Tok)
1554 ? static_cast<OpenMPMapClauseKind>(
1555 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1556 : OMPC_MAP_unknown;
1557 if (Data.MapType == OMPC_MAP_unknown ||
1558 Data.MapType == OMPC_MAP_always)
1559 Diag(Tok, diag::err_omp_unknown_map_type);
1560 ConsumeToken();
1561 } else {
1562 Data.MapType = OMPC_MAP_tofrom;
1563 Data.IsMapTypeImplicit = true;
1564 }
1565 } else {
1566 Data.MapType = OMPC_MAP_tofrom;
1567 Data.IsMapTypeImplicit = true;
1568 }
1569 } else {
1570 Data.MapType = OMPC_MAP_tofrom;
1571 Data.IsMapTypeImplicit = true;
1572 }
1573
1574 if (Tok.is(tok::colon))
1575 Data.ColonLoc = ConsumeToken();
1576 else if (ColonExpected)
1577 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1578 }
1579
1580 bool IsComma =
1581 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1582 (Kind == OMPC_reduction && !InvalidReductionId) ||
1583 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1584 (!MapTypeModifierSpecified ||
1585 Data.MapTypeModifier == OMPC_MAP_always)) ||
1586 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1587 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1588 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1589 Tok.isNot(tok::annot_pragma_openmp_end))) {
1590 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1591 // Parse variable
1592 ExprResult VarExpr =
1593 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1594 if (VarExpr.isUsable())
1595 Vars.push_back(VarExpr.get());
1596 else {
1597 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1598 StopBeforeMatch);
1599 }
1600 // Skip ',' if any
1601 IsComma = Tok.is(tok::comma);
1602 if (IsComma)
1603 ConsumeToken();
1604 else if (Tok.isNot(tok::r_paren) &&
1605 Tok.isNot(tok::annot_pragma_openmp_end) &&
1606 (!MayHaveTail || Tok.isNot(tok::colon)))
1607 Diag(Tok, diag::err_omp_expected_punc)
1608 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1609 : getOpenMPClauseName(Kind))
1610 << (Kind == OMPC_flush);
1611 }
1612
1613 // Parse ')' for linear clause with modifier.
1614 if (NeedRParenForLinear)
1615 LinearT.consumeClose();
1616
1617 // Parse ':' linear-step (or ':' alignment).
1618 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1619 if (MustHaveTail) {
1620 Data.ColonLoc = Tok.getLocation();
1621 SourceLocation ELoc = ConsumeToken();
1622 ExprResult Tail = ParseAssignmentExpression();
1623 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1624 if (Tail.isUsable())
1625 Data.TailExpr = Tail.get();
1626 else
1627 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1628 StopBeforeMatch);
1629 }
1630
1631 // Parse ')'.
1632 T.consumeClose();
1633 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1634 Vars.empty()) ||
1635 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1636 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1637 return true;
1638 return false;
1639}
1640
Alexander Musman1bb328c2014-06-04 13:06:39 +00001641/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001642/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001643///
1644/// private-clause:
1645/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001646/// firstprivate-clause:
1647/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001648/// lastprivate-clause:
1649/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001650/// shared-clause:
1651/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001652/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001653/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001654/// aligned-clause:
1655/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001656/// reduction-clause:
1657/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001658/// copyprivate-clause:
1659/// 'copyprivate' '(' list ')'
1660/// flush-clause:
1661/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001662/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001663/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001664/// map-clause:
1665/// 'map' '(' [ [ always , ]
1666/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001667///
Alexey Bataev182227b2015-08-20 10:54:39 +00001668/// For 'linear' clause linear-list may have the following forms:
1669/// list
1670/// modifier(list)
1671/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001672OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1673 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001674 SourceLocation Loc = Tok.getLocation();
1675 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001676 SmallVector<Expr *, 4> Vars;
1677 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001678
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001679 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001680 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001681
Alexey Bataevc5e02582014-06-16 07:08:35 +00001682 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001683 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1684 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1685 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1686 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001687}
1688