ParseOpenMP.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  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. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/StmtOpenMP.h"
  15. #include "clang/Parse/ParseDiagnostic.h"
  16. #include "clang/Parse/Parser.h"
  17. #include "clang/Parse/RAIIObjectsForParser.h"
  18. #include "clang/Sema/Scope.h"
  19. #include "llvm/ADT/PointerIntPair.h"
  20. using namespace clang;
  21. //===----------------------------------------------------------------------===//
  22. // OpenMP declarative directives.
  23. //===----------------------------------------------------------------------===//
  24. namespace {
  25. enum OpenMPDirectiveKindEx {
  26. OMPD_cancellation = OMPD_unknown + 1,
  27. OMPD_data,
  28. OMPD_declare,
  29. OMPD_end,
  30. OMPD_end_declare,
  31. OMPD_enter,
  32. OMPD_exit,
  33. OMPD_point,
  34. OMPD_reduction,
  35. OMPD_target_enter,
  36. OMPD_target_exit,
  37. OMPD_update,
  38. OMPD_distribute_parallel,
  39. OMPD_teams_distribute_parallel,
  40. OMPD_target_teams_distribute_parallel
  41. };
  42. class ThreadprivateListParserHelper final {
  43. SmallVector<Expr *, 4> Identifiers;
  44. Parser *P;
  45. public:
  46. ThreadprivateListParserHelper(Parser *P) : P(P) {}
  47. void operator()(CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
  48. ExprResult Res =
  49. P->getActions().ActOnOpenMPIdExpression(P->getCurScope(), SS, NameInfo);
  50. if (Res.isUsable())
  51. Identifiers.push_back(Res.get());
  52. }
  53. llvm::ArrayRef<Expr *> getIdentifiers() const { return Identifiers; }
  54. };
  55. } // namespace
  56. // Map token string to extended OMP token kind that are
  57. // OpenMPDirectiveKind + OpenMPDirectiveKindEx.
  58. static unsigned getOpenMPDirectiveKindEx(StringRef S) {
  59. auto DKind = getOpenMPDirectiveKind(S);
  60. if (DKind != OMPD_unknown)
  61. return DKind;
  62. return llvm::StringSwitch<unsigned>(S)
  63. .Case("cancellation", OMPD_cancellation)
  64. .Case("data", OMPD_data)
  65. .Case("declare", OMPD_declare)
  66. .Case("end", OMPD_end)
  67. .Case("enter", OMPD_enter)
  68. .Case("exit", OMPD_exit)
  69. .Case("point", OMPD_point)
  70. .Case("reduction", OMPD_reduction)
  71. .Case("update", OMPD_update)
  72. .Default(OMPD_unknown);
  73. }
  74. static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
  75. // Array of foldings: F[i][0] F[i][1] ===> F[i][2].
  76. // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd
  77. // TODO: add other combined directives in topological order.
  78. static const unsigned F[][3] = {
  79. {OMPD_cancellation, OMPD_point, OMPD_cancellation_point},
  80. {OMPD_declare, OMPD_reduction, OMPD_declare_reduction},
  81. {OMPD_declare, OMPD_simd, OMPD_declare_simd},
  82. {OMPD_declare, OMPD_target, OMPD_declare_target},
  83. {OMPD_distribute, OMPD_parallel, OMPD_distribute_parallel},
  84. {OMPD_distribute_parallel, OMPD_for, OMPD_distribute_parallel_for},
  85. {OMPD_distribute_parallel_for, OMPD_simd,
  86. OMPD_distribute_parallel_for_simd},
  87. {OMPD_distribute, OMPD_simd, OMPD_distribute_simd},
  88. {OMPD_end, OMPD_declare, OMPD_end_declare},
  89. {OMPD_end_declare, OMPD_target, OMPD_end_declare_target},
  90. {OMPD_target, OMPD_data, OMPD_target_data},
  91. {OMPD_target, OMPD_enter, OMPD_target_enter},
  92. {OMPD_target, OMPD_exit, OMPD_target_exit},
  93. {OMPD_target, OMPD_update, OMPD_target_update},
  94. {OMPD_target_enter, OMPD_data, OMPD_target_enter_data},
  95. {OMPD_target_exit, OMPD_data, OMPD_target_exit_data},
  96. {OMPD_for, OMPD_simd, OMPD_for_simd},
  97. {OMPD_parallel, OMPD_for, OMPD_parallel_for},
  98. {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd},
  99. {OMPD_parallel, OMPD_sections, OMPD_parallel_sections},
  100. {OMPD_taskloop, OMPD_simd, OMPD_taskloop_simd},
  101. {OMPD_target, OMPD_parallel, OMPD_target_parallel},
  102. {OMPD_target, OMPD_simd, OMPD_target_simd},
  103. {OMPD_target_parallel, OMPD_for, OMPD_target_parallel_for},
  104. {OMPD_target_parallel_for, OMPD_simd, OMPD_target_parallel_for_simd},
  105. {OMPD_teams, OMPD_distribute, OMPD_teams_distribute},
  106. {OMPD_teams_distribute, OMPD_simd, OMPD_teams_distribute_simd},
  107. {OMPD_teams_distribute, OMPD_parallel, OMPD_teams_distribute_parallel},
  108. {OMPD_teams_distribute_parallel, OMPD_for,
  109. OMPD_teams_distribute_parallel_for},
  110. {OMPD_teams_distribute_parallel_for, OMPD_simd,
  111. OMPD_teams_distribute_parallel_for_simd},
  112. {OMPD_target, OMPD_teams, OMPD_target_teams},
  113. {OMPD_target_teams, OMPD_distribute, OMPD_target_teams_distribute},
  114. {OMPD_target_teams_distribute, OMPD_parallel,
  115. OMPD_target_teams_distribute_parallel},
  116. {OMPD_target_teams_distribute, OMPD_simd,
  117. OMPD_target_teams_distribute_simd},
  118. {OMPD_target_teams_distribute_parallel, OMPD_for,
  119. OMPD_target_teams_distribute_parallel_for},
  120. {OMPD_target_teams_distribute_parallel_for, OMPD_simd,
  121. OMPD_target_teams_distribute_parallel_for_simd}};
  122. enum { CancellationPoint = 0, DeclareReduction = 1, TargetData = 2 };
  123. Token Tok = P.getCurToken();
  124. unsigned DKind =
  125. Tok.isAnnotation()
  126. ? static_cast<unsigned>(OMPD_unknown)
  127. : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
  128. if (DKind == OMPD_unknown)
  129. return OMPD_unknown;
  130. for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) {
  131. if (DKind != F[I][0])
  132. continue;
  133. Tok = P.getPreprocessor().LookAhead(0);
  134. unsigned SDKind =
  135. Tok.isAnnotation()
  136. ? static_cast<unsigned>(OMPD_unknown)
  137. : getOpenMPDirectiveKindEx(P.getPreprocessor().getSpelling(Tok));
  138. if (SDKind == OMPD_unknown)
  139. continue;
  140. if (SDKind == F[I][1]) {
  141. P.ConsumeToken();
  142. DKind = F[I][2];
  143. }
  144. }
  145. return DKind < OMPD_unknown ? static_cast<OpenMPDirectiveKind>(DKind)
  146. : OMPD_unknown;
  147. }
  148. static DeclarationName parseOpenMPReductionId(Parser &P) {
  149. Token Tok = P.getCurToken();
  150. Sema &Actions = P.getActions();
  151. OverloadedOperatorKind OOK = OO_None;
  152. // Allow to use 'operator' keyword for C++ operators
  153. bool WithOperator = false;
  154. if (Tok.is(tok::kw_operator)) {
  155. P.ConsumeToken();
  156. Tok = P.getCurToken();
  157. WithOperator = true;
  158. }
  159. switch (Tok.getKind()) {
  160. case tok::plus: // '+'
  161. OOK = OO_Plus;
  162. break;
  163. case tok::minus: // '-'
  164. OOK = OO_Minus;
  165. break;
  166. case tok::star: // '*'
  167. OOK = OO_Star;
  168. break;
  169. case tok::amp: // '&'
  170. OOK = OO_Amp;
  171. break;
  172. case tok::pipe: // '|'
  173. OOK = OO_Pipe;
  174. break;
  175. case tok::caret: // '^'
  176. OOK = OO_Caret;
  177. break;
  178. case tok::ampamp: // '&&'
  179. OOK = OO_AmpAmp;
  180. break;
  181. case tok::pipepipe: // '||'
  182. OOK = OO_PipePipe;
  183. break;
  184. case tok::identifier: // identifier
  185. if (!WithOperator)
  186. break;
  187. LLVM_FALLTHROUGH;
  188. default:
  189. P.Diag(Tok.getLocation(), diag::err_omp_expected_reduction_identifier);
  190. P.SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
  191. Parser::StopBeforeMatch);
  192. return DeclarationName();
  193. }
  194. P.ConsumeToken();
  195. auto &DeclNames = Actions.getASTContext().DeclarationNames;
  196. return OOK == OO_None ? DeclNames.getIdentifier(Tok.getIdentifierInfo())
  197. : DeclNames.getCXXOperatorName(OOK);
  198. }
  199. /// \brief Parse 'omp declare reduction' construct.
  200. ///
  201. /// declare-reduction-directive:
  202. /// annot_pragma_openmp 'declare' 'reduction'
  203. /// '(' <reduction_id> ':' <type> {',' <type>} ':' <expression> ')'
  204. /// ['initializer' '(' ('omp_priv' '=' <expression>)|<function_call> ')']
  205. /// annot_pragma_openmp_end
  206. /// <reduction_id> is either a base language identifier or one of the following
  207. /// operators: '+', '-', '*', '&', '|', '^', '&&' and '||'.
  208. ///
  209. Parser::DeclGroupPtrTy
  210. Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) {
  211. // Parse '('.
  212. BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
  213. if (T.expectAndConsume(diag::err_expected_lparen_after,
  214. getOpenMPDirectiveName(OMPD_declare_reduction))) {
  215. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  216. return DeclGroupPtrTy();
  217. }
  218. DeclarationName Name = parseOpenMPReductionId(*this);
  219. if (Name.isEmpty() && Tok.is(tok::annot_pragma_openmp_end))
  220. return DeclGroupPtrTy();
  221. // Consume ':'.
  222. bool IsCorrect = !ExpectAndConsume(tok::colon);
  223. if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
  224. return DeclGroupPtrTy();
  225. IsCorrect = IsCorrect && !Name.isEmpty();
  226. if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end)) {
  227. Diag(Tok.getLocation(), diag::err_expected_type);
  228. IsCorrect = false;
  229. }
  230. if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
  231. return DeclGroupPtrTy();
  232. SmallVector<std::pair<QualType, SourceLocation>, 8> ReductionTypes;
  233. // Parse list of types until ':' token.
  234. do {
  235. ColonProtectionRAIIObject ColonRAII(*this);
  236. SourceRange Range;
  237. TypeResult TR =
  238. ParseTypeName(&Range, DeclaratorContext::PrototypeContext, AS);
  239. if (TR.isUsable()) {
  240. QualType ReductionType =
  241. Actions.ActOnOpenMPDeclareReductionType(Range.getBegin(), TR);
  242. if (!ReductionType.isNull()) {
  243. ReductionTypes.push_back(
  244. std::make_pair(ReductionType, Range.getBegin()));
  245. }
  246. } else {
  247. SkipUntil(tok::comma, tok::colon, tok::annot_pragma_openmp_end,
  248. StopBeforeMatch);
  249. }
  250. if (Tok.is(tok::colon) || Tok.is(tok::annot_pragma_openmp_end))
  251. break;
  252. // Consume ','.
  253. if (ExpectAndConsume(tok::comma)) {
  254. IsCorrect = false;
  255. if (Tok.is(tok::annot_pragma_openmp_end)) {
  256. Diag(Tok.getLocation(), diag::err_expected_type);
  257. return DeclGroupPtrTy();
  258. }
  259. }
  260. } while (Tok.isNot(tok::annot_pragma_openmp_end));
  261. if (ReductionTypes.empty()) {
  262. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  263. return DeclGroupPtrTy();
  264. }
  265. if (!IsCorrect && Tok.is(tok::annot_pragma_openmp_end))
  266. return DeclGroupPtrTy();
  267. // Consume ':'.
  268. if (ExpectAndConsume(tok::colon))
  269. IsCorrect = false;
  270. if (Tok.is(tok::annot_pragma_openmp_end)) {
  271. Diag(Tok.getLocation(), diag::err_expected_expression);
  272. return DeclGroupPtrTy();
  273. }
  274. DeclGroupPtrTy DRD = Actions.ActOnOpenMPDeclareReductionDirectiveStart(
  275. getCurScope(), Actions.getCurLexicalContext(), Name, ReductionTypes, AS);
  276. // Parse <combiner> expression and then parse initializer if any for each
  277. // correct type.
  278. unsigned I = 0, E = ReductionTypes.size();
  279. for (Decl *D : DRD.get()) {
  280. TentativeParsingAction TPA(*this);
  281. ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
  282. Scope::CompoundStmtScope |
  283. Scope::OpenMPDirectiveScope);
  284. // Parse <combiner> expression.
  285. Actions.ActOnOpenMPDeclareReductionCombinerStart(getCurScope(), D);
  286. ExprResult CombinerResult =
  287. Actions.ActOnFinishFullExpr(ParseAssignmentExpression().get(),
  288. D->getLocation(), /*DiscardedValue=*/true);
  289. Actions.ActOnOpenMPDeclareReductionCombinerEnd(D, CombinerResult.get());
  290. if (CombinerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
  291. Tok.isNot(tok::annot_pragma_openmp_end)) {
  292. TPA.Commit();
  293. IsCorrect = false;
  294. break;
  295. }
  296. IsCorrect = !T.consumeClose() && IsCorrect && CombinerResult.isUsable();
  297. ExprResult InitializerResult;
  298. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  299. // Parse <initializer> expression.
  300. if (Tok.is(tok::identifier) &&
  301. Tok.getIdentifierInfo()->isStr("initializer")) {
  302. ConsumeToken();
  303. } else {
  304. Diag(Tok.getLocation(), diag::err_expected) << "'initializer'";
  305. TPA.Commit();
  306. IsCorrect = false;
  307. break;
  308. }
  309. // Parse '('.
  310. BalancedDelimiterTracker T(*this, tok::l_paren,
  311. tok::annot_pragma_openmp_end);
  312. IsCorrect =
  313. !T.expectAndConsume(diag::err_expected_lparen_after, "initializer") &&
  314. IsCorrect;
  315. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  316. ParseScope OMPDRScope(this, Scope::FnScope | Scope::DeclScope |
  317. Scope::CompoundStmtScope |
  318. Scope::OpenMPDirectiveScope);
  319. // Parse expression.
  320. VarDecl *OmpPrivParm =
  321. Actions.ActOnOpenMPDeclareReductionInitializerStart(getCurScope(),
  322. D);
  323. // Check if initializer is omp_priv <init_expr> or something else.
  324. if (Tok.is(tok::identifier) &&
  325. Tok.getIdentifierInfo()->isStr("omp_priv")) {
  326. ConsumeToken();
  327. ParseOpenMPReductionInitializerForDecl(OmpPrivParm);
  328. } else {
  329. InitializerResult = Actions.ActOnFinishFullExpr(
  330. ParseAssignmentExpression().get(), D->getLocation(),
  331. /*DiscardedValue=*/true);
  332. }
  333. Actions.ActOnOpenMPDeclareReductionInitializerEnd(
  334. D, InitializerResult.get(), OmpPrivParm);
  335. if (InitializerResult.isInvalid() && Tok.isNot(tok::r_paren) &&
  336. Tok.isNot(tok::annot_pragma_openmp_end)) {
  337. TPA.Commit();
  338. IsCorrect = false;
  339. break;
  340. }
  341. IsCorrect =
  342. !T.consumeClose() && IsCorrect && !InitializerResult.isInvalid();
  343. }
  344. }
  345. ++I;
  346. // Revert parsing if not the last type, otherwise accept it, we're done with
  347. // parsing.
  348. if (I != E)
  349. TPA.Revert();
  350. else
  351. TPA.Commit();
  352. }
  353. return Actions.ActOnOpenMPDeclareReductionDirectiveEnd(getCurScope(), DRD,
  354. IsCorrect);
  355. }
  356. void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
  357. // Parse declarator '=' initializer.
  358. // If a '==' or '+=' is found, suggest a fixit to '='.
  359. if (isTokenEqualOrEqualTypo()) {
  360. ConsumeToken();
  361. if (Tok.is(tok::code_completion)) {
  362. Actions.CodeCompleteInitializer(getCurScope(), OmpPrivParm);
  363. Actions.FinalizeDeclaration(OmpPrivParm);
  364. cutOffParsing();
  365. return;
  366. }
  367. ExprResult Init(ParseInitializer());
  368. if (Init.isInvalid()) {
  369. SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
  370. Actions.ActOnInitializerError(OmpPrivParm);
  371. } else {
  372. Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
  373. /*DirectInit=*/false);
  374. }
  375. } else if (Tok.is(tok::l_paren)) {
  376. // Parse C++ direct initializer: '(' expression-list ')'
  377. BalancedDelimiterTracker T(*this, tok::l_paren);
  378. T.consumeOpen();
  379. ExprVector Exprs;
  380. CommaLocsTy CommaLocs;
  381. if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] {
  382. Actions.CodeCompleteConstructor(
  383. getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
  384. OmpPrivParm->getLocation(), Exprs);
  385. })) {
  386. Actions.ActOnInitializerError(OmpPrivParm);
  387. SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
  388. } else {
  389. // Match the ')'.
  390. T.consumeClose();
  391. assert(!Exprs.empty() && Exprs.size() - 1 == CommaLocs.size() &&
  392. "Unexpected number of commas!");
  393. ExprResult Initializer = Actions.ActOnParenListExpr(
  394. T.getOpenLocation(), T.getCloseLocation(), Exprs);
  395. Actions.AddInitializerToDecl(OmpPrivParm, Initializer.get(),
  396. /*DirectInit=*/true);
  397. }
  398. } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
  399. // Parse C++0x braced-init-list.
  400. Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
  401. ExprResult Init(ParseBraceInitializer());
  402. if (Init.isInvalid()) {
  403. Actions.ActOnInitializerError(OmpPrivParm);
  404. } else {
  405. Actions.AddInitializerToDecl(OmpPrivParm, Init.get(),
  406. /*DirectInit=*/true);
  407. }
  408. } else {
  409. Actions.ActOnUninitializedDecl(OmpPrivParm);
  410. }
  411. }
  412. namespace {
  413. /// RAII that recreates function context for correct parsing of clauses of
  414. /// 'declare simd' construct.
  415. /// OpenMP, 2.8.2 declare simd Construct
  416. /// The expressions appearing in the clauses of this directive are evaluated in
  417. /// the scope of the arguments of the function declaration or definition.
  418. class FNContextRAII final {
  419. Parser &P;
  420. Sema::CXXThisScopeRAII *ThisScope;
  421. Parser::ParseScope *TempScope;
  422. Parser::ParseScope *FnScope;
  423. bool HasTemplateScope = false;
  424. bool HasFunScope = false;
  425. FNContextRAII() = delete;
  426. FNContextRAII(const FNContextRAII &) = delete;
  427. FNContextRAII &operator=(const FNContextRAII &) = delete;
  428. public:
  429. FNContextRAII(Parser &P, Parser::DeclGroupPtrTy Ptr) : P(P) {
  430. Decl *D = *Ptr.get().begin();
  431. NamedDecl *ND = dyn_cast<NamedDecl>(D);
  432. RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
  433. Sema &Actions = P.getActions();
  434. // Allow 'this' within late-parsed attributes.
  435. ThisScope = new Sema::CXXThisScopeRAII(Actions, RD, /*TypeQuals=*/0,
  436. ND && ND->isCXXInstanceMember());
  437. // If the Decl is templatized, add template parameters to scope.
  438. HasTemplateScope = D->isTemplateDecl();
  439. TempScope =
  440. new Parser::ParseScope(&P, Scope::TemplateParamScope, HasTemplateScope);
  441. if (HasTemplateScope)
  442. Actions.ActOnReenterTemplateScope(Actions.getCurScope(), D);
  443. // If the Decl is on a function, add function parameters to the scope.
  444. HasFunScope = D->isFunctionOrFunctionTemplate();
  445. FnScope = new Parser::ParseScope(
  446. &P, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
  447. HasFunScope);
  448. if (HasFunScope)
  449. Actions.ActOnReenterFunctionContext(Actions.getCurScope(), D);
  450. }
  451. ~FNContextRAII() {
  452. if (HasFunScope) {
  453. P.getActions().ActOnExitFunctionContext();
  454. FnScope->Exit(); // Pop scope, and remove Decls from IdResolver
  455. }
  456. if (HasTemplateScope)
  457. TempScope->Exit();
  458. delete FnScope;
  459. delete TempScope;
  460. delete ThisScope;
  461. }
  462. };
  463. } // namespace
  464. /// Parses clauses for 'declare simd' directive.
  465. /// clause:
  466. /// 'inbranch' | 'notinbranch'
  467. /// 'simdlen' '(' <expr> ')'
  468. /// { 'uniform' '(' <argument_list> ')' }
  469. /// { 'aligned '(' <argument_list> [ ':' <alignment> ] ')' }
  470. /// { 'linear '(' <argument_list> [ ':' <step> ] ')' }
  471. static bool parseDeclareSimdClauses(
  472. Parser &P, OMPDeclareSimdDeclAttr::BranchStateTy &BS, ExprResult &SimdLen,
  473. SmallVectorImpl<Expr *> &Uniforms, SmallVectorImpl<Expr *> &Aligneds,
  474. SmallVectorImpl<Expr *> &Alignments, SmallVectorImpl<Expr *> &Linears,
  475. SmallVectorImpl<unsigned> &LinModifiers, SmallVectorImpl<Expr *> &Steps) {
  476. SourceRange BSRange;
  477. const Token &Tok = P.getCurToken();
  478. bool IsError = false;
  479. while (Tok.isNot(tok::annot_pragma_openmp_end)) {
  480. if (Tok.isNot(tok::identifier))
  481. break;
  482. OMPDeclareSimdDeclAttr::BranchStateTy Out;
  483. IdentifierInfo *II = Tok.getIdentifierInfo();
  484. StringRef ClauseName = II->getName();
  485. // Parse 'inranch|notinbranch' clauses.
  486. if (OMPDeclareSimdDeclAttr::ConvertStrToBranchStateTy(ClauseName, Out)) {
  487. if (BS != OMPDeclareSimdDeclAttr::BS_Undefined && BS != Out) {
  488. P.Diag(Tok, diag::err_omp_declare_simd_inbranch_notinbranch)
  489. << ClauseName
  490. << OMPDeclareSimdDeclAttr::ConvertBranchStateTyToStr(BS) << BSRange;
  491. IsError = true;
  492. }
  493. BS = Out;
  494. BSRange = SourceRange(Tok.getLocation(), Tok.getEndLoc());
  495. P.ConsumeToken();
  496. } else if (ClauseName.equals("simdlen")) {
  497. if (SimdLen.isUsable()) {
  498. P.Diag(Tok, diag::err_omp_more_one_clause)
  499. << getOpenMPDirectiveName(OMPD_declare_simd) << ClauseName << 0;
  500. IsError = true;
  501. }
  502. P.ConsumeToken();
  503. SourceLocation RLoc;
  504. SimdLen = P.ParseOpenMPParensExpr(ClauseName, RLoc);
  505. if (SimdLen.isInvalid())
  506. IsError = true;
  507. } else {
  508. OpenMPClauseKind CKind = getOpenMPClauseKind(ClauseName);
  509. if (CKind == OMPC_uniform || CKind == OMPC_aligned ||
  510. CKind == OMPC_linear) {
  511. Parser::OpenMPVarListDataTy Data;
  512. SmallVectorImpl<Expr *> *Vars = &Uniforms;
  513. if (CKind == OMPC_aligned)
  514. Vars = &Aligneds;
  515. else if (CKind == OMPC_linear)
  516. Vars = &Linears;
  517. P.ConsumeToken();
  518. if (P.ParseOpenMPVarList(OMPD_declare_simd,
  519. getOpenMPClauseKind(ClauseName), *Vars, Data))
  520. IsError = true;
  521. if (CKind == OMPC_aligned) {
  522. Alignments.append(Aligneds.size() - Alignments.size(), Data.TailExpr);
  523. } else if (CKind == OMPC_linear) {
  524. if (P.getActions().CheckOpenMPLinearModifier(Data.LinKind,
  525. Data.DepLinMapLoc))
  526. Data.LinKind = OMPC_LINEAR_val;
  527. LinModifiers.append(Linears.size() - LinModifiers.size(),
  528. Data.LinKind);
  529. Steps.append(Linears.size() - Steps.size(), Data.TailExpr);
  530. }
  531. } else
  532. // TODO: add parsing of other clauses.
  533. break;
  534. }
  535. // Skip ',' if any.
  536. if (Tok.is(tok::comma))
  537. P.ConsumeToken();
  538. }
  539. return IsError;
  540. }
  541. /// Parse clauses for '#pragma omp declare simd'.
  542. Parser::DeclGroupPtrTy
  543. Parser::ParseOMPDeclareSimdClauses(Parser::DeclGroupPtrTy Ptr,
  544. CachedTokens &Toks, SourceLocation Loc) {
  545. PP.EnterToken(Tok);
  546. PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
  547. // Consume the previously pushed token.
  548. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
  549. FNContextRAII FnContext(*this, Ptr);
  550. OMPDeclareSimdDeclAttr::BranchStateTy BS =
  551. OMPDeclareSimdDeclAttr::BS_Undefined;
  552. ExprResult Simdlen;
  553. SmallVector<Expr *, 4> Uniforms;
  554. SmallVector<Expr *, 4> Aligneds;
  555. SmallVector<Expr *, 4> Alignments;
  556. SmallVector<Expr *, 4> Linears;
  557. SmallVector<unsigned, 4> LinModifiers;
  558. SmallVector<Expr *, 4> Steps;
  559. bool IsError =
  560. parseDeclareSimdClauses(*this, BS, Simdlen, Uniforms, Aligneds,
  561. Alignments, Linears, LinModifiers, Steps);
  562. // Need to check for extra tokens.
  563. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  564. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  565. << getOpenMPDirectiveName(OMPD_declare_simd);
  566. while (Tok.isNot(tok::annot_pragma_openmp_end))
  567. ConsumeAnyToken();
  568. }
  569. // Skip the last annot_pragma_openmp_end.
  570. SourceLocation EndLoc = ConsumeAnnotationToken();
  571. if (IsError)
  572. return Ptr;
  573. return Actions.ActOnOpenMPDeclareSimdDirective(
  574. Ptr, BS, Simdlen.get(), Uniforms, Aligneds, Alignments, Linears,
  575. LinModifiers, Steps, SourceRange(Loc, EndLoc));
  576. }
  577. /// \brief Parsing of declarative OpenMP directives.
  578. ///
  579. /// threadprivate-directive:
  580. /// annot_pragma_openmp 'threadprivate' simple-variable-list
  581. /// annot_pragma_openmp_end
  582. ///
  583. /// declare-reduction-directive:
  584. /// annot_pragma_openmp 'declare' 'reduction' [...]
  585. /// annot_pragma_openmp_end
  586. ///
  587. /// declare-simd-directive:
  588. /// annot_pragma_openmp 'declare simd' {<clause> [,]}
  589. /// annot_pragma_openmp_end
  590. /// <function declaration/definition>
  591. ///
  592. Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
  593. AccessSpecifier &AS, ParsedAttributesWithRange &Attrs,
  594. DeclSpec::TST TagType, Decl *Tag) {
  595. assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
  596. ParenBraceBracketBalancer BalancerRAIIObj(*this);
  597. SourceLocation Loc = ConsumeAnnotationToken();
  598. OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
  599. switch (DKind) {
  600. case OMPD_threadprivate: {
  601. ConsumeToken();
  602. ThreadprivateListParserHelper Helper(this);
  603. if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, true)) {
  604. // The last seen token is annot_pragma_openmp_end - need to check for
  605. // extra tokens.
  606. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  607. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  608. << getOpenMPDirectiveName(OMPD_threadprivate);
  609. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  610. }
  611. // Skip the last annot_pragma_openmp_end.
  612. ConsumeAnnotationToken();
  613. return Actions.ActOnOpenMPThreadprivateDirective(Loc,
  614. Helper.getIdentifiers());
  615. }
  616. break;
  617. }
  618. case OMPD_declare_reduction:
  619. ConsumeToken();
  620. if (DeclGroupPtrTy Res = ParseOpenMPDeclareReductionDirective(AS)) {
  621. // The last seen token is annot_pragma_openmp_end - need to check for
  622. // extra tokens.
  623. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  624. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  625. << getOpenMPDirectiveName(OMPD_declare_reduction);
  626. while (Tok.isNot(tok::annot_pragma_openmp_end))
  627. ConsumeAnyToken();
  628. }
  629. // Skip the last annot_pragma_openmp_end.
  630. ConsumeAnnotationToken();
  631. return Res;
  632. }
  633. break;
  634. case OMPD_declare_simd: {
  635. // The syntax is:
  636. // { #pragma omp declare simd }
  637. // <function-declaration-or-definition>
  638. //
  639. ConsumeToken();
  640. CachedTokens Toks;
  641. while(Tok.isNot(tok::annot_pragma_openmp_end)) {
  642. Toks.push_back(Tok);
  643. ConsumeAnyToken();
  644. }
  645. Toks.push_back(Tok);
  646. ConsumeAnyToken();
  647. DeclGroupPtrTy Ptr;
  648. if (Tok.is(tok::annot_pragma_openmp)) {
  649. Ptr = ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs, TagType, Tag);
  650. } else if (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
  651. // Here we expect to see some function declaration.
  652. if (AS == AS_none) {
  653. assert(TagType == DeclSpec::TST_unspecified);
  654. MaybeParseCXX11Attributes(Attrs);
  655. ParsingDeclSpec PDS(*this);
  656. Ptr = ParseExternalDeclaration(Attrs, &PDS);
  657. } else {
  658. Ptr =
  659. ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
  660. }
  661. }
  662. if (!Ptr) {
  663. Diag(Loc, diag::err_omp_decl_in_declare_simd);
  664. return DeclGroupPtrTy();
  665. }
  666. return ParseOMPDeclareSimdClauses(Ptr, Toks, Loc);
  667. }
  668. case OMPD_declare_target: {
  669. SourceLocation DTLoc = ConsumeAnyToken();
  670. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  671. // OpenMP 4.5 syntax with list of entities.
  672. Sema::NamedDeclSetType SameDirectiveDecls;
  673. while (Tok.isNot(tok::annot_pragma_openmp_end)) {
  674. OMPDeclareTargetDeclAttr::MapTypeTy MT =
  675. OMPDeclareTargetDeclAttr::MT_To;
  676. if (Tok.is(tok::identifier)) {
  677. IdentifierInfo *II = Tok.getIdentifierInfo();
  678. StringRef ClauseName = II->getName();
  679. // Parse 'to|link' clauses.
  680. if (!OMPDeclareTargetDeclAttr::ConvertStrToMapTypeTy(ClauseName,
  681. MT)) {
  682. Diag(Tok, diag::err_omp_declare_target_unexpected_clause)
  683. << ClauseName;
  684. break;
  685. }
  686. ConsumeToken();
  687. }
  688. auto &&Callback = [this, MT, &SameDirectiveDecls](
  689. CXXScopeSpec &SS, DeclarationNameInfo NameInfo) {
  690. Actions.ActOnOpenMPDeclareTargetName(getCurScope(), SS, NameInfo, MT,
  691. SameDirectiveDecls);
  692. };
  693. if (ParseOpenMPSimpleVarList(OMPD_declare_target, Callback,
  694. /*AllowScopeSpecifier=*/true))
  695. break;
  696. // Consume optional ','.
  697. if (Tok.is(tok::comma))
  698. ConsumeToken();
  699. }
  700. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  701. ConsumeAnyToken();
  702. SmallVector<Decl *, 4> Decls(SameDirectiveDecls.begin(),
  703. SameDirectiveDecls.end());
  704. if (Decls.empty())
  705. return DeclGroupPtrTy();
  706. return Actions.BuildDeclaratorGroup(Decls);
  707. }
  708. // Skip the last annot_pragma_openmp_end.
  709. ConsumeAnyToken();
  710. if (!Actions.ActOnStartOpenMPDeclareTargetDirective(DTLoc))
  711. return DeclGroupPtrTy();
  712. llvm::SmallVector<Decl *, 4> Decls;
  713. DKind = parseOpenMPDirectiveKind(*this);
  714. while (DKind != OMPD_end_declare_target && DKind != OMPD_declare_target &&
  715. Tok.isNot(tok::eof) && Tok.isNot(tok::r_brace)) {
  716. DeclGroupPtrTy Ptr;
  717. // Here we expect to see some function declaration.
  718. if (AS == AS_none) {
  719. assert(TagType == DeclSpec::TST_unspecified);
  720. MaybeParseCXX11Attributes(Attrs);
  721. ParsingDeclSpec PDS(*this);
  722. Ptr = ParseExternalDeclaration(Attrs, &PDS);
  723. } else {
  724. Ptr =
  725. ParseCXXClassMemberDeclarationWithPragmas(AS, Attrs, TagType, Tag);
  726. }
  727. if (Ptr) {
  728. DeclGroupRef Ref = Ptr.get();
  729. Decls.append(Ref.begin(), Ref.end());
  730. }
  731. if (Tok.isAnnotation() && Tok.is(tok::annot_pragma_openmp)) {
  732. TentativeParsingAction TPA(*this);
  733. ConsumeAnnotationToken();
  734. DKind = parseOpenMPDirectiveKind(*this);
  735. if (DKind != OMPD_end_declare_target)
  736. TPA.Revert();
  737. else
  738. TPA.Commit();
  739. }
  740. }
  741. if (DKind == OMPD_end_declare_target) {
  742. ConsumeAnyToken();
  743. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  744. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  745. << getOpenMPDirectiveName(OMPD_end_declare_target);
  746. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  747. }
  748. // Skip the last annot_pragma_openmp_end.
  749. ConsumeAnyToken();
  750. } else {
  751. Diag(Tok, diag::err_expected_end_declare_target);
  752. Diag(DTLoc, diag::note_matching) << "'#pragma omp declare target'";
  753. }
  754. Actions.ActOnFinishOpenMPDeclareTargetDirective();
  755. return Actions.BuildDeclaratorGroup(Decls);
  756. }
  757. case OMPD_unknown:
  758. Diag(Tok, diag::err_omp_unknown_directive);
  759. break;
  760. case OMPD_parallel:
  761. case OMPD_simd:
  762. case OMPD_task:
  763. case OMPD_taskyield:
  764. case OMPD_barrier:
  765. case OMPD_taskwait:
  766. case OMPD_taskgroup:
  767. case OMPD_flush:
  768. case OMPD_for:
  769. case OMPD_for_simd:
  770. case OMPD_sections:
  771. case OMPD_section:
  772. case OMPD_single:
  773. case OMPD_master:
  774. case OMPD_ordered:
  775. case OMPD_critical:
  776. case OMPD_parallel_for:
  777. case OMPD_parallel_for_simd:
  778. case OMPD_parallel_sections:
  779. case OMPD_atomic:
  780. case OMPD_target:
  781. case OMPD_teams:
  782. case OMPD_cancellation_point:
  783. case OMPD_cancel:
  784. case OMPD_target_data:
  785. case OMPD_target_enter_data:
  786. case OMPD_target_exit_data:
  787. case OMPD_target_parallel:
  788. case OMPD_target_parallel_for:
  789. case OMPD_taskloop:
  790. case OMPD_taskloop_simd:
  791. case OMPD_distribute:
  792. case OMPD_end_declare_target:
  793. case OMPD_target_update:
  794. case OMPD_distribute_parallel_for:
  795. case OMPD_distribute_parallel_for_simd:
  796. case OMPD_distribute_simd:
  797. case OMPD_target_parallel_for_simd:
  798. case OMPD_target_simd:
  799. case OMPD_teams_distribute:
  800. case OMPD_teams_distribute_simd:
  801. case OMPD_teams_distribute_parallel_for_simd:
  802. case OMPD_teams_distribute_parallel_for:
  803. case OMPD_target_teams:
  804. case OMPD_target_teams_distribute:
  805. case OMPD_target_teams_distribute_parallel_for:
  806. case OMPD_target_teams_distribute_parallel_for_simd:
  807. case OMPD_target_teams_distribute_simd:
  808. Diag(Tok, diag::err_omp_unexpected_directive)
  809. << 1 << getOpenMPDirectiveName(DKind);
  810. break;
  811. }
  812. while (Tok.isNot(tok::annot_pragma_openmp_end))
  813. ConsumeAnyToken();
  814. ConsumeAnyToken();
  815. return nullptr;
  816. }
  817. /// \brief Parsing of declarative or executable OpenMP directives.
  818. ///
  819. /// threadprivate-directive:
  820. /// annot_pragma_openmp 'threadprivate' simple-variable-list
  821. /// annot_pragma_openmp_end
  822. ///
  823. /// declare-reduction-directive:
  824. /// annot_pragma_openmp 'declare' 'reduction' '(' <reduction_id> ':'
  825. /// <type> {',' <type>} ':' <expression> ')' ['initializer' '('
  826. /// ('omp_priv' '=' <expression>|<function_call>) ')']
  827. /// annot_pragma_openmp_end
  828. ///
  829. /// executable-directive:
  830. /// annot_pragma_openmp 'parallel' | 'simd' | 'for' | 'sections' |
  831. /// 'section' | 'single' | 'master' | 'critical' [ '(' <name> ')' ] |
  832. /// 'parallel for' | 'parallel sections' | 'task' | 'taskyield' |
  833. /// 'barrier' | 'taskwait' | 'flush' | 'ordered' | 'atomic' |
  834. /// 'for simd' | 'parallel for simd' | 'target' | 'target data' |
  835. /// 'taskgroup' | 'teams' | 'taskloop' | 'taskloop simd' |
  836. /// 'distribute' | 'target enter data' | 'target exit data' |
  837. /// 'target parallel' | 'target parallel for' |
  838. /// 'target update' | 'distribute parallel for' |
  839. /// 'distribute paralle for simd' | 'distribute simd' |
  840. /// 'target parallel for simd' | 'target simd' |
  841. /// 'teams distribute' | 'teams distribute simd' |
  842. /// 'teams distribute parallel for simd' |
  843. /// 'teams distribute parallel for' | 'target teams' |
  844. /// 'target teams distribute' |
  845. /// 'target teams distribute parallel for' |
  846. /// 'target teams distribute parallel for simd' |
  847. /// 'target teams distribute simd' {clause}
  848. /// annot_pragma_openmp_end
  849. ///
  850. StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
  851. AllowedConstructsKind Allowed) {
  852. assert(Tok.is(tok::annot_pragma_openmp) && "Not an OpenMP directive!");
  853. ParenBraceBracketBalancer BalancerRAIIObj(*this);
  854. SmallVector<OMPClause *, 5> Clauses;
  855. SmallVector<llvm::PointerIntPair<OMPClause *, 1, bool>, OMPC_unknown + 1>
  856. FirstClauses(OMPC_unknown + 1);
  857. unsigned ScopeFlags = Scope::FnScope | Scope::DeclScope |
  858. Scope::CompoundStmtScope | Scope::OpenMPDirectiveScope;
  859. SourceLocation Loc = ConsumeAnnotationToken(), EndLoc;
  860. OpenMPDirectiveKind DKind = parseOpenMPDirectiveKind(*this);
  861. OpenMPDirectiveKind CancelRegion = OMPD_unknown;
  862. // Name of critical directive.
  863. DeclarationNameInfo DirName;
  864. StmtResult Directive = StmtError();
  865. bool HasAssociatedStatement = true;
  866. bool FlushHasClause = false;
  867. switch (DKind) {
  868. case OMPD_threadprivate: {
  869. if (Allowed != ACK_Any) {
  870. Diag(Tok, diag::err_omp_immediate_directive)
  871. << getOpenMPDirectiveName(DKind) << 0;
  872. }
  873. ConsumeToken();
  874. ThreadprivateListParserHelper Helper(this);
  875. if (!ParseOpenMPSimpleVarList(OMPD_threadprivate, Helper, false)) {
  876. // The last seen token is annot_pragma_openmp_end - need to check for
  877. // extra tokens.
  878. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  879. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  880. << getOpenMPDirectiveName(OMPD_threadprivate);
  881. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  882. }
  883. DeclGroupPtrTy Res = Actions.ActOnOpenMPThreadprivateDirective(
  884. Loc, Helper.getIdentifiers());
  885. Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
  886. }
  887. SkipUntil(tok::annot_pragma_openmp_end);
  888. break;
  889. }
  890. case OMPD_declare_reduction:
  891. ConsumeToken();
  892. if (DeclGroupPtrTy Res =
  893. ParseOpenMPDeclareReductionDirective(/*AS=*/AS_none)) {
  894. // The last seen token is annot_pragma_openmp_end - need to check for
  895. // extra tokens.
  896. if (Tok.isNot(tok::annot_pragma_openmp_end)) {
  897. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  898. << getOpenMPDirectiveName(OMPD_declare_reduction);
  899. while (Tok.isNot(tok::annot_pragma_openmp_end))
  900. ConsumeAnyToken();
  901. }
  902. ConsumeAnyToken();
  903. Directive = Actions.ActOnDeclStmt(Res, Loc, Tok.getLocation());
  904. } else {
  905. SkipUntil(tok::annot_pragma_openmp_end);
  906. }
  907. break;
  908. case OMPD_flush:
  909. if (PP.LookAhead(0).is(tok::l_paren)) {
  910. FlushHasClause = true;
  911. // Push copy of the current token back to stream to properly parse
  912. // pseudo-clause OMPFlushClause.
  913. PP.EnterToken(Tok);
  914. }
  915. LLVM_FALLTHROUGH;
  916. case OMPD_taskyield:
  917. case OMPD_barrier:
  918. case OMPD_taskwait:
  919. case OMPD_cancellation_point:
  920. case OMPD_cancel:
  921. case OMPD_target_enter_data:
  922. case OMPD_target_exit_data:
  923. case OMPD_target_update:
  924. if (Allowed == ACK_StatementsOpenMPNonStandalone) {
  925. Diag(Tok, diag::err_omp_immediate_directive)
  926. << getOpenMPDirectiveName(DKind) << 0;
  927. }
  928. HasAssociatedStatement = false;
  929. // Fall through for further analysis.
  930. LLVM_FALLTHROUGH;
  931. case OMPD_parallel:
  932. case OMPD_simd:
  933. case OMPD_for:
  934. case OMPD_for_simd:
  935. case OMPD_sections:
  936. case OMPD_single:
  937. case OMPD_section:
  938. case OMPD_master:
  939. case OMPD_critical:
  940. case OMPD_parallel_for:
  941. case OMPD_parallel_for_simd:
  942. case OMPD_parallel_sections:
  943. case OMPD_task:
  944. case OMPD_ordered:
  945. case OMPD_atomic:
  946. case OMPD_target:
  947. case OMPD_teams:
  948. case OMPD_taskgroup:
  949. case OMPD_target_data:
  950. case OMPD_target_parallel:
  951. case OMPD_target_parallel_for:
  952. case OMPD_taskloop:
  953. case OMPD_taskloop_simd:
  954. case OMPD_distribute:
  955. case OMPD_distribute_parallel_for:
  956. case OMPD_distribute_parallel_for_simd:
  957. case OMPD_distribute_simd:
  958. case OMPD_target_parallel_for_simd:
  959. case OMPD_target_simd:
  960. case OMPD_teams_distribute:
  961. case OMPD_teams_distribute_simd:
  962. case OMPD_teams_distribute_parallel_for_simd:
  963. case OMPD_teams_distribute_parallel_for:
  964. case OMPD_target_teams:
  965. case OMPD_target_teams_distribute:
  966. case OMPD_target_teams_distribute_parallel_for:
  967. case OMPD_target_teams_distribute_parallel_for_simd:
  968. case OMPD_target_teams_distribute_simd: {
  969. ConsumeToken();
  970. // Parse directive name of the 'critical' directive if any.
  971. if (DKind == OMPD_critical) {
  972. BalancedDelimiterTracker T(*this, tok::l_paren,
  973. tok::annot_pragma_openmp_end);
  974. if (!T.consumeOpen()) {
  975. if (Tok.isAnyIdentifier()) {
  976. DirName =
  977. DeclarationNameInfo(Tok.getIdentifierInfo(), Tok.getLocation());
  978. ConsumeAnyToken();
  979. } else {
  980. Diag(Tok, diag::err_omp_expected_identifier_for_critical);
  981. }
  982. T.consumeClose();
  983. }
  984. } else if (DKind == OMPD_cancellation_point || DKind == OMPD_cancel) {
  985. CancelRegion = parseOpenMPDirectiveKind(*this);
  986. if (Tok.isNot(tok::annot_pragma_openmp_end))
  987. ConsumeToken();
  988. }
  989. if (isOpenMPLoopDirective(DKind))
  990. ScopeFlags |= Scope::OpenMPLoopDirectiveScope;
  991. if (isOpenMPSimdDirective(DKind))
  992. ScopeFlags |= Scope::OpenMPSimdDirectiveScope;
  993. ParseScope OMPDirectiveScope(this, ScopeFlags);
  994. Actions.StartOpenMPDSABlock(DKind, DirName, Actions.getCurScope(), Loc);
  995. while (Tok.isNot(tok::annot_pragma_openmp_end)) {
  996. OpenMPClauseKind CKind =
  997. Tok.isAnnotation()
  998. ? OMPC_unknown
  999. : FlushHasClause ? OMPC_flush
  1000. : getOpenMPClauseKind(PP.getSpelling(Tok));
  1001. Actions.StartOpenMPClause(CKind);
  1002. FlushHasClause = false;
  1003. OMPClause *Clause =
  1004. ParseOpenMPClause(DKind, CKind, !FirstClauses[CKind].getInt());
  1005. FirstClauses[CKind].setInt(true);
  1006. if (Clause) {
  1007. FirstClauses[CKind].setPointer(Clause);
  1008. Clauses.push_back(Clause);
  1009. }
  1010. // Skip ',' if any.
  1011. if (Tok.is(tok::comma))
  1012. ConsumeToken();
  1013. Actions.EndOpenMPClause();
  1014. }
  1015. // End location of the directive.
  1016. EndLoc = Tok.getLocation();
  1017. // Consume final annot_pragma_openmp_end.
  1018. ConsumeAnnotationToken();
  1019. // OpenMP [2.13.8, ordered Construct, Syntax]
  1020. // If the depend clause is specified, the ordered construct is a stand-alone
  1021. // directive.
  1022. if (DKind == OMPD_ordered && FirstClauses[OMPC_depend].getInt()) {
  1023. if (Allowed == ACK_StatementsOpenMPNonStandalone) {
  1024. Diag(Loc, diag::err_omp_immediate_directive)
  1025. << getOpenMPDirectiveName(DKind) << 1
  1026. << getOpenMPClauseName(OMPC_depend);
  1027. }
  1028. HasAssociatedStatement = false;
  1029. }
  1030. StmtResult AssociatedStmt;
  1031. if (HasAssociatedStatement) {
  1032. // The body is a block scope like in Lambdas and Blocks.
  1033. Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
  1034. // FIXME: We create a bogus CompoundStmt scope to hold the contents of
  1035. // the captured region. Code elsewhere assumes that any FunctionScopeInfo
  1036. // should have at least one compound statement scope within it.
  1037. AssociatedStmt = (Sema::CompoundScopeRAII(Actions), ParseStatement());
  1038. AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
  1039. } else if (DKind == OMPD_target_update || DKind == OMPD_target_enter_data ||
  1040. DKind == OMPD_target_exit_data) {
  1041. Actions.ActOnOpenMPRegionStart(DKind, getCurScope());
  1042. AssociatedStmt = (Sema::CompoundScopeRAII(Actions),
  1043. Actions.ActOnCompoundStmt(Loc, Loc, llvm::None,
  1044. /*isStmtExpr=*/false));
  1045. AssociatedStmt = Actions.ActOnOpenMPRegionEnd(AssociatedStmt, Clauses);
  1046. }
  1047. Directive = Actions.ActOnOpenMPExecutableDirective(
  1048. DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc,
  1049. EndLoc);
  1050. // Exit scope.
  1051. Actions.EndOpenMPDSABlock(Directive.get());
  1052. OMPDirectiveScope.Exit();
  1053. break;
  1054. }
  1055. case OMPD_declare_simd:
  1056. case OMPD_declare_target:
  1057. case OMPD_end_declare_target:
  1058. Diag(Tok, diag::err_omp_unexpected_directive)
  1059. << 1 << getOpenMPDirectiveName(DKind);
  1060. SkipUntil(tok::annot_pragma_openmp_end);
  1061. break;
  1062. case OMPD_unknown:
  1063. Diag(Tok, diag::err_omp_unknown_directive);
  1064. SkipUntil(tok::annot_pragma_openmp_end);
  1065. break;
  1066. }
  1067. return Directive;
  1068. }
  1069. // Parses simple list:
  1070. // simple-variable-list:
  1071. // '(' id-expression {, id-expression} ')'
  1072. //
  1073. bool Parser::ParseOpenMPSimpleVarList(
  1074. OpenMPDirectiveKind Kind,
  1075. const llvm::function_ref<void(CXXScopeSpec &, DeclarationNameInfo)> &
  1076. Callback,
  1077. bool AllowScopeSpecifier) {
  1078. // Parse '('.
  1079. BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
  1080. if (T.expectAndConsume(diag::err_expected_lparen_after,
  1081. getOpenMPDirectiveName(Kind)))
  1082. return true;
  1083. bool IsCorrect = true;
  1084. bool NoIdentIsFound = true;
  1085. // Read tokens while ')' or annot_pragma_openmp_end is not found.
  1086. while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::annot_pragma_openmp_end)) {
  1087. CXXScopeSpec SS;
  1088. UnqualifiedId Name;
  1089. // Read var name.
  1090. Token PrevTok = Tok;
  1091. NoIdentIsFound = false;
  1092. if (AllowScopeSpecifier && getLangOpts().CPlusPlus &&
  1093. ParseOptionalCXXScopeSpecifier(SS, nullptr, false)) {
  1094. IsCorrect = false;
  1095. SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
  1096. StopBeforeMatch);
  1097. } else if (ParseUnqualifiedId(SS, false, false, false, false, nullptr,
  1098. nullptr, Name)) {
  1099. IsCorrect = false;
  1100. SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
  1101. StopBeforeMatch);
  1102. } else if (Tok.isNot(tok::comma) && Tok.isNot(tok::r_paren) &&
  1103. Tok.isNot(tok::annot_pragma_openmp_end)) {
  1104. IsCorrect = false;
  1105. SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
  1106. StopBeforeMatch);
  1107. Diag(PrevTok.getLocation(), diag::err_expected)
  1108. << tok::identifier
  1109. << SourceRange(PrevTok.getLocation(), PrevTokLocation);
  1110. } else {
  1111. Callback(SS, Actions.GetNameFromUnqualifiedId(Name));
  1112. }
  1113. // Consume ','.
  1114. if (Tok.is(tok::comma)) {
  1115. ConsumeToken();
  1116. }
  1117. }
  1118. if (NoIdentIsFound) {
  1119. Diag(Tok, diag::err_expected) << tok::identifier;
  1120. IsCorrect = false;
  1121. }
  1122. // Parse ')'.
  1123. IsCorrect = !T.consumeClose() && IsCorrect;
  1124. return !IsCorrect;
  1125. }
  1126. /// \brief Parsing of OpenMP clauses.
  1127. ///
  1128. /// clause:
  1129. /// if-clause | final-clause | num_threads-clause | safelen-clause |
  1130. /// default-clause | private-clause | firstprivate-clause | shared-clause
  1131. /// | linear-clause | aligned-clause | collapse-clause |
  1132. /// lastprivate-clause | reduction-clause | proc_bind-clause |
  1133. /// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
  1134. /// mergeable-clause | flush-clause | read-clause | write-clause |
  1135. /// update-clause | capture-clause | seq_cst-clause | device-clause |
  1136. /// simdlen-clause | threads-clause | simd-clause | num_teams-clause |
  1137. /// thread_limit-clause | priority-clause | grainsize-clause |
  1138. /// nogroup-clause | num_tasks-clause | hint-clause | to-clause |
  1139. /// from-clause | is_device_ptr-clause | task_reduction-clause |
  1140. /// in_reduction-clause
  1141. ///
  1142. OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
  1143. OpenMPClauseKind CKind, bool FirstClause) {
  1144. OMPClause *Clause = nullptr;
  1145. bool ErrorFound = false;
  1146. bool WrongDirective = false;
  1147. // Check if clause is allowed for the given directive.
  1148. if (CKind != OMPC_unknown && !isAllowedClauseForDirective(DKind, CKind)) {
  1149. Diag(Tok, diag::err_omp_unexpected_clause) << getOpenMPClauseName(CKind)
  1150. << getOpenMPDirectiveName(DKind);
  1151. ErrorFound = true;
  1152. WrongDirective = true;
  1153. }
  1154. switch (CKind) {
  1155. case OMPC_final:
  1156. case OMPC_num_threads:
  1157. case OMPC_safelen:
  1158. case OMPC_simdlen:
  1159. case OMPC_collapse:
  1160. case OMPC_ordered:
  1161. case OMPC_device:
  1162. case OMPC_num_teams:
  1163. case OMPC_thread_limit:
  1164. case OMPC_priority:
  1165. case OMPC_grainsize:
  1166. case OMPC_num_tasks:
  1167. case OMPC_hint:
  1168. // OpenMP [2.5, Restrictions]
  1169. // At most one num_threads clause can appear on the directive.
  1170. // OpenMP [2.8.1, simd construct, Restrictions]
  1171. // Only one safelen clause can appear on a simd directive.
  1172. // Only one simdlen clause can appear on a simd directive.
  1173. // Only one collapse clause can appear on a simd directive.
  1174. // OpenMP [2.9.1, target data construct, Restrictions]
  1175. // At most one device clause can appear on the directive.
  1176. // OpenMP [2.11.1, task Construct, Restrictions]
  1177. // At most one if clause can appear on the directive.
  1178. // At most one final clause can appear on the directive.
  1179. // OpenMP [teams Construct, Restrictions]
  1180. // At most one num_teams clause can appear on the directive.
  1181. // At most one thread_limit clause can appear on the directive.
  1182. // OpenMP [2.9.1, task Construct, Restrictions]
  1183. // At most one priority clause can appear on the directive.
  1184. // OpenMP [2.9.2, taskloop Construct, Restrictions]
  1185. // At most one grainsize clause can appear on the directive.
  1186. // OpenMP [2.9.2, taskloop Construct, Restrictions]
  1187. // At most one num_tasks clause can appear on the directive.
  1188. if (!FirstClause) {
  1189. Diag(Tok, diag::err_omp_more_one_clause)
  1190. << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
  1191. ErrorFound = true;
  1192. }
  1193. if (CKind == OMPC_ordered && PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
  1194. Clause = ParseOpenMPClause(CKind, WrongDirective);
  1195. else
  1196. Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
  1197. break;
  1198. case OMPC_default:
  1199. case OMPC_proc_bind:
  1200. // OpenMP [2.14.3.1, Restrictions]
  1201. // Only a single default clause may be specified on a parallel, task or
  1202. // teams directive.
  1203. // OpenMP [2.5, parallel Construct, Restrictions]
  1204. // At most one proc_bind clause can appear on the directive.
  1205. if (!FirstClause) {
  1206. Diag(Tok, diag::err_omp_more_one_clause)
  1207. << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
  1208. ErrorFound = true;
  1209. }
  1210. Clause = ParseOpenMPSimpleClause(CKind, WrongDirective);
  1211. break;
  1212. case OMPC_schedule:
  1213. case OMPC_dist_schedule:
  1214. case OMPC_defaultmap:
  1215. // OpenMP [2.7.1, Restrictions, p. 3]
  1216. // Only one schedule clause can appear on a loop directive.
  1217. // OpenMP [2.10.4, Restrictions, p. 106]
  1218. // At most one defaultmap clause can appear on the directive.
  1219. if (!FirstClause) {
  1220. Diag(Tok, diag::err_omp_more_one_clause)
  1221. << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
  1222. ErrorFound = true;
  1223. }
  1224. LLVM_FALLTHROUGH;
  1225. case OMPC_if:
  1226. Clause = ParseOpenMPSingleExprWithArgClause(CKind, WrongDirective);
  1227. break;
  1228. case OMPC_nowait:
  1229. case OMPC_untied:
  1230. case OMPC_mergeable:
  1231. case OMPC_read:
  1232. case OMPC_write:
  1233. case OMPC_update:
  1234. case OMPC_capture:
  1235. case OMPC_seq_cst:
  1236. case OMPC_threads:
  1237. case OMPC_simd:
  1238. case OMPC_nogroup:
  1239. // OpenMP [2.7.1, Restrictions, p. 9]
  1240. // Only one ordered clause can appear on a loop directive.
  1241. // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
  1242. // Only one nowait clause can appear on a for directive.
  1243. if (!FirstClause) {
  1244. Diag(Tok, diag::err_omp_more_one_clause)
  1245. << getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
  1246. ErrorFound = true;
  1247. }
  1248. Clause = ParseOpenMPClause(CKind, WrongDirective);
  1249. break;
  1250. case OMPC_private:
  1251. case OMPC_firstprivate:
  1252. case OMPC_lastprivate:
  1253. case OMPC_shared:
  1254. case OMPC_reduction:
  1255. case OMPC_task_reduction:
  1256. case OMPC_in_reduction:
  1257. case OMPC_linear:
  1258. case OMPC_aligned:
  1259. case OMPC_copyin:
  1260. case OMPC_copyprivate:
  1261. case OMPC_flush:
  1262. case OMPC_depend:
  1263. case OMPC_map:
  1264. case OMPC_to:
  1265. case OMPC_from:
  1266. case OMPC_use_device_ptr:
  1267. case OMPC_is_device_ptr:
  1268. Clause = ParseOpenMPVarListClause(DKind, CKind, WrongDirective);
  1269. break;
  1270. case OMPC_unknown:
  1271. Diag(Tok, diag::warn_omp_extra_tokens_at_eol)
  1272. << getOpenMPDirectiveName(DKind);
  1273. SkipUntil(tok::annot_pragma_openmp_end, StopBeforeMatch);
  1274. break;
  1275. case OMPC_threadprivate:
  1276. case OMPC_uniform:
  1277. if (!WrongDirective)
  1278. Diag(Tok, diag::err_omp_unexpected_clause)
  1279. << getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
  1280. SkipUntil(tok::comma, tok::annot_pragma_openmp_end, StopBeforeMatch);
  1281. break;
  1282. }
  1283. return ErrorFound ? nullptr : Clause;
  1284. }
  1285. /// Parses simple expression in parens for single-expression clauses of OpenMP
  1286. /// constructs.
  1287. /// \param RLoc Returned location of right paren.
  1288. ExprResult Parser::ParseOpenMPParensExpr(StringRef ClauseName,
  1289. SourceLocation &RLoc) {
  1290. BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
  1291. if (T.expectAndConsume(diag::err_expected_lparen_after, ClauseName.data()))
  1292. return ExprError();
  1293. SourceLocation ELoc = Tok.getLocation();
  1294. ExprResult LHS(ParseCastExpression(
  1295. /*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, NotTypeCast));
  1296. ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
  1297. Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
  1298. // Parse ')'.
  1299. T.consumeClose();
  1300. RLoc = T.getCloseLocation();
  1301. return Val;
  1302. }
  1303. /// \brief Parsing of OpenMP clauses with single expressions like 'final',
  1304. /// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams',
  1305. /// 'thread_limit', 'simdlen', 'priority', 'grainsize', 'num_tasks' or 'hint'.
  1306. ///
  1307. /// final-clause:
  1308. /// 'final' '(' expression ')'
  1309. ///
  1310. /// num_threads-clause:
  1311. /// 'num_threads' '(' expression ')'
  1312. ///
  1313. /// safelen-clause:
  1314. /// 'safelen' '(' expression ')'
  1315. ///
  1316. /// simdlen-clause:
  1317. /// 'simdlen' '(' expression ')'
  1318. ///
  1319. /// collapse-clause:
  1320. /// 'collapse' '(' expression ')'
  1321. ///
  1322. /// priority-clause:
  1323. /// 'priority' '(' expression ')'
  1324. ///
  1325. /// grainsize-clause:
  1326. /// 'grainsize' '(' expression ')'
  1327. ///
  1328. /// num_tasks-clause:
  1329. /// 'num_tasks' '(' expression ')'
  1330. ///
  1331. /// hint-clause:
  1332. /// 'hint' '(' expression ')'
  1333. ///
  1334. OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind,
  1335. bool ParseOnly) {
  1336. SourceLocation Loc = ConsumeToken();
  1337. SourceLocation LLoc = Tok.getLocation();
  1338. SourceLocation RLoc;
  1339. ExprResult Val = ParseOpenMPParensExpr(getOpenMPClauseName(Kind), RLoc);
  1340. if (Val.isInvalid())
  1341. return nullptr;
  1342. if (ParseOnly)
  1343. return nullptr;
  1344. return Actions.ActOnOpenMPSingleExprClause(Kind, Val.get(), Loc, LLoc, RLoc);
  1345. }
  1346. /// \brief Parsing of simple OpenMP clauses like 'default' or 'proc_bind'.
  1347. ///
  1348. /// default-clause:
  1349. /// 'default' '(' 'none' | 'shared' ')
  1350. ///
  1351. /// proc_bind-clause:
  1352. /// 'proc_bind' '(' 'master' | 'close' | 'spread' ')
  1353. ///
  1354. OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind,
  1355. bool ParseOnly) {
  1356. SourceLocation Loc = Tok.getLocation();
  1357. SourceLocation LOpen = ConsumeToken();
  1358. // Parse '('.
  1359. BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
  1360. if (T.expectAndConsume(diag::err_expected_lparen_after,
  1361. getOpenMPClauseName(Kind)))
  1362. return nullptr;
  1363. unsigned Type = getOpenMPSimpleClauseType(
  1364. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
  1365. SourceLocation TypeLoc = Tok.getLocation();
  1366. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1367. Tok.isNot(tok::annot_pragma_openmp_end))
  1368. ConsumeAnyToken();
  1369. // Parse ')'.
  1370. T.consumeClose();
  1371. if (ParseOnly)
  1372. return nullptr;
  1373. return Actions.ActOnOpenMPSimpleClause(Kind, Type, TypeLoc, LOpen, Loc,
  1374. Tok.getLocation());
  1375. }
  1376. /// \brief Parsing of OpenMP clauses like 'ordered'.
  1377. ///
  1378. /// ordered-clause:
  1379. /// 'ordered'
  1380. ///
  1381. /// nowait-clause:
  1382. /// 'nowait'
  1383. ///
  1384. /// untied-clause:
  1385. /// 'untied'
  1386. ///
  1387. /// mergeable-clause:
  1388. /// 'mergeable'
  1389. ///
  1390. /// read-clause:
  1391. /// 'read'
  1392. ///
  1393. /// threads-clause:
  1394. /// 'threads'
  1395. ///
  1396. /// simd-clause:
  1397. /// 'simd'
  1398. ///
  1399. /// nogroup-clause:
  1400. /// 'nogroup'
  1401. ///
  1402. OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind, bool ParseOnly) {
  1403. SourceLocation Loc = Tok.getLocation();
  1404. ConsumeAnyToken();
  1405. if (ParseOnly)
  1406. return nullptr;
  1407. return Actions.ActOnOpenMPClause(Kind, Loc, Tok.getLocation());
  1408. }
  1409. /// \brief Parsing of OpenMP clauses with single expressions and some additional
  1410. /// argument like 'schedule' or 'dist_schedule'.
  1411. ///
  1412. /// schedule-clause:
  1413. /// 'schedule' '(' [ modifier [ ',' modifier ] ':' ] kind [',' expression ]
  1414. /// ')'
  1415. ///
  1416. /// if-clause:
  1417. /// 'if' '(' [ directive-name-modifier ':' ] expression ')'
  1418. ///
  1419. /// defaultmap:
  1420. /// 'defaultmap' '(' modifier ':' kind ')'
  1421. ///
  1422. OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,
  1423. bool ParseOnly) {
  1424. SourceLocation Loc = ConsumeToken();
  1425. SourceLocation DelimLoc;
  1426. // Parse '('.
  1427. BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
  1428. if (T.expectAndConsume(diag::err_expected_lparen_after,
  1429. getOpenMPClauseName(Kind)))
  1430. return nullptr;
  1431. ExprResult Val;
  1432. SmallVector<unsigned, 4> Arg;
  1433. SmallVector<SourceLocation, 4> KLoc;
  1434. if (Kind == OMPC_schedule) {
  1435. enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
  1436. Arg.resize(NumberOfElements);
  1437. KLoc.resize(NumberOfElements);
  1438. Arg[Modifier1] = OMPC_SCHEDULE_MODIFIER_unknown;
  1439. Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
  1440. Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
  1441. unsigned KindModifier = getOpenMPSimpleClauseType(
  1442. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
  1443. if (KindModifier > OMPC_SCHEDULE_unknown) {
  1444. // Parse 'modifier'
  1445. Arg[Modifier1] = KindModifier;
  1446. KLoc[Modifier1] = Tok.getLocation();
  1447. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1448. Tok.isNot(tok::annot_pragma_openmp_end))
  1449. ConsumeAnyToken();
  1450. if (Tok.is(tok::comma)) {
  1451. // Parse ',' 'modifier'
  1452. ConsumeAnyToken();
  1453. KindModifier = getOpenMPSimpleClauseType(
  1454. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
  1455. Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
  1456. ? KindModifier
  1457. : (unsigned)OMPC_SCHEDULE_unknown;
  1458. KLoc[Modifier2] = Tok.getLocation();
  1459. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1460. Tok.isNot(tok::annot_pragma_openmp_end))
  1461. ConsumeAnyToken();
  1462. }
  1463. // Parse ':'
  1464. if (Tok.is(tok::colon))
  1465. ConsumeAnyToken();
  1466. else
  1467. Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
  1468. KindModifier = getOpenMPSimpleClauseType(
  1469. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
  1470. }
  1471. Arg[ScheduleKind] = KindModifier;
  1472. KLoc[ScheduleKind] = Tok.getLocation();
  1473. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1474. Tok.isNot(tok::annot_pragma_openmp_end))
  1475. ConsumeAnyToken();
  1476. if ((Arg[ScheduleKind] == OMPC_SCHEDULE_static ||
  1477. Arg[ScheduleKind] == OMPC_SCHEDULE_dynamic ||
  1478. Arg[ScheduleKind] == OMPC_SCHEDULE_guided) &&
  1479. Tok.is(tok::comma))
  1480. DelimLoc = ConsumeAnyToken();
  1481. } else if (Kind == OMPC_dist_schedule) {
  1482. Arg.push_back(getOpenMPSimpleClauseType(
  1483. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
  1484. KLoc.push_back(Tok.getLocation());
  1485. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1486. Tok.isNot(tok::annot_pragma_openmp_end))
  1487. ConsumeAnyToken();
  1488. if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
  1489. DelimLoc = ConsumeAnyToken();
  1490. } else if (Kind == OMPC_defaultmap) {
  1491. // Get a defaultmap modifier
  1492. Arg.push_back(getOpenMPSimpleClauseType(
  1493. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
  1494. KLoc.push_back(Tok.getLocation());
  1495. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1496. Tok.isNot(tok::annot_pragma_openmp_end))
  1497. ConsumeAnyToken();
  1498. // Parse ':'
  1499. if (Tok.is(tok::colon))
  1500. ConsumeAnyToken();
  1501. else if (Arg.back() != OMPC_DEFAULTMAP_MODIFIER_unknown)
  1502. Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
  1503. // Get a defaultmap kind
  1504. Arg.push_back(getOpenMPSimpleClauseType(
  1505. Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
  1506. KLoc.push_back(Tok.getLocation());
  1507. if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
  1508. Tok.isNot(tok::annot_pragma_openmp_end))
  1509. ConsumeAnyToken();
  1510. } else {
  1511. assert(Kind == OMPC_if);
  1512. KLoc.push_back(Tok.getLocation());
  1513. TentativeParsingAction TPA(*this);
  1514. Arg.push_back(parseOpenMPDirectiveKind(*this));
  1515. if (Arg.back() != OMPD_unknown) {
  1516. ConsumeToken();
  1517. if (Tok.is(tok::colon) && getLangOpts().OpenMP > 40) {
  1518. TPA.Commit();
  1519. DelimLoc = ConsumeToken();
  1520. } else {
  1521. TPA.Revert();
  1522. Arg.back() = OMPD_unknown;
  1523. }
  1524. } else {
  1525. TPA.Revert();
  1526. }
  1527. }
  1528. bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
  1529. (Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
  1530. Kind == OMPC_if;
  1531. if (NeedAnExpression) {
  1532. SourceLocation ELoc = Tok.getLocation();
  1533. ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
  1534. Val = ParseRHSOfBinaryExpression(LHS, prec::Conditional);
  1535. Val = Actions.ActOnFinishFullExpr(Val.get(), ELoc);
  1536. }
  1537. // Parse ')'.
  1538. T.consumeClose();
  1539. if (NeedAnExpression && Val.isInvalid())
  1540. return nullptr;
  1541. if (ParseOnly)
  1542. return nullptr;
  1543. return Actions.ActOnOpenMPSingleExprWithArgClause(
  1544. Kind, Arg, Val.get(), Loc, T.getOpenLocation(), KLoc, DelimLoc,
  1545. T.getCloseLocation());
  1546. }
  1547. static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
  1548. UnqualifiedId &ReductionId) {
  1549. if (ReductionIdScopeSpec.isEmpty()) {
  1550. auto OOK = OO_None;
  1551. switch (P.getCurToken().getKind()) {
  1552. case tok::plus:
  1553. OOK = OO_Plus;
  1554. break;
  1555. case tok::minus:
  1556. OOK = OO_Minus;
  1557. break;
  1558. case tok::star:
  1559. OOK = OO_Star;
  1560. break;
  1561. case tok::amp:
  1562. OOK = OO_Amp;
  1563. break;
  1564. case tok::pipe:
  1565. OOK = OO_Pipe;
  1566. break;
  1567. case tok::caret:
  1568. OOK = OO_Caret;
  1569. break;
  1570. case tok::ampamp:
  1571. OOK = OO_AmpAmp;
  1572. break;
  1573. case tok::pipepipe:
  1574. OOK = OO_PipePipe;
  1575. break;
  1576. default:
  1577. break;
  1578. }
  1579. if (OOK != OO_None) {
  1580. SourceLocation OpLoc = P.ConsumeToken();
  1581. SourceLocation SymbolLocations[] = {OpLoc, OpLoc, SourceLocation()};
  1582. ReductionId.setOperatorFunctionId(OpLoc, OOK, SymbolLocations);
  1583. return false;
  1584. }
  1585. }
  1586. return P.ParseUnqualifiedId(ReductionIdScopeSpec, /*EnteringContext*/ false,
  1587. /*AllowDestructorName*/ false,
  1588. /*AllowConstructorName*/ false,
  1589. /*AllowDeductionGuide*/ false,
  1590. nullptr, nullptr, ReductionId);
  1591. }
  1592. /// Parses clauses with list.
  1593. bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
  1594. OpenMPClauseKind Kind,
  1595. SmallVectorImpl<Expr *> &Vars,
  1596. OpenMPVarListDataTy &Data) {
  1597. UnqualifiedId UnqualifiedReductionId;
  1598. bool InvalidReductionId = false;
  1599. bool MapTypeModifierSpecified = false;
  1600. // Parse '('.
  1601. BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
  1602. if (T.expectAndConsume(diag::err_expected_lparen_after,
  1603. getOpenMPClauseName(Kind)))
  1604. return true;
  1605. bool NeedRParenForLinear = false;
  1606. BalancedDelimiterTracker LinearT(*this, tok::l_paren,
  1607. tok::annot_pragma_openmp_end);
  1608. // Handle reduction-identifier for reduction clause.
  1609. if (Kind == OMPC_reduction || Kind == OMPC_task_reduction ||
  1610. Kind == OMPC_in_reduction) {
  1611. ColonProtectionRAIIObject ColonRAII(*this);
  1612. if (getLangOpts().CPlusPlus)
  1613. ParseOptionalCXXScopeSpecifier(Data.ReductionIdScopeSpec,
  1614. /*ObjectType=*/nullptr,
  1615. /*EnteringContext=*/false);
  1616. InvalidReductionId = ParseReductionId(*this, Data.ReductionIdScopeSpec,
  1617. UnqualifiedReductionId);
  1618. if (InvalidReductionId) {
  1619. SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
  1620. StopBeforeMatch);
  1621. }
  1622. if (Tok.is(tok::colon))
  1623. Data.ColonLoc = ConsumeToken();
  1624. else
  1625. Diag(Tok, diag::warn_pragma_expected_colon) << "reduction identifier";
  1626. if (!InvalidReductionId)
  1627. Data.ReductionId =
  1628. Actions.GetNameFromUnqualifiedId(UnqualifiedReductionId);
  1629. } else if (Kind == OMPC_depend) {
  1630. // Handle dependency type for depend clause.
  1631. ColonProtectionRAIIObject ColonRAII(*this);
  1632. Data.DepKind =
  1633. static_cast<OpenMPDependClauseKind>(getOpenMPSimpleClauseType(
  1634. Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : ""));
  1635. Data.DepLinMapLoc = Tok.getLocation();
  1636. if (Data.DepKind == OMPC_DEPEND_unknown) {
  1637. SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
  1638. StopBeforeMatch);
  1639. } else {
  1640. ConsumeToken();
  1641. // Special processing for depend(source) clause.
  1642. if (DKind == OMPD_ordered && Data.DepKind == OMPC_DEPEND_source) {
  1643. // Parse ')'.
  1644. T.consumeClose();
  1645. return false;
  1646. }
  1647. }
  1648. if (Tok.is(tok::colon)) {
  1649. Data.ColonLoc = ConsumeToken();
  1650. } else {
  1651. Diag(Tok, DKind == OMPD_ordered ? diag::warn_pragma_expected_colon_r_paren
  1652. : diag::warn_pragma_expected_colon)
  1653. << "dependency type";
  1654. }
  1655. } else if (Kind == OMPC_linear) {
  1656. // Try to parse modifier if any.
  1657. if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
  1658. Data.LinKind = static_cast<OpenMPLinearClauseKind>(
  1659. getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
  1660. Data.DepLinMapLoc = ConsumeToken();
  1661. LinearT.consumeOpen();
  1662. NeedRParenForLinear = true;
  1663. }
  1664. } else if (Kind == OMPC_map) {
  1665. // Handle map type for map clause.
  1666. ColonProtectionRAIIObject ColonRAII(*this);
  1667. /// The map clause modifier token can be either a identifier or the C++
  1668. /// delete keyword.
  1669. auto &&IsMapClauseModifierToken = [](const Token &Tok) -> bool {
  1670. return Tok.isOneOf(tok::identifier, tok::kw_delete);
  1671. };
  1672. // The first identifier may be a list item, a map-type or a
  1673. // map-type-modifier. The map modifier can also be delete which has the same
  1674. // spelling of the C++ delete keyword.
  1675. Data.MapType =
  1676. IsMapClauseModifierToken(Tok)
  1677. ? static_cast<OpenMPMapClauseKind>(
  1678. getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
  1679. : OMPC_MAP_unknown;
  1680. Data.DepLinMapLoc = Tok.getLocation();
  1681. bool ColonExpected = false;
  1682. if (IsMapClauseModifierToken(Tok)) {
  1683. if (PP.LookAhead(0).is(tok::colon)) {
  1684. if (Data.MapType == OMPC_MAP_unknown)
  1685. Diag(Tok, diag::err_omp_unknown_map_type);
  1686. else if (Data.MapType == OMPC_MAP_always)
  1687. Diag(Tok, diag::err_omp_map_type_missing);
  1688. ConsumeToken();
  1689. } else if (PP.LookAhead(0).is(tok::comma)) {
  1690. if (IsMapClauseModifierToken(PP.LookAhead(1)) &&
  1691. PP.LookAhead(2).is(tok::colon)) {
  1692. Data.MapTypeModifier = Data.MapType;
  1693. if (Data.MapTypeModifier != OMPC_MAP_always) {
  1694. Diag(Tok, diag::err_omp_unknown_map_type_modifier);
  1695. Data.MapTypeModifier = OMPC_MAP_unknown;
  1696. } else {
  1697. MapTypeModifierSpecified = true;
  1698. }
  1699. ConsumeToken();
  1700. ConsumeToken();
  1701. Data.MapType =
  1702. IsMapClauseModifierToken(Tok)
  1703. ? static_cast<OpenMPMapClauseKind>(
  1704. getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
  1705. : OMPC_MAP_unknown;
  1706. if (Data.MapType == OMPC_MAP_unknown ||
  1707. Data.MapType == OMPC_MAP_always)
  1708. Diag(Tok, diag::err_omp_unknown_map_type);
  1709. ConsumeToken();
  1710. } else {
  1711. Data.MapType = OMPC_MAP_tofrom;
  1712. Data.IsMapTypeImplicit = true;
  1713. }
  1714. } else if (IsMapClauseModifierToken(PP.LookAhead(0))) {
  1715. if (PP.LookAhead(1).is(tok::colon)) {
  1716. Data.MapTypeModifier = Data.MapType;
  1717. if (Data.MapTypeModifier != OMPC_MAP_always) {
  1718. Diag(Tok, diag::err_omp_unknown_map_type_modifier);
  1719. Data.MapTypeModifier = OMPC_MAP_unknown;
  1720. } else {
  1721. MapTypeModifierSpecified = true;
  1722. }
  1723. ConsumeToken();
  1724. Data.MapType =
  1725. IsMapClauseModifierToken(Tok)
  1726. ? static_cast<OpenMPMapClauseKind>(
  1727. getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)))
  1728. : OMPC_MAP_unknown;
  1729. if (Data.MapType == OMPC_MAP_unknown ||
  1730. Data.MapType == OMPC_MAP_always)
  1731. Diag(Tok, diag::err_omp_unknown_map_type);
  1732. ConsumeToken();
  1733. } else {
  1734. Data.MapType = OMPC_MAP_tofrom;
  1735. Data.IsMapTypeImplicit = true;
  1736. }
  1737. } else {
  1738. Data.MapType = OMPC_MAP_tofrom;
  1739. Data.IsMapTypeImplicit = true;
  1740. }
  1741. } else {
  1742. Data.MapType = OMPC_MAP_tofrom;
  1743. Data.IsMapTypeImplicit = true;
  1744. }
  1745. if (Tok.is(tok::colon))
  1746. Data.ColonLoc = ConsumeToken();
  1747. else if (ColonExpected)
  1748. Diag(Tok, diag::warn_pragma_expected_colon) << "map type";
  1749. }
  1750. bool IsComma =
  1751. (Kind != OMPC_reduction && Kind != OMPC_task_reduction &&
  1752. Kind != OMPC_in_reduction && Kind != OMPC_depend && Kind != OMPC_map) ||
  1753. (Kind == OMPC_reduction && !InvalidReductionId) ||
  1754. (Kind == OMPC_map && Data.MapType != OMPC_MAP_unknown &&
  1755. (!MapTypeModifierSpecified ||
  1756. Data.MapTypeModifier == OMPC_MAP_always)) ||
  1757. (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown);
  1758. const bool MayHaveTail = (Kind == OMPC_linear || Kind == OMPC_aligned);
  1759. while (IsComma || (Tok.isNot(tok::r_paren) && Tok.isNot(tok::colon) &&
  1760. Tok.isNot(tok::annot_pragma_openmp_end))) {
  1761. ColonProtectionRAIIObject ColonRAII(*this, MayHaveTail);
  1762. // Parse variable
  1763. ExprResult VarExpr =
  1764. Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
  1765. if (VarExpr.isUsable()) {
  1766. Vars.push_back(VarExpr.get());
  1767. } else {
  1768. SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
  1769. StopBeforeMatch);
  1770. }
  1771. // Skip ',' if any
  1772. IsComma = Tok.is(tok::comma);
  1773. if (IsComma)
  1774. ConsumeToken();
  1775. else if (Tok.isNot(tok::r_paren) &&
  1776. Tok.isNot(tok::annot_pragma_openmp_end) &&
  1777. (!MayHaveTail || Tok.isNot(tok::colon)))
  1778. Diag(Tok, diag::err_omp_expected_punc)
  1779. << ((Kind == OMPC_flush) ? getOpenMPDirectiveName(OMPD_flush)
  1780. : getOpenMPClauseName(Kind))
  1781. << (Kind == OMPC_flush);
  1782. }
  1783. // Parse ')' for linear clause with modifier.
  1784. if (NeedRParenForLinear)
  1785. LinearT.consumeClose();
  1786. // Parse ':' linear-step (or ':' alignment).
  1787. const bool MustHaveTail = MayHaveTail && Tok.is(tok::colon);
  1788. if (MustHaveTail) {
  1789. Data.ColonLoc = Tok.getLocation();
  1790. SourceLocation ELoc = ConsumeToken();
  1791. ExprResult Tail = ParseAssignmentExpression();
  1792. Tail = Actions.ActOnFinishFullExpr(Tail.get(), ELoc);
  1793. if (Tail.isUsable())
  1794. Data.TailExpr = Tail.get();
  1795. else
  1796. SkipUntil(tok::comma, tok::r_paren, tok::annot_pragma_openmp_end,
  1797. StopBeforeMatch);
  1798. }
  1799. // Parse ')'.
  1800. T.consumeClose();
  1801. return (Kind == OMPC_depend && Data.DepKind != OMPC_DEPEND_unknown &&
  1802. Vars.empty()) ||
  1803. (Kind != OMPC_depend && Kind != OMPC_map && Vars.empty()) ||
  1804. (MustHaveTail && !Data.TailExpr) || InvalidReductionId;
  1805. }
  1806. /// \brief Parsing of OpenMP clause 'private', 'firstprivate', 'lastprivate',
  1807. /// 'shared', 'copyin', 'copyprivate', 'flush', 'reduction', 'task_reduction' or
  1808. /// 'in_reduction'.
  1809. ///
  1810. /// private-clause:
  1811. /// 'private' '(' list ')'
  1812. /// firstprivate-clause:
  1813. /// 'firstprivate' '(' list ')'
  1814. /// lastprivate-clause:
  1815. /// 'lastprivate' '(' list ')'
  1816. /// shared-clause:
  1817. /// 'shared' '(' list ')'
  1818. /// linear-clause:
  1819. /// 'linear' '(' linear-list [ ':' linear-step ] ')'
  1820. /// aligned-clause:
  1821. /// 'aligned' '(' list [ ':' alignment ] ')'
  1822. /// reduction-clause:
  1823. /// 'reduction' '(' reduction-identifier ':' list ')'
  1824. /// task_reduction-clause:
  1825. /// 'task_reduction' '(' reduction-identifier ':' list ')'
  1826. /// in_reduction-clause:
  1827. /// 'in_reduction' '(' reduction-identifier ':' list ')'
  1828. /// copyprivate-clause:
  1829. /// 'copyprivate' '(' list ')'
  1830. /// flush-clause:
  1831. /// 'flush' '(' list ')'
  1832. /// depend-clause:
  1833. /// 'depend' '(' in | out | inout : list | source ')'
  1834. /// map-clause:
  1835. /// 'map' '(' [ [ always , ]
  1836. /// to | from | tofrom | alloc | release | delete ':' ] list ')';
  1837. /// to-clause:
  1838. /// 'to' '(' list ')'
  1839. /// from-clause:
  1840. /// 'from' '(' list ')'
  1841. /// use_device_ptr-clause:
  1842. /// 'use_device_ptr' '(' list ')'
  1843. /// is_device_ptr-clause:
  1844. /// 'is_device_ptr' '(' list ')'
  1845. ///
  1846. /// For 'linear' clause linear-list may have the following forms:
  1847. /// list
  1848. /// modifier(list)
  1849. /// where modifier is 'val' (C) or 'ref', 'val' or 'uval'(C++).
  1850. OMPClause *Parser::ParseOpenMPVarListClause(OpenMPDirectiveKind DKind,
  1851. OpenMPClauseKind Kind,
  1852. bool ParseOnly) {
  1853. SourceLocation Loc = Tok.getLocation();
  1854. SourceLocation LOpen = ConsumeToken();
  1855. SmallVector<Expr *, 4> Vars;
  1856. OpenMPVarListDataTy Data;
  1857. if (ParseOpenMPVarList(DKind, Kind, Vars, Data))
  1858. return nullptr;
  1859. if (ParseOnly)
  1860. return nullptr;
  1861. return Actions.ActOnOpenMPVarListClause(
  1862. Kind, Vars, Data.TailExpr, Loc, LOpen, Data.ColonLoc, Tok.getLocation(),
  1863. Data.ReductionIdScopeSpec, Data.ReductionId, Data.DepKind, Data.LinKind,
  1864. Data.MapTypeModifier, Data.MapType, Data.IsMapTypeImplicit,
  1865. Data.DepLinMapLoc);
  1866. }