ParseOpenMP.cpp 74 KB

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