Alexey Bataev | a769e07 | 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" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseDiagnostic.h" |
Alexey Bataev | 6f6f3b4 | 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 | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // OpenMP declarative directives. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Alexey Bataev | 6f6f3b4 | 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 | a769e07 | 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 | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 34 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 35 | |
| 36 | SourceLocation Loc = ConsumeToken(); |
Alexey Bataev | 6f6f3b4 | 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 | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 43 | case OMPD_threadprivate: |
| 44 | ConsumeToken(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 45 | if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) { |
Alexey Bataev | a769e07 | 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); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 51 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 52 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 53 | // Skip the last annot_pragma_openmp_end. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 54 | ConsumeToken(); |
| 55 | return Actions.ActOnOpenMPThreadprivateDirective(Loc, |
Alexey Bataev | a769e07 | 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 | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 62 | case OMPD_parallel: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 63 | case OMPD_simd: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 64 | case OMPD_task: |
| 65 | case NUM_OPENMP_DIRECTIVES: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 66 | Diag(Tok, diag::err_omp_unexpected_directive) |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 67 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 68 | break; |
| 69 | } |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 70 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 71 | return DeclGroupPtrTy(); |
| 72 | } |
| 73 | |
Alexey Bataev | 5ec3eb1 | 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 | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 85 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | 5ec3eb1 | 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 | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 90 | const unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope | |
| 91 | Scope::OpenMPDirectiveScope; |
Alexey Bataev | 5ec3eb1 | 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 | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 96 | // Name of critical directive. |
| 97 | DeclarationNameInfo DirName; |
Alexey Bataev | 5ec3eb1 | 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); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 109 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 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 | } |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 116 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 117 | break; |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 118 | case OMPD_parallel: |
| 119 | case OMPD_simd: { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 120 | ConsumeToken(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 121 | |
| 122 | Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope()); |
| 123 | |
Alexey Bataev | 5ec3eb1 | 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 | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 151 | Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_OpenMP, 1); |
Alexey Bataev | 5ec3eb1 | 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 | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 170 | Actions.EndOpenMPDSABlock(Directive.get()); |
Alexey Bataev | 5ec3eb1 | 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); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 176 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 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); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 182 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | return Directive; |
| 186 | } |
| 187 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 188 | /// \brief Parses list of simple variables for '#pragma omp threadprivate' |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 189 | /// directive. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 190 | /// |
Alexey Bataev | 6f6f3b4 | 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 | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 198 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 199 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 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 | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 204 | bool NoIdentIsFound = true; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 205 | |
| 206 | // Read tokens while ')' or annot_pragma_openmp_end is not found. |
Alexey Bataev | 6f6f3b4 | 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 | a769e07 | 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 | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 213 | NoIdentIsFound = false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 214 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 215 | if (AllowScopeSpecifier && getLangOpts().CPlusPlus && |
| 216 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 217 | IsCorrect = false; |
| 218 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 219 | StopBeforeMatch); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 220 | } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(), |
| 221 | TemplateKWLoc, Name)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 222 | IsCorrect = false; |
| 223 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 224 | StopBeforeMatch); |
Alexey Bataev | 6f6f3b4 | 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, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 229 | StopBeforeMatch); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 230 | Diag(PrevTok.getLocation(), diag::err_expected) |
| 231 | << tok::identifier |
| 232 | << SourceRange(PrevTok.getLocation(), PrevTokLocation); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 233 | } else { |
Alexey Bataev | 6f6f3b4 | 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 | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 239 | } |
| 240 | // Consume ','. |
| 241 | if (Tok.is(tok::comma)) { |
| 242 | ConsumeToken(); |
| 243 | } |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 246 | if (NoIdentIsFound) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 247 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 248 | IsCorrect = false; |
| 249 | } |
| 250 | |
| 251 | // Parse ')'. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 252 | IsCorrect = !T.consumeClose() && IsCorrect; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 253 | |
| 254 | return !IsCorrect && VarList.empty(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 255 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 256 | |
| 257 | /// \brief Parsing of OpenMP clauses. |
| 258 | /// |
| 259 | /// clause: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 260 | /// default-clause|private-clause|firstprivate-clause|shared-clause |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 261 | /// |
| 262 | OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, |
| 263 | OpenMPClauseKind CKind, bool FirstClause) { |
| 264 | OMPClause *Clause = 0; |
| 265 | bool ErrorFound = false; |
| 266 | // Check if clause is allowed for the given directive. |
| 267 | if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) { |
| 268 | Diag(Tok, diag::err_omp_unexpected_clause) |
| 269 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
| 270 | ErrorFound = true; |
| 271 | } |
| 272 | |
| 273 | switch (CKind) { |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 274 | case OMPC_if: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 275 | case OMPC_num_threads: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 276 | // OpenMP [2.5, Restrictions] |
| 277 | // At most one if clause can appear on the directive. |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 278 | // At most one num_threads clause can appear on the directive. |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 279 | if (!FirstClause) { |
| 280 | Diag(Tok, diag::err_omp_more_one_clause) |
| 281 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind); |
| 282 | } |
| 283 | |
| 284 | Clause = ParseOpenMPSingleExprClause(CKind); |
| 285 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 286 | case OMPC_default: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 287 | // OpenMP [2.14.3.1, Restrictions] |
| 288 | // Only a single default clause may be specified on a parallel, task or |
| 289 | // teams directive. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 290 | if (!FirstClause) { |
| 291 | Diag(Tok, diag::err_omp_more_one_clause) |
| 292 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind); |
| 293 | } |
| 294 | |
| 295 | Clause = ParseOpenMPSimpleClause(CKind); |
| 296 | break; |
| 297 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 298 | case OMPC_firstprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 299 | case OMPC_shared: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 300 | Clause = ParseOpenMPVarListClause(CKind); |
| 301 | break; |
| 302 | case OMPC_unknown: |
| 303 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
| 304 | << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 305 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 306 | break; |
| 307 | case OMPC_threadprivate: |
| 308 | case NUM_OPENMP_CLAUSES: |
| 309 | Diag(Tok, diag::err_omp_unexpected_clause) |
| 310 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 311 | SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | return ErrorFound ? 0 : Clause; |
| 315 | } |
| 316 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 317 | /// \brief Parsing of OpenMP clauses with single expressions like 'if', |
| 318 | /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or |
| 319 | /// 'thread_limit'. |
| 320 | /// |
| 321 | /// if-clause: |
| 322 | /// 'if' '(' expression ')' |
| 323 | /// |
| 324 | OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) { |
| 325 | SourceLocation Loc = ConsumeToken(); |
| 326 | |
| 327 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 328 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 329 | getOpenMPClauseName(Kind))) |
| 330 | return 0; |
| 331 | |
| 332 | ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); |
| 333 | ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
| 334 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 335 | // Parse ')'. |
| 336 | T.consumeClose(); |
| 337 | |
| 338 | if (Val.isInvalid()) |
| 339 | return 0; |
| 340 | |
| 341 | return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc, |
| 342 | T.getOpenLocation(), |
| 343 | T.getCloseLocation()); |
| 344 | } |
| 345 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 346 | /// \brief Parsing of simple OpenMP clauses like 'default'. |
| 347 | /// |
| 348 | /// default-clause: |
| 349 | /// 'default' '(' 'none' | 'shared' ') |
| 350 | /// |
| 351 | OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) { |
| 352 | SourceLocation Loc = Tok.getLocation(); |
| 353 | SourceLocation LOpen = ConsumeToken(); |
| 354 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 355 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 356 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 357 | getOpenMPClauseName(Kind))) |
| 358 | return 0; |
| 359 | |
| 360 | unsigned Type = Tok.isAnnotation() ? |
Benjamin Kramer | a3d5304 | 2013-07-20 12:06:17 +0000 | [diff] [blame] | 361 | unsigned(OMPC_DEFAULT_unknown) : |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 362 | getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)); |
| 363 | SourceLocation TypeLoc = Tok.getLocation(); |
| 364 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
| 365 | Tok.isNot(tok::annot_pragma_openmp_end)) |
| 366 | ConsumeAnyToken(); |
| 367 | |
| 368 | // Parse ')'. |
| 369 | T.consumeClose(); |
| 370 | |
| 371 | return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, |
| 372 | Tok.getLocation()); |
| 373 | } |
| 374 | |
| 375 | /// \brief Parsing of OpenMP clause 'private', 'firstprivate', |
| 376 | /// 'shared', 'copyin', or 'reduction'. |
| 377 | /// |
| 378 | /// private-clause: |
| 379 | /// 'private' '(' list ')' |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 380 | /// firstprivate-clause: |
| 381 | /// 'firstprivate' '(' list ')' |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 382 | /// shared-clause: |
| 383 | /// 'shared' '(' list ')' |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 384 | /// |
| 385 | OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) { |
| 386 | SourceLocation Loc = Tok.getLocation(); |
| 387 | SourceLocation LOpen = ConsumeToken(); |
| 388 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 389 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 390 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 391 | getOpenMPClauseName(Kind))) |
| 392 | return 0; |
| 393 | |
| 394 | SmallVector<Expr *, 5> Vars; |
| 395 | bool IsComma = true; |
| 396 | while (IsComma || (Tok.isNot(tok::r_paren) && |
| 397 | Tok.isNot(tok::annot_pragma_openmp_end))) { |
| 398 | // Parse variable |
| 399 | ExprResult VarExpr = ParseAssignmentExpression(); |
| 400 | if (VarExpr.isUsable()) { |
| 401 | Vars.push_back(VarExpr.take()); |
| 402 | } else { |
| 403 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 404 | StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 405 | } |
| 406 | // Skip ',' if any |
| 407 | IsComma = Tok.is(tok::comma); |
| 408 | if (IsComma) { |
| 409 | ConsumeToken(); |
| 410 | } else if (Tok.isNot(tok::r_paren) && |
| 411 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 412 | Diag(Tok, diag::err_omp_expected_punc) |
David Majnemer | 8896981 | 2014-02-10 19:06:37 +0000 | [diff] [blame] | 413 | << getOpenMPClauseName(Kind); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | // Parse ')'. |
| 418 | T.consumeClose(); |
| 419 | if (Vars.empty()) |
| 420 | return 0; |
| 421 | |
| 422 | return Actions.ActOnOpenMPVarListClause(Kind, Vars, Loc, LOpen, |
| 423 | Tok.getLocation()); |
| 424 | } |
| 425 | |