blob: d87abb60204f74f591202a10dc726f3cabcab1ca [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Michael Wong65f367f2015-07-21 13:44:28 +000022
Alexey Bataeva769e072013-03-22 06:34:35 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// OpenMP declarative directives.
27//===----------------------------------------------------------------------===//
28
Dmitry Polukhin82478332016-02-13 06:53:38 +000029namespace {
30enum OpenMPDirectiveKindEx {
31 OMPD_cancellation = OMPD_unknown + 1,
32 OMPD_data,
33 OMPD_enter,
34 OMPD_exit,
35 OMPD_point,
36 OMPD_target_enter,
37 OMPD_target_exit
38};
39} // namespace
40
41// Map token string to extended OMP token kind that are
42// OpenMPDirectiveKind + OpenMPDirectiveKindEx.
43static unsigned getOpenMPDirectiveKindEx(StringRef S) {
44 auto DKind = getOpenMPDirectiveKind(S);
45 if (DKind != OMPD_unknown)
46 return DKind;
47
48 return llvm::StringSwitch<unsigned>(S)
49 .Case("cancellation", OMPD_cancellation)
50 .Case("data", OMPD_data)
51 .Case("enter", OMPD_enter)
52 .Case("exit", OMPD_exit)
53 .Case("point", OMPD_point)
54 .Default(OMPD_unknown);
55}
56
Alexey Bataev4acb8592014-07-07 13:01:15 +000057static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000058 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
59 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
60 // TODO: add other combined directives in topological order.
Dmitry Polukhin82478332016-02-13 06:53:38 +000061 static const unsigned F[][3] = {
62 { OMPD_cancellation, OMPD_point, OMPD_cancellation_point },
63 { OMPD_target, OMPD_data, OMPD_target_data },
64 { OMPD_target, OMPD_enter, OMPD_target_enter },
65 { OMPD_target, OMPD_exit, OMPD_target_exit },
66 { OMPD_target_enter, OMPD_data, OMPD_target_enter_data },
67 { OMPD_target_exit, OMPD_data, OMPD_target_exit_data },
68 { OMPD_for, OMPD_simd, OMPD_for_simd },
69 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
70 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
71 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections },
72 { OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd },
73 { OMPD_target, OMPD_parallel, OMPD_target_parallel },
74 { OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for }
75 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000076 auto Tok = P.getCurToken();
Dmitry Polukhin82478332016-02-13 06:53:38 +000077 unsigned DKind =
Alexey Bataev4acb8592014-07-07 13:01:15 +000078 Tok.isAnnotation()
Dmitry Polukhin82478332016-02-13 06:53:38 +000079 ? static_cast<unsigned>(OMPD_unknown)
80 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
81 if (DKind == OMPD_unknown)
82 return OMPD_unknown;
Michael Wong65f367f2015-07-21 13:44:28 +000083
Alexander Musmanf82886e2014-09-18 05:12:34 +000084 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Dmitry Polukhin82478332016-02-13 06:53:38 +000085 if (DKind != F[i][0])
86 continue;
Michael Wong65f367f2015-07-21 13:44:28 +000087
Dmitry Polukhin82478332016-02-13 06:53:38 +000088 Tok = P.getPreprocessor().LookAhead(0);
89 unsigned SDKind =
90 Tok.isAnnotation()
91 ? static_cast<unsigned>(OMPD_unknown)
92 : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
93 if (SDKind == OMPD_unknown)
94 continue;
Michael Wong65f367f2015-07-21 13:44:28 +000095
Dmitry Polukhin82478332016-02-13 06:53:38 +000096 if (SDKind == F[i][1]) {
97 P.ConsumeToken();
98 DKind = F[i][2];
Alexey Bataev4acb8592014-07-07 13:01:15 +000099 }
100 }
Dmitry Polukhin82478332016-02-13 06:53:38 +0000101 return DKind <= OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
102 : OMPD_unknown;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000103}
104
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000105/// \brief Parsing of declarative OpenMP directives.
106///
107/// threadprivate-directive:
108/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +0000109///
110Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
111 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000112 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000113
114 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000115 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000116 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000117
118 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000119 case OMPD_threadprivate:
120 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000121 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000122 // The last seen token is annot_pragma_openmp_end - need to check for
123 // extra tokens.
124 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
125 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000126 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000127 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000128 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000129 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000130 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000131 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000132 }
133 break;
134 case OMPD_unknown:
135 Diag(Tok, diag::err_omp_unknown_directive);
136 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000137 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000138 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000139 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000140 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000141 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000142 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000143 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000144 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000145 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000146 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000147 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000148 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000149 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000150 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000151 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000152 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000153 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000154 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000155 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000156 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000157 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000158 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000159 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000160 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000161 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000162 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000163 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000164 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000165 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000166 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000167 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000168 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000169 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000170 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000171 break;
172 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000173 SkipUntil(tok::annot_pragma_openmp_end);
David Blaikie0403cb12016-01-15 23:43:25 +0000174 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000175}
176
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177/// \brief Parsing of declarative or executable OpenMP directives.
178///
179/// threadprivate-directive:
180/// annot_pragma_openmp 'threadprivate' simple-variable-list
181/// annot_pragma_openmp_end
182///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000183/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000184/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000185/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
186/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000187/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000188/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000189/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000190/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000191/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000192/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000193///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000194StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
195 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000196 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000197 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000198 SmallVector<Expr *, 5> Identifiers;
199 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000200 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000201 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000202 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000203 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000204 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000205 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000206 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000207 // Name of critical directive.
208 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000209 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000210 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000211 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000212
213 switch (DKind) {
214 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000215 if (Allowed != ACK_Any) {
216 Diag(Tok, diag::err_omp_immediate_directive)
217 << getOpenMPDirectiveName(DKind) << 0;
218 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000219 ConsumeToken();
220 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
221 // The last seen token is annot_pragma_openmp_end - need to check for
222 // extra tokens.
223 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
224 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000225 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000226 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000227 }
228 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000229 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000230 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
231 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000232 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000233 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000234 case OMPD_flush:
235 if (PP.LookAhead(0).is(tok::l_paren)) {
236 FlushHasClause = true;
237 // Push copy of the current token back to stream to properly parse
238 // pseudo-clause OMPFlushClause.
239 PP.EnterToken(Tok);
240 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000241 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000242 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000243 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000244 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000245 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000246 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000247 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000248 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000249 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000250 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000251 }
252 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000253 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000254 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000255 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000256 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000257 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000258 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000259 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000260 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000261 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000262 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000263 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000264 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000265 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000266 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000267 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000268 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000269 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000270 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000271 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000272 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000273 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000274 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000275 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000276 case OMPD_taskloop_simd:
277 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000278 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000279 // Parse directive name of the 'critical' directive if any.
280 if (DKind == OMPD_critical) {
281 BalancedDelimiterTracker T(*this, tok::l_paren,
282 tok::annot_pragma_openmp_end);
283 if (!T.consumeOpen()) {
284 if (Tok.isAnyIdentifier()) {
285 DirName =
286 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
287 ConsumeAnyToken();
288 } else {
289 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
290 }
291 T.consumeClose();
292 }
Alexey Bataev80909872015-07-02 11:25:17 +0000293 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000294 CancelRegion = ParseOpenMPDirectiveKind(*this);
295 if (Tok.isNot(tok::annot_pragma_openmp_end))
296 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000297 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000298
Alexey Bataevf29276e2014-06-18 04:14:57 +0000299 if (isOpenMPLoopDirective(DKind))
300 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
301 if (isOpenMPSimdDirective(DKind))
302 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
303 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000304 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000306 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000307 OpenMPClauseKind CKind =
308 Tok.isAnnotation()
309 ? OMPC_unknown
310 : FlushHasClause ? OMPC_flush
311 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000312 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000313 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000314 OMPClause *Clause =
315 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 FirstClauses[CKind].setInt(true);
317 if (Clause) {
318 FirstClauses[CKind].setPointer(Clause);
319 Clauses.push_back(Clause);
320 }
321
322 // Skip ',' if any.
323 if (Tok.is(tok::comma))
324 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000325 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000326 }
327 // End location of the directive.
328 EndLoc = Tok.getLocation();
329 // Consume final annot_pragma_openmp_end.
330 ConsumeToken();
331
Alexey Bataeveb482352015-12-18 05:05:56 +0000332 // OpenMP [2.13.8, ordered Construct, Syntax]
333 // If the depend clause is specified, the ordered construct is a stand-alone
334 // directive.
335 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000336 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000337 Diag(Loc, diag::err_omp_immediate_directive)
338 << getOpenMPDirectiveName(DKind) << 1
339 << getOpenMPClauseName(OMPC_depend);
340 }
341 HasAssociatedStatement = false;
342 }
343
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000344 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000345 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000346 // The body is a block scope like in Lambdas and Blocks.
347 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000348 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000349 Actions.ActOnStartOfCompoundStmt();
350 // Parse statement
351 AssociatedStmt = ParseStatement();
352 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000353 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000354 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000355 Directive = Actions.ActOnOpenMPExecutableDirective(
356 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
357 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000358
359 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000360 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000361 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000363 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000364 case OMPD_unknown:
365 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000366 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000367 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000368 }
369 return Directive;
370}
371
Alexey Bataeva769e072013-03-22 06:34:35 +0000372/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000373/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000374///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000375/// simple-variable-list:
376/// '(' id-expression {, id-expression} ')'
377///
378bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
379 SmallVectorImpl<Expr *> &VarList,
380 bool AllowScopeSpecifier) {
381 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000382 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000383 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000384 if (T.expectAndConsume(diag::err_expected_lparen_after,
385 getOpenMPDirectiveName(Kind)))
386 return true;
387 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000388 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000389
390 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000391 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000392 CXXScopeSpec SS;
393 SourceLocation TemplateKWLoc;
394 UnqualifiedId Name;
395 // Read var name.
396 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000397 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000398
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000399 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000400 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000401 IsCorrect = false;
402 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000403 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000404 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000405 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000406 IsCorrect = false;
407 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000408 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000409 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
410 Tok.isNot(tok::annot_pragma_openmp_end)) {
411 IsCorrect = false;
412 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000413 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000414 Diag(PrevTok.getLocation(), diag::err_expected)
415 << tok::identifier
416 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000417 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000418 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000419 ExprResult Res =
420 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000421 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000422 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000423 }
424 // Consume ','.
425 if (Tok.is(tok::comma)) {
426 ConsumeToken();
427 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000428 }
429
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000430 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000431 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000432 IsCorrect = false;
433 }
434
435 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000436 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000437
438 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000439}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000440
441/// \brief Parsing of OpenMP clauses.
442///
443/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000444/// if-clause | final-clause | num_threads-clause | safelen-clause |
445/// default-clause | private-clause | firstprivate-clause | shared-clause
446/// | linear-clause | aligned-clause | collapse-clause |
447/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000448/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000449/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000450/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000451/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000452/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000453/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000454///
455OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
456 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000457 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000458 bool ErrorFound = false;
459 // Check if clause is allowed for the given directive.
460 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000461 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
462 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000463 ErrorFound = true;
464 }
465
466 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000467 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000468 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000469 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000470 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000471 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000472 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000473 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000474 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000475 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000476 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000477 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000478 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000479 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000480 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000481 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000482 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000483 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000484 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000485 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000486 // OpenMP [2.9.1, target data construct, Restrictions]
487 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000488 // OpenMP [2.11.1, task Construct, Restrictions]
489 // At most one if clause can appear on the directive.
490 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000491 // OpenMP [teams Construct, Restrictions]
492 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000493 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000494 // OpenMP [2.9.1, task Construct, Restrictions]
495 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000496 // OpenMP [2.9.2, taskloop Construct, Restrictions]
497 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000498 // OpenMP [2.9.2, taskloop Construct, Restrictions]
499 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000500 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000501 Diag(Tok, diag::err_omp_more_one_clause)
502 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000503 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000504 }
505
Alexey Bataev10e775f2015-07-30 11:36:16 +0000506 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
507 Clause = ParseOpenMPClause(CKind);
508 else
509 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000510 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000511 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000512 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000513 // OpenMP [2.14.3.1, Restrictions]
514 // Only a single default clause may be specified on a parallel, task or
515 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000516 // OpenMP [2.5, parallel Construct, Restrictions]
517 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000518 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000519 Diag(Tok, diag::err_omp_more_one_clause)
520 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000521 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000522 }
523
524 Clause = ParseOpenMPSimpleClause(CKind);
525 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000526 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000527 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000528 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000529 // OpenMP [2.7.1, Restrictions, p. 3]
530 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000531 // OpenMP [2.10.4, Restrictions, p. 106]
532 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000533 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000534 Diag(Tok, diag::err_omp_more_one_clause)
535 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000536 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000537 }
538
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000539 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000540 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
541 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000542 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000543 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000544 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000545 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000546 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000547 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000548 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000549 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000550 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000551 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000552 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000553 // OpenMP [2.7.1, Restrictions, p. 9]
554 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000555 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
556 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000557 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000558 Diag(Tok, diag::err_omp_more_one_clause)
559 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000560 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000561 }
562
563 Clause = ParseOpenMPClause(CKind);
564 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000565 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000566 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000567 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000569 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000570 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000571 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000572 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000573 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000574 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000575 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000576 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000577 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000578 break;
579 case OMPC_unknown:
580 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000581 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000582 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000583 break;
584 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000585 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
586 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000587 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000588 break;
589 }
Craig Topper161e4db2014-05-21 06:02:52 +0000590 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591}
592
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000593/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000594/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000595/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000596///
Alexey Bataev3778b602014-07-17 07:32:53 +0000597/// final-clause:
598/// 'final' '(' expression ')'
599///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000600/// num_threads-clause:
601/// 'num_threads' '(' expression ')'
602///
603/// safelen-clause:
604/// 'safelen' '(' expression ')'
605///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000606/// simdlen-clause:
607/// 'simdlen' '(' expression ')'
608///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000609/// collapse-clause:
610/// 'collapse' '(' expression ')'
611///
Alexey Bataeva0569352015-12-01 10:17:31 +0000612/// priority-clause:
613/// 'priority' '(' expression ')'
614///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000615/// grainsize-clause:
616/// 'grainsize' '(' expression ')'
617///
Alexey Bataev382967a2015-12-08 12:06:20 +0000618/// num_tasks-clause:
619/// 'num_tasks' '(' expression ')'
620///
Alexey Bataev28c75412015-12-15 08:19:24 +0000621/// hint-clause:
622/// 'hint' '(' expression ')'
623///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000624OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
625 SourceLocation Loc = ConsumeToken();
626
627 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
628 if (T.expectAndConsume(diag::err_expected_lparen_after,
629 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000630 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000631
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000632 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000633 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
634 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000635 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000636
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000637 // Parse ')'.
638 T.consumeClose();
639
640 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000641 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000642
Alexey Bataeva55ed262014-05-28 06:15:33 +0000643 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000644 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000645}
646
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000647/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000648///
649/// default-clause:
650/// 'default' '(' 'none' | 'shared' ')
651///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000652/// proc_bind-clause:
653/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
654///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000655OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
656 SourceLocation Loc = Tok.getLocation();
657 SourceLocation LOpen = ConsumeToken();
658 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000659 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000660 if (T.expectAndConsume(diag::err_expected_lparen_after,
661 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000662 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000663
Alexey Bataeva55ed262014-05-28 06:15:33 +0000664 unsigned Type = getOpenMPSimpleClauseType(
665 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000666 SourceLocation TypeLoc = Tok.getLocation();
667 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
668 Tok.isNot(tok::annot_pragma_openmp_end))
669 ConsumeAnyToken();
670
671 // Parse ')'.
672 T.consumeClose();
673
674 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
675 Tok.getLocation());
676}
677
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000678/// \brief Parsing of OpenMP clauses like 'ordered'.
679///
680/// ordered-clause:
681/// 'ordered'
682///
Alexey Bataev236070f2014-06-20 11:19:47 +0000683/// nowait-clause:
684/// 'nowait'
685///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000686/// untied-clause:
687/// 'untied'
688///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000689/// mergeable-clause:
690/// 'mergeable'
691///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000692/// read-clause:
693/// 'read'
694///
Alexey Bataev346265e2015-09-25 10:37:12 +0000695/// threads-clause:
696/// 'threads'
697///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000698/// simd-clause:
699/// 'simd'
700///
Alexey Bataevb825de12015-12-07 10:51:44 +0000701/// nogroup-clause:
702/// 'nogroup'
703///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000704OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
705 SourceLocation Loc = Tok.getLocation();
706 ConsumeAnyToken();
707
708 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
709}
710
711
Alexey Bataev56dafe82014-06-20 07:16:17 +0000712/// \brief Parsing of OpenMP clauses with single expressions and some additional
713/// argument like 'schedule' or 'dist_schedule'.
714///
715/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +0000716/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
717/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +0000718///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000719/// if-clause:
720/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
721///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000722/// defaultmap:
723/// 'defaultmap' '(' modifier ':' kind ')'
724///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000725OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
726 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000727 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000728 // Parse '('.
729 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
730 if (T.expectAndConsume(diag::err_expected_lparen_after,
731 getOpenMPClauseName(Kind)))
732 return nullptr;
733
734 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000735 SmallVector<unsigned, 4> Arg;
736 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000737 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000738 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
739 Arg.resize(NumberOfElements);
740 KLoc.resize(NumberOfElements);
741 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
742 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
743 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
744 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000745 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +0000746 if (KindModifier > OMPC_SCHEDULE_unknown) {
747 // Parse 'modifier'
748 Arg[Modifier1] = KindModifier;
749 KLoc[Modifier1] = Tok.getLocation();
750 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
751 Tok.isNot(tok::annot_pragma_openmp_end))
752 ConsumeAnyToken();
753 if (Tok.is(tok::comma)) {
754 // Parse ',' 'modifier'
755 ConsumeAnyToken();
756 KindModifier = getOpenMPSimpleClauseType(
757 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
758 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
759 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +0000760 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000761 KLoc[Modifier2] = Tok.getLocation();
762 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
763 Tok.isNot(tok::annot_pragma_openmp_end))
764 ConsumeAnyToken();
765 }
766 // Parse ':'
767 if (Tok.is(tok::colon))
768 ConsumeAnyToken();
769 else
770 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
771 KindModifier = getOpenMPSimpleClauseType(
772 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
773 }
774 Arg[ScheduleKind] = KindModifier;
775 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000776 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
777 Tok.isNot(tok::annot_pragma_openmp_end))
778 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +0000779 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
780 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
781 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000782 Tok.is(tok::comma))
783 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +0000784 } else if (Kind == OMPC_dist_schedule) {
785 Arg.push_back(getOpenMPSimpleClauseType(
786 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
787 KLoc.push_back(Tok.getLocation());
788 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
789 Tok.isNot(tok::annot_pragma_openmp_end))
790 ConsumeAnyToken();
791 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
792 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000793 } else if (Kind == OMPC_defaultmap) {
794 // Get a defaultmap modifier
795 Arg.push_back(getOpenMPSimpleClauseType(
796 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
797 KLoc.push_back(Tok.getLocation());
798 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
799 Tok.isNot(tok::annot_pragma_openmp_end))
800 ConsumeAnyToken();
801 // Parse ':'
802 if (Tok.is(tok::colon))
803 ConsumeAnyToken();
804 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
805 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
806 // Get a defaultmap kind
807 Arg.push_back(getOpenMPSimpleClauseType(
808 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
809 KLoc.push_back(Tok.getLocation());
810 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
811 Tok.isNot(tok::annot_pragma_openmp_end))
812 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000813 } else {
814 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +0000815 KLoc.push_back(Tok.getLocation());
816 Arg.push_back(ParseOpenMPDirectiveKind(*this));
817 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000818 ConsumeToken();
819 if (Tok.is(tok::colon))
820 DelimLoc = ConsumeToken();
821 else
822 Diag(Tok, diag::warn_pragma_expected_colon)
823 << "directive name modifier";
824 }
825 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000826
Carlo Bertollib4adf552016-01-15 18:50:31 +0000827 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
828 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
829 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000830 if (NeedAnExpression) {
831 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000832 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
833 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000834 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000835 }
836
837 // Parse ')'.
838 T.consumeClose();
839
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000840 if (NeedAnExpression && Val.isInvalid())
841 return nullptr;
842
Alexey Bataev56dafe82014-06-20 07:16:17 +0000843 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000844 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000845 T.getCloseLocation());
846}
847
Alexey Bataevc5e02582014-06-16 07:08:35 +0000848static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
849 UnqualifiedId &ReductionId) {
850 SourceLocation TemplateKWLoc;
851 if (ReductionIdScopeSpec.isEmpty()) {
852 auto OOK = OO_None;
853 switch (P.getCurToken().getKind()) {
854 case tok::plus:
855 OOK = OO_Plus;
856 break;
857 case tok::minus:
858 OOK = OO_Minus;
859 break;
860 case tok::star:
861 OOK = OO_Star;
862 break;
863 case tok::amp:
864 OOK = OO_Amp;
865 break;
866 case tok::pipe:
867 OOK = OO_Pipe;
868 break;
869 case tok::caret:
870 OOK = OO_Caret;
871 break;
872 case tok::ampamp:
873 OOK = OO_AmpAmp;
874 break;
875 case tok::pipepipe:
876 OOK = OO_PipePipe;
877 break;
878 default:
879 break;
880 }
881 if (OOK != OO_None) {
882 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000883 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000884 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
885 return false;
886 }
887 }
888 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
889 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +0000890 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +0000891 TemplateKWLoc, ReductionId);
892}
893
Alexander Musman1bb328c2014-06-04 13:06:39 +0000894/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000895/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000896///
897/// private-clause:
898/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000899/// firstprivate-clause:
900/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000901/// lastprivate-clause:
902/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000903/// shared-clause:
904/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000905/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000906/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000907/// aligned-clause:
908/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000909/// reduction-clause:
910/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000911/// copyprivate-clause:
912/// 'copyprivate' '(' list ')'
913/// flush-clause:
914/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000915/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +0000916/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000917/// map-clause:
918/// 'map' '(' [ [ always , ]
919/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920///
Alexey Bataev182227b2015-08-20 10:54:39 +0000921/// For 'linear' clause linear-list may have the following forms:
922/// list
923/// modifier(list)
924/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +0000925OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
926 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000927 SourceLocation Loc = Tok.getLocation();
928 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000929 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000930 // Optional scope specifier and unqualified id for reduction identifier.
931 CXXScopeSpec ReductionIdScopeSpec;
932 UnqualifiedId ReductionId;
933 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000934 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000935 // OpenMP 4.1 [2.15.3.7, linear Clause]
936 // If no modifier is specified it is assumed to be val.
937 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000938 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
939 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +0000940 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000941 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000942 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000943
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000944 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000945 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000946 if (T.expectAndConsume(diag::err_expected_lparen_after,
947 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000948 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949
Alexey Bataev182227b2015-08-20 10:54:39 +0000950 bool NeedRParenForLinear = false;
951 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
952 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000953 // Handle reduction-identifier for reduction clause.
954 if (Kind == OMPC_reduction) {
955 ColonProtectionRAIIObject ColonRAII(*this);
956 if (getLangOpts().CPlusPlus) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000957 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, nullptr, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000958 }
959 InvalidReductionId =
960 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
961 if (InvalidReductionId) {
962 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
963 StopBeforeMatch);
964 }
965 if (Tok.is(tok::colon)) {
966 ColonLoc = ConsumeToken();
967 } else {
968 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
969 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000970 } else if (Kind == OMPC_depend) {
971 // Handle dependency type for depend clause.
972 ColonProtectionRAIIObject ColonRAII(*this);
973 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
974 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000975 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000976
977 if (DepKind == OMPC_DEPEND_unknown) {
978 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
979 StopBeforeMatch);
980 } else {
981 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +0000982 // Special processing for depend(source) clause.
983 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
984 // Parse ')'.
985 T.consumeClose();
986 return Actions.ActOnOpenMPVarListClause(
987 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
988 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
989 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +0000990 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
991 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +0000992 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000993 }
994 if (Tok.is(tok::colon)) {
995 ColonLoc = ConsumeToken();
996 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +0000997 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
998 : diag::warn_pragma_expected_colon)
999 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001000 }
Alexey Bataev182227b2015-08-20 10:54:39 +00001001 } else if (Kind == OMPC_linear) {
1002 // Try to parse modifier if any.
1003 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +00001004 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +00001005 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001006 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +00001007 LinearT.consumeOpen();
1008 NeedRParenForLinear = true;
1009 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001010 } else if (Kind == OMPC_map) {
1011 // Handle map type for map clause.
1012 ColonProtectionRAIIObject ColonRAII(*this);
1013
Samuel Antaof91b1632016-02-27 00:01:58 +00001014 /// The map clause modifier token can be either a identifier or the C++
1015 /// delete keyword.
1016 auto IsMapClauseModifierToken = [](const Token &Tok) {
1017 return Tok.isOneOf(tok::identifier, tok::kw_delete);
1018 };
1019
1020 // The first identifier may be a list item, a map-type or a
1021 // map-type-modifier. The map modifier can also be delete which has the same
1022 // spelling of the C++ delete keyword.
Kelvin Li0bff7af2015-11-23 05:32:03 +00001023 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001024 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001025 DepLinMapLoc = Tok.getLocation();
1026 bool ColonExpected = false;
1027
Samuel Antaof91b1632016-02-27 00:01:58 +00001028 if (IsMapClauseModifierToken(Tok)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00001029 if (PP.LookAhead(0).is(tok::colon)) {
1030 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001031 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001032 if (MapType == OMPC_MAP_unknown) {
1033 Diag(Tok, diag::err_omp_unknown_map_type);
1034 } else if (MapType == OMPC_MAP_always) {
1035 Diag(Tok, diag::err_omp_map_type_missing);
1036 }
1037 ConsumeToken();
1038 } else if (PP.LookAhead(0).is(tok::comma)) {
Samuel Antaof91b1632016-02-27 00:01:58 +00001039 if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001040 PP.LookAhead(2).is(tok::colon)) {
1041 MapTypeModifier =
1042 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001043 Kind,
1044 IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001045 if (MapTypeModifier != OMPC_MAP_always) {
1046 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1047 MapTypeModifier = OMPC_MAP_unknown;
1048 } else {
1049 MapTypeModifierSpecified = true;
1050 }
1051
1052 ConsumeToken();
1053 ConsumeToken();
1054
1055 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
Samuel Antaof91b1632016-02-27 00:01:58 +00001056 Kind, IsMapClauseModifierToken(Tok) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001057 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1058 Diag(Tok, diag::err_omp_unknown_map_type);
1059 }
1060 ConsumeToken();
1061 } else {
1062 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001063 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001064 }
1065 } else {
1066 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001067 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001068 }
1069 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001070 MapType = OMPC_MAP_tofrom;
1071 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001072 }
1073
1074 if (Tok.is(tok::colon)) {
1075 ColonLoc = ConsumeToken();
1076 } else if (ColonExpected) {
1077 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1078 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001079 }
1080
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001081 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001082 bool IsComma =
1083 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1084 (Kind != OMPC_map)) ||
1085 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001086 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001087 (!MapTypeModifierSpecified ||
1088 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1089 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001090 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001091 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001092 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001093 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001094 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001095 ExprResult VarExpr =
1096 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001097 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001098 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001099 } else {
1100 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001101 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102 }
1103 // Skip ',' if any
1104 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +00001105 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001106 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001107 else if (Tok.isNot(tok::r_paren) &&
1108 Tok.isNot(tok::annot_pragma_openmp_end) &&
1109 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001110 Diag(Tok, diag::err_omp_expected_punc)
1111 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1112 : getOpenMPClauseName(Kind))
1113 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001114 }
1115
Alexey Bataev182227b2015-08-20 10:54:39 +00001116 // Parse ')' for linear clause with modifier.
1117 if (NeedRParenForLinear)
1118 LinearT.consumeClose();
1119
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001120 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001121 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001122 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1123 if (MustHaveTail) {
1124 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001125 SourceLocation ELoc = ConsumeToken();
1126 ExprResult Tail = ParseAssignmentExpression();
1127 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001128 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001129 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001130 else
1131 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1132 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001133 }
1134
1135 // Parse ')'.
1136 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001137 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001138 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1139 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001140 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001141 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001142
Alexey Bataevc5e02582014-06-16 07:08:35 +00001143 return Actions.ActOnOpenMPVarListClause(
1144 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1145 ReductionIdScopeSpec,
1146 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001147 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001148 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1149 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001150}
1151