blob: 89e4147e285c9ec0a4fc53914d51550e8824c41a [file] [log] [blame]
Alexey Bataevc6400582013-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
14#include "clang/AST/ASTConsumer.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000015#include "clang/AST/StmtOpenMP.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000016#include "clang/Parse/ParseDiagnostic.h"
Alexey Bataev6af701f2013-05-13 04:18:18 +000017#include "clang/Parse/Parser.h"
18#include "clang/Sema/Scope.h"
19#include "llvm/ADT/PointerIntPair.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000020#include "RAIIObjectsForParser.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// OpenMP declarative directives.
25//===----------------------------------------------------------------------===//
26
Alexey Bataev6af701f2013-05-13 04:18:18 +000027/// \brief Parsing of declarative OpenMP directives.
28///
29/// threadprivate-directive:
30/// annot_pragma_openmp 'threadprivate' simple-variable-list
Alexey Bataevc6400582013-03-22 06:34:35 +000031///
32Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() {
33 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8fe24752013-11-18 08:17:37 +000034 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataevc6400582013-03-22 06:34:35 +000035
36 SourceLocation Loc = ConsumeToken();
Alexey Bataev6af701f2013-05-13 04:18:18 +000037 SmallVector<Expr *, 5> Identifiers;
38 OpenMPDirectiveKind DKind = Tok.isAnnotation() ?
39 OMPD_unknown :
40 getOpenMPDirectiveKind(PP.getSpelling(Tok));
41
42 switch (DKind) {
Alexey Bataevc6400582013-03-22 06:34:35 +000043 case OMPD_threadprivate:
44 ConsumeToken();
Alexey Bataev6af701f2013-05-13 04:18:18 +000045 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) {
Alexey Bataevc6400582013-03-22 06:34:35 +000046 // The last seen token is annot_pragma_openmp_end - need to check for
47 // extra tokens.
48 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
49 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
50 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataev8fe24752013-11-18 08:17:37 +000051 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataevc6400582013-03-22 06:34:35 +000052 }
Alexey Bataev6af701f2013-05-13 04:18:18 +000053 // Skip the last annot_pragma_openmp_end.
Alexey Bataevc6400582013-03-22 06:34:35 +000054 ConsumeToken();
55 return Actions.ActOnOpenMPThreadprivateDirective(Loc,
Alexey Bataevc6400582013-03-22 06:34:35 +000056 Identifiers);
57 }
58 break;
59 case OMPD_unknown:
60 Diag(Tok, diag::err_omp_unknown_directive);
61 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000062 case OMPD_parallel:
63 case OMPD_task:
64 case NUM_OPENMP_DIRECTIVES:
Alexey Bataevc6400582013-03-22 06:34:35 +000065 Diag(Tok, diag::err_omp_unexpected_directive)
Alexey Bataev6af701f2013-05-13 04:18:18 +000066 << getOpenMPDirectiveName(DKind);
Alexey Bataevc6400582013-03-22 06:34:35 +000067 break;
68 }
Alexey Bataev8fe24752013-11-18 08:17:37 +000069 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataevc6400582013-03-22 06:34:35 +000070 return DeclGroupPtrTy();
71}
72
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000073/// \brief Parsing of declarative or executable OpenMP directives.
74///
75/// threadprivate-directive:
76/// annot_pragma_openmp 'threadprivate' simple-variable-list
77/// annot_pragma_openmp_end
78///
79/// parallel-directive:
80/// annot_pragma_openmp 'parallel' {clause} annot_pragma_openmp_end
81///
82StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
83 assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
Alexey Bataev8fe24752013-11-18 08:17:37 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000085 SmallVector<Expr *, 5> Identifiers;
86 SmallVector<OMPClause *, 5> Clauses;
87 SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, NUM_OPENMP_CLAUSES>
88 FirstClauses(NUM_OPENMP_CLAUSES);
Alexey Bataev0c018352013-09-06 18:03:48 +000089 const unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
90 Scope::OpenMPDirectiveScope;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000091 SourceLocation Loc = ConsumeToken(), EndLoc;
92 OpenMPDirectiveKind DKind = Tok.isAnnotation() ?
93 OMPD_unknown :
94 getOpenMPDirectiveKind(PP.getSpelling(Tok));
Alexey Bataev0c018352013-09-06 18:03:48 +000095 // Name of critical directive.
96 DeclarationNameInfo DirName;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000097 StmtResult Directive = StmtError();
98
99 switch (DKind) {
100 case OMPD_threadprivate:
101 ConsumeToken();
102 if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) {
103 // The last seen token is annot_pragma_openmp_end - need to check for
104 // extra tokens.
105 if (Tok.isNot(tok::annot_pragma_openmp_end)) {
106 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
107 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000108 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000109 }
110 DeclGroupPtrTy Res =
111 Actions.ActOnOpenMPThreadprivateDirective(Loc,
112 Identifiers);
113 Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
114 }
Alexey Bataev8fe24752013-11-18 08:17:37 +0000115 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000116 break;
117 case OMPD_parallel: {
118 ConsumeToken();
Alexey Bataev0c018352013-09-06 18:03:48 +0000119
120 Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope());
121
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000122 while (Tok.isNot(tok::annot_pragma_openmp_end)) {
123 OpenMPClauseKind CKind = Tok.isAnnotation() ?
124 OMPC_unknown :
125 getOpenMPClauseKind(PP.getSpelling(Tok));
126 OMPClause *Clause = ParseOpenMPClause(DKind, CKind,
127 !FirstClauses[CKind].getInt());
128 FirstClauses[CKind].setInt(true);
129 if (Clause) {
130 FirstClauses[CKind].setPointer(Clause);
131 Clauses.push_back(Clause);
132 }
133
134 // Skip ',' if any.
135 if (Tok.is(tok::comma))
136 ConsumeToken();
137 }
138 // End location of the directive.
139 EndLoc = Tok.getLocation();
140 // Consume final annot_pragma_openmp_end.
141 ConsumeToken();
142
143 StmtResult AssociatedStmt;
144 bool CreateDirective = true;
145 ParseScope OMPDirectiveScope(this, ScopeFlags);
146 {
147 // The body is a block scope like in Lambdas and Blocks.
148 Sema::CompoundScopeRAII CompoundScope(Actions);
Alexey Bataev0c018352013-09-06 18:03:48 +0000149 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_OpenMP, 1);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000150 Actions.ActOnStartOfCompoundStmt();
151 // Parse statement
152 AssociatedStmt = ParseStatement();
153 Actions.ActOnFinishOfCompoundStmt();
154 if (!AssociatedStmt.isUsable()) {
155 Actions.ActOnCapturedRegionError();
156 CreateDirective = false;
157 } else {
158 AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.take());
159 CreateDirective = AssociatedStmt.isUsable();
160 }
161 }
162 if (CreateDirective)
163 Directive = Actions.ActOnOpenMPExecutableDirective(DKind, Clauses,
164 AssociatedStmt.take(),
165 Loc, EndLoc);
166
167 // Exit scope.
Alexey Bataev0c018352013-09-06 18:03:48 +0000168 Actions.EndOpenMPDSABlock(Directive.get());
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000169 OMPDirectiveScope.Exit();
170 }
171 break;
172 case OMPD_unknown:
173 Diag(Tok, diag::err_omp_unknown_directive);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000174 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000175 break;
176 case OMPD_task:
177 case NUM_OPENMP_DIRECTIVES:
178 Diag(Tok, diag::err_omp_unexpected_directive)
179 << getOpenMPDirectiveName(DKind);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000180 SkipUntil(tok::annot_pragma_openmp_end);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000181 break;
182 }
183 return Directive;
184}
185
Alexey Bataevc6400582013-03-22 06:34:35 +0000186/// \brief Parses list of simple variables for '#pragma omp threadprivate'
Alexey Bataev6af701f2013-05-13 04:18:18 +0000187/// directive.
Alexey Bataevc6400582013-03-22 06:34:35 +0000188///
Alexey Bataev6af701f2013-05-13 04:18:18 +0000189/// simple-variable-list:
190/// '(' id-expression {, id-expression} ')'
191///
192bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
193 SmallVectorImpl<Expr *> &VarList,
194 bool AllowScopeSpecifier) {
195 VarList.clear();
Alexey Bataevc6400582013-03-22 06:34:35 +0000196 // Parse '('.
Alexey Bataev6af701f2013-05-13 04:18:18 +0000197 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000198 if (T.expectAndConsume(diag::err_expected_lparen_after,
199 getOpenMPDirectiveName(Kind)))
200 return true;
201 bool IsCorrect = true;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000202 bool NoIdentIsFound = true;
Alexey Bataevc6400582013-03-22 06:34:35 +0000203
204 // Read tokens while ')' or annot_pragma_openmp_end is not found.
Alexey Bataev6af701f2013-05-13 04:18:18 +0000205 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000206 CXXScopeSpec SS;
207 SourceLocation TemplateKWLoc;
208 UnqualifiedId Name;
209 // Read var name.
210 Token PrevTok = Tok;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000211 NoIdentIsFound = false;
Alexey Bataevc6400582013-03-22 06:34:35 +0000212
Alexey Bataev6af701f2013-05-13 04:18:18 +0000213 if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
214 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000215 IsCorrect = false;
216 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alexey Bataev8fe24752013-11-18 08:17:37 +0000217 StopBeforeMatch);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000218 } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(),
219 TemplateKWLoc, Name)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000220 IsCorrect = false;
221 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alexey Bataev8fe24752013-11-18 08:17:37 +0000222 StopBeforeMatch);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000223 } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
224 Tok.isNot(tok::annot_pragma_openmp_end)) {
225 IsCorrect = false;
226 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alexey Bataev8fe24752013-11-18 08:17:37 +0000227 StopBeforeMatch);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000228 Diag(PrevTok.getLocation(), diag::err_expected_ident)
Alexey Bataevc6400582013-03-22 06:34:35 +0000229 << SourceRange(PrevTok.getLocation(), PrevTokLocation);
230 } else {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000231 DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name);
232 ExprResult Res = Actions.ActOnOpenMPIdExpression(getCurScope(), SS,
233 NameInfo);
234 if (Res.isUsable())
235 VarList.push_back(Res.take());
Alexey Bataevc6400582013-03-22 06:34:35 +0000236 }
237 // Consume ','.
238 if (Tok.is(tok::comma)) {
239 ConsumeToken();
240 }
Alexey Bataevc6400582013-03-22 06:34:35 +0000241 }
242
Alexey Bataev6af701f2013-05-13 04:18:18 +0000243 if (NoIdentIsFound) {
244 Diag(Tok, diag::err_expected_ident);
245 IsCorrect = false;
246 }
247
248 // Parse ')'.
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000249 IsCorrect = !T.consumeClose() && IsCorrect;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000250
251 return !IsCorrect && VarList.empty();
Alexey Bataevc6400582013-03-22 06:34:35 +0000252}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000253
254/// \brief Parsing of OpenMP clauses.
255///
256/// clause:
Alexey Bataevd195bc32013-10-01 05:32:34 +0000257/// default-clause|private-clause|firstprivate-clause|shared-clause
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000258///
259OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
260 OpenMPClauseKind CKind, bool FirstClause) {
261 OMPClause *Clause = 0;
262 bool ErrorFound = false;
263 // Check if clause is allowed for the given directive.
264 if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
265 Diag(Tok, diag::err_omp_unexpected_clause)
266 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
267 ErrorFound = true;
268 }
269
270 switch (CKind) {
271 case OMPC_default:
272 // OpenMP [2.9.3.1, Restrictions]
273 // Only a single default clause may be specified on a parallel or task
274 // directive.
275 if (!FirstClause) {
276 Diag(Tok, diag::err_omp_more_one_clause)
277 << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
278 }
279
280 Clause = ParseOpenMPSimpleClause(CKind);
281 break;
282 case OMPC_private:
Alexey Bataevd195bc32013-10-01 05:32:34 +0000283 case OMPC_firstprivate:
Alexey Bataev0c018352013-09-06 18:03:48 +0000284 case OMPC_shared:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000285 Clause = ParseOpenMPVarListClause(CKind);
286 break;
287 case OMPC_unknown:
288 Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
289 << getOpenMPDirectiveName(DKind);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000290 SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000291 break;
292 case OMPC_threadprivate:
293 case NUM_OPENMP_CLAUSES:
294 Diag(Tok, diag::err_omp_unexpected_clause)
295 << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000296 SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000297 break;
298 }
299 return ErrorFound ? 0 : Clause;
300}
301
302/// \brief Parsing of simple OpenMP clauses like 'default'.
303///
304/// default-clause:
305/// 'default' '(' 'none' | 'shared' ')
306///
307OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
308 SourceLocation Loc = Tok.getLocation();
309 SourceLocation LOpen = ConsumeToken();
310 // Parse '('.
311 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
312 if (T.expectAndConsume(diag::err_expected_lparen_after,
313 getOpenMPClauseName(Kind)))
314 return 0;
315
316 unsigned Type = Tok.isAnnotation() ?
Benjamin Kramer844a5272013-07-20 12:06:17 +0000317 unsigned(OMPC_DEFAULT_unknown) :
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000318 getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
319 SourceLocation TypeLoc = Tok.getLocation();
320 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
321 Tok.isNot(tok::annot_pragma_openmp_end))
322 ConsumeAnyToken();
323
324 // Parse ')'.
325 T.consumeClose();
326
327 return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
328 Tok.getLocation());
329}
330
331/// \brief Parsing of OpenMP clause 'private', 'firstprivate',
332/// 'shared', 'copyin', or 'reduction'.
333///
334/// private-clause:
335/// 'private' '(' list ')'
Alexey Bataevd195bc32013-10-01 05:32:34 +0000336/// firstprivate-clause:
337/// 'firstprivate' '(' list ')'
Alexey Bataev0c018352013-09-06 18:03:48 +0000338/// shared-clause:
339/// 'shared' '(' list ')'
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000340///
341OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) {
342 SourceLocation Loc = Tok.getLocation();
343 SourceLocation LOpen = ConsumeToken();
344 // Parse '('.
345 BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
346 if (T.expectAndConsume(diag::err_expected_lparen_after,
347 getOpenMPClauseName(Kind)))
348 return 0;
349
350 SmallVector<Expr *, 5> Vars;
351 bool IsComma = true;
352 while (IsComma || (Tok.isNot(tok::r_paren) &&
353 Tok.isNot(tok::annot_pragma_openmp_end))) {
354 // Parse variable
355 ExprResult VarExpr = ParseAssignmentExpression();
356 if (VarExpr.isUsable()) {
357 Vars.push_back(VarExpr.take());
358 } else {
359 SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
Alexey Bataev8fe24752013-11-18 08:17:37 +0000360 StopBeforeMatch);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000361 }
362 // Skip ',' if any
363 IsComma = Tok.is(tok::comma);
364 if (IsComma) {
365 ConsumeToken();
366 } else if (Tok.isNot(tok::r_paren) &&
367 Tok.isNot(tok::annot_pragma_openmp_end)) {
368 Diag(Tok, diag::err_omp_expected_punc)
369 << 1 << getOpenMPClauseName(Kind);
370 }
371 }
372
373 // Parse ')'.
374 T.consumeClose();
375 if (Vars.empty())
376 return 0;
377
378 return Actions.ActOnOpenMPVarListClause(Kind, Vars, Loc, LOpen,
379 Tok.getLocation());
380}
381