blob: e50f4714e49297a925d8a6a90c5f8820dea455b8 [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 Bataev3778b602014-07-17 07:32:53 +0000289/// if-clause | final-clause | num_threads-clause | safelen-clause |
290/// default-clause | private-clause | firstprivate-clause | shared-clause
291/// | linear-clause | aligned-clause | collapse-clause |
292/// lastprivate-clause | reduction-clause | proc_bind-clause |
293/// schedule-clause | 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 Bataev3778b602014-07-17 07:32:53 +0000308 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000309 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000310 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000311 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000312 // OpenMP [2.5, Restrictions]
313 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000314 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000315 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000316 // Only one safelen clause can appear on a simd directive.
317 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000318 // OpenMP [2.11.1, task Construct, Restrictions]
319 // At most one if clause can appear on the directive.
320 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000321 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000322 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
323 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000324 }
325
326 Clause = ParseOpenMPSingleExprClause(CKind);
327 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000328 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000329 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000330 // OpenMP [2.14.3.1, Restrictions]
331 // Only a single default clause may be specified on a parallel, task or
332 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000333 // OpenMP [2.5, parallel Construct, Restrictions]
334 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000335 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000336 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
337 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000338 }
339
340 Clause = ParseOpenMPSimpleClause(CKind);
341 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000342 case OMPC_schedule:
343 // OpenMP [2.7.1, Restrictions, p. 3]
344 // Only one schedule clause can appear on a loop directive.
345 if (!FirstClause) {
346 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
347 << getOpenMPClauseName(CKind);
348 }
349
350 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
351 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000352 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000353 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000354 // OpenMP [2.7.1, Restrictions, p. 9]
355 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000356 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
357 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000358 if (!FirstClause) {
359 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
360 << getOpenMPClauseName(CKind);
361 }
362
363 Clause = ParseOpenMPClause(CKind);
364 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000365 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000366 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000367 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000368 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000369 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000370 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000371 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000372 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000373 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000374 Clause = ParseOpenMPVarListClause(CKind);
375 break;
376 case OMPC_unknown:
377 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000378 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000379 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000380 break;
381 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000382 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
383 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000384 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000385 break;
386 }
Craig Topper161e4db2014-05-21 06:02:52 +0000387 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000388}
389
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000390/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000391/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000392/// 'thread_limit'.
393///
394/// if-clause:
395/// 'if' '(' expression ')'
396///
Alexey Bataev3778b602014-07-17 07:32:53 +0000397/// final-clause:
398/// 'final' '(' expression ')'
399///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000400/// num_threads-clause:
401/// 'num_threads' '(' expression ')'
402///
403/// safelen-clause:
404/// 'safelen' '(' expression ')'
405///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000406/// collapse-clause:
407/// 'collapse' '(' expression ')'
408///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000409OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
410 SourceLocation Loc = ConsumeToken();
411
412 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
413 if (T.expectAndConsume(diag::err_expected_lparen_after,
414 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000415 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000416
417 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
418 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
419
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000420 // Parse ')'.
421 T.consumeClose();
422
423 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000424 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000425
Alexey Bataeva55ed262014-05-28 06:15:33 +0000426 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000427 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000428}
429
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000430/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000431///
432/// default-clause:
433/// 'default' '(' 'none' | 'shared' ')
434///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000435/// proc_bind-clause:
436/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
437///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000438OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
439 SourceLocation Loc = Tok.getLocation();
440 SourceLocation LOpen = ConsumeToken();
441 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000442 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000443 if (T.expectAndConsume(diag::err_expected_lparen_after,
444 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000445 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446
Alexey Bataeva55ed262014-05-28 06:15:33 +0000447 unsigned Type = getOpenMPSimpleClauseType(
448 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000449 SourceLocation TypeLoc = Tok.getLocation();
450 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
451 Tok.isNot(tok::annot_pragma_openmp_end))
452 ConsumeAnyToken();
453
454 // Parse ')'.
455 T.consumeClose();
456
457 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
458 Tok.getLocation());
459}
460
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000461/// \brief Parsing of OpenMP clauses like 'ordered'.
462///
463/// ordered-clause:
464/// 'ordered'
465///
Alexey Bataev236070f2014-06-20 11:19:47 +0000466/// nowait-clause:
467/// 'nowait'
468///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000469OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
470 SourceLocation Loc = Tok.getLocation();
471 ConsumeAnyToken();
472
473 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
474}
475
476
Alexey Bataev56dafe82014-06-20 07:16:17 +0000477/// \brief Parsing of OpenMP clauses with single expressions and some additional
478/// argument like 'schedule' or 'dist_schedule'.
479///
480/// schedule-clause:
481/// 'schedule' '(' kind [',' expression ] ')'
482///
483OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
484 SourceLocation Loc = ConsumeToken();
485 SourceLocation CommaLoc;
486 // Parse '('.
487 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
488 if (T.expectAndConsume(diag::err_expected_lparen_after,
489 getOpenMPClauseName(Kind)))
490 return nullptr;
491
492 ExprResult Val;
493 unsigned Type = getOpenMPSimpleClauseType(
494 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
495 SourceLocation KLoc = Tok.getLocation();
496 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
497 Tok.isNot(tok::annot_pragma_openmp_end))
498 ConsumeAnyToken();
499
500 if (Kind == OMPC_schedule &&
501 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
502 Type == OMPC_SCHEDULE_guided) &&
503 Tok.is(tok::comma)) {
504 CommaLoc = ConsumeAnyToken();
505 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
506 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
507 if (Val.isInvalid())
508 return nullptr;
509 }
510
511 // Parse ')'.
512 T.consumeClose();
513
514 return Actions.ActOnOpenMPSingleExprWithArgClause(
515 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
516 T.getCloseLocation());
517}
518
Alexey Bataevc5e02582014-06-16 07:08:35 +0000519static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
520 UnqualifiedId &ReductionId) {
521 SourceLocation TemplateKWLoc;
522 if (ReductionIdScopeSpec.isEmpty()) {
523 auto OOK = OO_None;
524 switch (P.getCurToken().getKind()) {
525 case tok::plus:
526 OOK = OO_Plus;
527 break;
528 case tok::minus:
529 OOK = OO_Minus;
530 break;
531 case tok::star:
532 OOK = OO_Star;
533 break;
534 case tok::amp:
535 OOK = OO_Amp;
536 break;
537 case tok::pipe:
538 OOK = OO_Pipe;
539 break;
540 case tok::caret:
541 OOK = OO_Caret;
542 break;
543 case tok::ampamp:
544 OOK = OO_AmpAmp;
545 break;
546 case tok::pipepipe:
547 OOK = OO_PipePipe;
548 break;
549 default:
550 break;
551 }
552 if (OOK != OO_None) {
553 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000554 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000555 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
556 return false;
557 }
558 }
559 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
560 /*AllowDestructorName*/ false,
561 /*AllowConstructorName*/ false, ParsedType(),
562 TemplateKWLoc, ReductionId);
563}
564
Alexander Musman1bb328c2014-06-04 13:06:39 +0000565/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000566/// 'shared', 'copyin', or 'reduction'.
567///
568/// private-clause:
569/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000570/// firstprivate-clause:
571/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000572/// lastprivate-clause:
573/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000574/// shared-clause:
575/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000576/// linear-clause:
577/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000578/// aligned-clause:
579/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000580/// reduction-clause:
581/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000582///
583OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
584 SourceLocation Loc = Tok.getLocation();
585 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000586 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000587 // Optional scope specifier and unqualified id for reduction identifier.
588 CXXScopeSpec ReductionIdScopeSpec;
589 UnqualifiedId ReductionId;
590 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000592 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000593 if (T.expectAndConsume(diag::err_expected_lparen_after,
594 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000595 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000596
Alexey Bataevc5e02582014-06-16 07:08:35 +0000597 // Handle reduction-identifier for reduction clause.
598 if (Kind == OMPC_reduction) {
599 ColonProtectionRAIIObject ColonRAII(*this);
600 if (getLangOpts().CPlusPlus) {
601 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
602 }
603 InvalidReductionId =
604 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
605 if (InvalidReductionId) {
606 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
607 StopBeforeMatch);
608 }
609 if (Tok.is(tok::colon)) {
610 ColonLoc = ConsumeToken();
611 } else {
612 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
613 }
614 }
615
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000616 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000617 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000618 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000619 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000620 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000621 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000622 // Parse variable
623 ExprResult VarExpr = ParseAssignmentExpression();
624 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000625 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000626 } else {
627 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000628 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000629 }
630 // Skip ',' if any
631 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000632 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000633 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000634 else if (Tok.isNot(tok::r_paren) &&
635 Tok.isNot(tok::annot_pragma_openmp_end) &&
636 (!MayHaveTail || Tok.isNot(tok::colon)))
637 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
638 }
639
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000640 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000641 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000642 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
643 if (MustHaveTail) {
644 ColonLoc = Tok.getLocation();
645 ConsumeToken();
646 ExprResult Tail = ParseAssignmentExpression();
647 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000648 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000649 else
650 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
651 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000652 }
653
654 // Parse ')'.
655 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000656 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000657 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000658
Alexey Bataevc5e02582014-06-16 07:08:35 +0000659 return Actions.ActOnOpenMPVarListClause(
660 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
661 ReductionIdScopeSpec,
662 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
663 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000664}
665