ParseOpenMP.cpp 73 KB

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