blob: 0113a3157c250166d8ef2fe34b5b2f6126440344 [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"
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Alexey Bataev4acb8592014-07-07 13:01:15 +000028static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000029 // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
30 // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
31 // TODO: add other combined directives in topological order.
32 const OpenMPDirectiveKind F[][3] = {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000033 {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/,
34 OMPD_cancellation_point},
35 {OMPD_for, OMPD_simd, OMPD_for_simd},
36 {OMPD_parallel, OMPD_for, OMPD_parallel_for},
37 {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
38 {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}};
Alexey Bataev4acb8592014-07-07 13:01:15 +000039 auto Tok = P.getCurToken();
40 auto DKind =
41 Tok.isAnnotation()
42 ? OMPD_unknown
43 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000044 bool TokenMatched = false;
Alexander Musmanf82886e2014-09-18 05:12:34 +000045 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000046 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
47 TokenMatched =
48 (i == 0) &&
49 !P.getPreprocessor().getSpelling(Tok).compare("cancellation");
50 } else {
51 TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown;
52 }
53 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000054 Tok = P.getPreprocessor().LookAhead(0);
55 auto SDKind =
56 Tok.isAnnotation()
57 ? OMPD_unknown
58 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Alexey Bataev6d4ed052015-07-01 06:57:41 +000059 if (!Tok.isAnnotation() && DKind == OMPD_unknown) {
60 TokenMatched =
61 (i == 0) && !P.getPreprocessor().getSpelling(Tok).compare("point");
62 } else {
63 TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown;
64 }
65 if (TokenMatched) {
Alexander Musmanf82886e2014-09-18 05:12:34 +000066 P.ConsumeToken();
67 DKind = F[i][2];
68 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000069 }
70 }
71 return DKind;
72}
73
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000074/// \brief Parsing of declarative OpenMP directives.
75///
76/// threadprivate-directive:
77/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000078///
79Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
80 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000081 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000082
83 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000084 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000085 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000086
87 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000088 case OMPD_threadprivate:
89 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000090 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000091 // The last seen token is annot_pragma_openmp_end - need to check for
92 // extra tokens.
93 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
94 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000095 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000096 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000097 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000098 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000099 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +0000100 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +0000101 }
102 break;
103 case OMPD_unknown:
104 Diag(Tok, diag::err_omp_unknown_directive);
105 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000106 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000107 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000108 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +0000109 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000110 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000111 case OMPD_taskwait:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000112 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +0000113 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000114 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000115 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000116 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000117 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000118 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000119 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000120 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000121 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000122 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000123 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000124 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000125 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000126 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000127 case OMPD_teams:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000128 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000129 case OMPD_cancel:
Alexey Bataeva769e072013-03-22 06:34:35 +0000130 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000131 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000132 break;
133 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000134 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000135 return DeclGroupPtrTy();
136}
137
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000138/// \brief Parsing of declarative or executable OpenMP directives.
139///
140/// threadprivate-directive:
141/// annot_pragma_openmp 'threadprivate' simple-variable-list
142/// annot_pragma_openmp_end
143///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000144/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000145/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000146/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
147/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000148/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000149/// 'for simd' | 'parallel for simd' | 'target' | 'teams' | 'taskgroup'
150/// {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000151/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000152///
Alexey Bataev68446b72014-07-18 07:47:19 +0000153StmtResult
154Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000155 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000156 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000157 SmallVector<Expr *, 5> Identifiers;
158 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000159 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000160 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000161 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000162 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000163 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000164 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000165 OpenMPDirectiveKind CancelRegion = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000166 // Name of critical directive.
167 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000169 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000170 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000171
172 switch (DKind) {
173 case OMPD_threadprivate:
174 ConsumeToken();
175 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
176 // The last seen token is annot_pragma_openmp_end - need to check for
177 // extra tokens.
178 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
179 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000180 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000181 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182 }
183 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000184 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000185 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
186 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000187 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000188 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000189 case OMPD_flush:
190 if (PP.LookAhead(0).is(tok::l_paren)) {
191 FlushHasClause = true;
192 // Push copy of the current token back to stream to properly parse
193 // pseudo-clause OMPFlushClause.
194 PP.EnterToken(Tok);
195 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000196 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000197 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000198 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000199 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +0000200 case OMPD_cancel:
Alexey Bataev68446b72014-07-18 07:47:19 +0000201 if (!StandAloneAllowed) {
202 Diag(Tok, diag::err_omp_immediate_directive)
203 << getOpenMPDirectiveName(DKind);
204 }
205 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000206 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000207 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000209 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000210 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000211 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000212 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000213 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000214 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000215 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000216 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000217 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000218 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000219 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000220 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000221 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000222 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000223 case OMPD_teams:
224 case OMPD_taskgroup: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000225 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000226 // Parse directive name of the 'critical' directive if any.
227 if (DKind == OMPD_critical) {
228 BalancedDelimiterTracker T(*this, tok::l_paren,
229 tok::annot_pragma_openmp_end);
230 if (!T.consumeOpen()) {
231 if (Tok.isAnyIdentifier()) {
232 DirName =
233 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
234 ConsumeAnyToken();
235 } else {
236 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
237 }
238 T.consumeClose();
239 }
Alexey Bataev80909872015-07-02 11:25:17 +0000240 } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000241 CancelRegion = ParseOpenMPDirectiveKind(*this);
242 if (Tok.isNot(tok::annot_pragma_openmp_end))
243 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000245
Alexey Bataevf29276e2014-06-18 04:14:57 +0000246 if (isOpenMPLoopDirective(DKind))
247 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
248 if (isOpenMPSimdDirective(DKind))
249 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
250 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000251 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000253 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000254 OpenMPClauseKind CKind =
255 Tok.isAnnotation()
256 ? OMPC_unknown
257 : FlushHasClause ? OMPC_flush
258 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000259 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000260 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000261 OMPClause *Clause =
262 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263 FirstClauses[CKind].setInt(true);
264 if (Clause) {
265 FirstClauses[CKind].setPointer(Clause);
266 Clauses.push_back(Clause);
267 }
268
269 // Skip ',' if any.
270 if (Tok.is(tok::comma))
271 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000272 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000273 }
274 // End location of the directive.
275 EndLoc = Tok.getLocation();
276 // Consume final annot_pragma_openmp_end.
277 ConsumeToken();
278
279 StmtResult AssociatedStmt;
280 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000281 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000282 // The body is a block scope like in Lambdas and Blocks.
283 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000284 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000285 Actions.ActOnStartOfCompoundStmt();
286 // Parse statement
287 AssociatedStmt = ParseStatement();
288 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000289 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
290 CreateDirective = AssociatedStmt.isUsable();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000291 }
292 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000293 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000294 DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
295 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000296
297 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000298 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000299 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000301 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000302 case OMPD_unknown:
303 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000304 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000305 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000306 }
307 return Directive;
308}
309
Alexey Bataeva769e072013-03-22 06:34:35 +0000310/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000311/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000312///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000313/// simple-variable-list:
314/// '(' id-expression {, id-expression} ')'
315///
316bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
317 SmallVectorImpl<Expr *> &VarList,
318 bool AllowScopeSpecifier) {
319 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000320 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000321 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000322 if (T.expectAndConsume(diag::err_expected_lparen_after,
323 getOpenMPDirectiveName(Kind)))
324 return true;
325 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000326 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000327
328 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000329 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000330 CXXScopeSpec SS;
331 SourceLocation TemplateKWLoc;
332 UnqualifiedId Name;
333 // Read var name.
334 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000335 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000336
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000337 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
338 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000339 IsCorrect = false;
340 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000341 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000342 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
343 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000344 IsCorrect = false;
345 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000346 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000347 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
348 Tok.isNot(tok::annot_pragma_openmp_end)) {
349 IsCorrect = false;
350 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000351 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000352 Diag(PrevTok.getLocation(), diag::err_expected)
353 << tok::identifier
354 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000355 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000356 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000357 ExprResult Res =
358 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000359 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000360 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000361 }
362 // Consume ','.
363 if (Tok.is(tok::comma)) {
364 ConsumeToken();
365 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000366 }
367
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000368 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000369 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000370 IsCorrect = false;
371 }
372
373 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000374 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000375
376 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000377}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000378
379/// \brief Parsing of OpenMP clauses.
380///
381/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000382/// if-clause | final-clause | num_threads-clause | safelen-clause |
383/// default-clause | private-clause | firstprivate-clause | shared-clause
384/// | linear-clause | aligned-clause | collapse-clause |
385/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000386/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000387/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000388/// update-clause | capture-clause | seq_cst-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000389///
390OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
391 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000392 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000393 bool ErrorFound = false;
394 // Check if clause is allowed for the given directive.
395 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000396 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
397 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000398 ErrorFound = true;
399 }
400
401 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000402 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000403 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000404 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000405 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000406 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000407 // OpenMP [2.5, Restrictions]
408 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000409 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000410 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000411 // Only one safelen clause can appear on a simd directive.
412 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000413 // OpenMP [2.11.1, task Construct, Restrictions]
414 // At most one if clause can appear on the directive.
415 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000416 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000417 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
418 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000419 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000420 }
421
422 Clause = ParseOpenMPSingleExprClause(CKind);
423 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000424 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000425 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000426 // OpenMP [2.14.3.1, Restrictions]
427 // Only a single default clause may be specified on a parallel, task or
428 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000429 // OpenMP [2.5, parallel Construct, Restrictions]
430 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000431 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000432 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
433 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000434 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000435 }
436
437 Clause = ParseOpenMPSimpleClause(CKind);
438 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000439 case OMPC_schedule:
440 // OpenMP [2.7.1, Restrictions, p. 3]
441 // Only one schedule clause can appear on a loop directive.
442 if (!FirstClause) {
443 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
444 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000445 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000446 }
447
448 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
449 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000450 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000451 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000452 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000453 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000454 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000455 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000456 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000457 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000458 case OMPC_seq_cst:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000459 // OpenMP [2.7.1, Restrictions, p. 9]
460 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000461 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
462 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000463 if (!FirstClause) {
464 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
465 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000466 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000467 }
468
469 Clause = ParseOpenMPClause(CKind);
470 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000471 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000472 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000473 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000475 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000476 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000477 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000478 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000479 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000480 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000481 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000482 Clause = ParseOpenMPVarListClause(CKind);
483 break;
484 case OMPC_unknown:
485 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000486 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000487 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000488 break;
489 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000490 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
491 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000492 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000493 break;
494 }
Craig Topper161e4db2014-05-21 06:02:52 +0000495 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000496}
497
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000498/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000499/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000500/// 'thread_limit'.
501///
502/// if-clause:
503/// 'if' '(' expression ')'
504///
Alexey Bataev3778b602014-07-17 07:32:53 +0000505/// final-clause:
506/// 'final' '(' expression ')'
507///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000508/// num_threads-clause:
509/// 'num_threads' '(' expression ')'
510///
511/// safelen-clause:
512/// 'safelen' '(' expression ')'
513///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000514/// collapse-clause:
515/// 'collapse' '(' expression ')'
516///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000517OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
518 SourceLocation Loc = ConsumeToken();
519
520 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
521 if (T.expectAndConsume(diag::err_expected_lparen_after,
522 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000523 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000524
525 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
526 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
527
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000528 // Parse ')'.
529 T.consumeClose();
530
531 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000532 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000533
Alexey Bataeva55ed262014-05-28 06:15:33 +0000534 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000535 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000536}
537
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000538/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000539///
540/// default-clause:
541/// 'default' '(' 'none' | 'shared' ')
542///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000543/// proc_bind-clause:
544/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
545///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000546OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
547 SourceLocation Loc = Tok.getLocation();
548 SourceLocation LOpen = ConsumeToken();
549 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000551 if (T.expectAndConsume(diag::err_expected_lparen_after,
552 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000553 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000554
Alexey Bataeva55ed262014-05-28 06:15:33 +0000555 unsigned Type = getOpenMPSimpleClauseType(
556 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000557 SourceLocation TypeLoc = Tok.getLocation();
558 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
559 Tok.isNot(tok::annot_pragma_openmp_end))
560 ConsumeAnyToken();
561
562 // Parse ')'.
563 T.consumeClose();
564
565 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
566 Tok.getLocation());
567}
568
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000569/// \brief Parsing of OpenMP clauses like 'ordered'.
570///
571/// ordered-clause:
572/// 'ordered'
573///
Alexey Bataev236070f2014-06-20 11:19:47 +0000574/// nowait-clause:
575/// 'nowait'
576///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000577/// untied-clause:
578/// 'untied'
579///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000580/// mergeable-clause:
581/// 'mergeable'
582///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000583/// read-clause:
584/// 'read'
585///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000586OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
587 SourceLocation Loc = Tok.getLocation();
588 ConsumeAnyToken();
589
590 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
591}
592
593
Alexey Bataev56dafe82014-06-20 07:16:17 +0000594/// \brief Parsing of OpenMP clauses with single expressions and some additional
595/// argument like 'schedule' or 'dist_schedule'.
596///
597/// schedule-clause:
598/// 'schedule' '(' kind [',' expression ] ')'
599///
600OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
601 SourceLocation Loc = ConsumeToken();
602 SourceLocation CommaLoc;
603 // Parse '('.
604 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
605 if (T.expectAndConsume(diag::err_expected_lparen_after,
606 getOpenMPClauseName(Kind)))
607 return nullptr;
608
609 ExprResult Val;
610 unsigned Type = getOpenMPSimpleClauseType(
611 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
612 SourceLocation KLoc = Tok.getLocation();
613 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
614 Tok.isNot(tok::annot_pragma_openmp_end))
615 ConsumeAnyToken();
616
617 if (Kind == OMPC_schedule &&
618 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
619 Type == OMPC_SCHEDULE_guided) &&
620 Tok.is(tok::comma)) {
621 CommaLoc = ConsumeAnyToken();
622 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
623 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
624 if (Val.isInvalid())
625 return nullptr;
626 }
627
628 // Parse ')'.
629 T.consumeClose();
630
631 return Actions.ActOnOpenMPSingleExprWithArgClause(
632 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
633 T.getCloseLocation());
634}
635
Alexey Bataevc5e02582014-06-16 07:08:35 +0000636static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
637 UnqualifiedId &ReductionId) {
638 SourceLocation TemplateKWLoc;
639 if (ReductionIdScopeSpec.isEmpty()) {
640 auto OOK = OO_None;
641 switch (P.getCurToken().getKind()) {
642 case tok::plus:
643 OOK = OO_Plus;
644 break;
645 case tok::minus:
646 OOK = OO_Minus;
647 break;
648 case tok::star:
649 OOK = OO_Star;
650 break;
651 case tok::amp:
652 OOK = OO_Amp;
653 break;
654 case tok::pipe:
655 OOK = OO_Pipe;
656 break;
657 case tok::caret:
658 OOK = OO_Caret;
659 break;
660 case tok::ampamp:
661 OOK = OO_AmpAmp;
662 break;
663 case tok::pipepipe:
664 OOK = OO_PipePipe;
665 break;
666 default:
667 break;
668 }
669 if (OOK != OO_None) {
670 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000671 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000672 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
673 return false;
674 }
675 }
676 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
677 /*AllowDestructorName*/ false,
678 /*AllowConstructorName*/ false, ParsedType(),
679 TemplateKWLoc, ReductionId);
680}
681
Alexander Musman1bb328c2014-06-04 13:06:39 +0000682/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000683/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000684///
685/// private-clause:
686/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000687/// firstprivate-clause:
688/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000689/// lastprivate-clause:
690/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691/// shared-clause:
692/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000693/// linear-clause:
694/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000695/// aligned-clause:
696/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000697/// reduction-clause:
698/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000699/// copyprivate-clause:
700/// 'copyprivate' '(' list ')'
701/// flush-clause:
702/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000703/// depend-clause:
704/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000705///
706OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
707 SourceLocation Loc = Tok.getLocation();
708 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000709 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000710 // Optional scope specifier and unqualified id for reduction identifier.
711 CXXScopeSpec ReductionIdScopeSpec;
712 UnqualifiedId ReductionId;
713 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000714 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
715 SourceLocation DepLoc;
716
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000718 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000719 if (T.expectAndConsume(diag::err_expected_lparen_after,
720 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000721 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000722
Alexey Bataevc5e02582014-06-16 07:08:35 +0000723 // Handle reduction-identifier for reduction clause.
724 if (Kind == OMPC_reduction) {
725 ColonProtectionRAIIObject ColonRAII(*this);
726 if (getLangOpts().CPlusPlus) {
727 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
728 }
729 InvalidReductionId =
730 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
731 if (InvalidReductionId) {
732 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
733 StopBeforeMatch);
734 }
735 if (Tok.is(tok::colon)) {
736 ColonLoc = ConsumeToken();
737 } else {
738 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
739 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000740 } else if (Kind == OMPC_depend) {
741 // Handle dependency type for depend clause.
742 ColonProtectionRAIIObject ColonRAII(*this);
743 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
744 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
745 DepLoc = Tok.getLocation();
746
747 if (DepKind == OMPC_DEPEND_unknown) {
748 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
749 StopBeforeMatch);
750 } else {
751 ConsumeToken();
752 }
753 if (Tok.is(tok::colon)) {
754 ColonLoc = ConsumeToken();
755 } else {
756 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
757 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000758 }
759
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000760 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000761 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
762 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
763 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000764 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000765 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000766 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000767 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000768 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000769 ExprResult VarExpr =
770 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000772 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000773 } else {
774 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000775 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000776 }
777 // Skip ',' if any
778 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000779 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000780 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000781 else if (Tok.isNot(tok::r_paren) &&
782 Tok.isNot(tok::annot_pragma_openmp_end) &&
783 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000784 Diag(Tok, diag::err_omp_expected_punc)
785 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
786 : getOpenMPClauseName(Kind))
787 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000788 }
789
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000790 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000791 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000792 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
793 if (MustHaveTail) {
794 ColonLoc = Tok.getLocation();
795 ConsumeToken();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000796 ExprResult Tail =
797 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexander Musman8dba6642014-04-22 13:09:42 +0000798 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000799 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000800 else
801 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
802 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000803 }
804
805 // Parse ')'.
806 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000807 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
808 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
809 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000810 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811
Alexey Bataevc5e02582014-06-16 07:08:35 +0000812 return Actions.ActOnOpenMPVarListClause(
813 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
814 ReductionIdScopeSpec,
815 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000816 : DeclarationNameInfo(),
817 DepKind, DepLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000818}
819