blob: 9f1fa0af154c647127e8345b18d72653c7df5683 [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 |
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000295/// schedule-clause | copyin-clause | copyprivate-clause | untied-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 Bataev7aea99a2014-07-17 12:19:31 +0000356 case OMPC_untied:
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000357 // OpenMP [2.7.1, Restrictions, p. 9]
358 // Only one ordered clause can appear on a loop directive.
Alexey Bataev236070f2014-06-20 11:19:47 +0000359 // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
360 // Only one nowait clause can appear on a for directive.
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000361 if (!FirstClause) {
362 Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind)
363 << getOpenMPClauseName(CKind);
364 }
365
366 Clause = ParseOpenMPClause(CKind);
367 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000368 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000369 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +0000370 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000371 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +0000372 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +0000373 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000374 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000375 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +0000376 case OMPC_copyprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000377 Clause = ParseOpenMPVarListClause(CKind);
378 break;
379 case OMPC_unknown:
380 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
Alexey Bataeva55ed262014-05-28 06:15:33 +0000381 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000382 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000383 break;
384 case OMPC_threadprivate:
Alexey Bataeva55ed262014-05-28 06:15:33 +0000385 Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
386 << getOpenMPDirectiveName(DKind);
Alp Tokerd751fa72013-12-18 19:10:49 +0000387 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000388 break;
389 }
Craig Topper161e4db2014-05-21 06:02:52 +0000390 return ErrorFound ? nullptr : Clause;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000391}
392
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000393/// \brief Parsing of OpenMP clauses with single expressions like 'if',
Alexey Bataev3778b602014-07-17 07:32:53 +0000394/// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000395/// 'thread_limit'.
396///
397/// if-clause:
398/// 'if' '(' expression ')'
399///
Alexey Bataev3778b602014-07-17 07:32:53 +0000400/// final-clause:
401/// 'final' '(' expression ')'
402///
Alexey Bataev62c87d22014-03-21 04:51:18 +0000403/// num_threads-clause:
404/// 'num_threads' '(' expression ')'
405///
406/// safelen-clause:
407/// 'safelen' '(' expression ')'
408///
Alexander Musman8bd31e62014-05-27 15:12:19 +0000409/// collapse-clause:
410/// 'collapse' '(' expression ')'
411///
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000412OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
413 SourceLocation Loc = ConsumeToken();
414
415 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
416 if (T.expectAndConsume(diag::err_expected_lparen_after,
417 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000418 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000419
420 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
421 ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
422
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000423 // Parse ')'.
424 T.consumeClose();
425
426 if (Val.isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000427 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000428
Alexey Bataeva55ed262014-05-28 06:15:33 +0000429 return Actions.ActOnOpenMPSingleExprClause(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000430 Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation());
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000431}
432
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000433/// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000434///
435/// default-clause:
436/// 'default' '(' 'none' | 'shared' ')
437///
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000438/// proc_bind-clause:
439/// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
440///
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000441OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
442 SourceLocation Loc = Tok.getLocation();
443 SourceLocation LOpen = ConsumeToken();
444 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000445 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000446 if (T.expectAndConsume(diag::err_expected_lparen_after,
447 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000448 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000449
Alexey Bataeva55ed262014-05-28 06:15:33 +0000450 unsigned Type = getOpenMPSimpleClauseType(
451 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000452 SourceLocation TypeLoc = Tok.getLocation();
453 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
454 Tok.isNot(tok::annot_pragma_openmp_end))
455 ConsumeAnyToken();
456
457 // Parse ')'.
458 T.consumeClose();
459
460 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
461 Tok.getLocation());
462}
463
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000464/// \brief Parsing of OpenMP clauses like 'ordered'.
465///
466/// ordered-clause:
467/// 'ordered'
468///
Alexey Bataev236070f2014-06-20 11:19:47 +0000469/// nowait-clause:
470/// 'nowait'
471///
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000472/// untied-clause:
473/// 'untied'
474///
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000475OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
476 SourceLocation Loc = Tok.getLocation();
477 ConsumeAnyToken();
478
479 return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
480}
481
482
Alexey Bataev56dafe82014-06-20 07:16:17 +0000483/// \brief Parsing of OpenMP clauses with single expressions and some additional
484/// argument like 'schedule' or 'dist_schedule'.
485///
486/// schedule-clause:
487/// 'schedule' '(' kind [',' expression ] ')'
488///
489OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) {
490 SourceLocation Loc = ConsumeToken();
491 SourceLocation CommaLoc;
492 // Parse '('.
493 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
494 if (T.expectAndConsume(diag::err_expected_lparen_after,
495 getOpenMPClauseName(Kind)))
496 return nullptr;
497
498 ExprResult Val;
499 unsigned Type = getOpenMPSimpleClauseType(
500 Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
501 SourceLocation KLoc = Tok.getLocation();
502 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
503 Tok.isNot(tok::annot_pragma_openmp_end))
504 ConsumeAnyToken();
505
506 if (Kind == OMPC_schedule &&
507 (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic ||
508 Type == OMPC_SCHEDULE_guided) &&
509 Tok.is(tok::comma)) {
510 CommaLoc = ConsumeAnyToken();
511 ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
512 Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
513 if (Val.isInvalid())
514 return nullptr;
515 }
516
517 // Parse ')'.
518 T.consumeClose();
519
520 return Actions.ActOnOpenMPSingleExprWithArgClause(
521 Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc,
522 T.getCloseLocation());
523}
524
Alexey Bataevc5e02582014-06-16 07:08:35 +0000525static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
526 UnqualifiedId &ReductionId) {
527 SourceLocation TemplateKWLoc;
528 if (ReductionIdScopeSpec.isEmpty()) {
529 auto OOK = OO_None;
530 switch (P.getCurToken().getKind()) {
531 case tok::plus:
532 OOK = OO_Plus;
533 break;
534 case tok::minus:
535 OOK = OO_Minus;
536 break;
537 case tok::star:
538 OOK = OO_Star;
539 break;
540 case tok::amp:
541 OOK = OO_Amp;
542 break;
543 case tok::pipe:
544 OOK = OO_Pipe;
545 break;
546 case tok::caret:
547 OOK = OO_Caret;
548 break;
549 case tok::ampamp:
550 OOK = OO_AmpAmp;
551 break;
552 case tok::pipepipe:
553 OOK = OO_PipePipe;
554 break;
555 default:
556 break;
557 }
558 if (OOK != OO_None) {
559 SourceLocation OpLoc = P.ConsumeToken();
Alexey Bataev23b69422014-06-18 07:08:49 +0000560 SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
Alexey Bataevc5e02582014-06-16 07:08:35 +0000561 ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
562 return false;
563 }
564 }
565 return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
566 /*AllowDestructorName*/ false,
567 /*AllowConstructorName*/ false, ParsedType(),
568 TemplateKWLoc, ReductionId);
569}
570
Alexander Musman1bb328c2014-06-04 13:06:39 +0000571/// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000572/// 'shared', 'copyin', or 'reduction'.
573///
574/// private-clause:
575/// 'private' '(' list ')'
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000576/// firstprivate-clause:
577/// 'firstprivate' '(' list ')'
Alexander Musman1bb328c2014-06-04 13:06:39 +0000578/// lastprivate-clause:
579/// 'lastprivate' '(' list ')'
Alexey Bataev758e55e2013-09-06 18:03:48 +0000580/// shared-clause:
581/// 'shared' '(' list ')'
Alexander Musman8dba6642014-04-22 13:09:42 +0000582/// linear-clause:
583/// 'linear' '(' list [ ':' linear-step ] ')'
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000584/// aligned-clause:
585/// 'aligned' '(' list [ ':' alignment ] ')'
Alexey Bataevc5e02582014-06-16 07:08:35 +0000586/// reduction-clause:
587/// 'reduction' '(' reduction-identifier ':' list ')'
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000588///
589OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
590 SourceLocation Loc = Tok.getLocation();
591 SourceLocation LOpen = ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000592 SourceLocation ColonLoc = SourceLocation();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000593 // Optional scope specifier and unqualified id for reduction identifier.
594 CXXScopeSpec ReductionIdScopeSpec;
595 UnqualifiedId ReductionId;
596 bool InvalidReductionId = false;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000597 // Parse '('.
Alp Tokerd751fa72013-12-18 19:10:49 +0000598 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000599 if (T.expectAndConsume(diag::err_expected_lparen_after,
600 getOpenMPClauseName(Kind)))
Craig Topper161e4db2014-05-21 06:02:52 +0000601 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000602
Alexey Bataevc5e02582014-06-16 07:08:35 +0000603 // Handle reduction-identifier for reduction clause.
604 if (Kind == OMPC_reduction) {
605 ColonProtectionRAIIObject ColonRAII(*this);
606 if (getLangOpts().CPlusPlus) {
607 ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false);
608 }
609 InvalidReductionId =
610 ParseReductionId(*this, ReductionIdScopeSpec, ReductionId);
611 if (InvalidReductionId) {
612 SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
613 StopBeforeMatch);
614 }
615 if (Tok.is(tok::colon)) {
616 ColonLoc = ConsumeToken();
617 } else {
618 Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
619 }
620 }
621
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000622 SmallVector<Expr *, 5> Vars;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000623 bool IsComma = !InvalidReductionId;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000624 const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
Alexander Musman8dba6642014-04-22 13:09:42 +0000625 while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000626 Tok.isNot(tok::annot_pragma_openmp_end))) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000627 ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000628 // Parse variable
629 ExprResult VarExpr = ParseAssignmentExpression();
630 if (VarExpr.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000631 Vars.push_back(VarExpr.get());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000632 } else {
633 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alp Tokerd751fa72013-12-18 19:10:49 +0000634 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000635 }
636 // Skip ',' if any
637 IsComma = Tok.is(tok::comma);
Alexander Musman8dba6642014-04-22 13:09:42 +0000638 if (IsComma)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000639 ConsumeToken();
Alexander Musman8dba6642014-04-22 13:09:42 +0000640 else if (Tok.isNot(tok::r_paren) &&
641 Tok.isNot(tok::annot_pragma_openmp_end) &&
642 (!MayHaveTail || Tok.isNot(tok::colon)))
643 Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind);
644 }
645
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000646 // Parse ':' linear-step (or ':' alignment).
Craig Topper161e4db2014-05-21 06:02:52 +0000647 Expr *TailExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +0000648 const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
649 if (MustHaveTail) {
650 ColonLoc = Tok.getLocation();
651 ConsumeToken();
652 ExprResult Tail = ParseAssignmentExpression();
653 if (Tail.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000654 TailExpr = Tail.get();
Alexander Musman8dba6642014-04-22 13:09:42 +0000655 else
656 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
657 StopBeforeMatch);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000658 }
659
660 // Parse ')'.
661 T.consumeClose();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000662 if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId)
Craig Topper161e4db2014-05-21 06:02:52 +0000663 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000664
Alexey Bataevc5e02582014-06-16 07:08:35 +0000665 return Actions.ActOnOpenMPVarListClause(
666 Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(),
667 ReductionIdScopeSpec,
668 ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId)
669 : DeclarationNameInfo());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000670}
671