blob: aaef4cd36d3bd9eae47675f5549a061aff5ddb36 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements parsing of all OpenMP directives and clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000010///
11//===----------------------------------------------------------------------===//
12
Alexey Bataev9959db52014-05-06 10:08:46 +000013#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000014#include "clang/AST/StmtOpenMP.h"
Alexey Bataev93dc40d2019-12-20 11:04:57 -050015#include "clang/Basic/OpenMPKinds.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Sema/Scope.h"
20#include "llvm/ADT/PointerIntPair.h"
Alexey Bataev4513e93f2019-10-10 15:15:26 +000021#include "llvm/ADT/UniqueVector.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060024using namespace llvm::omp;
Alexey Bataeva769e072013-03-22 06:34:35 +000025
26//===----------------------------------------------------------------------===//
27// OpenMP declarative directives.
28//===----------------------------------------------------------------------===//
29
Dmitry Polukhin82478332016-02-13 06:53:38 +000030namespace {
31enum OpenMPDirectiveKindEx {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060032 OMPD_cancellation = unsigned(OMPD_unknown) + 1,
Dmitry Polukhin82478332016-02-13 06:53:38 +000033 OMPD_data,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000034 OMPD_declare,
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000035 OMPD_end,
36 OMPD_end_declare,
Dmitry Polukhin82478332016-02-13 06:53:38 +000037 OMPD_enter,
38 OMPD_exit,
39 OMPD_point,
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000040 OMPD_reduction,
Dmitry Polukhin82478332016-02-13 06:53:38 +000041 OMPD_target_enter,
Samuel Antao686c70c2016-05-26 17:30:50 +000042 OMPD_target_exit,
43 OMPD_update,
Kelvin Li579e41c2016-11-30 23:51:03 +000044 OMPD_distribute_parallel,
Kelvin Li80e8f562016-12-29 22:16:30 +000045 OMPD_teams_distribute_parallel,
Michael Kruse251e1482019-02-01 20:25:04 +000046 OMPD_target_teams_distribute_parallel,
47 OMPD_mapper,
Alexey Bataevd158cf62019-09-13 20:18:17 +000048 OMPD_variant,
Dmitry Polukhin82478332016-02-13 06:53:38 +000049};
Dmitry Polukhind69b5052016-05-09 14:59:13 +000050
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060051// Helper to unify the enum class OpenMPDirectiveKind with its extension
52// the OpenMPDirectiveKindEx enum which allows to use them together as if they
53// are unsigned values.
54struct OpenMPDirectiveKindExWrapper {
55 OpenMPDirectiveKindExWrapper(unsigned Value) : Value(Value) {}
56 OpenMPDirectiveKindExWrapper(OpenMPDirectiveKind DK) : Value(unsigned(DK)) {}
57 bool operator==(OpenMPDirectiveKind V) const { return Value == unsigned(V); }
58 bool operator!=(OpenMPDirectiveKind V) const { return Value != unsigned(V); }
59 bool operator<(OpenMPDirectiveKind V) const { return Value < unsigned(V); }
60 operator unsigned() const { return Value; }
61 operator OpenMPDirectiveKind() const { return OpenMPDirectiveKind(Value); }
62 unsigned Value;
63};
64
Alexey Bataev25ed0c02019-03-07 17:54:44 +000065class DeclDirectiveListParserHelper final {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000066 SmallVector<Expr *, 4> Identifiers;
67 Parser *P;
Alexey Bataev25ed0c02019-03-07 17:54:44 +000068 OpenMPDirectiveKind Kind;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000069
70public:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000071 DeclDirectiveListParserHelper(Parser *P, OpenMPDirectiveKind Kind)
72 : P(P), Kind(Kind) {}
Dmitry Polukhind69b5052016-05-09 14:59:13 +000073 void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
Alexey Bataev25ed0c02019-03-07 17:54:44 +000074 ExprResult Res = P->getActions().ActOnOpenMPIdExpression(
75 P->getCurScope(), SS, NameInfo, Kind);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000076 if (Res.isUsable())
77 Identifiers.push_back(Res.get());
78 }
79 llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
80};
Dmitry Polukhin82478332016-02-13 06:53:38 +000081} // namespace
82
83// Map token string to extended OMP token kind that are
84// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
85static unsigned getOpenMPDirectiveKindEx(StringRef S) {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060086 OpenMPDirectiveKindExWrapper DKind = getOpenMPDirectiveKind(S);
Dmitry Polukhin82478332016-02-13 06:53:38 +000087 if (DKind != OMPD_unknown)
88 return DKind;
89
Johannes Doerferteb3e81f2019-11-04 22:00:49 -060090 return llvm::StringSwitch<OpenMPDirectiveKindExWrapper>(S)
Dmitry Polukhin82478332016-02-13 06:53:38 +000091 .Case("cancellation", OMPD_cancellation)
92 .Case("data", OMPD_data)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000093 .Case("declare", OMPD_declare)
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000094 .Case("end", OMPD_end)
Dmitry Polukhin82478332016-02-13 06:53:38 +000095 .Case("enter", OMPD_enter)
96 .Case("exit", OMPD_exit)
97 .Case("point", OMPD_point)
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000098 .Case("reduction", OMPD_reduction)
Samuel Antao686c70c2016-05-26 17:30:50 +000099 .Case("update", OMPD_update)
Michael Kruse251e1482019-02-01 20:25:04 +0000100 .Case("mapper", OMPD_mapper)
Alexey Bataevd158cf62019-09-13 20:18:17 +0000101 .Case("variant", OMPD_variant)
Dmitry Polukhin82478332016-02-13 06:53:38 +0000102 .Default(OMPD_unknown);
103}
104
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600105static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +0000106 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
107 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
108 // TODO: add other combined directives in topological order.
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600109 static const OpenMPDirectiveKindExWrapper F[][3] = {
Alexey Bataev61908f652018-04-23 19:53:05 +0000110 {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
111 {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
Michael Kruse251e1482019-02-01 20:25:04 +0000112 {OMPD_declare, OMPD_mapper, OMPD_declare_mapper},
Alexey Bataev61908f652018-04-23 19:53:05 +0000113 {OMPD_declare, OMPD_simd, OMPD_declare_simd},
114 {OMPD_declare, OMPD_target, OMPD_declare_target},
Alexey Bataevd158cf62019-09-13 20:18:17 +0000115 {OMPD_declare, OMPD_variant, OMPD_declare_variant},
Alexey Bataev61908f652018-04-23 19:53:05 +0000116 {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
117 {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
118 {OMPD_distribute_parallel_for, OMPD_simd,
119 OMPD_distribute_parallel_for_simd},
120 {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
121 {OMPD_end, OMPD_declare, OMPD_end_declare},
122 {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
123 {OMPD_target, OMPD_data, OMPD_target_data},
124 {OMPD_target, OMPD_enter, OMPD_target_enter},
125 {OMPD_target, OMPD_exit, OMPD_target_exit},
126 {OMPD_target, OMPD_update, OMPD_target_update},
127 {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
128 {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
129 {OMPD_for, OMPD_simd, OMPD_for_simd},
130 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
131 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
132 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
133 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
134 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
135 {OMPD_target, OMPD_simd, OMPD_target_simd},
136 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
137 {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
138 {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
139 {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
140 {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
141 {OMPD_teams_distribute_parallel, OMPD_for,
142 OMPD_teams_distribute_parallel_for},
143 {OMPD_teams_distribute_parallel_for, OMPD_simd,
144 OMPD_teams_distribute_parallel_for_simd},
145 {OMPD_target, OMPD_teams, OMPD_target_teams},
146 {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
147 {OMPD_target_teams_distribute, OMPD_parallel,
148 OMPD_target_teams_distribute_parallel},
149 {OMPD_target_teams_distribute, OMPD_simd,
150 OMPD_target_teams_distribute_simd},
151 {OMPD_target_teams_distribute_parallel, OMPD_for,
152 OMPD_target_teams_distribute_parallel_for},
153 {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
Alexey Bataev60e51c42019-10-10 20:13:02 +0000154 OMPD_target_teams_distribute_parallel_for_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000155 {OMPD_master, OMPD_taskloop, OMPD_master_taskloop},
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000156 {OMPD_master_taskloop, OMPD_simd, OMPD_master_taskloop_simd},
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000157 {OMPD_parallel, OMPD_master, OMPD_parallel_master},
Alexey Bataev14a388f2019-10-25 10:27:13 -0400158 {OMPD_parallel_master, OMPD_taskloop, OMPD_parallel_master_taskloop},
159 {OMPD_parallel_master_taskloop, OMPD_simd,
160 OMPD_parallel_master_taskloop_simd}};
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000161 enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
Alexey Bataev61908f652018-04-23 19:53:05 +0000162 Token Tok = P.getCurToken();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600163 OpenMPDirectiveKindExWrapper DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +0000164 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +0000165 ? static_cast<unsigned>(OMPD_unknown)
166 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
167 if (DKind == OMPD_unknown)
168 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +0000169
Alexey Bataev61908f652018-04-23 19:53:05 +0000170 for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
171 if (DKind != F[I][0])
Dmitry Polukhin82478332016-02-13 06:53:38 +0000172 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000173
Dmitry Polukhin82478332016-02-13 06:53:38 +0000174 Tok = P.getPreprocessor().LookAhead(0);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600175 OpenMPDirectiveKindExWrapper SDKind =
Dmitry Polukhin82478332016-02-13 06:53:38 +0000176 Tok.isAnnotation()
177 ? static_cast<unsigned>(OMPD_unknown)
178 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
179 if (SDKind == OMPD_unknown)
180 continue;
Michael Wong65f367f2015-07-21 13:44:28 +0000181
Alexey Bataev61908f652018-04-23 19:53:05 +0000182 if (SDKind == F[I][1]) {
Dmitry Polukhin82478332016-02-13 06:53:38 +0000183 P.ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000184 DKind = F[I][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +0000185 }
186 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000187 return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
188 : OMPD_unknown;
189}
190
191static DeclarationName parseOpenMPReductionId(Parser &P) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000192 Token Tok = P.getCurToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000193 Sema &Actions = P.getActions();
194 OverloadedOperatorKind OOK = OO_None;
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000195 // Allow to use 'operator' keyword for C++ operators
196 bool WithOperator = false;
197 if (Tok.is(tok::kw_operator)) {
198 P.ConsumeToken();
199 Tok = P.getCurToken();
200 WithOperator = true;
201 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000202 switch (Tok.getKind()) {
203 case tok::plus: // '+'
204 OOK = OO_Plus;
205 break;
206 case tok::minus: // '-'
207 OOK = OO_Minus;
208 break;
209 case tok::star: // '*'
210 OOK = OO_Star;
211 break;
212 case tok::amp: // '&'
213 OOK = OO_Amp;
214 break;
215 case tok::pipe: // '|'
216 OOK = OO_Pipe;
217 break;
218 case tok::caret: // '^'
219 OOK = OO_Caret;
220 break;
221 case tok::ampamp: // '&&'
222 OOK = OO_AmpAmp;
223 break;
224 case tok::pipepipe: // '||'
225 OOK = OO_PipePipe;
226 break;
227 case tok::identifier: // identifier
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000228 if (!WithOperator)
229 break;
Galina Kistanova474f2ce2017-06-01 21:26:38 +0000230 LLVM_FALLTHROUGH;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000231 default:
232 P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
233 P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
234 Parser::StopBeforeMatch);
235 return DeclarationName();
236 }
237 P.ConsumeToken();
238 auto &DeclNames = Actions.getASTContext().DeclarationNames;
239 return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
240 : DeclNames.getCXXOperatorName(OOK);
241}
242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000243/// Parse 'omp declare reduction' construct.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000244///
245/// declare-reduction-directive:
246/// annot_pragma_openmp 'declare' 'reduction'
247/// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
248/// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
249/// annot_pragma_openmp_end
250/// <reduction_id> is either a base language identifier or one of the following
251/// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
252///
253Parser::DeclGroupPtrTy
254Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
255 // Parse '('.
256 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600257 if (T.expectAndConsume(
258 diag::err_expected_lparen_after,
259 getOpenMPDirectiveName(OMPD_declare_reduction).data())) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000260 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
261 return DeclGroupPtrTy();
262 }
263
264 DeclarationName Name = parseOpenMPReductionId(*this);
265 if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
266 return DeclGroupPtrTy();
267
268 // Consume ':'.
269 bool IsCorrect = !ExpectAndConsume(tok::colon);
270
271 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
272 return DeclGroupPtrTy();
273
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000274 IsCorrect = IsCorrect && !Name.isEmpty();
275
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000276 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
277 Diag(Tok.getLocation(), diag::err_expected_type);
278 IsCorrect = false;
279 }
280
281 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
282 return DeclGroupPtrTy();
283
284 SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
285 // Parse list of types until ':' token.
286 do {
287 ColonProtectionRAIIObject ColonRAII(*this);
288 SourceRange Range;
Faisal Vali421b2d12017-12-29 05:41:00 +0000289 TypeResult TR =
290 ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000291 if (TR.isUsable()) {
Alexey Bataev61908f652018-04-23 19:53:05 +0000292 QualType ReductionType =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000293 Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
294 if (!ReductionType.isNull()) {
295 ReductionTypes.push_back(
296 std::make_pair(ReductionType, Range.getBegin()));
297 }
298 } else {
299 SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
300 StopBeforeMatch);
301 }
302
303 if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
304 break;
305
306 // Consume ','.
307 if (ExpectAndConsume(tok::comma)) {
308 IsCorrect = false;
309 if (Tok.is(tok::annot_pragma_openmp_end)) {
310 Diag(Tok.getLocation(), diag::err_expected_type);
311 return DeclGroupPtrTy();
312 }
313 }
314 } while (Tok.isNot(tok::annot_pragma_openmp_end));
315
316 if (ReductionTypes.empty()) {
317 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
318 return DeclGroupPtrTy();
319 }
320
321 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
322 return DeclGroupPtrTy();
323
324 // Consume ':'.
325 if (ExpectAndConsume(tok::colon))
326 IsCorrect = false;
327
328 if (Tok.is(tok::annot_pragma_openmp_end)) {
329 Diag(Tok.getLocation(), diag::err_expected_expression);
330 return DeclGroupPtrTy();
331 }
332
333 DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
334 getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
335
336 // Parse <combiner> expression and then parse initializer if any for each
337 // correct type.
338 unsigned I = 0, E = ReductionTypes.size();
Alexey Bataev61908f652018-04-23 19:53:05 +0000339 for (Decl *D : DRD.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000340 TentativeParsingAction TPA(*this);
341 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000342 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000343 Scope::OpenMPDirectiveScope);
344 // Parse <combiner> expression.
345 Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
346 ExprResult CombinerResult =
347 Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000348 D->getLocation(), /*DiscardedValue*/ false);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000349 Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
350
351 if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
352 Tok.isNot(tok::annot_pragma_openmp_end)) {
353 TPA.Commit();
354 IsCorrect = false;
355 break;
356 }
357 IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
358 ExprResult InitializerResult;
359 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
360 // Parse <initializer> expression.
361 if (Tok.is(tok::identifier) &&
Alexey Bataev61908f652018-04-23 19:53:05 +0000362 Tok.getIdentifierInfo()->isStr("initializer")) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000363 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000364 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000365 Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
366 TPA.Commit();
367 IsCorrect = false;
368 break;
369 }
370 // Parse '('.
371 BalancedDelimiterTracker T(*this, tok::l_paren,
372 tok::annot_pragma_openmp_end);
373 IsCorrect =
374 !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
375 IsCorrect;
376 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
377 ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
Momchil Velikov57c681f2017-08-10 15:43:06 +0000378 Scope::CompoundStmtScope |
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000379 Scope::OpenMPDirectiveScope);
380 // Parse expression.
Alexey Bataev070f43a2017-09-06 14:49:58 +0000381 VarDecl *OmpPrivParm =
382 Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
383 D);
384 // Check if initializer is omp_priv <init_expr> or something else.
385 if (Tok.is(tok::identifier) &&
386 Tok.getIdentifierInfo()->isStr("omp_priv")) {
Alexey Bataev3c676e32019-11-12 11:19:26 -0500387 ConsumeToken();
388 ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000389 } else {
390 InitializerResult = Actions.ActOnFinishFullExpr(
391 ParseAssignmentExpression().get(), D->getLocation(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000392 /*DiscardedValue*/ false);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000393 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000394 Actions.ActOnOpenMPDeclareReductionInitializerEnd(
Alexey Bataev070f43a2017-09-06 14:49:58 +0000395 D, InitializerResult.get(), OmpPrivParm);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000396 if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
397 Tok.isNot(tok::annot_pragma_openmp_end)) {
398 TPA.Commit();
399 IsCorrect = false;
400 break;
401 }
402 IsCorrect =
403 !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
404 }
405 }
406
407 ++I;
408 // Revert parsing if not the last type, otherwise accept it, we're done with
409 // parsing.
410 if (I != E)
411 TPA.Revert();
412 else
413 TPA.Commit();
414 }
415 return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
416 IsCorrect);
Alexey Bataev4acb8592014-07-07 13:01:15 +0000417}
418
Alexey Bataev070f43a2017-09-06 14:49:58 +0000419void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
420 // Parse declarator '=' initializer.
421 // If a '==' or '+=' is found, suggest a fixit to '='.
422 if (isTokenEqualOrEqualTypo()) {
423 ConsumeToken();
424
425 if (Tok.is(tok::code_completion)) {
426 Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
427 Actions.FinalizeDeclaration(OmpPrivParm);
428 cutOffParsing();
429 return;
430 }
431
Alexey Bataev3c676e32019-11-12 11:19:26 -0500432 PreferredType.enterVariableInit(Tok.getLocation(), OmpPrivParm);
Alexey Bataev1fcc9b62020-01-02 15:49:08 -0500433 ExprResult Init = ParseInitializer();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000434
435 if (Init.isInvalid()) {
436 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
437 Actions.ActOnInitializerError(OmpPrivParm);
438 } else {
439 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
440 /*DirectInit=*/false);
441 }
442 } else if (Tok.is(tok::l_paren)) {
443 // Parse C++ direct initializer: '(' expression-list ')'
444 BalancedDelimiterTracker T(*this, tok::l_paren);
445 T.consumeOpen();
446
447 ExprVector Exprs;
448 CommaLocsTy CommaLocs;
449
Ilya Biryukov2fab2352018-08-30 13:08:03 +0000450 SourceLocation LParLoc = T.getOpenLocation();
Ilya Biryukovff2a9972019-02-26 11:01:50 +0000451 auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() {
452 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
453 getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
454 OmpPrivParm->getLocation(), Exprs, LParLoc);
455 CalledSignatureHelp = true;
456 return PreferredType;
457 };
458 if (ParseExpressionList(Exprs, CommaLocs, [&] {
459 PreferredType.enterFunctionArgument(Tok.getLocation(),
460 RunSignatureHelp);
461 })) {
462 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
463 RunSignatureHelp();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000464 Actions.ActOnInitializerError(OmpPrivParm);
465 SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
466 } else {
467 // Match the ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000468 SourceLocation RLoc = Tok.getLocation();
469 if (!T.consumeClose())
470 RLoc = T.getCloseLocation();
Alexey Bataev070f43a2017-09-06 14:49:58 +0000471
472 assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
473 "Unexpected number of commas!");
474
Alexey Bataevdbc72c92018-07-06 19:35:42 +0000475 ExprResult Initializer =
476 Actions.ActOnParenListExpr(T.getOpenLocation(), RLoc, Exprs);
Alexey Bataev070f43a2017-09-06 14:49:58 +0000477 Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
478 /*DirectInit=*/true);
479 }
480 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
481 // Parse C++0x braced-init-list.
482 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
483
484 ExprResult Init(ParseBraceInitializer());
485
486 if (Init.isInvalid()) {
487 Actions.ActOnInitializerError(OmpPrivParm);
488 } else {
489 Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
490 /*DirectInit=*/true);
491 }
492 } else {
493 Actions.ActOnUninitializedDecl(OmpPrivParm);
494 }
495}
496
Michael Kruse251e1482019-02-01 20:25:04 +0000497/// Parses 'omp declare mapper' directive.
498///
499/// declare-mapper-directive:
500/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifier> ':']
501/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
502/// annot_pragma_openmp_end
503/// <mapper-identifier> and <var> are base language identifiers.
504///
505Parser::DeclGroupPtrTy
506Parser::ParseOpenMPDeclareMapperDirective(AccessSpecifier AS) {
507 bool IsCorrect = true;
508 // Parse '('
509 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
510 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -0600511 getOpenMPDirectiveName(OMPD_declare_mapper).data())) {
Michael Kruse251e1482019-02-01 20:25:04 +0000512 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
513 return DeclGroupPtrTy();
514 }
515
516 // Parse <mapper-identifier>
517 auto &DeclNames = Actions.getASTContext().DeclarationNames;
518 DeclarationName MapperId;
519 if (PP.LookAhead(0).is(tok::colon)) {
520 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
521 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
522 IsCorrect = false;
523 } else {
524 MapperId = DeclNames.getIdentifier(Tok.getIdentifierInfo());
525 }
526 ConsumeToken();
527 // Consume ':'.
528 ExpectAndConsume(tok::colon);
529 } else {
530 // If no mapper identifier is provided, its name is "default" by default
531 MapperId =
532 DeclNames.getIdentifier(&Actions.getASTContext().Idents.get("default"));
533 }
534
535 if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
536 return DeclGroupPtrTy();
537
538 // Parse <type> <var>
539 DeclarationName VName;
540 QualType MapperType;
541 SourceRange Range;
542 TypeResult ParsedType = parseOpenMPDeclareMapperVarDecl(Range, VName, AS);
543 if (ParsedType.isUsable())
544 MapperType =
545 Actions.ActOnOpenMPDeclareMapperType(Range.getBegin(), ParsedType);
546 if (MapperType.isNull())
547 IsCorrect = false;
548 if (!IsCorrect) {
549 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
550 return DeclGroupPtrTy();
551 }
552
553 // Consume ')'.
554 IsCorrect &= !T.consumeClose();
555 if (!IsCorrect) {
556 SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch);
557 return DeclGroupPtrTy();
558 }
559
560 // Enter scope.
561 OMPDeclareMapperDecl *DMD = Actions.ActOnOpenMPDeclareMapperDirectiveStart(
562 getCurScope(), Actions.getCurLexicalContext(), MapperId, MapperType,
563 Range.getBegin(), VName, AS);
564 DeclarationNameInfo DirName;
565 SourceLocation Loc = Tok.getLocation();
566 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
567 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
568 ParseScope OMPDirectiveScope(this, ScopeFlags);
569 Actions.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, getCurScope(), Loc);
570
571 // Add the mapper variable declaration.
572 Actions.ActOnOpenMPDeclareMapperDirectiveVarDecl(
573 DMD, getCurScope(), MapperType, Range.getBegin(), VName);
574
575 // Parse map clauses.
576 SmallVector<OMPClause *, 6> Clauses;
577 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
578 OpenMPClauseKind CKind = Tok.isAnnotation()
579 ? OMPC_unknown
580 : getOpenMPClauseKind(PP.getSpelling(Tok));
581 Actions.StartOpenMPClause(CKind);
582 OMPClause *Clause =
583 ParseOpenMPClause(OMPD_declare_mapper, CKind, Clauses.size() == 0);
584 if (Clause)
585 Clauses.push_back(Clause);
586 else
587 IsCorrect = false;
588 // Skip ',' if any.
589 if (Tok.is(tok::comma))
590 ConsumeToken();
591 Actions.EndOpenMPClause();
592 }
593 if (Clauses.empty()) {
594 Diag(Tok, diag::err_omp_expected_clause)
595 << getOpenMPDirectiveName(OMPD_declare_mapper);
596 IsCorrect = false;
597 }
598
599 // Exit scope.
600 Actions.EndOpenMPDSABlock(nullptr);
601 OMPDirectiveScope.Exit();
602
603 DeclGroupPtrTy DGP =
604 Actions.ActOnOpenMPDeclareMapperDirectiveEnd(DMD, getCurScope(), Clauses);
605 if (!IsCorrect)
606 return DeclGroupPtrTy();
607 return DGP;
608}
609
610TypeResult Parser::parseOpenMPDeclareMapperVarDecl(SourceRange &Range,
611 DeclarationName &Name,
612 AccessSpecifier AS) {
613 // Parse the common declaration-specifiers piece.
614 Parser::DeclSpecContext DSC = Parser::DeclSpecContext::DSC_type_specifier;
615 DeclSpec DS(AttrFactory);
616 ParseSpecifierQualifierList(DS, AS, DSC);
617
618 // Parse the declarator.
619 DeclaratorContext Context = DeclaratorContext::PrototypeContext;
620 Declarator DeclaratorInfo(DS, Context);
621 ParseDeclarator(DeclaratorInfo);
622 Range = DeclaratorInfo.getSourceRange();
623 if (DeclaratorInfo.getIdentifier() == nullptr) {
624 Diag(Tok.getLocation(), diag::err_omp_mapper_expected_declarator);
625 return true;
626 }
627 Name = Actions.GetNameForDeclarator(DeclaratorInfo).getName();
628
629 return Actions.ActOnOpenMPDeclareMapperVarDecl(getCurScope(), DeclaratorInfo);
630}
631
Alexey Bataev2af33e32016-04-07 12:45:37 +0000632namespace {
633/// RAII that recreates function context for correct parsing of clauses of
634/// 'declare simd' construct.
635/// OpenMP, 2.8.2 declare simd Construct
636/// The expressions appearing in the clauses of this directive are evaluated in
637/// the scope of the arguments of the function declaration or definition.
638class FNContextRAII final {
639 Parser &P;
640 Sema::CXXThisScopeRAII *ThisScope;
641 Parser::ParseScope *TempScope;
642 Parser::ParseScope *FnScope;
643 bool HasTemplateScope = false;
644 bool HasFunScope = false;
645 FNContextRAII() = delete;
646 FNContextRAII(const FNContextRAII &) = delete;
647 FNContextRAII &operator=(const FNContextRAII &) = delete;
648
649public:
650 FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
651 Decl *D = *Ptr.get().begin();
652 NamedDecl *ND = dyn_cast<NamedDecl>(D);
653 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
654 Sema &Actions = P.getActions();
655
656 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000657 ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, Qualifiers(),
Alexey Bataev2af33e32016-04-07 12:45:37 +0000658 ND && ND->isCXXInstanceMember());
659
660 // If the Decl is templatized, add template parameters to scope.
661 HasTemplateScope = D->isTemplateDecl();
662 TempScope =
663 new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
664 if (HasTemplateScope)
665 Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
666
667 // If the Decl is on a function, add function parameters to the scope.
668 HasFunScope = D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +0000669 FnScope = new Parser::ParseScope(
670 &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
671 HasFunScope);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000672 if (HasFunScope)
673 Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
674 }
675 ~FNContextRAII() {
676 if (HasFunScope) {
677 P.getActions().ActOnExitFunctionContext();
678 FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
679 }
680 if (HasTemplateScope)
681 TempScope->Exit();
682 delete FnScope;
683 delete TempScope;
684 delete ThisScope;
685 }
686};
687} // namespace
688
Alexey Bataevd93d3762016-04-12 09:35:56 +0000689/// Parses clauses for 'declare simd' directive.
690/// clause:
691/// 'inbranch' | 'notinbranch'
692/// 'simdlen' '(' <expr> ')'
693/// { 'uniform' '(' <argument_list> ')' }
694/// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
Alexey Bataevecba70f2016-04-12 11:02:11 +0000695/// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
696static bool parseDeclareSimdClauses(
697 Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
698 SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
699 SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
700 SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000701 SourceRange BSRange;
702 const Token &Tok = P.getCurToken();
703 bool IsError = false;
704 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
705 if (Tok.isNot(tok::identifier))
706 break;
707 OMPDeclareSimdDeclAttr::BranchStateTy Out;
708 IdentifierInfo *II = Tok.getIdentifierInfo();
709 StringRef ClauseName = II->getName();
710 // Parse 'inranch|notinbranch' clauses.
711 if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
712 if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
713 P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
714 << ClauseName
715 << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
716 IsError = true;
717 }
718 BS = Out;
719 BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
720 P.ConsumeToken();
721 } else if (ClauseName.equals("simdlen")) {
722 if (SimdLen.isUsable()) {
723 P.Diag(Tok, diag::err_omp_more_one_clause)
724 << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
725 IsError = true;
726 }
727 P.ConsumeToken();
728 SourceLocation RLoc;
729 SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
730 if (SimdLen.isInvalid())
731 IsError = true;
732 } else {
733 OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000734 if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
735 CKind == OMPC_linear) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000736 Parser::OpenMPVarListDataTy Data;
Alexey Bataev61908f652018-04-23 19:53:05 +0000737 SmallVectorImpl<Expr *> *Vars = &Uniforms;
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500738 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000739 Vars = &Aligneds;
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500740 } else if (CKind == OMPC_linear) {
741 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000742 Vars = &Linears;
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500743 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000744
745 P.ConsumeToken();
746 if (P.ParseOpenMPVarList(OMPD_declare_simd,
747 getOpenMPClauseKind(ClauseName), *Vars, Data))
748 IsError = true;
Alexey Bataev61908f652018-04-23 19:53:05 +0000749 if (CKind == OMPC_aligned) {
Alexey Bataevd93d3762016-04-12 09:35:56 +0000750 Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
Alexey Bataev61908f652018-04-23 19:53:05 +0000751 } else if (CKind == OMPC_linear) {
Alexey Bataev3732f4e2019-12-24 16:02:58 -0500752 assert(0 <= Data.ExtraModifier &&
753 Data.ExtraModifier <= OMPC_LINEAR_unknown &&
754 "Unexpected linear modifier.");
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500755 if (P.getActions().CheckOpenMPLinearModifier(
756 static_cast<OpenMPLinearClauseKind>(Data.ExtraModifier),
757 Data.DepLinMapLastLoc))
758 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000759 LinModifiers.append(Linears.size() - LinModifiers.size(),
Alexey Bataev93dc40d2019-12-20 11:04:57 -0500760 Data.ExtraModifier);
Alexey Bataevecba70f2016-04-12 11:02:11 +0000761 Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
762 }
Alexey Bataevd93d3762016-04-12 09:35:56 +0000763 } else
764 // TODO: add parsing of other clauses.
765 break;
766 }
767 // Skip ',' if any.
768 if (Tok.is(tok::comma))
769 P.ConsumeToken();
770 }
771 return IsError;
772}
773
Alexey Bataev2af33e32016-04-07 12:45:37 +0000774/// Parse clauses for '#pragma omp declare simd'.
775Parser::DeclGroupPtrTy
776Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
777 CachedTokens &Toks, SourceLocation Loc) {
Ilya Biryukov929af672019-05-17 09:32:05 +0000778 PP.EnterToken(Tok, /*IsReinject*/ true);
779 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
780 /*IsReinject*/ true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000781 // Consume the previously pushed token.
782 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataevd158cf62019-09-13 20:18:17 +0000783 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000784
785 FNContextRAII FnContext(*this, Ptr);
786 OMPDeclareSimdDeclAttr::BranchStateTy BS =
787 OMPDeclareSimdDeclAttr::BS_Undefined;
788 ExprResult Simdlen;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +0000789 SmallVector<Expr *, 4> Uniforms;
Alexey Bataevd93d3762016-04-12 09:35:56 +0000790 SmallVector<Expr *, 4> Aligneds;
791 SmallVector<Expr *, 4> Alignments;
Alexey Bataevecba70f2016-04-12 11:02:11 +0000792 SmallVector<Expr *, 4> Linears;
793 SmallVector<unsigned, 4> LinModifiers;
794 SmallVector<Expr *, 4> Steps;
795 bool IsError =
796 parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
797 Alignments, Linears, LinModifiers, Steps);
Alexey Bataev2af33e32016-04-07 12:45:37 +0000798 // Need to check for extra tokens.
799 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
800 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
801 << getOpenMPDirectiveName(OMPD_declare_simd);
802 while (Tok.isNot(tok::annot_pragma_openmp_end))
803 ConsumeAnyToken();
804 }
805 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +0000806 SourceLocation EndLoc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +0000807 if (IsError)
808 return Ptr;
809 return Actions.ActOnOpenMPDeclareSimdDirective(
810 Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
811 LinModifiers, Steps, SourceRange(Loc, EndLoc));
Alexey Bataev20dfd772016-04-04 10:12:15 +0000812}
813
Alexey Bataeva15a1412019-10-02 18:19:02 +0000814/// Parse optional 'score' '(' <expr> ')' ':'.
815static ExprResult parseContextScore(Parser &P) {
816 ExprResult ScoreExpr;
Alexey Bataevfde11e92019-11-07 11:03:10 -0500817 Sema::OMPCtxStringType Buffer;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000818 StringRef SelectorName =
819 P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
Alexey Bataevdcec2ac2019-11-05 15:33:18 -0500820 if (!SelectorName.equals("score"))
Alexey Bataeva15a1412019-10-02 18:19:02 +0000821 return ScoreExpr;
Alexey Bataeva15a1412019-10-02 18:19:02 +0000822 (void)P.ConsumeToken();
823 SourceLocation RLoc;
824 ScoreExpr = P.ParseOpenMPParensExpr(SelectorName, RLoc);
825 // Parse ':'
826 if (P.getCurToken().is(tok::colon))
827 (void)P.ConsumeAnyToken();
828 else
829 P.Diag(P.getCurToken(), diag::warn_pragma_expected_colon)
830 << "context selector score clause";
831 return ScoreExpr;
832}
833
Alexey Bataev9ff34742019-09-25 19:43:37 +0000834/// Parse context selector for 'implementation' selector set:
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000835/// 'vendor' '(' [ 'score' '(' <score _expr> ')' ':' ] <vendor> { ',' <vendor> }
836/// ')'
Alexey Bataevfde11e92019-11-07 11:03:10 -0500837static void
838parseImplementationSelector(Parser &P, SourceLocation Loc,
839 llvm::StringMap<SourceLocation> &UsedCtx,
840 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000841 const Token &Tok = P.getCurToken();
842 // Parse inner context selector set name, if any.
843 if (!Tok.is(tok::identifier)) {
844 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
845 << "implementation";
846 // Skip until either '}', ')', or end of directive.
847 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
848 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
849 ;
850 return;
851 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500852 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +0000853 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
Alexey Bataev70d2e542019-10-08 17:47:52 +0000854 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
855 if (!Res.second) {
856 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
857 // Each trait-selector-name can only be specified once.
858 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
859 << CtxSelectorName << "implementation";
860 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
861 << CtxSelectorName;
862 }
Alexey Bataevfde11e92019-11-07 11:03:10 -0500863 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000864 (void)P.ConsumeToken();
865 switch (CSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -0500866 case OMP_CTX_vendor: {
Alexey Bataev9ff34742019-09-25 19:43:37 +0000867 // Parse '('.
868 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
869 (void)T.expectAndConsume(diag::err_expected_lparen_after,
870 CtxSelectorName.data());
Alexey Bataevfde11e92019-11-07 11:03:10 -0500871 ExprResult Score = parseContextScore(P);
872 llvm::UniqueVector<Sema::OMPCtxStringType> Vendors;
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000873 do {
874 // Parse <vendor>.
875 StringRef VendorName;
876 if (Tok.is(tok::identifier)) {
877 Buffer.clear();
878 VendorName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
879 (void)P.ConsumeToken();
Alexey Bataev303657a2019-10-08 19:44:16 +0000880 if (!VendorName.empty())
Alexey Bataev4513e93f2019-10-10 15:15:26 +0000881 Vendors.insert(VendorName);
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000882 } else {
883 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
884 << "vendor identifier"
885 << "vendor"
886 << "implementation";
887 }
Alexey Bataev1c9e1732019-10-04 15:58:45 +0000888 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
889 P.Diag(Tok, diag::err_expected_punc)
890 << (VendorName.empty() ? "vendor name" : VendorName);
891 }
892 } while (Tok.is(tok::identifier));
Alexey Bataev9ff34742019-09-25 19:43:37 +0000893 // Parse ')'.
894 (void)T.consumeClose();
Alexey Bataevfde11e92019-11-07 11:03:10 -0500895 if (!Vendors.empty())
896 Data.emplace_back(OMP_CTX_SET_implementation, CSKind, Score, Vendors);
Alexey Bataev9ff34742019-09-25 19:43:37 +0000897 break;
898 }
Alexey Bataev4e8231b2019-11-05 15:13:30 -0500899 case OMP_CTX_kind:
Alexey Bataevfde11e92019-11-07 11:03:10 -0500900 case OMP_CTX_unknown:
Alexey Bataev9ff34742019-09-25 19:43:37 +0000901 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
902 << "implementation";
903 // Skip until either '}', ')', or end of directive.
904 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
905 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
906 ;
907 return;
908 }
Alexey Bataev9ff34742019-09-25 19:43:37 +0000909}
910
Alexey Bataev4e8231b2019-11-05 15:13:30 -0500911/// Parse context selector for 'device' selector set:
912/// 'kind' '(' <kind> { ',' <kind> } ')'
913static void
914parseDeviceSelector(Parser &P, SourceLocation Loc,
915 llvm::StringMap<SourceLocation> &UsedCtx,
916 SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
917 const Token &Tok = P.getCurToken();
918 // Parse inner context selector set name, if any.
919 if (!Tok.is(tok::identifier)) {
920 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
921 << "device";
922 // Skip until either '}', ')', or end of directive.
923 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
924 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
925 ;
926 return;
927 }
928 Sema::OMPCtxStringType Buffer;
929 StringRef CtxSelectorName = P.getPreprocessor().getSpelling(Tok, Buffer);
930 auto Res = UsedCtx.try_emplace(CtxSelectorName, Tok.getLocation());
931 if (!Res.second) {
932 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
933 // Each trait-selector-name can only be specified once.
934 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_mutiple_use)
935 << CtxSelectorName << "device";
936 P.Diag(Res.first->getValue(), diag::note_omp_declare_variant_ctx_used_here)
937 << CtxSelectorName;
938 }
939 OpenMPContextSelectorKind CSKind = getOpenMPContextSelector(CtxSelectorName);
940 (void)P.ConsumeToken();
941 switch (CSKind) {
942 case OMP_CTX_kind: {
943 // Parse '('.
944 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
945 (void)T.expectAndConsume(diag::err_expected_lparen_after,
946 CtxSelectorName.data());
947 llvm::UniqueVector<Sema::OMPCtxStringType> Kinds;
948 do {
949 // Parse <kind>.
950 StringRef KindName;
951 if (Tok.is(tok::identifier)) {
952 Buffer.clear();
953 KindName = P.getPreprocessor().getSpelling(P.getCurToken(), Buffer);
954 SourceLocation SLoc = P.getCurToken().getLocation();
955 (void)P.ConsumeToken();
956 if (llvm::StringSwitch<bool>(KindName)
957 .Case("host", false)
958 .Case("nohost", false)
959 .Case("cpu", false)
960 .Case("gpu", false)
961 .Case("fpga", false)
962 .Default(true)) {
963 P.Diag(SLoc, diag::err_omp_wrong_device_kind_trait) << KindName;
964 } else {
965 Kinds.insert(KindName);
966 }
967 } else {
968 P.Diag(Tok.getLocation(), diag::err_omp_declare_variant_item_expected)
969 << "'host', 'nohost', 'cpu', 'gpu', or 'fpga'"
970 << "kind"
971 << "device";
972 }
973 if (!P.TryConsumeToken(tok::comma) && Tok.isNot(tok::r_paren)) {
974 P.Diag(Tok, diag::err_expected_punc)
975 << (KindName.empty() ? "kind of device" : KindName);
976 }
977 } while (Tok.is(tok::identifier));
978 // Parse ')'.
979 (void)T.consumeClose();
980 if (!Kinds.empty())
981 Data.emplace_back(OMP_CTX_SET_device, CSKind, ExprResult(), Kinds);
982 break;
983 }
984 case OMP_CTX_vendor:
985 case OMP_CTX_unknown:
986 P.Diag(Tok.getLocation(), diag::warn_omp_declare_variant_cs_name_expected)
987 << "device";
988 // Skip until either '}', ')', or end of directive.
989 while (!P.SkipUntil(tok::r_brace, tok::r_paren,
990 tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
991 ;
992 return;
993 }
994}
995
Alexey Bataevd158cf62019-09-13 20:18:17 +0000996/// Parses clauses for 'declare variant' directive.
997/// clause:
Alexey Bataevd158cf62019-09-13 20:18:17 +0000998/// <selector_set_name> '=' '{' <context_selectors> '}'
Alexey Bataev0736f7f2019-09-18 16:24:31 +0000999/// [ ',' <selector_set_name> '=' '{' <context_selectors> '}' ]
1000bool Parser::parseOpenMPContextSelectors(
Alexey Bataevfde11e92019-11-07 11:03:10 -05001001 SourceLocation Loc, SmallVectorImpl<Sema::OMPCtxSelectorData> &Data) {
Alexey Bataev5d154c32019-10-08 15:56:43 +00001002 llvm::StringMap<SourceLocation> UsedCtxSets;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001003 do {
1004 // Parse inner context selector set name.
1005 if (!Tok.is(tok::identifier)) {
1006 Diag(Tok.getLocation(), diag::err_omp_declare_variant_no_ctx_selector)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001007 << getOpenMPClauseName(OMPC_match);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001008 return true;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001009 }
Alexey Bataevfde11e92019-11-07 11:03:10 -05001010 Sema::OMPCtxStringType Buffer;
Alexey Bataev9ff34742019-09-25 19:43:37 +00001011 StringRef CtxSelectorSetName = PP.getSpelling(Tok, Buffer);
Alexey Bataev5d154c32019-10-08 15:56:43 +00001012 auto Res = UsedCtxSets.try_emplace(CtxSelectorSetName, Tok.getLocation());
1013 if (!Res.second) {
1014 // OpenMP 5.0, 2.3.2 Context Selectors, Restrictions.
1015 // Each trait-set-selector-name can only be specified once.
1016 Diag(Tok.getLocation(), diag::err_omp_declare_variant_ctx_set_mutiple_use)
1017 << CtxSelectorSetName;
1018 Diag(Res.first->getValue(),
1019 diag::note_omp_declare_variant_ctx_set_used_here)
1020 << CtxSelectorSetName;
1021 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001022 // Parse '='.
1023 (void)ConsumeToken();
1024 if (Tok.isNot(tok::equal)) {
1025 Diag(Tok.getLocation(), diag::err_omp_declare_variant_equal_expected)
Alexey Bataev9ff34742019-09-25 19:43:37 +00001026 << CtxSelectorSetName;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001027 return true;
1028 }
1029 (void)ConsumeToken();
1030 // TBD: add parsing of known context selectors.
1031 // Unknown selector - just ignore it completely.
1032 {
1033 // Parse '{'.
1034 BalancedDelimiterTracker TBr(*this, tok::l_brace,
1035 tok::annot_pragma_openmp_end);
1036 if (TBr.expectAndConsume(diag::err_expected_lbrace_after, "="))
1037 return true;
Alexey Bataevfde11e92019-11-07 11:03:10 -05001038 OpenMPContextSelectorSetKind CSSKind =
1039 getOpenMPContextSelectorSet(CtxSelectorSetName);
Alexey Bataev70d2e542019-10-08 17:47:52 +00001040 llvm::StringMap<SourceLocation> UsedCtx;
1041 do {
1042 switch (CSSKind) {
Alexey Bataevfde11e92019-11-07 11:03:10 -05001043 case OMP_CTX_SET_implementation:
1044 parseImplementationSelector(*this, Loc, UsedCtx, Data);
Alexey Bataev70d2e542019-10-08 17:47:52 +00001045 break;
Alexey Bataev4e8231b2019-11-05 15:13:30 -05001046 case OMP_CTX_SET_device:
1047 parseDeviceSelector(*this, Loc, UsedCtx, Data);
1048 break;
Alexey Bataevfde11e92019-11-07 11:03:10 -05001049 case OMP_CTX_SET_unknown:
Alexey Bataev70d2e542019-10-08 17:47:52 +00001050 // Skip until either '}', ')', or end of directive.
1051 while (!SkipUntil(tok::r_brace, tok::r_paren,
1052 tok::annot_pragma_openmp_end, StopBeforeMatch))
1053 ;
1054 break;
1055 }
1056 const Token PrevTok = Tok;
1057 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
1058 Diag(Tok, diag::err_omp_expected_comma_brace)
1059 << (PrevTok.isAnnotation() ? "context selector trait"
1060 : PP.getSpelling(PrevTok));
1061 } while (Tok.is(tok::identifier));
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001062 // Parse '}'.
1063 (void)TBr.consumeClose();
1064 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001065 // Consume ','
1066 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end))
1067 (void)ExpectAndConsume(tok::comma);
1068 } while (Tok.isAnyIdentifier());
Alexey Bataevd158cf62019-09-13 20:18:17 +00001069 return false;
1070}
1071
1072/// Parse clauses for '#pragma omp declare variant ( variant-func-id ) clause'.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001073void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
1074 CachedTokens &Toks,
1075 SourceLocation Loc) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001076 PP.EnterToken(Tok, /*IsReinject*/ true);
1077 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1078 /*IsReinject*/ true);
1079 // Consume the previously pushed token.
1080 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1081 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1082
1083 FNContextRAII FnContext(*this, Ptr);
1084 // Parse function declaration id.
1085 SourceLocation RLoc;
1086 // Parse with IsAddressOfOperand set to true to parse methods as DeclRefExprs
1087 // instead of MemberExprs.
Alexey Bataev02d04d52019-12-10 16:12:53 -05001088 ExprResult AssociatedFunction;
1089 {
1090 // Do not mark function as is used to prevent its emission if this is the
1091 // only place where it is used.
1092 EnterExpressionEvaluationContext Unevaluated(
1093 Actions, Sema::ExpressionEvaluationContext::Unevaluated);
1094 AssociatedFunction = ParseOpenMPParensExpr(
1095 getOpenMPDirectiveName(OMPD_declare_variant), RLoc,
1096 /*IsAddressOfOperand=*/true);
1097 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001098 if (!AssociatedFunction.isUsable()) {
1099 if (!Tok.is(tok::annot_pragma_openmp_end))
1100 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1101 ;
1102 // Skip the last annot_pragma_openmp_end.
1103 (void)ConsumeAnnotationToken();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001104 return;
1105 }
1106 Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
1107 Actions.checkOpenMPDeclareVariantFunction(
1108 Ptr, AssociatedFunction.get(), SourceRange(Loc, Tok.getLocation()));
1109
1110 // Parse 'match'.
Alexey Bataevdba792c2019-09-23 18:13:31 +00001111 OpenMPClauseKind CKind = Tok.isAnnotation()
1112 ? OMPC_unknown
1113 : getOpenMPClauseKind(PP.getSpelling(Tok));
1114 if (CKind != OMPC_match) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001115 Diag(Tok.getLocation(), diag::err_omp_declare_variant_wrong_clause)
Alexey Bataevdba792c2019-09-23 18:13:31 +00001116 << getOpenMPClauseName(OMPC_match);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001117 while (!SkipUntil(tok::annot_pragma_openmp_end, Parser::StopBeforeMatch))
1118 ;
1119 // Skip the last annot_pragma_openmp_end.
1120 (void)ConsumeAnnotationToken();
1121 return;
1122 }
1123 (void)ConsumeToken();
1124 // Parse '('.
1125 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataevdba792c2019-09-23 18:13:31 +00001126 if (T.expectAndConsume(diag::err_expected_lparen_after,
1127 getOpenMPClauseName(OMPC_match))) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001128 while (!SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch))
1129 ;
1130 // Skip the last annot_pragma_openmp_end.
1131 (void)ConsumeAnnotationToken();
1132 return;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001133 }
1134
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001135 // Parse inner context selectors.
Alexey Bataevfde11e92019-11-07 11:03:10 -05001136 SmallVector<Sema::OMPCtxSelectorData, 4> Data;
1137 if (!parseOpenMPContextSelectors(Loc, Data)) {
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001138 // Parse ')'.
1139 (void)T.consumeClose();
1140 // Need to check for extra tokens.
1141 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1142 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1143 << getOpenMPDirectiveName(OMPD_declare_variant);
1144 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001145 }
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001146
1147 // Skip last tokens.
1148 while (Tok.isNot(tok::annot_pragma_openmp_end))
1149 ConsumeAnyToken();
Alexey Bataevfde11e92019-11-07 11:03:10 -05001150 if (DeclVarData.hasValue())
1151 Actions.ActOnOpenMPDeclareVariantDirective(
1152 DeclVarData.getValue().first, DeclVarData.getValue().second,
1153 SourceRange(Loc, Tok.getLocation()), Data);
Alexey Bataevd158cf62019-09-13 20:18:17 +00001154 // Skip the last annot_pragma_openmp_end.
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001155 (void)ConsumeAnnotationToken();
Alexey Bataevd158cf62019-09-13 20:18:17 +00001156}
1157
Alexey Bataev729e2422019-08-23 16:11:14 +00001158/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
1159///
1160/// default-clause:
1161/// 'default' '(' 'none' | 'shared' ')
1162///
1163/// proc_bind-clause:
1164/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
1165///
1166/// device_type-clause:
1167/// 'device_type' '(' 'host' | 'nohost' | 'any' )'
1168namespace {
1169 struct SimpleClauseData {
1170 unsigned Type;
1171 SourceLocation Loc;
1172 SourceLocation LOpen;
1173 SourceLocation TypeLoc;
1174 SourceLocation RLoc;
1175 SimpleClauseData(unsigned Type, SourceLocation Loc, SourceLocation LOpen,
1176 SourceLocation TypeLoc, SourceLocation RLoc)
1177 : Type(Type), Loc(Loc), LOpen(LOpen), TypeLoc(TypeLoc), RLoc(RLoc) {}
1178 };
1179} // anonymous namespace
1180
1181static Optional<SimpleClauseData>
1182parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
1183 const Token &Tok = P.getCurToken();
1184 SourceLocation Loc = Tok.getLocation();
1185 SourceLocation LOpen = P.ConsumeToken();
1186 // Parse '('.
1187 BalancedDelimiterTracker T(P, tok::l_paren, tok::annot_pragma_openmp_end);
1188 if (T.expectAndConsume(diag::err_expected_lparen_after,
1189 getOpenMPClauseName(Kind)))
1190 return llvm::None;
1191
1192 unsigned Type = getOpenMPSimpleClauseType(
1193 Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1194 SourceLocation TypeLoc = Tok.getLocation();
1195 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
1196 Tok.isNot(tok::annot_pragma_openmp_end))
1197 P.ConsumeAnyToken();
1198
1199 // Parse ')'.
1200 SourceLocation RLoc = Tok.getLocation();
1201 if (!T.consumeClose())
1202 RLoc = T.getCloseLocation();
1203
1204 return SimpleClauseData(Type, Loc, LOpen, TypeLoc, RLoc);
1205}
1206
Kelvin Lie0502752018-11-21 20:15:57 +00001207Parser::DeclGroupPtrTy Parser::ParseOMPDeclareTargetClauses() {
1208 // OpenMP 4.5 syntax with list of entities.
1209 Sema::NamedDeclSetType SameDirectiveDecls;
Alexey Bataev729e2422019-08-23 16:11:14 +00001210 SmallVector<std::tuple<OMPDeclareTargetDeclAttr::MapTypeTy, SourceLocation,
1211 NamedDecl *>,
1212 4>
1213 DeclareTargetDecls;
1214 OMPDeclareTargetDeclAttr::DevTypeTy DT = OMPDeclareTargetDeclAttr::DT_Any;
1215 SourceLocation DeviceTypeLoc;
Kelvin Lie0502752018-11-21 20:15:57 +00001216 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1217 OMPDeclareTargetDeclAttr::MapTypeTy MT = OMPDeclareTargetDeclAttr::MT_To;
1218 if (Tok.is(tok::identifier)) {
1219 IdentifierInfo *II = Tok.getIdentifierInfo();
1220 StringRef ClauseName = II->getName();
Alexey Bataev729e2422019-08-23 16:11:14 +00001221 bool IsDeviceTypeClause =
1222 getLangOpts().OpenMP >= 50 &&
1223 getOpenMPClauseKind(ClauseName) == OMPC_device_type;
1224 // Parse 'to|link|device_type' clauses.
1225 if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName, MT) &&
1226 !IsDeviceTypeClause) {
1227 Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
1228 << ClauseName << (getLangOpts().OpenMP >= 50 ? 1 : 0);
Kelvin Lie0502752018-11-21 20:15:57 +00001229 break;
1230 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001231 // Parse 'device_type' clause and go to next clause if any.
1232 if (IsDeviceTypeClause) {
1233 Optional<SimpleClauseData> DevTypeData =
1234 parseOpenMPSimpleClause(*this, OMPC_device_type);
1235 if (DevTypeData.hasValue()) {
1236 if (DeviceTypeLoc.isValid()) {
1237 // We already saw another device_type clause, diagnose it.
1238 Diag(DevTypeData.getValue().Loc,
1239 diag::warn_omp_more_one_device_type_clause);
1240 }
1241 switch(static_cast<OpenMPDeviceType>(DevTypeData.getValue().Type)) {
1242 case OMPC_DEVICE_TYPE_any:
1243 DT = OMPDeclareTargetDeclAttr::DT_Any;
1244 break;
1245 case OMPC_DEVICE_TYPE_host:
1246 DT = OMPDeclareTargetDeclAttr::DT_Host;
1247 break;
1248 case OMPC_DEVICE_TYPE_nohost:
1249 DT = OMPDeclareTargetDeclAttr::DT_NoHost;
1250 break;
1251 case OMPC_DEVICE_TYPE_unknown:
1252 llvm_unreachable("Unexpected device_type");
1253 }
1254 DeviceTypeLoc = DevTypeData.getValue().Loc;
1255 }
1256 continue;
1257 }
Kelvin Lie0502752018-11-21 20:15:57 +00001258 ConsumeToken();
1259 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001260 auto &&Callback = [this, MT, &DeclareTargetDecls, &SameDirectiveDecls](
1261 CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
1262 NamedDecl *ND = Actions.lookupOpenMPDeclareTargetName(
1263 getCurScope(), SS, NameInfo, SameDirectiveDecls);
1264 if (ND)
1265 DeclareTargetDecls.emplace_back(MT, NameInfo.getLoc(), ND);
Kelvin Lie0502752018-11-21 20:15:57 +00001266 };
1267 if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
1268 /*AllowScopeSpecifier=*/true))
1269 break;
1270
1271 // Consume optional ','.
1272 if (Tok.is(tok::comma))
1273 ConsumeToken();
1274 }
1275 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1276 ConsumeAnyToken();
Alexey Bataev729e2422019-08-23 16:11:14 +00001277 for (auto &MTLocDecl : DeclareTargetDecls) {
1278 OMPDeclareTargetDeclAttr::MapTypeTy MT;
1279 SourceLocation Loc;
1280 NamedDecl *ND;
1281 std::tie(MT, Loc, ND) = MTLocDecl;
1282 // device_type clause is applied only to functions.
1283 Actions.ActOnOpenMPDeclareTargetName(
1284 ND, Loc, MT, isa<VarDecl>(ND) ? OMPDeclareTargetDeclAttr::DT_Any : DT);
1285 }
Kelvin Lie0502752018-11-21 20:15:57 +00001286 SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
1287 SameDirectiveDecls.end());
1288 if (Decls.empty())
1289 return DeclGroupPtrTy();
1290 return Actions.BuildDeclaratorGroup(Decls);
1291}
1292
1293void Parser::ParseOMPEndDeclareTargetDirective(OpenMPDirectiveKind DKind,
1294 SourceLocation DTLoc) {
1295 if (DKind != OMPD_end_declare_target) {
1296 Diag(Tok, diag::err_expected_end_declare_target);
1297 Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
1298 return;
1299 }
1300 ConsumeAnyToken();
1301 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1302 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1303 << getOpenMPDirectiveName(OMPD_end_declare_target);
1304 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1305 }
1306 // Skip the last annot_pragma_openmp_end.
1307 ConsumeAnyToken();
1308}
1309
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001310/// Parsing of declarative OpenMP directives.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001311///
1312/// threadprivate-directive:
1313/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001314/// annot_pragma_openmp_end
Alexey Bataeva769e072013-03-22 06:34:35 +00001315///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001316/// allocate-directive:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001317/// annot_pragma_openmp 'allocate' simple-variable-list [<clause>]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001318/// annot_pragma_openmp_end
1319///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001320/// declare-reduction-directive:
1321/// annot_pragma_openmp 'declare' 'reduction' [...]
1322/// annot_pragma_openmp_end
1323///
Michael Kruse251e1482019-02-01 20:25:04 +00001324/// declare-mapper-directive:
1325/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1326/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1327/// annot_pragma_openmp_end
1328///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001329/// declare-simd-directive:
1330/// annot_pragma_openmp 'declare simd' {<clause> [,]}
1331/// annot_pragma_openmp_end
1332/// <function declaration/definition>
1333///
Kelvin Li1408f912018-09-26 04:28:39 +00001334/// requires directive:
1335/// annot_pragma_openmp 'requires' <clause> [[[,] <clause>] ... ]
1336/// annot_pragma_openmp_end
1337///
Alexey Bataev587e1de2016-03-30 10:43:55 +00001338Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
1339 AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
1340 DeclSpec::TST TagType, Decl *Tag) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001341 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001342 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001343 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +00001344
Richard Smithaf3b3252017-05-18 19:21:48 +00001345 SourceLocation Loc = ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001346 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001347
1348 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001349 case OMPD_threadprivate: {
Alexey Bataeva769e072013-03-22 06:34:35 +00001350 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001351 DeclDirectiveListParserHelper Helper(this, DKind);
1352 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1353 /*AllowScopeSpecifier=*/true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001354 // The last seen token is annot_pragma_openmp_end - need to check for
1355 // extra tokens.
1356 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1357 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001358 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001359 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +00001360 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001361 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001362 ConsumeAnnotationToken();
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001363 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
1364 Helper.getIdentifiers());
Alexey Bataeva769e072013-03-22 06:34:35 +00001365 }
1366 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001367 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001368 case OMPD_allocate: {
1369 ConsumeToken();
1370 DeclDirectiveListParserHelper Helper(this, DKind);
1371 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1372 /*AllowScopeSpecifier=*/true)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001373 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001374 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001375 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1376 OMPC_unknown + 1>
1377 FirstClauses(OMPC_unknown + 1);
1378 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1379 OpenMPClauseKind CKind =
1380 Tok.isAnnotation() ? OMPC_unknown
1381 : getOpenMPClauseKind(PP.getSpelling(Tok));
1382 Actions.StartOpenMPClause(CKind);
1383 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1384 !FirstClauses[CKind].getInt());
1385 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1386 StopBeforeMatch);
1387 FirstClauses[CKind].setInt(true);
1388 if (Clause != nullptr)
1389 Clauses.push_back(Clause);
1390 if (Tok.is(tok::annot_pragma_openmp_end)) {
1391 Actions.EndOpenMPClause();
1392 break;
1393 }
1394 // Skip ',' if any.
1395 if (Tok.is(tok::comma))
1396 ConsumeToken();
1397 Actions.EndOpenMPClause();
1398 }
1399 // The last seen token is annot_pragma_openmp_end - need to check for
1400 // extra tokens.
1401 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1402 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1403 << getOpenMPDirectiveName(DKind);
1404 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1405 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001406 }
1407 // Skip the last annot_pragma_openmp_end.
1408 ConsumeAnnotationToken();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001409 return Actions.ActOnOpenMPAllocateDirective(Loc, Helper.getIdentifiers(),
1410 Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001411 }
1412 break;
1413 }
Kelvin Li1408f912018-09-26 04:28:39 +00001414 case OMPD_requires: {
1415 SourceLocation StartLoc = ConsumeToken();
1416 SmallVector<OMPClause *, 5> Clauses;
1417 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
1418 FirstClauses(OMPC_unknown + 1);
1419 if (Tok.is(tok::annot_pragma_openmp_end)) {
Ilya Biryukovff2a9972019-02-26 11:01:50 +00001420 Diag(Tok, diag::err_omp_expected_clause)
Kelvin Li1408f912018-09-26 04:28:39 +00001421 << getOpenMPDirectiveName(OMPD_requires);
1422 break;
1423 }
1424 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1425 OpenMPClauseKind CKind = Tok.isAnnotation()
1426 ? OMPC_unknown
1427 : getOpenMPClauseKind(PP.getSpelling(Tok));
1428 Actions.StartOpenMPClause(CKind);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001429 OMPClause *Clause = ParseOpenMPClause(OMPD_requires, CKind,
1430 !FirstClauses[CKind].getInt());
1431 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1432 StopBeforeMatch);
Kelvin Li1408f912018-09-26 04:28:39 +00001433 FirstClauses[CKind].setInt(true);
1434 if (Clause != nullptr)
1435 Clauses.push_back(Clause);
1436 if (Tok.is(tok::annot_pragma_openmp_end)) {
1437 Actions.EndOpenMPClause();
1438 break;
1439 }
1440 // Skip ',' if any.
1441 if (Tok.is(tok::comma))
1442 ConsumeToken();
1443 Actions.EndOpenMPClause();
1444 }
1445 // Consume final annot_pragma_openmp_end
1446 if (Clauses.size() == 0) {
1447 Diag(Tok, diag::err_omp_expected_clause)
1448 << getOpenMPDirectiveName(OMPD_requires);
1449 ConsumeAnnotationToken();
1450 return nullptr;
1451 }
1452 ConsumeAnnotationToken();
1453 return Actions.ActOnOpenMPRequiresDirective(StartLoc, Clauses);
1454 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001455 case OMPD_declare_reduction:
1456 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001457 if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001458 // The last seen token is annot_pragma_openmp_end - need to check for
1459 // extra tokens.
1460 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1461 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1462 << getOpenMPDirectiveName(OMPD_declare_reduction);
1463 while (Tok.isNot(tok::annot_pragma_openmp_end))
1464 ConsumeAnyToken();
1465 }
1466 // Skip the last annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001467 ConsumeAnnotationToken();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001468 return Res;
1469 }
1470 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001471 case OMPD_declare_mapper: {
1472 ConsumeToken();
1473 if (DeclGroupPtrTy Res = ParseOpenMPDeclareMapperDirective(AS)) {
1474 // Skip the last annot_pragma_openmp_end.
1475 ConsumeAnnotationToken();
1476 return Res;
1477 }
1478 break;
1479 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001480 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001481 case OMPD_declare_simd: {
1482 // The syntax is:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001483 // { #pragma omp declare {simd|variant} }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001484 // <function-declaration-or-definition>
1485 //
Alexey Bataev2af33e32016-04-07 12:45:37 +00001486 CachedTokens Toks;
Alexey Bataevd158cf62019-09-13 20:18:17 +00001487 Toks.push_back(Tok);
1488 ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00001489 while(Tok.isNot(tok::annot_pragma_openmp_end)) {
1490 Toks.push_back(Tok);
1491 ConsumeAnyToken();
1492 }
1493 Toks.push_back(Tok);
1494 ConsumeAnyToken();
Alexey Bataev587e1de2016-03-30 10:43:55 +00001495
1496 DeclGroupPtrTy Ptr;
Alexey Bataev61908f652018-04-23 19:53:05 +00001497 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001498 Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
Alexey Bataev61908f652018-04-23 19:53:05 +00001499 } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00001500 // Here we expect to see some function declaration.
1501 if (AS == AS_none) {
1502 assert(TagType == DeclSpec::TST_unspecified);
1503 MaybeParseCXX11Attributes(Attrs);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001504 ParsingDeclSpec PDS(*this);
1505 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1506 } else {
1507 Ptr =
1508 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1509 }
1510 }
1511 if (!Ptr) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00001512 Diag(Loc, diag::err_omp_decl_in_declare_simd_variant)
1513 << (DKind == OMPD_declare_simd ? 0 : 1);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001514 return DeclGroupPtrTy();
1515 }
Alexey Bataevd158cf62019-09-13 20:18:17 +00001516 if (DKind == OMPD_declare_simd)
1517 return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
1518 assert(DKind == OMPD_declare_variant &&
1519 "Expected declare variant directive only");
Alexey Bataev0736f7f2019-09-18 16:24:31 +00001520 ParseOMPDeclareVariantClauses(Ptr, Toks, Loc);
1521 return Ptr;
Alexey Bataev587e1de2016-03-30 10:43:55 +00001522 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001523 case OMPD_declare_target: {
1524 SourceLocation DTLoc = ConsumeAnyToken();
1525 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Kelvin Lie0502752018-11-21 20:15:57 +00001526 return ParseOMPDeclareTargetClauses();
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001527 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001528
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001529 // Skip the last annot_pragma_openmp_end.
1530 ConsumeAnyToken();
1531
1532 if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
1533 return DeclGroupPtrTy();
1534
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001535 llvm::SmallVector<Decl *, 4> Decls;
Alexey Bataev61908f652018-04-23 19:53:05 +00001536 DKind = parseOpenMPDirectiveKind(*this);
Kelvin Libc38e632018-09-10 02:07:09 +00001537 while (DKind != OMPD_end_declare_target && Tok.isNot(tok::eof) &&
1538 Tok.isNot(tok::r_brace)) {
Alexey Bataev502ec492017-10-03 20:00:00 +00001539 DeclGroupPtrTy Ptr;
1540 // Here we expect to see some function declaration.
1541 if (AS == AS_none) {
1542 assert(TagType == DeclSpec::TST_unspecified);
1543 MaybeParseCXX11Attributes(Attrs);
1544 ParsingDeclSpec PDS(*this);
1545 Ptr = ParseExternalDeclaration(Attrs, &PDS);
1546 } else {
1547 Ptr =
1548 ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
1549 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001550 if (Ptr) {
1551 DeclGroupRef Ref = Ptr.get();
1552 Decls.append(Ref.begin(), Ref.end());
1553 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001554 if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
1555 TentativeParsingAction TPA(*this);
Richard Smithaf3b3252017-05-18 19:21:48 +00001556 ConsumeAnnotationToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001557 DKind = parseOpenMPDirectiveKind(*this);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001558 if (DKind != OMPD_end_declare_target)
1559 TPA.Revert();
1560 else
1561 TPA.Commit();
1562 }
1563 }
1564
Kelvin Lie0502752018-11-21 20:15:57 +00001565 ParseOMPEndDeclareTargetDirective(DKind, DTLoc);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001566 Actions.ActOnFinishOpenMPDeclareTargetDirective();
Alexey Bataev34f8a702018-03-28 14:28:54 +00001567 return Actions.BuildDeclaratorGroup(Decls);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001568 }
Alexey Bataeva769e072013-03-22 06:34:35 +00001569 case OMPD_unknown:
1570 Diag(Tok, diag::err_omp_unknown_directive);
1571 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001572 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001573 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001574 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +00001575 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001576 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001577 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001578 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +00001579 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001580 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001581 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001582 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001583 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001584 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +00001585 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001586 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001587 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001588 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001589 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001590 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001591 case OMPD_parallel_master:
Alexey Bataev0162e452014-07-22 10:10:35 +00001592 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001593 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001594 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001595 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001596 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +00001597 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001598 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001599 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001600 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001601 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001602 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001603 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001604 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001605 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001606 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001607 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001608 case OMPD_distribute:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001609 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001610 case OMPD_target_update:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001611 case OMPD_distribute_parallel_for:
Kelvin Li4a39add2016-07-05 05:00:15 +00001612 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001613 case OMPD_distribute_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001614 case OMPD_target_parallel_for_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001615 case OMPD_target_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001616 case OMPD_teams_distribute:
Kelvin Li4e325f72016-10-25 12:50:55 +00001617 case OMPD_teams_distribute_simd:
Kelvin Li579e41c2016-11-30 23:51:03 +00001618 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001619 case OMPD_teams_distribute_parallel_for:
Kelvin Libf594a52016-12-17 05:48:59 +00001620 case OMPD_target_teams:
Kelvin Li83c451e2016-12-25 04:52:54 +00001621 case OMPD_target_teams_distribute:
Kelvin Li80e8f562016-12-29 22:16:30 +00001622 case OMPD_target_teams_distribute_parallel_for:
Kelvin Li1851df52017-01-03 05:23:48 +00001623 case OMPD_target_teams_distribute_parallel_for_simd:
Kelvin Lida681182017-01-10 18:08:18 +00001624 case OMPD_target_teams_distribute_simd:
Alexey Bataeva769e072013-03-22 06:34:35 +00001625 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001626 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +00001627 break;
1628 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001629 while (Tok.isNot(tok::annot_pragma_openmp_end))
1630 ConsumeAnyToken();
1631 ConsumeAnyToken();
David Blaikie0403cb12016-01-15 23:43:25 +00001632 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001633}
1634
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001635/// Parsing of declarative or executable OpenMP directives.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001636///
1637/// threadprivate-directive:
1638/// annot_pragma_openmp 'threadprivate' simple-variable-list
1639/// annot_pragma_openmp_end
1640///
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001641/// allocate-directive:
1642/// annot_pragma_openmp 'allocate' simple-variable-list
1643/// annot_pragma_openmp_end
1644///
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001645/// declare-reduction-directive:
1646/// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
1647/// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
1648/// ('omp_priv' '=' <expression>|<function_call>) ')']
1649/// annot_pragma_openmp_end
1650///
Michael Kruse251e1482019-02-01 20:25:04 +00001651/// declare-mapper-directive:
1652/// annot_pragma_openmp 'declare' 'mapper' '(' [<mapper-identifer> ':']
1653/// <type> <var> ')' [<clause>[[,] <clause>] ... ]
1654/// annot_pragma_openmp_end
1655///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001656/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001657/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001658/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
cchen47d60942019-12-05 13:43:48 -05001659/// 'parallel for' | 'parallel sections' | 'parallel master' | 'task' |
1660/// 'taskyield' | 'barrier' | 'taskwait' | 'flush' | 'ordered' |
1661/// 'atomic' | 'for simd' | 'parallel for simd' | 'target' | 'target
1662/// data' | 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
1663/// 'master taskloop' | 'master taskloop simd' | 'parallel master
1664/// taskloop' | 'parallel master taskloop simd' | 'distribute' | 'target
1665/// enter data' | 'target exit data' | 'target parallel' | 'target
1666/// parallel for' | 'target update' | 'distribute parallel for' |
1667/// 'distribute paralle for simd' | 'distribute simd' | 'target parallel
1668/// for simd' | 'target simd' | 'teams distribute' | 'teams distribute
1669/// simd' | 'teams distribute parallel for simd' | 'teams distribute
1670/// parallel for' | 'target teams' | 'target teams distribute' | 'target
1671/// teams distribute parallel for' | 'target teams distribute parallel
1672/// for simd' | 'target teams distribute simd' {clause}
Alexey Bataev14a388f2019-10-25 10:27:13 -04001673/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001674///
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001675StmtResult
1676Parser::ParseOpenMPDeclarativeOrExecutableDirective(ParsedStmtContext StmtCtx) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001677 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8035bb42019-12-13 16:05:30 -05001678 ParsingOpenMPDirectiveRAII DirScope(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001679 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001680 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001681 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +00001682 FirstClauses(OMPC_unknown + 1);
Momchil Velikov57c681f2017-08-10 15:43:06 +00001683 unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
1684 Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
Richard Smithaf3b3252017-05-18 19:21:48 +00001685 SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
Alexey Bataev61908f652018-04-23 19:53:05 +00001686 OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001687 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001688 // Name of critical directive.
1689 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001690 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +00001691 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +00001692 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001693
1694 switch (DKind) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001695 case OMPD_threadprivate: {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001696 // FIXME: Should this be permitted in C++?
1697 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1698 ParsedStmtContext()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001699 Diag(Tok, diag::err_omp_immediate_directive)
1700 << getOpenMPDirectiveName(DKind) << 0;
1701 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001702 ConsumeToken();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001703 DeclDirectiveListParserHelper Helper(this, DKind);
1704 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1705 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001706 // The last seen token is annot_pragma_openmp_end - need to check for
1707 // extra tokens.
1708 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1709 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001710 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00001711 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001712 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001713 DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
1714 Loc, Helper.getIdentifiers());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001715 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1716 }
Alp Tokerd751fa72013-12-18 19:10:49 +00001717 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001718 break;
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001719 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001720 case OMPD_allocate: {
1721 // FIXME: Should this be permitted in C++?
1722 if ((StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
1723 ParsedStmtContext()) {
1724 Diag(Tok, diag::err_omp_immediate_directive)
1725 << getOpenMPDirectiveName(DKind) << 0;
1726 }
1727 ConsumeToken();
1728 DeclDirectiveListParserHelper Helper(this, DKind);
1729 if (!ParseOpenMPSimpleVarList(DKind, Helper,
1730 /*AllowScopeSpecifier=*/false)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001731 SmallVector<OMPClause *, 1> Clauses;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001732 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001733 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>,
1734 OMPC_unknown + 1>
1735 FirstClauses(OMPC_unknown + 1);
1736 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
1737 OpenMPClauseKind CKind =
1738 Tok.isAnnotation() ? OMPC_unknown
1739 : getOpenMPClauseKind(PP.getSpelling(Tok));
1740 Actions.StartOpenMPClause(CKind);
1741 OMPClause *Clause = ParseOpenMPClause(OMPD_allocate, CKind,
1742 !FirstClauses[CKind].getInt());
1743 SkipUntil(tok::comma, tok::identifier, tok::annot_pragma_openmp_end,
1744 StopBeforeMatch);
1745 FirstClauses[CKind].setInt(true);
1746 if (Clause != nullptr)
1747 Clauses.push_back(Clause);
1748 if (Tok.is(tok::annot_pragma_openmp_end)) {
1749 Actions.EndOpenMPClause();
1750 break;
1751 }
1752 // Skip ',' if any.
1753 if (Tok.is(tok::comma))
1754 ConsumeToken();
1755 Actions.EndOpenMPClause();
1756 }
1757 // The last seen token is annot_pragma_openmp_end - need to check for
1758 // extra tokens.
1759 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1760 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1761 << getOpenMPDirectiveName(DKind);
1762 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
1763 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001764 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001765 DeclGroupPtrTy Res = Actions.ActOnOpenMPAllocateDirective(
1766 Loc, Helper.getIdentifiers(), Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001767 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1768 }
1769 SkipUntil(tok::annot_pragma_openmp_end);
1770 break;
1771 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001772 case OMPD_declare_reduction:
1773 ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00001774 if (DeclGroupPtrTy Res =
1775 ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001776 // The last seen token is annot_pragma_openmp_end - need to check for
1777 // extra tokens.
1778 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
1779 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
1780 << getOpenMPDirectiveName(OMPD_declare_reduction);
1781 while (Tok.isNot(tok::annot_pragma_openmp_end))
1782 ConsumeAnyToken();
1783 }
1784 ConsumeAnyToken();
1785 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
Alexey Bataev61908f652018-04-23 19:53:05 +00001786 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001787 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev61908f652018-04-23 19:53:05 +00001788 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001789 break;
Michael Kruse251e1482019-02-01 20:25:04 +00001790 case OMPD_declare_mapper: {
1791 ConsumeToken();
1792 if (DeclGroupPtrTy Res =
1793 ParseOpenMPDeclareMapperDirective(/*AS=*/AS_none)) {
1794 // Skip the last annot_pragma_openmp_end.
1795 ConsumeAnnotationToken();
1796 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
1797 } else {
1798 SkipUntil(tok::annot_pragma_openmp_end);
1799 }
1800 break;
1801 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001802 case OMPD_flush:
1803 if (PP.LookAhead(0).is(tok::l_paren)) {
1804 FlushHasClause = true;
1805 // Push copy of the current token back to stream to properly parse
1806 // pseudo-clause OMPFlushClause.
Ilya Biryukov929af672019-05-17 09:32:05 +00001807 PP.EnterToken(Tok, /*IsReinject*/ true);
Alexey Bataev6125da92014-07-21 11:26:11 +00001808 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001809 LLVM_FALLTHROUGH;
Alexey Bataev68446b72014-07-18 07:47:19 +00001810 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001811 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +00001812 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001813 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001814 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001815 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001816 case OMPD_target_exit_data:
Samuel Antao686c70c2016-05-26 17:30:50 +00001817 case OMPD_target_update:
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001818 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1819 ParsedStmtContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00001820 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +00001821 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +00001822 }
1823 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001824 // Fall through for further analysis.
Galina Kistanova474f2ce2017-06-01 21:26:38 +00001825 LLVM_FALLTHROUGH;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001826 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +00001827 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001828 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +00001829 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001830 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001831 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001832 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +00001833 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001834 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001835 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +00001836 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001837 case OMPD_parallel_sections:
cchen47d60942019-12-05 13:43:48 -05001838 case OMPD_parallel_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001839 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +00001840 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001841 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +00001842 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001843 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +00001844 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +00001845 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001846 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001847 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001848 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001849 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +00001850 case OMPD_master_taskloop:
Alexey Bataevb8552ab2019-10-18 16:47:35 +00001851 case OMPD_master_taskloop_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +00001852 case OMPD_parallel_master_taskloop:
Alexey Bataev14a388f2019-10-25 10:27:13 -04001853 case OMPD_parallel_master_taskloop_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001854 case OMPD_distribute:
Kelvin Li4a39add2016-07-05 05:00:15 +00001855 case OMPD_distribute_parallel_for:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001856 case OMPD_distribute_parallel_for_simd:
Kelvin Lia579b912016-07-14 02:54:56 +00001857 case OMPD_distribute_simd:
Kelvin Li986330c2016-07-20 22:57:10 +00001858 case OMPD_target_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001859 case OMPD_target_simd:
Kelvin Li4e325f72016-10-25 12:50:55 +00001860 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001861 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001862 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Libf594a52016-12-17 05:48:59 +00001863 case OMPD_teams_distribute_parallel_for:
Kelvin Li83c451e2016-12-25 04:52:54 +00001864 case OMPD_target_teams:
Kelvin Li80e8f562016-12-29 22:16:30 +00001865 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001866 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001867 case OMPD_target_teams_distribute_parallel_for_simd:
1868 case OMPD_target_teams_distribute_simd: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001869 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001870 // Parse directive name of the 'critical' directive if any.
1871 if (DKind == OMPD_critical) {
1872 BalancedDelimiterTracker T(*this, tok::l_paren,
1873 tok::annot_pragma_openmp_end);
1874 if (!T.consumeOpen()) {
1875 if (Tok.isAnyIdentifier()) {
1876 DirName =
1877 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
1878 ConsumeAnyToken();
1879 } else {
1880 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
1881 }
1882 T.consumeClose();
1883 }
Alexey Bataev80909872015-07-02 11:25:17 +00001884 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev61908f652018-04-23 19:53:05 +00001885 CancelRegion = parseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001886 if (Tok.isNot(tok::annot_pragma_openmp_end))
1887 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001888 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001889
Alexey Bataevf29276e2014-06-18 04:14:57 +00001890 if (isOpenMPLoopDirective(DKind))
1891 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
1892 if (isOpenMPSimdDirective(DKind))
1893 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
1894 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001895 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001896
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001897 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +00001898 OpenMPClauseKind CKind =
1899 Tok.isAnnotation()
1900 ? OMPC_unknown
1901 : FlushHasClause ? OMPC_flush
1902 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001903 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +00001904 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001905 OMPClause *Clause =
1906 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001907 FirstClauses[CKind].setInt(true);
1908 if (Clause) {
1909 FirstClauses[CKind].setPointer(Clause);
1910 Clauses.push_back(Clause);
1911 }
1912
1913 // Skip ',' if any.
1914 if (Tok.is(tok::comma))
1915 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +00001916 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001917 }
1918 // End location of the directive.
1919 EndLoc = Tok.getLocation();
1920 // Consume final annot_pragma_openmp_end.
Richard Smithaf3b3252017-05-18 19:21:48 +00001921 ConsumeAnnotationToken();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001922
Alexey Bataeveb482352015-12-18 05:05:56 +00001923 // OpenMP [2.13.8, ordered Construct, Syntax]
1924 // If the depend clause is specified, the ordered construct is a stand-alone
1925 // directive.
1926 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Richard Smitha6e8d5e2019-02-15 00:27:53 +00001927 if ((StmtCtx & ParsedStmtContext::AllowStandaloneOpenMPDirectives) ==
1928 ParsedStmtContext()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00001929 Diag(Loc, diag::err_omp_immediate_directive)
1930 << getOpenMPDirectiveName(DKind) << 1
1931 << getOpenMPClauseName(OMPC_depend);
1932 }
1933 HasAssociatedStatement = false;
1934 }
1935
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001936 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +00001937 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001938 // The body is a block scope like in Lambdas and Blocks.
Alexey Bataevbae9a792014-06-27 10:37:06 +00001939 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001940 // FIXME: We create a bogus CompoundStmt scope to hold the contents of
1941 // the captured region. Code elsewhere assumes that any FunctionScopeInfo
1942 // should have at least one compound statement scope within it.
1943 AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001944 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev7828b252017-11-21 17:08:48 +00001945 } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
1946 DKind == OMPD_target_exit_data) {
Alexey Bataev7828b252017-11-21 17:08:48 +00001947 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Richard Smith6eb9b9e2018-02-03 00:44:57 +00001948 AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
1949 Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
1950 /*isStmtExpr=*/false));
Alexey Bataev7828b252017-11-21 17:08:48 +00001951 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001952 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001953 Directive = Actions.ActOnOpenMPExecutableDirective(
1954 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
1955 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001956
1957 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001958 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001959 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001960 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +00001961 }
Alexey Bataev587e1de2016-03-30 10:43:55 +00001962 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001963 case OMPD_declare_target:
1964 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00001965 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00001966 case OMPD_declare_variant:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001967 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev96dae812018-02-16 18:36:44 +00001968 << 1 << getOpenMPDirectiveName(DKind);
Alexey Bataev587e1de2016-03-30 10:43:55 +00001969 SkipUntil(tok::annot_pragma_openmp_end);
1970 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001971 case OMPD_unknown:
1972 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +00001973 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001974 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001975 }
1976 return Directive;
1977}
1978
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001979// Parses simple list:
1980// simple-variable-list:
1981// '(' id-expression {, id-expression} ')'
1982//
1983bool Parser::ParseOpenMPSimpleVarList(
1984 OpenMPDirectiveKind Kind,
1985 const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
1986 Callback,
1987 bool AllowScopeSpecifier) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001988 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +00001989 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001990 if (T.expectAndConsume(diag::err_expected_lparen_after,
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06001991 getOpenMPDirectiveName(Kind).data()))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001992 return true;
1993 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001994 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +00001995
1996 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001997 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001998 CXXScopeSpec SS;
Alexey Bataeva769e072013-03-22 06:34:35 +00001999 UnqualifiedId Name;
2000 // Read var name.
2001 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002002 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +00002003
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002004 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +00002005 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002006 IsCorrect = false;
2007 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002008 StopBeforeMatch);
Richard Smith35845152017-02-07 01:37:30 +00002009 } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
Richard Smithc08b6932018-04-27 02:00:13 +00002010 nullptr, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002011 IsCorrect = false;
2012 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002013 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002014 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
2015 Tok.isNot(tok::annot_pragma_openmp_end)) {
2016 IsCorrect = false;
2017 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00002018 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +00002019 Diag(PrevTok.getLocation(), diag::err_expected)
2020 << tok::identifier
2021 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +00002022 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002023 Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
Alexey Bataeva769e072013-03-22 06:34:35 +00002024 }
2025 // Consume ','.
2026 if (Tok.is(tok::comma)) {
2027 ConsumeToken();
2028 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002029 }
2030
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002031 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +00002032 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002033 IsCorrect = false;
2034 }
2035
2036 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002037 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002038
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002039 return !IsCorrect;
Alexey Bataeva769e072013-03-22 06:34:35 +00002040}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002041
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002042/// Parsing of OpenMP clauses.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002043///
2044/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +00002045/// if-clause | final-clause | num_threads-clause | safelen-clause |
2046/// default-clause | private-clause | firstprivate-clause | shared-clause
2047/// | linear-clause | aligned-clause | collapse-clause |
2048/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002049/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +00002050/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +00002051/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002052/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002053/// thread_limit-clause | priority-clause | grainsize-clause |
Samuel Antaoec172c62016-05-26 17:49:04 +00002054/// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
Alexey Bataevfa312f32017-07-21 18:48:21 +00002055/// from-clause | is_device_ptr-clause | task_reduction-clause |
Alexey Bataeve04483e2019-03-27 14:14:31 +00002056/// in_reduction-clause | allocator-clause | allocate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002057///
2058OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
2059 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +00002060 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002061 bool ErrorFound = false;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002062 bool WrongDirective = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002063 // Check if clause is allowed for the given directive.
Alexey Bataevd08c0562019-11-19 12:07:54 -05002064 if (CKind != OMPC_unknown &&
2065 !isAllowedClauseForDirective(DKind, CKind, getLangOpts().OpenMP)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +00002066 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
2067 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002068 ErrorFound = true;
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002069 WrongDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002070 }
2071
2072 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00002073 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002074 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002075 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00002076 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002077 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +00002078 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +00002079 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +00002080 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002081 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00002082 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002083 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00002084 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00002085 case OMPC_hint:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002086 case OMPC_allocator:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002087 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +00002088 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +00002089 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002090 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +00002091 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +00002092 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +00002093 // OpenMP [2.9.1, target data construct, Restrictions]
2094 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +00002095 // OpenMP [2.11.1, task Construct, Restrictions]
2096 // At most one if clause can appear on the directive.
2097 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +00002098 // OpenMP [teams Construct, Restrictions]
2099 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002100 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +00002101 // OpenMP [2.9.1, task Construct, Restrictions]
2102 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002103 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2104 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +00002105 // OpenMP [2.9.2, taskloop Construct, Restrictions]
2106 // At most one num_tasks clause can appear on the directive.
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002107 // OpenMP [2.11.3, allocate Directive, Restrictions]
2108 // At most one allocator clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002109 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002110 Diag(Tok, diag::err_omp_more_one_clause)
2111 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002112 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002113 }
2114
Alexey Bataev10e775f2015-07-30 11:36:16 +00002115 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002116 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev10e775f2015-07-30 11:36:16 +00002117 else
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002118 Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002119 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002120 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002121 case OMPC_proc_bind:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002122 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002123 // OpenMP [2.14.3.1, Restrictions]
2124 // Only a single default clause may be specified on a parallel, task or
2125 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002126 // OpenMP [2.5, parallel Construct, Restrictions]
2127 // At most one proc_bind clause can appear on the directive.
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00002128 // OpenMP [5.0, Requires directive, Restrictions]
2129 // At most one atomic_default_mem_order clause can appear
2130 // on the directive
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002131 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002132 Diag(Tok, diag::err_omp_more_one_clause)
2133 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002134 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002135 }
2136
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002137 Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002138 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002139 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002140 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002141 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002142 // OpenMP [2.7.1, Restrictions, p. 3]
2143 // Only one schedule clause can appear on a loop directive.
cchene06f3e02019-11-15 13:02:06 -05002144 // OpenMP 4.5 [2.10.4, Restrictions, p. 106]
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002145 // At most one defaultmap clause can appear on the directive.
cchene06f3e02019-11-15 13:02:06 -05002146 if ((getLangOpts().OpenMP < 50 || CKind != OMPC_defaultmap) &&
2147 !FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002148 Diag(Tok, diag::err_omp_more_one_clause)
2149 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002150 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002151 }
Galina Kistanova474f2ce2017-06-01 21:26:38 +00002152 LLVM_FALLTHROUGH;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002153 case OMPC_if:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002154 Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002155 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002156 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002157 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002158 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002159 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002160 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002161 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002162 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002163 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +00002164 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002165 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +00002166 case OMPC_nogroup:
Kelvin Li1408f912018-09-26 04:28:39 +00002167 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00002168 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00002169 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00002170 case OMPC_dynamic_allocators:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002171 // OpenMP [2.7.1, Restrictions, p. 9]
2172 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +00002173 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
2174 // Only one nowait clause can appear on a for directive.
Kelvin Li1408f912018-09-26 04:28:39 +00002175 // OpenMP [5.0, Requires directive, Restrictions]
2176 // Each of the requires clauses can appear at most once on the directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002177 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002178 Diag(Tok, diag::err_omp_more_one_clause)
2179 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +00002180 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002181 }
2182
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002183 Clause = ParseOpenMPClause(CKind, WrongDirective);
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002184 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002185 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002186 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002187 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002188 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002189 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002190 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00002191 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002192 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002193 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002194 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002195 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002196 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002197 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002198 case OMPC_map:
Samuel Antao661c0902016-05-26 17:39:58 +00002199 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00002200 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00002201 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00002202 case OMPC_is_device_ptr:
Alexey Bataeve04483e2019-03-27 14:14:31 +00002203 case OMPC_allocate:
Alexey Bataevb6e70842019-12-16 15:54:17 -05002204 case OMPC_nontemporal:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002205 Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002206 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00002207 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002208 case OMPC_unknown:
2209 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +00002210 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002211 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002212 break;
2213 case OMPC_threadprivate:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002214 case OMPC_uniform:
Alexey Bataevdba792c2019-09-23 18:13:31 +00002215 case OMPC_match:
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002216 if (!WrongDirective)
2217 Diag(Tok, diag::err_omp_unexpected_clause)
2218 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +00002219 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002220 break;
2221 }
Craig Topper161e4db2014-05-21 06:02:52 +00002222 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002223}
2224
Alexey Bataev2af33e32016-04-07 12:45:37 +00002225/// Parses simple expression in parens for single-expression clauses of OpenMP
2226/// constructs.
2227/// \param RLoc Returned location of right paren.
2228ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
Alexey Bataevd158cf62019-09-13 20:18:17 +00002229 SourceLocation &RLoc,
2230 bool IsAddressOfOperand) {
Alexey Bataev2af33e32016-04-07 12:45:37 +00002231 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2232 if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
2233 return ExprError();
2234
2235 SourceLocation ELoc = Tok.getLocation();
2236 ExprResult LHS(ParseCastExpression(
Alexey Bataevd158cf62019-09-13 20:18:17 +00002237 /*isUnaryExpression=*/false, IsAddressOfOperand, NotTypeCast));
Alexey Bataev2af33e32016-04-07 12:45:37 +00002238 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002239 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002240
2241 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002242 RLoc = Tok.getLocation();
2243 if (!T.consumeClose())
2244 RLoc = T.getCloseLocation();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002245
Alexey Bataev2af33e32016-04-07 12:45:37 +00002246 return Val;
2247}
2248
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002249/// Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +00002250/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +00002251/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002252///
Alexey Bataev3778b602014-07-17 07:32:53 +00002253/// final-clause:
2254/// 'final' '(' expression ')'
2255///
Alexey Bataev62c87d22014-03-21 04:51:18 +00002256/// num_threads-clause:
2257/// 'num_threads' '(' expression ')'
2258///
2259/// safelen-clause:
2260/// 'safelen' '(' expression ')'
2261///
Alexey Bataev66b15b52015-08-21 11:14:16 +00002262/// simdlen-clause:
2263/// 'simdlen' '(' expression ')'
2264///
Alexander Musman8bd31e62014-05-27 15:12:19 +00002265/// collapse-clause:
2266/// 'collapse' '(' expression ')'
2267///
Alexey Bataeva0569352015-12-01 10:17:31 +00002268/// priority-clause:
2269/// 'priority' '(' expression ')'
2270///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002271/// grainsize-clause:
2272/// 'grainsize' '(' expression ')'
2273///
Alexey Bataev382967a2015-12-08 12:06:20 +00002274/// num_tasks-clause:
2275/// 'num_tasks' '(' expression ')'
2276///
Alexey Bataev28c75412015-12-15 08:19:24 +00002277/// hint-clause:
2278/// 'hint' '(' expression ')'
2279///
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002280/// allocator-clause:
2281/// 'allocator' '(' expression ')'
2282///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002283OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
2284 bool ParseOnly) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002285 SourceLocation Loc = ConsumeToken();
Alexey Bataev2af33e32016-04-07 12:45:37 +00002286 SourceLocation LLoc = Tok.getLocation();
2287 SourceLocation RLoc;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002288
Alexey Bataev2af33e32016-04-07 12:45:37 +00002289 ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002290
2291 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +00002292 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002293
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002294 if (ParseOnly)
2295 return nullptr;
Alexey Bataev2af33e32016-04-07 12:45:37 +00002296 return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002297}
2298
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002299/// Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002300///
2301/// default-clause:
2302/// 'default' '(' 'none' | 'shared' ')
2303///
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002304/// proc_bind-clause:
2305/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
2306///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002307OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
2308 bool ParseOnly) {
Alexey Bataev729e2422019-08-23 16:11:14 +00002309 llvm::Optional<SimpleClauseData> Val = parseOpenMPSimpleClause(*this, Kind);
2310 if (!Val || ParseOnly)
Craig Topper161e4db2014-05-21 06:02:52 +00002311 return nullptr;
Alexey Bataev729e2422019-08-23 16:11:14 +00002312 return Actions.ActOnOpenMPSimpleClause(
2313 Kind, Val.getValue().Type, Val.getValue().TypeLoc, Val.getValue().LOpen,
2314 Val.getValue().Loc, Val.getValue().RLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002315}
2316
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002317/// Parsing of OpenMP clauses like 'ordered'.
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002318///
2319/// ordered-clause:
2320/// 'ordered'
2321///
Alexey Bataev236070f2014-06-20 11:19:47 +00002322/// nowait-clause:
2323/// 'nowait'
2324///
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002325/// untied-clause:
2326/// 'untied'
2327///
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002328/// mergeable-clause:
2329/// 'mergeable'
2330///
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002331/// read-clause:
2332/// 'read'
2333///
Alexey Bataev346265e2015-09-25 10:37:12 +00002334/// threads-clause:
2335/// 'threads'
2336///
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002337/// simd-clause:
2338/// 'simd'
2339///
Alexey Bataevb825de12015-12-07 10:51:44 +00002340/// nogroup-clause:
2341/// 'nogroup'
2342///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002343OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002344 SourceLocation Loc = Tok.getLocation();
2345 ConsumeAnyToken();
2346
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002347 if (ParseOnly)
2348 return nullptr;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002349 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
2350}
2351
2352
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002353/// Parsing of OpenMP clauses with single expressions and some additional
Alexey Bataev56dafe82014-06-20 07:16:17 +00002354/// argument like 'schedule' or 'dist_schedule'.
2355///
2356/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +00002357/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
2358/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +00002359///
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002360/// if-clause:
2361/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
2362///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002363/// defaultmap:
2364/// 'defaultmap' '(' modifier ':' kind ')'
2365///
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002366OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
2367 bool ParseOnly) {
Alexey Bataev56dafe82014-06-20 07:16:17 +00002368 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002369 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002370 // Parse '('.
2371 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2372 if (T.expectAndConsume(diag::err_expected_lparen_after,
2373 getOpenMPClauseName(Kind)))
2374 return nullptr;
2375
2376 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002377 SmallVector<unsigned, 4> Arg;
2378 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002379 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00002380 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
2381 Arg.resize(NumberOfElements);
2382 KLoc.resize(NumberOfElements);
2383 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
2384 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
2385 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
Alexey Bataev61908f652018-04-23 19:53:05 +00002386 unsigned KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002387 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +00002388 if (KindModifier > OMPC_SCHEDULE_unknown) {
2389 // Parse 'modifier'
2390 Arg[Modifier1] = KindModifier;
2391 KLoc[Modifier1] = Tok.getLocation();
2392 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2393 Tok.isNot(tok::annot_pragma_openmp_end))
2394 ConsumeAnyToken();
2395 if (Tok.is(tok::comma)) {
2396 // Parse ',' 'modifier'
2397 ConsumeAnyToken();
2398 KindModifier = getOpenMPSimpleClauseType(
2399 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2400 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
2401 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +00002402 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002403 KLoc[Modifier2] = Tok.getLocation();
2404 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2405 Tok.isNot(tok::annot_pragma_openmp_end))
2406 ConsumeAnyToken();
2407 }
2408 // Parse ':'
2409 if (Tok.is(tok::colon))
2410 ConsumeAnyToken();
2411 else
2412 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
2413 KindModifier = getOpenMPSimpleClauseType(
2414 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2415 }
2416 Arg[ScheduleKind] = KindModifier;
2417 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002418 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2419 Tok.isNot(tok::annot_pragma_openmp_end))
2420 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +00002421 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
2422 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
2423 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002424 Tok.is(tok::comma))
2425 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +00002426 } else if (Kind == OMPC_dist_schedule) {
2427 Arg.push_back(getOpenMPSimpleClauseType(
2428 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2429 KLoc.push_back(Tok.getLocation());
2430 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2431 Tok.isNot(tok::annot_pragma_openmp_end))
2432 ConsumeAnyToken();
2433 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
2434 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002435 } else if (Kind == OMPC_defaultmap) {
2436 // Get a defaultmap modifier
cchene06f3e02019-11-15 13:02:06 -05002437 unsigned Modifier = getOpenMPSimpleClauseType(
2438 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2439 // Set defaultmap modifier to unknown if it is either scalar, aggregate, or
2440 // pointer
2441 if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown)
2442 Modifier = OMPC_DEFAULTMAP_MODIFIER_unknown;
2443 Arg.push_back(Modifier);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002444 KLoc.push_back(Tok.getLocation());
2445 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2446 Tok.isNot(tok::annot_pragma_openmp_end))
2447 ConsumeAnyToken();
2448 // Parse ':'
2449 if (Tok.is(tok::colon))
2450 ConsumeAnyToken();
2451 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
2452 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
2453 // Get a defaultmap kind
2454 Arg.push_back(getOpenMPSimpleClauseType(
2455 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2456 KLoc.push_back(Tok.getLocation());
2457 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
2458 Tok.isNot(tok::annot_pragma_openmp_end))
2459 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002460 } else {
2461 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +00002462 KLoc.push_back(Tok.getLocation());
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002463 TentativeParsingAction TPA(*this);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002464 auto DK = parseOpenMPDirectiveKind(*this);
2465 Arg.push_back(DK);
2466 if (DK != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002467 ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002468 if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
2469 TPA.Commit();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002470 DelimLoc = ConsumeToken();
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002471 } else {
2472 TPA.Revert();
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06002473 Arg.back() = unsigned(OMPD_unknown);
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002474 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002475 } else {
Alexey Bataev2a6de8c2016-12-20 12:10:05 +00002476 TPA.Revert();
Alexey Bataev61908f652018-04-23 19:53:05 +00002477 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002478 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00002479
Carlo Bertollib4adf552016-01-15 18:50:31 +00002480 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
2481 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
2482 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002483 if (NeedAnExpression) {
2484 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002485 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
2486 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002487 Val =
2488 Actions.ActOnFinishFullExpr(Val.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002489 }
2490
2491 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002492 SourceLocation RLoc = Tok.getLocation();
2493 if (!T.consumeClose())
2494 RLoc = T.getCloseLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +00002495
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002496 if (NeedAnExpression && Val.isInvalid())
2497 return nullptr;
2498
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002499 if (ParseOnly)
2500 return nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00002501 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002502 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc, RLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00002503}
2504
Alexey Bataevc5e02582014-06-16 07:08:35 +00002505static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
2506 UnqualifiedId &ReductionId) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002507 if (ReductionIdScopeSpec.isEmpty()) {
2508 auto OOK = OO_None;
2509 switch (P.getCurToken().getKind()) {
2510 case tok::plus:
2511 OOK = OO_Plus;
2512 break;
2513 case tok::minus:
2514 OOK = OO_Minus;
2515 break;
2516 case tok::star:
2517 OOK = OO_Star;
2518 break;
2519 case tok::amp:
2520 OOK = OO_Amp;
2521 break;
2522 case tok::pipe:
2523 OOK = OO_Pipe;
2524 break;
2525 case tok::caret:
2526 OOK = OO_Caret;
2527 break;
2528 case tok::ampamp:
2529 OOK = OO_AmpAmp;
2530 break;
2531 case tok::pipepipe:
2532 OOK = OO_PipePipe;
2533 break;
2534 default:
2535 break;
2536 }
2537 if (OOK != OO_None) {
2538 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +00002539 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +00002540 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
2541 return false;
2542 }
2543 }
2544 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
2545 /*AllowDestructorName*/ false,
Richard Smith35845152017-02-07 01:37:30 +00002546 /*AllowConstructorName*/ false,
2547 /*AllowDeductionGuide*/ false,
Richard Smithc08b6932018-04-27 02:00:13 +00002548 nullptr, nullptr, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002549}
2550
Kelvin Lief579432018-12-18 22:18:41 +00002551/// Checks if the token is a valid map-type-modifier.
2552static OpenMPMapModifierKind isMapModifier(Parser &P) {
2553 Token Tok = P.getCurToken();
2554 if (!Tok.is(tok::identifier))
2555 return OMPC_MAP_MODIFIER_unknown;
2556
2557 Preprocessor &PP = P.getPreprocessor();
2558 OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
2559 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2560 return TypeModifier;
2561}
2562
Michael Kruse01f670d2019-02-22 22:29:42 +00002563/// Parse the mapper modifier in map, to, and from clauses.
2564bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
2565 // Parse '('.
2566 BalancedDelimiterTracker T(*this, tok::l_paren, tok::colon);
2567 if (T.expectAndConsume(diag::err_expected_lparen_after, "mapper")) {
2568 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2569 StopBeforeMatch);
2570 return true;
2571 }
2572 // Parse mapper-identifier
2573 if (getLangOpts().CPlusPlus)
2574 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
2575 /*ObjectType=*/nullptr,
2576 /*EnteringContext=*/false);
2577 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::kw_default)) {
2578 Diag(Tok.getLocation(), diag::err_omp_mapper_illegal_identifier);
2579 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2580 StopBeforeMatch);
2581 return true;
2582 }
2583 auto &DeclNames = Actions.getASTContext().DeclarationNames;
2584 Data.ReductionOrMapperId = DeclarationNameInfo(
2585 DeclNames.getIdentifier(Tok.getIdentifierInfo()), Tok.getLocation());
2586 ConsumeToken();
2587 // Parse ')'.
2588 return T.consumeClose();
2589}
2590
Kelvin Lief579432018-12-18 22:18:41 +00002591/// Parse map-type-modifiers in map clause.
2592/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002593/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
2594bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
2595 while (getCurToken().isNot(tok::colon)) {
2596 OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
Kelvin Lief579432018-12-18 22:18:41 +00002597 if (TypeModifier == OMPC_MAP_MODIFIER_always ||
2598 TypeModifier == OMPC_MAP_MODIFIER_close) {
2599 Data.MapTypeModifiers.push_back(TypeModifier);
2600 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
Michael Kruse4304e9d2019-02-19 16:38:20 +00002601 ConsumeToken();
2602 } else if (TypeModifier == OMPC_MAP_MODIFIER_mapper) {
2603 Data.MapTypeModifiers.push_back(TypeModifier);
2604 Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
2605 ConsumeToken();
Michael Kruse01f670d2019-02-22 22:29:42 +00002606 if (parseMapperModifier(Data))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002607 return true;
Kelvin Lief579432018-12-18 22:18:41 +00002608 } else {
2609 // For the case of unknown map-type-modifier or a map-type.
2610 // Map-type is followed by a colon; the function returns when it
2611 // encounters a token followed by a colon.
2612 if (Tok.is(tok::comma)) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00002613 Diag(Tok, diag::err_omp_map_type_modifier_missing);
2614 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002615 continue;
2616 }
2617 // Potential map-type token as it is followed by a colon.
2618 if (PP.LookAhead(0).is(tok::colon))
Michael Kruse4304e9d2019-02-19 16:38:20 +00002619 return false;
2620 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
2621 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002622 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002623 if (getCurToken().is(tok::comma))
2624 ConsumeToken();
Kelvin Lief579432018-12-18 22:18:41 +00002625 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00002626 return false;
Kelvin Lief579432018-12-18 22:18:41 +00002627}
2628
2629/// Checks if the token is a valid map-type.
2630static OpenMPMapClauseKind isMapType(Parser &P) {
2631 Token Tok = P.getCurToken();
2632 // The map-type token can be either an identifier or the C++ delete keyword.
2633 if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
2634 return OMPC_MAP_unknown;
2635 Preprocessor &PP = P.getPreprocessor();
2636 OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
2637 getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
2638 return MapType;
2639}
2640
2641/// Parse map-type in map clause.
2642/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002643/// where, map-type ::= to | from | tofrom | alloc | release | delete
Kelvin Lief579432018-12-18 22:18:41 +00002644static void parseMapType(Parser &P, Parser::OpenMPVarListDataTy &Data) {
2645 Token Tok = P.getCurToken();
2646 if (Tok.is(tok::colon)) {
2647 P.Diag(Tok, diag::err_omp_map_type_missing);
2648 return;
2649 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002650 Data.ExtraModifier = isMapType(P);
2651 if (Data.ExtraModifier == OMPC_MAP_unknown)
Kelvin Lief579432018-12-18 22:18:41 +00002652 P.Diag(Tok, diag::err_omp_unknown_map_type);
2653 P.ConsumeToken();
2654}
2655
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002656/// Parses clauses with list.
2657bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
2658 OpenMPClauseKind Kind,
2659 SmallVectorImpl<Expr *> &Vars,
2660 OpenMPVarListDataTy &Data) {
2661 UnqualifiedId UnqualifiedReductionId;
2662 bool InvalidReductionId = false;
Michael Kruse01f670d2019-02-22 22:29:42 +00002663 bool IsInvalidMapperModifier = false;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002664
2665 // Parse '('.
2666 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
2667 if (T.expectAndConsume(diag::err_expected_lparen_after,
2668 getOpenMPClauseName(Kind)))
2669 return true;
2670
2671 bool NeedRParenForLinear = false;
2672 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
2673 tok::annot_pragma_openmp_end);
2674 // Handle reduction-identifier for reduction clause.
Alexey Bataevfa312f32017-07-21 18:48:21 +00002675 if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
2676 Kind == OMPC_in_reduction) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002677 ColonProtectionRAIIObject ColonRAII(*this);
2678 if (getLangOpts().CPlusPlus)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002679 ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002680 /*ObjectType=*/nullptr,
2681 /*EnteringContext=*/false);
Michael Kruse4304e9d2019-02-19 16:38:20 +00002682 InvalidReductionId = ParseReductionId(
2683 *this, Data.ReductionOrMapperIdScopeSpec, UnqualifiedReductionId);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002684 if (InvalidReductionId) {
2685 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2686 StopBeforeMatch);
2687 }
2688 if (Tok.is(tok::colon))
2689 Data.ColonLoc = ConsumeToken();
2690 else
2691 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
2692 if (!InvalidReductionId)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002693 Data.ReductionOrMapperId =
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002694 Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
2695 } else if (Kind == OMPC_depend) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002696 // Handle dependency type for depend clause.
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002697 ColonProtectionRAIIObject ColonRAII(*this);
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002698 Data.ExtraModifier = getOpenMPSimpleClauseType(
2699 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "");
2700 Data.DepLinMapLastLoc = Tok.getLocation();
2701 if (Data.ExtraModifier == OMPC_DEPEND_unknown) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002702 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2703 StopBeforeMatch);
2704 } else {
2705 ConsumeToken();
2706 // Special processing for depend(source) clause.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002707 if (DKind == OMPD_ordered && Data.ExtraModifier == OMPC_DEPEND_source) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002708 // Parse ')'.
2709 T.consumeClose();
2710 return false;
2711 }
2712 }
Alexey Bataev61908f652018-04-23 19:53:05 +00002713 if (Tok.is(tok::colon)) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002714 Data.ColonLoc = ConsumeToken();
Alexey Bataev61908f652018-04-23 19:53:05 +00002715 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002716 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
2717 : diag::warn_pragma_expected_colon)
2718 << "dependency type";
2719 }
2720 } else if (Kind == OMPC_linear) {
2721 // Try to parse modifier if any.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002722 Data.ExtraModifier = OMPC_LINEAR_val;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002723 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002724 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
2725 Data.DepLinMapLastLoc = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002726 LinearT.consumeOpen();
2727 NeedRParenForLinear = true;
2728 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002729 } else if (Kind == OMPC_lastprivate) {
2730 // Try to parse modifier if any.
2731 Data.ExtraModifier = OMPC_LASTPRIVATE_unknown;
2732 // Conditional modifier allowed only in OpenMP 5.0 and not supported in
2733 // distribute and taskloop based directives.
2734 if ((getLangOpts().OpenMP >= 50 && !isOpenMPDistributeDirective(DKind) &&
2735 !isOpenMPTaskLoopDirective(DKind)) &&
2736 Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::colon)) {
2737 Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
2738 Data.DepLinMapLastLoc = Tok.getLocation();
2739 if (Data.ExtraModifier == OMPC_LASTPRIVATE_unknown) {
2740 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2741 StopBeforeMatch);
2742 } else {
2743 ConsumeToken();
2744 }
2745 assert(Tok.is(tok::colon) && "Expected colon.");
2746 Data.ColonLoc = ConsumeToken();
2747 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002748 } else if (Kind == OMPC_map) {
2749 // Handle map type for map clause.
2750 ColonProtectionRAIIObject ColonRAII(*this);
2751
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002752 // The first identifier may be a list item, a map-type or a
Kelvin Lief579432018-12-18 22:18:41 +00002753 // map-type-modifier. The map-type can also be delete which has the same
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002754 // spelling of the C++ delete keyword.
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002755 Data.ExtraModifier = OMPC_MAP_unknown;
2756 Data.DepLinMapLastLoc = Tok.getLocation();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002757
Kelvin Lief579432018-12-18 22:18:41 +00002758 // Check for presence of a colon in the map clause.
2759 TentativeParsingAction TPA(*this);
2760 bool ColonPresent = false;
2761 if (SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2762 StopBeforeMatch)) {
2763 if (Tok.is(tok::colon))
2764 ColonPresent = true;
2765 }
2766 TPA.Revert();
2767 // Only parse map-type-modifier[s] and map-type if a colon is present in
2768 // the map clause.
2769 if (ColonPresent) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002770 IsInvalidMapperModifier = parseMapTypeModifiers(Data);
2771 if (!IsInvalidMapperModifier)
Michael Kruse4304e9d2019-02-19 16:38:20 +00002772 parseMapType(*this, Data);
Michael Kruse01f670d2019-02-22 22:29:42 +00002773 else
2774 SkipUntil(tok::colon, tok::annot_pragma_openmp_end, StopBeforeMatch);
Kelvin Lief579432018-12-18 22:18:41 +00002775 }
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002776 if (Data.ExtraModifier == OMPC_MAP_unknown) {
2777 Data.ExtraModifier = OMPC_MAP_tofrom;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002778 Data.IsMapTypeImplicit = true;
2779 }
2780
2781 if (Tok.is(tok::colon))
2782 Data.ColonLoc = ConsumeToken();
Michael Kruse0336c752019-02-25 20:34:15 +00002783 } else if (Kind == OMPC_to || Kind == OMPC_from) {
Michael Kruse01f670d2019-02-22 22:29:42 +00002784 if (Tok.is(tok::identifier)) {
2785 bool IsMapperModifier = false;
Michael Kruse0336c752019-02-25 20:34:15 +00002786 if (Kind == OMPC_to) {
2787 auto Modifier = static_cast<OpenMPToModifierKind>(
2788 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2789 if (Modifier == OMPC_TO_MODIFIER_mapper)
2790 IsMapperModifier = true;
2791 } else {
2792 auto Modifier = static_cast<OpenMPFromModifierKind>(
2793 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
2794 if (Modifier == OMPC_FROM_MODIFIER_mapper)
2795 IsMapperModifier = true;
2796 }
Michael Kruse01f670d2019-02-22 22:29:42 +00002797 if (IsMapperModifier) {
2798 // Parse the mapper modifier.
2799 ConsumeToken();
2800 IsInvalidMapperModifier = parseMapperModifier(Data);
2801 if (Tok.isNot(tok::colon)) {
2802 if (!IsInvalidMapperModifier)
2803 Diag(Tok, diag::warn_pragma_expected_colon) << ")";
2804 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
2805 StopBeforeMatch);
2806 }
2807 // Consume ':'.
2808 if (Tok.is(tok::colon))
2809 ConsumeToken();
2810 }
2811 }
Alexey Bataeve04483e2019-03-27 14:14:31 +00002812 } else if (Kind == OMPC_allocate) {
2813 // Handle optional allocator expression followed by colon delimiter.
2814 ColonProtectionRAIIObject ColonRAII(*this);
2815 TentativeParsingAction TPA(*this);
2816 ExprResult Tail =
2817 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
2818 Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
2819 /*DiscardedValue=*/false);
2820 if (Tail.isUsable()) {
2821 if (Tok.is(tok::colon)) {
2822 Data.TailExpr = Tail.get();
2823 Data.ColonLoc = ConsumeToken();
2824 TPA.Commit();
2825 } else {
2826 // colon not found, no allocator specified, parse only list of
2827 // variables.
2828 TPA.Revert();
2829 }
2830 } else {
2831 // Parsing was unsuccessfull, revert and skip to the end of clause or
2832 // directive.
2833 TPA.Revert();
2834 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2835 StopBeforeMatch);
2836 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002837 }
2838
Alexey Bataevfa312f32017-07-21 18:48:21 +00002839 bool IsComma =
2840 (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
2841 Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
2842 (Kind == OMPC_reduction && !InvalidReductionId) ||
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002843 (Kind == OMPC_map && Data.ExtraModifier != OMPC_MAP_unknown) ||
2844 (Kind == OMPC_depend && Data.ExtraModifier != OMPC_DEPEND_unknown);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002845 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
2846 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
2847 Tok.isNot(tok::annot_pragma_openmp_end))) {
2848 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
2849 // Parse variable
2850 ExprResult VarExpr =
2851 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev61908f652018-04-23 19:53:05 +00002852 if (VarExpr.isUsable()) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002853 Vars.push_back(VarExpr.get());
Alexey Bataev61908f652018-04-23 19:53:05 +00002854 } else {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002855 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2856 StopBeforeMatch);
2857 }
2858 // Skip ',' if any
2859 IsComma = Tok.is(tok::comma);
2860 if (IsComma)
2861 ConsumeToken();
2862 else if (Tok.isNot(tok::r_paren) &&
2863 Tok.isNot(tok::annot_pragma_openmp_end) &&
2864 (!MayHaveTail || Tok.isNot(tok::colon)))
2865 Diag(Tok, diag::err_omp_expected_punc)
2866 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
2867 : getOpenMPClauseName(Kind))
2868 << (Kind == OMPC_flush);
2869 }
2870
2871 // Parse ')' for linear clause with modifier.
2872 if (NeedRParenForLinear)
2873 LinearT.consumeClose();
2874
2875 // Parse ':' linear-step (or ':' alignment).
2876 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
2877 if (MustHaveTail) {
2878 Data.ColonLoc = Tok.getLocation();
2879 SourceLocation ELoc = ConsumeToken();
2880 ExprResult Tail = ParseAssignmentExpression();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00002881 Tail =
2882 Actions.ActOnFinishFullExpr(Tail.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002883 if (Tail.isUsable())
2884 Data.TailExpr = Tail.get();
2885 else
2886 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
2887 StopBeforeMatch);
2888 }
2889
2890 // Parse ')'.
Alexey Bataevdbc72c92018-07-06 19:35:42 +00002891 Data.RLoc = Tok.getLocation();
2892 if (!T.consumeClose())
2893 Data.RLoc = T.getCloseLocation();
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002894 return (Kind == OMPC_depend && Data.ExtraModifier != OMPC_DEPEND_unknown &&
Alexey Bataev61908f652018-04-23 19:53:05 +00002895 Vars.empty()) ||
2896 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
Michael Kruse4304e9d2019-02-19 16:38:20 +00002897 (MustHaveTail && !Data.TailExpr) || InvalidReductionId ||
Michael Kruse01f670d2019-02-22 22:29:42 +00002898 IsInvalidMapperModifier;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002899}
2900
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002901/// Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataevfa312f32017-07-21 18:48:21 +00002902/// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
2903/// 'in_reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002904///
2905/// private-clause:
2906/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002907/// firstprivate-clause:
2908/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +00002909/// lastprivate-clause:
2910/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +00002911/// shared-clause:
2912/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +00002913/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +00002914/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002915/// aligned-clause:
2916/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +00002917/// reduction-clause:
2918/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev169d96a2017-07-18 20:17:46 +00002919/// task_reduction-clause:
2920/// 'task_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataevfa312f32017-07-21 18:48:21 +00002921/// in_reduction-clause:
2922/// 'in_reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +00002923/// copyprivate-clause:
2924/// 'copyprivate' '(' list ')'
2925/// flush-clause:
2926/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002927/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +00002928/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +00002929/// map-clause:
Kelvin Lief579432018-12-18 22:18:41 +00002930/// 'map' '(' [ [ always [,] ] [ close [,] ]
Michael Kruse01f670d2019-02-22 22:29:42 +00002931/// [ mapper '(' mapper-identifier ')' [,] ]
Kelvin Li0bff7af2015-11-23 05:32:03 +00002932/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Samuel Antao661c0902016-05-26 17:39:58 +00002933/// to-clause:
Michael Kruse01f670d2019-02-22 22:29:42 +00002934/// 'to' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Samuel Antaoec172c62016-05-26 17:49:04 +00002935/// from-clause:
Michael Kruse0336c752019-02-25 20:34:15 +00002936/// 'from' '(' [ mapper '(' mapper-identifier ')' ':' ] list ')'
Carlo Bertolli2404b172016-07-13 15:37:16 +00002937/// use_device_ptr-clause:
2938/// 'use_device_ptr' '(' list ')'
Carlo Bertolli70594e92016-07-13 17:16:49 +00002939/// is_device_ptr-clause:
2940/// 'is_device_ptr' '(' list ')'
Alexey Bataeve04483e2019-03-27 14:14:31 +00002941/// allocate-clause:
2942/// 'allocate' '(' [ allocator ':' ] list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002943///
Alexey Bataev182227b2015-08-20 10:54:39 +00002944/// For 'linear' clause linear-list may have the following forms:
2945/// list
2946/// modifier(list)
2947/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +00002948OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002949 OpenMPClauseKind Kind,
2950 bool ParseOnly) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002951 SourceLocation Loc = Tok.getLocation();
2952 SourceLocation LOpen = ConsumeToken();
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002953 SmallVector<Expr *, 4> Vars;
2954 OpenMPVarListDataTy Data;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002955
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002956 if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
Craig Topper161e4db2014-05-21 06:02:52 +00002957 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002958
Alexey Bataevf3c832a2018-01-09 19:21:04 +00002959 if (ParseOnly)
2960 return nullptr;
Michael Kruse4304e9d2019-02-19 16:38:20 +00002961 OMPVarListLocTy Locs(Loc, LOpen, Data.RLoc);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002962 return Actions.ActOnOpenMPVarListClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00002963 Kind, Vars, Data.TailExpr, Locs, Data.ColonLoc,
Alexey Bataev93dc40d2019-12-20 11:04:57 -05002964 Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId,
2965 Data.ExtraModifier, Data.MapTypeModifiers, Data.MapTypeModifiersLoc,
2966 Data.IsMapTypeImplicit, Data.DepLinMapLastLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002967}
2968