blob: 79cf6af6fd3df4ed5b0ed115393e47b8b4cd8673 [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 Bataev68446b72014-07-18 07:47:19 +000086 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +000087 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +000088 case OMPD_taskwait:
Alexey Bataevf29276e2014-06-18 04:14:57 +000089 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000090 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000091 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000092 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +000093 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +000094 case OMPD_critical:
Alexey Bataev4acb8592014-07-07 13:01:15 +000095 case OMPD_parallel_for:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000096 case OMPD_parallel_sections:
Alexey Bataeva769e072013-03-22 06:34:35 +000097 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000098 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000099 break;
100 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000101 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000102 return DeclGroupPtrTy();
103}
104
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000105/// \brief Parsing of declarative or executable OpenMP directives.
106///
107/// threadprivate-directive:
108/// annot_pragma_openmp 'threadprivate' simple-variable-list
109/// annot_pragma_openmp_end
110///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000111/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000112/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000113/// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
114/// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
115/// 'barrier' | 'taskwait' {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000116///
Alexey Bataev68446b72014-07-18 07:47:19 +0000117StmtResult
118Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000119 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000120 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000121 SmallVector<Expr *, 5> Identifiers;
122 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000123 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000124 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000125 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000126 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000127 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000128 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129 // Name of critical directive.
130 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000131 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000132 bool HasAssociatedStatement = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000133
134 switch (DKind) {
135 case OMPD_threadprivate:
136 ConsumeToken();
137 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
138 // The last seen token is annot_pragma_openmp_end - need to check for
139 // extra tokens.
140 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
141 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000142 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000143 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000144 }
145 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000146 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000147 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
148 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000149 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000150 break;
Alexey Bataev68446b72014-07-18 07:47:19 +0000151 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000152 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000153 case OMPD_taskwait:
Alexey Bataev68446b72014-07-18 07:47:19 +0000154 if (!StandAloneAllowed) {
155 Diag(Tok, diag::err_omp_immediate_directive)
156 << getOpenMPDirectiveName(DKind);
157 }
158 HasAssociatedStatement = false;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000159 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000160 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000161 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000162 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000163 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000164 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000165 case OMPD_master:
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000166 case OMPD_critical:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000167 case OMPD_parallel_for:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000168 case OMPD_parallel_sections:
169 case OMPD_task: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000170 ConsumeToken();
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000171 // Parse directive name of the 'critical' directive if any.
172 if (DKind == OMPD_critical) {
173 BalancedDelimiterTracker T(*this, tok::l_paren,
174 tok::annot_pragma_openmp_end);
175 if (!T.consumeOpen()) {
176 if (Tok.isAnyIdentifier()) {
177 DirName =
178 DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
179 ConsumeAnyToken();
180 } else {
181 Diag(Tok, diag::err_omp_expected_identifier_for_critical);
182 }
183 T.consumeClose();
184 }
185 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186
Alexey Bataevf29276e2014-06-18 04:14:57 +0000187 if (isOpenMPLoopDirective(DKind))
188 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
189 if (isOpenMPSimdDirective(DKind))
190 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
191 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000192 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000193
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000194 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000195 OpenMPClauseKind CKind = Tok.isAnnotation()
196 ? OMPC_unknown
197 : getOpenMPClauseKind(PP.getSpelling(Tok));
198 OMPClause *Clause =
199 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000200 FirstClauses[CKind].setInt(true);
201 if (Clause) {
202 FirstClauses[CKind].setPointer(Clause);
203 Clauses.push_back(Clause);
204 }
205
206 // Skip ',' if any.
207 if (Tok.is(tok::comma))
208 ConsumeToken();
209 }
210 // End location of the directive.
211 EndLoc = Tok.getLocation();
212 // Consume final annot_pragma_openmp_end.
213 ConsumeToken();
214
215 StmtResult AssociatedStmt;
216 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000217 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000218 // The body is a block scope like in Lambdas and Blocks.
219 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000220 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000221 Actions.ActOnStartOfCompoundStmt();
222 // Parse statement
223 AssociatedStmt = ParseStatement();
224 Actions.ActOnFinishOfCompoundStmt();
225 if (!AssociatedStmt.isUsable()) {
226 Actions.ActOnCapturedRegionError();
227 CreateDirective = false;
228 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000229 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000230 CreateDirective = AssociatedStmt.isUsable();
231 }
232 }
233 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000234 Directive = Actions.ActOnOpenMPExecutableDirective(
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000235 DKind, DirName, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000236
237 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000239 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000240 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000241 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000242 case OMPD_unknown:
243 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000244 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000245 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000246 }
247 return Directive;
248}
249
Alexey Bataeva769e072013-03-22 06:34:35 +0000250/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000251/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000252///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000253/// simple-variable-list:
254/// '(' id-expression {, id-expression} ')'
255///
256bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
257 SmallVectorImpl<Expr *> &VarList,
258 bool AllowScopeSpecifier) {
259 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000260 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000261 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000262 if (T.expectAndConsume(diag::err_expected_lparen_after,
263 getOpenMPDirectiveName(Kind)))
264 return true;
265 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000266 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000267
268 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000269 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000270 CXXScopeSpec SS;
271 SourceLocation TemplateKWLoc;
272 UnqualifiedId Name;
273 // Read var name.
274 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000275 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000276
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000277 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
278 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000279 IsCorrect = false;
280 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000281 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000282 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
283 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000284 IsCorrect = false;
285 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000286 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000287 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
288 Tok.isNot(tok::annot_pragma_openmp_end)) {
289 IsCorrect = false;
290 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000291 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000292 Diag(PrevTok.getLocation(), diag::err_expected)
293 << tok::identifier
294 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000295 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000296 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000297 ExprResult Res =
298 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000299 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000300 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000301 }
302 // Consume ','.
303 if (Tok.is(tok::comma)) {
304 ConsumeToken();
305 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000306 }
307
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000308 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000309 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000310 IsCorrect = false;
311 }
312
313 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000314 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000315
316 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000317}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000318
319/// \brief Parsing of OpenMP clauses.
320///
321/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000322/// if-clause | final-clause | num_threads-clause | safelen-clause |
323/// default-clause | private-clause | firstprivate-clause | shared-clause
324/// | linear-clause | aligned-clause | collapse-clause |
325/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000326/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
327/// mergeable-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000328///
329OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
330 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000331 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000332 bool ErrorFound = false;
333 // Check if clause is allowed for the given directive.
334 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000335 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
336 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000337 ErrorFound = true;
338 }
339
340 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000341 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000342 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000343 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000344 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000345 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000346 // OpenMP [2.5, Restrictions]
347 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000348 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000349 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000350 // Only one safelen clause can appear on a simd directive.
351 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000352 // OpenMP [2.11.1, task Construct, Restrictions]
353 // At most one if clause can appear on the directive.
354 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000355 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000356 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
357 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000358 }
359
360 Clause = ParseOpenMPSingleExprClause(CKind);
361 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000363 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000364 // OpenMP [2.14.3.1, Restrictions]
365 // Only a single default clause may be specified on a parallel, task or
366 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000367 // OpenMP [2.5, parallel Construct, Restrictions]
368 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000369 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000370 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
371 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000372 }
373
374 Clause = ParseOpenMPSimpleClause(CKind);
375 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000376 case OMPC_schedule:
377 // OpenMP [2.7.1, Restrictions, p. 3]
378 // Only one schedule clause can appear on a loop directive.
379 if (!FirstClause) {
380 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
381 << getOpenMPClauseName(CKind);
382 }
383
384 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
385 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000386 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000387 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000388 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000389 case OMPC_mergeable:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000390 // OpenMP [2.7.1, Restrictions, p. 9]
391 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000392 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
393 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000394 if (!FirstClause) {
395 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
396 << getOpenMPClauseName(CKind);
397 }
398
399 Clause = ParseOpenMPClause(CKind);
400 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000401 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000402 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000403 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000405 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000406 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000407 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000408 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000409 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000410 Clause = ParseOpenMPVarListClause(CKind);
411 break;
412 case OMPC_unknown:
413 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000414 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000415 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000416 break;
417 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000418 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
419 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000420 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000421 break;
422 }
Craig Topper161e4db2014-05-21 06:02:52 +0000423 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000424}
425
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000426/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000427/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000428/// 'thread_limit'.
429///
430/// if-clause:
431/// 'if' '(' expression ')'
432///
Alexey Bataev3778b602014-07-17 07:32:53 +0000433/// final-clause:
434/// 'final' '(' expression ')'
435///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000436/// num_threads-clause:
437/// 'num_threads' '(' expression ')'
438///
439/// safelen-clause:
440/// 'safelen' '(' expression ')'
441///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000442/// collapse-clause:
443/// 'collapse' '(' expression ')'
444///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000445OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
446 SourceLocation Loc = ConsumeToken();
447
448 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
449 if (T.expectAndConsume(diag::err_expected_lparen_after,
450 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000451 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000452
453 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
454 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
455
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000456 // Parse ')'.
457 T.consumeClose();
458
459 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000460 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000461
Alexey Bataeva55ed262014-05-28 06:15:33 +0000462 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000463 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000464}
465
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000466/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000467///
468/// default-clause:
469/// 'default' '(' 'none' | 'shared' ')
470///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000471/// proc_bind-clause:
472/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
473///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000474OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
475 SourceLocation Loc = Tok.getLocation();
476 SourceLocation LOpen = ConsumeToken();
477 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000478 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000479 if (T.expectAndConsume(diag::err_expected_lparen_after,
480 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000481 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000482
Alexey Bataeva55ed262014-05-28 06:15:33 +0000483 unsigned Type = getOpenMPSimpleClauseType(
484 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000485 SourceLocation TypeLoc = Tok.getLocation();
486 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
487 Tok.isNot(tok::annot_pragma_openmp_end))
488 ConsumeAnyToken();
489
490 // Parse ')'.
491 T.consumeClose();
492
493 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
494 Tok.getLocation());
495}
496
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000497/// \brief Parsing of OpenMP clauses like 'ordered'.
498///
499/// ordered-clause:
500/// 'ordered'
501///
Alexey Bataev236070f2014-06-20 11:19:47 +0000502/// nowait-clause:
503/// 'nowait'
504///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000505/// untied-clause:
506/// 'untied'
507///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000508/// mergeable-clause:
509/// 'mergeable'
510///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000511OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
512 SourceLocation Loc = Tok.getLocation();
513 ConsumeAnyToken();
514
515 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
516}
517
518
Alexey Bataev56dafe82014-06-20 07:16:17 +0000519/// \brief Parsing of OpenMP clauses with single expressions and some additional
520/// argument like 'schedule' or 'dist_schedule'.
521///
522/// schedule-clause:
523/// 'schedule' '(' kind [',' expression ] ')'
524///
525OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
526 SourceLocation Loc = ConsumeToken();
527 SourceLocation CommaLoc;
528 // Parse '('.
529 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
530 if (T.expectAndConsume(diag::err_expected_lparen_after,
531 getOpenMPClauseName(Kind)))
532 return nullptr;
533
534 ExprResult Val;
535 unsigned Type = getOpenMPSimpleClauseType(
536 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
537 SourceLocation KLoc = Tok.getLocation();
538 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
539 Tok.isNot(tok::annot_pragma_openmp_end))
540 ConsumeAnyToken();
541
542 if (Kind == OMPC_schedule &&
543 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
544 Type == OMPC_SCHEDULE_guided) &&
545 Tok.is(tok::comma)) {
546 CommaLoc = ConsumeAnyToken();
547 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
548 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
549 if (Val.isInvalid())
550 return nullptr;
551 }
552
553 // Parse ')'.
554 T.consumeClose();
555
556 return Actions.ActOnOpenMPSingleExprWithArgClause(
557 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
558 T.getCloseLocation());
559}
560
Alexey Bataevc5e02582014-06-16 07:08:35 +0000561static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
562 UnqualifiedId &ReductionId) {
563 SourceLocation TemplateKWLoc;
564 if (ReductionIdScopeSpec.isEmpty()) {
565 auto OOK = OO_None;
566 switch (P.getCurToken().getKind()) {
567 case tok::plus:
568 OOK = OO_Plus;
569 break;
570 case tok::minus:
571 OOK = OO_Minus;
572 break;
573 case tok::star:
574 OOK = OO_Star;
575 break;
576 case tok::amp:
577 OOK = OO_Amp;
578 break;
579 case tok::pipe:
580 OOK = OO_Pipe;
581 break;
582 case tok::caret:
583 OOK = OO_Caret;
584 break;
585 case tok::ampamp:
586 OOK = OO_AmpAmp;
587 break;
588 case tok::pipepipe:
589 OOK = OO_PipePipe;
590 break;
591 default:
592 break;
593 }
594 if (OOK != OO_None) {
595 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000596 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000597 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
598 return false;
599 }
600 }
601 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
602 /*AllowDestructorName*/ false,
603 /*AllowConstructorName*/ false, ParsedType(),
604 TemplateKWLoc, ReductionId);
605}
606
Alexander Musman1bb328c2014-06-04 13:06:39 +0000607/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000608/// 'shared', 'copyin', or 'reduction'.
609///
610/// private-clause:
611/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000612/// firstprivate-clause:
613/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000614/// lastprivate-clause:
615/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000616/// shared-clause:
617/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000618/// linear-clause:
619/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000620/// aligned-clause:
621/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000622/// reduction-clause:
623/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000624///
625OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
626 SourceLocation Loc = Tok.getLocation();
627 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000628 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000629 // Optional scope specifier and unqualified id for reduction identifier.
630 CXXScopeSpec ReductionIdScopeSpec;
631 UnqualifiedId ReductionId;
632 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000633 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000634 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000635 if (T.expectAndConsume(diag::err_expected_lparen_after,
636 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000637 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000638
Alexey Bataevc5e02582014-06-16 07:08:35 +0000639 // Handle reduction-identifier for reduction clause.
640 if (Kind == OMPC_reduction) {
641 ColonProtectionRAIIObject ColonRAII(*this);
642 if (getLangOpts().CPlusPlus) {
643 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
644 }
645 InvalidReductionId =
646 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
647 if (InvalidReductionId) {
648 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
649 StopBeforeMatch);
650 }
651 if (Tok.is(tok::colon)) {
652 ColonLoc = ConsumeToken();
653 } else {
654 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
655 }
656 }
657
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000658 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000659 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000660 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000661 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000662 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000663 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000664 // Parse variable
665 ExprResult VarExpr = ParseAssignmentExpression();
666 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000667 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000668 } else {
669 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000670 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000671 }
672 // Skip ',' if any
673 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000674 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000675 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000676 else if (Tok.isNot(tok::r_paren) &&
677 Tok.isNot(tok::annot_pragma_openmp_end) &&
678 (!MayHaveTail || Tok.isNot(tok::colon)))
679 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
680 }
681
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000682 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000683 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000684 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
685 if (MustHaveTail) {
686 ColonLoc = Tok.getLocation();
687 ConsumeToken();
688 ExprResult Tail = ParseAssignmentExpression();
689 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000690 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000691 else
692 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
693 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000694 }
695
696 // Parse ')'.
697 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000698 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000699 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000700
Alexey Bataevc5e02582014-06-16 07:08:35 +0000701 return Actions.ActOnOpenMPVarListClause(
702 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
703 ReductionIdScopeSpec,
704 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
705 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000706}
707