Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1 | //===--- 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" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 15 | #include "RAIIObjectsForParser.h" |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseDiagnostic.h" |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Parser.h" |
| 19 | #include "clang/Sema/Scope.h" |
| 20 | #include "llvm/ADT/PointerIntPair.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // OpenMP declarative directives. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 27 | /// \brief Parsing of declarative OpenMP directives. |
| 28 | /// |
| 29 | /// threadprivate-directive: |
| 30 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 31 | /// |
| 32 | Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() { |
| 33 | assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 34 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 35 | |
| 36 | SourceLocation Loc = ConsumeToken(); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 37 | SmallVector<Expr *, 5> Identifiers; |
| 38 | OpenMPDirectiveKind DKind = Tok.isAnnotation() ? |
| 39 | OMPD_unknown : |
| 40 | getOpenMPDirectiveKind(PP.getSpelling(Tok)); |
| 41 | |
| 42 | switch (DKind) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 43 | case OMPD_threadprivate: |
| 44 | ConsumeToken(); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 45 | if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 46 | // 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 Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 51 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 52 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 53 | // Skip the last annot_pragma_openmp_end. |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 54 | ConsumeToken(); |
| 55 | return Actions.ActOnOpenMPThreadprivateDirective(Loc, |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 56 | Identifiers); |
| 57 | } |
| 58 | break; |
| 59 | case OMPD_unknown: |
| 60 | Diag(Tok, diag::err_omp_unknown_directive); |
| 61 | break; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 62 | case OMPD_parallel: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 63 | case OMPD_simd: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 64 | case OMPD_task: |
| 65 | case NUM_OPENMP_DIRECTIVES: |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 66 | Diag(Tok, diag::err_omp_unexpected_directive) |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 67 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 68 | break; |
| 69 | } |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 70 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 71 | return DeclGroupPtrTy(); |
| 72 | } |
| 73 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 74 | /// \brief Parsing of declarative or executable OpenMP directives. |
| 75 | /// |
| 76 | /// threadprivate-directive: |
| 77 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
| 78 | /// annot_pragma_openmp_end |
| 79 | /// |
| 80 | /// parallel-directive: |
| 81 | /// annot_pragma_openmp 'parallel' {clause} annot_pragma_openmp_end |
| 82 | /// |
| 83 | StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() { |
| 84 | assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 85 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 86 | SmallVector<Expr *, 5> Identifiers; |
| 87 | SmallVector<OMPClause *, 5> Clauses; |
| 88 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, NUM_OPENMP_CLAUSES> |
| 89 | FirstClauses(NUM_OPENMP_CLAUSES); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 90 | const unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | |
| 91 | Scope::OpenMPDirectiveScope; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 92 | SourceLocation Loc = ConsumeToken(), EndLoc; |
| 93 | OpenMPDirectiveKind DKind = Tok.isAnnotation() ? |
| 94 | OMPD_unknown : |
| 95 | getOpenMPDirectiveKind(PP.getSpelling(Tok)); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 96 | // Name of critical directive. |
| 97 | DeclarationNameInfo DirName; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 98 | StmtResult Directive = StmtError(); |
| 99 | |
| 100 | switch (DKind) { |
| 101 | case OMPD_threadprivate: |
| 102 | ConsumeToken(); |
| 103 | if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) { |
| 104 | // The last seen token is annot_pragma_openmp_end - need to check for |
| 105 | // extra tokens. |
| 106 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 107 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
| 108 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 109 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 110 | } |
| 111 | DeclGroupPtrTy Res = |
| 112 | Actions.ActOnOpenMPThreadprivateDirective(Loc, |
| 113 | Identifiers); |
| 114 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
| 115 | } |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 116 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 117 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 118 | case OMPD_parallel: |
| 119 | case OMPD_simd: { |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 120 | ConsumeToken(); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 121 | |
| 122 | Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope()); |
| 123 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 124 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 125 | OpenMPClauseKind CKind = Tok.isAnnotation() ? |
| 126 | OMPC_unknown : |
| 127 | getOpenMPClauseKind(PP.getSpelling(Tok)); |
| 128 | OMPClause *Clause = ParseOpenMPClause(DKind, CKind, |
| 129 | !FirstClauses[CKind].getInt()); |
| 130 | FirstClauses[CKind].setInt(true); |
| 131 | if (Clause) { |
| 132 | FirstClauses[CKind].setPointer(Clause); |
| 133 | Clauses.push_back(Clause); |
| 134 | } |
| 135 | |
| 136 | // Skip ',' if any. |
| 137 | if (Tok.is(tok::comma)) |
| 138 | ConsumeToken(); |
| 139 | } |
| 140 | // End location of the directive. |
| 141 | EndLoc = Tok.getLocation(); |
| 142 | // Consume final annot_pragma_openmp_end. |
| 143 | ConsumeToken(); |
| 144 | |
| 145 | StmtResult AssociatedStmt; |
| 146 | bool CreateDirective = true; |
| 147 | ParseScope OMPDirectiveScope(this, ScopeFlags); |
| 148 | { |
| 149 | // The body is a block scope like in Lambdas and Blocks. |
| 150 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 151 | Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_OpenMP, 1); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 152 | Actions.ActOnStartOfCompoundStmt(); |
| 153 | // Parse statement |
| 154 | AssociatedStmt = ParseStatement(); |
| 155 | Actions.ActOnFinishOfCompoundStmt(); |
| 156 | if (!AssociatedStmt.isUsable()) { |
| 157 | Actions.ActOnCapturedRegionError(); |
| 158 | CreateDirective = false; |
| 159 | } else { |
| 160 | AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.take()); |
| 161 | CreateDirective = AssociatedStmt.isUsable(); |
| 162 | } |
| 163 | } |
| 164 | if (CreateDirective) |
| 165 | Directive = Actions.ActOnOpenMPExecutableDirective(DKind, Clauses, |
| 166 | AssociatedStmt.take(), |
| 167 | Loc, EndLoc); |
| 168 | |
| 169 | // Exit scope. |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 170 | Actions.EndOpenMPDSABlock(Directive.get()); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 171 | OMPDirectiveScope.Exit(); |
| 172 | } |
| 173 | break; |
| 174 | case OMPD_unknown: |
| 175 | Diag(Tok, diag::err_omp_unknown_directive); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 176 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 177 | break; |
| 178 | case OMPD_task: |
| 179 | case NUM_OPENMP_DIRECTIVES: |
| 180 | Diag(Tok, diag::err_omp_unexpected_directive) |
| 181 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 182 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | return Directive; |
| 186 | } |
| 187 | |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 188 | /// \brief Parses list of simple variables for '#pragma omp threadprivate' |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 189 | /// directive. |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 190 | /// |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 191 | /// simple-variable-list: |
| 192 | /// '(' id-expression {, id-expression} ')' |
| 193 | /// |
| 194 | bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind, |
| 195 | SmallVectorImpl<Expr *> &VarList, |
| 196 | bool AllowScopeSpecifier) { |
| 197 | VarList.clear(); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 198 | // Parse '('. |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 199 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 200 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 201 | getOpenMPDirectiveName(Kind))) |
| 202 | return true; |
| 203 | bool IsCorrect = true; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 204 | bool NoIdentIsFound = true; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 205 | |
| 206 | // Read tokens while ')' or annot_pragma_openmp_end is not found. |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 207 | while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 208 | CXXScopeSpec SS; |
| 209 | SourceLocation TemplateKWLoc; |
| 210 | UnqualifiedId Name; |
| 211 | // Read var name. |
| 212 | Token PrevTok = Tok; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 213 | NoIdentIsFound = false; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 214 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 215 | if (AllowScopeSpecifier && getLangOpts().CPlusPlus && |
| 216 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 217 | IsCorrect = false; |
| 218 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 219 | StopBeforeMatch); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 220 | } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(), |
| 221 | TemplateKWLoc, Name)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 222 | IsCorrect = false; |
| 223 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 224 | StopBeforeMatch); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 225 | } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && |
| 226 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 227 | IsCorrect = false; |
| 228 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 229 | StopBeforeMatch); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 230 | Diag(PrevTok.getLocation(), diag::err_expected) |
| 231 | << tok::identifier |
| 232 | << SourceRange(PrevTok.getLocation(), PrevTokLocation); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 233 | } else { |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 234 | DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name); |
| 235 | ExprResult Res = Actions.ActOnOpenMPIdExpression(getCurScope(), SS, |
| 236 | NameInfo); |
| 237 | if (Res.isUsable()) |
| 238 | VarList.push_back(Res.take()); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 239 | } |
| 240 | // Consume ','. |
| 241 | if (Tok.is(tok::comma)) { |
| 242 | ConsumeToken(); |
| 243 | } |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 246 | if (NoIdentIsFound) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 247 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 248 | IsCorrect = false; |
| 249 | } |
| 250 | |
| 251 | // Parse ')'. |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 252 | IsCorrect = !T.consumeClose() && IsCorrect; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 253 | |
| 254 | return !IsCorrect && VarList.empty(); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 255 | } |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 256 | |
| 257 | /// \brief Parsing of OpenMP clauses. |
| 258 | /// |
| 259 | /// clause: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 260 | /// if-clause | num_threads-clause | safelen-clause | default-clause | |
| 261 | /// private-clause | firstprivate-clause | shared-clause |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 262 | /// |
| 263 | OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, |
| 264 | OpenMPClauseKind CKind, bool FirstClause) { |
| 265 | OMPClause *Clause = 0; |
| 266 | bool ErrorFound = false; |
| 267 | // Check if clause is allowed for the given directive. |
| 268 | if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) { |
| 269 | Diag(Tok, diag::err_omp_unexpected_clause) |
| 270 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
| 271 | ErrorFound = true; |
| 272 | } |
| 273 | |
| 274 | switch (CKind) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 275 | case OMPC_if: |
| 276 | case OMPC_num_threads: |
| 277 | case OMPC_safelen: |
| 278 | // OpenMP [2.5, Restrictions] |
| 279 | // At most one if clause can appear on the directive. |
| 280 | // At most one num_threads clause can appear on the directive. |
| 281 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 282 | // Only one safelen clause can appear on a simd directive. |
| 283 | if (!FirstClause) { |
| 284 | Diag(Tok, diag::err_omp_more_one_clause) |
| 285 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind); |
| 286 | } |
| 287 | |
| 288 | Clause = ParseOpenMPSingleExprClause(CKind); |
| 289 | break; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 290 | case OMPC_default: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 291 | // OpenMP [2.14.3.1, Restrictions] |
| 292 | // Only a single default clause may be specified on a parallel, task or |
| 293 | // teams directive. |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 294 | if (!FirstClause) { |
| 295 | Diag(Tok, diag::err_omp_more_one_clause) |
| 296 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind); |
| 297 | } |
| 298 | |
| 299 | Clause = ParseOpenMPSimpleClause(CKind); |
| 300 | break; |
| 301 | case OMPC_private: |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 302 | case OMPC_firstprivate: |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 303 | case OMPC_shared: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 304 | case OMPC_copyin: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 305 | Clause = ParseOpenMPVarListClause(CKind); |
| 306 | break; |
| 307 | case OMPC_unknown: |
| 308 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
| 309 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 310 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 311 | break; |
| 312 | case OMPC_threadprivate: |
| 313 | case NUM_OPENMP_CLAUSES: |
| 314 | Diag(Tok, diag::err_omp_unexpected_clause) |
| 315 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 316 | SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | return ErrorFound ? 0 : Clause; |
| 320 | } |
| 321 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 322 | /// \brief Parsing of OpenMP clauses with single expressions like 'if', |
| 323 | /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or |
| 324 | /// 'thread_limit'. |
| 325 | /// |
| 326 | /// if-clause: |
| 327 | /// 'if' '(' expression ')' |
| 328 | /// |
| 329 | /// num_threads-clause: |
| 330 | /// 'num_threads' '(' expression ')' |
| 331 | /// |
| 332 | /// safelen-clause: |
| 333 | /// 'safelen' '(' expression ')' |
| 334 | /// |
| 335 | OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) { |
| 336 | SourceLocation Loc = ConsumeToken(); |
| 337 | |
| 338 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 339 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 340 | getOpenMPClauseName(Kind))) |
| 341 | return 0; |
| 342 | |
| 343 | ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); |
| 344 | ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
| 345 | |
| 346 | // Parse ')'. |
| 347 | T.consumeClose(); |
| 348 | |
| 349 | if (Val.isInvalid()) |
| 350 | return 0; |
| 351 | |
| 352 | return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc, |
| 353 | T.getOpenLocation(), |
| 354 | T.getCloseLocation()); |
| 355 | } |
| 356 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 357 | /// \brief Parsing of simple OpenMP clauses like 'default'. |
| 358 | /// |
| 359 | /// default-clause: |
| 360 | /// 'default' '(' 'none' | 'shared' ') |
| 361 | /// |
| 362 | OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) { |
| 363 | SourceLocation Loc = Tok.getLocation(); |
| 364 | SourceLocation LOpen = ConsumeToken(); |
| 365 | // Parse '('. |
| 366 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 367 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 368 | getOpenMPClauseName(Kind))) |
| 369 | return 0; |
| 370 | |
| 371 | unsigned Type = Tok.isAnnotation() ? |
Benjamin Kramer | 844a527 | 2013-07-20 12:06:17 +0000 | [diff] [blame] | 372 | unsigned(OMPC_DEFAULT_unknown) : |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 373 | getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)); |
| 374 | SourceLocation TypeLoc = Tok.getLocation(); |
| 375 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
| 376 | Tok.isNot(tok::annot_pragma_openmp_end)) |
| 377 | ConsumeAnyToken(); |
| 378 | |
| 379 | // Parse ')'. |
| 380 | T.consumeClose(); |
| 381 | |
| 382 | return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, |
| 383 | Tok.getLocation()); |
| 384 | } |
| 385 | |
| 386 | /// \brief Parsing of OpenMP clause 'private', 'firstprivate', |
| 387 | /// 'shared', 'copyin', or 'reduction'. |
| 388 | /// |
| 389 | /// private-clause: |
| 390 | /// 'private' '(' list ')' |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 391 | /// firstprivate-clause: |
| 392 | /// 'firstprivate' '(' list ')' |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 393 | /// shared-clause: |
| 394 | /// 'shared' '(' list ')' |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 395 | /// |
| 396 | OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) { |
| 397 | SourceLocation Loc = Tok.getLocation(); |
| 398 | SourceLocation LOpen = ConsumeToken(); |
| 399 | // Parse '('. |
| 400 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 401 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 402 | getOpenMPClauseName(Kind))) |
| 403 | return 0; |
| 404 | |
| 405 | SmallVector<Expr *, 5> Vars; |
| 406 | bool IsComma = true; |
| 407 | while (IsComma || (Tok.isNot(tok::r_paren) && |
| 408 | Tok.isNot(tok::annot_pragma_openmp_end))) { |
| 409 | // Parse variable |
| 410 | ExprResult VarExpr = ParseAssignmentExpression(); |
| 411 | if (VarExpr.isUsable()) { |
| 412 | Vars.push_back(VarExpr.take()); |
| 413 | } else { |
| 414 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 415 | StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 416 | } |
| 417 | // Skip ',' if any |
| 418 | IsComma = Tok.is(tok::comma); |
| 419 | if (IsComma) { |
| 420 | ConsumeToken(); |
| 421 | } else if (Tok.isNot(tok::r_paren) && |
| 422 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 423 | Diag(Tok, diag::err_omp_expected_punc) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 424 | << getOpenMPClauseName(Kind); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | // Parse ')'. |
| 429 | T.consumeClose(); |
| 430 | if (Vars.empty()) |
| 431 | return 0; |
| 432 | |
| 433 | return Actions.ActOnOpenMPVarListClause(Kind, Vars, Loc, LOpen, |
| 434 | Tok.getLocation()); |
| 435 | } |
| 436 | |