blob: b35082e6c4fbbb542ae91c36b71086805225ff57 [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 Bataev6f6f3b42013-05-13 04:18:18 +000028/// \brief Parsing of declarative OpenMP directives.
29///
30/// threadprivate-directive:
31/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000032///
33Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
34 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000035 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000036
37 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000038 SmallVector<Expr *, 5> Identifiers;
Alexey Bataeva55ed262014-05-28 06:15:33 +000039 OpenMPDirectiveKind DKind = Tok.isAnnotation()
40 ? OMPD_unknown
41 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000042
43 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000044 case OMPD_threadprivate:
45 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000046 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000047 // The last seen token is annot_pragma_openmp_end - need to check for
48 // extra tokens.
49 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
50 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000051 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000052 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000053 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000054 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000055 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +000056 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000057 }
58 break;
59 case OMPD_unknown:
60 Diag(Tok, diag::err_omp_unknown_directive);
61 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000062 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000063 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000064 case OMPD_task:
Alexey Bataevf29276e2014-06-18 04:14:57 +000065 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000066 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000067 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000068 case OMPD_single:
Alexey Bataeva769e072013-03-22 06:34:35 +000069 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000070 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000071 break;
72 }
Alp Tokerd751fa72013-12-18 19:10:49 +000073 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000074 return DeclGroupPtrTy();
75}
76
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000077/// \brief Parsing of declarative or executable OpenMP directives.
78///
79/// threadprivate-directive:
80/// annot_pragma_openmp 'threadprivate' simple-variable-list
81/// annot_pragma_openmp_end
82///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000083/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000084/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
85/// 'section' | 'single' {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000086///
87StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
88 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000089 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000090 SmallVector<Expr *, 5> Identifiers;
91 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000092 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +000093 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000094 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +000095 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000096 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataeva55ed262014-05-28 06:15:33 +000097 OpenMPDirectiveKind DKind = Tok.isAnnotation()
98 ? OMPD_unknown
99 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 // Name of critical directive.
101 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000102 StmtResult Directive = StmtError();
103
104 switch (DKind) {
105 case OMPD_threadprivate:
106 ConsumeToken();
107 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
108 // The last seen token is annot_pragma_openmp_end - need to check for
109 // extra tokens.
110 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
111 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000112 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000113 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000114 }
115 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000116 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000117 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
118 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000119 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000121 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000122 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000123 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000124 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000125 case OMPD_single:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000126 case OMPD_section: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000127 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128
Alexey Bataevf29276e2014-06-18 04:14:57 +0000129 if (isOpenMPLoopDirective(DKind))
130 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
131 if (isOpenMPSimdDirective(DKind))
132 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
133 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000134 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000135
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000136 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000137 OpenMPClauseKind CKind = Tok.isAnnotation()
138 ? OMPC_unknown
139 : getOpenMPClauseKind(PP.getSpelling(Tok));
140 OMPClause *Clause =
141 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000142 FirstClauses[CKind].setInt(true);
143 if (Clause) {
144 FirstClauses[CKind].setPointer(Clause);
145 Clauses.push_back(Clause);
146 }
147
148 // Skip ',' if any.
149 if (Tok.is(tok::comma))
150 ConsumeToken();
151 }
152 // End location of the directive.
153 EndLoc = Tok.getLocation();
154 // Consume final annot_pragma_openmp_end.
155 ConsumeToken();
156
157 StmtResult AssociatedStmt;
158 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000159 {
160 // The body is a block scope like in Lambdas and Blocks.
161 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000162 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000163 Actions.ActOnStartOfCompoundStmt();
164 // Parse statement
165 AssociatedStmt = ParseStatement();
166 Actions.ActOnFinishOfCompoundStmt();
167 if (!AssociatedStmt.isUsable()) {
168 Actions.ActOnCapturedRegionError();
169 CreateDirective = false;
170 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000171 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000172 CreateDirective = AssociatedStmt.isUsable();
173 }
174 }
175 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000176 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000177 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000178
179 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000181 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000183 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000184 case OMPD_unknown:
185 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000186 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000187 break;
188 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000189 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000190 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000191 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000192 break;
193 }
194 return Directive;
195}
196
Alexey Bataeva769e072013-03-22 06:34:35 +0000197/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000198/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000199///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000200/// simple-variable-list:
201/// '(' id-expression {, id-expression} ')'
202///
203bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
204 SmallVectorImpl<Expr *> &VarList,
205 bool AllowScopeSpecifier) {
206 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000207 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000208 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000209 if (T.expectAndConsume(diag::err_expected_lparen_after,
210 getOpenMPDirectiveName(Kind)))
211 return true;
212 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000213 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000214
215 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000216 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000217 CXXScopeSpec SS;
218 SourceLocation TemplateKWLoc;
219 UnqualifiedId Name;
220 // Read var name.
221 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000222 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000223
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000224 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
225 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000226 IsCorrect = false;
227 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000228 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000229 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
230 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000231 IsCorrect = false;
232 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000233 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000234 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
235 Tok.isNot(tok::annot_pragma_openmp_end)) {
236 IsCorrect = false;
237 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000238 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000239 Diag(PrevTok.getLocation(), diag::err_expected)
240 << tok::identifier
241 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000242 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000243 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000244 ExprResult Res =
245 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000246 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000247 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000248 }
249 // Consume ','.
250 if (Tok.is(tok::comma)) {
251 ConsumeToken();
252 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000253 }
254
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000255 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000256 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000257 IsCorrect = false;
258 }
259
260 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000261 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000262
263 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000264}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000265
266/// \brief Parsing of OpenMP clauses.
267///
268/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000269/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000270/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000271/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataevbae9a792014-06-27 10:37:06 +0000272/// reduction-clause | proc_bind-clause | schedule-clause |
273/// copyin-clause | copyprivate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000274///
275OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
276 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000277 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000278 bool ErrorFound = false;
279 // Check if clause is allowed for the given directive.
280 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000281 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
282 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000283 ErrorFound = true;
284 }
285
286 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000287 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000288 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000289 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000290 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000291 // OpenMP [2.5, Restrictions]
292 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000293 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000294 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000295 // Only one safelen clause can appear on a simd directive.
296 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000297 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000298 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
299 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000300 }
301
302 Clause = ParseOpenMPSingleExprClause(CKind);
303 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000304 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000305 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000306 // OpenMP [2.14.3.1, Restrictions]
307 // Only a single default clause may be specified on a parallel, task or
308 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000309 // OpenMP [2.5, parallel Construct, Restrictions]
310 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000311 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000312 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
313 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000314 }
315
316 Clause = ParseOpenMPSimpleClause(CKind);
317 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000318 case OMPC_schedule:
319 // OpenMP [2.7.1, Restrictions, p. 3]
320 // Only one schedule clause can appear on a loop directive.
321 if (!FirstClause) {
322 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
323 << getOpenMPClauseName(CKind);
324 }
325
326 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
327 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000328 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000329 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000330 // OpenMP [2.7.1, Restrictions, p. 9]
331 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000332 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
333 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000334 if (!FirstClause) {
335 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
336 << getOpenMPClauseName(CKind);
337 }
338
339 Clause = ParseOpenMPClause(CKind);
340 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000341 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000342 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000343 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000344 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000345 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000346 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000347 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000348 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000349 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000350 Clause = ParseOpenMPVarListClause(CKind);
351 break;
352 case OMPC_unknown:
353 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000354 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000355 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000356 break;
357 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000358 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
359 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000360 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000361 break;
362 }
Craig Topper161e4db2014-05-21 06:02:52 +0000363 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000364}
365
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000366/// \brief Parsing of OpenMP clauses with single expressions like 'if',
367/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
368/// 'thread_limit'.
369///
370/// if-clause:
371/// 'if' '(' expression ')'
372///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000373/// num_threads-clause:
374/// 'num_threads' '(' expression ')'
375///
376/// safelen-clause:
377/// 'safelen' '(' expression ')'
378///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000379/// collapse-clause:
380/// 'collapse' '(' expression ')'
381///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000382OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
383 SourceLocation Loc = ConsumeToken();
384
385 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
386 if (T.expectAndConsume(diag::err_expected_lparen_after,
387 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000388 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000389
390 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
391 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
392
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000393 // Parse ')'.
394 T.consumeClose();
395
396 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000397 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000398
Alexey Bataeva55ed262014-05-28 06:15:33 +0000399 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000400 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000401}
402
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000403/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000404///
405/// default-clause:
406/// 'default' '(' 'none' | 'shared' ')
407///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000408/// proc_bind-clause:
409/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
410///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000411OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
412 SourceLocation Loc = Tok.getLocation();
413 SourceLocation LOpen = ConsumeToken();
414 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000415 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000416 if (T.expectAndConsume(diag::err_expected_lparen_after,
417 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000418 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000419
Alexey Bataeva55ed262014-05-28 06:15:33 +0000420 unsigned Type = getOpenMPSimpleClauseType(
421 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000422 SourceLocation TypeLoc = Tok.getLocation();
423 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
424 Tok.isNot(tok::annot_pragma_openmp_end))
425 ConsumeAnyToken();
426
427 // Parse ')'.
428 T.consumeClose();
429
430 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
431 Tok.getLocation());
432}
433
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000434/// \brief Parsing of OpenMP clauses like 'ordered'.
435///
436/// ordered-clause:
437/// 'ordered'
438///
Alexey Bataev236070f2014-06-20 11:19:47 +0000439/// nowait-clause:
440/// 'nowait'
441///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000442OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
443 SourceLocation Loc = Tok.getLocation();
444 ConsumeAnyToken();
445
446 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
447}
448
449
Alexey Bataev56dafe82014-06-20 07:16:17 +0000450/// \brief Parsing of OpenMP clauses with single expressions and some additional
451/// argument like 'schedule' or 'dist_schedule'.
452///
453/// schedule-clause:
454/// 'schedule' '(' kind [',' expression ] ')'
455///
456OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
457 SourceLocation Loc = ConsumeToken();
458 SourceLocation CommaLoc;
459 // Parse '('.
460 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
461 if (T.expectAndConsume(diag::err_expected_lparen_after,
462 getOpenMPClauseName(Kind)))
463 return nullptr;
464
465 ExprResult Val;
466 unsigned Type = getOpenMPSimpleClauseType(
467 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
468 SourceLocation KLoc = Tok.getLocation();
469 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
470 Tok.isNot(tok::annot_pragma_openmp_end))
471 ConsumeAnyToken();
472
473 if (Kind == OMPC_schedule &&
474 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
475 Type == OMPC_SCHEDULE_guided) &&
476 Tok.is(tok::comma)) {
477 CommaLoc = ConsumeAnyToken();
478 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
479 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
480 if (Val.isInvalid())
481 return nullptr;
482 }
483
484 // Parse ')'.
485 T.consumeClose();
486
487 return Actions.ActOnOpenMPSingleExprWithArgClause(
488 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
489 T.getCloseLocation());
490}
491
Alexey Bataevc5e02582014-06-16 07:08:35 +0000492static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
493 UnqualifiedId &ReductionId) {
494 SourceLocation TemplateKWLoc;
495 if (ReductionIdScopeSpec.isEmpty()) {
496 auto OOK = OO_None;
497 switch (P.getCurToken().getKind()) {
498 case tok::plus:
499 OOK = OO_Plus;
500 break;
501 case tok::minus:
502 OOK = OO_Minus;
503 break;
504 case tok::star:
505 OOK = OO_Star;
506 break;
507 case tok::amp:
508 OOK = OO_Amp;
509 break;
510 case tok::pipe:
511 OOK = OO_Pipe;
512 break;
513 case tok::caret:
514 OOK = OO_Caret;
515 break;
516 case tok::ampamp:
517 OOK = OO_AmpAmp;
518 break;
519 case tok::pipepipe:
520 OOK = OO_PipePipe;
521 break;
522 default:
523 break;
524 }
525 if (OOK != OO_None) {
526 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000527 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000528 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
529 return false;
530 }
531 }
532 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
533 /*AllowDestructorName*/ false,
534 /*AllowConstructorName*/ false, ParsedType(),
535 TemplateKWLoc, ReductionId);
536}
537
Alexander Musman1bb328c2014-06-04 13:06:39 +0000538/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000539/// 'shared', 'copyin', or 'reduction'.
540///
541/// private-clause:
542/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000543/// firstprivate-clause:
544/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000545/// lastprivate-clause:
546/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000547/// shared-clause:
548/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000549/// linear-clause:
550/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000551/// aligned-clause:
552/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000553/// reduction-clause:
554/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000555///
556OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
557 SourceLocation Loc = Tok.getLocation();
558 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000559 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000560 // Optional scope specifier and unqualified id for reduction identifier.
561 CXXScopeSpec ReductionIdScopeSpec;
562 UnqualifiedId ReductionId;
563 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000564 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000565 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000566 if (T.expectAndConsume(diag::err_expected_lparen_after,
567 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000568 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000569
Alexey Bataevc5e02582014-06-16 07:08:35 +0000570 // Handle reduction-identifier for reduction clause.
571 if (Kind == OMPC_reduction) {
572 ColonProtectionRAIIObject ColonRAII(*this);
573 if (getLangOpts().CPlusPlus) {
574 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
575 }
576 InvalidReductionId =
577 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
578 if (InvalidReductionId) {
579 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
580 StopBeforeMatch);
581 }
582 if (Tok.is(tok::colon)) {
583 ColonLoc = ConsumeToken();
584 } else {
585 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
586 }
587 }
588
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000589 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000590 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000591 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000592 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000593 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000594 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000595 // Parse variable
596 ExprResult VarExpr = ParseAssignmentExpression();
597 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000598 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000599 } else {
600 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000601 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000602 }
603 // Skip ',' if any
604 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000605 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000606 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000607 else if (Tok.isNot(tok::r_paren) &&
608 Tok.isNot(tok::annot_pragma_openmp_end) &&
609 (!MayHaveTail || Tok.isNot(tok::colon)))
610 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
611 }
612
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000613 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000614 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000615 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
616 if (MustHaveTail) {
617 ColonLoc = Tok.getLocation();
618 ConsumeToken();
619 ExprResult Tail = ParseAssignmentExpression();
620 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000621 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000622 else
623 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
624 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000625 }
626
627 // Parse ')'.
628 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000629 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000630 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000631
Alexey Bataevc5e02582014-06-16 07:08:35 +0000632 return Actions.ActOnOpenMPVarListClause(
633 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
634 ReductionIdScopeSpec,
635 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
636 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000637}
638