blob: a6bc3a2c3322a0cfac676f1c8fded38a0fe55059 [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;
43 }
44 }
45 return DKind;
46}
47
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000048/// \brief Parsing of declarative OpenMP directives.
49///
50/// threadprivate-directive:
51/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataeva769e072013-03-22 06:34:35 +000052///
53Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
54 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000055 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataeva769e072013-03-22 06:34:35 +000056
57 SourceLocation Loc = ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000058 SmallVector<Expr *, 5> Identifiers;
Alexey Bataev4acb8592014-07-07 13:01:15 +000059 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000060
61 switch (DKind) {
Alexey Bataeva769e072013-03-22 06:34:35 +000062 case OMPD_threadprivate:
63 ConsumeToken();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000064 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataeva769e072013-03-22 06:34:35 +000065 // The last seen token is annot_pragma_openmp_end - need to check for
66 // extra tokens.
67 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
68 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +000069 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +000070 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataeva769e072013-03-22 06:34:35 +000071 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000072 // Skip the last annot_pragma_openmp_end.
Alexey Bataeva769e072013-03-22 06:34:35 +000073 ConsumeToken();
Alexey Bataeva55ed262014-05-28 06:15:33 +000074 return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataeva769e072013-03-22 06:34:35 +000075 }
76 break;
77 case OMPD_unknown:
78 Diag(Tok, diag::err_omp_unknown_directive);
79 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000080 case OMPD_parallel:
Alexey Bataev1b59ab52014-02-27 08:29:12 +000081 case OMPD_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000082 case OMPD_task:
Alexey Bataevf29276e2014-06-18 04:14:57 +000083 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000084 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000085 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000086 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +000087 case OMPD_parallel_for:
Alexey Bataeva769e072013-03-22 06:34:35 +000088 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000089 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000090 break;
91 }
Alp Tokerd751fa72013-12-18 19:10:49 +000092 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000093 return DeclGroupPtrTy();
94}
95
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000096/// \brief Parsing of declarative or executable OpenMP directives.
97///
98/// threadprivate-directive:
99/// annot_pragma_openmp 'threadprivate' simple-variable-list
100/// annot_pragma_openmp_end
101///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000102/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000103/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexey Bataev4acb8592014-07-07 13:01:15 +0000104/// 'section' | 'single' | 'parallel for' {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000105///
106StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
107 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000108 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000109 SmallVector<Expr *, 5> Identifiers;
110 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000111 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000112 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000113 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000114 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000116 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000117 // Name of critical directive.
118 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000119 StmtResult Directive = StmtError();
120
121 switch (DKind) {
122 case OMPD_threadprivate:
123 ConsumeToken();
124 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
125 // The last seen token is annot_pragma_openmp_end - need to check for
126 // extra tokens.
127 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
128 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000129 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000130 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000131 }
132 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000133 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000134 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
135 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000136 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000137 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000138 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000139 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000140 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000141 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000142 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000143 case OMPD_section:
144 case OMPD_parallel_for: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000145 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146
Alexey Bataevf29276e2014-06-18 04:14:57 +0000147 if (isOpenMPLoopDirective(DKind))
148 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
149 if (isOpenMPSimdDirective(DKind))
150 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
151 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000152 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000154 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000155 OpenMPClauseKind CKind = Tok.isAnnotation()
156 ? OMPC_unknown
157 : getOpenMPClauseKind(PP.getSpelling(Tok));
158 OMPClause *Clause =
159 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000160 FirstClauses[CKind].setInt(true);
161 if (Clause) {
162 FirstClauses[CKind].setPointer(Clause);
163 Clauses.push_back(Clause);
164 }
165
166 // Skip ',' if any.
167 if (Tok.is(tok::comma))
168 ConsumeToken();
169 }
170 // End location of the directive.
171 EndLoc = Tok.getLocation();
172 // Consume final annot_pragma_openmp_end.
173 ConsumeToken();
174
175 StmtResult AssociatedStmt;
176 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177 {
178 // The body is a block scope like in Lambdas and Blocks.
179 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000180 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000181 Actions.ActOnStartOfCompoundStmt();
182 // Parse statement
183 AssociatedStmt = ParseStatement();
184 Actions.ActOnFinishOfCompoundStmt();
185 if (!AssociatedStmt.isUsable()) {
186 Actions.ActOnCapturedRegionError();
187 CreateDirective = false;
188 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000189 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000190 CreateDirective = AssociatedStmt.isUsable();
191 }
192 }
193 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000194 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000195 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000196
197 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000199 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000200 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000201 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000202 case OMPD_unknown:
203 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000204 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000205 break;
206 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000207 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000208 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000209 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000210 break;
211 }
212 return Directive;
213}
214
Alexey Bataeva769e072013-03-22 06:34:35 +0000215/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000216/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000217///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000218/// simple-variable-list:
219/// '(' id-expression {, id-expression} ')'
220///
221bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
222 SmallVectorImpl<Expr *> &VarList,
223 bool AllowScopeSpecifier) {
224 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000225 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000226 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000227 if (T.expectAndConsume(diag::err_expected_lparen_after,
228 getOpenMPDirectiveName(Kind)))
229 return true;
230 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000231 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000232
233 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000234 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000235 CXXScopeSpec SS;
236 SourceLocation TemplateKWLoc;
237 UnqualifiedId Name;
238 // Read var name.
239 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000240 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000241
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000242 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
243 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000244 IsCorrect = false;
245 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000246 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000247 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
248 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000249 IsCorrect = false;
250 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000251 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000252 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
253 Tok.isNot(tok::annot_pragma_openmp_end)) {
254 IsCorrect = false;
255 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000256 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000257 Diag(PrevTok.getLocation(), diag::err_expected)
258 << tok::identifier
259 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000260 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000261 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000262 ExprResult Res =
263 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000264 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000265 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000266 }
267 // Consume ','.
268 if (Tok.is(tok::comma)) {
269 ConsumeToken();
270 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000271 }
272
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000273 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000274 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000275 IsCorrect = false;
276 }
277
278 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000279 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000280
281 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000282}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000283
284/// \brief Parsing of OpenMP clauses.
285///
286/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000287/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000288/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000289/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataevbae9a792014-06-27 10:37:06 +0000290/// reduction-clause | proc_bind-clause | schedule-clause |
291/// copyin-clause | copyprivate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000292///
293OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
294 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000295 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000296 bool ErrorFound = false;
297 // Check if clause is allowed for the given directive.
298 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000299 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
300 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000301 ErrorFound = true;
302 }
303
304 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000305 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000306 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000307 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000308 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000309 // OpenMP [2.5, Restrictions]
310 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000311 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000312 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000313 // Only one safelen clause can appear on a simd directive.
314 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000315 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000316 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
317 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000318 }
319
320 Clause = ParseOpenMPSingleExprClause(CKind);
321 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000322 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000323 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000324 // OpenMP [2.14.3.1, Restrictions]
325 // Only a single default clause may be specified on a parallel, task or
326 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000327 // OpenMP [2.5, parallel Construct, Restrictions]
328 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000329 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000330 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
331 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000332 }
333
334 Clause = ParseOpenMPSimpleClause(CKind);
335 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000336 case OMPC_schedule:
337 // OpenMP [2.7.1, Restrictions, p. 3]
338 // Only one schedule clause can appear on a loop directive.
339 if (!FirstClause) {
340 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
341 << getOpenMPClauseName(CKind);
342 }
343
344 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
345 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000346 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000347 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000348 // OpenMP [2.7.1, Restrictions, p. 9]
349 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000350 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
351 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000352 if (!FirstClause) {
353 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
354 << getOpenMPClauseName(CKind);
355 }
356
357 Clause = ParseOpenMPClause(CKind);
358 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000359 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000360 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000361 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000362 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000363 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000364 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000365 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000366 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000367 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000368 Clause = ParseOpenMPVarListClause(CKind);
369 break;
370 case OMPC_unknown:
371 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000372 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000373 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000374 break;
375 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000376 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
377 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000378 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000379 break;
380 }
Craig Topper161e4db2014-05-21 06:02:52 +0000381 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000382}
383
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000384/// \brief Parsing of OpenMP clauses with single expressions like 'if',
385/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
386/// 'thread_limit'.
387///
388/// if-clause:
389/// 'if' '(' expression ')'
390///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000391/// num_threads-clause:
392/// 'num_threads' '(' expression ')'
393///
394/// safelen-clause:
395/// 'safelen' '(' expression ')'
396///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000397/// collapse-clause:
398/// 'collapse' '(' expression ')'
399///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000400OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
401 SourceLocation Loc = ConsumeToken();
402
403 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
404 if (T.expectAndConsume(diag::err_expected_lparen_after,
405 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000406 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000407
408 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
409 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
410
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000411 // Parse ')'.
412 T.consumeClose();
413
414 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000415 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000416
Alexey Bataeva55ed262014-05-28 06:15:33 +0000417 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000418 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000419}
420
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000421/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000422///
423/// default-clause:
424/// 'default' '(' 'none' | 'shared' ')
425///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000426/// proc_bind-clause:
427/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
428///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000429OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
430 SourceLocation Loc = Tok.getLocation();
431 SourceLocation LOpen = ConsumeToken();
432 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000433 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000434 if (T.expectAndConsume(diag::err_expected_lparen_after,
435 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000436 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000437
Alexey Bataeva55ed262014-05-28 06:15:33 +0000438 unsigned Type = getOpenMPSimpleClauseType(
439 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000440 SourceLocation TypeLoc = Tok.getLocation();
441 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
442 Tok.isNot(tok::annot_pragma_openmp_end))
443 ConsumeAnyToken();
444
445 // Parse ')'.
446 T.consumeClose();
447
448 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
449 Tok.getLocation());
450}
451
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000452/// \brief Parsing of OpenMP clauses like 'ordered'.
453///
454/// ordered-clause:
455/// 'ordered'
456///
Alexey Bataev236070f2014-06-20 11:19:47 +0000457/// nowait-clause:
458/// 'nowait'
459///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000460OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
461 SourceLocation Loc = Tok.getLocation();
462 ConsumeAnyToken();
463
464 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
465}
466
467
Alexey Bataev56dafe82014-06-20 07:16:17 +0000468/// \brief Parsing of OpenMP clauses with single expressions and some additional
469/// argument like 'schedule' or 'dist_schedule'.
470///
471/// schedule-clause:
472/// 'schedule' '(' kind [',' expression ] ')'
473///
474OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
475 SourceLocation Loc = ConsumeToken();
476 SourceLocation CommaLoc;
477 // Parse '('.
478 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
479 if (T.expectAndConsume(diag::err_expected_lparen_after,
480 getOpenMPClauseName(Kind)))
481 return nullptr;
482
483 ExprResult Val;
484 unsigned Type = getOpenMPSimpleClauseType(
485 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
486 SourceLocation KLoc = Tok.getLocation();
487 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
488 Tok.isNot(tok::annot_pragma_openmp_end))
489 ConsumeAnyToken();
490
491 if (Kind == OMPC_schedule &&
492 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
493 Type == OMPC_SCHEDULE_guided) &&
494 Tok.is(tok::comma)) {
495 CommaLoc = ConsumeAnyToken();
496 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
497 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
498 if (Val.isInvalid())
499 return nullptr;
500 }
501
502 // Parse ')'.
503 T.consumeClose();
504
505 return Actions.ActOnOpenMPSingleExprWithArgClause(
506 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
507 T.getCloseLocation());
508}
509
Alexey Bataevc5e02582014-06-16 07:08:35 +0000510static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
511 UnqualifiedId &ReductionId) {
512 SourceLocation TemplateKWLoc;
513 if (ReductionIdScopeSpec.isEmpty()) {
514 auto OOK = OO_None;
515 switch (P.getCurToken().getKind()) {
516 case tok::plus:
517 OOK = OO_Plus;
518 break;
519 case tok::minus:
520 OOK = OO_Minus;
521 break;
522 case tok::star:
523 OOK = OO_Star;
524 break;
525 case tok::amp:
526 OOK = OO_Amp;
527 break;
528 case tok::pipe:
529 OOK = OO_Pipe;
530 break;
531 case tok::caret:
532 OOK = OO_Caret;
533 break;
534 case tok::ampamp:
535 OOK = OO_AmpAmp;
536 break;
537 case tok::pipepipe:
538 OOK = OO_PipePipe;
539 break;
540 default:
541 break;
542 }
543 if (OOK != OO_None) {
544 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000545 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000546 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
547 return false;
548 }
549 }
550 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
551 /*AllowDestructorName*/ false,
552 /*AllowConstructorName*/ false, ParsedType(),
553 TemplateKWLoc, ReductionId);
554}
555
Alexander Musman1bb328c2014-06-04 13:06:39 +0000556/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000557/// 'shared', 'copyin', or 'reduction'.
558///
559/// private-clause:
560/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000561/// firstprivate-clause:
562/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000563/// lastprivate-clause:
564/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000565/// shared-clause:
566/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000567/// linear-clause:
568/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000569/// aligned-clause:
570/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000571/// reduction-clause:
572/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000573///
574OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
575 SourceLocation Loc = Tok.getLocation();
576 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000577 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000578 // Optional scope specifier and unqualified id for reduction identifier.
579 CXXScopeSpec ReductionIdScopeSpec;
580 UnqualifiedId ReductionId;
581 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000582 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000583 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000584 if (T.expectAndConsume(diag::err_expected_lparen_after,
585 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000586 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000587
Alexey Bataevc5e02582014-06-16 07:08:35 +0000588 // Handle reduction-identifier for reduction clause.
589 if (Kind == OMPC_reduction) {
590 ColonProtectionRAIIObject ColonRAII(*this);
591 if (getLangOpts().CPlusPlus) {
592 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
593 }
594 InvalidReductionId =
595 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
596 if (InvalidReductionId) {
597 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
598 StopBeforeMatch);
599 }
600 if (Tok.is(tok::colon)) {
601 ColonLoc = ConsumeToken();
602 } else {
603 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
604 }
605 }
606
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000607 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000608 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000609 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000610 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000611 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000612 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000613 // Parse variable
614 ExprResult VarExpr = ParseAssignmentExpression();
615 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000616 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000617 } else {
618 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000619 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000620 }
621 // Skip ',' if any
622 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000623 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000624 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000625 else if (Tok.isNot(tok::r_paren) &&
626 Tok.isNot(tok::annot_pragma_openmp_end) &&
627 (!MayHaveTail || Tok.isNot(tok::colon)))
628 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
629 }
630
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000631 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000632 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000633 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
634 if (MustHaveTail) {
635 ColonLoc = Tok.getLocation();
636 ConsumeToken();
637 ExprResult Tail = ParseAssignmentExpression();
638 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000639 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000640 else
641 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
642 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000643 }
644
645 // Parse ')'.
646 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000647 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000648 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000649
Alexey Bataevc5e02582014-06-16 07:08:35 +0000650 return Actions.ActOnOpenMPVarListClause(
651 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
652 ReductionIdScopeSpec,
653 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
654 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000655}
656