blob: 3a119a601e226929d27aea3057a09894a0401672 [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] = {
33 { OMPD_for, OMPD_simd, OMPD_for_simd },
34 { OMPD_parallel, OMPD_for, OMPD_parallel_for },
Alexander Musmane4e893b2014-09-23 09:33:00 +000035 { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd },
Alexander Musmanf82886e2014-09-18 05:12:34 +000036 { OMPD_parallel, OMPD_sections, OMPD_parallel_sections }
37 };
Alexey Bataev4acb8592014-07-07 13:01:15 +000038 auto Tok = P.getCurToken();
39 auto DKind =
40 Tok.isAnnotation()
41 ? OMPD_unknown
42 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
Alexander Musmanf82886e2014-09-18 05:12:34 +000043 for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) {
44 if (DKind == F[i][0]) {
45 Tok = P.getPreprocessor().LookAhead(0);
46 auto SDKind =
47 Tok.isAnnotation()
48 ? OMPD_unknown
49 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
50 if (SDKind == F[i][1]) {
51 P.ConsumeToken();
52 DKind = F[i][2];
53 }
Alexey Bataev4acb8592014-07-07 13:01:15 +000054 }
55 }
56 return DKind;
57}
58
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000059/// \brief Parsing of declarative OpenMP directives.
60///
61/// threadprivate-directive:
62/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000063///
64Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
65 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000066 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000067
68 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000069 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000070 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000071
72 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000073 case OMPD_threadprivate:
74 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000075 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000076 // The last seen token is annot_pragma_openmp_end - need to check for
77 // extra tokens.
78 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
79 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000080 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000081 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000082 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000083 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000084 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +000085 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000086 }
87 break;
88 case OMPD_unknown:
89 Diag(Tok, diag::err_omp_unknown_directive);
90 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000091 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000092 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000093 case OMPD_task:
Alexey Bataev68446b72014-07-18 07:47:19 +000094 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +000095 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +000096 case OMPD_taskwait:
Alexey Bataev6125da92014-07-21 11:26:11 +000097 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +000098 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +000099 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000100 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000101 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000102 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000103 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000104 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000105 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000106 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000107 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000108 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000109 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000110 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000111 case OMPD_teams:
Alexey Bataeva769e072013-03-22 06:34:35 +0000112 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000113 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000114 break;
115 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000116 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000117 return DeclGroupPtrTy();
118}
119
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120/// \brief Parsing of declarative or executable OpenMP directives.
121///
122/// threadprivate-directive:
123/// annot_pragma_openmp 'threadprivate' simple-variable-list
124/// annot_pragma_openmp_end
125///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000126/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000127/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000128/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
129/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000130/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Alexey Bataev13314bf2014-10-09 04:18:56 +0000131/// 'for simd' | 'parallel for simd' | 'target' | 'teams' {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000132/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000133///
Alexey Bataev68446b72014-07-18 07:47:19 +0000134StmtResult
135Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000136 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000137 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000138 SmallVector<Expr *, 5> Identifiers;
139 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000140 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000141 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000142 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000143 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000144 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000145 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146 // Name of critical directive.
147 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000148 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000149 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000150 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000151
152 switch (DKind) {
153 case OMPD_threadprivate:
154 ConsumeToken();
155 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
156 // The last seen token is annot_pragma_openmp_end - need to check for
157 // extra tokens.
158 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
159 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000160 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000161 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000162 }
163 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000164 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000165 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
166 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000167 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000169 case OMPD_flush:
170 if (PP.LookAhead(0).is(tok::l_paren)) {
171 FlushHasClause = true;
172 // Push copy of the current token back to stream to properly parse
173 // pseudo-clause OMPFlushClause.
174 PP.EnterToken(Tok);
175 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000176 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000177 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000178 case OMPD_taskwait:
Alexey Bataev68446b72014-07-18 07:47:19 +0000179 if (!StandAloneAllowed) {
180 Diag(Tok, diag::err_omp_immediate_directive)
181 << getOpenMPDirectiveName(DKind);
182 }
183 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000184 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000185 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000186 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000187 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000188 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000189 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000190 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000191 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000192 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000193 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000194 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000195 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000196 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000197 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000198 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000199 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000200 case OMPD_target:
201 case OMPD_teams: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000202 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000203 // Parse directive name of the 'critical' directive if any.
204 if (DKind == OMPD_critical) {
205 BalancedDelimiterTracker T(*this, tok::l_paren,
206 tok::annot_pragma_openmp_end);
207 if (!T.consumeOpen()) {
208 if (Tok.isAnyIdentifier()) {
209 DirName =
210 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
211 ConsumeAnyToken();
212 } else {
213 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
214 }
215 T.consumeClose();
216 }
217 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218
Alexey Bataevf29276e2014-06-18 04:14:57 +0000219 if (isOpenMPLoopDirective(DKind))
220 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
221 if (isOpenMPSimdDirective(DKind))
222 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
223 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000224 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000226 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000227 OpenMPClauseKind CKind =
228 Tok.isAnnotation()
229 ? OMPC_unknown
230 : FlushHasClause ? OMPC_flush
231 : getOpenMPClauseKind(PP.getSpelling(Tok));
232 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000233 OMPClause *Clause =
234 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000235 FirstClauses[CKind].setInt(true);
236 if (Clause) {
237 FirstClauses[CKind].setPointer(Clause);
238 Clauses.push_back(Clause);
239 }
240
241 // Skip ',' if any.
242 if (Tok.is(tok::comma))
243 ConsumeToken();
244 }
245 // End location of the directive.
246 EndLoc = Tok.getLocation();
247 // Consume final annot_pragma_openmp_end.
248 ConsumeToken();
249
250 StmtResult AssociatedStmt;
251 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000252 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000253 // The body is a block scope like in Lambdas and Blocks.
254 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000255 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000256 Actions.ActOnStartOfCompoundStmt();
257 // Parse statement
258 AssociatedStmt = ParseStatement();
259 Actions.ActOnFinishOfCompoundStmt();
260 if (!AssociatedStmt.isUsable()) {
261 Actions.ActOnCapturedRegionError();
262 CreateDirective = false;
263 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000264 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000265 CreateDirective = AssociatedStmt.isUsable();
266 }
267 }
268 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000269 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000270 DKind, DirName, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000271
272 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000274 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000275 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000276 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000277 case OMPD_unknown:
278 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000279 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000280 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000281 }
282 return Directive;
283}
284
Alexey Bataeva769e072013-03-22 06:34:35 +0000285/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000286/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000287///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000288/// simple-variable-list:
289/// '(' id-expression {, id-expression} ')'
290///
291bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
292 SmallVectorImpl<Expr *> &VarList,
293 bool AllowScopeSpecifier) {
294 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000295 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000296 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 if (T.expectAndConsume(diag::err_expected_lparen_after,
298 getOpenMPDirectiveName(Kind)))
299 return true;
300 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000301 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000302
303 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000304 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000305 CXXScopeSpec SS;
306 SourceLocation TemplateKWLoc;
307 UnqualifiedId Name;
308 // Read var name.
309 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000310 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000311
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000312 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
313 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000314 IsCorrect = false;
315 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000316 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000317 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
318 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000319 IsCorrect = false;
320 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000321 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000322 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
323 Tok.isNot(tok::annot_pragma_openmp_end)) {
324 IsCorrect = false;
325 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000326 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000327 Diag(PrevTok.getLocation(), diag::err_expected)
328 << tok::identifier
329 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000330 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000331 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000332 ExprResult Res =
333 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000334 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000335 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000336 }
337 // Consume ','.
338 if (Tok.is(tok::comma)) {
339 ConsumeToken();
340 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000341 }
342
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000343 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000344 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000345 IsCorrect = false;
346 }
347
348 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000349 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000350
351 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000352}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000353
354/// \brief Parsing of OpenMP clauses.
355///
356/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000357/// if-clause | final-clause | num_threads-clause | safelen-clause |
358/// default-clause | private-clause | firstprivate-clause | shared-clause
359/// | linear-clause | aligned-clause | collapse-clause |
360/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000361/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
Alexey Bataev67a4f222014-07-23 10:25:33 +0000362/// mergeable-clause | flush-clause | read-clause | write-clause |
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000363/// update-clause | capture-clause | seq_cst-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000364///
365OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
366 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000367 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000368 bool ErrorFound = false;
369 // Check if clause is allowed for the given directive.
370 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000371 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
372 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000373 ErrorFound = true;
374 }
375
376 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000377 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000378 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000379 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000380 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000381 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000382 // OpenMP [2.5, Restrictions]
383 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000384 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000385 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000386 // Only one safelen clause can appear on a simd directive.
387 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000388 // OpenMP [2.11.1, task Construct, Restrictions]
389 // At most one if clause can appear on the directive.
390 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000391 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000392 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
393 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000394 ErrorFound = true;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000395 }
396
397 Clause = ParseOpenMPSingleExprClause(CKind);
398 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000399 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000400 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000401 // OpenMP [2.14.3.1, Restrictions]
402 // Only a single default clause may be specified on a parallel, task or
403 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000404 // OpenMP [2.5, parallel Construct, Restrictions]
405 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000406 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000407 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
408 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000409 ErrorFound = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000410 }
411
412 Clause = ParseOpenMPSimpleClause(CKind);
413 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000414 case OMPC_schedule:
415 // OpenMP [2.7.1, Restrictions, p. 3]
416 // Only one schedule clause can appear on a loop directive.
417 if (!FirstClause) {
418 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
419 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000420 ErrorFound = true;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000421 }
422
423 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
424 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000425 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000426 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000427 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000428 case OMPC_mergeable:
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000429 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +0000430 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +0000431 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +0000432 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000433 case OMPC_seq_cst:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000434 // OpenMP [2.7.1, Restrictions, p. 9]
435 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000436 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
437 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000438 if (!FirstClause) {
439 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
440 << getOpenMPClauseName(CKind);
Alexey Bataevdea47612014-07-23 07:46:59 +0000441 ErrorFound = true;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000442 }
443
444 Clause = ParseOpenMPClause(CKind);
445 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000447 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000448 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000450 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000451 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000452 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000453 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000454 case OMPC_copyprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +0000455 case OMPC_flush:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000456 Clause = ParseOpenMPVarListClause(CKind);
457 break;
458 case OMPC_unknown:
459 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000460 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000461 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000462 break;
463 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000464 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
465 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000466 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000467 break;
468 }
Craig Topper161e4db2014-05-21 06:02:52 +0000469 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000470}
471
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000472/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000473/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000474/// 'thread_limit'.
475///
476/// if-clause:
477/// 'if' '(' expression ')'
478///
Alexey Bataev3778b602014-07-17 07:32:53 +0000479/// final-clause:
480/// 'final' '(' expression ')'
481///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000482/// num_threads-clause:
483/// 'num_threads' '(' expression ')'
484///
485/// safelen-clause:
486/// 'safelen' '(' expression ')'
487///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000488/// collapse-clause:
489/// 'collapse' '(' expression ')'
490///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000491OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
492 SourceLocation Loc = ConsumeToken();
493
494 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
495 if (T.expectAndConsume(diag::err_expected_lparen_after,
496 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000497 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000498
499 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
500 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
501
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000502 // Parse ')'.
503 T.consumeClose();
504
505 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000506 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000507
Alexey Bataeva55ed262014-05-28 06:15:33 +0000508 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000509 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000510}
511
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000512/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000513///
514/// default-clause:
515/// 'default' '(' 'none' | 'shared' ')
516///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000517/// proc_bind-clause:
518/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
519///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000520OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
521 SourceLocation Loc = Tok.getLocation();
522 SourceLocation LOpen = ConsumeToken();
523 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000524 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000525 if (T.expectAndConsume(diag::err_expected_lparen_after,
526 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000527 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000528
Alexey Bataeva55ed262014-05-28 06:15:33 +0000529 unsigned Type = getOpenMPSimpleClauseType(
530 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000531 SourceLocation TypeLoc = Tok.getLocation();
532 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
533 Tok.isNot(tok::annot_pragma_openmp_end))
534 ConsumeAnyToken();
535
536 // Parse ')'.
537 T.consumeClose();
538
539 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
540 Tok.getLocation());
541}
542
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000543/// \brief Parsing of OpenMP clauses like 'ordered'.
544///
545/// ordered-clause:
546/// 'ordered'
547///
Alexey Bataev236070f2014-06-20 11:19:47 +0000548/// nowait-clause:
549/// 'nowait'
550///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000551/// untied-clause:
552/// 'untied'
553///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000554/// mergeable-clause:
555/// 'mergeable'
556///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000557/// read-clause:
558/// 'read'
559///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000560OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
561 SourceLocation Loc = Tok.getLocation();
562 ConsumeAnyToken();
563
564 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
565}
566
567
Alexey Bataev56dafe82014-06-20 07:16:17 +0000568/// \brief Parsing of OpenMP clauses with single expressions and some additional
569/// argument like 'schedule' or 'dist_schedule'.
570///
571/// schedule-clause:
572/// 'schedule' '(' kind [',' expression ] ')'
573///
574OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
575 SourceLocation Loc = ConsumeToken();
576 SourceLocation CommaLoc;
577 // Parse '('.
578 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
579 if (T.expectAndConsume(diag::err_expected_lparen_after,
580 getOpenMPClauseName(Kind)))
581 return nullptr;
582
583 ExprResult Val;
584 unsigned Type = getOpenMPSimpleClauseType(
585 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
586 SourceLocation KLoc = Tok.getLocation();
587 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
588 Tok.isNot(tok::annot_pragma_openmp_end))
589 ConsumeAnyToken();
590
591 if (Kind == OMPC_schedule &&
592 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
593 Type == OMPC_SCHEDULE_guided) &&
594 Tok.is(tok::comma)) {
595 CommaLoc = ConsumeAnyToken();
596 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
597 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
598 if (Val.isInvalid())
599 return nullptr;
600 }
601
602 // Parse ')'.
603 T.consumeClose();
604
605 return Actions.ActOnOpenMPSingleExprWithArgClause(
606 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
607 T.getCloseLocation());
608}
609
Alexey Bataevc5e02582014-06-16 07:08:35 +0000610static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
611 UnqualifiedId &ReductionId) {
612 SourceLocation TemplateKWLoc;
613 if (ReductionIdScopeSpec.isEmpty()) {
614 auto OOK = OO_None;
615 switch (P.getCurToken().getKind()) {
616 case tok::plus:
617 OOK = OO_Plus;
618 break;
619 case tok::minus:
620 OOK = OO_Minus;
621 break;
622 case tok::star:
623 OOK = OO_Star;
624 break;
625 case tok::amp:
626 OOK = OO_Amp;
627 break;
628 case tok::pipe:
629 OOK = OO_Pipe;
630 break;
631 case tok::caret:
632 OOK = OO_Caret;
633 break;
634 case tok::ampamp:
635 OOK = OO_AmpAmp;
636 break;
637 case tok::pipepipe:
638 OOK = OO_PipePipe;
639 break;
640 default:
641 break;
642 }
643 if (OOK != OO_None) {
644 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000645 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000646 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
647 return false;
648 }
649 }
650 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
651 /*AllowDestructorName*/ false,
652 /*AllowConstructorName*/ false, ParsedType(),
653 TemplateKWLoc, ReductionId);
654}
655
Alexander Musman1bb328c2014-06-04 13:06:39 +0000656/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000657/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000658///
659/// private-clause:
660/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000661/// firstprivate-clause:
662/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000663/// lastprivate-clause:
664/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000665/// shared-clause:
666/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000667/// linear-clause:
668/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000669/// aligned-clause:
670/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000671/// reduction-clause:
672/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000673/// copyprivate-clause:
674/// 'copyprivate' '(' list ')'
675/// flush-clause:
676/// 'flush' '(' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000677///
678OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
679 SourceLocation Loc = Tok.getLocation();
680 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000681 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000682 // Optional scope specifier and unqualified id for reduction identifier.
683 CXXScopeSpec ReductionIdScopeSpec;
684 UnqualifiedId ReductionId;
685 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000686 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000687 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000688 if (T.expectAndConsume(diag::err_expected_lparen_after,
689 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000690 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000691
Alexey Bataevc5e02582014-06-16 07:08:35 +0000692 // Handle reduction-identifier for reduction clause.
693 if (Kind == OMPC_reduction) {
694 ColonProtectionRAIIObject ColonRAII(*this);
695 if (getLangOpts().CPlusPlus) {
696 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
697 }
698 InvalidReductionId =
699 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
700 if (InvalidReductionId) {
701 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
702 StopBeforeMatch);
703 }
704 if (Tok.is(tok::colon)) {
705 ColonLoc = ConsumeToken();
706 } else {
707 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
708 }
709 }
710
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000711 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000712 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000713 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000714 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000715 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000716 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 // Parse variable
718 ExprResult VarExpr = ParseAssignmentExpression();
719 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000720 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000721 } else {
722 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000723 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000724 }
725 // Skip ',' if any
726 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000727 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000728 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000729 else if (Tok.isNot(tok::r_paren) &&
730 Tok.isNot(tok::annot_pragma_openmp_end) &&
731 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000732 Diag(Tok, diag::err_omp_expected_punc)
733 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
734 : getOpenMPClauseName(Kind))
735 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000736 }
737
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000738 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000739 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000740 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
741 if (MustHaveTail) {
742 ColonLoc = Tok.getLocation();
743 ConsumeToken();
744 ExprResult Tail = ParseAssignmentExpression();
745 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000746 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000747 else
748 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
749 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000750 }
751
752 // Parse ')'.
753 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000754 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000755 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000756
Alexey Bataevc5e02582014-06-16 07:08:35 +0000757 return Actions.ActOnOpenMPVarListClause(
758 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
759 ReductionIdScopeSpec,
760 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
761 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000762}
763