blob: 172aef8cf3e206e59ae0edf82167db90262e11d4 [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 Bataevf29276e2014-06-18 04:14:57 +000086 case OMPD_for:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +000087 case OMPD_sections:
Alexey Bataev1e0498a2014-06-26 08:21:58 +000088 case OMPD_section:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +000089 case OMPD_single:
Alexander Musman80c22892014-07-17 08:54:58 +000090 case OMPD_master:
Alexey Bataev4acb8592014-07-07 13:01:15 +000091 case OMPD_parallel_for:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +000092 case OMPD_parallel_sections:
Alexey Bataeva769e072013-03-22 06:34:35 +000093 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataeva55ed262014-05-28 06:15:33 +000094 << getOpenMPDirectiveName(DKind);
Alexey Bataeva769e072013-03-22 06:34:35 +000095 break;
96 }
Alp Tokerd751fa72013-12-18 19:10:49 +000097 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataeva769e072013-03-22 06:34:35 +000098 return DeclGroupPtrTy();
99}
100
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000101/// \brief Parsing of declarative or executable OpenMP directives.
102///
103/// threadprivate-directive:
104/// annot_pragma_openmp 'threadprivate' simple-variable-list
105/// annot_pragma_openmp_end
106///
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000107/// executable-directive:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000108/// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
Alexander Musman80c22892014-07-17 08:54:58 +0000109/// 'section' | 'single' | 'master' | 'parallel for' |
110/// 'parallel sections' | 'task' {clause} annot_pragma_openmp_end
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000111///
112StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
113 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataevee6507d2013-11-18 08:17:37 +0000114 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000115 SmallVector<Expr *, 5> Identifiers;
116 SmallVector<OMPClause *, 5> Clauses;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000117 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
Alexey Bataeva55ed262014-05-28 06:15:33 +0000118 FirstClauses(OMPC_unknown + 1);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000119 unsigned ScopeFlags =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000120 Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000121 SourceLocation Loc = ConsumeToken(), EndLoc;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000122 auto DKind = ParseOpenMPDirectiveKind(*this);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123 // Name of critical directive.
124 DeclarationNameInfo DirName;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000125 StmtResult Directive = StmtError();
126
127 switch (DKind) {
128 case OMPD_threadprivate:
129 ConsumeToken();
130 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
131 // The last seen token is annot_pragma_openmp_end - need to check for
132 // extra tokens.
133 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
134 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000135 << getOpenMPDirectiveName(OMPD_threadprivate);
Alp Tokerd751fa72013-12-18 19:10:49 +0000136 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000137 }
138 DeclGroupPtrTy Res =
Alexey Bataeva55ed262014-05-28 06:15:33 +0000139 Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000140 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
141 }
Alp Tokerd751fa72013-12-18 19:10:49 +0000142 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000143 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000144 case OMPD_parallel:
Alexey Bataevf29276e2014-06-18 04:14:57 +0000145 case OMPD_simd:
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000146 case OMPD_for:
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000147 case OMPD_sections:
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000148 case OMPD_single:
Alexey Bataev4acb8592014-07-07 13:01:15 +0000149 case OMPD_section:
Alexander Musman80c22892014-07-17 08:54:58 +0000150 case OMPD_master:
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000151 case OMPD_parallel_for:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000152 case OMPD_parallel_sections:
153 case OMPD_task: {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000154 ConsumeToken();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155
Alexey Bataevf29276e2014-06-18 04:14:57 +0000156 if (isOpenMPLoopDirective(DKind))
157 ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
158 if (isOpenMPSimdDirective(DKind))
159 ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
160 ParseScope OMPDirectiveScope(this, ScopeFlags);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000161 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000163 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000164 OpenMPClauseKind CKind = Tok.isAnnotation()
165 ? OMPC_unknown
166 : getOpenMPClauseKind(PP.getSpelling(Tok));
167 OMPClause *Clause =
168 ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000169 FirstClauses[CKind].setInt(true);
170 if (Clause) {
171 FirstClauses[CKind].setPointer(Clause);
172 Clauses.push_back(Clause);
173 }
174
175 // Skip ',' if any.
176 if (Tok.is(tok::comma))
177 ConsumeToken();
178 }
179 // End location of the directive.
180 EndLoc = Tok.getLocation();
181 // Consume final annot_pragma_openmp_end.
182 ConsumeToken();
183
184 StmtResult AssociatedStmt;
185 bool CreateDirective = true;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000186 {
187 // The body is a block scope like in Lambdas and Blocks.
188 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataevbae9a792014-06-27 10:37:06 +0000189 Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000190 Actions.ActOnStartOfCompoundStmt();
191 // Parse statement
192 AssociatedStmt = ParseStatement();
193 Actions.ActOnFinishOfCompoundStmt();
194 if (!AssociatedStmt.isUsable()) {
195 Actions.ActOnCapturedRegionError();
196 CreateDirective = false;
197 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000198 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000199 CreateDirective = AssociatedStmt.isUsable();
200 }
201 }
202 if (CreateDirective)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000203 Directive = Actions.ActOnOpenMPExecutableDirective(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000204 DKind, Clauses, AssociatedStmt.get(), Loc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000205
206 // Exit scope.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000207 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000208 OMPDirectiveScope.Exit();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000209 break;
Alexey Bataeva55ed262014-05-28 06:15:33 +0000210 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000211 case OMPD_unknown:
212 Diag(Tok, diag::err_omp_unknown_directive);
Alp Tokerd751fa72013-12-18 19:10:49 +0000213 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000214 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000215 }
216 return Directive;
217}
218
Alexey Bataeva769e072013-03-22 06:34:35 +0000219/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000220/// directive.
Alexey Bataeva769e072013-03-22 06:34:35 +0000221///
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000222/// simple-variable-list:
223/// '(' id-expression {, id-expression} ')'
224///
225bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
226 SmallVectorImpl<Expr *> &VarList,
227 bool AllowScopeSpecifier) {
228 VarList.clear();
Alexey Bataeva769e072013-03-22 06:34:35 +0000229 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000230 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000231 if (T.expectAndConsume(diag::err_expected_lparen_after,
232 getOpenMPDirectiveName(Kind)))
233 return true;
234 bool IsCorrect = true;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000235 bool NoIdentIsFound = true;
Alexey Bataeva769e072013-03-22 06:34:35 +0000236
237 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000238 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000239 CXXScopeSpec SS;
240 SourceLocation TemplateKWLoc;
241 UnqualifiedId Name;
242 // Read var name.
243 Token PrevTok = Tok;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000244 NoIdentIsFound = false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000245
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000246 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
247 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000248 IsCorrect = false;
249 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000250 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000251 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
252 TemplateKWLoc, Name)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000253 IsCorrect = false;
254 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000255 StopBeforeMatch);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000256 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
257 Tok.isNot(tok::annot_pragma_openmp_end)) {
258 IsCorrect = false;
259 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000260 StopBeforeMatch);
Alp Tokerec543272013-12-24 09:48:30 +0000261 Diag(PrevTok.getLocation(), diag::err_expected)
262 << tok::identifier
263 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
Alexey Bataeva769e072013-03-22 06:34:35 +0000264 } else {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000265 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
Alexey Bataeva55ed262014-05-28 06:15:33 +0000266 ExprResult Res =
267 Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000268 if (Res.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000269 VarList.push_back(Res.get());
Alexey Bataeva769e072013-03-22 06:34:35 +0000270 }
271 // Consume ','.
272 if (Tok.is(tok::comma)) {
273 ConsumeToken();
274 }
Alexey Bataeva769e072013-03-22 06:34:35 +0000275 }
276
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000277 if (NoIdentIsFound) {
Alp Tokerec543272013-12-24 09:48:30 +0000278 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000279 IsCorrect = false;
280 }
281
282 // Parse ')'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000283 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000284
285 return !IsCorrect && VarList.empty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000286}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000287
288/// \brief Parsing of OpenMP clauses.
289///
290/// clause:
Alexey Bataev3778b602014-07-17 07:32:53 +0000291/// if-clause | final-clause | num_threads-clause | safelen-clause |
292/// default-clause | private-clause | firstprivate-clause | shared-clause
293/// | linear-clause | aligned-clause | collapse-clause |
294/// lastprivate-clause | reduction-clause | proc_bind-clause |
295/// schedule-clause | copyin-clause | copyprivate-clause
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000296///
297OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
298 OpenMPClauseKind CKind, bool FirstClause) {
Craig Topper161e4db2014-05-21 06:02:52 +0000299 OMPClause *Clause = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000300 bool ErrorFound = false;
301 // Check if clause is allowed for the given directive.
302 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000303 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
304 << getOpenMPDirectiveName(DKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000305 ErrorFound = true;
306 }
307
308 switch (CKind) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000309 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +0000310 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +0000311 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000312 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000313 case OMPC_collapse:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000314 // OpenMP [2.5, Restrictions]
315 // At most one if clause can appear on the directive.
Alexey Bataev568a8332014-03-06 06:15:19 +0000316 // At most one num_threads clause can appear on the directive.
Alexey Bataev62c87d22014-03-21 04:51:18 +0000317 // OpenMP [2.8.1, simd construct, Restrictions]
Alexander Musman8bd31e62014-05-27 15:12:19 +0000318 // Only one safelen clause can appear on a simd directive.
319 // Only one collapse clause can appear on a simd directive.
Alexey Bataev3778b602014-07-17 07:32:53 +0000320 // OpenMP [2.11.1, task Construct, Restrictions]
321 // At most one if clause can appear on the directive.
322 // At most one final clause can appear on the directive.
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000323 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000324 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
325 << getOpenMPClauseName(CKind);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000326 }
327
328 Clause = ParseOpenMPSingleExprClause(CKind);
329 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000330 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000331 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000332 // OpenMP [2.14.3.1, Restrictions]
333 // Only a single default clause may be specified on a parallel, task or
334 // teams directive.
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000335 // OpenMP [2.5, parallel Construct, Restrictions]
336 // At most one proc_bind clause can appear on the directive.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000337 if (!FirstClause) {
Alexey Bataeva55ed262014-05-28 06:15:33 +0000338 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
339 << getOpenMPClauseName(CKind);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000340 }
341
342 Clause = ParseOpenMPSimpleClause(CKind);
343 break;
Alexey Bataev56dafe82014-06-20 07:16:17 +0000344 case OMPC_schedule:
345 // OpenMP [2.7.1, Restrictions, p. 3]
346 // Only one schedule clause can appear on a loop directive.
347 if (!FirstClause) {
348 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
349 << getOpenMPClauseName(CKind);
350 }
351
352 Clause = ParseOpenMPSingleExprWithArgClause(CKind);
353 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000354 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +0000355 case OMPC_nowait:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000356 // OpenMP [2.7.1, Restrictions, p. 9]
357 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000358 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
359 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000360 if (!FirstClause) {
361 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
362 << getOpenMPClauseName(CKind);
363 }
364
365 Clause = ParseOpenMPClause(CKind);
366 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000367 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000368 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000369 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000370 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000371 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000372 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000373 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000374 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000375 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000376 Clause = ParseOpenMPVarListClause(CKind);
377 break;
378 case OMPC_unknown:
379 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000380 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000381 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000382 break;
383 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000384 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
385 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000386 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000387 break;
388 }
Craig Topper161e4db2014-05-21 06:02:52 +0000389 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000390}
391
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000392/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000393/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000394/// 'thread_limit'.
395///
396/// if-clause:
397/// 'if' '(' expression ')'
398///
Alexey Bataev3778b602014-07-17 07:32:53 +0000399/// final-clause:
400/// 'final' '(' expression ')'
401///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000402/// num_threads-clause:
403/// 'num_threads' '(' expression ')'
404///
405/// safelen-clause:
406/// 'safelen' '(' expression ')'
407///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000408/// collapse-clause:
409/// 'collapse' '(' expression ')'
410///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000411OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
412 SourceLocation Loc = ConsumeToken();
413
414 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
415 if (T.expectAndConsume(diag::err_expected_lparen_after,
416 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000417 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000418
419 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
420 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
421
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000422 // Parse ')'.
423 T.consumeClose();
424
425 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000426 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000427
Alexey Bataeva55ed262014-05-28 06:15:33 +0000428 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000429 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000430}
431
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000432/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000433///
434/// default-clause:
435/// 'default' '(' 'none' | 'shared' ')
436///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000437/// proc_bind-clause:
438/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
439///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000440OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
441 SourceLocation Loc = Tok.getLocation();
442 SourceLocation LOpen = ConsumeToken();
443 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000444 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000445 if (T.expectAndConsume(diag::err_expected_lparen_after,
446 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000447 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000448
Alexey Bataeva55ed262014-05-28 06:15:33 +0000449 unsigned Type = getOpenMPSimpleClauseType(
450 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000451 SourceLocation TypeLoc = Tok.getLocation();
452 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
453 Tok.isNot(tok::annot_pragma_openmp_end))
454 ConsumeAnyToken();
455
456 // Parse ')'.
457 T.consumeClose();
458
459 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
460 Tok.getLocation());
461}
462
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000463/// \brief Parsing of OpenMP clauses like 'ordered'.
464///
465/// ordered-clause:
466/// 'ordered'
467///
Alexey Bataev236070f2014-06-20 11:19:47 +0000468/// nowait-clause:
469/// 'nowait'
470///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000471OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
472 SourceLocation Loc = Tok.getLocation();
473 ConsumeAnyToken();
474
475 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
476}
477
478
Alexey Bataev56dafe82014-06-20 07:16:17 +0000479/// \brief Parsing of OpenMP clauses with single expressions and some additional
480/// argument like 'schedule' or 'dist_schedule'.
481///
482/// schedule-clause:
483/// 'schedule' '(' kind [',' expression ] ')'
484///
485OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
486 SourceLocation Loc = ConsumeToken();
487 SourceLocation CommaLoc;
488 // Parse '('.
489 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
490 if (T.expectAndConsume(diag::err_expected_lparen_after,
491 getOpenMPClauseName(Kind)))
492 return nullptr;
493
494 ExprResult Val;
495 unsigned Type = getOpenMPSimpleClauseType(
496 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
497 SourceLocation KLoc = Tok.getLocation();
498 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
499 Tok.isNot(tok::annot_pragma_openmp_end))
500 ConsumeAnyToken();
501
502 if (Kind == OMPC_schedule &&
503 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
504 Type == OMPC_SCHEDULE_guided) &&
505 Tok.is(tok::comma)) {
506 CommaLoc = ConsumeAnyToken();
507 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
508 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
509 if (Val.isInvalid())
510 return nullptr;
511 }
512
513 // Parse ')'.
514 T.consumeClose();
515
516 return Actions.ActOnOpenMPSingleExprWithArgClause(
517 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
518 T.getCloseLocation());
519}
520
Alexey Bataevc5e02582014-06-16 07:08:35 +0000521static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
522 UnqualifiedId &ReductionId) {
523 SourceLocation TemplateKWLoc;
524 if (ReductionIdScopeSpec.isEmpty()) {
525 auto OOK = OO_None;
526 switch (P.getCurToken().getKind()) {
527 case tok::plus:
528 OOK = OO_Plus;
529 break;
530 case tok::minus:
531 OOK = OO_Minus;
532 break;
533 case tok::star:
534 OOK = OO_Star;
535 break;
536 case tok::amp:
537 OOK = OO_Amp;
538 break;
539 case tok::pipe:
540 OOK = OO_Pipe;
541 break;
542 case tok::caret:
543 OOK = OO_Caret;
544 break;
545 case tok::ampamp:
546 OOK = OO_AmpAmp;
547 break;
548 case tok::pipepipe:
549 OOK = OO_PipePipe;
550 break;
551 default:
552 break;
553 }
554 if (OOK != OO_None) {
555 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000556 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000557 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
558 return false;
559 }
560 }
561 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
562 /*AllowDestructorName*/ false,
563 /*AllowConstructorName*/ false, ParsedType(),
564 TemplateKWLoc, ReductionId);
565}
566
Alexander Musman1bb328c2014-06-04 13:06:39 +0000567/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000568/// 'shared', 'copyin', or 'reduction'.
569///
570/// private-clause:
571/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000572/// firstprivate-clause:
573/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000574/// lastprivate-clause:
575/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000576/// shared-clause:
577/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000578/// linear-clause:
579/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000580/// aligned-clause:
581/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000582/// reduction-clause:
583/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000584///
585OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
586 SourceLocation Loc = Tok.getLocation();
587 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000588 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000589 // Optional scope specifier and unqualified id for reduction identifier.
590 CXXScopeSpec ReductionIdScopeSpec;
591 UnqualifiedId ReductionId;
592 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000593 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000594 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000595 if (T.expectAndConsume(diag::err_expected_lparen_after,
596 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000597 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000598
Alexey Bataevc5e02582014-06-16 07:08:35 +0000599 // Handle reduction-identifier for reduction clause.
600 if (Kind == OMPC_reduction) {
601 ColonProtectionRAIIObject ColonRAII(*this);
602 if (getLangOpts().CPlusPlus) {
603 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
604 }
605 InvalidReductionId =
606 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
607 if (InvalidReductionId) {
608 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
609 StopBeforeMatch);
610 }
611 if (Tok.is(tok::colon)) {
612 ColonLoc = ConsumeToken();
613 } else {
614 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
615 }
616 }
617
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000618 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000619 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000620 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000621 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000622 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000623 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000624 // Parse variable
625 ExprResult VarExpr = ParseAssignmentExpression();
626 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000627 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000628 } else {
629 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000630 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000631 }
632 // Skip ',' if any
633 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000634 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000635 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000636 else if (Tok.isNot(tok::r_paren) &&
637 Tok.isNot(tok::annot_pragma_openmp_end) &&
638 (!MayHaveTail || Tok.isNot(tok::colon)))
639 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
640 }
641
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000642 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000643 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000644 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
645 if (MustHaveTail) {
646 ColonLoc = Tok.getLocation();
647 ConsumeToken();
648 ExprResult Tail = ParseAssignmentExpression();
649 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000650 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000651 else
652 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
653 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000654 }
655
656 // Parse ')'.
657 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000658 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000659 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000660
Alexey Bataevc5e02582014-06-16 07:08:35 +0000661 return Actions.ActOnOpenMPVarListClause(
662 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
663 ReductionIdScopeSpec,
664 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
665 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000666}
667