blob: 38efed892641459b9da9a8224fd072931a3add17 [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 Bataevc30dd2d2015-06-18 12:14:09 +000097 case OMPD_taskgroup:
Alexey Bataev6125da92014-07-21 11:26:11 +000098 case OMPD_flush:
Alexey Bataevf29276e2014-06-18 04:14:57 +000099 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000100 case OMPD_for_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000101 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000102 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000103 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +0000104 case OMPD_master:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000105 case OMPD_ordered:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000106 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000107 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000108 case OMPD_parallel_for_simd:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000109 case OMPD_parallel_sections:
Alexey Bataev0162e452014-07-22 10:10:35 +0000110 case OMPD_atomic:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000111 case OMPD_target:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000112 case OMPD_teams:
Alexey Bataeva769e072013-03-22 06:34:35 +0000113 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000114 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +0000115 break;
116 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000117 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000118 return DeclGroupPtrTy();
119}
120
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000121/// \brief Parsing of declarative or executable OpenMP directives.
122///
123/// threadprivate-directive:
124/// annot_pragma_openmp 'threadprivate' simple-variable-list
125/// annot_pragma_openmp_end
126///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000127/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000128/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000129/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
130/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
Alexander Musmanf82886e2014-09-18 05:12:34 +0000131/// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000132/// 'for simd' | 'parallel for simd' | 'target' | 'teams' | 'taskgroup'
133/// {clause}
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000134/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000135///
Alexey Bataev68446b72014-07-18 07:47:19 +0000136StmtResult
137Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000138 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000139 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000140 SmallVector<Expr *, 5> Identifiers;
141 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000142 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000143 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000144 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000145 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000146 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000147 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148 // Name of critical directive.
149 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000150 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000151 bool HasAssociatedStatement = true;
Alexey Bataev6125da92014-07-21 11:26:11 +0000152 bool FlushHasClause = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000153
154 switch (DKind) {
155 case OMPD_threadprivate:
156 ConsumeToken();
157 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
158 // The last seen token is annot_pragma_openmp_end - need to check for
159 // extra tokens.
160 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
161 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000162 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000163 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000164 }
165 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000166 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
168 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000169 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000170 break;
Alexey Bataev6125da92014-07-21 11:26:11 +0000171 case OMPD_flush:
172 if (PP.LookAhead(0).is(tok::l_paren)) {
173 FlushHasClause = true;
174 // Push copy of the current token back to stream to properly parse
175 // pseudo-clause OMPFlushClause.
176 PP.EnterToken(Tok);
177 }
Alexey Bataev68446b72014-07-18 07:47:19 +0000178 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000179 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000180 case OMPD_taskwait:
Alexey Bataev68446b72014-07-18 07:47:19 +0000181 if (!StandAloneAllowed) {
182 Diag(Tok, diag::err_omp_immediate_directive)
183 << getOpenMPDirectiveName(DKind);
184 }
185 HasAssociatedStatement = false;
Alexey Bataev6125da92014-07-21 11:26:11 +0000186 // Fall through for further analysis.
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000187 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000188 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000189 case OMPD_for:
Alexander Musmanf82886e2014-09-18 05:12:34 +0000190 case OMPD_for_simd:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000191 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000192 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000193 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000194 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000195 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000196 case OMPD_parallel_for:
Alexander Musmane4e893b2014-09-23 09:33:00 +0000197 case OMPD_parallel_for_simd:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000198 case OMPD_parallel_sections:
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000199 case OMPD_task:
Alexey Bataev0162e452014-07-22 10:10:35 +0000200 case OMPD_ordered:
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000201 case OMPD_atomic:
Alexey Bataev13314bf2014-10-09 04:18:56 +0000202 case OMPD_target:
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000203 case OMPD_teams:
204 case OMPD_taskgroup: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000205 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000206 // Parse directive name of the 'critical' directive if any.
207 if (DKind == OMPD_critical) {
208 BalancedDelimiterTracker T(*this, tok::l_paren,
209 tok::annot_pragma_openmp_end);
210 if (!T.consumeOpen()) {
211 if (Tok.isAnyIdentifier()) {
212 DirName =
213 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
214 ConsumeAnyToken();
215 } else {
216 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
217 }
218 T.consumeClose();
219 }
220 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000221
Alexey Bataevf29276e2014-06-18 04:14:57 +0000222 if (isOpenMPLoopDirective(DKind))
223 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
224 if (isOpenMPSimdDirective(DKind))
225 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
226 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000227 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000228
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000229 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataev6125da92014-07-21 11:26:11 +0000230 OpenMPClauseKind CKind =
231 Tok.isAnnotation()
232 ? OMPC_unknown
233 : FlushHasClause ? OMPC_flush
234 : getOpenMPClauseKind(PP.getSpelling(Tok));
Alexey Bataevaac108a2015-06-23 04:51:00 +0000235 Actions.StartOpenMPClause(CKind);
Alexey Bataev6125da92014-07-21 11:26:11 +0000236 FlushHasClause = false;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000237 OMPClause *Clause =
238 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000239 FirstClauses[CKind].setInt(true);
240 if (Clause) {
241 FirstClauses[CKind].setPointer(Clause);
242 Clauses.push_back(Clause);
243 }
244
245 // Skip ',' if any.
246 if (Tok.is(tok::comma))
247 ConsumeToken();
Alexey Bataevaac108a2015-06-23 04:51:00 +0000248 Actions.EndOpenMPClause();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000249 }
250 // End location of the directive.
251 EndLoc = Tok.getLocation();
252 // Consume final annot_pragma_openmp_end.
253 ConsumeToken();
254
255 StmtResult AssociatedStmt;
256 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000257 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000258 // The body is a block scope like in Lambdas and Blocks.
259 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000260 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000261 Actions.ActOnStartOfCompoundStmt();
262 // Parse statement
263 AssociatedStmt = ParseStatement();
264 Actions.ActOnFinishOfCompoundStmt();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +0000265 AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
266 CreateDirective = AssociatedStmt.isUsable();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000267 }
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 Bataev1c2cfbc2015-06-23 14:25:19 +0000456 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000457 Clause = ParseOpenMPVarListClause(CKind);
458 break;
459 case OMPC_unknown:
460 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000461 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000462 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000463 break;
464 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000465 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
466 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000467 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000468 break;
469 }
Craig Topper161e4db2014-05-21 06:02:52 +0000470 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000471}
472
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000473/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000474/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000475/// 'thread_limit'.
476///
477/// if-clause:
478/// 'if' '(' expression ')'
479///
Alexey Bataev3778b602014-07-17 07:32:53 +0000480/// final-clause:
481/// 'final' '(' expression ')'
482///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000483/// num_threads-clause:
484/// 'num_threads' '(' expression ')'
485///
486/// safelen-clause:
487/// 'safelen' '(' expression ')'
488///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000489/// collapse-clause:
490/// 'collapse' '(' expression ')'
491///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000492OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
493 SourceLocation Loc = ConsumeToken();
494
495 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
496 if (T.expectAndConsume(diag::err_expected_lparen_after,
497 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000498 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000499
500 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
501 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
502
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000503 // Parse ')'.
504 T.consumeClose();
505
506 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000507 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000508
Alexey Bataeva55ed262014-05-28 06:15:33 +0000509 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000510 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000511}
512
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000513/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000514///
515/// default-clause:
516/// 'default' '(' 'none' | 'shared' ')
517///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000518/// proc_bind-clause:
519/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
520///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000521OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
522 SourceLocation Loc = Tok.getLocation();
523 SourceLocation LOpen = ConsumeToken();
524 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000525 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000526 if (T.expectAndConsume(diag::err_expected_lparen_after,
527 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000528 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000529
Alexey Bataeva55ed262014-05-28 06:15:33 +0000530 unsigned Type = getOpenMPSimpleClauseType(
531 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000532 SourceLocation TypeLoc = Tok.getLocation();
533 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
534 Tok.isNot(tok::annot_pragma_openmp_end))
535 ConsumeAnyToken();
536
537 // Parse ')'.
538 T.consumeClose();
539
540 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
541 Tok.getLocation());
542}
543
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000544/// \brief Parsing of OpenMP clauses like 'ordered'.
545///
546/// ordered-clause:
547/// 'ordered'
548///
Alexey Bataev236070f2014-06-20 11:19:47 +0000549/// nowait-clause:
550/// 'nowait'
551///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000552/// untied-clause:
553/// 'untied'
554///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000555/// mergeable-clause:
556/// 'mergeable'
557///
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000558/// read-clause:
559/// 'read'
560///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000561OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
562 SourceLocation Loc = Tok.getLocation();
563 ConsumeAnyToken();
564
565 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
566}
567
568
Alexey Bataev56dafe82014-06-20 07:16:17 +0000569/// \brief Parsing of OpenMP clauses with single expressions and some additional
570/// argument like 'schedule' or 'dist_schedule'.
571///
572/// schedule-clause:
573/// 'schedule' '(' kind [',' expression ] ')'
574///
575OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
576 SourceLocation Loc = ConsumeToken();
577 SourceLocation CommaLoc;
578 // Parse '('.
579 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
580 if (T.expectAndConsume(diag::err_expected_lparen_after,
581 getOpenMPClauseName(Kind)))
582 return nullptr;
583
584 ExprResult Val;
585 unsigned Type = getOpenMPSimpleClauseType(
586 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
587 SourceLocation KLoc = Tok.getLocation();
588 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
589 Tok.isNot(tok::annot_pragma_openmp_end))
590 ConsumeAnyToken();
591
592 if (Kind == OMPC_schedule &&
593 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
594 Type == OMPC_SCHEDULE_guided) &&
595 Tok.is(tok::comma)) {
596 CommaLoc = ConsumeAnyToken();
597 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
598 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
599 if (Val.isInvalid())
600 return nullptr;
601 }
602
603 // Parse ')'.
604 T.consumeClose();
605
606 return Actions.ActOnOpenMPSingleExprWithArgClause(
607 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
608 T.getCloseLocation());
609}
610
Alexey Bataevc5e02582014-06-16 07:08:35 +0000611static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
612 UnqualifiedId &ReductionId) {
613 SourceLocation TemplateKWLoc;
614 if (ReductionIdScopeSpec.isEmpty()) {
615 auto OOK = OO_None;
616 switch (P.getCurToken().getKind()) {
617 case tok::plus:
618 OOK = OO_Plus;
619 break;
620 case tok::minus:
621 OOK = OO_Minus;
622 break;
623 case tok::star:
624 OOK = OO_Star;
625 break;
626 case tok::amp:
627 OOK = OO_Amp;
628 break;
629 case tok::pipe:
630 OOK = OO_Pipe;
631 break;
632 case tok::caret:
633 OOK = OO_Caret;
634 break;
635 case tok::ampamp:
636 OOK = OO_AmpAmp;
637 break;
638 case tok::pipepipe:
639 OOK = OO_PipePipe;
640 break;
641 default:
642 break;
643 }
644 if (OOK != OO_None) {
645 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000646 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000647 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
648 return false;
649 }
650 }
651 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
652 /*AllowDestructorName*/ false,
653 /*AllowConstructorName*/ false, ParsedType(),
654 TemplateKWLoc, ReductionId);
655}
656
Alexander Musman1bb328c2014-06-04 13:06:39 +0000657/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev6125da92014-07-21 11:26:11 +0000658/// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000659///
660/// private-clause:
661/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000662/// firstprivate-clause:
663/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000664/// lastprivate-clause:
665/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000666/// shared-clause:
667/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000668/// linear-clause:
669/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000670/// aligned-clause:
671/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000672/// reduction-clause:
673/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev6125da92014-07-21 11:26:11 +0000674/// copyprivate-clause:
675/// 'copyprivate' '(' list ')'
676/// flush-clause:
677/// 'flush' '(' list ')'
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000678/// depend-clause:
679/// 'depend' '(' in | out | inout : list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000680///
681OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
682 SourceLocation Loc = Tok.getLocation();
683 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000684 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000685 // Optional scope specifier and unqualified id for reduction identifier.
686 CXXScopeSpec ReductionIdScopeSpec;
687 UnqualifiedId ReductionId;
688 bool InvalidReductionId = false;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000689 OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
690 SourceLocation DepLoc;
691
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000692 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000693 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000694 if (T.expectAndConsume(diag::err_expected_lparen_after,
695 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000696 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000697
Alexey Bataevc5e02582014-06-16 07:08:35 +0000698 // Handle reduction-identifier for reduction clause.
699 if (Kind == OMPC_reduction) {
700 ColonProtectionRAIIObject ColonRAII(*this);
701 if (getLangOpts().CPlusPlus) {
702 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
703 }
704 InvalidReductionId =
705 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
706 if (InvalidReductionId) {
707 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
708 StopBeforeMatch);
709 }
710 if (Tok.is(tok::colon)) {
711 ColonLoc = ConsumeToken();
712 } else {
713 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
714 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000715 } else if (Kind == OMPC_depend) {
716 // Handle dependency type for depend clause.
717 ColonProtectionRAIIObject ColonRAII(*this);
718 DepKind = static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
719 Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
720 DepLoc = Tok.getLocation();
721
722 if (DepKind == OMPC_DEPEND_unknown) {
723 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
724 StopBeforeMatch);
725 } else {
726 ConsumeToken();
727 }
728 if (Tok.is(tok::colon)) {
729 ColonLoc = ConsumeToken();
730 } else {
731 Diag(Tok, diag::warn_pragma_expected_colon) << "dependency type";
732 }
Alexey Bataevc5e02582014-06-16 07:08:35 +0000733 }
734
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000735 SmallVector<Expr *, 5> Vars;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000736 bool IsComma = ((Kind != OMPC_reduction) && (Kind != OMPC_depend)) ||
737 ((Kind == OMPC_reduction) && !InvalidReductionId) ||
738 ((Kind == OMPC_depend) && DepKind != OMPC_DEPEND_unknown);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000739 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000740 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000741 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000742 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000743 // Parse variable
Kaelyn Takata15867822014-11-21 18:48:04 +0000744 ExprResult VarExpr =
745 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000746 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000747 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000748 } else {
749 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000750 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000751 }
752 // Skip ',' if any
753 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000754 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000755 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000756 else if (Tok.isNot(tok::r_paren) &&
757 Tok.isNot(tok::annot_pragma_openmp_end) &&
758 (!MayHaveTail || Tok.isNot(tok::colon)))
Alexey Bataev6125da92014-07-21 11:26:11 +0000759 Diag(Tok, diag::err_omp_expected_punc)
760 << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
761 : getOpenMPClauseName(Kind))
762 << (Kind == OMPC_flush);
Alexander Musman8dba6642014-04-22 13:09:42 +0000763 }
764
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000765 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000766 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000767 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
768 if (MustHaveTail) {
769 ColonLoc = Tok.getLocation();
770 ConsumeToken();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000771 ExprResult Tail =
772 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Alexander Musman8dba6642014-04-22 13:09:42 +0000773 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000774 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000775 else
776 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
777 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000778 }
779
780 // Parse ')'.
781 T.consumeClose();
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000782 if ((Kind == OMPC_depend && DepKind != OMPC_DEPEND_unknown && Vars.empty()) ||
783 (Kind != OMPC_depend && Vars.empty()) || (MustHaveTail && !TailExpr) ||
784 InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000785 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786
Alexey Bataevc5e02582014-06-16 07:08:35 +0000787 return Actions.ActOnOpenMPVarListClause(
788 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
789 ReductionIdScopeSpec,
790 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000791 : DeclarationNameInfo(),
792 DepKind, DepLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793}
794