blob: cf991784306e656fba095b03e5833f82dc6c6193 [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:
Alexey Bataev4acb8592014-07-07 13:01:15 +000094 case OMPD_parallel_for:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000095 case OMPD_parallel_sections:
Alexey Bataeva769e072013-03-22 06:34:35 +000096 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000097 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000098 break;
99 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000100 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +0000101 return DeclGroupPtrTy();
102}
103
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000104/// \brief Parsing of declarative or executable OpenMP directives.
105///
106/// threadprivate-directive:
107/// annot_pragma_openmp 'threadprivate' simple-variable-list
108/// annot_pragma_openmp_end
109///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000110/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000111/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musman80c22892014-07-17 08:54:58 +0000112/// 'section' | 'single' | 'master' | 'parallel for' |
Alexey Bataev2df347a2014-07-18 10:17:07 +0000113/// 'parallel sections' | 'task' | 'taskyield' | 'barrier' | 'taskwait'
114/// {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115///
Alexey Bataev68446b72014-07-18 07:47:19 +0000116StmtResult
117Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000118 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000119 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000120 SmallVector<Expr *, 5> Identifiers;
121 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000122 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000123 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000124 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000125 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000126 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000127 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128 // Name of critical directive.
129 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000130 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000131 bool HasAssociatedStatement = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000132
133 switch (DKind) {
134 case OMPD_threadprivate:
135 ConsumeToken();
136 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
137 // The last seen token is annot_pragma_openmp_end - need to check for
138 // extra tokens.
139 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
140 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000141 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000142 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000143 }
144 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000145 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000146 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
147 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000148 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000149 break;
Alexey Bataev68446b72014-07-18 07:47:19 +0000150 case OMPD_taskyield:
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000151 case OMPD_barrier:
Alexey Bataev2df347a2014-07-18 10:17:07 +0000152 case OMPD_taskwait:
Alexey Bataev68446b72014-07-18 07:47:19 +0000153 if (!StandAloneAllowed) {
154 Diag(Tok, diag::err_omp_immediate_directive)
155 << getOpenMPDirectiveName(DKind);
156 }
157 HasAssociatedStatement = false;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000158 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000159 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000160 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000161 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000162 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000163 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000164 case OMPD_master:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000165 case OMPD_parallel_for:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000166 case OMPD_parallel_sections:
167 case OMPD_task: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000168 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000169
Alexey Bataevf29276e2014-06-18 04:14:57 +0000170 if (isOpenMPLoopDirective(DKind))
171 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
172 if (isOpenMPSimdDirective(DKind))
173 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
174 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000175 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000177 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000178 OpenMPClauseKind CKind = Tok.isAnnotation()
179 ? OMPC_unknown
180 : getOpenMPClauseKind(PP.getSpelling(Tok));
181 OMPClause *Clause =
182 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000183 FirstClauses[CKind].setInt(true);
184 if (Clause) {
185 FirstClauses[CKind].setPointer(Clause);
186 Clauses.push_back(Clause);
187 }
188
189 // Skip ',' if any.
190 if (Tok.is(tok::comma))
191 ConsumeToken();
192 }
193 // End location of the directive.
194 EndLoc = Tok.getLocation();
195 // Consume final annot_pragma_openmp_end.
196 ConsumeToken();
197
198 StmtResult AssociatedStmt;
199 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000200 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000201 // The body is a block scope like in Lambdas and Blocks.
202 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000203 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000204 Actions.ActOnStartOfCompoundStmt();
205 // Parse statement
206 AssociatedStmt = ParseStatement();
207 Actions.ActOnFinishOfCompoundStmt();
208 if (!AssociatedStmt.isUsable()) {
209 Actions.ActOnCapturedRegionError();
210 CreateDirective = false;
211 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000212 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000213 CreateDirective = AssociatedStmt.isUsable();
214 }
215 }
216 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000217 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000218 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000219
220 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000221 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000222 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000223 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000224 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000225 case OMPD_unknown:
226 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000227 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000228 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000229 }
230 return Directive;
231}
232
Alexey Bataeva769e072013-03-22 06:34:35 +0000233/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000234/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000235///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000236/// simple-variable-list:
237/// '(' id-expression {, id-expression} ')'
238///
239bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
240 SmallVectorImpl<Expr *> &VarList,
241 bool AllowScopeSpecifier) {
242 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000243 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000244 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000245 if (T.expectAndConsume(diag::err_expected_lparen_after,
246 getOpenMPDirectiveName(Kind)))
247 return true;
248 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000249 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000250
251 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000252 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000253 CXXScopeSpec SS;
254 SourceLocation TemplateKWLoc;
255 UnqualifiedId Name;
256 // Read var name.
257 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000258 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000259
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000260 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
261 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000262 IsCorrect = false;
263 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000264 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000265 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
266 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000267 IsCorrect = false;
268 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000269 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000270 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
271 Tok.isNot(tok::annot_pragma_openmp_end)) {
272 IsCorrect = false;
273 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000274 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000275 Diag(PrevTok.getLocation(), diag::err_expected)
276 << tok::identifier
277 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000278 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000279 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000280 ExprResult Res =
281 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000282 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000283 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000284 }
285 // Consume ','.
286 if (Tok.is(tok::comma)) {
287 ConsumeToken();
288 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000289 }
290
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000291 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000292 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000293 IsCorrect = false;
294 }
295
296 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000298
299 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000300}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000301
302/// \brief Parsing of OpenMP clauses.
303///
304/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000305/// if-clause | final-clause | num_threads-clause | safelen-clause |
306/// default-clause | private-clause | firstprivate-clause | shared-clause
307/// | linear-clause | aligned-clause | collapse-clause |
308/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000309/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
310/// mergeable-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000311///
312OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
313 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000314 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000315 bool ErrorFound = false;
316 // Check if clause is allowed for the given directive.
317 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000318 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
319 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000320 ErrorFound = true;
321 }
322
323 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000324 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000325 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000326 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000327 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000328 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000329 // OpenMP [2.5, Restrictions]
330 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000331 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000332 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000333 // Only one safelen clause can appear on a simd directive.
334 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000335 // OpenMP [2.11.1, task Construct, Restrictions]
336 // At most one if clause can appear on the directive.
337 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000338 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000339 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
340 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000341 }
342
343 Clause = ParseOpenMPSingleExprClause(CKind);
344 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000345 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000346 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000347 // OpenMP [2.14.3.1, Restrictions]
348 // Only a single default clause may be specified on a parallel, task or
349 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000350 // OpenMP [2.5, parallel Construct, Restrictions]
351 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000352 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000353 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
354 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000355 }
356
357 Clause = ParseOpenMPSimpleClause(CKind);
358 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000359 case OMPC_schedule:
360 // OpenMP [2.7.1, Restrictions, p. 3]
361 // Only one schedule clause can appear on a loop directive.
362 if (!FirstClause) {
363 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
364 << getOpenMPClauseName(CKind);
365 }
366
367 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
368 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000369 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000370 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000371 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000372 case OMPC_mergeable:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000373 // OpenMP [2.7.1, Restrictions, p. 9]
374 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000375 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
376 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000377 if (!FirstClause) {
378 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
379 << getOpenMPClauseName(CKind);
380 }
381
382 Clause = ParseOpenMPClause(CKind);
383 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000384 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000385 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000386 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000388 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000389 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000390 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000391 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000392 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000393 Clause = ParseOpenMPVarListClause(CKind);
394 break;
395 case OMPC_unknown:
396 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000397 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000398 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000399 break;
400 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000401 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
402 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000403 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000404 break;
405 }
Craig Topper161e4db2014-05-21 06:02:52 +0000406 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000407}
408
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000409/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000410/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000411/// 'thread_limit'.
412///
413/// if-clause:
414/// 'if' '(' expression ')'
415///
Alexey Bataev3778b602014-07-17 07:32:53 +0000416/// final-clause:
417/// 'final' '(' expression ')'
418///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000419/// num_threads-clause:
420/// 'num_threads' '(' expression ')'
421///
422/// safelen-clause:
423/// 'safelen' '(' expression ')'
424///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000425/// collapse-clause:
426/// 'collapse' '(' expression ')'
427///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000428OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
429 SourceLocation Loc = ConsumeToken();
430
431 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
432 if (T.expectAndConsume(diag::err_expected_lparen_after,
433 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000434 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000435
436 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
437 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
438
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000439 // Parse ')'.
440 T.consumeClose();
441
442 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000443 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000444
Alexey Bataeva55ed262014-05-28 06:15:33 +0000445 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000446 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000447}
448
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000449/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000450///
451/// default-clause:
452/// 'default' '(' 'none' | 'shared' ')
453///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000454/// proc_bind-clause:
455/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
456///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000457OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
458 SourceLocation Loc = Tok.getLocation();
459 SourceLocation LOpen = ConsumeToken();
460 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000461 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000462 if (T.expectAndConsume(diag::err_expected_lparen_after,
463 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000464 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000465
Alexey Bataeva55ed262014-05-28 06:15:33 +0000466 unsigned Type = getOpenMPSimpleClauseType(
467 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000468 SourceLocation TypeLoc = Tok.getLocation();
469 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
470 Tok.isNot(tok::annot_pragma_openmp_end))
471 ConsumeAnyToken();
472
473 // Parse ')'.
474 T.consumeClose();
475
476 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
477 Tok.getLocation());
478}
479
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000480/// \brief Parsing of OpenMP clauses like 'ordered'.
481///
482/// ordered-clause:
483/// 'ordered'
484///
Alexey Bataev236070f2014-06-20 11:19:47 +0000485/// nowait-clause:
486/// 'nowait'
487///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000488/// untied-clause:
489/// 'untied'
490///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000491/// mergeable-clause:
492/// 'mergeable'
493///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000494OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
495 SourceLocation Loc = Tok.getLocation();
496 ConsumeAnyToken();
497
498 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
499}
500
501
Alexey Bataev56dafe82014-06-20 07:16:17 +0000502/// \brief Parsing of OpenMP clauses with single expressions and some additional
503/// argument like 'schedule' or 'dist_schedule'.
504///
505/// schedule-clause:
506/// 'schedule' '(' kind [',' expression ] ')'
507///
508OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
509 SourceLocation Loc = ConsumeToken();
510 SourceLocation CommaLoc;
511 // Parse '('.
512 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
513 if (T.expectAndConsume(diag::err_expected_lparen_after,
514 getOpenMPClauseName(Kind)))
515 return nullptr;
516
517 ExprResult Val;
518 unsigned Type = getOpenMPSimpleClauseType(
519 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
520 SourceLocation KLoc = Tok.getLocation();
521 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
522 Tok.isNot(tok::annot_pragma_openmp_end))
523 ConsumeAnyToken();
524
525 if (Kind == OMPC_schedule &&
526 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
527 Type == OMPC_SCHEDULE_guided) &&
528 Tok.is(tok::comma)) {
529 CommaLoc = ConsumeAnyToken();
530 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
531 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
532 if (Val.isInvalid())
533 return nullptr;
534 }
535
536 // Parse ')'.
537 T.consumeClose();
538
539 return Actions.ActOnOpenMPSingleExprWithArgClause(
540 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
541 T.getCloseLocation());
542}
543
Alexey Bataevc5e02582014-06-16 07:08:35 +0000544static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
545 UnqualifiedId &ReductionId) {
546 SourceLocation TemplateKWLoc;
547 if (ReductionIdScopeSpec.isEmpty()) {
548 auto OOK = OO_None;
549 switch (P.getCurToken().getKind()) {
550 case tok::plus:
551 OOK = OO_Plus;
552 break;
553 case tok::minus:
554 OOK = OO_Minus;
555 break;
556 case tok::star:
557 OOK = OO_Star;
558 break;
559 case tok::amp:
560 OOK = OO_Amp;
561 break;
562 case tok::pipe:
563 OOK = OO_Pipe;
564 break;
565 case tok::caret:
566 OOK = OO_Caret;
567 break;
568 case tok::ampamp:
569 OOK = OO_AmpAmp;
570 break;
571 case tok::pipepipe:
572 OOK = OO_PipePipe;
573 break;
574 default:
575 break;
576 }
577 if (OOK != OO_None) {
578 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000579 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000580 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
581 return false;
582 }
583 }
584 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
585 /*AllowDestructorName*/ false,
586 /*AllowConstructorName*/ false, ParsedType(),
587 TemplateKWLoc, ReductionId);
588}
589
Alexander Musman1bb328c2014-06-04 13:06:39 +0000590/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591/// 'shared', 'copyin', or 'reduction'.
592///
593/// private-clause:
594/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000595/// firstprivate-clause:
596/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000597/// lastprivate-clause:
598/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000599/// shared-clause:
600/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000601/// linear-clause:
602/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000603/// aligned-clause:
604/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000605/// reduction-clause:
606/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000607///
608OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
609 SourceLocation Loc = Tok.getLocation();
610 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000611 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000612 // Optional scope specifier and unqualified id for reduction identifier.
613 CXXScopeSpec ReductionIdScopeSpec;
614 UnqualifiedId ReductionId;
615 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000616 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000617 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000618 if (T.expectAndConsume(diag::err_expected_lparen_after,
619 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000620 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000621
Alexey Bataevc5e02582014-06-16 07:08:35 +0000622 // Handle reduction-identifier for reduction clause.
623 if (Kind == OMPC_reduction) {
624 ColonProtectionRAIIObject ColonRAII(*this);
625 if (getLangOpts().CPlusPlus) {
626 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
627 }
628 InvalidReductionId =
629 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
630 if (InvalidReductionId) {
631 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
632 StopBeforeMatch);
633 }
634 if (Tok.is(tok::colon)) {
635 ColonLoc = ConsumeToken();
636 } else {
637 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
638 }
639 }
640
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000641 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000642 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000643 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000644 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000645 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000646 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647 // Parse variable
648 ExprResult VarExpr = ParseAssignmentExpression();
649 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000650 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000651 } else {
652 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000653 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000654 }
655 // Skip ',' if any
656 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000657 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000658 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000659 else if (Tok.isNot(tok::r_paren) &&
660 Tok.isNot(tok::annot_pragma_openmp_end) &&
661 (!MayHaveTail || Tok.isNot(tok::colon)))
662 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
663 }
664
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000665 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000666 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000667 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
668 if (MustHaveTail) {
669 ColonLoc = Tok.getLocation();
670 ConsumeToken();
671 ExprResult Tail = ParseAssignmentExpression();
672 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000673 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000674 else
675 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
676 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000677 }
678
679 // Parse ')'.
680 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000681 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000682 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000683
Alexey Bataevc5e02582014-06-16 07:08:35 +0000684 return Actions.ActOnOpenMPVarListClause(
685 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
686 ReductionIdScopeSpec,
687 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
688 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000689}
690