blob: 599dda42124f7fcb3b95ba301e222e63bb70aa26 [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 Bataeva769e072013-03-22 06:34:35 +000066 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000067 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000068 break;
69 }
Alp Tokerd751fa72013-12-18 19:10:49 +000070 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000071 return DeclGroupPtrTy();
72}
73
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000074/// \brief Parsing of declarative or executable OpenMP directives.
75///
76/// threadprivate-directive:
77/// annot_pragma_openmp 'threadprivate' simple-variable-list
78/// annot_pragma_openmp_end
79///
80/// parallel-directive:
81/// annot_pragma_openmp 'parallel' {clause} annot_pragma_openmp_end
82///
83StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
84 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +000085 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000086 SmallVector<Expr *, 5> Identifiers;
87 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000088 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +000089 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000090 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +000091 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000092 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataeva55ed262014-05-28 06:15:33 +000093 OpenMPDirectiveKind DKind = Tok.isAnnotation()
94 ? OMPD_unknown
95 : getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev758e55e2013-09-06 18:03:48 +000096 // Name of critical directive.
97 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000098 StmtResult Directive = StmtError();
99
100 switch (DKind) {
101 case OMPD_threadprivate:
102 ConsumeToken();
103 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
104 // The last seen token is annot_pragma_openmp_end - need to check for
105 // extra tokens.
106 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
107 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000108 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000109 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000110 }
111 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000112 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000113 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
114 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000115 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000116 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000117 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000118 case OMPD_simd:
119 case OMPD_for: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121
Alexey Bataevf29276e2014-06-18 04:14:57 +0000122 if (isOpenMPLoopDirective(DKind))
123 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
124 if (isOpenMPSimdDirective(DKind))
125 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
126 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
128
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000129 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000130 OpenMPClauseKind CKind = Tok.isAnnotation()
131 ? OMPC_unknown
132 : getOpenMPClauseKind(PP.getSpelling(Tok));
133 OMPClause *Clause =
134 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000135 FirstClauses[CKind].setInt(true);
136 if (Clause) {
137 FirstClauses[CKind].setPointer(Clause);
138 Clauses.push_back(Clause);
139 }
140
141 // Skip ',' if any.
142 if (Tok.is(tok::comma))
143 ConsumeToken();
144 }
145 // End location of the directive.
146 EndLoc = Tok.getLocation();
147 // Consume final annot_pragma_openmp_end.
148 ConsumeToken();
149
150 StmtResult AssociatedStmt;
151 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000152 {
153 // The body is a block scope like in Lambdas and Blocks.
154 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev9959db52014-05-06 10:08:46 +0000155 Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000156 Actions.ActOnStartOfCompoundStmt();
157 // Parse statement
158 AssociatedStmt = ParseStatement();
159 Actions.ActOnFinishOfCompoundStmt();
160 if (!AssociatedStmt.isUsable()) {
161 Actions.ActOnCapturedRegionError();
162 CreateDirective = false;
163 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000164 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000165 CreateDirective = AssociatedStmt.isUsable();
166 }
167 }
168 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000169 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000170 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000171
172 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000174 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000175 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000176 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177 case OMPD_unknown:
178 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000179 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000180 break;
181 case OMPD_task:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000182 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000183 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000184 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000185 break;
186 }
187 return Directive;
188}
189
Alexey Bataeva769e072013-03-22 06:34:35 +0000190/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000191/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000192///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000193/// simple-variable-list:
194/// '(' id-expression {, id-expression} ')'
195///
196bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
197 SmallVectorImpl<Expr *> &VarList,
198 bool AllowScopeSpecifier) {
199 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000200 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000201 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000202 if (T.expectAndConsume(diag::err_expected_lparen_after,
203 getOpenMPDirectiveName(Kind)))
204 return true;
205 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000206 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000207
208 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000209 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000210 CXXScopeSpec SS;
211 SourceLocation TemplateKWLoc;
212 UnqualifiedId Name;
213 // Read var name.
214 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000215 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000216
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000217 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
218 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000219 IsCorrect = false;
220 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000221 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000222 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
223 TemplateKWLoc, Name)) {
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 (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
228 Tok.isNot(tok::annot_pragma_openmp_end)) {
229 IsCorrect = false;
230 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000231 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000232 Diag(PrevTok.getLocation(), diag::err_expected)
233 << tok::identifier
234 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000235 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000236 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000237 ExprResult Res =
238 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000239 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000240 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000241 }
242 // Consume ','.
243 if (Tok.is(tok::comma)) {
244 ConsumeToken();
245 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000246 }
247
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000248 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000249 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000250 IsCorrect = false;
251 }
252
253 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000254 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000255
256 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000257}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000258
259/// \brief Parsing of OpenMP clauses.
260///
261/// clause:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000262/// if-clause | num_threads-clause | safelen-clause | default-clause |
Alexander Musman8bd31e62014-05-27 15:12:19 +0000263/// private-clause | firstprivate-clause | shared-clause | linear-clause |
Alexey Bataevc5e02582014-06-16 07:08:35 +0000264/// aligned-clause | collapse-clause | lastprivate-clause |
Alexey Bataev56dafe82014-06-20 07:16:17 +0000265/// reduction-clause | proc_bind-clause | schedule-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000266///
267OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
268 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000269 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000270 bool ErrorFound = false;
271 // Check if clause is allowed for the given directive.
272 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000273 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
274 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000275 ErrorFound = true;
276 }
277
278 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000279 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000280 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000281 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000282 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000283 // OpenMP [2.5, Restrictions]
284 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000285 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000286 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000287 // Only one safelen clause can appear on a simd directive.
288 // Only one collapse clause can appear on a simd directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000289 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000290 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
291 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000292 }
293
294 Clause = ParseOpenMPSingleExprClause(CKind);
295 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000296 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000297 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000298 // OpenMP [2.14.3.1, Restrictions]
299 // Only a single default clause may be specified on a parallel, task or
300 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000301 // OpenMP [2.5, parallel Construct, Restrictions]
302 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000303 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000304 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
305 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000306 }
307
308 Clause = ParseOpenMPSimpleClause(CKind);
309 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000310 case OMPC_schedule:
311 // OpenMP [2.7.1, Restrictions, p. 3]
312 // Only one schedule clause can appear on a loop directive.
313 if (!FirstClause) {
314 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
315 << getOpenMPClauseName(CKind);
316 }
317
318 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
319 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000320 case OMPC_ordered:
321 // OpenMP [2.7.1, Restrictions, p. 9]
322 // Only one ordered clause can appear on a loop directive.
323 if (!FirstClause) {
324 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
325 << getOpenMPClauseName(CKind);
326 }
327
328 Clause = ParseOpenMPClause(CKind);
329 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000330 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000331 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000332 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000333 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000334 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000335 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000336 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000337 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000338 Clause = ParseOpenMPVarListClause(CKind);
339 break;
340 case OMPC_unknown:
341 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000342 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000343 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000344 break;
345 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000346 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
347 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000348 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000349 break;
350 }
Craig Topper161e4db2014-05-21 06:02:52 +0000351 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000352}
353
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000354/// \brief Parsing of OpenMP clauses with single expressions like 'if',
355/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
356/// 'thread_limit'.
357///
358/// if-clause:
359/// 'if' '(' expression ')'
360///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000361/// num_threads-clause:
362/// 'num_threads' '(' expression ')'
363///
364/// safelen-clause:
365/// 'safelen' '(' expression ')'
366///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000367/// collapse-clause:
368/// 'collapse' '(' expression ')'
369///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000370OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
371 SourceLocation Loc = ConsumeToken();
372
373 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
374 if (T.expectAndConsume(diag::err_expected_lparen_after,
375 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000376 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000377
378 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
379 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
380
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000381 // Parse ')'.
382 T.consumeClose();
383
384 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000385 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000386
Alexey Bataeva55ed262014-05-28 06:15:33 +0000387 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000388 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000389}
390
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000391/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000392///
393/// default-clause:
394/// 'default' '(' 'none' | 'shared' ')
395///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000396/// proc_bind-clause:
397/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
398///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000399OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
400 SourceLocation Loc = Tok.getLocation();
401 SourceLocation LOpen = ConsumeToken();
402 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000403 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000404 if (T.expectAndConsume(diag::err_expected_lparen_after,
405 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000406 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000407
Alexey Bataeva55ed262014-05-28 06:15:33 +0000408 unsigned Type = getOpenMPSimpleClauseType(
409 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000410 SourceLocation TypeLoc = Tok.getLocation();
411 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
412 Tok.isNot(tok::annot_pragma_openmp_end))
413 ConsumeAnyToken();
414
415 // Parse ')'.
416 T.consumeClose();
417
418 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
419 Tok.getLocation());
420}
421
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000422/// \brief Parsing of OpenMP clauses like 'ordered'.
423///
424/// ordered-clause:
425/// 'ordered'
426///
427OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
428 SourceLocation Loc = Tok.getLocation();
429 ConsumeAnyToken();
430
431 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
432}
433
434
Alexey Bataev56dafe82014-06-20 07:16:17 +0000435/// \brief Parsing of OpenMP clauses with single expressions and some additional
436/// argument like 'schedule' or 'dist_schedule'.
437///
438/// schedule-clause:
439/// 'schedule' '(' kind [',' expression ] ')'
440///
441OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
442 SourceLocation Loc = ConsumeToken();
443 SourceLocation CommaLoc;
444 // Parse '('.
445 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
446 if (T.expectAndConsume(diag::err_expected_lparen_after,
447 getOpenMPClauseName(Kind)))
448 return nullptr;
449
450 ExprResult Val;
451 unsigned Type = getOpenMPSimpleClauseType(
452 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
453 SourceLocation KLoc = Tok.getLocation();
454 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
455 Tok.isNot(tok::annot_pragma_openmp_end))
456 ConsumeAnyToken();
457
458 if (Kind == OMPC_schedule &&
459 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
460 Type == OMPC_SCHEDULE_guided) &&
461 Tok.is(tok::comma)) {
462 CommaLoc = ConsumeAnyToken();
463 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
464 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
465 if (Val.isInvalid())
466 return nullptr;
467 }
468
469 // Parse ')'.
470 T.consumeClose();
471
472 return Actions.ActOnOpenMPSingleExprWithArgClause(
473 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
474 T.getCloseLocation());
475}
476
Alexey Bataevc5e02582014-06-16 07:08:35 +0000477static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
478 UnqualifiedId &ReductionId) {
479 SourceLocation TemplateKWLoc;
480 if (ReductionIdScopeSpec.isEmpty()) {
481 auto OOK = OO_None;
482 switch (P.getCurToken().getKind()) {
483 case tok::plus:
484 OOK = OO_Plus;
485 break;
486 case tok::minus:
487 OOK = OO_Minus;
488 break;
489 case tok::star:
490 OOK = OO_Star;
491 break;
492 case tok::amp:
493 OOK = OO_Amp;
494 break;
495 case tok::pipe:
496 OOK = OO_Pipe;
497 break;
498 case tok::caret:
499 OOK = OO_Caret;
500 break;
501 case tok::ampamp:
502 OOK = OO_AmpAmp;
503 break;
504 case tok::pipepipe:
505 OOK = OO_PipePipe;
506 break;
507 default:
508 break;
509 }
510 if (OOK != OO_None) {
511 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000512 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000513 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
514 return false;
515 }
516 }
517 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
518 /*AllowDestructorName*/ false,
519 /*AllowConstructorName*/ false, ParsedType(),
520 TemplateKWLoc, ReductionId);
521}
522
Alexander Musman1bb328c2014-06-04 13:06:39 +0000523/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000524/// 'shared', 'copyin', or 'reduction'.
525///
526/// private-clause:
527/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000528/// firstprivate-clause:
529/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000530/// lastprivate-clause:
531/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000532/// shared-clause:
533/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000534/// linear-clause:
535/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000536/// aligned-clause:
537/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000538/// reduction-clause:
539/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000540///
541OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
542 SourceLocation Loc = Tok.getLocation();
543 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000544 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000545 // Optional scope specifier and unqualified id for reduction identifier.
546 CXXScopeSpec ReductionIdScopeSpec;
547 UnqualifiedId ReductionId;
548 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000549 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000551 if (T.expectAndConsume(diag::err_expected_lparen_after,
552 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000553 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000554
Alexey Bataevc5e02582014-06-16 07:08:35 +0000555 // Handle reduction-identifier for reduction clause.
556 if (Kind == OMPC_reduction) {
557 ColonProtectionRAIIObject ColonRAII(*this);
558 if (getLangOpts().CPlusPlus) {
559 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
560 }
561 InvalidReductionId =
562 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
563 if (InvalidReductionId) {
564 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
565 StopBeforeMatch);
566 }
567 if (Tok.is(tok::colon)) {
568 ColonLoc = ConsumeToken();
569 } else {
570 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
571 }
572 }
573
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000574 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000575 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000576 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000577 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000578 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000579 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000580 // Parse variable
581 ExprResult VarExpr = ParseAssignmentExpression();
582 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000583 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000584 } else {
585 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000586 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000587 }
588 // Skip ',' if any
589 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000590 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000592 else if (Tok.isNot(tok::r_paren) &&
593 Tok.isNot(tok::annot_pragma_openmp_end) &&
594 (!MayHaveTail || Tok.isNot(tok::colon)))
595 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
596 }
597
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000598 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000599 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000600 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
601 if (MustHaveTail) {
602 ColonLoc = Tok.getLocation();
603 ConsumeToken();
604 ExprResult Tail = ParseAssignmentExpression();
605 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000606 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000607 else
608 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
609 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000610 }
611
612 // Parse ')'.
613 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000614 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000615 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000616
Alexey Bataevc5e02582014-06-16 07:08:35 +0000617 return Actions.ActOnOpenMPVarListClause(
618 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
619 ReductionIdScopeSpec,
620 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
621 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000622}
623