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 | |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 14 | #include "RAIIObjectsForParser.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 18 | #include "clang/Parse/ParseDiagnostic.h" |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Parser.h" |
| 20 | #include "clang/Sema/Scope.h" |
| 21 | #include "llvm/ADT/PointerIntPair.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // OpenMP declarative directives. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 28 | /// \brief Parsing of declarative OpenMP directives. |
| 29 | /// |
| 30 | /// threadprivate-directive: |
| 31 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 32 | /// |
| 33 | Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() { |
| 34 | assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 35 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 36 | |
| 37 | SourceLocation Loc = ConsumeToken(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 38 | SmallVector<Expr *, 5> Identifiers; |
| 39 | OpenMPDirectiveKind DKind = Tok.isAnnotation() ? |
| 40 | OMPD_unknown : |
| 41 | getOpenMPDirectiveKind(PP.getSpelling(Tok)); |
| 42 | |
| 43 | switch (DKind) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 44 | case OMPD_threadprivate: |
| 45 | ConsumeToken(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 46 | if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 47 | // The last seen token is annot_pragma_openmp_end - need to check for |
| 48 | // extra tokens. |
| 49 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 50 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
| 51 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 52 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 53 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 54 | // Skip the last annot_pragma_openmp_end. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 55 | ConsumeToken(); |
| 56 | return Actions.ActOnOpenMPThreadprivateDirective(Loc, |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 57 | Identifiers); |
| 58 | } |
| 59 | break; |
| 60 | case OMPD_unknown: |
| 61 | Diag(Tok, diag::err_omp_unknown_directive); |
| 62 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 63 | case OMPD_parallel: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 64 | case OMPD_simd: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 65 | case OMPD_task: |
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; |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 88 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1> |
| 89 | FirstClauses(OMPC_unknown + 1); |
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 | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 151 | Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope()); |
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: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 179 | Diag(Tok, diag::err_omp_unexpected_directive) |
| 180 | << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 181 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | return Directive; |
| 185 | } |
| 186 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 187 | /// \brief Parses list of simple variables for '#pragma omp threadprivate' |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 188 | /// directive. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 189 | /// |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 190 | /// simple-variable-list: |
| 191 | /// '(' id-expression {, id-expression} ')' |
| 192 | /// |
| 193 | bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind, |
| 194 | SmallVectorImpl<Expr *> &VarList, |
| 195 | bool AllowScopeSpecifier) { |
| 196 | VarList.clear(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 197 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 198 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 199 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 200 | getOpenMPDirectiveName(Kind))) |
| 201 | return true; |
| 202 | bool IsCorrect = true; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 203 | bool NoIdentIsFound = true; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 204 | |
| 205 | // Read tokens while ')' or annot_pragma_openmp_end is not found. |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 206 | 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] | 207 | CXXScopeSpec SS; |
| 208 | SourceLocation TemplateKWLoc; |
| 209 | UnqualifiedId Name; |
| 210 | // Read var name. |
| 211 | Token PrevTok = Tok; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 212 | NoIdentIsFound = false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 213 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 214 | if (AllowScopeSpecifier && getLangOpts().CPlusPlus && |
| 215 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 216 | IsCorrect = false; |
| 217 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 218 | StopBeforeMatch); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 219 | } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(), |
| 220 | TemplateKWLoc, Name)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 221 | IsCorrect = false; |
| 222 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 223 | StopBeforeMatch); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 224 | } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && |
| 225 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 226 | IsCorrect = false; |
| 227 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 228 | StopBeforeMatch); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 229 | Diag(PrevTok.getLocation(), diag::err_expected) |
| 230 | << tok::identifier |
| 231 | << SourceRange(PrevTok.getLocation(), PrevTokLocation); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 232 | } else { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 233 | DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name); |
| 234 | ExprResult Res = Actions.ActOnOpenMPIdExpression(getCurScope(), SS, |
| 235 | NameInfo); |
| 236 | if (Res.isUsable()) |
| 237 | VarList.push_back(Res.take()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 238 | } |
| 239 | // Consume ','. |
| 240 | if (Tok.is(tok::comma)) { |
| 241 | ConsumeToken(); |
| 242 | } |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 245 | if (NoIdentIsFound) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 246 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 247 | IsCorrect = false; |
| 248 | } |
| 249 | |
| 250 | // Parse ')'. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 251 | IsCorrect = !T.consumeClose() && IsCorrect; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 252 | |
| 253 | return !IsCorrect && VarList.empty(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 254 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 255 | |
| 256 | /// \brief Parsing of OpenMP clauses. |
| 257 | /// |
| 258 | /// clause: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 259 | /// if-clause | num_threads-clause | safelen-clause | default-clause | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame^] | 260 | /// private-clause | firstprivate-clause | shared-clause | linear-clause | |
| 261 | /// collapse-clause |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 262 | /// |
| 263 | OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, |
| 264 | OpenMPClauseKind CKind, bool FirstClause) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 265 | OMPClause *Clause = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 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) { |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 275 | case OMPC_if: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 276 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 277 | case OMPC_safelen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame^] | 278 | case OMPC_collapse: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 279 | // OpenMP [2.5, Restrictions] |
| 280 | // At most one if clause can appear on the directive. |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 281 | // At most one num_threads clause can appear on the directive. |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 282 | // OpenMP [2.8.1, simd construct, Restrictions] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame^] | 283 | // Only one safelen clause can appear on a simd directive. |
| 284 | // Only one collapse clause can appear on a simd directive. |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 285 | if (!FirstClause) { |
| 286 | Diag(Tok, diag::err_omp_more_one_clause) |
| 287 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind); |
| 288 | } |
| 289 | |
| 290 | Clause = ParseOpenMPSingleExprClause(CKind); |
| 291 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 292 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 293 | case OMPC_proc_bind: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 294 | // OpenMP [2.14.3.1, Restrictions] |
| 295 | // Only a single default clause may be specified on a parallel, task or |
| 296 | // teams directive. |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 297 | // OpenMP [2.5, parallel Construct, Restrictions] |
| 298 | // At most one proc_bind clause can appear on the directive. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 299 | if (!FirstClause) { |
| 300 | Diag(Tok, diag::err_omp_more_one_clause) |
| 301 | << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind); |
| 302 | } |
| 303 | |
| 304 | Clause = ParseOpenMPSimpleClause(CKind); |
| 305 | break; |
| 306 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 307 | case OMPC_firstprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 308 | case OMPC_shared: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 309 | case OMPC_linear: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 310 | case OMPC_copyin: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 311 | Clause = ParseOpenMPVarListClause(CKind); |
| 312 | break; |
| 313 | case OMPC_unknown: |
| 314 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
| 315 | << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 316 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 317 | break; |
| 318 | case OMPC_threadprivate: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 319 | Diag(Tok, diag::err_omp_unexpected_clause) |
| 320 | << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 321 | SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 324 | return ErrorFound ? nullptr : Clause; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 327 | /// \brief Parsing of OpenMP clauses with single expressions like 'if', |
| 328 | /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or |
| 329 | /// 'thread_limit'. |
| 330 | /// |
| 331 | /// if-clause: |
| 332 | /// 'if' '(' expression ')' |
| 333 | /// |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 334 | /// num_threads-clause: |
| 335 | /// 'num_threads' '(' expression ')' |
| 336 | /// |
| 337 | /// safelen-clause: |
| 338 | /// 'safelen' '(' expression ')' |
| 339 | /// |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame^] | 340 | /// collapse-clause: |
| 341 | /// 'collapse' '(' expression ')' |
| 342 | /// |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 343 | OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) { |
| 344 | SourceLocation Loc = ConsumeToken(); |
| 345 | |
| 346 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 347 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 348 | getOpenMPClauseName(Kind))) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 349 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 350 | |
| 351 | ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); |
| 352 | ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
| 353 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 354 | // Parse ')'. |
| 355 | T.consumeClose(); |
| 356 | |
| 357 | if (Val.isInvalid()) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 358 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 359 | |
| 360 | return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc, |
| 361 | T.getOpenLocation(), |
| 362 | T.getCloseLocation()); |
| 363 | } |
| 364 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 365 | /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 366 | /// |
| 367 | /// default-clause: |
| 368 | /// 'default' '(' 'none' | 'shared' ') |
| 369 | /// |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 370 | /// proc_bind-clause: |
| 371 | /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') |
| 372 | /// |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 373 | OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) { |
| 374 | SourceLocation Loc = Tok.getLocation(); |
| 375 | SourceLocation LOpen = ConsumeToken(); |
| 376 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 377 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 378 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 379 | getOpenMPClauseName(Kind))) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 380 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 381 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 382 | unsigned Type = |
| 383 | getOpenMPSimpleClauseType(Kind, |
| 384 | Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 385 | SourceLocation TypeLoc = Tok.getLocation(); |
| 386 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
| 387 | Tok.isNot(tok::annot_pragma_openmp_end)) |
| 388 | ConsumeAnyToken(); |
| 389 | |
| 390 | // Parse ')'. |
| 391 | T.consumeClose(); |
| 392 | |
| 393 | return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, |
| 394 | Tok.getLocation()); |
| 395 | } |
| 396 | |
| 397 | /// \brief Parsing of OpenMP clause 'private', 'firstprivate', |
| 398 | /// 'shared', 'copyin', or 'reduction'. |
| 399 | /// |
| 400 | /// private-clause: |
| 401 | /// 'private' '(' list ')' |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 402 | /// firstprivate-clause: |
| 403 | /// 'firstprivate' '(' list ')' |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 404 | /// shared-clause: |
| 405 | /// 'shared' '(' list ')' |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 406 | /// linear-clause: |
| 407 | /// 'linear' '(' list [ ':' linear-step ] ')' |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 408 | /// |
| 409 | OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) { |
| 410 | SourceLocation Loc = Tok.getLocation(); |
| 411 | SourceLocation LOpen = ConsumeToken(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 412 | SourceLocation ColonLoc = SourceLocation(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 413 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 414 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 415 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 416 | getOpenMPClauseName(Kind))) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 417 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 418 | |
| 419 | SmallVector<Expr *, 5> Vars; |
| 420 | bool IsComma = true; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 421 | const bool MayHaveTail = (Kind == OMPC_linear); |
| 422 | while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) && |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 423 | Tok.isNot(tok::annot_pragma_openmp_end))) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 424 | ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 425 | // Parse variable |
| 426 | ExprResult VarExpr = ParseAssignmentExpression(); |
| 427 | if (VarExpr.isUsable()) { |
| 428 | Vars.push_back(VarExpr.take()); |
| 429 | } else { |
| 430 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 431 | StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 432 | } |
| 433 | // Skip ',' if any |
| 434 | IsComma = Tok.is(tok::comma); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 435 | if (IsComma) |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 436 | ConsumeToken(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 437 | else if (Tok.isNot(tok::r_paren) && |
| 438 | Tok.isNot(tok::annot_pragma_openmp_end) && |
| 439 | (!MayHaveTail || Tok.isNot(tok::colon))) |
| 440 | Diag(Tok, diag::err_omp_expected_punc) << getOpenMPClauseName(Kind); |
| 441 | } |
| 442 | |
| 443 | // Parse ':' linear-step |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 444 | Expr *TailExpr = nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 445 | const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon); |
| 446 | if (MustHaveTail) { |
| 447 | ColonLoc = Tok.getLocation(); |
| 448 | ConsumeToken(); |
| 449 | ExprResult Tail = ParseAssignmentExpression(); |
| 450 | if (Tail.isUsable()) |
| 451 | TailExpr = Tail.take(); |
| 452 | else |
| 453 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
| 454 | StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Parse ')'. |
| 458 | T.consumeClose(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 459 | if (Vars.empty() || (MustHaveTail && !TailExpr)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 460 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 461 | |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 462 | return Actions.ActOnOpenMPVarListClause(Kind, Vars, TailExpr, Loc, LOpen, |
| 463 | ColonLoc, Tok.getLocation()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 464 | } |
| 465 | |