blob: 25d5935b171437d02b1acec97e4afc4fc9130aa9 [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},
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +000048 {OMPD_target, OMPD_parallel, OMPD_target_parallel},
49 {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000050 auto Tok = P.getCurToken();
51 auto DKind =
52 Tok.isAnnotation()
53 ? OMPD_unknown
54 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000055
Alexey Bataev6d4ed052015-07-01 06:57:41 +000056 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000057 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000058 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
59 TokenMatched =
Samuel Antaodf67fc42016-01-19 19:15:56 +000060 ((i == 0) &&
61 !P.getPreprocessor().getSpelling(Tok).compare("cancellation")) ||
Samuel Antao72590762016-01-19 20:04:50 +000062 ((i == 3) &&
63 !P.getPreprocessor().getSpelling(Tok).compare("enter")) ||
64 ((i == 4) && !P.getPreprocessor().getSpelling(Tok).compare("exit"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000065 } else {
66 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
67 }
Michael Wong65f367f2015-07-21 13:44:28 +000068
Alexey Bataev6d4ed052015-07-01 06:57:41 +000069 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000070 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000071 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000072 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000073 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000074 ? OMPD_unknown
75 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000076
77 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000078 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000079 ((i == 0) &&
80 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
Samuel Antao72590762016-01-19 20:04:50 +000081 ((i == 1 || i == 3 || i == 4) &&
Samuel Antaodf67fc42016-01-19 19:15:56 +000082 !P.getPreprocessor().getSpelling(Tok).compare("data")) ||
83 ((i == 2) &&
Samuel Antao72590762016-01-19 20:04:50 +000084 (!P.getPreprocessor().getSpelling(Tok).compare("enter") ||
85 !P.getPreprocessor().getSpelling(Tok).compare("exit")));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000086 } else {
87 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
88 }
Michael Wong65f367f2015-07-21 13:44:28 +000089
Alexey Bataev6d4ed052015-07-01 06:57:41 +000090 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000091 P.ConsumeToken();
92 DKind = F[i][2];
93 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000094 }
95 }
96 return DKind;
97}
98
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000099/// \brief Parsing of declarative OpenMP directives.
100///
101/// threadprivate-directive:
102/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +0000103///
104Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
105 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000106 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000107
108 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000109 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000110 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000111
112 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000113 case OMPD_threadprivate:
114 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000115 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000116 // The last seen token is annot_pragma_openmp_end - need to check for
117 // extra tokens.
118 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
119 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000120 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000121 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000122 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000123 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000124 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000125 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000126 }
127 break;
128 case OMPD_unknown:
129 Diag(Tok, diag::err_omp_unknown_directive);
130 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000131 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000132 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000133 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000134 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000135 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000136 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000137 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000138 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000139 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000140 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000141 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000142 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000143 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000144 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000145 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000146 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000147 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000148 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000149 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000150 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000151 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000152 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000153 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000154 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000155 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000156 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000157 case OMPD_target_exit_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000158 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000159 case OMPD_target_parallel_for:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000160 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000161 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000162 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000163 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000164 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000165 break;
166 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000167 SkipUntil(tok::annot_pragma_openmp_end);
David Blaikie0403cb12016-01-15 23:43:25 +0000168 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000169}
170
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000171/// \brief Parsing of declarative or executable OpenMP directives.
172///
173/// threadprivate-directive:
174/// annot_pragma_openmp 'threadprivate' simple-variable-list
175/// annot_pragma_openmp_end
176///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000177/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000178/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000179/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
180/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000181/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000182/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000183/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000184/// 'distribute' | 'target enter data' | 'target exit data' |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000185/// 'target parallel' | 'target parallel for' {clause}
Samuel Antao72590762016-01-19 20:04:50 +0000186/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000187///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000188StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
189 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000190 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000191 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000192 SmallVector<Expr *, 5> Identifiers;
193 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000194 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000195 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000196 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000197 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000198 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000199 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000200 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000201 // Name of critical directive.
202 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000203 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000204 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000205 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000206
207 switch (DKind) {
208 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000209 if (Allowed != ACK_Any) {
210 Diag(Tok, diag::err_omp_immediate_directive)
211 << getOpenMPDirectiveName(DKind) << 0;
212 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000213 ConsumeToken();
214 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
215 // The last seen token is annot_pragma_openmp_end - need to check for
216 // extra tokens.
217 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
218 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000219 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000220 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000221 }
222 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000223 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000224 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
225 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000226 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000227 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000228 case OMPD_flush:
229 if (PP.LookAhead(0).is(tok::l_paren)) {
230 FlushHasClause = true;
231 // Push copy of the current token back to stream to properly parse
232 // pseudo-clause OMPFlushClause.
233 PP.EnterToken(Tok);
234 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000235 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000236 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000237 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000238 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000239 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000240 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000241 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000242 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000243 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000244 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000245 }
246 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000247 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000248 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000249 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000250 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000251 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000252 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000253 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000254 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000255 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000256 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000257 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000258 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000259 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000260 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000261 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000262 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000263 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000264 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000265 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000266 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000267 case OMPD_target_parallel:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000268 case OMPD_target_parallel_for:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000269 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000270 case OMPD_taskloop_simd:
271 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000272 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000273 // Parse directive name of the 'critical' directive if any.
274 if (DKind == OMPD_critical) {
275 BalancedDelimiterTracker T(*this, tok::l_paren,
276 tok::annot_pragma_openmp_end);
277 if (!T.consumeOpen()) {
278 if (Tok.isAnyIdentifier()) {
279 DirName =
280 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
281 ConsumeAnyToken();
282 } else {
283 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
284 }
285 T.consumeClose();
286 }
Alexey Bataev80909872015-07-02 11:25:17 +0000287 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000288 CancelRegion = ParseOpenMPDirectiveKind(*this);
289 if (Tok.isNot(tok::annot_pragma_openmp_end))
290 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000291 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000292
Alexey Bataevf29276e2014-06-18 04:14:57 +0000293 if (isOpenMPLoopDirective(DKind))
294 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
295 if (isOpenMPSimdDirective(DKind))
296 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
297 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000298 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000299
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000301 OpenMPClauseKind CKind =
302 Tok.isAnnotation()
303 ? OMPC_unknown
304 : FlushHasClause ? OMPC_flush
305 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000306 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000307 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000308 OMPClause *Clause =
309 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000310 FirstClauses[CKind].setInt(true);
311 if (Clause) {
312 FirstClauses[CKind].setPointer(Clause);
313 Clauses.push_back(Clause);
314 }
315
316 // Skip ',' if any.
317 if (Tok.is(tok::comma))
318 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000319 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000320 }
321 // End location of the directive.
322 EndLoc = Tok.getLocation();
323 // Consume final annot_pragma_openmp_end.
324 ConsumeToken();
325
Alexey Bataeveb482352015-12-18 05:05:56 +0000326 // OpenMP [2.13.8, ordered Construct, Syntax]
327 // If the depend clause is specified, the ordered construct is a stand-alone
328 // directive.
329 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000330 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000331 Diag(Loc, diag::err_omp_immediate_directive)
332 << getOpenMPDirectiveName(DKind) << 1
333 << getOpenMPClauseName(OMPC_depend);
334 }
335 HasAssociatedStatement = false;
336 }
337
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000338 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000339 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000340 // The body is a block scope like in Lambdas and Blocks.
341 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000342 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000343 Actions.ActOnStartOfCompoundStmt();
344 // Parse statement
345 AssociatedStmt = ParseStatement();
346 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000347 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000348 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000349 Directive = Actions.ActOnOpenMPExecutableDirective(
350 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
351 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000352
353 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000354 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000355 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000356 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000357 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000358 case OMPD_unknown:
359 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000360 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000361 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362 }
363 return Directive;
364}
365
Alexey Bataeva769e072013-03-22 06:34:35 +0000366/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000367/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000368///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000369/// simple-variable-list:
370/// '(' id-expression {, id-expression} ')'
371///
372bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
373 SmallVectorImpl<Expr *> &VarList,
374 bool AllowScopeSpecifier) {
375 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000376 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000377 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000378 if (T.expectAndConsume(diag::err_expected_lparen_after,
379 getOpenMPDirectiveName(Kind)))
380 return true;
381 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000382 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000383
384 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000385 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000386 CXXScopeSpec SS;
387 SourceLocation TemplateKWLoc;
388 UnqualifiedId Name;
389 // Read var name.
390 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000391 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000392
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000393 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000394 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000395 IsCorrect = false;
396 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000397 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000398 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000399 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000400 IsCorrect = false;
401 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000402 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000403 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
404 Tok.isNot(tok::annot_pragma_openmp_end)) {
405 IsCorrect = false;
406 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000407 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000408 Diag(PrevTok.getLocation(), diag::err_expected)
409 << tok::identifier
410 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000411 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000412 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000413 ExprResult Res =
414 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000415 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000416 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000417 }
418 // Consume ','.
419 if (Tok.is(tok::comma)) {
420 ConsumeToken();
421 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000422 }
423
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000424 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000425 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000426 IsCorrect = false;
427 }
428
429 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000430 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000431
432 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000433}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000434
435/// \brief Parsing of OpenMP clauses.
436///
437/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000438/// if-clause | final-clause | num_threads-clause | safelen-clause |
439/// default-clause | private-clause | firstprivate-clause | shared-clause
440/// | linear-clause | aligned-clause | collapse-clause |
441/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000442/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000443/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000444/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000445/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000446/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000447/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000448///
449OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
450 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000451 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000452 bool ErrorFound = false;
453 // Check if clause is allowed for the given directive.
454 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000455 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
456 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000457 ErrorFound = true;
458 }
459
460 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000461 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000462 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000463 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000464 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000465 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000466 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000467 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000468 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000469 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000470 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000471 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000472 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000473 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000474 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000475 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000476 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000477 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000478 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000479 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000480 // OpenMP [2.9.1, target data construct, Restrictions]
481 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000482 // OpenMP [2.11.1, task Construct, Restrictions]
483 // At most one if clause can appear on the directive.
484 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000485 // OpenMP [teams Construct, Restrictions]
486 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000487 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000488 // OpenMP [2.9.1, task Construct, Restrictions]
489 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000490 // OpenMP [2.9.2, taskloop Construct, Restrictions]
491 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000492 // OpenMP [2.9.2, taskloop Construct, Restrictions]
493 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000494 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000495 Diag(Tok, diag::err_omp_more_one_clause)
496 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000497 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000498 }
499
Alexey Bataev10e775f2015-07-30 11:36:16 +0000500 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
501 Clause = ParseOpenMPClause(CKind);
502 else
503 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000504 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000505 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000506 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000507 // OpenMP [2.14.3.1, Restrictions]
508 // Only a single default clause may be specified on a parallel, task or
509 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000510 // OpenMP [2.5, parallel Construct, Restrictions]
511 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000512 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000513 Diag(Tok, diag::err_omp_more_one_clause)
514 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000515 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000516 }
517
518 Clause = ParseOpenMPSimpleClause(CKind);
519 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000520 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000521 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000522 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000523 // OpenMP [2.7.1, Restrictions, p. 3]
524 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000525 // OpenMP [2.10.4, Restrictions, p. 106]
526 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000527 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000528 Diag(Tok, diag::err_omp_more_one_clause)
529 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000530 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000531 }
532
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000533 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000534 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
535 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000536 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000537 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000538 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000539 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000540 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000541 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000542 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000543 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000544 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000545 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000546 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000547 // OpenMP [2.7.1, Restrictions, p. 9]
548 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000549 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
550 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000551 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000552 Diag(Tok, diag::err_omp_more_one_clause)
553 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000554 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000555 }
556
557 Clause = ParseOpenMPClause(CKind);
558 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000559 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000560 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000561 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000563 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000564 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000565 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000566 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000567 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000568 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000569 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000570 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000571 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000572 break;
573 case OMPC_unknown:
574 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000575 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000576 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000577 break;
578 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000579 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
580 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000581 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000582 break;
583 }
Craig Topper161e4db2014-05-21 06:02:52 +0000584 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000585}
586
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000587/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000588/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000589/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000590///
Alexey Bataev3778b602014-07-17 07:32:53 +0000591/// final-clause:
592/// 'final' '(' expression ')'
593///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000594/// num_threads-clause:
595/// 'num_threads' '(' expression ')'
596///
597/// safelen-clause:
598/// 'safelen' '(' expression ')'
599///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000600/// simdlen-clause:
601/// 'simdlen' '(' expression ')'
602///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000603/// collapse-clause:
604/// 'collapse' '(' expression ')'
605///
Alexey Bataeva0569352015-12-01 10:17:31 +0000606/// priority-clause:
607/// 'priority' '(' expression ')'
608///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000609/// grainsize-clause:
610/// 'grainsize' '(' expression ')'
611///
Alexey Bataev382967a2015-12-08 12:06:20 +0000612/// num_tasks-clause:
613/// 'num_tasks' '(' expression ')'
614///
Alexey Bataev28c75412015-12-15 08:19:24 +0000615/// hint-clause:
616/// 'hint' '(' expression ')'
617///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000618OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
619 SourceLocation Loc = ConsumeToken();
620
621 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
622 if (T.expectAndConsume(diag::err_expected_lparen_after,
623 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000624 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000625
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000626 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000627 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
628 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000629 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000630
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000631 // Parse ')'.
632 T.consumeClose();
633
634 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000635 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000636
Alexey Bataeva55ed262014-05-28 06:15:33 +0000637 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000638 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000639}
640
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000641/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000642///
643/// default-clause:
644/// 'default' '(' 'none' | 'shared' ')
645///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000646/// proc_bind-clause:
647/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
648///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000649OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
650 SourceLocation Loc = Tok.getLocation();
651 SourceLocation LOpen = ConsumeToken();
652 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000653 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000654 if (T.expectAndConsume(diag::err_expected_lparen_after,
655 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000656 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000657
Alexey Bataeva55ed262014-05-28 06:15:33 +0000658 unsigned Type = getOpenMPSimpleClauseType(
659 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000660 SourceLocation TypeLoc = Tok.getLocation();
661 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
662 Tok.isNot(tok::annot_pragma_openmp_end))
663 ConsumeAnyToken();
664
665 // Parse ')'.
666 T.consumeClose();
667
668 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
669 Tok.getLocation());
670}
671
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000672/// \brief Parsing of OpenMP clauses like 'ordered'.
673///
674/// ordered-clause:
675/// 'ordered'
676///
Alexey Bataev236070f2014-06-20 11:19:47 +0000677/// nowait-clause:
678/// 'nowait'
679///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000680/// untied-clause:
681/// 'untied'
682///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000683/// mergeable-clause:
684/// 'mergeable'
685///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000686/// read-clause:
687/// 'read'
688///
Alexey Bataev346265e2015-09-25 10:37:12 +0000689/// threads-clause:
690/// 'threads'
691///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000692/// simd-clause:
693/// 'simd'
694///
Alexey Bataevb825de12015-12-07 10:51:44 +0000695/// nogroup-clause:
696/// 'nogroup'
697///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000698OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
699 SourceLocation Loc = Tok.getLocation();
700 ConsumeAnyToken();
701
702 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
703}
704
705
Alexey Bataev56dafe82014-06-20 07:16:17 +0000706/// \brief Parsing of OpenMP clauses with single expressions and some additional
707/// argument like 'schedule' or 'dist_schedule'.
708///
709/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +0000710/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
711/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +0000712///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000713/// if-clause:
714/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
715///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000716/// defaultmap:
717/// 'defaultmap' '(' modifier ':' kind ')'
718///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000719OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
720 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000721 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000722 // Parse '('.
723 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
724 if (T.expectAndConsume(diag::err_expected_lparen_after,
725 getOpenMPClauseName(Kind)))
726 return nullptr;
727
728 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000729 SmallVector<unsigned, 4> Arg;
730 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000731 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000732 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
733 Arg.resize(NumberOfElements);
734 KLoc.resize(NumberOfElements);
735 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
736 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
737 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
738 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000739 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +0000740 if (KindModifier > OMPC_SCHEDULE_unknown) {
741 // Parse 'modifier'
742 Arg[Modifier1] = KindModifier;
743 KLoc[Modifier1] = Tok.getLocation();
744 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
745 Tok.isNot(tok::annot_pragma_openmp_end))
746 ConsumeAnyToken();
747 if (Tok.is(tok::comma)) {
748 // Parse ',' 'modifier'
749 ConsumeAnyToken();
750 KindModifier = getOpenMPSimpleClauseType(
751 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
752 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
753 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +0000754 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000755 KLoc[Modifier2] = Tok.getLocation();
756 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
757 Tok.isNot(tok::annot_pragma_openmp_end))
758 ConsumeAnyToken();
759 }
760 // Parse ':'
761 if (Tok.is(tok::colon))
762 ConsumeAnyToken();
763 else
764 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
765 KindModifier = getOpenMPSimpleClauseType(
766 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
767 }
768 Arg[ScheduleKind] = KindModifier;
769 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000770 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
771 Tok.isNot(tok::annot_pragma_openmp_end))
772 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +0000773 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
774 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
775 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000776 Tok.is(tok::comma))
777 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +0000778 } else if (Kind == OMPC_dist_schedule) {
779 Arg.push_back(getOpenMPSimpleClauseType(
780 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
781 KLoc.push_back(Tok.getLocation());
782 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
783 Tok.isNot(tok::annot_pragma_openmp_end))
784 ConsumeAnyToken();
785 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
786 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000787 } else if (Kind == OMPC_defaultmap) {
788 // Get a defaultmap modifier
789 Arg.push_back(getOpenMPSimpleClauseType(
790 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
791 KLoc.push_back(Tok.getLocation());
792 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
793 Tok.isNot(tok::annot_pragma_openmp_end))
794 ConsumeAnyToken();
795 // Parse ':'
796 if (Tok.is(tok::colon))
797 ConsumeAnyToken();
798 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
799 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
800 // Get a defaultmap kind
801 Arg.push_back(getOpenMPSimpleClauseType(
802 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
803 KLoc.push_back(Tok.getLocation());
804 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
805 Tok.isNot(tok::annot_pragma_openmp_end))
806 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000807 } else {
808 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +0000809 KLoc.push_back(Tok.getLocation());
810 Arg.push_back(ParseOpenMPDirectiveKind(*this));
811 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000812 ConsumeToken();
813 if (Tok.is(tok::colon))
814 DelimLoc = ConsumeToken();
815 else
816 Diag(Tok, diag::warn_pragma_expected_colon)
817 << "directive name modifier";
818 }
819 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000820
Carlo Bertollib4adf552016-01-15 18:50:31 +0000821 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
822 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
823 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000824 if (NeedAnExpression) {
825 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000826 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
827 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000828 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000829 }
830
831 // Parse ')'.
832 T.consumeClose();
833
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000834 if (NeedAnExpression && Val.isInvalid())
835 return nullptr;
836
Alexey Bataev56dafe82014-06-20 07:16:17 +0000837 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000838 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000839 T.getCloseLocation());
840}
841
Alexey Bataevc5e02582014-06-16 07:08:35 +0000842static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
843 UnqualifiedId &ReductionId) {
844 SourceLocation TemplateKWLoc;
845 if (ReductionIdScopeSpec.isEmpty()) {
846 auto OOK = OO_None;
847 switch (P.getCurToken().getKind()) {
848 case tok::plus:
849 OOK = OO_Plus;
850 break;
851 case tok::minus:
852 OOK = OO_Minus;
853 break;
854 case tok::star:
855 OOK = OO_Star;
856 break;
857 case tok::amp:
858 OOK = OO_Amp;
859 break;
860 case tok::pipe:
861 OOK = OO_Pipe;
862 break;
863 case tok::caret:
864 OOK = OO_Caret;
865 break;
866 case tok::ampamp:
867 OOK = OO_AmpAmp;
868 break;
869 case tok::pipepipe:
870 OOK = OO_PipePipe;
871 break;
872 default:
873 break;
874 }
875 if (OOK != OO_None) {
876 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000877 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000878 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
879 return false;
880 }
881 }
882 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
883 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +0000884 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +0000885 TemplateKWLoc, ReductionId);
886}
887
Alexander Musman1bb328c2014-06-04 13:06:39 +0000888/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000889/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000890///
891/// private-clause:
892/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000893/// firstprivate-clause:
894/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000895/// lastprivate-clause:
896/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000897/// shared-clause:
898/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000899/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000900/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000901/// aligned-clause:
902/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000903/// reduction-clause:
904/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000905/// copyprivate-clause:
906/// 'copyprivate' '(' list ')'
907/// flush-clause:
908/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000909/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +0000910/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000911/// map-clause:
912/// 'map' '(' [ [ always , ]
913/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914///
Alexey Bataev182227b2015-08-20 10:54:39 +0000915/// For 'linear' clause linear-list may have the following forms:
916/// list
917/// modifier(list)
918/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +0000919OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
920 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000921 SourceLocation Loc = Tok.getLocation();
922 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000923 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000924 // Optional scope specifier and unqualified id for reduction identifier.
925 CXXScopeSpec ReductionIdScopeSpec;
926 UnqualifiedId ReductionId;
927 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000928 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000929 // OpenMP 4.1 [2.15.3.7, linear Clause]
930 // If no modifier is specified it is assumed to be val.
931 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000932 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
933 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +0000934 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000935 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000936 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000937
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000938 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000939 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940 if (T.expectAndConsume(diag::err_expected_lparen_after,
941 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000942 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000943
Alexey Bataev182227b2015-08-20 10:54:39 +0000944 bool NeedRParenForLinear = false;
945 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
946 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000947 // Handle reduction-identifier for reduction clause.
948 if (Kind == OMPC_reduction) {
949 ColonProtectionRAIIObject ColonRAII(*this);
950 if (getLangOpts().CPlusPlus) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000951 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, nullptr, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000952 }
953 InvalidReductionId =
954 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
955 if (InvalidReductionId) {
956 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
957 StopBeforeMatch);
958 }
959 if (Tok.is(tok::colon)) {
960 ColonLoc = ConsumeToken();
961 } else {
962 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
963 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000964 } else if (Kind == OMPC_depend) {
965 // Handle dependency type for depend clause.
966 ColonProtectionRAIIObject ColonRAII(*this);
967 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
968 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000969 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000970
971 if (DepKind == OMPC_DEPEND_unknown) {
972 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
973 StopBeforeMatch);
974 } else {
975 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +0000976 // Special processing for depend(source) clause.
977 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
978 // Parse ')'.
979 T.consumeClose();
980 return Actions.ActOnOpenMPVarListClause(
981 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
982 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
983 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +0000984 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
985 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +0000986 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000987 }
988 if (Tok.is(tok::colon)) {
989 ColonLoc = ConsumeToken();
990 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +0000991 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
992 : diag::warn_pragma_expected_colon)
993 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000994 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000995 } else if (Kind == OMPC_linear) {
996 // Try to parse modifier if any.
997 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000998 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000999 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +00001000 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +00001001 LinearT.consumeOpen();
1002 NeedRParenForLinear = true;
1003 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00001004 } else if (Kind == OMPC_map) {
1005 // Handle map type for map clause.
1006 ColonProtectionRAIIObject ColonRAII(*this);
1007
1008 // the first identifier may be a list item, a map-type or
1009 // a map-type-modifier
1010 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1011 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1012 DepLinMapLoc = Tok.getLocation();
1013 bool ColonExpected = false;
1014
1015 if (Tok.is(tok::identifier)) {
1016 if (PP.LookAhead(0).is(tok::colon)) {
1017 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1018 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1019 if (MapType == OMPC_MAP_unknown) {
1020 Diag(Tok, diag::err_omp_unknown_map_type);
1021 } else if (MapType == OMPC_MAP_always) {
1022 Diag(Tok, diag::err_omp_map_type_missing);
1023 }
1024 ConsumeToken();
1025 } else if (PP.LookAhead(0).is(tok::comma)) {
1026 if (PP.LookAhead(1).is(tok::identifier) &&
1027 PP.LookAhead(2).is(tok::colon)) {
1028 MapTypeModifier =
1029 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1030 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1031 if (MapTypeModifier != OMPC_MAP_always) {
1032 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1033 MapTypeModifier = OMPC_MAP_unknown;
1034 } else {
1035 MapTypeModifierSpecified = true;
1036 }
1037
1038 ConsumeToken();
1039 ConsumeToken();
1040
1041 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1042 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1043 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1044 Diag(Tok, diag::err_omp_unknown_map_type);
1045 }
1046 ConsumeToken();
1047 } else {
1048 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001049 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001050 }
1051 } else {
1052 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001053 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001054 }
1055 } else {
Samuel Antao5de996e2016-01-22 20:21:36 +00001056 MapType = OMPC_MAP_tofrom;
1057 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001058 }
1059
1060 if (Tok.is(tok::colon)) {
1061 ColonLoc = ConsumeToken();
1062 } else if (ColonExpected) {
1063 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1064 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001065 }
1066
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001067 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001068 bool IsComma =
1069 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1070 (Kind != OMPC_map)) ||
1071 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001072 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001073 (!MapTypeModifierSpecified ||
1074 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1075 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001076 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001077 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001078 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001079 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001081 ExprResult VarExpr =
1082 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001083 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001084 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085 } else {
1086 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001087 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001088 }
1089 // Skip ',' if any
1090 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +00001091 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001092 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001093 else if (Tok.isNot(tok::r_paren) &&
1094 Tok.isNot(tok::annot_pragma_openmp_end) &&
1095 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001096 Diag(Tok, diag::err_omp_expected_punc)
1097 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1098 : getOpenMPClauseName(Kind))
1099 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001100 }
1101
Alexey Bataev182227b2015-08-20 10:54:39 +00001102 // Parse ')' for linear clause with modifier.
1103 if (NeedRParenForLinear)
1104 LinearT.consumeClose();
1105
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001106 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001107 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001108 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1109 if (MustHaveTail) {
1110 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001111 SourceLocation ELoc = ConsumeToken();
1112 ExprResult Tail = ParseAssignmentExpression();
1113 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001114 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001115 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001116 else
1117 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1118 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001119 }
1120
1121 // Parse ')'.
1122 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001123 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001124 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1125 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001126 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001127 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001128
Alexey Bataevc5e02582014-06-16 07:08:35 +00001129 return Actions.ActOnOpenMPVarListClause(
1130 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1131 ReductionIdScopeSpec,
1132 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001133 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001134 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1135 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001136}
1137