blob: a2e887f7cfd7d2d33cb769aa1eba40b0dbac200e [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:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000515 // OpenMP [2.7.1, Restrictions, p. 3]
516 // Only one schedule clause can appear on a loop directive.
517 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000518 Diag(Tok, diag::err_omp_more_one_clause)
519 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000520 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000521 }
522
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000523 case OMPC_if:
Alexey Bataev56dafe82014-06-20 07:16:17 +0000524 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
525 break;
Alexey Bataev236070f2014-06-20 11:19:47 +0000526 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000527 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000528 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000529 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000530 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000531 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000532 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000533 case OMPC_seq_cst:
Alexey Bataev346265e2015-09-25 10:37:12 +0000534 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000535 case OMPC_simd:
Alexey Bataevb825de12015-12-07 10:51:44 +0000536 case OMPC_nogroup:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000537 // OpenMP [2.7.1, Restrictions, p. 9]
538 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000539 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
540 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000541 if (!FirstClause) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000542 Diag(Tok, diag::err_omp_more_one_clause)
543 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
Alexey Bataevdea47612014-07-23 07:46:59 +0000544 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000545 }
546
547 Clause = ParseOpenMPClause(CKind);
548 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000549 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000550 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000551 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000552 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000553 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000554 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000555 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000556 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000557 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000558 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000559 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +0000560 case OMPC_map:
Alexey Bataeveb482352015-12-18 05:05:56 +0000561 Clause = ParseOpenMPVarListClause(DKind, CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000562 break;
563 case OMPC_unknown:
564 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000565 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000566 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000567 break;
568 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000569 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
570 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000571 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000572 break;
573 }
Craig Topper161e4db2014-05-21 06:02:52 +0000574 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575}
576
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000577/// \brief Parsing of OpenMP clauses with single expressions like 'final',
Alexey Bataeva0569352015-12-01 10:17:31 +0000578/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
Alexey Bataev28c75412015-12-15 08:19:24 +0000579/// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000580///
Alexey Bataev3778b602014-07-17 07:32:53 +0000581/// final-clause:
582/// 'final' '(' expression ')'
583///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000584/// num_threads-clause:
585/// 'num_threads' '(' expression ')'
586///
587/// safelen-clause:
588/// 'safelen' '(' expression ')'
589///
Alexey Bataev66b15b52015-08-21 11:14:16 +0000590/// simdlen-clause:
591/// 'simdlen' '(' expression ')'
592///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000593/// collapse-clause:
594/// 'collapse' '(' expression ')'
595///
Alexey Bataeva0569352015-12-01 10:17:31 +0000596/// priority-clause:
597/// 'priority' '(' expression ')'
598///
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000599/// grainsize-clause:
600/// 'grainsize' '(' expression ')'
601///
Alexey Bataev382967a2015-12-08 12:06:20 +0000602/// num_tasks-clause:
603/// 'num_tasks' '(' expression ')'
604///
Alexey Bataev28c75412015-12-15 08:19:24 +0000605/// hint-clause:
606/// 'hint' '(' expression ')'
607///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000608OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
609 SourceLocation Loc = ConsumeToken();
610
611 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
612 if (T.expectAndConsume(diag::err_expected_lparen_after,
613 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000614 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000615
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000616 SourceLocation ELoc = Tok.getLocation();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000617 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
618 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000619 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000620
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000621 // Parse ')'.
622 T.consumeClose();
623
624 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000625 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000626
Alexey Bataeva55ed262014-05-28 06:15:33 +0000627 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000628 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000629}
630
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000631/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000632///
633/// default-clause:
634/// 'default' '(' 'none' | 'shared' ')
635///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000636/// proc_bind-clause:
637/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
638///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000639OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
640 SourceLocation Loc = Tok.getLocation();
641 SourceLocation LOpen = ConsumeToken();
642 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000643 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000644 if (T.expectAndConsume(diag::err_expected_lparen_after,
645 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000646 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647
Alexey Bataeva55ed262014-05-28 06:15:33 +0000648 unsigned Type = getOpenMPSimpleClauseType(
649 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000650 SourceLocation TypeLoc = Tok.getLocation();
651 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
652 Tok.isNot(tok::annot_pragma_openmp_end))
653 ConsumeAnyToken();
654
655 // Parse ')'.
656 T.consumeClose();
657
658 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
659 Tok.getLocation());
660}
661
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000662/// \brief Parsing of OpenMP clauses like 'ordered'.
663///
664/// ordered-clause:
665/// 'ordered'
666///
Alexey Bataev236070f2014-06-20 11:19:47 +0000667/// nowait-clause:
668/// 'nowait'
669///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000670/// untied-clause:
671/// 'untied'
672///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000673/// mergeable-clause:
674/// 'mergeable'
675///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000676/// read-clause:
677/// 'read'
678///
Alexey Bataev346265e2015-09-25 10:37:12 +0000679/// threads-clause:
680/// 'threads'
681///
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000682/// simd-clause:
683/// 'simd'
684///
Alexey Bataevb825de12015-12-07 10:51:44 +0000685/// nogroup-clause:
686/// 'nogroup'
687///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000688OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
689 SourceLocation Loc = Tok.getLocation();
690 ConsumeAnyToken();
691
692 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
693}
694
695
Alexey Bataev56dafe82014-06-20 07:16:17 +0000696/// \brief Parsing of OpenMP clauses with single expressions and some additional
697/// argument like 'schedule' or 'dist_schedule'.
698///
699/// schedule-clause:
Alexey Bataev6402bca2015-12-28 07:25:51 +0000700/// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
701/// ')'
Alexey Bataev56dafe82014-06-20 07:16:17 +0000702///
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000703/// if-clause:
704/// 'if' '(' [ directive-name-modifier ':' ] expression ')'
705///
Alexey Bataev56dafe82014-06-20 07:16:17 +0000706OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
707 SourceLocation Loc = ConsumeToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000708 SourceLocation DelimLoc;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000709 // Parse '('.
710 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
711 if (T.expectAndConsume(diag::err_expected_lparen_after,
712 getOpenMPClauseName(Kind)))
713 return nullptr;
714
715 ExprResult Val;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000716 SmallVector<unsigned, 4> Arg;
717 SmallVector<SourceLocation, 4> KLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000718 if (Kind == OMPC_schedule) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000719 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
720 Arg.resize(NumberOfElements);
721 KLoc.resize(NumberOfElements);
722 Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
723 Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
724 Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
725 auto KindModifier = getOpenMPSimpleClauseType(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000726 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev6402bca2015-12-28 07:25:51 +0000727 if (KindModifier > OMPC_SCHEDULE_unknown) {
728 // Parse 'modifier'
729 Arg[Modifier1] = KindModifier;
730 KLoc[Modifier1] = Tok.getLocation();
731 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
732 Tok.isNot(tok::annot_pragma_openmp_end))
733 ConsumeAnyToken();
734 if (Tok.is(tok::comma)) {
735 // Parse ',' 'modifier'
736 ConsumeAnyToken();
737 KindModifier = getOpenMPSimpleClauseType(
738 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
739 Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
740 ? KindModifier
Aaron Ballmanad8a1042015-12-28 15:52:46 +0000741 : (unsigned)OMPC_SCHEDULE_unknown;
Alexey Bataev6402bca2015-12-28 07:25:51 +0000742 KLoc[Modifier2] = Tok.getLocation();
743 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
744 Tok.isNot(tok::annot_pragma_openmp_end))
745 ConsumeAnyToken();
746 }
747 // Parse ':'
748 if (Tok.is(tok::colon))
749 ConsumeAnyToken();
750 else
751 Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
752 KindModifier = getOpenMPSimpleClauseType(
753 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
754 }
755 Arg[ScheduleKind] = KindModifier;
756 KLoc[ScheduleKind] = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000757 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
758 Tok.isNot(tok::annot_pragma_openmp_end))
759 ConsumeAnyToken();
Alexey Bataev6402bca2015-12-28 07:25:51 +0000760 if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
761 Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
762 Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000763 Tok.is(tok::comma))
764 DelimLoc = ConsumeAnyToken();
Carlo Bertollib4adf552016-01-15 18:50:31 +0000765 } else if (Kind == OMPC_dist_schedule) {
766 Arg.push_back(getOpenMPSimpleClauseType(
767 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
768 KLoc.push_back(Tok.getLocation());
769 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
770 Tok.isNot(tok::annot_pragma_openmp_end))
771 ConsumeAnyToken();
772 if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
773 DelimLoc = ConsumeAnyToken();
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000774 } else {
775 assert(Kind == OMPC_if);
Alexey Bataev6402bca2015-12-28 07:25:51 +0000776 KLoc.push_back(Tok.getLocation());
777 Arg.push_back(ParseOpenMPDirectiveKind(*this));
778 if (Arg.back() != OMPD_unknown) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000779 ConsumeToken();
780 if (Tok.is(tok::colon))
781 DelimLoc = ConsumeToken();
782 else
783 Diag(Tok, diag::warn_pragma_expected_colon)
784 << "directive name modifier";
785 }
786 }
Alexey Bataev56dafe82014-06-20 07:16:17 +0000787
Carlo Bertollib4adf552016-01-15 18:50:31 +0000788 bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
789 (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
790 Kind == OMPC_if;
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000791 if (NeedAnExpression) {
792 SourceLocation ELoc = Tok.getLocation();
Alexey Bataev56dafe82014-06-20 07:16:17 +0000793 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
794 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000795 Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000796 }
797
798 // Parse ')'.
799 T.consumeClose();
800
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000801 if (NeedAnExpression && Val.isInvalid())
802 return nullptr;
803
Alexey Bataev56dafe82014-06-20 07:16:17 +0000804 return Actions.ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000805 Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +0000806 T.getCloseLocation());
807}
808
Alexey Bataevc5e02582014-06-16 07:08:35 +0000809static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
810 UnqualifiedId &ReductionId) {
811 SourceLocation TemplateKWLoc;
812 if (ReductionIdScopeSpec.isEmpty()) {
813 auto OOK = OO_None;
814 switch (P.getCurToken().getKind()) {
815 case tok::plus:
816 OOK = OO_Plus;
817 break;
818 case tok::minus:
819 OOK = OO_Minus;
820 break;
821 case tok::star:
822 OOK = OO_Star;
823 break;
824 case tok::amp:
825 OOK = OO_Amp;
826 break;
827 case tok::pipe:
828 OOK = OO_Pipe;
829 break;
830 case tok::caret:
831 OOK = OO_Caret;
832 break;
833 case tok::ampamp:
834 OOK = OO_AmpAmp;
835 break;
836 case tok::pipepipe:
837 OOK = OO_PipePipe;
838 break;
839 default:
840 break;
841 }
842 if (OOK != OO_None) {
843 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000844 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000845 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
846 return false;
847 }
848 }
849 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
850 /*AllowDestructorName*/ false,
David Blaikieefdccaa2016-01-15 23:43:34 +0000851 /*AllowConstructorName*/ false, nullptr,
Alexey Bataevc5e02582014-06-16 07:08:35 +0000852 TemplateKWLoc, ReductionId);
853}
854
Alexander Musman1bb328c2014-06-04 13:06:39 +0000855/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000856/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000857///
858/// private-clause:
859/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000860/// firstprivate-clause:
861/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000862/// lastprivate-clause:
863/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000864/// shared-clause:
865/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000866/// linear-clause:
Alexey Bataev182227b2015-08-20 10:54:39 +0000867/// 'linear' '(' linear-list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000868/// aligned-clause:
869/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000870/// reduction-clause:
871/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000872/// copyprivate-clause:
873/// 'copyprivate' '(' list ')'
874/// flush-clause:
875/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000876/// depend-clause:
Alexey Bataeveb482352015-12-18 05:05:56 +0000877/// 'depend' '(' in | out | inout : list | source ')'
Kelvin Li0bff7af2015-11-23 05:32:03 +0000878/// map-clause:
879/// 'map' '(' [ [ always , ]
880/// to | from | tofrom | alloc | release | delete ':' ] list ')';
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000881///
Alexey Bataev182227b2015-08-20 10:54:39 +0000882/// For 'linear' clause linear-list may have the following forms:
883/// list
884/// modifier(list)
885/// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
Alexey Bataeveb482352015-12-18 05:05:56 +0000886OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
887 OpenMPClauseKind Kind) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000888 SourceLocation Loc = Tok.getLocation();
889 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000890 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000891 // Optional scope specifier and unqualified id for reduction identifier.
892 CXXScopeSpec ReductionIdScopeSpec;
893 UnqualifiedId ReductionId;
894 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000895 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
Alexey Bataev182227b2015-08-20 10:54:39 +0000896 // OpenMP 4.1 [2.15.3.7, linear Clause]
897 // If no modifier is specified it is assumed to be val.
898 OpenMPLinearClauseKind LinearModifier = OMPC_LINEAR_val;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000899 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
900 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
Samuel Antao23abd722016-01-19 20:40:49 +0000901 bool MapTypeIsImplicit = false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000902 bool MapTypeModifierSpecified = false;
903 bool UnexpectedId = false;
904 SourceLocation DepLinMapLoc;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000905
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000906 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000907 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000908 if (T.expectAndConsume(diag::err_expected_lparen_after,
909 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000910 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000911
Alexey Bataev182227b2015-08-20 10:54:39 +0000912 bool NeedRParenForLinear = false;
913 BalancedDelimiterTracker LinearT(*this, tok::l_paren,
914 tok::annot_pragma_openmp_end);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000915 // Handle reduction-identifier for reduction clause.
916 if (Kind == OMPC_reduction) {
917 ColonProtectionRAIIObject ColonRAII(*this);
918 if (getLangOpts().CPlusPlus) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000919 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, nullptr, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000920 }
921 InvalidReductionId =
922 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
923 if (InvalidReductionId) {
924 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
925 StopBeforeMatch);
926 }
927 if (Tok.is(tok::colon)) {
928 ColonLoc = ConsumeToken();
929 } else {
930 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
931 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000932 } else if (Kind == OMPC_depend) {
933 // Handle dependency type for depend clause.
934 ColonProtectionRAIIObject ColonRAII(*this);
935 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
936 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000937 DepLinMapLoc = Tok.getLocation();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000938
939 if (DepKind == OMPC_DEPEND_unknown) {
940 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
941 StopBeforeMatch);
942 } else {
943 ConsumeToken();
Alexey Bataeveb482352015-12-18 05:05:56 +0000944 // Special processing for depend(source) clause.
945 if (DKind == OMPD_ordered && DepKind == OMPC_DEPEND_source) {
946 // Parse ')'.
947 T.consumeClose();
948 return Actions.ActOnOpenMPVarListClause(
949 Kind, llvm::None, /*TailExpr=*/nullptr, Loc, LOpen,
950 /*ColonLoc=*/SourceLocation(), Tok.getLocation(),
951 ReductionIdScopeSpec, DeclarationNameInfo(), DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +0000952 LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
953 DepLinMapLoc);
Alexey Bataeveb482352015-12-18 05:05:56 +0000954 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000955 }
956 if (Tok.is(tok::colon)) {
957 ColonLoc = ConsumeToken();
958 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +0000959 Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
960 : diag::warn_pragma_expected_colon)
961 << "dependency type";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000962 }
Alexey Bataev182227b2015-08-20 10:54:39 +0000963 } else if (Kind == OMPC_linear) {
964 // Try to parse modifier if any.
965 if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
Alexey Bataev182227b2015-08-20 10:54:39 +0000966 LinearModifier = static_cast<OpenMPLinearClauseKind>(
Alexey Bataev1185e192015-08-20 12:15:57 +0000967 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
Kelvin Li0bff7af2015-11-23 05:32:03 +0000968 DepLinMapLoc = ConsumeToken();
Alexey Bataev182227b2015-08-20 10:54:39 +0000969 LinearT.consumeOpen();
970 NeedRParenForLinear = true;
971 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000972 } else if (Kind == OMPC_map) {
973 // Handle map type for map clause.
974 ColonProtectionRAIIObject ColonRAII(*this);
975
976 // the first identifier may be a list item, a map-type or
977 // a map-type-modifier
978 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
979 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
980 DepLinMapLoc = Tok.getLocation();
981 bool ColonExpected = false;
982
983 if (Tok.is(tok::identifier)) {
984 if (PP.LookAhead(0).is(tok::colon)) {
985 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
986 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
987 if (MapType == OMPC_MAP_unknown) {
988 Diag(Tok, diag::err_omp_unknown_map_type);
989 } else if (MapType == OMPC_MAP_always) {
990 Diag(Tok, diag::err_omp_map_type_missing);
991 }
992 ConsumeToken();
993 } else if (PP.LookAhead(0).is(tok::comma)) {
994 if (PP.LookAhead(1).is(tok::identifier) &&
995 PP.LookAhead(2).is(tok::colon)) {
996 MapTypeModifier =
997 static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
998 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
999 if (MapTypeModifier != OMPC_MAP_always) {
1000 Diag(Tok, diag::err_omp_unknown_map_type_modifier);
1001 MapTypeModifier = OMPC_MAP_unknown;
1002 } else {
1003 MapTypeModifierSpecified = true;
1004 }
1005
1006 ConsumeToken();
1007 ConsumeToken();
1008
1009 MapType = static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
1010 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
1011 if (MapType == OMPC_MAP_unknown || MapType == OMPC_MAP_always) {
1012 Diag(Tok, diag::err_omp_unknown_map_type);
1013 }
1014 ConsumeToken();
1015 } else {
1016 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001017 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001018 }
1019 } else {
1020 MapType = OMPC_MAP_tofrom;
Samuel Antao23abd722016-01-19 20:40:49 +00001021 MapTypeIsImplicit = true;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001022 }
1023 } else {
1024 UnexpectedId = true;
1025 }
1026
1027 if (Tok.is(tok::colon)) {
1028 ColonLoc = ConsumeToken();
1029 } else if (ColonExpected) {
1030 Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
1031 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00001032 }
1033
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001034 SmallVector<Expr *, 5> Vars;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001035 bool IsComma =
1036 ((Kind != OMPC_reduction) && (Kind != OMPC_depend) &&
1037 (Kind != OMPC_map)) ||
1038 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
1039 ((Kind == OMPC_map) && (UnexpectedId || MapType != OMPC_MAP_unknown) &&
1040 (!MapTypeModifierSpecified ||
1041 (MapTypeModifierSpecified && MapTypeModifier == OMPC_MAP_always))) ||
1042 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001043 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +00001044 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001045 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001046 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001047 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +00001048 ExprResult VarExpr =
1049 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001050 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001051 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001052 } else {
1053 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +00001054 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001055 }
1056 // Skip ',' if any
1057 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +00001058 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001059 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +00001060 else if (Tok.isNot(tok::r_paren) &&
1061 Tok.isNot(tok::annot_pragma_openmp_end) &&
1062 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +00001063 Diag(Tok, diag::err_omp_expected_punc)
1064 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
1065 : getOpenMPClauseName(Kind))
1066 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +00001067 }
1068
Alexey Bataev182227b2015-08-20 10:54:39 +00001069 // Parse ')' for linear clause with modifier.
1070 if (NeedRParenForLinear)
1071 LinearT.consumeClose();
1072
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001073 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +00001074 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001075 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
1076 if (MustHaveTail) {
1077 ColonLoc = Tok.getLocation();
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001078 SourceLocation ELoc = ConsumeToken();
1079 ExprResult Tail = ParseAssignmentExpression();
1080 Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00001081 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001082 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001083 else
1084 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
1085 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001086 }
1087
1088 // Parse ')'.
1089 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001090 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
1091 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
Kelvin Li0bff7af2015-11-23 05:32:03 +00001092 (Kind == OMPC_map && MapType == OMPC_MAP_unknown) ||
1093 InvalidReductionId) {
Craig Topper161e4db2014-05-21 06:02:52 +00001094 return nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +00001095 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001096
Alexey Bataevc5e02582014-06-16 07:08:35 +00001097 return Actions.ActOnOpenMPVarListClause(
1098 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
1099 ReductionIdScopeSpec,
1100 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00001101 : DeclarationNameInfo(),
Samuel Antao23abd722016-01-19 20:40:49 +00001102 DepKind, LinearModifier, MapTypeModifier, MapType, MapTypeIsImplicit,
1103 DepLinMapLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001104}
1105