blob: 28748dcac6b105546a2881fca1c37a186ae26bb2 [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},
47 {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000048 auto Tok = P.getCurToken();
49 auto DKind =
50 Tok.isAnnotation()
51 ? OMPD_unknown
52 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000053
Alexey Bataev6d4ed052015-07-01 06:57:41 +000054 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000055 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000056 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
57 TokenMatched =
Samuel Antaodf67fc42016-01-19 19:15:56 +000058 ((i == 0) &&
59 !P.getPreprocessor().getSpelling(Tok).compare("cancellation")) ||
Samuel Antao72590762016-01-19 20:04:50 +000060 ((i == 3) &&
61 !P.getPreprocessor().getSpelling(Tok).compare("enter")) ||
62 ((i == 4) && !P.getPreprocessor().getSpelling(Tok).compare("exit"));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000063 } else {
64 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
65 }
Michael Wong65f367f2015-07-21 13:44:28 +000066
Alexey Bataev6d4ed052015-07-01 06:57:41 +000067 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000068 Tok = P.getPreprocessor().LookAhead(0);
Michael Wong65f367f2015-07-21 13:44:28 +000069 auto TokenIsAnnotation = Tok.isAnnotation();
Alexander Musmanf82886e2014-09-18 05:12:34 +000070 auto SDKind =
Michael Wong65f367f2015-07-21 13:44:28 +000071 TokenIsAnnotation
Alexander Musmanf82886e2014-09-18 05:12:34 +000072 ? OMPD_unknown
73 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Michael Wong65f367f2015-07-21 13:44:28 +000074
75 if (!TokenIsAnnotation && SDKind == OMPD_unknown) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000076 TokenMatched =
Daniel Jasper9aea8602015-07-21 16:18:51 +000077 ((i == 0) &&
78 !P.getPreprocessor().getSpelling(Tok).compare("point")) ||
Samuel Antao72590762016-01-19 20:04:50 +000079 ((i == 1 || i == 3 || i == 4) &&
Samuel Antaodf67fc42016-01-19 19:15:56 +000080 !P.getPreprocessor().getSpelling(Tok).compare("data")) ||
81 ((i == 2) &&
Samuel Antao72590762016-01-19 20:04:50 +000082 (!P.getPreprocessor().getSpelling(Tok).compare("enter") ||
83 !P.getPreprocessor().getSpelling(Tok).compare("exit")));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000084 } else {
85 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
86 }
Michael Wong65f367f2015-07-21 13:44:28 +000087
Alexey Bataev6d4ed052015-07-01 06:57:41 +000088 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000089 P.ConsumeToken();
90 DKind = F[i][2];
91 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000092 }
93 }
94 return DKind;
95}
96
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000097/// \brief Parsing of declarative OpenMP directives.
98///
99/// threadprivate-directive:
100/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +0000101///
102Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
103 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000104 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +0000105
106 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000107 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000108 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000109
110 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000111 case OMPD_threadprivate:
112 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000113 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000114 // The last seen token is annot_pragma_openmp_end - need to check for
115 // extra tokens.
116 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
117 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000118 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000119 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +0000120 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000121 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +0000122 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000123 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000124 }
125 break;
126 case OMPD_unknown:
127 Diag(Tok, diag::err_omp_unknown_directive);
128 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000129 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000130 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000131 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000132 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000133 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000134 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000135 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000136 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000137 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000138 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000139 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000140 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000141 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000142 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000143 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000144 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000145 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000146 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000147 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000148 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000149 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000150 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000151 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000152 case OMPD_cancel:
Samuel Antao5b0688e2015-07-22 16:02:46 +0000153 case OMPD_target_data:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000154 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000155 case OMPD_target_exit_data:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000156 case OMPD_taskloop:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000157 case OMPD_taskloop_simd:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000158 case OMPD_distribute:
Alexey Bataeva769e072013-03-22 06:34:35 +0000159 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000160 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000161 break;
162 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000163 SkipUntil(tok::annot_pragma_openmp_end);
David Blaikie0403cb12016-01-15 23:43:25 +0000164 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000165}
166
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167/// \brief Parsing of declarative or executable OpenMP directives.
168///
169/// threadprivate-directive:
170/// annot_pragma_openmp 'threadprivate' simple-variable-list
171/// annot_pragma_openmp_end
172///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000173/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000174/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000175/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
176/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000177/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Michael Wong65f367f2015-07-21 13:44:28 +0000178/// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000179/// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' {clause} |
Samuel Antao72590762016-01-19 20:04:50 +0000180/// 'distribute' | 'target enter data' | 'target exit data'
181/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182///
Alexey Bataevc4fad652016-01-13 11:18:54 +0000183StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
184 AllowedContsructsKind Allowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000185 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000186 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000187 SmallVector<Expr *, 5> Identifiers;
188 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000189 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000190 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000191 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000192 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000193 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000194 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000195 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000196 // Name of critical directive.
197 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000198 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000199 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000200 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000201
202 switch (DKind) {
203 case OMPD_threadprivate:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000204 if (Allowed != ACK_Any) {
205 Diag(Tok, diag::err_omp_immediate_directive)
206 << getOpenMPDirectiveName(DKind) << 0;
207 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000208 ConsumeToken();
209 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
210 // The last seen token is annot_pragma_openmp_end - need to check for
211 // extra tokens.
212 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
213 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000214 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000215 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000216 }
217 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000218 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000219 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
220 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000221 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000222 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000223 case OMPD_flush:
224 if (PP.LookAhead(0).is(tok::l_paren)) {
225 FlushHasClause = true;
226 // Push copy of the current token back to stream to properly parse
227 // pseudo-clause OMPFlushClause.
228 PP.EnterToken(Tok);
229 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000230 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000231 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000232 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000233 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000234 case OMPD_cancel:
Samuel Antaodf67fc42016-01-19 19:15:56 +0000235 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +0000236 case OMPD_target_exit_data:
Alexey Bataevc4fad652016-01-13 11:18:54 +0000237 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataev68446b72014-07-18 07:47:19 +0000238 Diag(Tok, diag::err_omp_immediate_directive)
Alexey Bataeveb482352015-12-18 05:05:56 +0000239 << getOpenMPDirectiveName(DKind) << 0;
Alexey Bataev68446b72014-07-18 07:47:19 +0000240 }
241 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000242 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000243 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000244 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000245 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000246 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000247 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000248 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000249 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000250 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000251 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000252 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000253 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000254 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000255 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000256 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000257 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000258 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000259 case OMPD_teams:
Michael Wong65f367f2015-07-21 13:44:28 +0000260 case OMPD_taskgroup:
Alexey Bataev49f6e782015-12-01 04:18:41 +0000261 case OMPD_target_data:
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000262 case OMPD_taskloop:
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000263 case OMPD_taskloop_simd:
264 case OMPD_distribute: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000265 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000266 // Parse directive name of the 'critical' directive if any.
267 if (DKind == OMPD_critical) {
268 BalancedDelimiterTracker T(*this, tok::l_paren,
269 tok::annot_pragma_openmp_end);
270 if (!T.consumeOpen()) {
271 if (Tok.isAnyIdentifier()) {
272 DirName =
273 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
274 ConsumeAnyToken();
275 } else {
276 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
277 }
278 T.consumeClose();
279 }
Alexey Bataev80909872015-07-02 11:25:17 +0000280 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000281 CancelRegion = ParseOpenMPDirectiveKind(*this);
282 if (Tok.isNot(tok::annot_pragma_openmp_end))
283 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000284 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000285
Alexey Bataevf29276e2014-06-18 04:14:57 +0000286 if (isOpenMPLoopDirective(DKind))
287 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
288 if (isOpenMPSimdDirective(DKind))
289 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
290 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000291 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000292
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000293 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000294 OpenMPClauseKind CKind =
295 Tok.isAnnotation()
296 ? OMPC_unknown
297 : FlushHasClause ? OMPC_flush
298 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000299 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000300 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000301 OMPClause *Clause =
302 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000303 FirstClauses[CKind].setInt(true);
304 if (Clause) {
305 FirstClauses[CKind].setPointer(Clause);
306 Clauses.push_back(Clause);
307 }
308
309 // Skip ',' if any.
310 if (Tok.is(tok::comma))
311 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000312 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000313 }
314 // End location of the directive.
315 EndLoc = Tok.getLocation();
316 // Consume final annot_pragma_openmp_end.
317 ConsumeToken();
318
Alexey Bataeveb482352015-12-18 05:05:56 +0000319 // OpenMP [2.13.8, ordered Construct, Syntax]
320 // If the depend clause is specified, the ordered construct is a stand-alone
321 // directive.
322 if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000323 if (Allowed == ACK_StatementsOpenMPNonStandalone) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000324 Diag(Loc, diag::err_omp_immediate_directive)
325 << getOpenMPDirectiveName(DKind) << 1
326 << getOpenMPClauseName(OMPC_depend);
327 }
328 HasAssociatedStatement = false;
329 }
330
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000331 StmtResult AssociatedStmt;
Alexey Bataev68446b72014-07-18 07:47:19 +0000332 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000333 // The body is a block scope like in Lambdas and Blocks.
334 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000335 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000336 Actions.ActOnStartOfCompoundStmt();
337 // Parse statement
338 AssociatedStmt = ParseStatement();
339 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000340 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000341 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000342 Directive = Actions.ActOnOpenMPExecutableDirective(
343 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
344 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000345
346 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000347 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000348 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000349 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000350 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000351 case OMPD_unknown:
352 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000353 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000354 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000355 }
356 return Directive;
357}
358
Alexey Bataeva769e072013-03-22 06:34:35 +0000359/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000360/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000361///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000362/// simple-variable-list:
363/// '(' id-expression {, id-expression} ')'
364///
365bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
366 SmallVectorImpl<Expr *> &VarList,
367 bool AllowScopeSpecifier) {
368 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000369 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000370 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000371 if (T.expectAndConsume(diag::err_expected_lparen_after,
372 getOpenMPDirectiveName(Kind)))
373 return true;
374 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000375 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000376
377 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000378 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000379 CXXScopeSpec SS;
380 SourceLocation TemplateKWLoc;
381 UnqualifiedId Name;
382 // Read var name.
383 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000384 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000385
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000386 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
David Blaikieefdccaa2016-01-15 23:43:34 +0000387 ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000388 IsCorrect = false;
389 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000390 StopBeforeMatch);
David Blaikieefdccaa2016-01-15 23:43:34 +0000391 } else if (ParseUnqualifiedId(SS, false, false, false, nullptr,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000392 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000393 IsCorrect = false;
394 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000395 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000396 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
397 Tok.isNot(tok::annot_pragma_openmp_end)) {
398 IsCorrect = false;
399 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000400 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000401 Diag(PrevTok.getLocation(), diag::err_expected)
402 << tok::identifier
403 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000404 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000405 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000406 ExprResult Res =
407 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000408 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000409 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000410 }
411 // Consume ','.
412 if (Tok.is(tok::comma)) {
413 ConsumeToken();
414 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000415 }
416
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000417 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000418 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000419 IsCorrect = false;
420 }
421
422 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000423 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000424
425 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000426}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000427
428/// \brief Parsing of OpenMP clauses.
429///
430/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000431/// if-clause | final-clause | num_threads-clause | safelen-clause |
432/// default-clause | private-clause | firstprivate-clause | shared-clause
433/// | linear-clause | aligned-clause | collapse-clause |
434/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000435/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000436/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev66b15b52015-08-21 11:14:16 +0000437/// update-clause | capture-clause | seq_cst-clause | device-clause |
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000438/// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000439/// thread_limit-clause | priority-clause | grainsize-clause |
Alexey Bataev28c75412015-12-15 08:19:24 +0000440/// nogroup-clause | num_tasks-clause | hint-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000441///
442OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
443 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000444 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000445 bool ErrorFound = false;
446 // Check if clause is allowed for the given directive.
447 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000448 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
449 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000450 ErrorFound = true;
451 }
452
453 switch (CKind) {
Alexey Bataev3778b602014-07-17 07:32:53 +0000454 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000455 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000456 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +0000457 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000458 case OMPC_collapse:
Alexey Bataev10e775f2015-07-30 11:36:16 +0000459 case OMPC_ordered:
Michael Wonge710d542015-08-07 16:16:36 +0000460 case OMPC_device:
Kelvin Li099bb8c2015-11-24 20:50:12 +0000461 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000462 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +0000463 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000464 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +0000465 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +0000466 case OMPC_hint:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000467 // OpenMP [2.5, Restrictions]
Alexey Bataev568a8332014-03-06 06:15:19 +0000468 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000469 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000470 // Only one safelen clause can appear on a simd directive.
Alexey Bataev66b15b52015-08-21 11:14:16 +0000471 // Only one simdlen clause can appear on a simd directive.
Alexander Musman8bd31e62014-05-27 15:12:19 +0000472 // Only one collapse clause can appear on a simd directive.
Michael Wonge710d542015-08-07 16:16:36 +0000473 // OpenMP [2.9.1, target data construct, Restrictions]
474 // At most one device clause can appear on the directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000475 // OpenMP [2.11.1, task Construct, Restrictions]
476 // At most one if clause can appear on the directive.
477 // At most one final clause can appear on the directive.
Kelvin Li099bb8c2015-11-24 20:50:12 +0000478 // OpenMP [teams Construct, Restrictions]
479 // At most one num_teams clause can appear on the directive.
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000480 // At most one thread_limit clause can appear on the directive.
Alexey Bataeva0569352015-12-01 10:17:31 +0000481 // OpenMP [2.9.1, task Construct, Restrictions]
482 // At most one priority clause can appear on the directive.
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000483 // OpenMP [2.9.2, taskloop Construct, Restrictions]
484 // At most one grainsize clause can appear on the directive.
Alexey Bataev382967a2015-12-08 12:06:20 +0000485 // OpenMP [2.9.2, taskloop Construct, Restrictions]
486 // At most one num_tasks clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000487 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000488 Diag(Tok, diag::err_omp_more_one_clause)
489 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000490 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000491 }
492
Alexey Bataev10e775f2015-07-30 11:36:16 +0000493 if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
494 Clause = ParseOpenMPClause(CKind);
495 else
496 Clause = ParseOpenMPSingleExprClause(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000497 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000498 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000499 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000500 // OpenMP [2.14.3.1, Restrictions]
501 // Only a single default clause may be specified on a parallel, task or
502 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000503 // OpenMP [2.5, parallel Construct, Restrictions]
504 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000505 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000506 Diag(Tok, diag::err_omp_more_one_clause)
507 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000508 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000509 }
510
511 Clause = ParseOpenMPSimpleClause(CKind);
512 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000513 case OMPC_schedule:
Carlo Bertollib4adf552016-01-15 18:50:31 +0000514 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000515 case OMPC_defaultmap:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000516 // OpenMP [2.7.1, Restrictions, p. 3]
517 // Only one schedule clause can appear on a loop directive.
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000518 // OpenMP [2.10.4, Restrictions, p. 106]
519 // At most one defaultmap clause can appear on the directive.
Alexey Bataev56dafe82014-06-20 07:16:17 +0000520 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000521 Diag(Tok, diag::err_omp_more_one_clause)
522 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000523 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000524 }
525
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000526 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000527 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
528 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000529 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000530 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000531 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000532 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000533 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000534 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000535 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000536 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000537 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000538 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000539 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000540 // OpenMP [2.7.1, Restrictions, p. 9]
541 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000542 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
543 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000544 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000545 Diag(Tok, diag::err_omp_more_one_clause)
546 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000547 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000548 }
549
550 Clause = ParseOpenMPClause(CKind);
551 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000552 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000553 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000554 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000556 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000557 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000558 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000559 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000560 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000561 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000562 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000563 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000564 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000565 break;
566 case OMPC_unknown:
567 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000568 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000569 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000570 break;
571 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000572 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
573 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000574 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575 break;
576 }
Craig Topper161e4db2014-05-21 06:02:52 +0000577 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000578}
579
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000580/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000581/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000582/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000583///
Alexey Bataev3778b602014-07-17 07:32:53 +0000584/// final-clause:
585/// 'final' '(' expression ')'
586///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000587/// num_threads-clause:
588/// 'num_threads' '(' expression ')'
589///
590/// safelen-clause:
591/// 'safelen' '(' expression ')'
592///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000593/// simdlen-clause:
594/// 'simdlen' '(' expression ')'
595///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000596/// collapse-clause:
597/// 'collapse' '(' expression ')'
598///
Alexey Bataeva0569352015-12-01 10:17:31 +0000599/// priority-clause:
600/// 'priority' '(' expression ')'
601///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000602/// grainsize-clause:
603/// 'grainsize' '(' expression ')'
604///
Alexey Bataev382967a2015-12-08 12:06:20 +0000605/// num_tasks-clause:
606/// 'num_tasks' '(' expression ')'
607///
Alexey Bataev28c75412015-12-15 08:19:24 +0000608/// hint-clause:
609/// 'hint' '(' expression ')'
610///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000611OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
612 SourceLocation Loc = ConsumeToken();
613
614 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
615 if (T.expectAndConsume(diag::err_expected_lparen_after,
616 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000617 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000618
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000619 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000620 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
621 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000622 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000623
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000624 // Parse ')'.
625 T.consumeClose();
626
627 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000628 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000629
Alexey Bataeva55ed262014-05-28 06:15:33 +0000630 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000631 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000632}
633
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000634/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000635///
636/// default-clause:
637/// 'default' '(' 'none' | 'shared' ')
638///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000639/// proc_bind-clause:
640/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
641///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000642OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
643 SourceLocation Loc = Tok.getLocation();
644 SourceLocation LOpen = ConsumeToken();
645 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000646 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647 if (T.expectAndConsume(diag::err_expected_lparen_after,
648 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000649 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000650
Alexey Bataeva55ed262014-05-28 06:15:33 +0000651 unsigned Type = getOpenMPSimpleClauseType(
652 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000653 SourceLocation TypeLoc = Tok.getLocation();
654 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
655 Tok.isNot(tok::annot_pragma_openmp_end))
656 ConsumeAnyToken();
657
658 // Parse ')'.
659 T.consumeClose();
660
661 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
662 Tok.getLocation());
663}
664
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000665/// \brief Parsing of OpenMP clauses like 'ordered'.
666///
667/// ordered-clause:
668/// 'ordered'
669///
Alexey Bataev236070f2014-06-20 11:19:47 +0000670/// nowait-clause:
671/// 'nowait'
672///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000673/// untied-clause:
674/// 'untied'
675///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000676/// mergeable-clause:
677/// 'mergeable'
678///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000679/// read-clause:
680/// 'read'
681///
Alexey Bataev346265e2015-09-25 10:37:12 +0000682/// threads-clause:
683/// 'threads'
684///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000685/// simd-clause:
686/// 'simd'
687///
Alexey Bataevb825de12015-12-07 10:51:44 +0000688/// nogroup-clause:
689/// 'nogroup'
690///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000691OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
692 SourceLocation Loc = Tok.getLocation();
693 ConsumeAnyToken();
694
695 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
696}
697
698
Alexey Bataev56dafe82014-06-20 07:16:17 +0000699/// \brief Parsing of OpenMP clauses with single expressions and some additional
700/// argument like 'schedule' or 'dist_schedule'.
701///
702/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +0000703/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
704/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +0000705///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000706/// if-clause:
707/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
708///
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000709/// defaultmap:
710/// 'defaultmap' '(' modifier ':' kind ')'
711///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000712OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
713 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000714 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000715 // Parse '('.
716 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
717 if (T.expectAndConsume(diag::err_expected_lparen_after,
718 getOpenMPClauseName(Kind)))
719 return nullptr;
720
721 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000722 SmallVector<unsigned, 4> Arg;
723 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000724 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000725 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
726 Arg.resize(NumberOfElements);
727 KLoc.resize(NumberOfElements);
728 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
729 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
730 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
731 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000732 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +0000733 if (KindModifier > OMPC_SCHEDULE_unknown) {
734 // Parse 'modifier'
735 Arg[Modifier1] = KindModifier;
736 KLoc[Modifier1] = Tok.getLocation();
737 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
738 Tok.isNot(tok::annot_pragma_openmp_end))
739 ConsumeAnyToken();
740 if (Tok.is(tok::comma)) {
741 // Parse ',' 'modifier'
742 ConsumeAnyToken();
743 KindModifier = getOpenMPSimpleClauseType(
744 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
745 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
746 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +0000747 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000748 KLoc[Modifier2] = Tok.getLocation();
749 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
750 Tok.isNot(tok::annot_pragma_openmp_end))
751 ConsumeAnyToken();
752 }
753 // Parse ':'
754 if (Tok.is(tok::colon))
755 ConsumeAnyToken();
756 else
757 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
758 KindModifier = getOpenMPSimpleClauseType(
759 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
760 }
761 Arg[ScheduleKind] = KindModifier;
762 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000763 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
764 Tok.isNot(tok::annot_pragma_openmp_end))
765 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +0000766 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
767 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
768 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000769 Tok.is(tok::comma))
770 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +0000771 } else if (Kind == OMPC_dist_schedule) {
772 Arg.push_back(getOpenMPSimpleClauseType(
773 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
774 KLoc.push_back(Tok.getLocation());
775 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
776 Tok.isNot(tok::annot_pragma_openmp_end))
777 ConsumeAnyToken();
778 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
779 DelimLoc = ConsumeAnyToken();
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000780 } else if (Kind == OMPC_defaultmap) {
781 // Get a defaultmap modifier
782 Arg.push_back(getOpenMPSimpleClauseType(
783 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
784 KLoc.push_back(Tok.getLocation());
785 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
786 Tok.isNot(tok::annot_pragma_openmp_end))
787 ConsumeAnyToken();
788 // Parse ':'
789 if (Tok.is(tok::colon))
790 ConsumeAnyToken();
791 else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
792 Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
793 // Get a defaultmap kind
794 Arg.push_back(getOpenMPSimpleClauseType(
795 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
796 KLoc.push_back(Tok.getLocation());
797 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
798 Tok.isNot(tok::annot_pragma_openmp_end))
799 ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000800 } else {
801 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +0000802 KLoc.push_back(Tok.getLocation());
803 Arg.push_back(ParseOpenMPDirectiveKind(*this));
804 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000805 ConsumeToken();
806 if (Tok.is(tok::colon))
807 DelimLoc = ConsumeToken();
808 else
809 Diag(Tok, diag::warn_pragma_expected_colon)
810 << "directive name modifier";
811 }
812 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000813
Carlo Bertollib4adf552016-01-15 18:50:31 +0000814 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
815 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
816 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000817 if (NeedAnExpression) {
818 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000819 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
820 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000821 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000822 }
823
824 // Parse ')'.
825 T.consumeClose();
826
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000827 if (NeedAnExpression && Val.isInvalid())
828 return nullptr;
829
Alexey Bataev56dafe82014-06-20 07:16:17 +0000830 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000831 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000832 T.getCloseLocation());
833}
834
Alexey Bataevc5e02582014-06-16 07:08:35 +0000835static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
836 UnqualifiedId &ReductionId) {
837 SourceLocation TemplateKWLoc;
838 if (ReductionIdScopeSpec.isEmpty()) {
839 auto OOK = OO_None;
840 switch (P.getCurToken().getKind()) {
841 case tok::plus:
842 OOK = OO_Plus;
843 break;
844 case tok::minus:
845 OOK = OO_Minus;
846 break;
847 case tok::star:
848 OOK = OO_Star;
849 break;
850 case tok::amp:
851 OOK = OO_Amp;
852 break;
853 case tok::pipe:
854 OOK = OO_Pipe;
855 break;
856 case tok::caret:
857 OOK = OO_Caret;
858 break;
859 case tok::ampamp:
860 OOK = OO_AmpAmp;
861 break;
862 case tok::pipepipe:
863 OOK = OO_PipePipe;
864 break;
865 default:
866 break;
867 }
868 if (OOK != OO_None) {
869 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000870 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000871 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
872 return false;
873 }
874 }
875 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
876 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +0000877 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +0000878 TemplateKWLoc, ReductionId);
879}
880
Alexander Musman1bb328c2014-06-04 13:06:39 +0000881/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000882/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000883///
884/// private-clause:
885/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000886/// firstprivate-clause:
887/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000888/// lastprivate-clause:
889/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000890/// shared-clause:
891/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000892/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000893/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000894/// aligned-clause:
895/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000896/// reduction-clause:
897/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000898/// copyprivate-clause:
899/// 'copyprivate' '(' list ')'
900/// flush-clause:
901/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000902/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +0000903/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000904/// map-clause:
905/// 'map' '(' [ [ always , ]
906/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000907///
Alexey Bataev182227b2015-08-20 10:54:39 +0000908/// For 'linear' clause linear-list may have the following forms:
909/// list
910/// modifier(list)
911/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +0000912OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
913 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914 SourceLocation Loc = Tok.getLocation();
915 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000916 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000917 // Optional scope specifier and unqualified id for reduction identifier.
918 CXXScopeSpec ReductionIdScopeSpec;
919 UnqualifiedId ReductionId;
920 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000921 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000922 // OpenMP 4.1 [2.15.3.7, linear Clause]
923 // If no modifier is specified it is assumed to be val.
924 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000925 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
926 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +0000927 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000928 bool MapTypeModifierSpecified = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000929 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000930
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000931 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000932 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000933 if (T.expectAndConsume(diag::err_expected_lparen_after,
934 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000935 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000936
Alexey Bataev182227b2015-08-20 10:54:39 +0000937 bool NeedRParenForLinear = false;
938 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
939 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000940 // Handle reduction-identifier for reduction clause.
941 if (Kind == OMPC_reduction) {
942 ColonProtectionRAIIObject ColonRAII(*this);
943 if (getLangOpts().CPlusPlus) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000944 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, nullptr, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000945 }
946 InvalidReductionId =
947 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
948 if (InvalidReductionId) {
949 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
950 StopBeforeMatch);
951 }
952 if (Tok.is(tok::colon)) {
953 ColonLoc = ConsumeToken();
954 } else {
955 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
956 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000957 } else if (Kind == OMPC_depend) {
958 // Handle dependency type for depend clause.
959 ColonProtectionRAIIObject ColonRAII(*this);
960 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
961 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000962 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000963
964 if (DepKind == OMPC_DEPEND_unknown) {
965 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
966 StopBeforeMatch);
967 } else {
968 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +0000969 // Special processing for depend(source) clause.
970 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
971 // Parse ')'.
972 T.consumeClose();
973 return Actions.ActOnOpenMPVarListClause(
974 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
975 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
976 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +0000977 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
978 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +0000979 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000980 }
981 if (Tok.is(tok::colon)) {
982 ColonLoc = ConsumeToken();
983 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +0000984 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
985 : diag::warn_pragma_expected_colon)
986 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000987 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000988 } else if (Kind == OMPC_linear) {
989 // Try to parse modifier if any.
990 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000991 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000992 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000993 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000994 LinearT.consumeOpen();
995 NeedRParenForLinear = true;
996 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000997 } else if (Kind == OMPC_map) {
998 // Handle map type for map clause.
999 ColonProtectionRAIIObject ColonRAII(*this);
1000
1001 // the first identifier may be a list item, a map-type or
1002 // a map-type-modifier
1003 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1004 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1005 DepLinMapLoc = Tok.getLocation();
1006 bool ColonExpected = false;
1007
1008 if (Tok.is(tok::identifier)) {
1009 if (PP.LookAhead(0).is(tok::colon)) {
1010 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1011 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1012 if (MapType == OMPC_MAP_unknown) {
1013 Diag(Tok, diag::err_omp_unknown_map_type);
1014 } else if (MapType == OMPC_MAP_always) {
1015 Diag(Tok, diag::err_omp_map_type_missing);
1016 }
1017 ConsumeToken();
1018 } else if (PP.LookAhead(0).is(tok::comma)) {
1019 if (PP.LookAhead(1).is(tok::identifier) &&
1020 PP.LookAhead(2).is(tok::colon)) {
1021 MapTypeModifier =
1022 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1023 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1024 if (MapTypeModifier != OMPC_MAP_always) {
1025 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1026 MapTypeModifier = OMPC_MAP_unknown;
1027 } else {
1028 MapTypeModifierSpecified = true;
1029 }
1030
1031 ConsumeToken();
1032 ConsumeToken();
1033
1034 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1035 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1036 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1037 Diag(Tok, diag::err_omp_unknown_map_type);
1038 }
1039 ConsumeToken();
1040 } else {
1041 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001042 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001043 }
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 {
Samuel Antao5de996e2016-01-22 20:21:36 +00001049 MapType = OMPC_MAP_tofrom;
1050 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001051 }
1052
1053 if (Tok.is(tok::colon)) {
1054 ColonLoc = ConsumeToken();
1055 } else if (ColonExpected) {
1056 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1057 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001058 }
1059
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001060 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001061 bool IsComma =
1062 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1063 (Kind != OMPC_map)) ||
1064 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001065 ((Kind == OMPC_map) && (MapType != OMPC_MAP_unknown) &&
Kelvin Li0bff7af2015-11-23 05:32:03 +00001066 (!MapTypeModifierSpecified ||
1067 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1068 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001069 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001070 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001071 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001072 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001074 ExprResult VarExpr =
1075 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001077 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001078 } else {
1079 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001080 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001081 }
1082 // Skip ',' if any
1083 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +00001084 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001086 else if (Tok.isNot(tok::r_paren) &&
1087 Tok.isNot(tok::annot_pragma_openmp_end) &&
1088 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001089 Diag(Tok, diag::err_omp_expected_punc)
1090 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1091 : getOpenMPClauseName(Kind))
1092 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001093 }
1094
Alexey Bataev182227b2015-08-20 10:54:39 +00001095 // Parse ')' for linear clause with modifier.
1096 if (NeedRParenForLinear)
1097 LinearT.consumeClose();
1098
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001099 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001100 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001101 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1102 if (MustHaveTail) {
1103 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001104 SourceLocation ELoc = ConsumeToken();
1105 ExprResult Tail = ParseAssignmentExpression();
1106 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001107 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001108 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001109 else
1110 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1111 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001112 }
1113
1114 // Parse ')'.
1115 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001116 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
Samuel Antao5de996e2016-01-22 20:21:36 +00001117 (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
1118 (MustHaveTail && !TailExpr) || InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001119 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001120 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001121
Alexey Bataevc5e02582014-06-16 07:08:35 +00001122 return Actions.ActOnOpenMPVarListClause(
1123 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1124 ReductionIdScopeSpec,
1125 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001126 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001127 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1128 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001129}
1130