blob: 2fb53d149cdcbe409042001dda7fe20be95553da [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
Alexey Bataev4acb8592014-07-07 13:01:15 +000029static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000030 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
31 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
32 // TODO: add other combined directives in topological order.
33 const OpenMPDirectiveKind F[][3] = {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000034 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
35 OMPD_cancellation_point},
Michael Wong65f367f2015-07-21 13:44:28 +000036 {OMPD_target, OMPD_unknown /*data*/, OMPD_target_data},
Samuel Antaodf67fc42016-01-19 19:15:56 +000037 {OMPD_target, OMPD_unknown /*enter/exit*/,
38 OMPD_unknown /*target enter/exit*/},
39 {OMPD_unknown /*target enter*/, OMPD_unknown /*data*/,
40 OMPD_target_enter_data},
Samuel Antao72590762016-01-19 20:04:50 +000041 {OMPD_unknown /*target exit*/, OMPD_unknown /*data*/,
42 OMPD_target_exit_data},
Alexey Bataev6d4ed052015-07-01 06:57:41 +000043 {OMPD_for, OMPD_simd, OMPD_for_simd},
44 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
45 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
Alexey Bataev0a6ed842015-12-03 09:40:15 +000046 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +000047 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
48 {OMPD_target, OMPD_parallel, OMPD_target_parallel}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000049 auto Tok = P.getCurToken();
50 auto DKind =
51 Tok.isAnnotation()
52 ? OMPD_unknown
53 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000054
Alexey Bataev6d4ed052015-07-01 06:57:41 +000055 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000056 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000057 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
58 TokenMatched =
Samuel Antaodf67fc42016-01-19 19:15:56 +000059 ((i == 0) &&
60 !P.getPreprocessor().getSpelling(Tok).compare("cancellation")) ||
Samuel Antao72590762016-01-19 20:04:50 +000061 ((i == 3) &&
62 !P.getPreprocessor().getSpelling(Tok).compare("enter")) ||
63 ((i == 4) && !P.getPreprocessor().getSpelling(Tok).compare("exit"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000064 } else {
65 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
66 }
Michael Wong65f367f2015-07-21 13:44:28 +000067
Alexey Bataev6d4ed052015-07-01 06:57:41 +000068 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000069 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000070 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000071 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000072 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000073 ? OMPD_unknown
74 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000075
76 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000077 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000078 ((i == 0) &&
79 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
Samuel Antao72590762016-01-19 20:04:50 +000080 ((i == 1 || i == 3 || i == 4) &&
Samuel Antaodf67fc42016-01-19 19:15:56 +000081 !P.getPreprocessor().getSpelling(Tok).compare("data")) ||
82 ((i == 2) &&
Samuel Antao72590762016-01-19 20:04:50 +000083 (!P.getPreprocessor().getSpelling(Tok).compare("enter") ||
84 !P.getPreprocessor().getSpelling(Tok).compare("exit")));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000085 } else {
86 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
87 }
Michael Wong65f367f2015-07-21 13:44:28 +000088
Alexey Bataev6d4ed052015-07-01 06:57:41 +000089 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000090 P.ConsumeToken();
91 DKind = F[i][2];
92 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000093 }
94 }
95 return DKind;
96}
97
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000098/// \brief Parsing of declarative OpenMP directives.
99///
100/// threadprivate-directive:
101/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +0000102///
103Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
104 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000105 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000106
107 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000108 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000109 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000110
111 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000112 case OMPD_threadprivate:
113 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000114 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000115 // The last seen token is annot_pragma_openmp_end - need to check for
116 // extra tokens.
117 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
118 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000119 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000120 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000121 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000122 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000123 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000124 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000125 }
126 break;
127 case OMPD_unknown:
128 Diag(Tok, diag::err_omp_unknown_directive);
129 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000130 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000131 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000132 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000133 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000134 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000135 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000136 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000137 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000138 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000139 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000140 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000141 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000142 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000143 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000144 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000145 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000146 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000147 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000148 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000149 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000150 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000151 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000152 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000153 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000154 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000155 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000156 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000157 case OMPD_target_parallel:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000158 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000159 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000160 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000161 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000162 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000163 break;
164 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000165 SkipUntil(tok::annot_pragma_openmp_end);
David Blaikie0403cb12016-01-15 23:43:25 +0000166 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000167}
168
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000169/// \brief Parsing of declarative or executable OpenMP directives.
170///
171/// threadprivate-directive:
172/// annot_pragma_openmp 'threadprivate' simple-variable-list
173/// annot_pragma_openmp_end
174///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000175/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000176/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000177/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
178/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000179/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000180/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000181/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' {clause} |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000182/// 'distribute' | 'target enter data' | 'target exit data' |
183/// 'target parallel'
Samuel Antao72590762016-01-19 20:04:50 +0000184/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000185///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000186StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
187 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000188 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000189 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000190 SmallVector<Expr *, 5> Identifiers;
191 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000192 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000193 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000194 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000195 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000196 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000197 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000198 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199 // Name of critical directive.
200 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000201 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000202 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000203 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000204
205 switch (DKind) {
206 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000207 if (Allowed != ACK_Any) {
208 Diag(Tok, diag::err_omp_immediate_directive)
209 << getOpenMPDirectiveName(DKind) << 0;
210 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000211 ConsumeToken();
212 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
213 // The last seen token is annot_pragma_openmp_end - need to check for
214 // extra tokens.
215 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
216 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000217 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000218 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000219 }
220 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000221 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000222 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
223 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000224 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000225 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000226 case OMPD_flush:
227 if (PP.LookAhead(0).is(tok::l_paren)) {
228 FlushHasClause = true;
229 // Push copy of the current token back to stream to properly parse
230 // pseudo-clause OMPFlushClause.
231 PP.EnterToken(Tok);
232 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000233 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000234 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000235 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000236 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000237 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000238 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000239 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000240 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000241 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000242 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000243 }
244 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000245 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000246 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000247 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000248 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000249 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000250 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000251 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000252 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000253 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000254 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000255 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000256 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000257 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000258 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000259 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000260 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000261 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000262 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000263 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000264 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000265 case OMPD_target_parallel:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000266 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000267 case OMPD_taskloop_simd:
268 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000269 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000270 // Parse directive name of the 'critical' directive if any.
271 if (DKind == OMPD_critical) {
272 BalancedDelimiterTracker T(*this, tok::l_paren,
273 tok::annot_pragma_openmp_end);
274 if (!T.consumeOpen()) {
275 if (Tok.isAnyIdentifier()) {
276 DirName =
277 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
278 ConsumeAnyToken();
279 } else {
280 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
281 }
282 T.consumeClose();
283 }
Alexey Bataev80909872015-07-02 11:25:17 +0000284 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000285 CancelRegion = ParseOpenMPDirectiveKind(*this);
286 if (Tok.isNot(tok::annot_pragma_openmp_end))
287 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000288 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289
Alexey Bataevf29276e2014-06-18 04:14:57 +0000290 if (isOpenMPLoopDirective(DKind))
291 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
292 if (isOpenMPSimdDirective(DKind))
293 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
294 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000295 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000296
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000298 OpenMPClauseKind CKind =
299 Tok.isAnnotation()
300 ? OMPC_unknown
301 : FlushHasClause ? OMPC_flush
302 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000303 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000304 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000305 OMPClause *Clause =
306 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000307 FirstClauses[CKind].setInt(true);
308 if (Clause) {
309 FirstClauses[CKind].setPointer(Clause);
310 Clauses.push_back(Clause);
311 }
312
313 // Skip ',' if any.
314 if (Tok.is(tok::comma))
315 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000316 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000317 }
318 // End location of the directive.
319 EndLoc = Tok.getLocation();
320 // Consume final annot_pragma_openmp_end.
321 ConsumeToken();
322
Alexey Bataeveb482352015-12-18 05:05:56 +0000323 // OpenMP [2.13.8, ordered Construct, Syntax]
324 // If the depend clause is specified, the ordered construct is a stand-alone
325 // directive.
326 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000327 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000328 Diag(Loc, diag::err_omp_immediate_directive)
329 << getOpenMPDirectiveName(DKind) << 1
330 << getOpenMPClauseName(OMPC_depend);
331 }
332 HasAssociatedStatement = false;
333 }
334
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000335 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000336 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000337 // The body is a block scope like in Lambdas and Blocks.
338 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000339 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000340 Actions.ActOnStartOfCompoundStmt();
341 // Parse statement
342 AssociatedStmt = ParseStatement();
343 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000344 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000345 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000346 Directive = Actions.ActOnOpenMPExecutableDirective(
347 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
348 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000349
350 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000351 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000352 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000353 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000354 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000355 case OMPD_unknown:
356 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000357 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000358 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000359 }
360 return Directive;
361}
362
Alexey Bataeva769e072013-03-22 06:34:35 +0000363/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000364/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000365///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000366/// simple-variable-list:
367/// '(' id-expression {, id-expression} ')'
368///
369bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
370 SmallVectorImpl<Expr *> &VarList,
371 bool AllowScopeSpecifier) {
372 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000373 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000374 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
376 getOpenMPDirectiveName(Kind)))
377 return true;
378 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000379 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000380
381 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000382 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000383 CXXScopeSpec SS;
384 SourceLocation TemplateKWLoc;
385 UnqualifiedId Name;
386 // Read var name.
387 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000388 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000389
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000390 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000391 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000392 IsCorrect = false;
393 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000394 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000395 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000396 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000397 IsCorrect = false;
398 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000399 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000400 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
401 Tok.isNot(tok::annot_pragma_openmp_end)) {
402 IsCorrect = false;
403 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000404 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000405 Diag(PrevTok.getLocation(), diag::err_expected)
406 << tok::identifier
407 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000408 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000409 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000410 ExprResult Res =
411 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000412 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000413 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000414 }
415 // Consume ','.
416 if (Tok.is(tok::comma)) {
417 ConsumeToken();
418 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000419 }
420
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000421 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000422 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000423 IsCorrect = false;
424 }
425
426 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000427 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000428
429 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000430}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000431
432/// \brief Parsing of OpenMP clauses.
433///
434/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000435/// if-clause | final-clause | num_threads-clause | safelen-clause |
436/// default-clause | private-clause | firstprivate-clause | shared-clause
437/// | linear-clause | aligned-clause | collapse-clause |
438/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000439/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000440/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000441/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000442/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000443/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000444/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000445///
446OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
447 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000448 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000449 bool ErrorFound = false;
450 // Check if clause is allowed for the given directive.
451 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000452 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
453 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000454 ErrorFound = true;
455 }
456
457 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000458 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000459 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000460 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000461 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000462 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000463 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000464 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000465 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000466 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000467 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000468 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000469 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000470 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000471 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000472 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000473 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000474 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000475 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000476 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000477 // OpenMP [2.9.1, target data construct, Restrictions]
478 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000479 // OpenMP [2.11.1, task Construct, Restrictions]
480 // At most one if clause can appear on the directive.
481 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000482 // OpenMP [teams Construct, Restrictions]
483 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000484 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000485 // OpenMP [2.9.1, task Construct, Restrictions]
486 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000487 // OpenMP [2.9.2, taskloop Construct, Restrictions]
488 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000489 // OpenMP [2.9.2, taskloop Construct, Restrictions]
490 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000491 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000492 Diag(Tok, diag::err_omp_more_one_clause)
493 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000494 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000495 }
496
Alexey Bataev10e775f2015-07-30 11:36:16 +0000497 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
498 Clause = ParseOpenMPClause(CKind);
499 else
500 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000501 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000502 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000503 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000504 // OpenMP [2.14.3.1, Restrictions]
505 // Only a single default clause may be specified on a parallel, task or
506 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000507 // OpenMP [2.5, parallel Construct, Restrictions]
508 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000509 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000510 Diag(Tok, diag::err_omp_more_one_clause)
511 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000512 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000513 }
514
515 Clause = ParseOpenMPSimpleClause(CKind);
516 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000517 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000518 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000519 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000520 // OpenMP [2.7.1, Restrictions, p. 3]
521 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000522 // OpenMP [2.10.4, Restrictions, p. 106]
523 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000524 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000525 Diag(Tok, diag::err_omp_more_one_clause)
526 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000527 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000528 }
529
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000530 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000531 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
532 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000533 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000534 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000535 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000536 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000537 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000538 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000539 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000540 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000541 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000542 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000543 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000544 // OpenMP [2.7.1, Restrictions, p. 9]
545 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000546 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
547 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000548 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000549 Diag(Tok, diag::err_omp_more_one_clause)
550 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000551 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000552 }
553
554 Clause = ParseOpenMPClause(CKind);
555 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000556 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000557 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000558 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000560 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000561 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000562 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000563 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000564 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000565 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000566 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000567 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000568 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000569 break;
570 case OMPC_unknown:
571 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000572 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000573 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000574 break;
575 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000576 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
577 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000578 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000579 break;
580 }
Craig Topper161e4db2014-05-21 06:02:52 +0000581 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000582}
583
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000584/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000585/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000586/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000587///
Alexey Bataev3778b602014-07-17 07:32:53 +0000588/// final-clause:
589/// 'final' '(' expression ')'
590///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000591/// num_threads-clause:
592/// 'num_threads' '(' expression ')'
593///
594/// safelen-clause:
595/// 'safelen' '(' expression ')'
596///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000597/// simdlen-clause:
598/// 'simdlen' '(' expression ')'
599///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000600/// collapse-clause:
601/// 'collapse' '(' expression ')'
602///
Alexey Bataeva0569352015-12-01 10:17:31 +0000603/// priority-clause:
604/// 'priority' '(' expression ')'
605///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000606/// grainsize-clause:
607/// 'grainsize' '(' expression ')'
608///
Alexey Bataev382967a2015-12-08 12:06:20 +0000609/// num_tasks-clause:
610/// 'num_tasks' '(' expression ')'
611///
Alexey Bataev28c75412015-12-15 08:19:24 +0000612/// hint-clause:
613/// 'hint' '(' expression ')'
614///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000615OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
616 SourceLocation Loc = ConsumeToken();
617
618 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
619 if (T.expectAndConsume(diag::err_expected_lparen_after,
620 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000621 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000622
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000623 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000624 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
625 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000626 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000627
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000628 // Parse ')'.
629 T.consumeClose();
630
631 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000632 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000633
Alexey Bataeva55ed262014-05-28 06:15:33 +0000634 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000635 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000636}
637
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000638/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000639///
640/// default-clause:
641/// 'default' '(' 'none' | 'shared' ')
642///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000643/// proc_bind-clause:
644/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
645///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000646OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
647 SourceLocation Loc = Tok.getLocation();
648 SourceLocation LOpen = ConsumeToken();
649 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000650 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000651 if (T.expectAndConsume(diag::err_expected_lparen_after,
652 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000653 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000654
Alexey Bataeva55ed262014-05-28 06:15:33 +0000655 unsigned Type = getOpenMPSimpleClauseType(
656 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000657 SourceLocation TypeLoc = Tok.getLocation();
658 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
659 Tok.isNot(tok::annot_pragma_openmp_end))
660 ConsumeAnyToken();
661
662 // Parse ')'.
663 T.consumeClose();
664
665 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
666 Tok.getLocation());
667}
668
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000669/// \brief Parsing of OpenMP clauses like 'ordered'.
670///
671/// ordered-clause:
672/// 'ordered'
673///
Alexey Bataev236070f2014-06-20 11:19:47 +0000674/// nowait-clause:
675/// 'nowait'
676///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000677/// untied-clause:
678/// 'untied'
679///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000680/// mergeable-clause:
681/// 'mergeable'
682///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000683/// read-clause:
684/// 'read'
685///
Alexey Bataev346265e2015-09-25 10:37:12 +0000686/// threads-clause:
687/// 'threads'
688///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000689/// simd-clause:
690/// 'simd'
691///
Alexey Bataevb825de12015-12-07 10:51:44 +0000692/// nogroup-clause:
693/// 'nogroup'
694///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000695OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
696 SourceLocation Loc = Tok.getLocation();
697 ConsumeAnyToken();
698
699 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
700}
701
702
Alexey Bataev56dafe82014-06-20 07:16:17 +0000703/// \brief Parsing of OpenMP clauses with single expressions and some additional
704/// argument like 'schedule' or 'dist_schedule'.
705///
706/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +0000707/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
708/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +0000709///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000710/// if-clause:
711/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
712///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000713/// defaultmap:
714/// 'defaultmap' '(' modifier ':' kind ')'
715///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000716OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
717 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000718 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000719 // Parse '('.
720 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
721 if (T.expectAndConsume(diag::err_expected_lparen_after,
722 getOpenMPClauseName(Kind)))
723 return nullptr;
724
725 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000726 SmallVector<unsigned, 4> Arg;
727 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000728 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000729 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
730 Arg.resize(NumberOfElements);
731 KLoc.resize(NumberOfElements);
732 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
733 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
734 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
735 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000736 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +0000737 if (KindModifier > OMPC_SCHEDULE_unknown) {
738 // Parse 'modifier'
739 Arg[Modifier1] = KindModifier;
740 KLoc[Modifier1] = Tok.getLocation();
741 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
742 Tok.isNot(tok::annot_pragma_openmp_end))
743 ConsumeAnyToken();
744 if (Tok.is(tok::comma)) {
745 // Parse ',' 'modifier'
746 ConsumeAnyToken();
747 KindModifier = getOpenMPSimpleClauseType(
748 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
749 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
750 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +0000751 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000752 KLoc[Modifier2] = Tok.getLocation();
753 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
754 Tok.isNot(tok::annot_pragma_openmp_end))
755 ConsumeAnyToken();
756 }
757 // Parse ':'
758 if (Tok.is(tok::colon))
759 ConsumeAnyToken();
760 else
761 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
762 KindModifier = getOpenMPSimpleClauseType(
763 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
764 }
765 Arg[ScheduleKind] = KindModifier;
766 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000767 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
768 Tok.isNot(tok::annot_pragma_openmp_end))
769 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +0000770 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
771 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
772 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000773 Tok.is(tok::comma))
774 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +0000775 } else if (Kind == OMPC_dist_schedule) {
776 Arg.push_back(getOpenMPSimpleClauseType(
777 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
778 KLoc.push_back(Tok.getLocation());
779 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
780 Tok.isNot(tok::annot_pragma_openmp_end))
781 ConsumeAnyToken();
782 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
783 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000784 } else if (Kind == OMPC_defaultmap) {
785 // Get a defaultmap modifier
786 Arg.push_back(getOpenMPSimpleClauseType(
787 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
788 KLoc.push_back(Tok.getLocation());
789 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
790 Tok.isNot(tok::annot_pragma_openmp_end))
791 ConsumeAnyToken();
792 // Parse ':'
793 if (Tok.is(tok::colon))
794 ConsumeAnyToken();
795 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
796 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
797 // Get a defaultmap kind
798 Arg.push_back(getOpenMPSimpleClauseType(
799 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
800 KLoc.push_back(Tok.getLocation());
801 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
802 Tok.isNot(tok::annot_pragma_openmp_end))
803 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000804 } else {
805 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +0000806 KLoc.push_back(Tok.getLocation());
807 Arg.push_back(ParseOpenMPDirectiveKind(*this));
808 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000809 ConsumeToken();
810 if (Tok.is(tok::colon))
811 DelimLoc = ConsumeToken();
812 else
813 Diag(Tok, diag::warn_pragma_expected_colon)
814 << "directive name modifier";
815 }
816 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000817
Carlo Bertollib4adf552016-01-15 18:50:31 +0000818 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
819 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
820 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000821 if (NeedAnExpression) {
822 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000823 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
824 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000825 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000826 }
827
828 // Parse ')'.
829 T.consumeClose();
830
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000831 if (NeedAnExpression && Val.isInvalid())
832 return nullptr;
833
Alexey Bataev56dafe82014-06-20 07:16:17 +0000834 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000835 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000836 T.getCloseLocation());
837}
838
Alexey Bataevc5e02582014-06-16 07:08:35 +0000839static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
840 UnqualifiedId &ReductionId) {
841 SourceLocation TemplateKWLoc;
842 if (ReductionIdScopeSpec.isEmpty()) {
843 auto OOK = OO_None;
844 switch (P.getCurToken().getKind()) {
845 case tok::plus:
846 OOK = OO_Plus;
847 break;
848 case tok::minus:
849 OOK = OO_Minus;
850 break;
851 case tok::star:
852 OOK = OO_Star;
853 break;
854 case tok::amp:
855 OOK = OO_Amp;
856 break;
857 case tok::pipe:
858 OOK = OO_Pipe;
859 break;
860 case tok::caret:
861 OOK = OO_Caret;
862 break;
863 case tok::ampamp:
864 OOK = OO_AmpAmp;
865 break;
866 case tok::pipepipe:
867 OOK = OO_PipePipe;
868 break;
869 default:
870 break;
871 }
872 if (OOK != OO_None) {
873 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000874 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000875 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
876 return false;
877 }
878 }
879 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
880 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +0000881 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +0000882 TemplateKWLoc, ReductionId);
883}
884
Alexander Musman1bb328c2014-06-04 13:06:39 +0000885/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000886/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000887///
888/// private-clause:
889/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000890/// firstprivate-clause:
891/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000892/// lastprivate-clause:
893/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894/// shared-clause:
895/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000896/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000897/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000898/// aligned-clause:
899/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000900/// reduction-clause:
901/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000902/// copyprivate-clause:
903/// 'copyprivate' '(' list ')'
904/// flush-clause:
905/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000906/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +0000907/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000908/// map-clause:
909/// 'map' '(' [ [ always , ]
910/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911///
Alexey Bataev182227b2015-08-20 10:54:39 +0000912/// For 'linear' clause linear-list may have the following forms:
913/// list
914/// modifier(list)
915/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +0000916OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
917 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000918 SourceLocation Loc = Tok.getLocation();
919 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000920 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000921 // Optional scope specifier and unqualified id for reduction identifier.
922 CXXScopeSpec ReductionIdScopeSpec;
923 UnqualifiedId ReductionId;
924 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000925 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000926 // OpenMP 4.1 [2.15.3.7, linear Clause]
927 // If no modifier is specified it is assumed to be val.
928 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000929 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
930 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +0000931 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000932 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000933 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000934
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000936 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000937 if (T.expectAndConsume(diag::err_expected_lparen_after,
938 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000939 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940
Alexey Bataev182227b2015-08-20 10:54:39 +0000941 bool NeedRParenForLinear = false;
942 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
943 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000944 // Handle reduction-identifier for reduction clause.
945 if (Kind == OMPC_reduction) {
946 ColonProtectionRAIIObject ColonRAII(*this);
947 if (getLangOpts().CPlusPlus) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000948 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, nullptr, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000949 }
950 InvalidReductionId =
951 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
952 if (InvalidReductionId) {
953 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
954 StopBeforeMatch);
955 }
956 if (Tok.is(tok::colon)) {
957 ColonLoc = ConsumeToken();
958 } else {
959 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
960 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000961 } else if (Kind == OMPC_depend) {
962 // Handle dependency type for depend clause.
963 ColonProtectionRAIIObject ColonRAII(*this);
964 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
965 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000966 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000967
968 if (DepKind == OMPC_DEPEND_unknown) {
969 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
970 StopBeforeMatch);
971 } else {
972 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +0000973 // Special processing for depend(source) clause.
974 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
975 // Parse ')'.
976 T.consumeClose();
977 return Actions.ActOnOpenMPVarListClause(
978 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
979 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
980 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +0000981 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
982 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +0000983 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000984 }
985 if (Tok.is(tok::colon)) {
986 ColonLoc = ConsumeToken();
987 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +0000988 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
989 : diag::warn_pragma_expected_colon)
990 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000991 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000992 } else if (Kind == OMPC_linear) {
993 // Try to parse modifier if any.
994 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000995 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000996 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000997 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000998 LinearT.consumeOpen();
999 NeedRParenForLinear = true;
1000 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001001 } else if (Kind == OMPC_map) {
1002 // Handle map type for map clause.
1003 ColonProtectionRAIIObject ColonRAII(*this);
1004
1005 // the first identifier may be a list item, a map-type or
1006 // a map-type-modifier
1007 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1008 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1009 DepLinMapLoc = Tok.getLocation();
1010 bool ColonExpected = false;
1011
1012 if (Tok.is(tok::identifier)) {
1013 if (PP.LookAhead(0).is(tok::colon)) {
1014 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1015 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1016 if (MapType == OMPC_MAP_unknown) {
1017 Diag(Tok, diag::err_omp_unknown_map_type);
1018 } else if (MapType == OMPC_MAP_always) {
1019 Diag(Tok, diag::err_omp_map_type_missing);
1020 }
1021 ConsumeToken();
1022 } else if (PP.LookAhead(0).is(tok::comma)) {
1023 if (PP.LookAhead(1).is(tok::identifier) &&
1024 PP.LookAhead(2).is(tok::colon)) {
1025 MapTypeModifier =
1026 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1027 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1028 if (MapTypeModifier != OMPC_MAP_always) {
1029 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1030 MapTypeModifier = OMPC_MAP_unknown;
1031 } else {
1032 MapTypeModifierSpecified = true;
1033 }
1034
1035 ConsumeToken();
1036 ConsumeToken();
1037
1038 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1039 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1040 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1041 Diag(Tok, diag::err_omp_unknown_map_type);
1042 }
1043 ConsumeToken();
1044 } else {
1045 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001046 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001047 }
1048 } else {
1049 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001050 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001051 }
1052 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001053 MapType = OMPC_MAP_tofrom;
1054 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001055 }
1056
1057 if (Tok.is(tok::colon)) {
1058 ColonLoc = ConsumeToken();
1059 } else if (ColonExpected) {
1060 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1061 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001062 }
1063
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001064 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001065 bool IsComma =
1066 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1067 (Kind != OMPC_map)) ||
1068 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001069 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001070 (!MapTypeModifierSpecified ||
1071 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1072 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001073 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001074 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001075 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001076 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001077 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001078 ExprResult VarExpr =
1079 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001081 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082 } else {
1083 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001084 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085 }
1086 // Skip ',' if any
1087 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +00001088 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001090 else if (Tok.isNot(tok::r_paren) &&
1091 Tok.isNot(tok::annot_pragma_openmp_end) &&
1092 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001093 Diag(Tok, diag::err_omp_expected_punc)
1094 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1095 : getOpenMPClauseName(Kind))
1096 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001097 }
1098
Alexey Bataev182227b2015-08-20 10:54:39 +00001099 // Parse ')' for linear clause with modifier.
1100 if (NeedRParenForLinear)
1101 LinearT.consumeClose();
1102
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001103 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001104 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001105 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1106 if (MustHaveTail) {
1107 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001108 SourceLocation ELoc = ConsumeToken();
1109 ExprResult Tail = ParseAssignmentExpression();
1110 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001111 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001112 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001113 else
1114 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1115 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001116 }
1117
1118 // Parse ')'.
1119 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001120 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001121 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1122 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001123 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001124 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001125
Alexey Bataevc5e02582014-06-16 07:08:35 +00001126 return Actions.ActOnOpenMPVarListClause(
1127 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1128 ReductionIdScopeSpec,
1129 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001130 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001131 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1132 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001133}
1134