blob: 11e888efb4033ffdee6205beea521d49356f5a54 [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 Bataeva769e072013-03-22 06:34:35 +000068 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000069 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000070 break;
71 }
Alp Tokerd751fa72013-12-18 19:10:49 +000072 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000073 return DeclGroupPtrTy();
74}
75
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000076/// \brief Parsing of declarative or executable OpenMP directives.
77///
78/// threadprivate-directive:
79/// annot_pragma_openmp 'threadprivate' simple-variable-list
80/// annot_pragma_openmp_end
81///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000082/// executable-directive:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000083/// annot_pragma_openmp 'parallel'|'simd'|'for'|'sections'|'section'
84/// {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000085///
86StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
87 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000088 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000089 SmallVector<Expr *, 5> Identifiers;
90 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000091 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +000092 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000093 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +000094 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000095 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataeva55ed262014-05-28 06:15:33 +000096 OpenMPDirectiveKind DKind = Tok.isAnnotation()
97 ? OMPD_unknown
98 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +000099 // Name of critical directive.
100 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000101 StmtResult Directive = StmtError();
102
103 switch (DKind) {
104 case OMPD_threadprivate:
105 ConsumeToken();
106 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
107 // The last seen token is annot_pragma_openmp_end - need to check for
108 // extra tokens.
109 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
110 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000111 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000112 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000113 }
114 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000115 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000116 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
117 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000118 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000119 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000120 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000121 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000122 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000123 case OMPD_sections:
124 case OMPD_section: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000125 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126
Alexey Bataevf29276e2014-06-18 04:14:57 +0000127 if (isOpenMPLoopDirective(DKind))
128 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
129 if (isOpenMPSimdDirective(DKind))
130 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
131 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000132 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
133
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000134 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000135 OpenMPClauseKind CKind = Tok.isAnnotation()
136 ? OMPC_unknown
137 : getOpenMPClauseKind(PP.getSpelling(Tok));
138 OMPClause *Clause =
139 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000140 FirstClauses[CKind].setInt(true);
141 if (Clause) {
142 FirstClauses[CKind].setPointer(Clause);
143 Clauses.push_back(Clause);
144 }
145
146 // Skip ',' if any.
147 if (Tok.is(tok::comma))
148 ConsumeToken();
149 }
150 // End location of the directive.
151 EndLoc = Tok.getLocation();
152 // Consume final annot_pragma_openmp_end.
153 ConsumeToken();
154
155 StmtResult AssociatedStmt;
156 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000157 {
158 // The body is a block scope like in Lambdas and Blocks.
159 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev9959db52014-05-06 10:08:46 +0000160 Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000161 Actions.ActOnStartOfCompoundStmt();
162 // Parse statement
163 AssociatedStmt = ParseStatement();
164 Actions.ActOnFinishOfCompoundStmt();
165 if (!AssociatedStmt.isUsable()) {
166 Actions.ActOnCapturedRegionError();
167 CreateDirective = false;
168 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000169 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000170 CreateDirective = AssociatedStmt.isUsable();
171 }
172 }
173 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000174 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000175 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000176
177 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000178 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000179 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000180 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000181 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182 case OMPD_unknown:
183 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000184 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000185 break;
186 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000187 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000188 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000189 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000190 break;
191 }
192 return Directive;
193}
194
Alexey Bataeva769e072013-03-22 06:34:35 +0000195/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000196/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000197///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000198/// simple-variable-list:
199/// '(' id-expression {, id-expression} ')'
200///
201bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
202 SmallVectorImpl<Expr *> &VarList,
203 bool AllowScopeSpecifier) {
204 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000205 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000206 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000207 if (T.expectAndConsume(diag::err_expected_lparen_after,
208 getOpenMPDirectiveName(Kind)))
209 return true;
210 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000211 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000212
213 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000214 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000215 CXXScopeSpec SS;
216 SourceLocation TemplateKWLoc;
217 UnqualifiedId Name;
218 // Read var name.
219 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000220 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000221
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000222 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
223 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000224 IsCorrect = false;
225 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000226 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000227 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
228 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000229 IsCorrect = false;
230 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000231 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000232 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
233 Tok.isNot(tok::annot_pragma_openmp_end)) {
234 IsCorrect = false;
235 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000236 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000237 Diag(PrevTok.getLocation(), diag::err_expected)
238 << tok::identifier
239 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000240 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000241 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000242 ExprResult Res =
243 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000244 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000245 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000246 }
247 // Consume ','.
248 if (Tok.is(tok::comma)) {
249 ConsumeToken();
250 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000251 }
252
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000253 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000254 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000255 IsCorrect = false;
256 }
257
258 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000259 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000260
261 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000262}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000263
264/// \brief Parsing of OpenMP clauses.
265///
266/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000267/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000268/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000269/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataev56dafe82014-06-20 07:16:17 +0000270/// reduction-clause | proc_bind-clause | schedule-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000271///
272OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
273 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000274 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000275 bool ErrorFound = false;
276 // Check if clause is allowed for the given directive.
277 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000278 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
279 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000280 ErrorFound = true;
281 }
282
283 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000284 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000285 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000286 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000287 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000288 // OpenMP [2.5, Restrictions]
289 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000290 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000291 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000292 // Only one safelen clause can appear on a simd directive.
293 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000294 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000295 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
296 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000297 }
298
299 Clause = ParseOpenMPSingleExprClause(CKind);
300 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000301 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000302 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000303 // OpenMP [2.14.3.1, Restrictions]
304 // Only a single default clause may be specified on a parallel, task or
305 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000306 // OpenMP [2.5, parallel Construct, Restrictions]
307 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000308 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000309 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
310 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000311 }
312
313 Clause = ParseOpenMPSimpleClause(CKind);
314 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000315 case OMPC_schedule:
316 // OpenMP [2.7.1, Restrictions, p. 3]
317 // Only one schedule clause can appear on a loop directive.
318 if (!FirstClause) {
319 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
320 << getOpenMPClauseName(CKind);
321 }
322
323 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
324 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000325 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000326 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000327 // OpenMP [2.7.1, Restrictions, p. 9]
328 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000329 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
330 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000331 if (!FirstClause) {
332 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
333 << getOpenMPClauseName(CKind);
334 }
335
336 Clause = ParseOpenMPClause(CKind);
337 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000338 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000339 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000340 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000341 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000342 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000343 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000344 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000345 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000346 Clause = ParseOpenMPVarListClause(CKind);
347 break;
348 case OMPC_unknown:
349 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000350 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000351 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000352 break;
353 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000354 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
355 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000356 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000357 break;
358 }
Craig Topper161e4db2014-05-21 06:02:52 +0000359 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000360}
361
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000362/// \brief Parsing of OpenMP clauses with single expressions like 'if',
363/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
364/// 'thread_limit'.
365///
366/// if-clause:
367/// 'if' '(' expression ')'
368///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000369/// num_threads-clause:
370/// 'num_threads' '(' expression ')'
371///
372/// safelen-clause:
373/// 'safelen' '(' expression ')'
374///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000375/// collapse-clause:
376/// 'collapse' '(' expression ')'
377///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000378OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
379 SourceLocation Loc = ConsumeToken();
380
381 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
382 if (T.expectAndConsume(diag::err_expected_lparen_after,
383 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000384 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000385
386 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
387 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
388
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000389 // Parse ')'.
390 T.consumeClose();
391
392 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000393 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000394
Alexey Bataeva55ed262014-05-28 06:15:33 +0000395 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000396 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000397}
398
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000399/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000400///
401/// default-clause:
402/// 'default' '(' 'none' | 'shared' ')
403///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000404/// proc_bind-clause:
405/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
406///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000407OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
408 SourceLocation Loc = Tok.getLocation();
409 SourceLocation LOpen = ConsumeToken();
410 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000411 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000412 if (T.expectAndConsume(diag::err_expected_lparen_after,
413 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000414 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000415
Alexey Bataeva55ed262014-05-28 06:15:33 +0000416 unsigned Type = getOpenMPSimpleClauseType(
417 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000418 SourceLocation TypeLoc = Tok.getLocation();
419 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
420 Tok.isNot(tok::annot_pragma_openmp_end))
421 ConsumeAnyToken();
422
423 // Parse ')'.
424 T.consumeClose();
425
426 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
427 Tok.getLocation());
428}
429
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000430/// \brief Parsing of OpenMP clauses like 'ordered'.
431///
432/// ordered-clause:
433/// 'ordered'
434///
Alexey Bataev236070f2014-06-20 11:19:47 +0000435/// nowait-clause:
436/// 'nowait'
437///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000438OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
439 SourceLocation Loc = Tok.getLocation();
440 ConsumeAnyToken();
441
442 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
443}
444
445
Alexey Bataev56dafe82014-06-20 07:16:17 +0000446/// \brief Parsing of OpenMP clauses with single expressions and some additional
447/// argument like 'schedule' or 'dist_schedule'.
448///
449/// schedule-clause:
450/// 'schedule' '(' kind [',' expression ] ')'
451///
452OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
453 SourceLocation Loc = ConsumeToken();
454 SourceLocation CommaLoc;
455 // Parse '('.
456 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
457 if (T.expectAndConsume(diag::err_expected_lparen_after,
458 getOpenMPClauseName(Kind)))
459 return nullptr;
460
461 ExprResult Val;
462 unsigned Type = getOpenMPSimpleClauseType(
463 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
464 SourceLocation KLoc = Tok.getLocation();
465 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
466 Tok.isNot(tok::annot_pragma_openmp_end))
467 ConsumeAnyToken();
468
469 if (Kind == OMPC_schedule &&
470 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
471 Type == OMPC_SCHEDULE_guided) &&
472 Tok.is(tok::comma)) {
473 CommaLoc = ConsumeAnyToken();
474 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
475 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
476 if (Val.isInvalid())
477 return nullptr;
478 }
479
480 // Parse ')'.
481 T.consumeClose();
482
483 return Actions.ActOnOpenMPSingleExprWithArgClause(
484 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
485 T.getCloseLocation());
486}
487
Alexey Bataevc5e02582014-06-16 07:08:35 +0000488static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
489 UnqualifiedId &ReductionId) {
490 SourceLocation TemplateKWLoc;
491 if (ReductionIdScopeSpec.isEmpty()) {
492 auto OOK = OO_None;
493 switch (P.getCurToken().getKind()) {
494 case tok::plus:
495 OOK = OO_Plus;
496 break;
497 case tok::minus:
498 OOK = OO_Minus;
499 break;
500 case tok::star:
501 OOK = OO_Star;
502 break;
503 case tok::amp:
504 OOK = OO_Amp;
505 break;
506 case tok::pipe:
507 OOK = OO_Pipe;
508 break;
509 case tok::caret:
510 OOK = OO_Caret;
511 break;
512 case tok::ampamp:
513 OOK = OO_AmpAmp;
514 break;
515 case tok::pipepipe:
516 OOK = OO_PipePipe;
517 break;
518 default:
519 break;
520 }
521 if (OOK != OO_None) {
522 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000523 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000524 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
525 return false;
526 }
527 }
528 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
529 /*AllowDestructorName*/ false,
530 /*AllowConstructorName*/ false, ParsedType(),
531 TemplateKWLoc, ReductionId);
532}
533
Alexander Musman1bb328c2014-06-04 13:06:39 +0000534/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000535/// 'shared', 'copyin', or 'reduction'.
536///
537/// private-clause:
538/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000539/// firstprivate-clause:
540/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000541/// lastprivate-clause:
542/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000543/// shared-clause:
544/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000545/// linear-clause:
546/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000547/// aligned-clause:
548/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000549/// reduction-clause:
550/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000551///
552OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
553 SourceLocation Loc = Tok.getLocation();
554 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000555 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000556 // Optional scope specifier and unqualified id for reduction identifier.
557 CXXScopeSpec ReductionIdScopeSpec;
558 UnqualifiedId ReductionId;
559 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000560 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000561 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000562 if (T.expectAndConsume(diag::err_expected_lparen_after,
563 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000564 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000565
Alexey Bataevc5e02582014-06-16 07:08:35 +0000566 // Handle reduction-identifier for reduction clause.
567 if (Kind == OMPC_reduction) {
568 ColonProtectionRAIIObject ColonRAII(*this);
569 if (getLangOpts().CPlusPlus) {
570 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
571 }
572 InvalidReductionId =
573 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
574 if (InvalidReductionId) {
575 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
576 StopBeforeMatch);
577 }
578 if (Tok.is(tok::colon)) {
579 ColonLoc = ConsumeToken();
580 } else {
581 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
582 }
583 }
584
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000585 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000586 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000587 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000588 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000589 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000590 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591 // Parse variable
592 ExprResult VarExpr = ParseAssignmentExpression();
593 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000594 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000595 } else {
596 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000597 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000598 }
599 // Skip ',' if any
600 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000601 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000602 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000603 else if (Tok.isNot(tok::r_paren) &&
604 Tok.isNot(tok::annot_pragma_openmp_end) &&
605 (!MayHaveTail || Tok.isNot(tok::colon)))
606 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
607 }
608
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000609 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000610 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000611 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
612 if (MustHaveTail) {
613 ColonLoc = Tok.getLocation();
614 ConsumeToken();
615 ExprResult Tail = ParseAssignmentExpression();
616 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000617 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000618 else
619 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
620 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000621 }
622
623 // Parse ')'.
624 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000625 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000626 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000627
Alexey Bataevc5e02582014-06-16 07:08:35 +0000628 return Actions.ActOnOpenMPVarListClause(
629 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
630 ReductionIdScopeSpec,
631 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
632 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000633}
634