blob: a280bb9e26724aa239ed48a8dddb24f9fd517f68 [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 Bataev20dfd772016-04-04 10:12:15 +0000332/// Parses clauses for 'declare simd' directive.
333/// clause:
334/// 'inbranch' | 'notinbranch'
335static void parseDeclareSimdClauses(Parser &P,
336 OMPDeclareSimdDeclAttr::BranchStateTy &BS) {
337 SourceRange BSRange;
338 const Token &Tok = P.getCurToken();
339 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
340 if (Tok.isNot(tok::identifier))
341 break;
342 OMPDeclareSimdDeclAttr::BranchStateTy Out;
343 StringRef TokName = Tok.getIdentifierInfo()->getName();
344 // Parse 'inranch|notinbranch' clauses.
345 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(TokName, Out)) {
346 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
347 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
348 << TokName << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS)
349 << BSRange;
350 }
351 BS = Out;
352 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
353 } else
354 // TODO: add parsing of other clauses.
355 break;
356 P.ConsumeToken();
357 // Skip ',' if any.
358 if (Tok.is(tok::comma))
359 P.ConsumeToken();
360 }
361}
362
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000363/// \brief Parsing of declarative OpenMP directives.
364///
365/// threadprivate-directive:
366/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000367/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +0000368///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000369/// declare-reduction-directive:
370/// annot_pragma_openmp 'declare' 'reduction' [...]
371/// annot_pragma_openmp_end
372///
Alexey Bataev587e1de2016-03-30 10:43:55 +0000373/// declare-simd-directive:
374/// annot_pragma_openmp 'declare simd' {<clause> [,]}
375/// annot_pragma_openmp_end
376/// <function declaration/definition>
377///
378Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
379 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
380 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000381 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000382 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000383
384 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000385 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000386 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000387
388 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000389 case OMPD_threadprivate:
390 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000391 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000392 // The last seen token is annot_pragma_openmp_end - need to check for
393 // extra tokens.
394 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
395 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000396 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000397 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000398 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000399 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000400 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000401 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000402 }
403 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000404 case OMPD_declare_reduction:
405 ConsumeToken();
406 if (auto Res = ParseOpenMPDeclareReductionDirective(AS)) {
407 // The last seen token is annot_pragma_openmp_end - need to check for
408 // extra tokens.
409 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
410 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
411 << getOpenMPDirectiveName(OMPD_declare_reduction);
412 while (Tok.isNot(tok::annot_pragma_openmp_end))
413 ConsumeAnyToken();
414 }
415 // Skip the last annot_pragma_openmp_end.
416 ConsumeToken();
417 return Res;
418 }
419 break;
Alexey Bataev587e1de2016-03-30 10:43:55 +0000420 case OMPD_declare_simd: {
421 // The syntax is:
422 // { #pragma omp declare simd }
423 // <function-declaration-or-definition>
424 //
425
426 ConsumeToken();
Alexey Bataev20dfd772016-04-04 10:12:15 +0000427 OMPDeclareSimdDeclAttr::BranchStateTy BS =
428 OMPDeclareSimdDeclAttr::BS_Undefined;
429 parseDeclareSimdClauses(*this, BS);
430
431 // Need to check for extra tokens.
Alexey Bataev587e1de2016-03-30 10:43:55 +0000432 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
433 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
434 << getOpenMPDirectiveName(OMPD_declare_simd);
435 while (Tok.isNot(tok::annot_pragma_openmp_end))
436 ConsumeAnyToken();
437 }
438 // Skip the last annot_pragma_openmp_end.
Alexey Bataev20dfd772016-04-04 10:12:15 +0000439 SourceLocation EndLoc = ConsumeToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +0000440
441 DeclGroupPtrTy Ptr;
Alexey Bataev20dfd772016-04-04 10:12:15 +0000442 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +0000443 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev20dfd772016-04-04 10:12:15 +0000444 else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +0000445 // Here we expect to see some function declaration.
446 if (AS == AS_none) {
447 assert(TagType == DeclSpec::TST_unspecified);
448 MaybeParseCXX11Attributes(Attrs);
449 MaybeParseMicrosoftAttributes(Attrs);
450 ParsingDeclSpec PDS(*this);
451 Ptr = ParseExternalDeclaration(Attrs, &PDS);
452 } else {
453 Ptr =
454 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
455 }
456 }
457 if (!Ptr) {
458 Diag(Loc, diag::err_omp_decl_in_declare_simd);
459 return DeclGroupPtrTy();
460 }
461
Alexey Bataev20dfd772016-04-04 10:12:15 +0000462 return Actions.ActOnOpenMPDeclareSimdDirective(Ptr, BS,
463 SourceRange(Loc, EndLoc));
Alexey Bataev587e1de2016-03-30 10:43:55 +0000464 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000465 case OMPD_declare_target: {
466 SourceLocation DTLoc = ConsumeAnyToken();
467 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
468 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
469 << getOpenMPDirectiveName(OMPD_declare_target);
470 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
471 }
472 // Skip the last annot_pragma_openmp_end.
473 ConsumeAnyToken();
474
475 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
476 return DeclGroupPtrTy();
477
478 DKind = ParseOpenMPDirectiveKind(*this);
479 while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
480 Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
481 ParsedAttributesWithRange attrs(AttrFactory);
482 MaybeParseCXX11Attributes(attrs);
483 MaybeParseMicrosoftAttributes(attrs);
484 ParseExternalDeclaration(attrs);
485 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
486 TentativeParsingAction TPA(*this);
487 ConsumeToken();
488 DKind = ParseOpenMPDirectiveKind(*this);
489 if (DKind != OMPD_end_declare_target)
490 TPA.Revert();
491 else
492 TPA.Commit();
493 }
494 }
495
496 if (DKind == OMPD_end_declare_target) {
497 ConsumeAnyToken();
498 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
499 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
500 << getOpenMPDirectiveName(OMPD_end_declare_target);
501 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
502 }
503 // Skip the last annot_pragma_openmp_end.
504 ConsumeAnyToken();
505 } else {
506 Diag(Tok, diag::err_expected_end_declare_target);
507 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
508 }
509 Actions.ActOnFinishOpenMPDeclareTargetDirective();
510 return DeclGroupPtrTy();
511 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000512 case OMPD_unknown:
513 Diag(Tok, diag::err_omp_unknown_directive);
514 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000515 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000516 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000517 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000518 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000519 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000520 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000521 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000522 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000523 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000524 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000525 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000526 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000527 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000528 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000529 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000530 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000531 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000532 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000533 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000534 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000535 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000536 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000537 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000538 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000539 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000540 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000541 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000542 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000543 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000544 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000545 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000546 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000547 case OMPD_end_declare_target:
Alexey Bataeva769e072013-03-22 06:34:35 +0000548 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000549 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000550 break;
551 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000552 while (Tok.isNot(tok::annot_pragma_openmp_end))
553 ConsumeAnyToken();
554 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +0000555 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000556}
557
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000558/// \brief Parsing of declarative or executable OpenMP directives.
559///
560/// threadprivate-directive:
561/// annot_pragma_openmp 'threadprivate' simple-variable-list
562/// annot_pragma_openmp_end
563///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000564/// declare-reduction-directive:
565/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
566/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
567/// ('omp_priv' '=' <expression>|<function_call>) ')']
568/// annot_pragma_openmp_end
569///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000570/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000571/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000572/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
573/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000574/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000575/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000576/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000577/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000578/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000579/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000580///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000581StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
582 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000583 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000584 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000585 SmallVector<Expr *, 5> Identifiers;
586 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000587 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000588 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000589 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000590 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000592 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000593 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 // Name of critical directive.
595 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000596 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000597 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000598 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000599
600 switch (DKind) {
601 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000602 if (Allowed != ACK_Any) {
603 Diag(Tok, diag::err_omp_immediate_directive)
604 << getOpenMPDirectiveName(DKind) << 0;
605 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000606 ConsumeToken();
607 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
608 // The last seen token is annot_pragma_openmp_end - need to check for
609 // extra tokens.
610 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
611 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000612 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000613 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000614 }
615 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000616 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000617 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
618 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000619 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000620 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000621 case OMPD_declare_reduction:
622 ConsumeToken();
623 if (auto Res = ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
624 // The last seen token is annot_pragma_openmp_end - need to check for
625 // extra tokens.
626 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
627 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
628 << getOpenMPDirectiveName(OMPD_declare_reduction);
629 while (Tok.isNot(tok::annot_pragma_openmp_end))
630 ConsumeAnyToken();
631 }
632 ConsumeAnyToken();
633 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
634 } else
635 SkipUntil(tok::annot_pragma_openmp_end);
636 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000637 case OMPD_flush:
638 if (PP.LookAhead(0).is(tok::l_paren)) {
639 FlushHasClause = true;
640 // Push copy of the current token back to stream to properly parse
641 // pseudo-clause OMPFlushClause.
642 PP.EnterToken(Tok);
643 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000644 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000645 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000646 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000647 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000648 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000649 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000650 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000651 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000652 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000653 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000654 }
655 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000656 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000657 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000658 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000659 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000660 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000661 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000662 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000663 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000664 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000665 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000666 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000667 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000668 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000669 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000670 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000671 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000672 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000673 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000674 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000675 case OMPD_target_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 Bataev0a6ed842015-12-03 09:40:15 +0000678 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000679 case OMPD_taskloop_simd:
680 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000681 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000682 // Parse directive name of the 'critical' directive if any.
683 if (DKind == OMPD_critical) {
684 BalancedDelimiterTracker T(*this, tok::l_paren,
685 tok::annot_pragma_openmp_end);
686 if (!T.consumeOpen()) {
687 if (Tok.isAnyIdentifier()) {
688 DirName =
689 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
690 ConsumeAnyToken();
691 } else {
692 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
693 }
694 T.consumeClose();
695 }
Alexey Bataev80909872015-07-02 11:25:17 +0000696 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000697 CancelRegion = ParseOpenMPDirectiveKind(*this);
698 if (Tok.isNot(tok::annot_pragma_openmp_end))
699 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000700 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701
Alexey Bataevf29276e2014-06-18 04:14:57 +0000702 if (isOpenMPLoopDirective(DKind))
703 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
704 if (isOpenMPSimdDirective(DKind))
705 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
706 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000707 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000708
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000709 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000710 OpenMPClauseKind CKind =
711 Tok.isAnnotation()
712 ? OMPC_unknown
713 : FlushHasClause ? OMPC_flush
714 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000715 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000716 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000717 OMPClause *Clause =
718 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719 FirstClauses[CKind].setInt(true);
720 if (Clause) {
721 FirstClauses[CKind].setPointer(Clause);
722 Clauses.push_back(Clause);
723 }
724
725 // Skip ',' if any.
726 if (Tok.is(tok::comma))
727 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000728 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000729 }
730 // End location of the directive.
731 EndLoc = Tok.getLocation();
732 // Consume final annot_pragma_openmp_end.
733 ConsumeToken();
734
Alexey Bataeveb482352015-12-18 05:05:56 +0000735 // OpenMP [2.13.8, ordered Construct, Syntax]
736 // If the depend clause is specified, the ordered construct is a stand-alone
737 // directive.
738 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000739 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000740 Diag(Loc, diag::err_omp_immediate_directive)
741 << getOpenMPDirectiveName(DKind) << 1
742 << getOpenMPClauseName(OMPC_depend);
743 }
744 HasAssociatedStatement = false;
745 }
746
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000747 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000748 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000749 // The body is a block scope like in Lambdas and Blocks.
750 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000751 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000752 Actions.ActOnStartOfCompoundStmt();
753 // Parse statement
754 AssociatedStmt = ParseStatement();
755 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000756 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000757 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000758 Directive = Actions.ActOnOpenMPExecutableDirective(
759 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
760 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000761
762 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000763 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000764 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000765 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000766 }
Alexey Bataev587e1de2016-03-30 10:43:55 +0000767 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000768 case OMPD_declare_target:
769 case OMPD_end_declare_target:
Alexey Bataev587e1de2016-03-30 10:43:55 +0000770 Diag(Tok, diag::err_omp_unexpected_directive)
771 << getOpenMPDirectiveName(DKind);
772 SkipUntil(tok::annot_pragma_openmp_end);
773 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000774 case OMPD_unknown:
775 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000776 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000777 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000778 }
779 return Directive;
780}
781
Alexey Bataeva769e072013-03-22 06:34:35 +0000782/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000783/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000784///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000785/// simple-variable-list:
786/// '(' id-expression {, id-expression} ')'
787///
788bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
789 SmallVectorImpl<Expr *> &VarList,
790 bool AllowScopeSpecifier) {
791 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000792 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000793 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000794 if (T.expectAndConsume(diag::err_expected_lparen_after,
795 getOpenMPDirectiveName(Kind)))
796 return true;
797 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000798 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000799
800 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000801 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000802 CXXScopeSpec SS;
803 SourceLocation TemplateKWLoc;
804 UnqualifiedId Name;
805 // Read var name.
806 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000807 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000808
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000810 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000811 IsCorrect = false;
812 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000813 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000814 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000815 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000816 IsCorrect = false;
817 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000818 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000819 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
820 Tok.isNot(tok::annot_pragma_openmp_end)) {
821 IsCorrect = false;
822 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000823 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000824 Diag(PrevTok.getLocation(), diag::err_expected)
825 << tok::identifier
826 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000827 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000828 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000829 ExprResult Res =
830 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000831 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000832 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000833 }
834 // Consume ','.
835 if (Tok.is(tok::comma)) {
836 ConsumeToken();
837 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000838 }
839
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000840 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000841 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000842 IsCorrect = false;
843 }
844
845 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000846 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000847
848 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000849}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000850
851/// \brief Parsing of OpenMP clauses.
852///
853/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000854/// if-clause | final-clause | num_threads-clause | safelen-clause |
855/// default-clause | private-clause | firstprivate-clause | shared-clause
856/// | linear-clause | aligned-clause | collapse-clause |
857/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000858/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000859/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000860/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000861/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000862/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000863/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000864///
865OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
866 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000867 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000868 bool ErrorFound = false;
869 // Check if clause is allowed for the given directive.
870 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000871 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
872 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000873 ErrorFound = true;
874 }
875
876 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000877 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000878 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000879 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000880 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000881 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000882 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000883 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000884 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000885 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000886 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000887 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000888 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000889 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000890 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000891 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000892 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000893 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000894 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000895 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000896 // OpenMP [2.9.1, target data construct, Restrictions]
897 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000898 // OpenMP [2.11.1, task Construct, Restrictions]
899 // At most one if clause can appear on the directive.
900 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000901 // OpenMP [teams Construct, Restrictions]
902 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000903 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000904 // OpenMP [2.9.1, task Construct, Restrictions]
905 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000906 // OpenMP [2.9.2, taskloop Construct, Restrictions]
907 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000908 // OpenMP [2.9.2, taskloop Construct, Restrictions]
909 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000910 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000911 Diag(Tok, diag::err_omp_more_one_clause)
912 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000913 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000914 }
915
Alexey Bataev10e775f2015-07-30 11:36:16 +0000916 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
917 Clause = ParseOpenMPClause(CKind);
918 else
919 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000920 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000921 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000922 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000923 // OpenMP [2.14.3.1, Restrictions]
924 // Only a single default clause may be specified on a parallel, task or
925 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000926 // OpenMP [2.5, parallel Construct, Restrictions]
927 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000928 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000929 Diag(Tok, diag::err_omp_more_one_clause)
930 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000931 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000932 }
933
934 Clause = ParseOpenMPSimpleClause(CKind);
935 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000936 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000937 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000938 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000939 // OpenMP [2.7.1, Restrictions, p. 3]
940 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000941 // OpenMP [2.10.4, Restrictions, p. 106]
942 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000943 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000944 Diag(Tok, diag::err_omp_more_one_clause)
945 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000946 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000947 }
948
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000949 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000950 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
951 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000952 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000953 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000954 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000955 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000956 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000957 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000958 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000959 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000960 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000961 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000962 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000963 // OpenMP [2.7.1, Restrictions, p. 9]
964 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000965 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
966 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000967 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000968 Diag(Tok, diag::err_omp_more_one_clause)
969 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000970 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000971 }
972
973 Clause = ParseOpenMPClause(CKind);
974 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000975 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000976 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000977 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000979 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000980 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000981 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000982 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000983 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000984 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000985 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000986 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000987 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000988 break;
989 case OMPC_unknown:
990 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000991 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000992 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000993 break;
994 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000995 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
996 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000997 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000998 break;
999 }
Craig Topper161e4db2014-05-21 06:02:52 +00001000 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001001}
1002
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001003/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00001004/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00001005/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001006///
Alexey Bataev3778b602014-07-17 07:32:53 +00001007/// final-clause:
1008/// 'final' '(' expression ')'
1009///
Alexey Bataev62c87d22014-03-21 04:51:18 +00001010/// num_threads-clause:
1011/// 'num_threads' '(' expression ')'
1012///
1013/// safelen-clause:
1014/// 'safelen' '(' expression ')'
1015///
Alexey Bataev66b15b52015-08-21 11:14:16 +00001016/// simdlen-clause:
1017/// 'simdlen' '(' expression ')'
1018///
Alexander Musman8bd31e62014-05-27 15:12:19 +00001019/// collapse-clause:
1020/// 'collapse' '(' expression ')'
1021///
Alexey Bataeva0569352015-12-01 10:17:31 +00001022/// priority-clause:
1023/// 'priority' '(' expression ')'
1024///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00001025/// grainsize-clause:
1026/// 'grainsize' '(' expression ')'
1027///
Alexey Bataev382967a2015-12-08 12:06:20 +00001028/// num_tasks-clause:
1029/// 'num_tasks' '(' expression ')'
1030///
Alexey Bataev28c75412015-12-15 08:19:24 +00001031/// hint-clause:
1032/// 'hint' '(' expression ')'
1033///
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001034OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
1035 SourceLocation Loc = ConsumeToken();
1036
1037 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1038 if (T.expectAndConsume(diag::err_expected_lparen_after,
1039 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001040 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001041
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001042 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001043 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1044 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001045 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001046
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001047 // Parse ')'.
1048 T.consumeClose();
1049
1050 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00001051 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001052
Alexey Bataeva55ed262014-05-28 06:15:33 +00001053 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001054 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001055}
1056
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001057/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001058///
1059/// default-clause:
1060/// 'default' '(' 'none' | 'shared' ')
1061///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001062/// proc_bind-clause:
1063/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1064///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001065OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
1066 SourceLocation Loc = Tok.getLocation();
1067 SourceLocation LOpen = ConsumeToken();
1068 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001069 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001070 if (T.expectAndConsume(diag::err_expected_lparen_after,
1071 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001072 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073
Alexey Bataeva55ed262014-05-28 06:15:33 +00001074 unsigned Type = getOpenMPSimpleClauseType(
1075 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 SourceLocation TypeLoc = Tok.getLocation();
1077 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1078 Tok.isNot(tok::annot_pragma_openmp_end))
1079 ConsumeAnyToken();
1080
1081 // Parse ')'.
1082 T.consumeClose();
1083
1084 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
1085 Tok.getLocation());
1086}
1087
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001088/// \brief Parsing of OpenMP clauses like 'ordered'.
1089///
1090/// ordered-clause:
1091/// 'ordered'
1092///
Alexey Bataev236070f2014-06-20 11:19:47 +00001093/// nowait-clause:
1094/// 'nowait'
1095///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00001096/// untied-clause:
1097/// 'untied'
1098///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00001099/// mergeable-clause:
1100/// 'mergeable'
1101///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00001102/// read-clause:
1103/// 'read'
1104///
Alexey Bataev346265e2015-09-25 10:37:12 +00001105/// threads-clause:
1106/// 'threads'
1107///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001108/// simd-clause:
1109/// 'simd'
1110///
Alexey Bataevb825de12015-12-07 10:51:44 +00001111/// nogroup-clause:
1112/// 'nogroup'
1113///
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001114OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
1115 SourceLocation Loc = Tok.getLocation();
1116 ConsumeAnyToken();
1117
1118 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
1119}
1120
1121
Alexey Bataev56dafe82014-06-20 07:16:17 +00001122/// \brief Parsing of OpenMP clauses with single expressions and some additional
1123/// argument like 'schedule' or 'dist_schedule'.
1124///
1125/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00001126/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
1127/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00001128///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001129/// if-clause:
1130/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
1131///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001132/// defaultmap:
1133/// 'defaultmap' '(' modifier ':' kind ')'
1134///
Alexey Bataev56dafe82014-06-20 07:16:17 +00001135OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
1136 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001137 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00001138 // Parse '('.
1139 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
1140 if (T.expectAndConsume(diag::err_expected_lparen_after,
1141 getOpenMPClauseName(Kind)))
1142 return nullptr;
1143
1144 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001145 SmallVector<unsigned, 4> Arg;
1146 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001147 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00001148 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
1149 Arg.resize(NumberOfElements);
1150 KLoc.resize(NumberOfElements);
1151 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
1152 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
1153 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
1154 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001155 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00001156 if (KindModifier > OMPC_SCHEDULE_unknown) {
1157 // Parse 'modifier'
1158 Arg[Modifier1] = KindModifier;
1159 KLoc[Modifier1] = Tok.getLocation();
1160 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1161 Tok.isNot(tok::annot_pragma_openmp_end))
1162 ConsumeAnyToken();
1163 if (Tok.is(tok::comma)) {
1164 // Parse ',' 'modifier'
1165 ConsumeAnyToken();
1166 KindModifier = getOpenMPSimpleClauseType(
1167 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1168 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
1169 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00001170 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001171 KLoc[Modifier2] = Tok.getLocation();
1172 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1173 Tok.isNot(tok::annot_pragma_openmp_end))
1174 ConsumeAnyToken();
1175 }
1176 // Parse ':'
1177 if (Tok.is(tok::colon))
1178 ConsumeAnyToken();
1179 else
1180 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
1181 KindModifier = getOpenMPSimpleClauseType(
1182 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
1183 }
1184 Arg[ScheduleKind] = KindModifier;
1185 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001186 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1187 Tok.isNot(tok::annot_pragma_openmp_end))
1188 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00001189 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
1190 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
1191 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001192 Tok.is(tok::comma))
1193 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00001194 } else if (Kind == OMPC_dist_schedule) {
1195 Arg.push_back(getOpenMPSimpleClauseType(
1196 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1197 KLoc.push_back(Tok.getLocation());
1198 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1199 Tok.isNot(tok::annot_pragma_openmp_end))
1200 ConsumeAnyToken();
1201 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
1202 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00001203 } else if (Kind == OMPC_defaultmap) {
1204 // Get a defaultmap modifier
1205 Arg.push_back(getOpenMPSimpleClauseType(
1206 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1207 KLoc.push_back(Tok.getLocation());
1208 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1209 Tok.isNot(tok::annot_pragma_openmp_end))
1210 ConsumeAnyToken();
1211 // Parse ':'
1212 if (Tok.is(tok::colon))
1213 ConsumeAnyToken();
1214 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
1215 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
1216 // Get a defaultmap kind
1217 Arg.push_back(getOpenMPSimpleClauseType(
1218 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
1219 KLoc.push_back(Tok.getLocation());
1220 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1221 Tok.isNot(tok::annot_pragma_openmp_end))
1222 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001223 } else {
1224 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00001225 KLoc.push_back(Tok.getLocation());
1226 Arg.push_back(ParseOpenMPDirectiveKind(*this));
1227 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001228 ConsumeToken();
1229 if (Tok.is(tok::colon))
1230 DelimLoc = ConsumeToken();
1231 else
1232 Diag(Tok, diag::warn_pragma_expected_colon)
1233 << "directive name modifier";
1234 }
1235 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00001236
Carlo Bertollib4adf552016-01-15 18:50:31 +00001237 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
1238 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
1239 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001240 if (NeedAnExpression) {
1241 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00001242 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
1243 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001244 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00001245 }
1246
1247 // Parse ')'.
1248 T.consumeClose();
1249
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001250 if (NeedAnExpression && Val.isInvalid())
1251 return nullptr;
1252
Alexey Bataev56dafe82014-06-20 07:16:17 +00001253 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001254 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00001255 T.getCloseLocation());
1256}
1257
Alexey Bataevc5e02582014-06-16 07:08:35 +00001258static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
1259 UnqualifiedId &ReductionId) {
1260 SourceLocation TemplateKWLoc;
1261 if (ReductionIdScopeSpec.isEmpty()) {
1262 auto OOK = OO_None;
1263 switch (P.getCurToken().getKind()) {
1264 case tok::plus:
1265 OOK = OO_Plus;
1266 break;
1267 case tok::minus:
1268 OOK = OO_Minus;
1269 break;
1270 case tok::star:
1271 OOK = OO_Star;
1272 break;
1273 case tok::amp:
1274 OOK = OO_Amp;
1275 break;
1276 case tok::pipe:
1277 OOK = OO_Pipe;
1278 break;
1279 case tok::caret:
1280 OOK = OO_Caret;
1281 break;
1282 case tok::ampamp:
1283 OOK = OO_AmpAmp;
1284 break;
1285 case tok::pipepipe:
1286 OOK = OO_PipePipe;
1287 break;
1288 default:
1289 break;
1290 }
1291 if (OOK != OO_None) {
1292 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00001293 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00001294 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
1295 return false;
1296 }
1297 }
1298 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
1299 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +00001300 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +00001301 TemplateKWLoc, ReductionId);
1302}
1303
Alexander Musman1bb328c2014-06-04 13:06:39 +00001304/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +00001305/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001306///
1307/// private-clause:
1308/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001309/// firstprivate-clause:
1310/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00001311/// lastprivate-clause:
1312/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00001313/// shared-clause:
1314/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00001315/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00001316/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001317/// aligned-clause:
1318/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00001319/// reduction-clause:
1320/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00001321/// copyprivate-clause:
1322/// 'copyprivate' '(' list ')'
1323/// flush-clause:
1324/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001325/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00001326/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00001327/// map-clause:
1328/// 'map' '(' [ [ always , ]
1329/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001330///
Alexey Bataev182227b2015-08-20 10:54:39 +00001331/// For 'linear' clause linear-list may have the following forms:
1332/// list
1333/// modifier(list)
1334/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00001335OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
1336 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001337 SourceLocation Loc = Tok.getLocation();
1338 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001339 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001340 // Optional scope specifier and unqualified id for reduction identifier.
1341 CXXScopeSpec ReductionIdScopeSpec;
1342 UnqualifiedId ReductionId;
1343 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001344 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +00001345 // OpenMP 4.1 [2.15.3.7, linear Clause]
1346 // If no modifier is specified it is assumed to be val.
1347 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001348 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
1349 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +00001350 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001351 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001352 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001353
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001354 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001355 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001356 if (T.expectAndConsume(diag::err_expected_lparen_after,
1357 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +00001358 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001359
Alexey Bataev182227b2015-08-20 10:54:39 +00001360 bool NeedRParenForLinear = false;
1361 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
1362 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001363 // Handle reduction-identifier for reduction clause.
1364 if (Kind == OMPC_reduction) {
1365 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001366 if (getLangOpts().CPlusPlus)
1367 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec,
1368 /*ObjectType=*/nullptr,
1369 /*EnteringContext=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001370 InvalidReductionId =
1371 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
1372 if (InvalidReductionId) {
1373 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1374 StopBeforeMatch);
1375 }
1376 if (Tok.is(tok::colon)) {
1377 ColonLoc = ConsumeToken();
1378 } else {
1379 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
1380 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001381 } else if (Kind == OMPC_depend) {
1382 // Handle dependency type for depend clause.
1383 ColonProtectionRAIIObject ColonRAII(*this);
1384 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
1385 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001386 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001387
1388 if (DepKind == OMPC_DEPEND_unknown) {
1389 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
1390 StopBeforeMatch);
1391 } else {
1392 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +00001393 // Special processing for depend(source) clause.
1394 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
1395 // Parse ')'.
1396 T.consumeClose();
1397 return Actions.ActOnOpenMPVarListClause(
1398 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
1399 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
1400 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00001401 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1402 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +00001403 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001404 }
1405 if (Tok.is(tok::colon)) {
1406 ColonLoc = ConsumeToken();
1407 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00001408 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
1409 : diag::warn_pragma_expected_colon)
1410 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001411 }
Alexey Bataev182227b2015-08-20 10:54:39 +00001412 } else if (Kind == OMPC_linear) {
1413 // Try to parse modifier if any.
1414 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +00001415 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +00001416 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001417 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +00001418 LinearT.consumeOpen();
1419 NeedRParenForLinear = true;
1420 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001421 } else if (Kind == OMPC_map) {
1422 // Handle map type for map clause.
1423 ColonProtectionRAIIObject ColonRAII(*this);
1424
Samuel Antaof91b1632016-02-27 00:01:58 +00001425 /// The map clause modifier token can be either a identifier or the C++
1426 /// delete keyword.
1427 auto IsMapClauseModifierToken = [](const Token &Tok) {
1428 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1429 };
1430
1431 // The first identifier may be a list item, a map-type or a
1432 // map-type-modifier. The map modifier can also be delete which has the same
1433 // spelling of the C++ delete keyword.
Kelvin Li0bff7af2015-11-23 05:32:03 +00001434 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001435 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001436 DepLinMapLoc = Tok.getLocation();
1437 bool ColonExpected = false;
1438
Samuel Antaof91b1632016-02-27 00:01:58 +00001439 if (IsMapClauseModifierToken(Tok)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00001440 if (PP.LookAhead(0).is(tok::colon)) {
1441 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001442 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001443 if (MapType == OMPC_MAP_unknown) {
1444 Diag(Tok, diag::err_omp_unknown_map_type);
1445 } else if (MapType == OMPC_MAP_always) {
1446 Diag(Tok, diag::err_omp_map_type_missing);
1447 }
1448 ConsumeToken();
1449 } else if (PP.LookAhead(0).is(tok::comma)) {
Samuel Antaof91b1632016-02-27 00:01:58 +00001450 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001451 PP.LookAhead(2).is(tok::colon)) {
1452 MapTypeModifier =
1453 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001454 Kind,
1455 IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001456 if (MapTypeModifier != OMPC_MAP_always) {
1457 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1458 MapTypeModifier = OMPC_MAP_unknown;
1459 } else {
1460 MapTypeModifierSpecified = true;
1461 }
1462
1463 ConsumeToken();
1464 ConsumeToken();
1465
1466 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001467 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001468 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1469 Diag(Tok, diag::err_omp_unknown_map_type);
1470 }
1471 ConsumeToken();
1472 } else {
1473 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001474 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001475 }
1476 } else {
1477 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001478 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001479 }
1480 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001481 MapType = OMPC_MAP_tofrom;
1482 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001483 }
1484
1485 if (Tok.is(tok::colon)) {
1486 ColonLoc = ConsumeToken();
1487 } else if (ColonExpected) {
1488 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1489 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001490 }
1491
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001492 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001493 bool IsComma =
1494 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1495 (Kind != OMPC_map)) ||
1496 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001497 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001498 (!MapTypeModifierSpecified ||
1499 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1500 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001501 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001502 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001503 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001504 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001505 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001506 ExprResult VarExpr =
1507 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataevc5970622016-04-01 08:43:42 +00001508 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001509 Vars.push_back(VarExpr.get());
Alexey Bataevc5970622016-04-01 08:43:42 +00001510 } else {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001511 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001512 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001513 }
1514 // Skip ',' if any
1515 IsComma = Tok.is(tok::comma);
Alexey Bataevc5970622016-04-01 08:43:42 +00001516 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001517 ConsumeToken();
Alexey Bataevc5970622016-04-01 08:43:42 +00001518 else if (Tok.isNot(tok::r_paren) &&
Alexander Musman8dba6642014-04-22 13:09:42 +00001519 Tok.isNot(tok::annot_pragma_openmp_end) &&
1520 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001521 Diag(Tok, diag::err_omp_expected_punc)
1522 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1523 : getOpenMPClauseName(Kind))
1524 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001525 }
1526
Alexey Bataev182227b2015-08-20 10:54:39 +00001527 // Parse ')' for linear clause with modifier.
1528 if (NeedRParenForLinear)
1529 LinearT.consumeClose();
1530
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001531 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001532 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001533 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1534 if (MustHaveTail) {
1535 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001536 SourceLocation ELoc = ConsumeToken();
1537 ExprResult Tail = ParseAssignmentExpression();
1538 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001539 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001540 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001541 else
1542 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1543 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001544 }
1545
1546 // Parse ')'.
1547 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001548 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001549 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1550 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001551 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001552 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001553
Alexey Bataevc5e02582014-06-16 07:08:35 +00001554 return Actions.ActOnOpenMPVarListClause(
1555 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1556 ReductionIdScopeSpec,
1557 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001558 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001559 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1560 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001561}
1562