blob: dcff1595611dd571d4ce110ff7b3378155cb2991 [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 Polukhin82478332016-02-13 06:53:38 +000034 OMPD_enter,
35 OMPD_exit,
36 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000037 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000038 OMPD_target_enter,
39 OMPD_target_exit
40};
41} // namespace
42
43// Map token string to extended OMP token kind that are
44// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
45static unsigned getOpenMPDirectiveKindEx(StringRef S) {
46 auto DKind = getOpenMPDirectiveKind(S);
47 if (DKind != OMPD_unknown)
48 return DKind;
49
50 return llvm::StringSwitch<unsigned>(S)
51 .Case("cancellation", OMPD_cancellation)
52 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000053 .Case("declare", OMPD_declare)
Dmitry Polukhin82478332016-02-13 06:53:38 +000054 .Case("enter", OMPD_enter)
55 .Case("exit", OMPD_exit)
56 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000057 .Case("reduction", OMPD_reduction)
Dmitry Polukhin82478332016-02-13 06:53:38 +000058 .Default(OMPD_unknown);
59}
60
Alexey Bataev4acb8592014-07-07 13:01:15 +000061static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000062 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
63 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
64 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000065 static const unsigned F[][3] = {
66 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000067 { OMPD_declare, OMPD_reduction, OMPD_declare_reduction },
Alexey Bataev587e1de2016-03-30 10:43:55 +000068 { OMPD_declare, OMPD_simd, OMPD_declare_simd },
Dmitry Polukhin82478332016-02-13 06:53:38 +000069 { OMPD_target, OMPD_data, OMPD_target_data },
70 { OMPD_target, OMPD_enter, OMPD_target_enter },
71 { OMPD_target, OMPD_exit, OMPD_target_exit },
72 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
73 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
74 { OMPD_for, OMPD_simd, OMPD_for_simd },
75 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
76 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
77 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
78 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
79 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
80 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
81 };
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000082 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000083 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +000084 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +000085 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +000086 ? static_cast<unsigned>(OMPD_unknown)
87 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
88 if (DKind == OMPD_unknown)
89 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +000090
Alexander Musmanf82886e2014-09-18 05:12:34 +000091 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +000092 if (DKind != F[i][0])
93 continue;
Michael Wong65f367f2015-07-21 13:44:28 +000094
Dmitry Polukhin82478332016-02-13 06:53:38 +000095 Tok = P.getPreprocessor().LookAhead(0);
96 unsigned SDKind =
97 Tok.isAnnotation()
98 ? static_cast<unsigned>(OMPD_unknown)
99 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
100 if (SDKind == OMPD_unknown)
101 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000102
Dmitry Polukhin82478332016-02-13 06:53:38 +0000103 if (SDKind == F[i][1]) {
104 P.ConsumeToken();
105 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000106 }
107 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000108 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
109 : OMPD_unknown;
110}
111
112static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000113 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000114 Sema &Actions = P.getActions();
115 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000116 // Allow to use 'operator' keyword for C++ operators
117 bool WithOperator = false;
118 if (Tok.is(tok::kw_operator)) {
119 P.ConsumeToken();
120 Tok = P.getCurToken();
121 WithOperator = true;
122 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000123 switch (Tok.getKind()) {
124 case tok::plus: // '+'
125 OOK = OO_Plus;
126 break;
127 case tok::minus: // '-'
128 OOK = OO_Minus;
129 break;
130 case tok::star: // '*'
131 OOK = OO_Star;
132 break;
133 case tok::amp: // '&'
134 OOK = OO_Amp;
135 break;
136 case tok::pipe: // '|'
137 OOK = OO_Pipe;
138 break;
139 case tok::caret: // '^'
140 OOK = OO_Caret;
141 break;
142 case tok::ampamp: // '&&'
143 OOK = OO_AmpAmp;
144 break;
145 case tok::pipepipe: // '||'
146 OOK = OO_PipePipe;
147 break;
148 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000149 if (!WithOperator)
150 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000151 default:
152 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
153 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
154 Parser::StopBeforeMatch);
155 return DeclarationName();
156 }
157 P.ConsumeToken();
158 auto &DeclNames = Actions.getASTContext().DeclarationNames;
159 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
160 : DeclNames.getCXXOperatorName(OOK);
161}
162
163/// \brief Parse 'omp declare reduction' construct.
164///
165/// declare-reduction-directive:
166/// annot_pragma_openmp 'declare' 'reduction'
167/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
168/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
169/// annot_pragma_openmp_end
170/// <reduction_id> is either a base language identifier or one of the following
171/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
172///
173Parser::DeclGroupPtrTy
174Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
175 // Parse '('.
176 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
177 if (T.expectAndConsume(diag::err_expected_lparen_after,
178 getOpenMPDirectiveName(OMPD_declare_reduction))) {
179 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
180 return DeclGroupPtrTy();
181 }
182
183 DeclarationName Name = parseOpenMPReductionId(*this);
184 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
185 return DeclGroupPtrTy();
186
187 // Consume ':'.
188 bool IsCorrect = !ExpectAndConsume(tok::colon);
189
190 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
191 return DeclGroupPtrTy();
192
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000193 IsCorrect = IsCorrect && !Name.isEmpty();
194
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000195 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
196 Diag(Tok.getLocation(), diag::err_expected_type);
197 IsCorrect = false;
198 }
199
200 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
201 return DeclGroupPtrTy();
202
203 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
204 // Parse list of types until ':' token.
205 do {
206 ColonProtectionRAIIObject ColonRAII(*this);
207 SourceRange Range;
208 TypeResult TR = ParseTypeName(&Range, Declarator::PrototypeContext, AS);
209 if (TR.isUsable()) {
210 auto ReductionType =
211 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
212 if (!ReductionType.isNull()) {
213 ReductionTypes.push_back(
214 std::make_pair(ReductionType, Range.getBegin()));
215 }
216 } else {
217 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
218 StopBeforeMatch);
219 }
220
221 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
222 break;
223
224 // Consume ','.
225 if (ExpectAndConsume(tok::comma)) {
226 IsCorrect = false;
227 if (Tok.is(tok::annot_pragma_openmp_end)) {
228 Diag(Tok.getLocation(), diag::err_expected_type);
229 return DeclGroupPtrTy();
230 }
231 }
232 } while (Tok.isNot(tok::annot_pragma_openmp_end));
233
234 if (ReductionTypes.empty()) {
235 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
236 return DeclGroupPtrTy();
237 }
238
239 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
240 return DeclGroupPtrTy();
241
242 // Consume ':'.
243 if (ExpectAndConsume(tok::colon))
244 IsCorrect = false;
245
246 if (Tok.is(tok::annot_pragma_openmp_end)) {
247 Diag(Tok.getLocation(), diag::err_expected_expression);
248 return DeclGroupPtrTy();
249 }
250
251 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
252 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
253
254 // Parse <combiner> expression and then parse initializer if any for each
255 // correct type.
256 unsigned I = 0, E = ReductionTypes.size();
257 for (auto *D : DRD.get()) {
258 TentativeParsingAction TPA(*this);
259 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
260 Scope::OpenMPDirectiveScope);
261 // Parse <combiner> expression.
262 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
263 ExprResult CombinerResult =
264 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
265 D->getLocation(), /*DiscardedValue=*/true);
266 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
267
268 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
269 Tok.isNot(tok::annot_pragma_openmp_end)) {
270 TPA.Commit();
271 IsCorrect = false;
272 break;
273 }
274 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
275 ExprResult InitializerResult;
276 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
277 // Parse <initializer> expression.
278 if (Tok.is(tok::identifier) &&
279 Tok.getIdentifierInfo()->isStr("initializer"))
280 ConsumeToken();
281 else {
282 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
283 TPA.Commit();
284 IsCorrect = false;
285 break;
286 }
287 // Parse '('.
288 BalancedDelimiterTracker T(*this, tok::l_paren,
289 tok::annot_pragma_openmp_end);
290 IsCorrect =
291 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
292 IsCorrect;
293 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
294 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
295 Scope::OpenMPDirectiveScope);
296 // Parse expression.
297 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(), D);
298 InitializerResult = Actions.ActOnFinishFullExpr(
299 ParseAssignmentExpression().get(), D->getLocation(),
300 /*DiscardedValue=*/true);
301 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
302 D, InitializerResult.get());
303 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
304 Tok.isNot(tok::annot_pragma_openmp_end)) {
305 TPA.Commit();
306 IsCorrect = false;
307 break;
308 }
309 IsCorrect =
310 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
311 }
312 }
313
314 ++I;
315 // Revert parsing if not the last type, otherwise accept it, we're done with
316 // parsing.
317 if (I != E)
318 TPA.Revert();
319 else
320 TPA.Commit();
321 }
322 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
323 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000324}
325
Alexey Bataev20dfd772016-04-04 10:12:15 +0000326/// Parses clauses for 'declare simd' directive.
327/// clause:
328/// 'inbranch' | 'notinbranch'
329static void parseDeclareSimdClauses(Parser &P,
330 OMPDeclareSimdDeclAttr::BranchStateTy &BS) {
331 SourceRange BSRange;
332 const Token &Tok = P.getCurToken();
333 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
334 if (Tok.isNot(tok::identifier))
335 break;
336 OMPDeclareSimdDeclAttr::BranchStateTy Out;
337 StringRef TokName = Tok.getIdentifierInfo()->getName();
338 // Parse 'inranch|notinbranch' clauses.
339 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(TokName, Out)) {
340 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
341 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
342 << TokName << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS)
343 << BSRange;
344 }
345 BS = Out;
346 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
347 } else
348 // TODO: add parsing of other clauses.
349 break;
350 P.ConsumeToken();
351 // Skip ',' if any.
352 if (Tok.is(tok::comma))
353 P.ConsumeToken();
354 }
355}
356
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000357/// \brief Parsing of declarative OpenMP directives.
358///
359/// threadprivate-directive:
360/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000361/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000362///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000363/// declare-reduction-directive:
364/// annot_pragma_openmp 'declare' 'reduction' [...]
365/// annot_pragma_openmp_end
366///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000367/// declare-simd-directive:
368/// annot_pragma_openmp 'declare simd' {<clause> [,]}
369/// annot_pragma_openmp_end
370/// <function declaration/definition>
371///
372Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
373 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
374 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000375 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000376 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000377
378 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000379 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000380 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000381
382 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000383 case OMPD_threadprivate:
384 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000385 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000386 // The last seen token is annot_pragma_openmp_end - need to check for
387 // extra tokens.
388 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
389 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000390 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000391 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000392 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000393 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000394 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000395 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000396 }
397 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000398 case OMPD_declare_reduction:
399 ConsumeToken();
400 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
401 // The last seen token is annot_pragma_openmp_end - need to check for
402 // extra tokens.
403 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
404 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
405 << getOpenMPDirectiveName(OMPD_declare_reduction);
406 while (Tok.isNot(tok::annot_pragma_openmp_end))
407 ConsumeAnyToken();
408 }
409 // Skip the last annot_pragma_openmp_end.
410 ConsumeToken();
411 return Res;
412 }
413 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000414 case OMPD_declare_simd: {
415 // The syntax is:
416 // { #pragma omp declare simd }
417 // <function-declaration-or-definition>
418 //
419
420 ConsumeToken();
Alexey Bataev20dfd772016-04-04 10:12:15 +0000421 OMPDeclareSimdDeclAttr::BranchStateTy BS =
422 OMPDeclareSimdDeclAttr::BS_Undefined;
423 parseDeclareSimdClauses(*this, BS);
424
425 // Need to check for extra tokens.
Alexey Bataev587e1de2016-03-30 10:43:55 +0000426 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
427 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
428 << getOpenMPDirectiveName(OMPD_declare_simd);
429 while (Tok.isNot(tok::annot_pragma_openmp_end))
430 ConsumeAnyToken();
431 }
432 // Skip the last annot_pragma_openmp_end.
Alexey Bataev20dfd772016-04-04 10:12:15 +0000433 SourceLocation EndLoc = ConsumeToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000434
435 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000436 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000437 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000438 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000439 // Here we expect to see some function declaration.
440 if (AS == AS_none) {
441 assert(TagType == DeclSpec::TST_unspecified);
442 MaybeParseCXX11Attributes(Attrs);
443 MaybeParseMicrosoftAttributes(Attrs);
444 ParsingDeclSpec PDS(*this);
445 Ptr = ParseExternalDeclaration(Attrs, &PDS);
446 } else {
447 Ptr =
448 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
449 }
450 }
451 if (!Ptr) {
452 Diag(Loc, diag::err_omp_decl_in_declare_simd);
453 return DeclGroupPtrTy();
454 }
455
Alexey Bataev20dfd772016-04-04 10:12:15 +0000456 return Actions.ActOnOpenMPDeclareSimdDirective(Ptr, BS,
457 SourceRange(Loc, EndLoc));
Alexey Bataev587e1de2016-03-30 10:43:55 +0000458 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000459 case OMPD_unknown:
460 Diag(Tok, diag::err_omp_unknown_directive);
461 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000462 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000463 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000464 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000465 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000466 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000467 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000468 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000469 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000470 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000471 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000472 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000473 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000474 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000475 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000476 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000477 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000478 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000479 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000480 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000481 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000482 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000483 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000484 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000485 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000486 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000487 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000488 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000489 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000490 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000491 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000492 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000493 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000494 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000495 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000496 break;
497 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000498 while (Tok.isNot(tok::annot_pragma_openmp_end))
499 ConsumeAnyToken();
500 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000501 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000502}
503
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000504/// \brief Parsing of declarative or executable OpenMP directives.
505///
506/// threadprivate-directive:
507/// annot_pragma_openmp 'threadprivate' simple-variable-list
508/// annot_pragma_openmp_end
509///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000510/// declare-reduction-directive:
511/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
512/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
513/// ('omp_priv' '=' <expression>|<function_call>) ')']
514/// annot_pragma_openmp_end
515///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000516/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000517/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000518/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
519/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000520/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000521/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000522/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000523/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000524/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000525/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000526///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000527StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
528 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000529 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000530 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000531 SmallVector<Expr *, 5> Identifiers;
532 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000533 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000534 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000535 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000536 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000537 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000538 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000539 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000540 // Name of critical directive.
541 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000542 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000543 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000544 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000545
546 switch (DKind) {
547 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000548 if (Allowed != ACK_Any) {
549 Diag(Tok, diag::err_omp_immediate_directive)
550 << getOpenMPDirectiveName(DKind) << 0;
551 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000552 ConsumeToken();
553 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
554 // The last seen token is annot_pragma_openmp_end - need to check for
555 // extra tokens.
556 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
557 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000558 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000559 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000560 }
561 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000562 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000563 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
564 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000565 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000566 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000567 case OMPD_declare_reduction:
568 ConsumeToken();
569 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
570 // The last seen token is annot_pragma_openmp_end - need to check for
571 // extra tokens.
572 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
573 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
574 << getOpenMPDirectiveName(OMPD_declare_reduction);
575 while (Tok.isNot(tok::annot_pragma_openmp_end))
576 ConsumeAnyToken();
577 }
578 ConsumeAnyToken();
579 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
580 } else
581 SkipUntil(tok::annot_pragma_openmp_end);
582 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000583 case OMPD_flush:
584 if (PP.LookAhead(0).is(tok::l_paren)) {
585 FlushHasClause = true;
586 // Push copy of the current token back to stream to properly parse
587 // pseudo-clause OMPFlushClause.
588 PP.EnterToken(Tok);
589 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000590 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000591 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000592 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000593 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000594 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000595 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000596 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000597 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000598 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000599 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000600 }
601 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000602 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000603 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000604 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000605 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000606 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000607 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000608 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000609 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000610 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000611 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000612 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000613 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000614 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000615 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000616 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000617 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000618 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000619 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000620 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000621 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000622 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000623 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000624 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000625 case OMPD_taskloop_simd:
626 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000627 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000628 // Parse directive name of the 'critical' directive if any.
629 if (DKind == OMPD_critical) {
630 BalancedDelimiterTracker T(*this, tok::l_paren,
631 tok::annot_pragma_openmp_end);
632 if (!T.consumeOpen()) {
633 if (Tok.isAnyIdentifier()) {
634 DirName =
635 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
636 ConsumeAnyToken();
637 } else {
638 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
639 }
640 T.consumeClose();
641 }
Alexey Bataev80909872015-07-02 11:25:17 +0000642 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000643 CancelRegion = ParseOpenMPDirectiveKind(*this);
644 if (Tok.isNot(tok::annot_pragma_openmp_end))
645 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000646 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000647
Alexey Bataevf29276e2014-06-18 04:14:57 +0000648 if (isOpenMPLoopDirective(DKind))
649 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
650 if (isOpenMPSimdDirective(DKind))
651 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
652 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000653 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000655 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000656 OpenMPClauseKind CKind =
657 Tok.isAnnotation()
658 ? OMPC_unknown
659 : FlushHasClause ? OMPC_flush
660 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000661 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000662 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000663 OMPClause *Clause =
664 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000665 FirstClauses[CKind].setInt(true);
666 if (Clause) {
667 FirstClauses[CKind].setPointer(Clause);
668 Clauses.push_back(Clause);
669 }
670
671 // Skip ',' if any.
672 if (Tok.is(tok::comma))
673 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000674 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000675 }
676 // End location of the directive.
677 EndLoc = Tok.getLocation();
678 // Consume final annot_pragma_openmp_end.
679 ConsumeToken();
680
Alexey Bataeveb482352015-12-18 05:05:56 +0000681 // OpenMP [2.13.8, ordered Construct, Syntax]
682 // If the depend clause is specified, the ordered construct is a stand-alone
683 // directive.
684 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000685 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000686 Diag(Loc, diag::err_omp_immediate_directive)
687 << getOpenMPDirectiveName(DKind) << 1
688 << getOpenMPClauseName(OMPC_depend);
689 }
690 HasAssociatedStatement = false;
691 }
692
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000693 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000694 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000695 // The body is a block scope like in Lambdas and Blocks.
696 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000697 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000698 Actions.ActOnStartOfCompoundStmt();
699 // Parse statement
700 AssociatedStmt = ParseStatement();
701 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000702 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000704 Directive = Actions.ActOnOpenMPExecutableDirective(
705 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
706 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000707
708 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000709 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000710 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000711 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000712 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000713 case OMPD_declare_simd:
714 Diag(Tok, diag::err_omp_unexpected_directive)
715 << getOpenMPDirectiveName(DKind);
716 SkipUntil(tok::annot_pragma_openmp_end);
717 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000718 case OMPD_unknown:
719 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000720 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000721 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000722 }
723 return Directive;
724}
725
Alexey Bataeva769e072013-03-22 06:34:35 +0000726/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000727/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000728///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000729/// simple-variable-list:
730/// '(' id-expression {, id-expression} ')'
731///
732bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
733 SmallVectorImpl<Expr *> &VarList,
734 bool AllowScopeSpecifier) {
735 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000736 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000737 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000738 if (T.expectAndConsume(diag::err_expected_lparen_after,
739 getOpenMPDirectiveName(Kind)))
740 return true;
741 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000742 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000743
744 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000745 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000746 CXXScopeSpec SS;
747 SourceLocation TemplateKWLoc;
748 UnqualifiedId Name;
749 // Read var name.
750 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000751 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000752
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000754 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000755 IsCorrect = false;
756 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000757 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000758 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000760 IsCorrect = false;
761 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000762 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
764 Tok.isNot(tok::annot_pragma_openmp_end)) {
765 IsCorrect = false;
766 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000767 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000768 Diag(PrevTok.getLocation(), diag::err_expected)
769 << tok::identifier
770 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000771 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000772 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000773 ExprResult Res =
774 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000775 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000776 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000777 }
778 // Consume ','.
779 if (Tok.is(tok::comma)) {
780 ConsumeToken();
781 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000782 }
783
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000784 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000785 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000786 IsCorrect = false;
787 }
788
789 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000790 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000791
792 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000793}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000794
795/// \brief Parsing of OpenMP clauses.
796///
797/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000798/// if-clause | final-clause | num_threads-clause | safelen-clause |
799/// default-clause | private-clause | firstprivate-clause | shared-clause
800/// | linear-clause | aligned-clause | collapse-clause |
801/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000802/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000803/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000804/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000805/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000806/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000807/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808///
809OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
810 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000811 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000812 bool ErrorFound = false;
813 // Check if clause is allowed for the given directive.
814 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000815 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
816 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000817 ErrorFound = true;
818 }
819
820 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000821 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000822 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000823 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000824 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000825 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000826 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000827 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000828 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000829 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000830 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000831 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000832 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000833 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000834 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000835 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000836 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000837 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000838 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000839 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000840 // OpenMP [2.9.1, target data construct, Restrictions]
841 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000842 // OpenMP [2.11.1, task Construct, Restrictions]
843 // At most one if clause can appear on the directive.
844 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000845 // OpenMP [teams Construct, Restrictions]
846 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000847 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000848 // OpenMP [2.9.1, task Construct, Restrictions]
849 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000850 // OpenMP [2.9.2, taskloop Construct, Restrictions]
851 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000852 // OpenMP [2.9.2, taskloop Construct, Restrictions]
853 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000854 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000855 Diag(Tok, diag::err_omp_more_one_clause)
856 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000857 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000858 }
859
Alexey Bataev10e775f2015-07-30 11:36:16 +0000860 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
861 Clause = ParseOpenMPClause(CKind);
862 else
863 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000864 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000865 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000866 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000867 // OpenMP [2.14.3.1, Restrictions]
868 // Only a single default clause may be specified on a parallel, task or
869 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000870 // OpenMP [2.5, parallel Construct, Restrictions]
871 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000872 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000873 Diag(Tok, diag::err_omp_more_one_clause)
874 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000875 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000876 }
877
878 Clause = ParseOpenMPSimpleClause(CKind);
879 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000880 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000881 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000882 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000883 // OpenMP [2.7.1, Restrictions, p. 3]
884 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000885 // OpenMP [2.10.4, Restrictions, p. 106]
886 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000887 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000888 Diag(Tok, diag::err_omp_more_one_clause)
889 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000890 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000891 }
892
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000893 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000894 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
895 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000896 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000897 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000898 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000899 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000900 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000901 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000902 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000903 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000904 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000905 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000906 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000907 // OpenMP [2.7.1, Restrictions, p. 9]
908 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000909 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
910 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000911 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000912 Diag(Tok, diag::err_omp_more_one_clause)
913 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000914 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000915 }
916
917 Clause = ParseOpenMPClause(CKind);
918 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000919 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000920 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000921 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000922 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000923 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000924 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000925 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000926 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000927 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000928 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000929 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000930 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000931 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000932 break;
933 case OMPC_unknown:
934 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000935 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000936 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000937 break;
938 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000939 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
940 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000941 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000942 break;
943 }
Craig Topper161e4db2014-05-21 06:02:52 +0000944 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000945}
946
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000947/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000948/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000949/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000950///
Alexey Bataev3778b602014-07-17 07:32:53 +0000951/// final-clause:
952/// 'final' '(' expression ')'
953///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000954/// num_threads-clause:
955/// 'num_threads' '(' expression ')'
956///
957/// safelen-clause:
958/// 'safelen' '(' expression ')'
959///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000960/// simdlen-clause:
961/// 'simdlen' '(' expression ')'
962///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000963/// collapse-clause:
964/// 'collapse' '(' expression ')'
965///
Alexey Bataeva0569352015-12-01 10:17:31 +0000966/// priority-clause:
967/// 'priority' '(' expression ')'
968///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000969/// grainsize-clause:
970/// 'grainsize' '(' expression ')'
971///
Alexey Bataev382967a2015-12-08 12:06:20 +0000972/// num_tasks-clause:
973/// 'num_tasks' '(' expression ')'
974///
Alexey Bataev28c75412015-12-15 08:19:24 +0000975/// hint-clause:
976/// 'hint' '(' expression ')'
977///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000978OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
979 SourceLocation Loc = ConsumeToken();
980
981 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
982 if (T.expectAndConsume(diag::err_expected_lparen_after,
983 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000984 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000985
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000986 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000987 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
988 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000989 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000990
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000991 // Parse ')'.
992 T.consumeClose();
993
994 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000995 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000996
Alexey Bataeva55ed262014-05-28 06:15:33 +0000997 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000998 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000999}
1000
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001001/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001002///
1003/// default-clause:
1004/// 'default' '(' 'none' | 'shared' ')
1005///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001006/// proc_bind-clause:
1007/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1008///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001009OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1010 SourceLocation Loc = Tok.getLocation();
1011 SourceLocation LOpen = ConsumeToken();
1012 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001013 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001014 if (T.expectAndConsume(diag::err_expected_lparen_after,
1015 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001016 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017
Alexey Bataeva55ed262014-05-28 06:15:33 +00001018 unsigned Type = getOpenMPSimpleClauseType(
1019 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001020 SourceLocation TypeLoc = Tok.getLocation();
1021 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1022 Tok.isNot(tok::annot_pragma_openmp_end))
1023 ConsumeAnyToken();
1024
1025 // Parse ')'.
1026 T.consumeClose();
1027
1028 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1029 Tok.getLocation());
1030}
1031
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001032/// \brief Parsing of OpenMP clauses like 'ordered'.
1033///
1034/// ordered-clause:
1035/// 'ordered'
1036///
Alexey Bataev236070f2014-06-20 11:19:47 +00001037/// nowait-clause:
1038/// 'nowait'
1039///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001040/// untied-clause:
1041/// 'untied'
1042///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001043/// mergeable-clause:
1044/// 'mergeable'
1045///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001046/// read-clause:
1047/// 'read'
1048///
Alexey Bataev346265e2015-09-25 10:37:12 +00001049/// threads-clause:
1050/// 'threads'
1051///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001052/// simd-clause:
1053/// 'simd'
1054///
Alexey Bataevb825de12015-12-07 10:51:44 +00001055/// nogroup-clause:
1056/// 'nogroup'
1057///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001058OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1059 SourceLocation Loc = Tok.getLocation();
1060 ConsumeAnyToken();
1061
1062 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1063}
1064
1065
Alexey Bataev56dafe82014-06-20 07:16:17 +00001066/// \brief Parsing of OpenMP clauses with single expressions and some additional
1067/// argument like 'schedule' or 'dist_schedule'.
1068///
1069/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001070/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1071/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001072///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001073/// if-clause:
1074/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1075///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001076/// defaultmap:
1077/// 'defaultmap' '(' modifier ':' kind ')'
1078///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001079OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1080 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001081 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001082 // Parse '('.
1083 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1084 if (T.expectAndConsume(diag::err_expected_lparen_after,
1085 getOpenMPClauseName(Kind)))
1086 return nullptr;
1087
1088 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001089 SmallVector<unsigned, 4> Arg;
1090 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001091 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001092 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1093 Arg.resize(NumberOfElements);
1094 KLoc.resize(NumberOfElements);
1095 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1096 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1097 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1098 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001099 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001100 if (KindModifier > OMPC_SCHEDULE_unknown) {
1101 // Parse 'modifier'
1102 Arg[Modifier1] = KindModifier;
1103 KLoc[Modifier1] = Tok.getLocation();
1104 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1105 Tok.isNot(tok::annot_pragma_openmp_end))
1106 ConsumeAnyToken();
1107 if (Tok.is(tok::comma)) {
1108 // Parse ',' 'modifier'
1109 ConsumeAnyToken();
1110 KindModifier = getOpenMPSimpleClauseType(
1111 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1112 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1113 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001114 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001115 KLoc[Modifier2] = Tok.getLocation();
1116 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1117 Tok.isNot(tok::annot_pragma_openmp_end))
1118 ConsumeAnyToken();
1119 }
1120 // Parse ':'
1121 if (Tok.is(tok::colon))
1122 ConsumeAnyToken();
1123 else
1124 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1125 KindModifier = getOpenMPSimpleClauseType(
1126 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1127 }
1128 Arg[ScheduleKind] = KindModifier;
1129 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001130 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1131 Tok.isNot(tok::annot_pragma_openmp_end))
1132 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001133 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1134 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1135 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001136 Tok.is(tok::comma))
1137 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001138 } else if (Kind == OMPC_dist_schedule) {
1139 Arg.push_back(getOpenMPSimpleClauseType(
1140 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1141 KLoc.push_back(Tok.getLocation());
1142 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1143 Tok.isNot(tok::annot_pragma_openmp_end))
1144 ConsumeAnyToken();
1145 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1146 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001147 } else if (Kind == OMPC_defaultmap) {
1148 // Get a defaultmap modifier
1149 Arg.push_back(getOpenMPSimpleClauseType(
1150 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1151 KLoc.push_back(Tok.getLocation());
1152 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1153 Tok.isNot(tok::annot_pragma_openmp_end))
1154 ConsumeAnyToken();
1155 // Parse ':'
1156 if (Tok.is(tok::colon))
1157 ConsumeAnyToken();
1158 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1159 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1160 // Get a defaultmap kind
1161 Arg.push_back(getOpenMPSimpleClauseType(
1162 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1163 KLoc.push_back(Tok.getLocation());
1164 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1165 Tok.isNot(tok::annot_pragma_openmp_end))
1166 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001167 } else {
1168 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001169 KLoc.push_back(Tok.getLocation());
1170 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1171 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001172 ConsumeToken();
1173 if (Tok.is(tok::colon))
1174 DelimLoc = ConsumeToken();
1175 else
1176 Diag(Tok, diag::warn_pragma_expected_colon)
1177 << "directive name modifier";
1178 }
1179 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001180
Carlo Bertollib4adf552016-01-15 18:50:31 +00001181 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1182 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1183 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001184 if (NeedAnExpression) {
1185 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001186 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1187 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001188 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001189 }
1190
1191 // Parse ')'.
1192 T.consumeClose();
1193
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001194 if (NeedAnExpression && Val.isInvalid())
1195 return nullptr;
1196
Alexey Bataev56dafe82014-06-20 07:16:17 +00001197 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001198 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001199 T.getCloseLocation());
1200}
1201
Alexey Bataevc5e02582014-06-16 07:08:35 +00001202static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1203 UnqualifiedId &ReductionId) {
1204 SourceLocation TemplateKWLoc;
1205 if (ReductionIdScopeSpec.isEmpty()) {
1206 auto OOK = OO_None;
1207 switch (P.getCurToken().getKind()) {
1208 case tok::plus:
1209 OOK = OO_Plus;
1210 break;
1211 case tok::minus:
1212 OOK = OO_Minus;
1213 break;
1214 case tok::star:
1215 OOK = OO_Star;
1216 break;
1217 case tok::amp:
1218 OOK = OO_Amp;
1219 break;
1220 case tok::pipe:
1221 OOK = OO_Pipe;
1222 break;
1223 case tok::caret:
1224 OOK = OO_Caret;
1225 break;
1226 case tok::ampamp:
1227 OOK = OO_AmpAmp;
1228 break;
1229 case tok::pipepipe:
1230 OOK = OO_PipePipe;
1231 break;
1232 default:
1233 break;
1234 }
1235 if (OOK != OO_None) {
1236 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001237 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001238 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1239 return false;
1240 }
1241 }
1242 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1243 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001244 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001245 TemplateKWLoc, ReductionId);
1246}
1247
Alexander Musman1bb328c2014-06-04 13:06:39 +00001248/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001249/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001250///
1251/// private-clause:
1252/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001253/// firstprivate-clause:
1254/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001255/// lastprivate-clause:
1256/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001257/// shared-clause:
1258/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001259/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001260/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001261/// aligned-clause:
1262/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001263/// reduction-clause:
1264/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001265/// copyprivate-clause:
1266/// 'copyprivate' '(' list ')'
1267/// flush-clause:
1268/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001269/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001270/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001271/// map-clause:
1272/// 'map' '(' [ [ always , ]
1273/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001274///
Alexey Bataev182227b2015-08-20 10:54:39 +00001275/// For 'linear' clause linear-list may have the following forms:
1276/// list
1277/// modifier(list)
1278/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001279OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1280 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001281 SourceLocation Loc = Tok.getLocation();
1282 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001283 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001284 // Optional scope specifier and unqualified id for reduction identifier.
1285 CXXScopeSpec ReductionIdScopeSpec;
1286 UnqualifiedId ReductionId;
1287 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001288 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +00001289 // OpenMP 4.1 [2.15.3.7, linear Clause]
1290 // If no modifier is specified it is assumed to be val.
1291 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001292 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
1293 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +00001294 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001295 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001296 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001297
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001298 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001299 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001300 if (T.expectAndConsume(diag::err_expected_lparen_after,
1301 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001302 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001303
Alexey Bataev182227b2015-08-20 10:54:39 +00001304 bool NeedRParenForLinear = false;
1305 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1306 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001307 // Handle reduction-identifier for reduction clause.
1308 if (Kind == OMPC_reduction) {
1309 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001310 if (getLangOpts().CPlusPlus)
1311 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec,
1312 /*ObjectType=*/nullptr,
1313 /*EnteringContext=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001314 InvalidReductionId =
1315 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
1316 if (InvalidReductionId) {
1317 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1318 StopBeforeMatch);
1319 }
1320 if (Tok.is(tok::colon)) {
1321 ColonLoc = ConsumeToken();
1322 } else {
1323 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1324 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001325 } else if (Kind == OMPC_depend) {
1326 // Handle dependency type for depend clause.
1327 ColonProtectionRAIIObject ColonRAII(*this);
1328 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1329 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001330 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001331
1332 if (DepKind == OMPC_DEPEND_unknown) {
1333 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1334 StopBeforeMatch);
1335 } else {
1336 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +00001337 // Special processing for depend(source) clause.
1338 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
1339 // Parse ')'.
1340 T.consumeClose();
1341 return Actions.ActOnOpenMPVarListClause(
1342 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
1343 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
1344 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00001345 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1346 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +00001347 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001348 }
1349 if (Tok.is(tok::colon)) {
1350 ColonLoc = ConsumeToken();
1351 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00001352 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1353 : diag::warn_pragma_expected_colon)
1354 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001355 }
Alexey Bataev182227b2015-08-20 10:54:39 +00001356 } else if (Kind == OMPC_linear) {
1357 // Try to parse modifier if any.
1358 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +00001359 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +00001360 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001361 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +00001362 LinearT.consumeOpen();
1363 NeedRParenForLinear = true;
1364 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001365 } else if (Kind == OMPC_map) {
1366 // Handle map type for map clause.
1367 ColonProtectionRAIIObject ColonRAII(*this);
1368
Samuel Antaof91b1632016-02-27 00:01:58 +00001369 /// The map clause modifier token can be either a identifier or the C++
1370 /// delete keyword.
1371 auto IsMapClauseModifierToken = [](const Token &Tok) {
1372 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1373 };
1374
1375 // The first identifier may be a list item, a map-type or a
1376 // map-type-modifier. The map modifier can also be delete which has the same
1377 // spelling of the C++ delete keyword.
Kelvin Li0bff7af2015-11-23 05:32:03 +00001378 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001379 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001380 DepLinMapLoc = Tok.getLocation();
1381 bool ColonExpected = false;
1382
Samuel Antaof91b1632016-02-27 00:01:58 +00001383 if (IsMapClauseModifierToken(Tok)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00001384 if (PP.LookAhead(0).is(tok::colon)) {
1385 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001386 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001387 if (MapType == OMPC_MAP_unknown) {
1388 Diag(Tok, diag::err_omp_unknown_map_type);
1389 } else if (MapType == OMPC_MAP_always) {
1390 Diag(Tok, diag::err_omp_map_type_missing);
1391 }
1392 ConsumeToken();
1393 } else if (PP.LookAhead(0).is(tok::comma)) {
Samuel Antaof91b1632016-02-27 00:01:58 +00001394 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001395 PP.LookAhead(2).is(tok::colon)) {
1396 MapTypeModifier =
1397 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001398 Kind,
1399 IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001400 if (MapTypeModifier != OMPC_MAP_always) {
1401 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1402 MapTypeModifier = OMPC_MAP_unknown;
1403 } else {
1404 MapTypeModifierSpecified = true;
1405 }
1406
1407 ConsumeToken();
1408 ConsumeToken();
1409
1410 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001411 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001412 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1413 Diag(Tok, diag::err_omp_unknown_map_type);
1414 }
1415 ConsumeToken();
1416 } else {
1417 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001418 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001419 }
1420 } else {
1421 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001422 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001423 }
1424 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001425 MapType = OMPC_MAP_tofrom;
1426 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001427 }
1428
1429 if (Tok.is(tok::colon)) {
1430 ColonLoc = ConsumeToken();
1431 } else if (ColonExpected) {
1432 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1433 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001434 }
1435
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001436 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001437 bool IsComma =
1438 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1439 (Kind != OMPC_map)) ||
1440 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001441 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001442 (!MapTypeModifierSpecified ||
1443 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1444 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001445 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001446 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001447 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001448 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001449 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001450 ExprResult VarExpr =
1451 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataevc5970622016-04-01 08:43:42 +00001452 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001453 Vars.push_back(VarExpr.get());
Alexey Bataevc5970622016-04-01 08:43:42 +00001454 } else {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001455 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001456 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001457 }
1458 // Skip ',' if any
1459 IsComma = Tok.is(tok::comma);
Alexey Bataevc5970622016-04-01 08:43:42 +00001460 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001461 ConsumeToken();
Alexey Bataevc5970622016-04-01 08:43:42 +00001462 else if (Tok.isNot(tok::r_paren) &&
Alexander Musman8dba6642014-04-22 13:09:42 +00001463 Tok.isNot(tok::annot_pragma_openmp_end) &&
1464 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001465 Diag(Tok, diag::err_omp_expected_punc)
1466 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1467 : getOpenMPClauseName(Kind))
1468 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001469 }
1470
Alexey Bataev182227b2015-08-20 10:54:39 +00001471 // Parse ')' for linear clause with modifier.
1472 if (NeedRParenForLinear)
1473 LinearT.consumeClose();
1474
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001475 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001476 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001477 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1478 if (MustHaveTail) {
1479 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001480 SourceLocation ELoc = ConsumeToken();
1481 ExprResult Tail = ParseAssignmentExpression();
1482 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001483 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001484 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001485 else
1486 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1487 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001488 }
1489
1490 // Parse ')'.
1491 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001492 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001493 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1494 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001495 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001496 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001497
Alexey Bataevc5e02582014-06-16 07:08:35 +00001498 return Actions.ActOnOpenMPVarListClause(
1499 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1500 ReductionIdScopeSpec,
1501 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001502 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001503 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1504 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001505}
1506