blob: 7a3c05e96bedda1693b17e0ff1428f222133933f [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 Bataeva769e072013-03-22 06:34:35 +000067 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000068 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000069 break;
70 }
Alp Tokerd751fa72013-12-18 19:10:49 +000071 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000072 return DeclGroupPtrTy();
73}
74
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000075/// \brief Parsing of declarative or executable OpenMP directives.
76///
77/// threadprivate-directive:
78/// annot_pragma_openmp 'threadprivate' simple-variable-list
79/// annot_pragma_openmp_end
80///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000081/// executable-directive:
82/// annot_pragma_openmp 'parallel'|'simd'|'for'|'sections' {clause}
83/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000084///
85StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
86 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000087 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000088 SmallVector<Expr *, 5> Identifiers;
89 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000090 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +000091 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000092 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +000093 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000094 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataeva55ed262014-05-28 06:15:33 +000095 OpenMPDirectiveKind DKind = Tok.isAnnotation()
96 ? OMPD_unknown
97 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +000098 // Name of critical directive.
99 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000100 StmtResult Directive = StmtError();
101
102 switch (DKind) {
103 case OMPD_threadprivate:
104 ConsumeToken();
105 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
106 // The last seen token is annot_pragma_openmp_end - need to check for
107 // extra tokens.
108 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
109 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000110 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000111 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000112 }
113 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000114 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
116 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000117 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000118 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000119 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000120 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000121 case OMPD_for:
122 case OMPD_sections: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000123 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124
Alexey Bataevf29276e2014-06-18 04:14:57 +0000125 if (isOpenMPLoopDirective(DKind))
126 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
127 if (isOpenMPSimdDirective(DKind))
128 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
129 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
131
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000132 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000133 OpenMPClauseKind CKind = Tok.isAnnotation()
134 ? OMPC_unknown
135 : getOpenMPClauseKind(PP.getSpelling(Tok));
136 OMPClause *Clause =
137 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000138 FirstClauses[CKind].setInt(true);
139 if (Clause) {
140 FirstClauses[CKind].setPointer(Clause);
141 Clauses.push_back(Clause);
142 }
143
144 // Skip ',' if any.
145 if (Tok.is(tok::comma))
146 ConsumeToken();
147 }
148 // End location of the directive.
149 EndLoc = Tok.getLocation();
150 // Consume final annot_pragma_openmp_end.
151 ConsumeToken();
152
153 StmtResult AssociatedStmt;
154 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000155 {
156 // The body is a block scope like in Lambdas and Blocks.
157 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev9959db52014-05-06 10:08:46 +0000158 Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000159 Actions.ActOnStartOfCompoundStmt();
160 // Parse statement
161 AssociatedStmt = ParseStatement();
162 Actions.ActOnFinishOfCompoundStmt();
163 if (!AssociatedStmt.isUsable()) {
164 Actions.ActOnCapturedRegionError();
165 CreateDirective = false;
166 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000167 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 CreateDirective = AssociatedStmt.isUsable();
169 }
170 }
171 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000172 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000173 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000174
175 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000178 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000179 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000180 case OMPD_unknown:
181 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000182 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000183 break;
184 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000185 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000186 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000187 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000188 break;
189 }
190 return Directive;
191}
192
Alexey Bataeva769e072013-03-22 06:34:35 +0000193/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000194/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000195///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000196/// simple-variable-list:
197/// '(' id-expression {, id-expression} ')'
198///
199bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
200 SmallVectorImpl<Expr *> &VarList,
201 bool AllowScopeSpecifier) {
202 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000203 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000204 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000205 if (T.expectAndConsume(diag::err_expected_lparen_after,
206 getOpenMPDirectiveName(Kind)))
207 return true;
208 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000209 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000210
211 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000212 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000213 CXXScopeSpec SS;
214 SourceLocation TemplateKWLoc;
215 UnqualifiedId Name;
216 // Read var name.
217 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000218 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000219
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000220 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
221 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000222 IsCorrect = false;
223 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000224 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000225 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
226 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000227 IsCorrect = false;
228 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000229 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000230 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
231 Tok.isNot(tok::annot_pragma_openmp_end)) {
232 IsCorrect = false;
233 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000234 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000235 Diag(PrevTok.getLocation(), diag::err_expected)
236 << tok::identifier
237 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000238 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000239 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000240 ExprResult Res =
241 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000242 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000243 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000244 }
245 // Consume ','.
246 if (Tok.is(tok::comma)) {
247 ConsumeToken();
248 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000249 }
250
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000251 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000252 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000253 IsCorrect = false;
254 }
255
256 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000257 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000258
259 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000260}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000261
262/// \brief Parsing of OpenMP clauses.
263///
264/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000265/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000266/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000267/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataev56dafe82014-06-20 07:16:17 +0000268/// reduction-clause | proc_bind-clause | schedule-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000269///
270OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
271 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000272 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000273 bool ErrorFound = false;
274 // Check if clause is allowed for the given directive.
275 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000276 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
277 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000278 ErrorFound = true;
279 }
280
281 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000282 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000283 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000284 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000285 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000286 // OpenMP [2.5, Restrictions]
287 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000288 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000289 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000290 // Only one safelen clause can appear on a simd directive.
291 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000292 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000293 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
294 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000295 }
296
297 Clause = ParseOpenMPSingleExprClause(CKind);
298 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000299 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000300 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000301 // OpenMP [2.14.3.1, Restrictions]
302 // Only a single default clause may be specified on a parallel, task or
303 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000304 // OpenMP [2.5, parallel Construct, Restrictions]
305 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000306 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000307 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
308 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000309 }
310
311 Clause = ParseOpenMPSimpleClause(CKind);
312 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000313 case OMPC_schedule:
314 // OpenMP [2.7.1, Restrictions, p. 3]
315 // Only one schedule clause can appear on a loop directive.
316 if (!FirstClause) {
317 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
318 << getOpenMPClauseName(CKind);
319 }
320
321 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
322 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000323 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000324 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000325 // OpenMP [2.7.1, Restrictions, p. 9]
326 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000327 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
328 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000329 if (!FirstClause) {
330 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
331 << getOpenMPClauseName(CKind);
332 }
333
334 Clause = ParseOpenMPClause(CKind);
335 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000336 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000337 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000338 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000340 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000341 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000342 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000343 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000344 Clause = ParseOpenMPVarListClause(CKind);
345 break;
346 case OMPC_unknown:
347 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000348 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000349 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000350 break;
351 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000352 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
353 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000354 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000355 break;
356 }
Craig Topper161e4db2014-05-21 06:02:52 +0000357 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000358}
359
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000360/// \brief Parsing of OpenMP clauses with single expressions like 'if',
361/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
362/// 'thread_limit'.
363///
364/// if-clause:
365/// 'if' '(' expression ')'
366///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000367/// num_threads-clause:
368/// 'num_threads' '(' expression ')'
369///
370/// safelen-clause:
371/// 'safelen' '(' expression ')'
372///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000373/// collapse-clause:
374/// 'collapse' '(' expression ')'
375///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000376OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
377 SourceLocation Loc = ConsumeToken();
378
379 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
380 if (T.expectAndConsume(diag::err_expected_lparen_after,
381 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000382 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000383
384 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
385 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
386
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000387 // Parse ')'.
388 T.consumeClose();
389
390 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000391 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000392
Alexey Bataeva55ed262014-05-28 06:15:33 +0000393 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000394 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000395}
396
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000397/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000398///
399/// default-clause:
400/// 'default' '(' 'none' | 'shared' ')
401///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000402/// proc_bind-clause:
403/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
404///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000405OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
406 SourceLocation Loc = Tok.getLocation();
407 SourceLocation LOpen = ConsumeToken();
408 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000409 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000410 if (T.expectAndConsume(diag::err_expected_lparen_after,
411 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000412 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000413
Alexey Bataeva55ed262014-05-28 06:15:33 +0000414 unsigned Type = getOpenMPSimpleClauseType(
415 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000416 SourceLocation TypeLoc = Tok.getLocation();
417 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
418 Tok.isNot(tok::annot_pragma_openmp_end))
419 ConsumeAnyToken();
420
421 // Parse ')'.
422 T.consumeClose();
423
424 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
425 Tok.getLocation());
426}
427
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000428/// \brief Parsing of OpenMP clauses like 'ordered'.
429///
430/// ordered-clause:
431/// 'ordered'
432///
Alexey Bataev236070f2014-06-20 11:19:47 +0000433/// nowait-clause:
434/// 'nowait'
435///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000436OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
437 SourceLocation Loc = Tok.getLocation();
438 ConsumeAnyToken();
439
440 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
441}
442
443
Alexey Bataev56dafe82014-06-20 07:16:17 +0000444/// \brief Parsing of OpenMP clauses with single expressions and some additional
445/// argument like 'schedule' or 'dist_schedule'.
446///
447/// schedule-clause:
448/// 'schedule' '(' kind [',' expression ] ')'
449///
450OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
451 SourceLocation Loc = ConsumeToken();
452 SourceLocation CommaLoc;
453 // Parse '('.
454 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
455 if (T.expectAndConsume(diag::err_expected_lparen_after,
456 getOpenMPClauseName(Kind)))
457 return nullptr;
458
459 ExprResult Val;
460 unsigned Type = getOpenMPSimpleClauseType(
461 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
462 SourceLocation KLoc = Tok.getLocation();
463 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
464 Tok.isNot(tok::annot_pragma_openmp_end))
465 ConsumeAnyToken();
466
467 if (Kind == OMPC_schedule &&
468 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
469 Type == OMPC_SCHEDULE_guided) &&
470 Tok.is(tok::comma)) {
471 CommaLoc = ConsumeAnyToken();
472 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
473 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
474 if (Val.isInvalid())
475 return nullptr;
476 }
477
478 // Parse ')'.
479 T.consumeClose();
480
481 return Actions.ActOnOpenMPSingleExprWithArgClause(
482 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
483 T.getCloseLocation());
484}
485
Alexey Bataevc5e02582014-06-16 07:08:35 +0000486static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
487 UnqualifiedId &ReductionId) {
488 SourceLocation TemplateKWLoc;
489 if (ReductionIdScopeSpec.isEmpty()) {
490 auto OOK = OO_None;
491 switch (P.getCurToken().getKind()) {
492 case tok::plus:
493 OOK = OO_Plus;
494 break;
495 case tok::minus:
496 OOK = OO_Minus;
497 break;
498 case tok::star:
499 OOK = OO_Star;
500 break;
501 case tok::amp:
502 OOK = OO_Amp;
503 break;
504 case tok::pipe:
505 OOK = OO_Pipe;
506 break;
507 case tok::caret:
508 OOK = OO_Caret;
509 break;
510 case tok::ampamp:
511 OOK = OO_AmpAmp;
512 break;
513 case tok::pipepipe:
514 OOK = OO_PipePipe;
515 break;
516 default:
517 break;
518 }
519 if (OOK != OO_None) {
520 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000521 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000522 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
523 return false;
524 }
525 }
526 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
527 /*AllowDestructorName*/ false,
528 /*AllowConstructorName*/ false, ParsedType(),
529 TemplateKWLoc, ReductionId);
530}
531
Alexander Musman1bb328c2014-06-04 13:06:39 +0000532/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000533/// 'shared', 'copyin', or 'reduction'.
534///
535/// private-clause:
536/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000537/// firstprivate-clause:
538/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000539/// lastprivate-clause:
540/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000541/// shared-clause:
542/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000543/// linear-clause:
544/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000545/// aligned-clause:
546/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000547/// reduction-clause:
548/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000549///
550OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
551 SourceLocation Loc = Tok.getLocation();
552 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000553 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000554 // Optional scope specifier and unqualified id for reduction identifier.
555 CXXScopeSpec ReductionIdScopeSpec;
556 UnqualifiedId ReductionId;
557 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000558 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000559 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000560 if (T.expectAndConsume(diag::err_expected_lparen_after,
561 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000562 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000563
Alexey Bataevc5e02582014-06-16 07:08:35 +0000564 // Handle reduction-identifier for reduction clause.
565 if (Kind == OMPC_reduction) {
566 ColonProtectionRAIIObject ColonRAII(*this);
567 if (getLangOpts().CPlusPlus) {
568 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
569 }
570 InvalidReductionId =
571 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
572 if (InvalidReductionId) {
573 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
574 StopBeforeMatch);
575 }
576 if (Tok.is(tok::colon)) {
577 ColonLoc = ConsumeToken();
578 } else {
579 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
580 }
581 }
582
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000583 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000584 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000585 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000586 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000587 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000588 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000589 // Parse variable
590 ExprResult VarExpr = ParseAssignmentExpression();
591 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000592 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000593 } else {
594 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000595 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000596 }
597 // Skip ',' if any
598 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000599 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000600 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000601 else if (Tok.isNot(tok::r_paren) &&
602 Tok.isNot(tok::annot_pragma_openmp_end) &&
603 (!MayHaveTail || Tok.isNot(tok::colon)))
604 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
605 }
606
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000607 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000608 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000609 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
610 if (MustHaveTail) {
611 ColonLoc = Tok.getLocation();
612 ConsumeToken();
613 ExprResult Tail = ParseAssignmentExpression();
614 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000615 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000616 else
617 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
618 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000619 }
620
621 // Parse ')'.
622 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000623 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000624 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000625
Alexey Bataevc5e02582014-06-16 07:08:35 +0000626 return Actions.ActOnOpenMPVarListClause(
627 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
628 ReductionIdScopeSpec,
629 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
630 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000631}
632