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