blob: 06fe1ad464d3f95ffec1f3d1646245136180c213 [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 Bataevf29276e2014-06-18 04:14:57 +000087 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000088 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000089 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000090 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +000091 case OMPD_master:
Alexey Bataev4acb8592014-07-07 13:01:15 +000092 case OMPD_parallel_for:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000093 case OMPD_parallel_sections:
Alexey Bataeva769e072013-03-22 06:34:35 +000094 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000095 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000096 break;
97 }
Alp Tokerd751fa72013-12-18 19:10:49 +000098 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000099 return DeclGroupPtrTy();
100}
101
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000102/// \brief Parsing of declarative or executable OpenMP directives.
103///
104/// threadprivate-directive:
105/// annot_pragma_openmp 'threadprivate' simple-variable-list
106/// annot_pragma_openmp_end
107///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000108/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000109/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musman80c22892014-07-17 08:54:58 +0000110/// 'section' | 'single' | 'master' | 'parallel for' |
Alexey Bataev68446b72014-07-18 07:47:19 +0000111/// 'parallel sections' | 'task' | 'taskyield' {clause}
112/// annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000113///
Alexey Bataev68446b72014-07-18 07:47:19 +0000114StmtResult
115Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000116 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000117 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000118 SmallVector<Expr *, 5> Identifiers;
119 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000120 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000121 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000122 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000123 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000124 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000125 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 // Name of critical directive.
127 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000128 StmtResult Directive = StmtError();
Alexey Bataev68446b72014-07-18 07:47:19 +0000129 bool HasAssociatedStatement = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000130
131 switch (DKind) {
132 case OMPD_threadprivate:
133 ConsumeToken();
134 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
135 // The last seen token is annot_pragma_openmp_end - need to check for
136 // extra tokens.
137 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
138 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000139 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000140 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000141 }
142 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000143 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000144 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
145 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000146 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000147 break;
Alexey Bataev68446b72014-07-18 07:47:19 +0000148 case OMPD_taskyield:
149 if (!StandAloneAllowed) {
150 Diag(Tok, diag::err_omp_immediate_directive)
151 << getOpenMPDirectiveName(DKind);
152 }
153 HasAssociatedStatement = false;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000154 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000155 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000156 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000157 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000158 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000159 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000160 case OMPD_master:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000161 case OMPD_parallel_for:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000162 case OMPD_parallel_sections:
163 case OMPD_task: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000164 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165
Alexey Bataevf29276e2014-06-18 04:14:57 +0000166 if (isOpenMPLoopDirective(DKind))
167 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
168 if (isOpenMPSimdDirective(DKind))
169 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
170 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000171 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000172
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000173 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000174 OpenMPClauseKind CKind = Tok.isAnnotation()
175 ? OMPC_unknown
176 : getOpenMPClauseKind(PP.getSpelling(Tok));
177 OMPClause *Clause =
178 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000179 FirstClauses[CKind].setInt(true);
180 if (Clause) {
181 FirstClauses[CKind].setPointer(Clause);
182 Clauses.push_back(Clause);
183 }
184
185 // Skip ',' if any.
186 if (Tok.is(tok::comma))
187 ConsumeToken();
188 }
189 // End location of the directive.
190 EndLoc = Tok.getLocation();
191 // Consume final annot_pragma_openmp_end.
192 ConsumeToken();
193
194 StmtResult AssociatedStmt;
195 bool CreateDirective = true;
Alexey Bataev68446b72014-07-18 07:47:19 +0000196 if (HasAssociatedStatement) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000197 // The body is a block scope like in Lambdas and Blocks.
198 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000199 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000200 Actions.ActOnStartOfCompoundStmt();
201 // Parse statement
202 AssociatedStmt = ParseStatement();
203 Actions.ActOnFinishOfCompoundStmt();
204 if (!AssociatedStmt.isUsable()) {
205 Actions.ActOnCapturedRegionError();
206 CreateDirective = false;
207 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000208 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000209 CreateDirective = AssociatedStmt.isUsable();
210 }
211 }
212 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000213 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000214 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000215
216 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000217 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000218 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000219 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000220 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000221 case OMPD_unknown:
222 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000223 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000224 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000225 }
226 return Directive;
227}
228
Alexey Bataeva769e072013-03-22 06:34:35 +0000229/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000230/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000231///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000232/// simple-variable-list:
233/// '(' id-expression {, id-expression} ')'
234///
235bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
236 SmallVectorImpl<Expr *> &VarList,
237 bool AllowScopeSpecifier) {
238 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000239 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000240 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000241 if (T.expectAndConsume(diag::err_expected_lparen_after,
242 getOpenMPDirectiveName(Kind)))
243 return true;
244 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000245 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000246
247 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000248 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000249 CXXScopeSpec SS;
250 SourceLocation TemplateKWLoc;
251 UnqualifiedId Name;
252 // Read var name.
253 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000254 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000255
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000256 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
257 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000258 IsCorrect = false;
259 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000260 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000261 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
262 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000263 IsCorrect = false;
264 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000265 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000266 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
267 Tok.isNot(tok::annot_pragma_openmp_end)) {
268 IsCorrect = false;
269 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000270 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000271 Diag(PrevTok.getLocation(), diag::err_expected)
272 << tok::identifier
273 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000274 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000275 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000276 ExprResult Res =
277 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000278 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000279 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000280 }
281 // Consume ','.
282 if (Tok.is(tok::comma)) {
283 ConsumeToken();
284 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000285 }
286
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000287 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000288 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000289 IsCorrect = false;
290 }
291
292 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000293 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000294
295 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000296}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000297
298/// \brief Parsing of OpenMP clauses.
299///
300/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000301/// if-clause | final-clause | num_threads-clause | safelen-clause |
302/// default-clause | private-clause | firstprivate-clause | shared-clause
303/// | linear-clause | aligned-clause | collapse-clause |
304/// lastprivate-clause | reduction-clause | proc_bind-clause |
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000305/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
306/// mergeable-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000307///
308OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
309 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000310 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000311 bool ErrorFound = false;
312 // Check if clause is allowed for the given directive.
313 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000314 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
315 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000316 ErrorFound = true;
317 }
318
319 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000320 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000321 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000322 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000323 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000324 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000325 // OpenMP [2.5, Restrictions]
326 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000327 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000328 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000329 // Only one safelen clause can appear on a simd directive.
330 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000331 // OpenMP [2.11.1, task Construct, Restrictions]
332 // At most one if clause can appear on the directive.
333 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000334 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000335 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
336 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000337 }
338
339 Clause = ParseOpenMPSingleExprClause(CKind);
340 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000341 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000342 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000343 // OpenMP [2.14.3.1, Restrictions]
344 // Only a single default clause may be specified on a parallel, task or
345 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000346 // OpenMP [2.5, parallel Construct, Restrictions]
347 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000348 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000349 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
350 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000351 }
352
353 Clause = ParseOpenMPSimpleClause(CKind);
354 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000355 case OMPC_schedule:
356 // OpenMP [2.7.1, Restrictions, p. 3]
357 // Only one schedule clause can appear on a loop directive.
358 if (!FirstClause) {
359 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
360 << getOpenMPClauseName(CKind);
361 }
362
363 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
364 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000365 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000366 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000367 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000368 case OMPC_mergeable:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000369 // OpenMP [2.7.1, Restrictions, p. 9]
370 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000371 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
372 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000373 if (!FirstClause) {
374 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
375 << getOpenMPClauseName(CKind);
376 }
377
378 Clause = ParseOpenMPClause(CKind);
379 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000380 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000381 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000382 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000384 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000385 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000386 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000387 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000388 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000389 Clause = ParseOpenMPVarListClause(CKind);
390 break;
391 case OMPC_unknown:
392 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000393 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000394 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000395 break;
396 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000397 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
398 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000399 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000400 break;
401 }
Craig Topper161e4db2014-05-21 06:02:52 +0000402 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000403}
404
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000405/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000406/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000407/// 'thread_limit'.
408///
409/// if-clause:
410/// 'if' '(' expression ')'
411///
Alexey Bataev3778b602014-07-17 07:32:53 +0000412/// final-clause:
413/// 'final' '(' expression ')'
414///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000415/// num_threads-clause:
416/// 'num_threads' '(' expression ')'
417///
418/// safelen-clause:
419/// 'safelen' '(' expression ')'
420///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000421/// collapse-clause:
422/// 'collapse' '(' expression ')'
423///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000424OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
425 SourceLocation Loc = ConsumeToken();
426
427 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
428 if (T.expectAndConsume(diag::err_expected_lparen_after,
429 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000430 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000431
432 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
433 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
434
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000435 // Parse ')'.
436 T.consumeClose();
437
438 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000439 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000440
Alexey Bataeva55ed262014-05-28 06:15:33 +0000441 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000442 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000443}
444
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000445/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446///
447/// default-clause:
448/// 'default' '(' 'none' | 'shared' ')
449///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000450/// proc_bind-clause:
451/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
452///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000453OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
454 SourceLocation Loc = Tok.getLocation();
455 SourceLocation LOpen = ConsumeToken();
456 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000457 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000458 if (T.expectAndConsume(diag::err_expected_lparen_after,
459 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000460 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000461
Alexey Bataeva55ed262014-05-28 06:15:33 +0000462 unsigned Type = getOpenMPSimpleClauseType(
463 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000464 SourceLocation TypeLoc = Tok.getLocation();
465 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
466 Tok.isNot(tok::annot_pragma_openmp_end))
467 ConsumeAnyToken();
468
469 // Parse ')'.
470 T.consumeClose();
471
472 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
473 Tok.getLocation());
474}
475
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000476/// \brief Parsing of OpenMP clauses like 'ordered'.
477///
478/// ordered-clause:
479/// 'ordered'
480///
Alexey Bataev236070f2014-06-20 11:19:47 +0000481/// nowait-clause:
482/// 'nowait'
483///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000484/// untied-clause:
485/// 'untied'
486///
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000487/// mergeable-clause:
488/// 'mergeable'
489///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000490OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
491 SourceLocation Loc = Tok.getLocation();
492 ConsumeAnyToken();
493
494 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
495}
496
497
Alexey Bataev56dafe82014-06-20 07:16:17 +0000498/// \brief Parsing of OpenMP clauses with single expressions and some additional
499/// argument like 'schedule' or 'dist_schedule'.
500///
501/// schedule-clause:
502/// 'schedule' '(' kind [',' expression ] ')'
503///
504OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
505 SourceLocation Loc = ConsumeToken();
506 SourceLocation CommaLoc;
507 // Parse '('.
508 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
509 if (T.expectAndConsume(diag::err_expected_lparen_after,
510 getOpenMPClauseName(Kind)))
511 return nullptr;
512
513 ExprResult Val;
514 unsigned Type = getOpenMPSimpleClauseType(
515 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
516 SourceLocation KLoc = Tok.getLocation();
517 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
518 Tok.isNot(tok::annot_pragma_openmp_end))
519 ConsumeAnyToken();
520
521 if (Kind == OMPC_schedule &&
522 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
523 Type == OMPC_SCHEDULE_guided) &&
524 Tok.is(tok::comma)) {
525 CommaLoc = ConsumeAnyToken();
526 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
527 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
528 if (Val.isInvalid())
529 return nullptr;
530 }
531
532 // Parse ')'.
533 T.consumeClose();
534
535 return Actions.ActOnOpenMPSingleExprWithArgClause(
536 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
537 T.getCloseLocation());
538}
539
Alexey Bataevc5e02582014-06-16 07:08:35 +0000540static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
541 UnqualifiedId &ReductionId) {
542 SourceLocation TemplateKWLoc;
543 if (ReductionIdScopeSpec.isEmpty()) {
544 auto OOK = OO_None;
545 switch (P.getCurToken().getKind()) {
546 case tok::plus:
547 OOK = OO_Plus;
548 break;
549 case tok::minus:
550 OOK = OO_Minus;
551 break;
552 case tok::star:
553 OOK = OO_Star;
554 break;
555 case tok::amp:
556 OOK = OO_Amp;
557 break;
558 case tok::pipe:
559 OOK = OO_Pipe;
560 break;
561 case tok::caret:
562 OOK = OO_Caret;
563 break;
564 case tok::ampamp:
565 OOK = OO_AmpAmp;
566 break;
567 case tok::pipepipe:
568 OOK = OO_PipePipe;
569 break;
570 default:
571 break;
572 }
573 if (OOK != OO_None) {
574 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000575 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000576 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
577 return false;
578 }
579 }
580 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
581 /*AllowDestructorName*/ false,
582 /*AllowConstructorName*/ false, ParsedType(),
583 TemplateKWLoc, ReductionId);
584}
585
Alexander Musman1bb328c2014-06-04 13:06:39 +0000586/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000587/// 'shared', 'copyin', or 'reduction'.
588///
589/// private-clause:
590/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000591/// firstprivate-clause:
592/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000593/// lastprivate-clause:
594/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000595/// shared-clause:
596/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000597/// linear-clause:
598/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000599/// aligned-clause:
600/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000601/// reduction-clause:
602/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000603///
604OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
605 SourceLocation Loc = Tok.getLocation();
606 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000607 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000608 // Optional scope specifier and unqualified id for reduction identifier.
609 CXXScopeSpec ReductionIdScopeSpec;
610 UnqualifiedId ReductionId;
611 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000612 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000613 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000614 if (T.expectAndConsume(diag::err_expected_lparen_after,
615 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000616 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000617
Alexey Bataevc5e02582014-06-16 07:08:35 +0000618 // Handle reduction-identifier for reduction clause.
619 if (Kind == OMPC_reduction) {
620 ColonProtectionRAIIObject ColonRAII(*this);
621 if (getLangOpts().CPlusPlus) {
622 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
623 }
624 InvalidReductionId =
625 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
626 if (InvalidReductionId) {
627 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
628 StopBeforeMatch);
629 }
630 if (Tok.is(tok::colon)) {
631 ColonLoc = ConsumeToken();
632 } else {
633 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
634 }
635 }
636
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000637 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000638 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000639 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000640 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000641 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000642 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000643 // Parse variable
644 ExprResult VarExpr = ParseAssignmentExpression();
645 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000646 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647 } else {
648 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000649 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000650 }
651 // Skip ',' if any
652 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000653 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000654 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000655 else if (Tok.isNot(tok::r_paren) &&
656 Tok.isNot(tok::annot_pragma_openmp_end) &&
657 (!MayHaveTail || Tok.isNot(tok::colon)))
658 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
659 }
660
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000661 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000662 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000663 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
664 if (MustHaveTail) {
665 ColonLoc = Tok.getLocation();
666 ConsumeToken();
667 ExprResult Tail = ParseAssignmentExpression();
668 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000669 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000670 else
671 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
672 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000673 }
674
675 // Parse ')'.
676 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000677 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000678 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000679
Alexey Bataevc5e02582014-06-16 07:08:35 +0000680 return Actions.ActOnOpenMPVarListClause(
681 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
682 ReductionIdScopeSpec,
683 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
684 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000685}
686