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 | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 28 | static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) { |
| 29 | auto Tok = P.getCurToken(); |
| 30 | auto DKind = |
| 31 | Tok.isAnnotation() |
| 32 | ? OMPD_unknown |
| 33 | : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok)); |
| 34 | if (DKind == OMPD_parallel) { |
| 35 | Tok = P.getPreprocessor().LookAhead(0); |
| 36 | auto SDKind = |
| 37 | Tok.isAnnotation() |
| 38 | ? OMPD_unknown |
| 39 | : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok)); |
| 40 | if (SDKind == OMPD_for) { |
| 41 | P.ConsumeToken(); |
| 42 | DKind = OMPD_parallel_for; |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 43 | } else if (SDKind == OMPD_sections) { |
| 44 | P.ConsumeToken(); |
| 45 | DKind = OMPD_parallel_sections; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | return DKind; |
| 49 | } |
| 50 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 51 | /// \brief Parsing of declarative OpenMP directives. |
| 52 | /// |
| 53 | /// threadprivate-directive: |
| 54 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 55 | /// |
| 56 | Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() { |
| 57 | assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 58 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 59 | |
| 60 | SourceLocation Loc = ConsumeToken(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 61 | SmallVector<Expr *, 5> Identifiers; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 62 | auto DKind = ParseOpenMPDirectiveKind(*this); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 63 | |
| 64 | switch (DKind) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 65 | case OMPD_threadprivate: |
| 66 | ConsumeToken(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 67 | if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, true)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 68 | // The last seen token is annot_pragma_openmp_end - need to check for |
| 69 | // extra tokens. |
| 70 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 71 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 72 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 73 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 74 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 75 | // Skip the last annot_pragma_openmp_end. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 76 | ConsumeToken(); |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 77 | return Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 78 | } |
| 79 | break; |
| 80 | case OMPD_unknown: |
| 81 | Diag(Tok, diag::err_omp_unknown_directive); |
| 82 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 83 | case OMPD_parallel: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 84 | case OMPD_simd: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 85 | case OMPD_task: |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 86 | case OMPD_taskyield: |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 87 | case OMPD_barrier: |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 88 | case OMPD_taskwait: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 89 | case OMPD_flush: |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 90 | case OMPD_for: |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 91 | case OMPD_sections: |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 92 | case OMPD_section: |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 93 | case OMPD_single: |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 94 | case OMPD_master: |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 95 | case OMPD_ordered: |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 96 | case OMPD_critical: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 97 | case OMPD_parallel_for: |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 98 | case OMPD_parallel_sections: |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame^] | 99 | case OMPD_atomic: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 100 | Diag(Tok, diag::err_omp_unexpected_directive) |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 101 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 102 | break; |
| 103 | } |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 104 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 105 | return DeclGroupPtrTy(); |
| 106 | } |
| 107 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 108 | /// \brief Parsing of declarative or executable OpenMP directives. |
| 109 | /// |
| 110 | /// threadprivate-directive: |
| 111 | /// annot_pragma_openmp 'threadprivate' simple-variable-list |
| 112 | /// annot_pragma_openmp_end |
| 113 | /// |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 114 | /// executable-directive: |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 115 | /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 116 | /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] | |
| 117 | /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame^] | 118 | /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' {clause} |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 119 | /// annot_pragma_openmp_end |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 120 | /// |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 121 | StmtResult |
| 122 | Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 123 | assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!"); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 124 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 125 | SmallVector<Expr *, 5> Identifiers; |
| 126 | SmallVector<OMPClause *, 5> Clauses; |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 127 | SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1> |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 128 | FirstClauses(OMPC_unknown + 1); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 129 | unsigned ScopeFlags = |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 130 | Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 131 | SourceLocation Loc = ConsumeToken(), EndLoc; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 132 | auto DKind = ParseOpenMPDirectiveKind(*this); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 133 | // Name of critical directive. |
| 134 | DeclarationNameInfo DirName; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 135 | StmtResult Directive = StmtError(); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 136 | bool HasAssociatedStatement = true; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 137 | bool FlushHasClause = false; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 138 | |
| 139 | switch (DKind) { |
| 140 | case OMPD_threadprivate: |
| 141 | ConsumeToken(); |
| 142 | if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Identifiers, false)) { |
| 143 | // The last seen token is annot_pragma_openmp_end - need to check for |
| 144 | // extra tokens. |
| 145 | if (Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 146 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 147 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 148 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 149 | } |
| 150 | DeclGroupPtrTy Res = |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 151 | Actions.ActOnOpenMPThreadprivateDirective(Loc, Identifiers); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 152 | Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation()); |
| 153 | } |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 154 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 155 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 156 | case OMPD_flush: |
| 157 | if (PP.LookAhead(0).is(tok::l_paren)) { |
| 158 | FlushHasClause = true; |
| 159 | // Push copy of the current token back to stream to properly parse |
| 160 | // pseudo-clause OMPFlushClause. |
| 161 | PP.EnterToken(Tok); |
| 162 | } |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 163 | case OMPD_taskyield: |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 164 | case OMPD_barrier: |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 165 | case OMPD_taskwait: |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 166 | if (!StandAloneAllowed) { |
| 167 | Diag(Tok, diag::err_omp_immediate_directive) |
| 168 | << getOpenMPDirectiveName(DKind); |
| 169 | } |
| 170 | HasAssociatedStatement = false; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 171 | // Fall through for further analysis. |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 172 | case OMPD_parallel: |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 173 | case OMPD_simd: |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 174 | case OMPD_for: |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 175 | case OMPD_sections: |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 176 | case OMPD_single: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 177 | case OMPD_section: |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 178 | case OMPD_master: |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 179 | case OMPD_critical: |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 180 | case OMPD_parallel_for: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 181 | case OMPD_parallel_sections: |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 182 | case OMPD_task: |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame^] | 183 | case OMPD_ordered: |
| 184 | case OMPD_atomic: { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 185 | ConsumeToken(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 186 | // Parse directive name of the 'critical' directive if any. |
| 187 | if (DKind == OMPD_critical) { |
| 188 | BalancedDelimiterTracker T(*this, tok::l_paren, |
| 189 | tok::annot_pragma_openmp_end); |
| 190 | if (!T.consumeOpen()) { |
| 191 | if (Tok.isAnyIdentifier()) { |
| 192 | DirName = |
| 193 | DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 194 | ConsumeAnyToken(); |
| 195 | } else { |
| 196 | Diag(Tok, diag::err_omp_expected_identifier_for_critical); |
| 197 | } |
| 198 | T.consumeClose(); |
| 199 | } |
| 200 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 201 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 202 | if (isOpenMPLoopDirective(DKind)) |
| 203 | ScopeFlags |= Scope::OpenMPLoopDirectiveScope; |
| 204 | if (isOpenMPSimdDirective(DKind)) |
| 205 | ScopeFlags |= Scope::OpenMPSimdDirectiveScope; |
| 206 | ParseScope OMPDirectiveScope(this, ScopeFlags); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 207 | Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 208 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 209 | while (Tok.isNot(tok::annot_pragma_openmp_end)) { |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 210 | OpenMPClauseKind CKind = |
| 211 | Tok.isAnnotation() |
| 212 | ? OMPC_unknown |
| 213 | : FlushHasClause ? OMPC_flush |
| 214 | : getOpenMPClauseKind(PP.getSpelling(Tok)); |
| 215 | FlushHasClause = false; |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 216 | OMPClause *Clause = |
| 217 | ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 218 | FirstClauses[CKind].setInt(true); |
| 219 | if (Clause) { |
| 220 | FirstClauses[CKind].setPointer(Clause); |
| 221 | Clauses.push_back(Clause); |
| 222 | } |
| 223 | |
| 224 | // Skip ',' if any. |
| 225 | if (Tok.is(tok::comma)) |
| 226 | ConsumeToken(); |
| 227 | } |
| 228 | // End location of the directive. |
| 229 | EndLoc = Tok.getLocation(); |
| 230 | // Consume final annot_pragma_openmp_end. |
| 231 | ConsumeToken(); |
| 232 | |
| 233 | StmtResult AssociatedStmt; |
| 234 | bool CreateDirective = true; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 235 | if (HasAssociatedStatement) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 236 | // The body is a block scope like in Lambdas and Blocks. |
| 237 | Sema::CompoundScopeRAII CompoundScope(Actions); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 238 | Actions.ActOnOpenMPRegionStart(DKind, getCurScope()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 239 | Actions.ActOnStartOfCompoundStmt(); |
| 240 | // Parse statement |
| 241 | AssociatedStmt = ParseStatement(); |
| 242 | Actions.ActOnFinishOfCompoundStmt(); |
| 243 | if (!AssociatedStmt.isUsable()) { |
| 244 | Actions.ActOnCapturedRegionError(); |
| 245 | CreateDirective = false; |
| 246 | } else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 247 | AssociatedStmt = Actions.ActOnCapturedRegionEnd(AssociatedStmt.get()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 248 | CreateDirective = AssociatedStmt.isUsable(); |
| 249 | } |
| 250 | } |
| 251 | if (CreateDirective) |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 252 | Directive = Actions.ActOnOpenMPExecutableDirective( |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 253 | DKind, DirName, Clauses, AssociatedStmt.get(), Loc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 254 | |
| 255 | // Exit scope. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 256 | Actions.EndOpenMPDSABlock(Directive.get()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 257 | OMPDirectiveScope.Exit(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 258 | break; |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 259 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 260 | case OMPD_unknown: |
| 261 | Diag(Tok, diag::err_omp_unknown_directive); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 262 | SkipUntil(tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 263 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 264 | } |
| 265 | return Directive; |
| 266 | } |
| 267 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 268 | /// \brief Parses list of simple variables for '#pragma omp threadprivate' |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 269 | /// directive. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 270 | /// |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 271 | /// simple-variable-list: |
| 272 | /// '(' id-expression {, id-expression} ')' |
| 273 | /// |
| 274 | bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind, |
| 275 | SmallVectorImpl<Expr *> &VarList, |
| 276 | bool AllowScopeSpecifier) { |
| 277 | VarList.clear(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 278 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 279 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 280 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 281 | getOpenMPDirectiveName(Kind))) |
| 282 | return true; |
| 283 | bool IsCorrect = true; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 284 | bool NoIdentIsFound = true; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 285 | |
| 286 | // Read tokens while ')' or annot_pragma_openmp_end is not found. |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 287 | 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] | 288 | CXXScopeSpec SS; |
| 289 | SourceLocation TemplateKWLoc; |
| 290 | UnqualifiedId Name; |
| 291 | // Read var name. |
| 292 | Token PrevTok = Tok; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 293 | NoIdentIsFound = false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 294 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 295 | if (AllowScopeSpecifier && getLangOpts().CPlusPlus && |
| 296 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 297 | IsCorrect = false; |
| 298 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 299 | StopBeforeMatch); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 300 | } else if (ParseUnqualifiedId(SS, false, false, false, ParsedType(), |
| 301 | TemplateKWLoc, Name)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 302 | IsCorrect = false; |
| 303 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 304 | StopBeforeMatch); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 305 | } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) && |
| 306 | Tok.isNot(tok::annot_pragma_openmp_end)) { |
| 307 | IsCorrect = false; |
| 308 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 309 | StopBeforeMatch); |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 310 | Diag(PrevTok.getLocation(), diag::err_expected) |
| 311 | << tok::identifier |
| 312 | << SourceRange(PrevTok.getLocation(), PrevTokLocation); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 313 | } else { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 314 | DeclarationNameInfo NameInfo = Actions.GetNameFromUnqualifiedId(Name); |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 315 | ExprResult Res = |
| 316 | Actions.ActOnOpenMPIdExpression(getCurScope(), SS, NameInfo); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 317 | if (Res.isUsable()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 318 | VarList.push_back(Res.get()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 319 | } |
| 320 | // Consume ','. |
| 321 | if (Tok.is(tok::comma)) { |
| 322 | ConsumeToken(); |
| 323 | } |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 326 | if (NoIdentIsFound) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 327 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 328 | IsCorrect = false; |
| 329 | } |
| 330 | |
| 331 | // Parse ')'. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 332 | IsCorrect = !T.consumeClose() && IsCorrect; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 333 | |
| 334 | return !IsCorrect && VarList.empty(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 335 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 336 | |
| 337 | /// \brief Parsing of OpenMP clauses. |
| 338 | /// |
| 339 | /// clause: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 340 | /// if-clause | final-clause | num_threads-clause | safelen-clause | |
| 341 | /// default-clause | private-clause | firstprivate-clause | shared-clause |
| 342 | /// | linear-clause | aligned-clause | collapse-clause | |
| 343 | /// lastprivate-clause | reduction-clause | proc_bind-clause | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 344 | /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 345 | /// mergeable-clause | flush-clause |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 346 | /// |
| 347 | OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind, |
| 348 | OpenMPClauseKind CKind, bool FirstClause) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 349 | OMPClause *Clause = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 350 | bool ErrorFound = false; |
| 351 | // Check if clause is allowed for the given directive. |
| 352 | if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) { |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 353 | Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind) |
| 354 | << getOpenMPDirectiveName(DKind); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 355 | ErrorFound = true; |
| 356 | } |
| 357 | |
| 358 | switch (CKind) { |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 359 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 360 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 361 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 362 | case OMPC_safelen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 363 | case OMPC_collapse: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 364 | // OpenMP [2.5, Restrictions] |
| 365 | // At most one if clause can appear on the directive. |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 366 | // At most one num_threads clause can appear on the directive. |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 367 | // OpenMP [2.8.1, simd construct, Restrictions] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 368 | // Only one safelen clause can appear on a simd directive. |
| 369 | // Only one collapse clause can appear on a simd directive. |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 370 | // OpenMP [2.11.1, task Construct, Restrictions] |
| 371 | // At most one if clause can appear on the directive. |
| 372 | // At most one final clause can appear on the directive. |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 373 | if (!FirstClause) { |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 374 | Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind) |
| 375 | << getOpenMPClauseName(CKind); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | Clause = ParseOpenMPSingleExprClause(CKind); |
| 379 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 380 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 381 | case OMPC_proc_bind: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 382 | // OpenMP [2.14.3.1, Restrictions] |
| 383 | // Only a single default clause may be specified on a parallel, task or |
| 384 | // teams directive. |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 385 | // OpenMP [2.5, parallel Construct, Restrictions] |
| 386 | // At most one proc_bind clause can appear on the directive. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 387 | if (!FirstClause) { |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 388 | Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind) |
| 389 | << getOpenMPClauseName(CKind); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | Clause = ParseOpenMPSimpleClause(CKind); |
| 393 | break; |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 394 | case OMPC_schedule: |
| 395 | // OpenMP [2.7.1, Restrictions, p. 3] |
| 396 | // Only one schedule clause can appear on a loop directive. |
| 397 | if (!FirstClause) { |
| 398 | Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind) |
| 399 | << getOpenMPClauseName(CKind); |
| 400 | } |
| 401 | |
| 402 | Clause = ParseOpenMPSingleExprWithArgClause(CKind); |
| 403 | break; |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 404 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 405 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 406 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 407 | case OMPC_mergeable: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 408 | // OpenMP [2.7.1, Restrictions, p. 9] |
| 409 | // Only one ordered clause can appear on a loop directive. |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 410 | // OpenMP [2.7.1, Restrictions, C/C++, p. 4] |
| 411 | // Only one nowait clause can appear on a for directive. |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 412 | if (!FirstClause) { |
| 413 | Diag(Tok, diag::err_omp_more_one_clause) << getOpenMPDirectiveName(DKind) |
| 414 | << getOpenMPClauseName(CKind); |
| 415 | } |
| 416 | |
| 417 | Clause = ParseOpenMPClause(CKind); |
| 418 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 419 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 420 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 421 | case OMPC_lastprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 422 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 423 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 424 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 425 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 426 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 427 | case OMPC_copyprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 428 | case OMPC_flush: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 429 | Clause = ParseOpenMPVarListClause(CKind); |
| 430 | break; |
| 431 | case OMPC_unknown: |
| 432 | Diag(Tok, diag::warn_omp_extra_tokens_at_eol) |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 433 | << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 434 | SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 435 | break; |
| 436 | case OMPC_threadprivate: |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 437 | Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind) |
| 438 | << getOpenMPDirectiveName(DKind); |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 439 | SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 440 | break; |
| 441 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 442 | return ErrorFound ? nullptr : Clause; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 445 | /// \brief Parsing of OpenMP clauses with single expressions like 'if', |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 446 | /// 'final', 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 447 | /// 'thread_limit'. |
| 448 | /// |
| 449 | /// if-clause: |
| 450 | /// 'if' '(' expression ')' |
| 451 | /// |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 452 | /// final-clause: |
| 453 | /// 'final' '(' expression ')' |
| 454 | /// |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 455 | /// num_threads-clause: |
| 456 | /// 'num_threads' '(' expression ')' |
| 457 | /// |
| 458 | /// safelen-clause: |
| 459 | /// 'safelen' '(' expression ')' |
| 460 | /// |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 461 | /// collapse-clause: |
| 462 | /// 'collapse' '(' expression ')' |
| 463 | /// |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 464 | OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) { |
| 465 | SourceLocation Loc = ConsumeToken(); |
| 466 | |
| 467 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 468 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 469 | getOpenMPClauseName(Kind))) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 470 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 471 | |
| 472 | ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); |
| 473 | ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); |
| 474 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 475 | // Parse ')'. |
| 476 | T.consumeClose(); |
| 477 | |
| 478 | if (Val.isInvalid()) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 479 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 480 | |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 481 | return Actions.ActOnOpenMPSingleExprClause( |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 482 | Kind, Val.get(), Loc, T.getOpenLocation(), T.getCloseLocation()); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 485 | /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 486 | /// |
| 487 | /// default-clause: |
| 488 | /// 'default' '(' 'none' | 'shared' ') |
| 489 | /// |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 490 | /// proc_bind-clause: |
| 491 | /// 'proc_bind' '(' 'master' | 'close' | 'spread' ') |
| 492 | /// |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 493 | OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) { |
| 494 | SourceLocation Loc = Tok.getLocation(); |
| 495 | SourceLocation LOpen = ConsumeToken(); |
| 496 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 497 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 498 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 499 | getOpenMPClauseName(Kind))) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 500 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 501 | |
Alexey Bataev | a55ed26 | 2014-05-28 06:15:33 +0000 | [diff] [blame] | 502 | unsigned Type = getOpenMPSimpleClauseType( |
| 503 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 504 | SourceLocation TypeLoc = Tok.getLocation(); |
| 505 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
| 506 | Tok.isNot(tok::annot_pragma_openmp_end)) |
| 507 | ConsumeAnyToken(); |
| 508 | |
| 509 | // Parse ')'. |
| 510 | T.consumeClose(); |
| 511 | |
| 512 | return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc, |
| 513 | Tok.getLocation()); |
| 514 | } |
| 515 | |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 516 | /// \brief Parsing of OpenMP clauses like 'ordered'. |
| 517 | /// |
| 518 | /// ordered-clause: |
| 519 | /// 'ordered' |
| 520 | /// |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 521 | /// nowait-clause: |
| 522 | /// 'nowait' |
| 523 | /// |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 524 | /// untied-clause: |
| 525 | /// 'untied' |
| 526 | /// |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 527 | /// mergeable-clause: |
| 528 | /// 'mergeable' |
| 529 | /// |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 530 | OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) { |
| 531 | SourceLocation Loc = Tok.getLocation(); |
| 532 | ConsumeAnyToken(); |
| 533 | |
| 534 | return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation()); |
| 535 | } |
| 536 | |
| 537 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 538 | /// \brief Parsing of OpenMP clauses with single expressions and some additional |
| 539 | /// argument like 'schedule' or 'dist_schedule'. |
| 540 | /// |
| 541 | /// schedule-clause: |
| 542 | /// 'schedule' '(' kind [',' expression ] ')' |
| 543 | /// |
| 544 | OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind) { |
| 545 | SourceLocation Loc = ConsumeToken(); |
| 546 | SourceLocation CommaLoc; |
| 547 | // Parse '('. |
| 548 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
| 549 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 550 | getOpenMPClauseName(Kind))) |
| 551 | return nullptr; |
| 552 | |
| 553 | ExprResult Val; |
| 554 | unsigned Type = getOpenMPSimpleClauseType( |
| 555 | Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)); |
| 556 | SourceLocation KLoc = Tok.getLocation(); |
| 557 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) && |
| 558 | Tok.isNot(tok::annot_pragma_openmp_end)) |
| 559 | ConsumeAnyToken(); |
| 560 | |
| 561 | if (Kind == OMPC_schedule && |
| 562 | (Type == OMPC_SCHEDULE_static || Type == OMPC_SCHEDULE_dynamic || |
| 563 | Type == OMPC_SCHEDULE_guided) && |
| 564 | Tok.is(tok::comma)) { |
| 565 | CommaLoc = ConsumeAnyToken(); |
| 566 | ExprResult LHS(ParseCastExpression(false, false, NotTypeCast)); |
| 567 | Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional); |
| 568 | if (Val.isInvalid()) |
| 569 | return nullptr; |
| 570 | } |
| 571 | |
| 572 | // Parse ')'. |
| 573 | T.consumeClose(); |
| 574 | |
| 575 | return Actions.ActOnOpenMPSingleExprWithArgClause( |
| 576 | Kind, Type, Val.get(), Loc, T.getOpenLocation(), KLoc, CommaLoc, |
| 577 | T.getCloseLocation()); |
| 578 | } |
| 579 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 580 | static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec, |
| 581 | UnqualifiedId &ReductionId) { |
| 582 | SourceLocation TemplateKWLoc; |
| 583 | if (ReductionIdScopeSpec.isEmpty()) { |
| 584 | auto OOK = OO_None; |
| 585 | switch (P.getCurToken().getKind()) { |
| 586 | case tok::plus: |
| 587 | OOK = OO_Plus; |
| 588 | break; |
| 589 | case tok::minus: |
| 590 | OOK = OO_Minus; |
| 591 | break; |
| 592 | case tok::star: |
| 593 | OOK = OO_Star; |
| 594 | break; |
| 595 | case tok::amp: |
| 596 | OOK = OO_Amp; |
| 597 | break; |
| 598 | case tok::pipe: |
| 599 | OOK = OO_Pipe; |
| 600 | break; |
| 601 | case tok::caret: |
| 602 | OOK = OO_Caret; |
| 603 | break; |
| 604 | case tok::ampamp: |
| 605 | OOK = OO_AmpAmp; |
| 606 | break; |
| 607 | case tok::pipepipe: |
| 608 | OOK = OO_PipePipe; |
| 609 | break; |
| 610 | default: |
| 611 | break; |
| 612 | } |
| 613 | if (OOK != OO_None) { |
| 614 | SourceLocation OpLoc = P.ConsumeToken(); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 615 | SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()}; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 616 | ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations); |
| 617 | return false; |
| 618 | } |
| 619 | } |
| 620 | return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false, |
| 621 | /*AllowDestructorName*/ false, |
| 622 | /*AllowConstructorName*/ false, ParsedType(), |
| 623 | TemplateKWLoc, ReductionId); |
| 624 | } |
| 625 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 626 | /// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate', |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 627 | /// 'shared', 'copyin', 'copyprivate', 'flush' or 'reduction'. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 628 | /// |
| 629 | /// private-clause: |
| 630 | /// 'private' '(' list ')' |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 631 | /// firstprivate-clause: |
| 632 | /// 'firstprivate' '(' list ')' |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 633 | /// lastprivate-clause: |
| 634 | /// 'lastprivate' '(' list ')' |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 635 | /// shared-clause: |
| 636 | /// 'shared' '(' list ')' |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 637 | /// linear-clause: |
| 638 | /// 'linear' '(' list [ ':' linear-step ] ')' |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 639 | /// aligned-clause: |
| 640 | /// 'aligned' '(' list [ ':' alignment ] ')' |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 641 | /// reduction-clause: |
| 642 | /// 'reduction' '(' reduction-identifier ':' list ')' |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 643 | /// copyprivate-clause: |
| 644 | /// 'copyprivate' '(' list ')' |
| 645 | /// flush-clause: |
| 646 | /// 'flush' '(' list ')' |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 647 | /// |
| 648 | OMPClause *Parser::ParseOpenMPVarListClause(OpenMPClauseKind Kind) { |
| 649 | SourceLocation Loc = Tok.getLocation(); |
| 650 | SourceLocation LOpen = ConsumeToken(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 651 | SourceLocation ColonLoc = SourceLocation(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 652 | // Optional scope specifier and unqualified id for reduction identifier. |
| 653 | CXXScopeSpec ReductionIdScopeSpec; |
| 654 | UnqualifiedId ReductionId; |
| 655 | bool InvalidReductionId = false; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 656 | // Parse '('. |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 657 | BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 658 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 659 | getOpenMPClauseName(Kind))) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 660 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 661 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 662 | // Handle reduction-identifier for reduction clause. |
| 663 | if (Kind == OMPC_reduction) { |
| 664 | ColonProtectionRAIIObject ColonRAII(*this); |
| 665 | if (getLangOpts().CPlusPlus) { |
| 666 | ParseOptionalCXXScopeSpecifier(ReductionIdScopeSpec, ParsedType(), false); |
| 667 | } |
| 668 | InvalidReductionId = |
| 669 | ParseReductionId(*this, ReductionIdScopeSpec, ReductionId); |
| 670 | if (InvalidReductionId) { |
| 671 | SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end, |
| 672 | StopBeforeMatch); |
| 673 | } |
| 674 | if (Tok.is(tok::colon)) { |
| 675 | ColonLoc = ConsumeToken(); |
| 676 | } else { |
| 677 | Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier"; |
| 678 | } |
| 679 | } |
| 680 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 681 | SmallVector<Expr *, 5> Vars; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 682 | bool IsComma = !InvalidReductionId; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 683 | const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 684 | while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) && |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 685 | Tok.isNot(tok::annot_pragma_openmp_end))) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 686 | ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 687 | // Parse variable |
| 688 | ExprResult VarExpr = ParseAssignmentExpression(); |
| 689 | if (VarExpr.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 690 | Vars.push_back(VarExpr.get()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 691 | } else { |
| 692 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
Alp Toker | d751fa7 | 2013-12-18 19:10:49 +0000 | [diff] [blame] | 693 | StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 694 | } |
| 695 | // Skip ',' if any |
| 696 | IsComma = Tok.is(tok::comma); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 697 | if (IsComma) |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 698 | ConsumeToken(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 699 | else if (Tok.isNot(tok::r_paren) && |
| 700 | Tok.isNot(tok::annot_pragma_openmp_end) && |
| 701 | (!MayHaveTail || Tok.isNot(tok::colon))) |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 702 | Diag(Tok, diag::err_omp_expected_punc) |
| 703 | << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush) |
| 704 | : getOpenMPClauseName(Kind)) |
| 705 | << (Kind == OMPC_flush); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 708 | // Parse ':' linear-step (or ':' alignment). |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 709 | Expr *TailExpr = nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 710 | const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon); |
| 711 | if (MustHaveTail) { |
| 712 | ColonLoc = Tok.getLocation(); |
| 713 | ConsumeToken(); |
| 714 | ExprResult Tail = ParseAssignmentExpression(); |
| 715 | if (Tail.isUsable()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 716 | TailExpr = Tail.get(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 717 | else |
| 718 | SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end, |
| 719 | StopBeforeMatch); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // Parse ')'. |
| 723 | T.consumeClose(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 724 | if (Vars.empty() || (MustHaveTail && !TailExpr) || InvalidReductionId) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 725 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 726 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 727 | return Actions.ActOnOpenMPVarListClause( |
| 728 | Kind, Vars, TailExpr, Loc, LOpen, ColonLoc, Tok.getLocation(), |
| 729 | ReductionIdScopeSpec, |
| 730 | ReductionId.isValid() ? Actions.GetNameFromUnqualifiedId(ReductionId) |
| 731 | : DeclarationNameInfo()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 732 | } |
| 733 | |