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" |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 15 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Parser.h" |
| 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "llvm/ADT/PointerIntPair.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 20 | #include "RAIIObjectsForParser.h" |
| 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: |
| 63 | case OMPD_task: |
| 64 | case NUM_OPENMP_DIRECTIVES: |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 65 | Diag(Tok, diag::err_omp_unexpected_directive) |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 66 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 67 | break; |
| 68 | } |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 69 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 70 | return DeclGroupPtrTy(); |
| 71 | } |
| 72 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 73 | /// \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 | /// |
| 82 | StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() { |
| 83 | assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 84 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 85 | 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 Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 89 | const unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | |
| 90 | Scope::OpenMPDirectiveScope; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 91 | SourceLocation Loc = ConsumeToken(), EndLoc; |
| 92 | OpenMPDirectiveKind DKind = Tok.isAnnotation() ? |
| 93 | OMPD_unknown : |
| 94 | getOpenMPDirectiveKind(PP.getSpelling(Tok)); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 95 | // Name of critical directive. |
| 96 | DeclarationNameInfo DirName; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 97 | 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 Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 108 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 109 | } |
| 110 | DeclGroupPtrTy Res = |
| 111 | Actions.ActOnOpenMPThreadprivateDirective(Loc, |
| 112 | Identifiers); |
| 113 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
| 114 | } |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 115 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 116 | break; |
| 117 | case OMPD_parallel: { |
| 118 | ConsumeToken(); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 119 | |
| 120 | Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope()); |
| 121 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 122 | 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 Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 149 | Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_OpenMP, 1); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 150 | 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 Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 168 | Actions.EndOpenMPDSABlock(Directive.get()); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 169 | OMPDirectiveScope.Exit(); |
| 170 | } |
| 171 | break; |
| 172 | case OMPD_unknown: |
| 173 | Diag(Tok, diag::err_omp_unknown_directive); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 174 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 175 | break; |
| 176 | case OMPD_task: |
| 177 | case NUM_OPENMP_DIRECTIVES: |
| 178 | Diag(Tok, diag::err_omp_unexpected_directive) |
| 179 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 180 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | return Directive; |
| 184 | } |
| 185 | |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 186 | /// \brief Parses list of simple variables for '#pragma omp threadprivate' |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 187 | /// directive. |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 188 | /// |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 189 | /// simple-variable-list: |
| 190 | /// '(' id-expression {, id-expression} ')' |
| 191 | /// |
| 192 | bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind, |
| 193 | SmallVectorImpl<Expr *> &VarList, |
| 194 | bool AllowScopeSpecifier) { |
| 195 | VarList.clear(); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 196 | // Parse '('. |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 197 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 198 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 199 | getOpenMPDirectiveName(Kind))) |
| 200 | return true; |
| 201 | bool IsCorrect = true; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 202 | bool NoIdentIsFound = true; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 203 | |
| 204 | // Read tokens while ')' or annot_pragma_openmp_end is not found. |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 205 | 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] | 206 | CXXScopeSpec SS; |
| 207 | SourceLocation TemplateKWLoc; |
| 208 | UnqualifiedId Name; |
| 209 | // Read var name. |
| 210 | Token PrevTok = Tok; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 211 | NoIdentIsFound = false; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 212 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 213 | if (AllowScopeSpecifier && getLangOpts().CPlusPlus && |
| 214 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 215 | IsCorrect = false; |
| 216 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 217 | StopBeforeMatch); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 218 | } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(), |
| 219 | TemplateKWLoc, Name)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 220 | IsCorrect = false; |
| 221 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 222 | StopBeforeMatch); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 223 | } 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 Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 227 | StopBeforeMatch); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 228 | Diag(PrevTok.getLocation(), diag::err_expected_ident) |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 229 | << SourceRange(PrevTok.getLocation(), PrevTokLocation); |
| 230 | } else { |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 231 | 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 Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 236 | } |
| 237 | // Consume ','. |
| 238 | if (Tok.is(tok::comma)) { |
| 239 | ConsumeToken(); |
| 240 | } |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 243 | if (NoIdentIsFound) { |
| 244 | Diag(Tok, diag::err_expected_ident); |
| 245 | IsCorrect = false; |
| 246 | } |
| 247 | |
| 248 | // Parse ')'. |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 249 | IsCorrect = !T.consumeClose() && IsCorrect; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 250 | |
| 251 | return !IsCorrect && VarList.empty(); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 252 | } |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 253 | |
| 254 | /// \brief Parsing of OpenMP clauses. |
| 255 | /// |
| 256 | /// clause: |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 257 | /// default-clause|private-clause|firstprivate-clause|shared-clause |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 258 | /// |
| 259 | OMPClause *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 Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 283 | case OMPC_firstprivate: |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 284 | case OMPC_shared: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 285 | Clause = ParseOpenMPVarListClause(CKind); |
| 286 | break; |
| 287 | case OMPC_unknown: |
| 288 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
| 289 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 290 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 291 | break; |
| 292 | case OMPC_threadprivate: |
| 293 | case NUM_OPENMP_CLAUSES: |
| 294 | Diag(Tok, diag::err_omp_unexpected_clause) |
| 295 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 296 | SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 297 | 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 | /// |
| 307 | OMPClause *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 Kramer | 844a527 | 2013-07-20 12:06:17 +0000 | [diff] [blame] | 317 | unsigned(OMPC_DEFAULT_unknown) : |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 318 | 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 Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 336 | /// firstprivate-clause: |
| 337 | /// 'firstprivate' '(' list ')' |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 338 | /// shared-clause: |
| 339 | /// 'shared' '(' list ')' |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 340 | /// |
| 341 | OMPClause *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 Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 360 | StopBeforeMatch); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 361 | } |
| 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 | |