blob: b1538fde257cc59a8c76c35e5370518f0afbc552 [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> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000394/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
395static bool parseDeclareSimdClauses(
396 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
397 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
398 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
399 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000400 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);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000433 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
434 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000435 Parser::OpenMPVarListDataTy Data;
436 auto *Vars = &Uniforms;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000437 if (CKind == OMPC_aligned)
Alexey Bataevd93d3762016-04-12 09:35:56 +0000438 Vars = &Aligneds;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000439 else if (CKind == OMPC_linear)
440 Vars = &Linears;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000441
442 P.ConsumeToken();
443 if (P.ParseOpenMPVarList(OMPD_declare_simd,
444 getOpenMPClauseKind(ClauseName), *Vars, Data))
445 IsError = true;
446 if (CKind == OMPC_aligned)
447 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000448 else if (CKind == OMPC_linear) {
449 if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
450 Data.DepLinMapLoc))
451 Data.LinKind = OMPC_LINEAR_val;
452 LinModifiers.append(Linears.size() - LinModifiers.size(),
453 Data.LinKind);
454 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
455 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000456 } else
457 // TODO: add parsing of other clauses.
458 break;
459 }
460 // Skip ',' if any.
461 if (Tok.is(tok::comma))
462 P.ConsumeToken();
463 }
464 return IsError;
465}
466
Alexey Bataev2af33e32016-04-07 12:45:37 +0000467/// Parse clauses for '#pragma omp declare simd'.
468Parser::DeclGroupPtrTy
469Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
470 CachedTokens &Toks, SourceLocation Loc) {
471 PP.EnterToken(Tok);
472 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
473 // Consume the previously pushed token.
474 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
475
476 FNContextRAII FnContext(*this, Ptr);
477 OMPDeclareSimdDeclAttr::BranchStateTy BS =
478 OMPDeclareSimdDeclAttr::BS_Undefined;
479 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000480 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000481 SmallVector<Expr *, 4> Aligneds;
482 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000483 SmallVector<Expr *, 4> Linears;
484 SmallVector<unsigned, 4> LinModifiers;
485 SmallVector<Expr *, 4> Steps;
486 bool IsError =
487 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
488 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000489 // Need to check for extra tokens.
490 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
491 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
492 << getOpenMPDirectiveName(OMPD_declare_simd);
493 while (Tok.isNot(tok::annot_pragma_openmp_end))
494 ConsumeAnyToken();
495 }
496 // Skip the last annot_pragma_openmp_end.
497 SourceLocation EndLoc = ConsumeToken();
Alexey Bataevd93d3762016-04-12 09:35:56 +0000498 if (!IsError) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000499 return Actions.ActOnOpenMPDeclareSimdDirective(
Alexey Bataevecba70f2016-04-12 11:02:11 +0000500 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
501 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataevd93d3762016-04-12 09:35:56 +0000502 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000503 return Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000504}
505
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000506/// \brief Parsing of declarative OpenMP directives.
507///
508/// threadprivate-directive:
509/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000510/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000511///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000512/// declare-reduction-directive:
513/// annot_pragma_openmp 'declare' 'reduction' [...]
514/// annot_pragma_openmp_end
515///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000516/// declare-simd-directive:
517/// annot_pragma_openmp 'declare simd' {<clause> [,]}
518/// annot_pragma_openmp_end
519/// <function declaration/definition>
520///
521Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
522 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
523 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000524 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000525 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000526
527 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000528 SmallVector<Expr *, 4> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000529 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000530
531 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000532 case OMPD_threadprivate:
533 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000534 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000535 // The last seen token is annot_pragma_openmp_end - need to check for
536 // extra tokens.
537 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
538 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000539 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000540 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000541 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000542 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000543 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000544 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000545 }
546 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000547 case OMPD_declare_reduction:
548 ConsumeToken();
549 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
550 // The last seen token is annot_pragma_openmp_end - need to check for
551 // extra tokens.
552 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
553 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
554 << getOpenMPDirectiveName(OMPD_declare_reduction);
555 while (Tok.isNot(tok::annot_pragma_openmp_end))
556 ConsumeAnyToken();
557 }
558 // Skip the last annot_pragma_openmp_end.
559 ConsumeToken();
560 return Res;
561 }
562 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000563 case OMPD_declare_simd: {
564 // The syntax is:
565 // { #pragma omp declare simd }
566 // <function-declaration-or-definition>
567 //
Alexey Bataev587e1de2016-03-30 10:43:55 +0000568 ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +0000569 CachedTokens Toks;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000570 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
571 Toks.push_back(Tok);
572 ConsumeAnyToken();
573 }
574 Toks.push_back(Tok);
575 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000576
577 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000578 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000579 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000580 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000581 // Here we expect to see some function declaration.
582 if (AS == AS_none) {
583 assert(TagType == DeclSpec::TST_unspecified);
584 MaybeParseCXX11Attributes(Attrs);
585 MaybeParseMicrosoftAttributes(Attrs);
586 ParsingDeclSpec PDS(*this);
587 Ptr = ParseExternalDeclaration(Attrs, &PDS);
588 } else {
589 Ptr =
590 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
591 }
592 }
593 if (!Ptr) {
594 Diag(Loc, diag::err_omp_decl_in_declare_simd);
595 return DeclGroupPtrTy();
596 }
Alexey Bataev2af33e32016-04-07 12:45:37 +0000597 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
Alexey Bataev587e1de2016-03-30 10:43:55 +0000598 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000599 case OMPD_declare_target: {
600 SourceLocation DTLoc = ConsumeAnyToken();
601 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
602 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
603 << getOpenMPDirectiveName(OMPD_declare_target);
604 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
605 }
606 // Skip the last annot_pragma_openmp_end.
607 ConsumeAnyToken();
608
609 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
610 return DeclGroupPtrTy();
611
612 DKind = ParseOpenMPDirectiveKind(*this);
613 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
614 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
615 ParsedAttributesWithRange attrs(AttrFactory);
616 MaybeParseCXX11Attributes(attrs);
617 MaybeParseMicrosoftAttributes(attrs);
618 ParseExternalDeclaration(attrs);
619 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
620 TentativeParsingAction TPA(*this);
621 ConsumeToken();
622 DKind = ParseOpenMPDirectiveKind(*this);
623 if (DKind != OMPD_end_declare_target)
624 TPA.Revert();
625 else
626 TPA.Commit();
627 }
628 }
629
630 if (DKind == OMPD_end_declare_target) {
631 ConsumeAnyToken();
632 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
633 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
634 << getOpenMPDirectiveName(OMPD_end_declare_target);
635 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
636 }
637 // Skip the last annot_pragma_openmp_end.
638 ConsumeAnyToken();
639 } else {
640 Diag(Tok, diag::err_expected_end_declare_target);
641 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
642 }
643 Actions.ActOnFinishOpenMPDeclareTargetDirective();
644 return DeclGroupPtrTy();
645 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000646 case OMPD_unknown:
647 Diag(Tok, diag::err_omp_unknown_directive);
648 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000649 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000650 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000651 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000652 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000653 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000654 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000655 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000656 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000657 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000658 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000659 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000660 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000661 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000662 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000663 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000664 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000665 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000666 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000667 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000668 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000669 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000670 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000671 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000672 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000673 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000674 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000675 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000676 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000677 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000678 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000679 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000680 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000681 case OMPD_end_declare_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000682 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000683 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000684 break;
685 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000686 while (Tok.isNot(tok::annot_pragma_openmp_end))
687 ConsumeAnyToken();
688 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000689 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000690}
691
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000692/// \brief Parsing of declarative or executable OpenMP directives.
693///
694/// threadprivate-directive:
695/// annot_pragma_openmp 'threadprivate' simple-variable-list
696/// annot_pragma_openmp_end
697///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000698/// declare-reduction-directive:
699/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
700/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
701/// ('omp_priv' '=' <expression>|<function_call>) ')']
702/// annot_pragma_openmp_end
703///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000704/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000705/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000706/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
707/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000708/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000709/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000710/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000711/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000712/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000713/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000714///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000715StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
716 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000718 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719 SmallVector<Expr *, 5> Identifiers;
720 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000721 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000722 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000723 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000724 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000725 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000726 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000727 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000728 // Name of critical directive.
729 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000730 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000731 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000732 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000733
734 switch (DKind) {
735 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000736 if (Allowed != ACK_Any) {
737 Diag(Tok, diag::err_omp_immediate_directive)
738 << getOpenMPDirectiveName(DKind) << 0;
739 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000740 ConsumeToken();
741 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
742 // The last seen token is annot_pragma_openmp_end - need to check for
743 // extra tokens.
744 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
745 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000746 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000747 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000748 }
749 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000750 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000751 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
752 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000753 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000754 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000755 case OMPD_declare_reduction:
756 ConsumeToken();
757 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
758 // The last seen token is annot_pragma_openmp_end - need to check for
759 // extra tokens.
760 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
761 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
762 << getOpenMPDirectiveName(OMPD_declare_reduction);
763 while (Tok.isNot(tok::annot_pragma_openmp_end))
764 ConsumeAnyToken();
765 }
766 ConsumeAnyToken();
767 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
768 } else
769 SkipUntil(tok::annot_pragma_openmp_end);
770 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000771 case OMPD_flush:
772 if (PP.LookAhead(0).is(tok::l_paren)) {
773 FlushHasClause = true;
774 // Push copy of the current token back to stream to properly parse
775 // pseudo-clause OMPFlushClause.
776 PP.EnterToken(Tok);
777 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000778 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000779 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000780 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000781 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000782 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000783 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000784 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000785 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000786 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000787 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000788 }
789 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000790 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000791 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000792 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000793 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000794 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000795 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000796 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000797 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000798 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000799 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000800 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000801 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000802 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000803 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000804 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000805 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000806 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000807 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000808 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000809 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000810 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000811 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000812 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000813 case OMPD_taskloop_simd:
814 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000815 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000816 // Parse directive name of the 'critical' directive if any.
817 if (DKind == OMPD_critical) {
818 BalancedDelimiterTracker T(*this, tok::l_paren,
819 tok::annot_pragma_openmp_end);
820 if (!T.consumeOpen()) {
821 if (Tok.isAnyIdentifier()) {
822 DirName =
823 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
824 ConsumeAnyToken();
825 } else {
826 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
827 }
828 T.consumeClose();
829 }
Alexey Bataev80909872015-07-02 11:25:17 +0000830 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000831 CancelRegion = ParseOpenMPDirectiveKind(*this);
832 if (Tok.isNot(tok::annot_pragma_openmp_end))
833 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000834 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000835
Alexey Bataevf29276e2014-06-18 04:14:57 +0000836 if (isOpenMPLoopDirective(DKind))
837 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
838 if (isOpenMPSimdDirective(DKind))
839 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
840 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000841 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000842
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000843 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000844 OpenMPClauseKind CKind =
845 Tok.isAnnotation()
846 ? OMPC_unknown
847 : FlushHasClause ? OMPC_flush
848 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000849 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000850 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000851 OMPClause *Clause =
852 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000853 FirstClauses[CKind].setInt(true);
854 if (Clause) {
855 FirstClauses[CKind].setPointer(Clause);
856 Clauses.push_back(Clause);
857 }
858
859 // Skip ',' if any.
860 if (Tok.is(tok::comma))
861 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000862 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000863 }
864 // End location of the directive.
865 EndLoc = Tok.getLocation();
866 // Consume final annot_pragma_openmp_end.
867 ConsumeToken();
868
Alexey Bataeveb482352015-12-18 05:05:56 +0000869 // OpenMP [2.13.8, ordered Construct, Syntax]
870 // If the depend clause is specified, the ordered construct is a stand-alone
871 // directive.
872 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000873 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000874 Diag(Loc, diag::err_omp_immediate_directive)
875 << getOpenMPDirectiveName(DKind) << 1
876 << getOpenMPClauseName(OMPC_depend);
877 }
878 HasAssociatedStatement = false;
879 }
880
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000881 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000882 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000883 // The body is a block scope like in Lambdas and Blocks.
884 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000885 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000886 Actions.ActOnStartOfCompoundStmt();
887 // Parse statement
888 AssociatedStmt = ParseStatement();
889 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000890 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000891 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000892 Directive = Actions.ActOnOpenMPExecutableDirective(
893 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
894 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000895
896 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000897 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000898 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000899 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000900 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000901 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000902 case OMPD_declare_target:
903 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000904 Diag(Tok, diag::err_omp_unexpected_directive)
905 << getOpenMPDirectiveName(DKind);
906 SkipUntil(tok::annot_pragma_openmp_end);
907 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000908 case OMPD_unknown:
909 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000910 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000912 }
913 return Directive;
914}
915
Alexey Bataeva769e072013-03-22 06:34:35 +0000916/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000917/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000918///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000919/// simple-variable-list:
920/// '(' id-expression {, id-expression} ')'
921///
922bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
923 SmallVectorImpl<Expr *> &VarList,
924 bool AllowScopeSpecifier) {
925 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000926 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000927 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000928 if (T.expectAndConsume(diag::err_expected_lparen_after,
929 getOpenMPDirectiveName(Kind)))
930 return true;
931 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000932 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000933
934 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000935 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000936 CXXScopeSpec SS;
937 SourceLocation TemplateKWLoc;
938 UnqualifiedId Name;
939 // Read var name.
940 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000941 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000942
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000943 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000944 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000945 IsCorrect = false;
946 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000947 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000948 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000949 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000950 IsCorrect = false;
951 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000952 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000953 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
954 Tok.isNot(tok::annot_pragma_openmp_end)) {
955 IsCorrect = false;
956 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000957 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000958 Diag(PrevTok.getLocation(), diag::err_expected)
959 << tok::identifier
960 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000961 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000962 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000963 ExprResult Res =
964 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000965 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000966 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000967 }
968 // Consume ','.
969 if (Tok.is(tok::comma)) {
970 ConsumeToken();
971 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000972 }
973
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000974 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000975 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000976 IsCorrect = false;
977 }
978
979 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000980 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000981
982 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000983}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000984
985/// \brief Parsing of OpenMP clauses.
986///
987/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000988/// if-clause | final-clause | num_threads-clause | safelen-clause |
989/// default-clause | private-clause | firstprivate-clause | shared-clause
990/// | linear-clause | aligned-clause | collapse-clause |
991/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000992/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000993/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000994/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000995/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000996/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000997/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000998///
999OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
1000 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00001001 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001002 bool ErrorFound = false;
1003 // Check if clause is allowed for the given directive.
1004 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00001005 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1006 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001007 ErrorFound = true;
1008 }
1009
1010 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00001011 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00001012 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001013 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00001014 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001015 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00001016 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00001017 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00001018 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001019 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00001020 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001021 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00001022 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00001023 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001024 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00001025 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00001026 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001027 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00001028 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00001029 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00001030 // OpenMP [2.9.1, target data construct, Restrictions]
1031 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00001032 // OpenMP [2.11.1, task Construct, Restrictions]
1033 // At most one if clause can appear on the directive.
1034 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00001035 // OpenMP [teams Construct, Restrictions]
1036 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00001037 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00001038 // OpenMP [2.9.1, task Construct, Restrictions]
1039 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001040 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1041 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00001042 // OpenMP [2.9.2, taskloop Construct, Restrictions]
1043 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001044 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001045 Diag(Tok, diag::err_omp_more_one_clause)
1046 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001047 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001048 }
1049
Alexey Bataev10e775f2015-07-30 11:36:16 +00001050 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
1051 Clause = ParseOpenMPClause(CKind);
1052 else
1053 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001054 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001055 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001056 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001057 // OpenMP [2.14.3.1, Restrictions]
1058 // Only a single default clause may be specified on a parallel, task or
1059 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001060 // OpenMP [2.5, parallel Construct, Restrictions]
1061 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001062 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001063 Diag(Tok, diag::err_omp_more_one_clause)
1064 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001065 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001066 }
1067
1068 Clause = ParseOpenMPSimpleClause(CKind);
1069 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001070 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00001071 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001072 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001073 // OpenMP [2.7.1, Restrictions, p. 3]
1074 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001075 // OpenMP [2.10.4, Restrictions, p. 106]
1076 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +00001077 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001078 Diag(Tok, diag::err_omp_more_one_clause)
1079 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001080 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001081 }
1082
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001083 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001084 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
1085 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001086 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001087 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001088 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001089 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00001090 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00001091 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00001092 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00001093 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00001094 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001095 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00001096 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001097 // OpenMP [2.7.1, Restrictions, p. 9]
1098 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00001099 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
1100 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001101 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001102 Diag(Tok, diag::err_omp_more_one_clause)
1103 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00001104 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001105 }
1106
1107 Clause = ParseOpenMPClause(CKind);
1108 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001109 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001110 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001111 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001112 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001113 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001114 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001115 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001116 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001117 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00001118 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001119 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00001120 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +00001121 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122 break;
1123 case OMPC_unknown:
1124 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00001125 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001126 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001127 break;
1128 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001129 case OMPC_uniform:
Alexey Bataeva55ed262014-05-28 06:15:33 +00001130 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
1131 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001132 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001133 break;
1134 }
Craig Topper161e4db2014-05-21 06:02:52 +00001135 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001136}
1137
Alexey Bataev2af33e32016-04-07 12:45:37 +00001138/// Parses simple expression in parens for single-expression clauses of OpenMP
1139/// constructs.
1140/// \param RLoc Returned location of right paren.
1141ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
1142 SourceLocation &RLoc) {
1143 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1144 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
1145 return ExprError();
1146
1147 SourceLocation ELoc = Tok.getLocation();
1148 ExprResult LHS(ParseCastExpression(
1149 /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
1150 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
1151 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
1152
1153 // Parse ')'.
1154 T.consumeClose();
1155
1156 RLoc = T.getCloseLocation();
1157 return Val;
1158}
1159
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001160/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001161/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001162/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001163///
Alexey Bataev3778b602014-07-17 07:32:53 +00001164/// final-clause:
1165/// 'final' '(' expression ')'
1166///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001167/// num_threads-clause:
1168/// 'num_threads' '(' expression ')'
1169///
1170/// safelen-clause:
1171/// 'safelen' '(' expression ')'
1172///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001173/// simdlen-clause:
1174/// 'simdlen' '(' expression ')'
1175///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001176/// collapse-clause:
1177/// 'collapse' '(' expression ')'
1178///
Alexey Bataeva0569352015-12-01 10:17:31 +00001179/// priority-clause:
1180/// 'priority' '(' expression ')'
1181///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001182/// grainsize-clause:
1183/// 'grainsize' '(' expression ')'
1184///
Alexey Bataev382967a2015-12-08 12:06:20 +00001185/// num_tasks-clause:
1186/// 'num_tasks' '(' expression ')'
1187///
Alexey Bataev28c75412015-12-15 08:19:24 +00001188/// hint-clause:
1189/// 'hint' '(' expression ')'
1190///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001191OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1192 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00001193 SourceLocation LLoc = Tok.getLocation();
1194 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001195
Alexey Bataev2af33e32016-04-07 12:45:37 +00001196 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001197
1198 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001199 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001200
Alexey Bataev2af33e32016-04-07 12:45:37 +00001201 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001202}
1203
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001204/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001205///
1206/// default-clause:
1207/// 'default' '(' 'none' | 'shared' ')
1208///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001209/// proc_bind-clause:
1210/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1211///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001212OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1213 SourceLocation Loc = Tok.getLocation();
1214 SourceLocation LOpen = ConsumeToken();
1215 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001216 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217 if (T.expectAndConsume(diag::err_expected_lparen_after,
1218 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001219 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001220
Alexey Bataeva55ed262014-05-28 06:15:33 +00001221 unsigned Type = getOpenMPSimpleClauseType(
1222 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001223 SourceLocation TypeLoc = Tok.getLocation();
1224 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1225 Tok.isNot(tok::annot_pragma_openmp_end))
1226 ConsumeAnyToken();
1227
1228 // Parse ')'.
1229 T.consumeClose();
1230
1231 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1232 Tok.getLocation());
1233}
1234
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001235/// \brief Parsing of OpenMP clauses like 'ordered'.
1236///
1237/// ordered-clause:
1238/// 'ordered'
1239///
Alexey Bataev236070f2014-06-20 11:19:47 +00001240/// nowait-clause:
1241/// 'nowait'
1242///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001243/// untied-clause:
1244/// 'untied'
1245///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001246/// mergeable-clause:
1247/// 'mergeable'
1248///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001249/// read-clause:
1250/// 'read'
1251///
Alexey Bataev346265e2015-09-25 10:37:12 +00001252/// threads-clause:
1253/// 'threads'
1254///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001255/// simd-clause:
1256/// 'simd'
1257///
Alexey Bataevb825de12015-12-07 10:51:44 +00001258/// nogroup-clause:
1259/// 'nogroup'
1260///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001261OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1262 SourceLocation Loc = Tok.getLocation();
1263 ConsumeAnyToken();
1264
1265 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1266}
1267
1268
Alexey Bataev56dafe82014-06-20 07:16:17 +00001269/// \brief Parsing of OpenMP clauses with single expressions and some additional
1270/// argument like 'schedule' or 'dist_schedule'.
1271///
1272/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001273/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1274/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001275///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001276/// if-clause:
1277/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1278///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001279/// defaultmap:
1280/// 'defaultmap' '(' modifier ':' kind ')'
1281///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001282OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1283 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001284 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001285 // Parse '('.
1286 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1287 if (T.expectAndConsume(diag::err_expected_lparen_after,
1288 getOpenMPClauseName(Kind)))
1289 return nullptr;
1290
1291 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001292 SmallVector<unsigned, 4> Arg;
1293 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001294 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001295 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1296 Arg.resize(NumberOfElements);
1297 KLoc.resize(NumberOfElements);
1298 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1299 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1300 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1301 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001302 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001303 if (KindModifier > OMPC_SCHEDULE_unknown) {
1304 // Parse 'modifier'
1305 Arg[Modifier1] = KindModifier;
1306 KLoc[Modifier1] = Tok.getLocation();
1307 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1308 Tok.isNot(tok::annot_pragma_openmp_end))
1309 ConsumeAnyToken();
1310 if (Tok.is(tok::comma)) {
1311 // Parse ',' 'modifier'
1312 ConsumeAnyToken();
1313 KindModifier = getOpenMPSimpleClauseType(
1314 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1315 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1316 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001317 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001318 KLoc[Modifier2] = Tok.getLocation();
1319 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1320 Tok.isNot(tok::annot_pragma_openmp_end))
1321 ConsumeAnyToken();
1322 }
1323 // Parse ':'
1324 if (Tok.is(tok::colon))
1325 ConsumeAnyToken();
1326 else
1327 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1328 KindModifier = getOpenMPSimpleClauseType(
1329 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1330 }
1331 Arg[ScheduleKind] = KindModifier;
1332 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001333 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1334 Tok.isNot(tok::annot_pragma_openmp_end))
1335 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001336 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1337 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1338 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001339 Tok.is(tok::comma))
1340 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001341 } else if (Kind == OMPC_dist_schedule) {
1342 Arg.push_back(getOpenMPSimpleClauseType(
1343 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1344 KLoc.push_back(Tok.getLocation());
1345 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1346 Tok.isNot(tok::annot_pragma_openmp_end))
1347 ConsumeAnyToken();
1348 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1349 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001350 } else if (Kind == OMPC_defaultmap) {
1351 // Get a defaultmap modifier
1352 Arg.push_back(getOpenMPSimpleClauseType(
1353 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1354 KLoc.push_back(Tok.getLocation());
1355 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1356 Tok.isNot(tok::annot_pragma_openmp_end))
1357 ConsumeAnyToken();
1358 // Parse ':'
1359 if (Tok.is(tok::colon))
1360 ConsumeAnyToken();
1361 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1362 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1363 // Get a defaultmap kind
1364 Arg.push_back(getOpenMPSimpleClauseType(
1365 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1366 KLoc.push_back(Tok.getLocation());
1367 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1368 Tok.isNot(tok::annot_pragma_openmp_end))
1369 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001370 } else {
1371 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001372 KLoc.push_back(Tok.getLocation());
1373 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1374 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001375 ConsumeToken();
1376 if (Tok.is(tok::colon))
1377 DelimLoc = ConsumeToken();
1378 else
1379 Diag(Tok, diag::warn_pragma_expected_colon)
1380 << "directive name modifier";
1381 }
1382 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001383
Carlo Bertollib4adf552016-01-15 18:50:31 +00001384 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1385 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1386 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001387 if (NeedAnExpression) {
1388 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001389 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1390 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001391 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001392 }
1393
1394 // Parse ')'.
1395 T.consumeClose();
1396
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001397 if (NeedAnExpression && Val.isInvalid())
1398 return nullptr;
1399
Alexey Bataev56dafe82014-06-20 07:16:17 +00001400 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001401 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001402 T.getCloseLocation());
1403}
1404
Alexey Bataevc5e02582014-06-16 07:08:35 +00001405static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1406 UnqualifiedId &ReductionId) {
1407 SourceLocation TemplateKWLoc;
1408 if (ReductionIdScopeSpec.isEmpty()) {
1409 auto OOK = OO_None;
1410 switch (P.getCurToken().getKind()) {
1411 case tok::plus:
1412 OOK = OO_Plus;
1413 break;
1414 case tok::minus:
1415 OOK = OO_Minus;
1416 break;
1417 case tok::star:
1418 OOK = OO_Star;
1419 break;
1420 case tok::amp:
1421 OOK = OO_Amp;
1422 break;
1423 case tok::pipe:
1424 OOK = OO_Pipe;
1425 break;
1426 case tok::caret:
1427 OOK = OO_Caret;
1428 break;
1429 case tok::ampamp:
1430 OOK = OO_AmpAmp;
1431 break;
1432 case tok::pipepipe:
1433 OOK = OO_PipePipe;
1434 break;
1435 default:
1436 break;
1437 }
1438 if (OOK != OO_None) {
1439 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001440 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001441 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1442 return false;
1443 }
1444 }
1445 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1446 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001447 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001448 TemplateKWLoc, ReductionId);
1449}
1450
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001451/// Parses clauses with list.
1452bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
1453 OpenMPClauseKind Kind,
1454 SmallVectorImpl<Expr *> &Vars,
1455 OpenMPVarListDataTy &Data) {
1456 UnqualifiedId UnqualifiedReductionId;
1457 bool InvalidReductionId = false;
1458 bool MapTypeModifierSpecified = false;
1459
1460 // Parse '('.
1461 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1462 if (T.expectAndConsume(diag::err_expected_lparen_after,
1463 getOpenMPClauseName(Kind)))
1464 return true;
1465
1466 bool NeedRParenForLinear = false;
1467 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1468 tok::annot_pragma_openmp_end);
1469 // Handle reduction-identifier for reduction clause.
1470 if (Kind == OMPC_reduction) {
1471 ColonProtectionRAIIObject ColonRAII(*this);
1472 if (getLangOpts().CPlusPlus)
1473 ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
1474 /*ObjectType=*/nullptr,
1475 /*EnteringContext=*/false);
1476 InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
1477 UnqualifiedReductionId);
1478 if (InvalidReductionId) {
1479 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1480 StopBeforeMatch);
1481 }
1482 if (Tok.is(tok::colon))
1483 Data.ColonLoc = ConsumeToken();
1484 else
1485 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1486 if (!InvalidReductionId)
1487 Data.ReductionId =
1488 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
1489 } else if (Kind == OMPC_depend) {
1490 // Handle dependency type for depend clause.
1491 ColonProtectionRAIIObject ColonRAII(*this);
1492 Data.DepKind =
1493 static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1494 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1495 Data.DepLinMapLoc = Tok.getLocation();
1496
1497 if (Data.DepKind == OMPC_DEPEND_unknown) {
1498 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1499 StopBeforeMatch);
1500 } else {
1501 ConsumeToken();
1502 // Special processing for depend(source) clause.
1503 if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
1504 // Parse ')'.
1505 T.consumeClose();
1506 return false;
1507 }
1508 }
1509 if (Tok.is(tok::colon))
1510 Data.ColonLoc = ConsumeToken();
1511 else {
1512 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1513 : diag::warn_pragma_expected_colon)
1514 << "dependency type";
1515 }
1516 } else if (Kind == OMPC_linear) {
1517 // Try to parse modifier if any.
1518 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
1519 Data.LinKind = static_cast<OpenMPLinearClauseKind>(
1520 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
1521 Data.DepLinMapLoc = ConsumeToken();
1522 LinearT.consumeOpen();
1523 NeedRParenForLinear = true;
1524 }
1525 } else if (Kind == OMPC_map) {
1526 // Handle map type for map clause.
1527 ColonProtectionRAIIObject ColonRAII(*this);
1528
1529 /// The map clause modifier token can be either a identifier or the C++
1530 /// delete keyword.
1531 auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
1532 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1533 };
1534
1535 // The first identifier may be a list item, a map-type or a
1536 // map-type-modifier. The map modifier can also be delete which has the same
1537 // spelling of the C++ delete keyword.
1538 Data.MapType =
1539 IsMapClauseModifierToken(Tok)
1540 ? static_cast<OpenMPMapClauseKind>(
1541 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1542 : OMPC_MAP_unknown;
1543 Data.DepLinMapLoc = Tok.getLocation();
1544 bool ColonExpected = false;
1545
1546 if (IsMapClauseModifierToken(Tok)) {
1547 if (PP.LookAhead(0).is(tok::colon)) {
1548 if (Data.MapType == OMPC_MAP_unknown)
1549 Diag(Tok, diag::err_omp_unknown_map_type);
1550 else if (Data.MapType == OMPC_MAP_always)
1551 Diag(Tok, diag::err_omp_map_type_missing);
1552 ConsumeToken();
1553 } else if (PP.LookAhead(0).is(tok::comma)) {
1554 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
1555 PP.LookAhead(2).is(tok::colon)) {
1556 Data.MapTypeModifier = Data.MapType;
1557 if (Data.MapTypeModifier != OMPC_MAP_always) {
1558 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1559 Data.MapTypeModifier = OMPC_MAP_unknown;
1560 } else
1561 MapTypeModifierSpecified = true;
1562
1563 ConsumeToken();
1564 ConsumeToken();
1565
1566 Data.MapType =
1567 IsMapClauseModifierToken(Tok)
1568 ? static_cast<OpenMPMapClauseKind>(
1569 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
1570 : OMPC_MAP_unknown;
1571 if (Data.MapType == OMPC_MAP_unknown ||
1572 Data.MapType == OMPC_MAP_always)
1573 Diag(Tok, diag::err_omp_unknown_map_type);
1574 ConsumeToken();
1575 } else {
1576 Data.MapType = OMPC_MAP_tofrom;
1577 Data.IsMapTypeImplicit = true;
1578 }
1579 } else {
1580 Data.MapType = OMPC_MAP_tofrom;
1581 Data.IsMapTypeImplicit = true;
1582 }
1583 } else {
1584 Data.MapType = OMPC_MAP_tofrom;
1585 Data.IsMapTypeImplicit = true;
1586 }
1587
1588 if (Tok.is(tok::colon))
1589 Data.ColonLoc = ConsumeToken();
1590 else if (ColonExpected)
1591 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1592 }
1593
1594 bool IsComma =
1595 (Kind != OMPC_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
1596 (Kind == OMPC_reduction && !InvalidReductionId) ||
1597 (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
1598 (!MapTypeModifierSpecified ||
1599 Data.MapTypeModifier == OMPC_MAP_always)) ||
1600 (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
1601 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
1602 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
1603 Tok.isNot(tok::annot_pragma_openmp_end))) {
1604 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
1605 // Parse variable
1606 ExprResult VarExpr =
1607 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
1608 if (VarExpr.isUsable())
1609 Vars.push_back(VarExpr.get());
1610 else {
1611 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1612 StopBeforeMatch);
1613 }
1614 // Skip ',' if any
1615 IsComma = Tok.is(tok::comma);
1616 if (IsComma)
1617 ConsumeToken();
1618 else if (Tok.isNot(tok::r_paren) &&
1619 Tok.isNot(tok::annot_pragma_openmp_end) &&
1620 (!MayHaveTail || Tok.isNot(tok::colon)))
1621 Diag(Tok, diag::err_omp_expected_punc)
1622 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1623 : getOpenMPClauseName(Kind))
1624 << (Kind == OMPC_flush);
1625 }
1626
1627 // Parse ')' for linear clause with modifier.
1628 if (NeedRParenForLinear)
1629 LinearT.consumeClose();
1630
1631 // Parse ':' linear-step (or ':' alignment).
1632 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1633 if (MustHaveTail) {
1634 Data.ColonLoc = Tok.getLocation();
1635 SourceLocation ELoc = ConsumeToken();
1636 ExprResult Tail = ParseAssignmentExpression();
1637 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
1638 if (Tail.isUsable())
1639 Data.TailExpr = Tail.get();
1640 else
1641 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1642 StopBeforeMatch);
1643 }
1644
1645 // Parse ')'.
1646 T.consumeClose();
1647 if ((Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
1648 Vars.empty()) ||
1649 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1650 (MustHaveTail && !Data.TailExpr) || InvalidReductionId)
1651 return true;
1652 return false;
1653}
1654
Alexander Musman1bb328c2014-06-04 13:06:39 +00001655/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001656/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001657///
1658/// private-clause:
1659/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001660/// firstprivate-clause:
1661/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001662/// lastprivate-clause:
1663/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001664/// shared-clause:
1665/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001666/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001667/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001668/// aligned-clause:
1669/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001670/// reduction-clause:
1671/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001672/// copyprivate-clause:
1673/// 'copyprivate' '(' list ')'
1674/// flush-clause:
1675/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001676/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001677/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001678/// map-clause:
1679/// 'map' '(' [ [ always , ]
1680/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001681///
Alexey Bataev182227b2015-08-20 10:54:39 +00001682/// For 'linear' clause linear-list may have the following forms:
1683/// list
1684/// modifier(list)
1685/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001686OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1687 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001688 SourceLocation Loc = Tok.getLocation();
1689 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001690 SmallVector<Expr *, 4> Vars;
1691 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001692
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001693 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00001694 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001695
Alexey Bataevc5e02582014-06-16 07:08:35 +00001696 return Actions.ActOnOpenMPVarListClause(
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001697 Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
1698 Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
1699 Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
1700 Data.DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001701}
1702