blob: caa8d8f782c1f56797a4c24ec965351cc8b1b001 [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) {
29 auto Tok = P.getCurToken();
30 auto DKind =
31 Tok.isAnnotation()
32 ? OMPD_unknown
33 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
34 if (DKind == OMPD_parallel) {
35 Tok = P.getPreprocessor().LookAhead(0);
36 auto SDKind =
37 Tok.isAnnotation()
38 ? OMPD_unknown
39 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
40 if (SDKind == OMPD_for) {
41 P.ConsumeToken();
42 DKind = OMPD_parallel_for;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000043 } else if (SDKind == OMPD_sections) {
44 P.ConsumeToken();
45 DKind = OMPD_parallel_sections;
Alexey Bataev4acb8592014-07-07 13:01:15 +000046 }
47 }
48 return DKind;
49}
50
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000051/// \brief Parsing of declarative OpenMP directives.
52///
53/// threadprivate-directive:
54/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000055///
56Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
57 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000058 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000059
60 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000061 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000062 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000063
64 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000065 case OMPD_threadprivate:
66 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000067 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000068 // The last seen token is annot_pragma_openmp_end - need to check for
69 // extra tokens.
70 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
71 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000072 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000073 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000074 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000075 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000076 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +000077 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000078 }
79 break;
80 case OMPD_unknown:
81 Diag(Tok, diag::err_omp_unknown_directive);
82 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000083 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000084 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000085 case OMPD_task:
Alexey Bataevf29276e2014-06-18 04:14:57 +000086 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000087 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000088 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000089 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +000090 case OMPD_parallel_for:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000091 case OMPD_parallel_sections:
Alexey Bataeva769e072013-03-22 06:34:35 +000092 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000093 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000094 break;
95 }
Alp Tokerd751fa72013-12-18 19:10:49 +000096 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000097 return DeclGroupPtrTy();
98}
99
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000100/// \brief Parsing of declarative or executable OpenMP directives.
101///
102/// threadprivate-directive:
103/// annot_pragma_openmp 'threadprivate' simple-variable-list
104/// annot_pragma_openmp_end
105///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000106/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000107/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000108/// 'section' | 'single' | 'parallel for' | 'parallel sections' | 'task'
109/// {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000110///
111StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
112 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000113 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000114 SmallVector<Expr *, 5> Identifiers;
115 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000116 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000117 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000118 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000119 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000121 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122 // Name of critical directive.
123 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000124 StmtResult Directive = StmtError();
125
126 switch (DKind) {
127 case OMPD_threadprivate:
128 ConsumeToken();
129 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
130 // The last seen token is annot_pragma_openmp_end - need to check for
131 // extra tokens.
132 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
133 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000134 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000135 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000136 }
137 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000138 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000139 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
140 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000141 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000142 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000143 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000144 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000145 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000146 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000147 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000148 case OMPD_section:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000149 case OMPD_parallel_for:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000150 case OMPD_parallel_sections:
151 case OMPD_task: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000152 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153
Alexey Bataevf29276e2014-06-18 04:14:57 +0000154 if (isOpenMPLoopDirective(DKind))
155 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
156 if (isOpenMPSimdDirective(DKind))
157 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
158 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000159 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000161 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000162 OpenMPClauseKind CKind = Tok.isAnnotation()
163 ? OMPC_unknown
164 : getOpenMPClauseKind(PP.getSpelling(Tok));
165 OMPClause *Clause =
166 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000167 FirstClauses[CKind].setInt(true);
168 if (Clause) {
169 FirstClauses[CKind].setPointer(Clause);
170 Clauses.push_back(Clause);
171 }
172
173 // Skip ',' if any.
174 if (Tok.is(tok::comma))
175 ConsumeToken();
176 }
177 // End location of the directive.
178 EndLoc = Tok.getLocation();
179 // Consume final annot_pragma_openmp_end.
180 ConsumeToken();
181
182 StmtResult AssociatedStmt;
183 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000184 {
185 // The body is a block scope like in Lambdas and Blocks.
186 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000187 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000188 Actions.ActOnStartOfCompoundStmt();
189 // Parse statement
190 AssociatedStmt = ParseStatement();
191 Actions.ActOnFinishOfCompoundStmt();
192 if (!AssociatedStmt.isUsable()) {
193 Actions.ActOnCapturedRegionError();
194 CreateDirective = false;
195 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000196 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000197 CreateDirective = AssociatedStmt.isUsable();
198 }
199 }
200 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000201 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000202 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000203
204 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000206 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000207 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000208 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000209 case OMPD_unknown:
210 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000211 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000212 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000213 }
214 return Directive;
215}
216
Alexey Bataeva769e072013-03-22 06:34:35 +0000217/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000218/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000219///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000220/// simple-variable-list:
221/// '(' id-expression {, id-expression} ')'
222///
223bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
224 SmallVectorImpl<Expr *> &VarList,
225 bool AllowScopeSpecifier) {
226 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000227 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000228 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000229 if (T.expectAndConsume(diag::err_expected_lparen_after,
230 getOpenMPDirectiveName(Kind)))
231 return true;
232 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000233 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000234
235 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000236 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000237 CXXScopeSpec SS;
238 SourceLocation TemplateKWLoc;
239 UnqualifiedId Name;
240 // Read var name.
241 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000242 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000243
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000244 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
245 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000246 IsCorrect = false;
247 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000248 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000249 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
250 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000251 IsCorrect = false;
252 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000253 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000254 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
255 Tok.isNot(tok::annot_pragma_openmp_end)) {
256 IsCorrect = false;
257 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000258 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000259 Diag(PrevTok.getLocation(), diag::err_expected)
260 << tok::identifier
261 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000262 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000263 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000264 ExprResult Res =
265 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000266 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000267 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000268 }
269 // Consume ','.
270 if (Tok.is(tok::comma)) {
271 ConsumeToken();
272 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000273 }
274
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000275 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000276 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000277 IsCorrect = false;
278 }
279
280 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000281 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000282
283 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000284}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000285
286/// \brief Parsing of OpenMP clauses.
287///
288/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000289/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000290/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000291/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataevbae9a792014-06-27 10:37:06 +0000292/// reduction-clause | proc_bind-clause | schedule-clause |
293/// copyin-clause | copyprivate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000294///
295OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
296 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000297 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000298 bool ErrorFound = false;
299 // Check if clause is allowed for the given directive.
300 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000301 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
302 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000303 ErrorFound = true;
304 }
305
306 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000307 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000308 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000309 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000310 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000311 // OpenMP [2.5, Restrictions]
312 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000313 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000314 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000315 // Only one safelen clause can appear on a simd directive.
316 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000317 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000318 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
319 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000320 }
321
322 Clause = ParseOpenMPSingleExprClause(CKind);
323 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000324 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000325 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000326 // OpenMP [2.14.3.1, Restrictions]
327 // Only a single default clause may be specified on a parallel, task or
328 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000329 // OpenMP [2.5, parallel Construct, Restrictions]
330 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000331 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000332 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
333 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000334 }
335
336 Clause = ParseOpenMPSimpleClause(CKind);
337 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000338 case OMPC_schedule:
339 // OpenMP [2.7.1, Restrictions, p. 3]
340 // Only one schedule clause can appear on a loop directive.
341 if (!FirstClause) {
342 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
343 << getOpenMPClauseName(CKind);
344 }
345
346 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
347 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000348 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000349 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000350 // OpenMP [2.7.1, Restrictions, p. 9]
351 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000352 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
353 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000354 if (!FirstClause) {
355 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
356 << getOpenMPClauseName(CKind);
357 }
358
359 Clause = ParseOpenMPClause(CKind);
360 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000361 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000362 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000363 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000364 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000365 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000366 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000367 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000368 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000369 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000370 Clause = ParseOpenMPVarListClause(CKind);
371 break;
372 case OMPC_unknown:
373 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000374 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000375 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000376 break;
377 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000378 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
379 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000380 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000381 break;
382 }
Craig Topper161e4db2014-05-21 06:02:52 +0000383 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000384}
385
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000386/// \brief Parsing of OpenMP clauses with single expressions like 'if',
387/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
388/// 'thread_limit'.
389///
390/// if-clause:
391/// 'if' '(' expression ')'
392///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000393/// num_threads-clause:
394/// 'num_threads' '(' expression ')'
395///
396/// safelen-clause:
397/// 'safelen' '(' expression ')'
398///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000399/// collapse-clause:
400/// 'collapse' '(' expression ')'
401///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000402OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
403 SourceLocation Loc = ConsumeToken();
404
405 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
406 if (T.expectAndConsume(diag::err_expected_lparen_after,
407 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000408 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000409
410 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
411 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
412
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000413 // Parse ')'.
414 T.consumeClose();
415
416 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000417 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000418
Alexey Bataeva55ed262014-05-28 06:15:33 +0000419 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000420 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000421}
422
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000423/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000424///
425/// default-clause:
426/// 'default' '(' 'none' | 'shared' ')
427///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000428/// proc_bind-clause:
429/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
430///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000431OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
432 SourceLocation Loc = Tok.getLocation();
433 SourceLocation LOpen = ConsumeToken();
434 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000435 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000436 if (T.expectAndConsume(diag::err_expected_lparen_after,
437 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000438 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000439
Alexey Bataeva55ed262014-05-28 06:15:33 +0000440 unsigned Type = getOpenMPSimpleClauseType(
441 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000442 SourceLocation TypeLoc = Tok.getLocation();
443 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
444 Tok.isNot(tok::annot_pragma_openmp_end))
445 ConsumeAnyToken();
446
447 // Parse ')'.
448 T.consumeClose();
449
450 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
451 Tok.getLocation());
452}
453
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000454/// \brief Parsing of OpenMP clauses like 'ordered'.
455///
456/// ordered-clause:
457/// 'ordered'
458///
Alexey Bataev236070f2014-06-20 11:19:47 +0000459/// nowait-clause:
460/// 'nowait'
461///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000462OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
463 SourceLocation Loc = Tok.getLocation();
464 ConsumeAnyToken();
465
466 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
467}
468
469
Alexey Bataev56dafe82014-06-20 07:16:17 +0000470/// \brief Parsing of OpenMP clauses with single expressions and some additional
471/// argument like 'schedule' or 'dist_schedule'.
472///
473/// schedule-clause:
474/// 'schedule' '(' kind [',' expression ] ')'
475///
476OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
477 SourceLocation Loc = ConsumeToken();
478 SourceLocation CommaLoc;
479 // Parse '('.
480 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
481 if (T.expectAndConsume(diag::err_expected_lparen_after,
482 getOpenMPClauseName(Kind)))
483 return nullptr;
484
485 ExprResult Val;
486 unsigned Type = getOpenMPSimpleClauseType(
487 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
488 SourceLocation KLoc = Tok.getLocation();
489 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
490 Tok.isNot(tok::annot_pragma_openmp_end))
491 ConsumeAnyToken();
492
493 if (Kind == OMPC_schedule &&
494 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
495 Type == OMPC_SCHEDULE_guided) &&
496 Tok.is(tok::comma)) {
497 CommaLoc = ConsumeAnyToken();
498 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
499 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
500 if (Val.isInvalid())
501 return nullptr;
502 }
503
504 // Parse ')'.
505 T.consumeClose();
506
507 return Actions.ActOnOpenMPSingleExprWithArgClause(
508 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
509 T.getCloseLocation());
510}
511
Alexey Bataevc5e02582014-06-16 07:08:35 +0000512static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
513 UnqualifiedId &ReductionId) {
514 SourceLocation TemplateKWLoc;
515 if (ReductionIdScopeSpec.isEmpty()) {
516 auto OOK = OO_None;
517 switch (P.getCurToken().getKind()) {
518 case tok::plus:
519 OOK = OO_Plus;
520 break;
521 case tok::minus:
522 OOK = OO_Minus;
523 break;
524 case tok::star:
525 OOK = OO_Star;
526 break;
527 case tok::amp:
528 OOK = OO_Amp;
529 break;
530 case tok::pipe:
531 OOK = OO_Pipe;
532 break;
533 case tok::caret:
534 OOK = OO_Caret;
535 break;
536 case tok::ampamp:
537 OOK = OO_AmpAmp;
538 break;
539 case tok::pipepipe:
540 OOK = OO_PipePipe;
541 break;
542 default:
543 break;
544 }
545 if (OOK != OO_None) {
546 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000547 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000548 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
549 return false;
550 }
551 }
552 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
553 /*AllowDestructorName*/ false,
554 /*AllowConstructorName*/ false, ParsedType(),
555 TemplateKWLoc, ReductionId);
556}
557
Alexander Musman1bb328c2014-06-04 13:06:39 +0000558/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000559/// 'shared', 'copyin', or 'reduction'.
560///
561/// private-clause:
562/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000563/// firstprivate-clause:
564/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000565/// lastprivate-clause:
566/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567/// shared-clause:
568/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000569/// linear-clause:
570/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000571/// aligned-clause:
572/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000573/// reduction-clause:
574/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000575///
576OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
577 SourceLocation Loc = Tok.getLocation();
578 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000579 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000580 // Optional scope specifier and unqualified id for reduction identifier.
581 CXXScopeSpec ReductionIdScopeSpec;
582 UnqualifiedId ReductionId;
583 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000584 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000585 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000586 if (T.expectAndConsume(diag::err_expected_lparen_after,
587 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000588 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000589
Alexey Bataevc5e02582014-06-16 07:08:35 +0000590 // Handle reduction-identifier for reduction clause.
591 if (Kind == OMPC_reduction) {
592 ColonProtectionRAIIObject ColonRAII(*this);
593 if (getLangOpts().CPlusPlus) {
594 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
595 }
596 InvalidReductionId =
597 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
598 if (InvalidReductionId) {
599 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
600 StopBeforeMatch);
601 }
602 if (Tok.is(tok::colon)) {
603 ColonLoc = ConsumeToken();
604 } else {
605 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
606 }
607 }
608
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000609 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000610 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000611 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000612 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000613 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000614 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000615 // Parse variable
616 ExprResult VarExpr = ParseAssignmentExpression();
617 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000618 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000619 } else {
620 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000621 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000622 }
623 // Skip ',' if any
624 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000625 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000626 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000627 else if (Tok.isNot(tok::r_paren) &&
628 Tok.isNot(tok::annot_pragma_openmp_end) &&
629 (!MayHaveTail || Tok.isNot(tok::colon)))
630 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
631 }
632
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000633 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000634 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000635 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
636 if (MustHaveTail) {
637 ColonLoc = Tok.getLocation();
638 ConsumeToken();
639 ExprResult Tail = ParseAssignmentExpression();
640 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000641 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000642 else
643 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
644 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000645 }
646
647 // Parse ')'.
648 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000649 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000650 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000651
Alexey Bataevc5e02582014-06-16 07:08:35 +0000652 return Actions.ActOnOpenMPVarListClause(
653 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
654 ReductionIdScopeSpec,
655 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
656 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000657}
658