blob: b3a1063ed23287a526c088a9709d5b16ba2023f0 [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- ParseOpenMP.cpp - OpenMP directives parsing ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements parsing of all OpenMP directives and clauses.
11///
12//===----------------------------------------------------------------------===//
13
Chandler Carruth5553d0d2014-01-07 11:51:46 +000014#include "RAIIObjectsForParser.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/StmtOpenMP.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000019#include "clang/Parse/Parser.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/PointerIntPair.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// OpenMP declarative directives.
26//===----------------------------------------------------------------------===//
27
Alexey Bataev4acb8592014-07-07 13:01:15 +000028static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) {
29 auto Tok = P.getCurToken();
30 auto DKind =
31 Tok.isAnnotation()
32 ? OMPD_unknown
33 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
34 if (DKind == OMPD_parallel) {
35 Tok = P.getPreprocessor().LookAhead(0);
36 auto SDKind =
37 Tok.isAnnotation()
38 ? OMPD_unknown
39 : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok));
40 if (SDKind == OMPD_for) {
41 P.ConsumeToken();
42 DKind = OMPD_parallel_for;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000043 } else if (SDKind == OMPD_sections) {
44 P.ConsumeToken();
45 DKind = OMPD_parallel_sections;
Alexey Bataev4acb8592014-07-07 13:01:15 +000046 }
47 }
48 return DKind;
49}
50
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000051/// \brief Parsing of declarative OpenMP directives.
52///
53/// threadprivate-directive:
54/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000055///
56Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
57 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000058 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000059
60 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000061 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000062 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000063
64 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000065 case OMPD_threadprivate:
66 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000067 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000068 // The last seen token is annot_pragma_openmp_end - need to check for
69 // extra tokens.
70 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
71 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000072 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000073 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000074 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000075 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000076 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +000077 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000078 }
79 break;
80 case OMPD_unknown:
81 Diag(Tok, diag::err_omp_unknown_directive);
82 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000083 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000084 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000085 case OMPD_task:
Alexey Bataevf29276e2014-06-18 04:14:57 +000086 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000087 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000088 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000089 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +000090 case OMPD_parallel_for:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000091 case OMPD_parallel_sections:
Alexey Bataeva769e072013-03-22 06:34:35 +000092 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000093 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000094 break;
95 }
Alp Tokerd751fa72013-12-18 19:10:49 +000096 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000097 return DeclGroupPtrTy();
98}
99
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000100/// \brief Parsing of declarative or executable OpenMP directives.
101///
102/// threadprivate-directive:
103/// annot_pragma_openmp 'threadprivate' simple-variable-list
104/// annot_pragma_openmp_end
105///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000106/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000107/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000108/// 'section' | 'single' | 'parallel for' | 'parallel sections' {clause}
109/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000110///
111StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
112 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000113 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000114 SmallVector<Expr *, 5> Identifiers;
115 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000116 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000117 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000118 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000119 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000121 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122 // Name of critical directive.
123 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000124 StmtResult Directive = StmtError();
125
126 switch (DKind) {
127 case OMPD_threadprivate:
128 ConsumeToken();
129 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
130 // The last seen token is annot_pragma_openmp_end - need to check for
131 // extra tokens.
132 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
133 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000134 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000135 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000136 }
137 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000138 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000139 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
140 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000141 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000142 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000143 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000144 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000145 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000146 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000147 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000148 case OMPD_section:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000149 case OMPD_parallel_for:
150 case OMPD_parallel_sections: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000151 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000152
Alexey Bataevf29276e2014-06-18 04:14:57 +0000153 if (isOpenMPLoopDirective(DKind))
154 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
155 if (isOpenMPSimdDirective(DKind))
156 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
157 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000158 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000160 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000161 OpenMPClauseKind CKind = Tok.isAnnotation()
162 ? OMPC_unknown
163 : getOpenMPClauseKind(PP.getSpelling(Tok));
164 OMPClause *Clause =
165 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000166 FirstClauses[CKind].setInt(true);
167 if (Clause) {
168 FirstClauses[CKind].setPointer(Clause);
169 Clauses.push_back(Clause);
170 }
171
172 // Skip ',' if any.
173 if (Tok.is(tok::comma))
174 ConsumeToken();
175 }
176 // End location of the directive.
177 EndLoc = Tok.getLocation();
178 // Consume final annot_pragma_openmp_end.
179 ConsumeToken();
180
181 StmtResult AssociatedStmt;
182 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000183 {
184 // The body is a block scope like in Lambdas and Blocks.
185 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000186 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000187 Actions.ActOnStartOfCompoundStmt();
188 // Parse statement
189 AssociatedStmt = ParseStatement();
190 Actions.ActOnFinishOfCompoundStmt();
191 if (!AssociatedStmt.isUsable()) {
192 Actions.ActOnCapturedRegionError();
193 CreateDirective = false;
194 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000195 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000196 CreateDirective = AssociatedStmt.isUsable();
197 }
198 }
199 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000200 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000201 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000202
203 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000204 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000205 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000206 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000207 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000208 case OMPD_unknown:
209 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000210 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000211 break;
212 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000213 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000214 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000215 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000216 break;
217 }
218 return Directive;
219}
220
Alexey Bataeva769e072013-03-22 06:34:35 +0000221/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000222/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000223///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000224/// simple-variable-list:
225/// '(' id-expression {, id-expression} ')'
226///
227bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
228 SmallVectorImpl<Expr *> &VarList,
229 bool AllowScopeSpecifier) {
230 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000231 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000232 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000233 if (T.expectAndConsume(diag::err_expected_lparen_after,
234 getOpenMPDirectiveName(Kind)))
235 return true;
236 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000237 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000238
239 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000240 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000241 CXXScopeSpec SS;
242 SourceLocation TemplateKWLoc;
243 UnqualifiedId Name;
244 // Read var name.
245 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000246 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000247
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000248 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
249 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000250 IsCorrect = false;
251 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000252 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000253 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
254 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000255 IsCorrect = false;
256 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000257 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000258 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
259 Tok.isNot(tok::annot_pragma_openmp_end)) {
260 IsCorrect = false;
261 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000262 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000263 Diag(PrevTok.getLocation(), diag::err_expected)
264 << tok::identifier
265 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000266 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000267 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000268 ExprResult Res =
269 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000270 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000271 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000272 }
273 // Consume ','.
274 if (Tok.is(tok::comma)) {
275 ConsumeToken();
276 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000277 }
278
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000279 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000280 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000281 IsCorrect = false;
282 }
283
284 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000285 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000286
287 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000288}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000289
290/// \brief Parsing of OpenMP clauses.
291///
292/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000293/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000294/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000295/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataevbae9a792014-06-27 10:37:06 +0000296/// reduction-clause | proc_bind-clause | schedule-clause |
297/// copyin-clause | copyprivate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000298///
299OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
300 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000301 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000302 bool ErrorFound = false;
303 // Check if clause is allowed for the given directive.
304 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000305 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
306 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000307 ErrorFound = true;
308 }
309
310 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000311 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000312 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000313 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000314 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000315 // OpenMP [2.5, Restrictions]
316 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000317 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000318 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000319 // Only one safelen clause can appear on a simd directive.
320 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000321 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000322 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
323 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000324 }
325
326 Clause = ParseOpenMPSingleExprClause(CKind);
327 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000328 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000329 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000330 // OpenMP [2.14.3.1, Restrictions]
331 // Only a single default clause may be specified on a parallel, task or
332 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000333 // OpenMP [2.5, parallel Construct, Restrictions]
334 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000335 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000336 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
337 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000338 }
339
340 Clause = ParseOpenMPSimpleClause(CKind);
341 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000342 case OMPC_schedule:
343 // OpenMP [2.7.1, Restrictions, p. 3]
344 // Only one schedule clause can appear on a loop directive.
345 if (!FirstClause) {
346 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
347 << getOpenMPClauseName(CKind);
348 }
349
350 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
351 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000352 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000353 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000354 // OpenMP [2.7.1, Restrictions, p. 9]
355 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000356 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
357 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000358 if (!FirstClause) {
359 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
360 << getOpenMPClauseName(CKind);
361 }
362
363 Clause = ParseOpenMPClause(CKind);
364 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000365 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000366 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000367 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000368 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000369 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000370 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000371 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000372 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000373 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000374 Clause = ParseOpenMPVarListClause(CKind);
375 break;
376 case OMPC_unknown:
377 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000378 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000379 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000380 break;
381 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000382 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
383 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000384 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000385 break;
386 }
Craig Topper161e4db2014-05-21 06:02:52 +0000387 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000388}
389
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000390/// \brief Parsing of OpenMP clauses with single expressions like 'if',
391/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
392/// 'thread_limit'.
393///
394/// if-clause:
395/// 'if' '(' expression ')'
396///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000397/// num_threads-clause:
398/// 'num_threads' '(' expression ')'
399///
400/// safelen-clause:
401/// 'safelen' '(' expression ')'
402///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000403/// collapse-clause:
404/// 'collapse' '(' expression ')'
405///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000406OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
407 SourceLocation Loc = ConsumeToken();
408
409 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
410 if (T.expectAndConsume(diag::err_expected_lparen_after,
411 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000412 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000413
414 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
415 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
416
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000417 // Parse ')'.
418 T.consumeClose();
419
420 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000421 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000422
Alexey Bataeva55ed262014-05-28 06:15:33 +0000423 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000424 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000425}
426
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000427/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000428///
429/// default-clause:
430/// 'default' '(' 'none' | 'shared' ')
431///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000432/// proc_bind-clause:
433/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
434///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000435OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
436 SourceLocation Loc = Tok.getLocation();
437 SourceLocation LOpen = ConsumeToken();
438 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000439 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000440 if (T.expectAndConsume(diag::err_expected_lparen_after,
441 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000442 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000443
Alexey Bataeva55ed262014-05-28 06:15:33 +0000444 unsigned Type = getOpenMPSimpleClauseType(
445 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446 SourceLocation TypeLoc = Tok.getLocation();
447 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
448 Tok.isNot(tok::annot_pragma_openmp_end))
449 ConsumeAnyToken();
450
451 // Parse ')'.
452 T.consumeClose();
453
454 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
455 Tok.getLocation());
456}
457
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000458/// \brief Parsing of OpenMP clauses like 'ordered'.
459///
460/// ordered-clause:
461/// 'ordered'
462///
Alexey Bataev236070f2014-06-20 11:19:47 +0000463/// nowait-clause:
464/// 'nowait'
465///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000466OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
467 SourceLocation Loc = Tok.getLocation();
468 ConsumeAnyToken();
469
470 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
471}
472
473
Alexey Bataev56dafe82014-06-20 07:16:17 +0000474/// \brief Parsing of OpenMP clauses with single expressions and some additional
475/// argument like 'schedule' or 'dist_schedule'.
476///
477/// schedule-clause:
478/// 'schedule' '(' kind [',' expression ] ')'
479///
480OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
481 SourceLocation Loc = ConsumeToken();
482 SourceLocation CommaLoc;
483 // Parse '('.
484 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
485 if (T.expectAndConsume(diag::err_expected_lparen_after,
486 getOpenMPClauseName(Kind)))
487 return nullptr;
488
489 ExprResult Val;
490 unsigned Type = getOpenMPSimpleClauseType(
491 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
492 SourceLocation KLoc = Tok.getLocation();
493 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
494 Tok.isNot(tok::annot_pragma_openmp_end))
495 ConsumeAnyToken();
496
497 if (Kind == OMPC_schedule &&
498 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
499 Type == OMPC_SCHEDULE_guided) &&
500 Tok.is(tok::comma)) {
501 CommaLoc = ConsumeAnyToken();
502 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
503 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
504 if (Val.isInvalid())
505 return nullptr;
506 }
507
508 // Parse ')'.
509 T.consumeClose();
510
511 return Actions.ActOnOpenMPSingleExprWithArgClause(
512 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
513 T.getCloseLocation());
514}
515
Alexey Bataevc5e02582014-06-16 07:08:35 +0000516static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
517 UnqualifiedId &ReductionId) {
518 SourceLocation TemplateKWLoc;
519 if (ReductionIdScopeSpec.isEmpty()) {
520 auto OOK = OO_None;
521 switch (P.getCurToken().getKind()) {
522 case tok::plus:
523 OOK = OO_Plus;
524 break;
525 case tok::minus:
526 OOK = OO_Minus;
527 break;
528 case tok::star:
529 OOK = OO_Star;
530 break;
531 case tok::amp:
532 OOK = OO_Amp;
533 break;
534 case tok::pipe:
535 OOK = OO_Pipe;
536 break;
537 case tok::caret:
538 OOK = OO_Caret;
539 break;
540 case tok::ampamp:
541 OOK = OO_AmpAmp;
542 break;
543 case tok::pipepipe:
544 OOK = OO_PipePipe;
545 break;
546 default:
547 break;
548 }
549 if (OOK != OO_None) {
550 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000551 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000552 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
553 return false;
554 }
555 }
556 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
557 /*AllowDestructorName*/ false,
558 /*AllowConstructorName*/ false, ParsedType(),
559 TemplateKWLoc, ReductionId);
560}
561
Alexander Musman1bb328c2014-06-04 13:06:39 +0000562/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000563/// 'shared', 'copyin', or 'reduction'.
564///
565/// private-clause:
566/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000567/// firstprivate-clause:
568/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000569/// lastprivate-clause:
570/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000571/// shared-clause:
572/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000573/// linear-clause:
574/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000575/// aligned-clause:
576/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000577/// reduction-clause:
578/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000579///
580OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
581 SourceLocation Loc = Tok.getLocation();
582 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000583 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000584 // Optional scope specifier and unqualified id for reduction identifier.
585 CXXScopeSpec ReductionIdScopeSpec;
586 UnqualifiedId ReductionId;
587 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000588 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000589 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000590 if (T.expectAndConsume(diag::err_expected_lparen_after,
591 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000592 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000593
Alexey Bataevc5e02582014-06-16 07:08:35 +0000594 // Handle reduction-identifier for reduction clause.
595 if (Kind == OMPC_reduction) {
596 ColonProtectionRAIIObject ColonRAII(*this);
597 if (getLangOpts().CPlusPlus) {
598 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
599 }
600 InvalidReductionId =
601 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
602 if (InvalidReductionId) {
603 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
604 StopBeforeMatch);
605 }
606 if (Tok.is(tok::colon)) {
607 ColonLoc = ConsumeToken();
608 } else {
609 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
610 }
611 }
612
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000613 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000614 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000615 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000616 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000617 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000618 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000619 // Parse variable
620 ExprResult VarExpr = ParseAssignmentExpression();
621 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000622 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000623 } else {
624 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000625 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000626 }
627 // Skip ',' if any
628 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000629 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000630 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000631 else if (Tok.isNot(tok::r_paren) &&
632 Tok.isNot(tok::annot_pragma_openmp_end) &&
633 (!MayHaveTail || Tok.isNot(tok::colon)))
634 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
635 }
636
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000637 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000638 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000639 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
640 if (MustHaveTail) {
641 ColonLoc = Tok.getLocation();
642 ConsumeToken();
643 ExprResult Tail = ParseAssignmentExpression();
644 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000645 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000646 else
647 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
648 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000649 }
650
651 // Parse ')'.
652 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000653 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000654 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000655
Alexey Bataevc5e02582014-06-16 07:08:35 +0000656 return Actions.ActOnOpenMPVarListClause(
657 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
658 ReductionIdScopeSpec,
659 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
660 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000661}
662