SemaOpenMP.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. /// \brief This file implements semantic analysis for OpenMP directives and
  11. /// clauses.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Basic/OpenMPKinds.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "clang/AST/DeclOpenMP.h"
  18. #include "clang/AST/StmtCXX.h"
  19. #include "clang/AST/StmtOpenMP.h"
  20. #include "clang/AST/StmtVisitor.h"
  21. #include "clang/Lex/Preprocessor.h"
  22. #include "clang/Sema/Initialization.h"
  23. #include "clang/Sema/SemaInternal.h"
  24. #include "clang/Sema/Lookup.h"
  25. #include "clang/Sema/Scope.h"
  26. #include "clang/Sema/ScopeInfo.h"
  27. using namespace clang;
  28. //===----------------------------------------------------------------------===//
  29. // Stack of data-sharing attributes for variables
  30. //===----------------------------------------------------------------------===//
  31. namespace {
  32. /// \brief Default data sharing attributes, which can be applied to directive.
  33. enum DefaultDataSharingAttributes {
  34. DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
  35. DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
  36. DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
  37. };
  38. /// \brief Stack for tracking declarations used in OpenMP directives and
  39. /// clauses and their data-sharing attributes.
  40. class DSAStackTy {
  41. public:
  42. struct DSAVarData {
  43. OpenMPDirectiveKind DKind;
  44. OpenMPClauseKind CKind;
  45. DeclRefExpr *RefExpr;
  46. DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { }
  47. };
  48. private:
  49. struct DSAInfo {
  50. OpenMPClauseKind Attributes;
  51. DeclRefExpr *RefExpr;
  52. };
  53. typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
  54. struct SharingMapTy {
  55. DeclSAMapTy SharingMap;
  56. DefaultDataSharingAttributes DefaultAttr;
  57. OpenMPDirectiveKind Directive;
  58. DeclarationNameInfo DirectiveName;
  59. Scope *CurScope;
  60. SharingMapTy(OpenMPDirectiveKind DKind,
  61. const DeclarationNameInfo &Name,
  62. Scope *CurScope)
  63. : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
  64. DirectiveName(Name), CurScope(CurScope) { }
  65. SharingMapTy()
  66. : SharingMap(), DefaultAttr(DSA_unspecified),
  67. Directive(OMPD_unknown), DirectiveName(),
  68. CurScope(0) { }
  69. };
  70. typedef SmallVector<SharingMapTy, 64> StackTy;
  71. /// \brief Stack of used declaration and their data-sharing attributes.
  72. StackTy Stack;
  73. Sema &Actions;
  74. typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
  75. DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
  76. public:
  77. explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
  78. void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
  79. Scope *CurScope) {
  80. Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
  81. }
  82. void pop() {
  83. assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
  84. Stack.pop_back();
  85. }
  86. /// \brief Adds explicit data sharing attribute to the specified declaration.
  87. void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
  88. /// \brief Checks if the variable is a local for OpenMP region.
  89. bool isOpenMPLocal(VarDecl *D);
  90. /// \brief Returns data sharing attributes from top of the stack for the
  91. /// specified declaration.
  92. DSAVarData getTopDSA(VarDecl *D);
  93. /// \brief Returns data-sharing attributes for the specified declaration.
  94. DSAVarData getImplicitDSA(VarDecl *D);
  95. /// \brief Checks if the specified variables has \a CKind data-sharing
  96. /// attribute in \a DKind directive.
  97. DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
  98. OpenMPDirectiveKind DKind = OMPD_unknown);
  99. /// \brief Returns currently analyzed directive.
  100. OpenMPDirectiveKind getCurrentDirective() const {
  101. return Stack.back().Directive;
  102. }
  103. /// \brief Set default data sharing attribute to none.
  104. void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
  105. /// \brief Set default data sharing attribute to shared.
  106. void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
  107. DefaultDataSharingAttributes getDefaultDSA() const {
  108. return Stack.back().DefaultAttr;
  109. }
  110. Scope *getCurScope() { return Stack.back().CurScope; }
  111. };
  112. } // end anonymous namespace.
  113. DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
  114. VarDecl *D) {
  115. DSAVarData DVar;
  116. if (Iter == Stack.rend() - 1) {
  117. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  118. // in a region but not in construct]
  119. // File-scope or namespace-scope variables referenced in called routines
  120. // in the region are shared unless they appear in a threadprivate
  121. // directive.
  122. // TODO
  123. if (!D->isFunctionOrMethodVarDecl())
  124. DVar.CKind = OMPC_shared;
  125. // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
  126. // in a region but not in construct]
  127. // Variables with static storage duration that are declared in called
  128. // routines in the region are shared.
  129. if (D->hasGlobalStorage())
  130. DVar.CKind = OMPC_shared;
  131. return DVar;
  132. }
  133. DVar.DKind = Iter->Directive;
  134. // Explicitly specified attributes and local variables with predetermined
  135. // attributes.
  136. if (Iter->SharingMap.count(D)) {
  137. DVar.RefExpr = Iter->SharingMap[D].RefExpr;
  138. DVar.CKind = Iter->SharingMap[D].Attributes;
  139. return DVar;
  140. }
  141. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  142. // in a Construct, C/C++, implicitly determined, p.1]
  143. // In a parallel or task construct, the data-sharing attributes of these
  144. // variables are determined by the default clause, if present.
  145. switch (Iter->DefaultAttr) {
  146. case DSA_shared:
  147. DVar.CKind = OMPC_shared;
  148. return DVar;
  149. case DSA_none:
  150. return DVar;
  151. case DSA_unspecified:
  152. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  153. // in a Construct, implicitly determined, p.2]
  154. // In a parallel construct, if no default clause is present, these
  155. // variables are shared.
  156. if (DVar.DKind == OMPD_parallel) {
  157. DVar.CKind = OMPC_shared;
  158. return DVar;
  159. }
  160. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  161. // in a Construct, implicitly determined, p.4]
  162. // In a task construct, if no default clause is present, a variable that in
  163. // the enclosing context is determined to be shared by all implicit tasks
  164. // bound to the current team is shared.
  165. // TODO
  166. if (DVar.DKind == OMPD_task) {
  167. DSAVarData DVarTemp;
  168. for (StackTy::reverse_iterator I = Iter + 1,
  169. EE = Stack.rend() - 1;
  170. I != EE; ++I) {
  171. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  172. // in a Construct, implicitly determined, p.6]
  173. // In a task construct, if no default clause is present, a variable
  174. // whose data-sharing attribute is not determined by the rules above is
  175. // firstprivate.
  176. DVarTemp = getDSA(I, D);
  177. if (DVarTemp.CKind != OMPC_shared) {
  178. DVar.RefExpr = 0;
  179. DVar.DKind = OMPD_task;
  180. DVar.CKind = OMPC_firstprivate;
  181. return DVar;
  182. }
  183. if (I->Directive == OMPD_parallel) break;
  184. }
  185. DVar.DKind = OMPD_task;
  186. DVar.CKind =
  187. (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
  188. return DVar;
  189. }
  190. }
  191. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  192. // in a Construct, implicitly determined, p.3]
  193. // For constructs other than task, if no default clause is present, these
  194. // variables inherit their data-sharing attributes from the enclosing
  195. // context.
  196. return getDSA(Iter + 1, D);
  197. }
  198. void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
  199. if (A == OMPC_threadprivate) {
  200. Stack[0].SharingMap[D].Attributes = A;
  201. Stack[0].SharingMap[D].RefExpr = E;
  202. } else {
  203. assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
  204. Stack.back().SharingMap[D].Attributes = A;
  205. Stack.back().SharingMap[D].RefExpr = E;
  206. }
  207. }
  208. bool DSAStackTy::isOpenMPLocal(VarDecl *D) {
  209. Scope *CurScope = getCurScope();
  210. while (CurScope && !CurScope->isDeclScope(D))
  211. CurScope = CurScope->getParent();
  212. while (CurScope && !CurScope->isOpenMPDirectiveScope())
  213. CurScope = CurScope->getParent();
  214. bool isOpenMPLocal = !!CurScope;
  215. if (!isOpenMPLocal) {
  216. CurScope = getCurScope();
  217. while (CurScope && !CurScope->isOpenMPDirectiveScope())
  218. CurScope = CurScope->getParent();
  219. isOpenMPLocal =
  220. CurScope &&
  221. isa<CapturedDecl>(D->getDeclContext()) &&
  222. CurScope->getFnParent()->getEntity()->Encloses(D->getDeclContext());
  223. }
  224. return isOpenMPLocal;
  225. }
  226. DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
  227. DSAVarData DVar;
  228. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  229. // in a Construct, C/C++, predetermined, p.1]
  230. // Variables appearing in threadprivate directives are threadprivate.
  231. if (D->getTLSKind() != VarDecl::TLS_None) {
  232. DVar.CKind = OMPC_threadprivate;
  233. return DVar;
  234. }
  235. if (Stack[0].SharingMap.count(D)) {
  236. DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
  237. DVar.CKind = OMPC_threadprivate;
  238. return DVar;
  239. }
  240. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  241. // in a Construct, C/C++, predetermined, p.1]
  242. // Variables with automatic storage duration that are declared in a scope
  243. // inside the construct are private.
  244. if (isOpenMPLocal(D) && D->isLocalVarDecl() &&
  245. (D->getStorageClass() == SC_Auto ||
  246. D->getStorageClass() == SC_None)) {
  247. DVar.CKind = OMPC_private;
  248. return DVar;
  249. }
  250. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  251. // in a Construct, C/C++, predetermined, p.4]
  252. // Static data memebers are shared.
  253. if (D->isStaticDataMember()) {
  254. // Variables with const-qualified type having no mutable member may be listed
  255. // in a firstprivate clause, even if they are static data members.
  256. DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
  257. if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
  258. return DVar;
  259. DVar.CKind = OMPC_shared;
  260. return DVar;
  261. }
  262. QualType Type = D->getType().getNonReferenceType().getCanonicalType();
  263. bool IsConstant = Type.isConstant(Actions.getASTContext());
  264. while (Type->isArrayType()) {
  265. QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
  266. Type = ElemType.getNonReferenceType().getCanonicalType();
  267. }
  268. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  269. // in a Construct, C/C++, predetermined, p.6]
  270. // Variables with const qualified type having no mutable member are
  271. // shared.
  272. CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
  273. Type->getAsCXXRecordDecl() : 0;
  274. if (IsConstant &&
  275. !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
  276. // Variables with const-qualified type having no mutable member may be
  277. // listed in a firstprivate clause, even if they are static data members.
  278. DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
  279. if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
  280. return DVar;
  281. DVar.CKind = OMPC_shared;
  282. return DVar;
  283. }
  284. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  285. // in a Construct, C/C++, predetermined, p.7]
  286. // Variables with static storage duration that are declared in a scope
  287. // inside the construct are shared.
  288. if (isOpenMPLocal(D) && D->isStaticLocal()) {
  289. DVar.CKind = OMPC_shared;
  290. return DVar;
  291. }
  292. // Explicitly specified attributes and local variables with predetermined
  293. // attributes.
  294. if (Stack.back().SharingMap.count(D)) {
  295. DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
  296. DVar.CKind = Stack.back().SharingMap[D].Attributes;
  297. }
  298. return DVar;
  299. }
  300. DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
  301. return getDSA(Stack.rbegin() + 1, D);
  302. }
  303. DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
  304. OpenMPDirectiveKind DKind) {
  305. for (StackTy::reverse_iterator I = Stack.rbegin() + 1,
  306. E = Stack.rend() - 1;
  307. I != E; ++I) {
  308. if (DKind != OMPD_unknown && DKind != I->Directive) continue;
  309. DSAVarData DVar = getDSA(I, D);
  310. if (DVar.CKind == CKind)
  311. return DVar;
  312. }
  313. return DSAVarData();
  314. }
  315. void Sema::InitDataSharingAttributesStack() {
  316. VarDataSharingAttributesStack = new DSAStackTy(*this);
  317. }
  318. #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
  319. void Sema::DestroyDataSharingAttributesStack() {
  320. delete DSAStack;
  321. }
  322. void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
  323. const DeclarationNameInfo &DirName,
  324. Scope *CurScope) {
  325. DSAStack->push(DKind, DirName, CurScope);
  326. PushExpressionEvaluationContext(PotentiallyEvaluated);
  327. }
  328. void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
  329. DSAStack->pop();
  330. DiscardCleanupsInEvaluationContext();
  331. PopExpressionEvaluationContext();
  332. }
  333. namespace {
  334. class VarDeclFilterCCC : public CorrectionCandidateCallback {
  335. private:
  336. Sema &Actions;
  337. public:
  338. VarDeclFilterCCC(Sema &S) : Actions(S) { }
  339. virtual bool ValidateCandidate(const TypoCorrection &Candidate) {
  340. NamedDecl *ND = Candidate.getCorrectionDecl();
  341. if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
  342. return VD->hasGlobalStorage() &&
  343. Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
  344. Actions.getCurScope());
  345. }
  346. return false;
  347. }
  348. };
  349. }
  350. ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
  351. CXXScopeSpec &ScopeSpec,
  352. const DeclarationNameInfo &Id) {
  353. LookupResult Lookup(*this, Id, LookupOrdinaryName);
  354. LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
  355. if (Lookup.isAmbiguous())
  356. return ExprError();
  357. VarDecl *VD;
  358. if (!Lookup.isSingleResult()) {
  359. VarDeclFilterCCC Validator(*this);
  360. if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
  361. 0, Validator)) {
  362. diagnoseTypo(Corrected,
  363. PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
  364. : diag::err_omp_expected_var_arg_suggest)
  365. << Id.getName());
  366. VD = Corrected.getCorrectionDeclAs<VarDecl>();
  367. } else {
  368. Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
  369. : diag::err_omp_expected_var_arg)
  370. << Id.getName();
  371. return ExprError();
  372. }
  373. } else {
  374. if (!(VD = Lookup.getAsSingle<VarDecl>())) {
  375. Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
  376. << Id.getName();
  377. Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
  378. return ExprError();
  379. }
  380. }
  381. Lookup.suppressDiagnostics();
  382. // OpenMP [2.9.2, Syntax, C/C++]
  383. // Variables must be file-scope, namespace-scope, or static block-scope.
  384. if (!VD->hasGlobalStorage()) {
  385. Diag(Id.getLoc(), diag::err_omp_global_var_arg)
  386. << getOpenMPDirectiveName(OMPD_threadprivate)
  387. << !VD->isStaticLocal();
  388. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  389. VarDecl::DeclarationOnly;
  390. Diag(VD->getLocation(),
  391. IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
  392. return ExprError();
  393. }
  394. VarDecl *CanonicalVD = VD->getCanonicalDecl();
  395. NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
  396. // OpenMP [2.9.2, Restrictions, C/C++, p.2]
  397. // A threadprivate directive for file-scope variables must appear outside
  398. // any definition or declaration.
  399. if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
  400. !getCurLexicalContext()->isTranslationUnit()) {
  401. Diag(Id.getLoc(), diag::err_omp_var_scope)
  402. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  403. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  404. VarDecl::DeclarationOnly;
  405. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  406. diag::note_defined_here) << VD;
  407. return ExprError();
  408. }
  409. // OpenMP [2.9.2, Restrictions, C/C++, p.3]
  410. // A threadprivate directive for static class member variables must appear
  411. // in the class definition, in the same scope in which the member
  412. // variables are declared.
  413. if (CanonicalVD->isStaticDataMember() &&
  414. !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
  415. Diag(Id.getLoc(), diag::err_omp_var_scope)
  416. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  417. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  418. VarDecl::DeclarationOnly;
  419. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  420. diag::note_defined_here) << VD;
  421. return ExprError();
  422. }
  423. // OpenMP [2.9.2, Restrictions, C/C++, p.4]
  424. // A threadprivate directive for namespace-scope variables must appear
  425. // outside any definition or declaration other than the namespace
  426. // definition itself.
  427. if (CanonicalVD->getDeclContext()->isNamespace() &&
  428. (!getCurLexicalContext()->isFileContext() ||
  429. !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
  430. Diag(Id.getLoc(), diag::err_omp_var_scope)
  431. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  432. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  433. VarDecl::DeclarationOnly;
  434. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  435. diag::note_defined_here) << VD;
  436. return ExprError();
  437. }
  438. // OpenMP [2.9.2, Restrictions, C/C++, p.6]
  439. // A threadprivate directive for static block-scope variables must appear
  440. // in the scope of the variable and not in a nested scope.
  441. if (CanonicalVD->isStaticLocal() && CurScope &&
  442. !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
  443. Diag(Id.getLoc(), diag::err_omp_var_scope)
  444. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  445. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  446. VarDecl::DeclarationOnly;
  447. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  448. diag::note_defined_here) << VD;
  449. return ExprError();
  450. }
  451. // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
  452. // A threadprivate directive must lexically precede all references to any
  453. // of the variables in its list.
  454. if (VD->isUsed()) {
  455. Diag(Id.getLoc(), diag::err_omp_var_used)
  456. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  457. return ExprError();
  458. }
  459. QualType ExprType = VD->getType().getNonReferenceType();
  460. ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc());
  461. DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate);
  462. return DE;
  463. }
  464. Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
  465. SourceLocation Loc,
  466. ArrayRef<Expr *> VarList) {
  467. if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
  468. CurContext->addDecl(D);
  469. return DeclGroupPtrTy::make(DeclGroupRef(D));
  470. }
  471. return DeclGroupPtrTy();
  472. }
  473. OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
  474. SourceLocation Loc,
  475. ArrayRef<Expr *> VarList) {
  476. SmallVector<Expr *, 8> Vars;
  477. for (ArrayRef<Expr *>::iterator I = VarList.begin(),
  478. E = VarList.end();
  479. I != E; ++I) {
  480. DeclRefExpr *DE = cast<DeclRefExpr>(*I);
  481. VarDecl *VD = cast<VarDecl>(DE->getDecl());
  482. SourceLocation ILoc = DE->getExprLoc();
  483. // OpenMP [2.9.2, Restrictions, C/C++, p.10]
  484. // A threadprivate variable must not have an incomplete type.
  485. if (RequireCompleteType(ILoc, VD->getType(),
  486. diag::err_omp_threadprivate_incomplete_type)) {
  487. continue;
  488. }
  489. // OpenMP [2.9.2, Restrictions, C/C++, p.10]
  490. // A threadprivate variable must not have a reference type.
  491. if (VD->getType()->isReferenceType()) {
  492. Diag(ILoc, diag::err_omp_ref_type_arg)
  493. << getOpenMPDirectiveName(OMPD_threadprivate)
  494. << VD->getType();
  495. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  496. VarDecl::DeclarationOnly;
  497. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  498. diag::note_defined_here) << VD;
  499. continue;
  500. }
  501. // Check if this is a TLS variable.
  502. if (VD->getTLSKind()) {
  503. Diag(ILoc, diag::err_omp_var_thread_local) << VD;
  504. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  505. VarDecl::DeclarationOnly;
  506. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  507. diag::note_defined_here) << VD;
  508. continue;
  509. }
  510. Vars.push_back(*I);
  511. }
  512. return Vars.empty() ?
  513. 0 : OMPThreadPrivateDecl::Create(Context,
  514. getCurLexicalContext(),
  515. Loc, Vars);
  516. }
  517. namespace {
  518. class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
  519. DSAStackTy *Stack;
  520. Sema &Actions;
  521. bool ErrorFound;
  522. CapturedStmt *CS;
  523. llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
  524. public:
  525. void VisitDeclRefExpr(DeclRefExpr *E) {
  526. if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
  527. // Skip internally declared variables.
  528. if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
  529. SourceLocation ELoc = E->getExprLoc();
  530. OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
  531. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
  532. if (DVar.CKind != OMPC_unknown) {
  533. if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
  534. DVar.CKind != OMPC_threadprivate && !DVar.RefExpr)
  535. ImplicitFirstprivate.push_back(DVar.RefExpr);
  536. return;
  537. }
  538. // The default(none) clause requires that each variable that is referenced
  539. // in the construct, and does not have a predetermined data-sharing
  540. // attribute, must have its data-sharing attribute explicitly determined
  541. // by being listed in a data-sharing attribute clause.
  542. if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
  543. (DKind == OMPD_parallel || DKind == OMPD_task)) {
  544. ErrorFound = true;
  545. Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
  546. return;
  547. }
  548. // OpenMP [2.9.3.6, Restrictions, p.2]
  549. // A list item that appears in a reduction clause of the innermost
  550. // enclosing worksharing or parallel construct may not be accessed in an
  551. // explicit task.
  552. // TODO:
  553. // Define implicit data-sharing attributes for task.
  554. DVar = Stack->getImplicitDSA(VD);
  555. if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
  556. ImplicitFirstprivate.push_back(DVar.RefExpr);
  557. }
  558. }
  559. void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
  560. for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
  561. E = S->clauses().end();
  562. I != E; ++I)
  563. if (OMPClause *C = *I)
  564. for (StmtRange R = C->children(); R; ++R)
  565. if (Stmt *Child = *R)
  566. Visit(Child);
  567. }
  568. void VisitStmt(Stmt *S) {
  569. for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
  570. I != E; ++I)
  571. if (Stmt *Child = *I)
  572. if (!isa<OMPExecutableDirective>(Child))
  573. Visit(Child);
  574. }
  575. bool isErrorFound() { return ErrorFound; }
  576. ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
  577. DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
  578. : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
  579. };
  580. }
  581. StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
  582. ArrayRef<OMPClause *> Clauses,
  583. Stmt *AStmt,
  584. SourceLocation StartLoc,
  585. SourceLocation EndLoc) {
  586. assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
  587. StmtResult Res = StmtError();
  588. // Check default data sharing attributes for referenced variables.
  589. DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
  590. DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
  591. if (DSAChecker.isErrorFound())
  592. return StmtError();
  593. // Generate list of implicitly defined firstprivate variables.
  594. llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
  595. ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
  596. bool ErrorFound = false;
  597. if (!DSAChecker.getImplicitFirstprivate().empty()) {
  598. if (OMPClause *Implicit =
  599. ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
  600. SourceLocation(), SourceLocation(),
  601. SourceLocation())) {
  602. ClausesWithImplicit.push_back(Implicit);
  603. ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
  604. DSAChecker.getImplicitFirstprivate().size();
  605. } else
  606. ErrorFound = true;
  607. }
  608. switch (Kind) {
  609. case OMPD_parallel:
  610. Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
  611. StartLoc, EndLoc);
  612. break;
  613. case OMPD_threadprivate:
  614. case OMPD_task:
  615. llvm_unreachable("OpenMP Directive is not allowed");
  616. case OMPD_unknown:
  617. case NUM_OPENMP_DIRECTIVES:
  618. llvm_unreachable("Unknown OpenMP directive");
  619. }
  620. if (ErrorFound) return StmtError();
  621. return Res;
  622. }
  623. StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
  624. Stmt *AStmt,
  625. SourceLocation StartLoc,
  626. SourceLocation EndLoc) {
  627. getCurFunction()->setHasBranchProtectedScope();
  628. return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
  629. Clauses, AStmt));
  630. }
  631. OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
  632. unsigned Argument,
  633. SourceLocation ArgumentLoc,
  634. SourceLocation StartLoc,
  635. SourceLocation LParenLoc,
  636. SourceLocation EndLoc) {
  637. OMPClause *Res = 0;
  638. switch (Kind) {
  639. case OMPC_default:
  640. Res =
  641. ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
  642. ArgumentLoc, StartLoc, LParenLoc, EndLoc);
  643. break;
  644. case OMPC_private:
  645. case OMPC_firstprivate:
  646. case OMPC_shared:
  647. case OMPC_threadprivate:
  648. case OMPC_unknown:
  649. case NUM_OPENMP_CLAUSES:
  650. llvm_unreachable("Clause is not allowed.");
  651. }
  652. return Res;
  653. }
  654. OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
  655. SourceLocation KindKwLoc,
  656. SourceLocation StartLoc,
  657. SourceLocation LParenLoc,
  658. SourceLocation EndLoc) {
  659. if (Kind == OMPC_DEFAULT_unknown) {
  660. std::string Values;
  661. std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : "");
  662. for (unsigned i = OMPC_DEFAULT_unknown + 1;
  663. i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
  664. Values += "'";
  665. Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
  666. Values += "'";
  667. switch (i) {
  668. case NUM_OPENMP_DEFAULT_KINDS - 2:
  669. Values += " or ";
  670. break;
  671. case NUM_OPENMP_DEFAULT_KINDS - 1:
  672. break;
  673. default:
  674. Values += Sep;
  675. break;
  676. }
  677. }
  678. Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
  679. << Values << getOpenMPClauseName(OMPC_default);
  680. return 0;
  681. }
  682. switch (Kind) {
  683. case OMPC_DEFAULT_none:
  684. DSAStack->setDefaultDSANone();
  685. break;
  686. case OMPC_DEFAULT_shared:
  687. DSAStack->setDefaultDSAShared();
  688. break;
  689. default:
  690. break;
  691. }
  692. return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
  693. EndLoc);
  694. }
  695. OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
  696. ArrayRef<Expr *> VarList,
  697. SourceLocation StartLoc,
  698. SourceLocation LParenLoc,
  699. SourceLocation EndLoc) {
  700. OMPClause *Res = 0;
  701. switch (Kind) {
  702. case OMPC_private:
  703. Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  704. break;
  705. case OMPC_firstprivate:
  706. Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  707. break;
  708. case OMPC_shared:
  709. Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
  710. break;
  711. case OMPC_default:
  712. case OMPC_threadprivate:
  713. case OMPC_unknown:
  714. case NUM_OPENMP_CLAUSES:
  715. llvm_unreachable("Clause is not allowed.");
  716. }
  717. return Res;
  718. }
  719. OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
  720. SourceLocation StartLoc,
  721. SourceLocation LParenLoc,
  722. SourceLocation EndLoc) {
  723. SmallVector<Expr *, 8> Vars;
  724. for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
  725. I != E; ++I) {
  726. assert(*I && "NULL expr in OpenMP private clause.");
  727. if (isa<DependentScopeDeclRefExpr>(*I)) {
  728. // It will be analyzed later.
  729. Vars.push_back(*I);
  730. continue;
  731. }
  732. SourceLocation ELoc = (*I)->getExprLoc();
  733. // OpenMP [2.1, C/C++]
  734. // A list item is a variable name.
  735. // OpenMP [2.9.3.3, Restrictions, p.1]
  736. // A variable that is part of another variable (as an array or
  737. // structure element) cannot appear in a private clause.
  738. DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
  739. if (!DE || !isa<VarDecl>(DE->getDecl())) {
  740. Diag(ELoc, diag::err_omp_expected_var_name)
  741. << (*I)->getSourceRange();
  742. continue;
  743. }
  744. Decl *D = DE->getDecl();
  745. VarDecl *VD = cast<VarDecl>(D);
  746. QualType Type = VD->getType();
  747. if (Type->isDependentType() || Type->isInstantiationDependentType()) {
  748. // It will be analyzed later.
  749. Vars.push_back(DE);
  750. continue;
  751. }
  752. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  753. // A variable that appears in a private clause must not have an incomplete
  754. // type or a reference type.
  755. if (RequireCompleteType(ELoc, Type,
  756. diag::err_omp_private_incomplete_type)) {
  757. continue;
  758. }
  759. if (Type->isReferenceType()) {
  760. Diag(ELoc, diag::err_omp_clause_ref_type_arg)
  761. << getOpenMPClauseName(OMPC_private) << Type;
  762. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  763. VarDecl::DeclarationOnly;
  764. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  765. diag::note_defined_here) << VD;
  766. continue;
  767. }
  768. // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
  769. // A variable of class type (or array thereof) that appears in a private
  770. // clause requires an accesible, unambiguous default constructor for the
  771. // class type.
  772. while (Type.getNonReferenceType()->isArrayType()) {
  773. Type = cast<ArrayType>(
  774. Type.getNonReferenceType().getTypePtr())->getElementType();
  775. }
  776. CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
  777. Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
  778. if (RD) {
  779. CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
  780. PartialDiagnostic PD =
  781. PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
  782. if (!CD ||
  783. CheckConstructorAccess(ELoc, CD,
  784. InitializedEntity::InitializeTemporary(Type),
  785. CD->getAccess(), PD) == AR_inaccessible ||
  786. CD->isDeleted()) {
  787. Diag(ELoc, diag::err_omp_required_method)
  788. << getOpenMPClauseName(OMPC_private) << 0;
  789. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  790. VarDecl::DeclarationOnly;
  791. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  792. diag::note_defined_here) << VD;
  793. Diag(RD->getLocation(), diag::note_previous_decl) << RD;
  794. continue;
  795. }
  796. MarkFunctionReferenced(ELoc, CD);
  797. DiagnoseUseOfDecl(CD, ELoc);
  798. CXXDestructorDecl *DD = RD->getDestructor();
  799. if (DD) {
  800. if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
  801. DD->isDeleted()) {
  802. Diag(ELoc, diag::err_omp_required_method)
  803. << getOpenMPClauseName(OMPC_private) << 4;
  804. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  805. VarDecl::DeclarationOnly;
  806. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  807. diag::note_defined_here) << VD;
  808. Diag(RD->getLocation(), diag::note_previous_decl) << RD;
  809. continue;
  810. }
  811. MarkFunctionReferenced(ELoc, DD);
  812. DiagnoseUseOfDecl(DD, ELoc);
  813. }
  814. }
  815. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  816. // in a Construct]
  817. // Variables with the predetermined data-sharing attributes may not be
  818. // listed in data-sharing attributes clauses, except for the cases
  819. // listed below. For these exceptions only, listing a predetermined
  820. // variable in a data-sharing attribute clause is allowed and overrides
  821. // the variable's predetermined data-sharing attributes.
  822. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
  823. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
  824. Diag(ELoc, diag::err_omp_wrong_dsa)
  825. << getOpenMPClauseName(DVar.CKind)
  826. << getOpenMPClauseName(OMPC_private);
  827. if (DVar.RefExpr) {
  828. Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
  829. << getOpenMPClauseName(DVar.CKind);
  830. } else {
  831. Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
  832. << getOpenMPClauseName(DVar.CKind);
  833. }
  834. continue;
  835. }
  836. DSAStack->addDSA(VD, DE, OMPC_private);
  837. Vars.push_back(DE);
  838. }
  839. if (Vars.empty()) return 0;
  840. return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
  841. }
  842. OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
  843. SourceLocation StartLoc,
  844. SourceLocation LParenLoc,
  845. SourceLocation EndLoc) {
  846. SmallVector<Expr *, 8> Vars;
  847. for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
  848. I != E; ++I) {
  849. assert(*I && "NULL expr in OpenMP firstprivate clause.");
  850. if (isa<DependentScopeDeclRefExpr>(*I)) {
  851. // It will be analyzed later.
  852. Vars.push_back(*I);
  853. continue;
  854. }
  855. SourceLocation ELoc = (*I)->getExprLoc();
  856. // OpenMP [2.1, C/C++]
  857. // A list item is a variable name.
  858. // OpenMP [2.9.3.3, Restrictions, p.1]
  859. // A variable that is part of another variable (as an array or
  860. // structure element) cannot appear in a private clause.
  861. DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
  862. if (!DE || !isa<VarDecl>(DE->getDecl())) {
  863. Diag(ELoc, diag::err_omp_expected_var_name)
  864. << (*I)->getSourceRange();
  865. continue;
  866. }
  867. Decl *D = DE->getDecl();
  868. VarDecl *VD = cast<VarDecl>(D);
  869. QualType Type = VD->getType();
  870. if (Type->isDependentType() || Type->isInstantiationDependentType()) {
  871. // It will be analyzed later.
  872. Vars.push_back(DE);
  873. continue;
  874. }
  875. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  876. // A variable that appears in a private clause must not have an incomplete
  877. // type or a reference type.
  878. if (RequireCompleteType(ELoc, Type,
  879. diag::err_omp_firstprivate_incomplete_type)) {
  880. continue;
  881. }
  882. if (Type->isReferenceType()) {
  883. Diag(ELoc, diag::err_omp_clause_ref_type_arg)
  884. << getOpenMPClauseName(OMPC_firstprivate) << Type;
  885. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  886. VarDecl::DeclarationOnly;
  887. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  888. diag::note_defined_here) << VD;
  889. continue;
  890. }
  891. // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
  892. // A variable of class type (or array thereof) that appears in a private
  893. // clause requires an accesible, unambiguous copy constructor for the
  894. // class type.
  895. Type = Context.getBaseElementType(Type);
  896. CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
  897. Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
  898. if (RD) {
  899. CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
  900. PartialDiagnostic PD =
  901. PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
  902. if (!CD ||
  903. CheckConstructorAccess(ELoc, CD,
  904. InitializedEntity::InitializeTemporary(Type),
  905. CD->getAccess(), PD) == AR_inaccessible ||
  906. CD->isDeleted()) {
  907. Diag(ELoc, diag::err_omp_required_method)
  908. << getOpenMPClauseName(OMPC_firstprivate) << 1;
  909. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  910. VarDecl::DeclarationOnly;
  911. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  912. diag::note_defined_here) << VD;
  913. Diag(RD->getLocation(), diag::note_previous_decl) << RD;
  914. continue;
  915. }
  916. MarkFunctionReferenced(ELoc, CD);
  917. DiagnoseUseOfDecl(CD, ELoc);
  918. CXXDestructorDecl *DD = RD->getDestructor();
  919. if (DD) {
  920. if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
  921. DD->isDeleted()) {
  922. Diag(ELoc, diag::err_omp_required_method)
  923. << getOpenMPClauseName(OMPC_firstprivate) << 4;
  924. bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
  925. VarDecl::DeclarationOnly;
  926. Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
  927. diag::note_defined_here) << VD;
  928. Diag(RD->getLocation(), diag::note_previous_decl) << RD;
  929. continue;
  930. }
  931. MarkFunctionReferenced(ELoc, DD);
  932. DiagnoseUseOfDecl(DD, ELoc);
  933. }
  934. }
  935. // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
  936. // variable and it was checked already.
  937. if (StartLoc.isValid() && EndLoc.isValid()) {
  938. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
  939. Type = Type.getNonReferenceType().getCanonicalType();
  940. bool IsConstant = Type.isConstant(Context);
  941. Type = Context.getBaseElementType(Type);
  942. // OpenMP [2.4.13, Data-sharing Attribute Clauses]
  943. // A list item that specifies a given variable may not appear in more
  944. // than one clause on the same directive, except that a variable may be
  945. // specified in both firstprivate and lastprivate clauses.
  946. // TODO: add processing for lastprivate.
  947. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
  948. DVar.RefExpr) {
  949. Diag(ELoc, diag::err_omp_wrong_dsa)
  950. << getOpenMPClauseName(DVar.CKind)
  951. << getOpenMPClauseName(OMPC_firstprivate);
  952. Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
  953. << getOpenMPClauseName(DVar.CKind);
  954. continue;
  955. }
  956. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  957. // in a Construct]
  958. // Variables with the predetermined data-sharing attributes may not be
  959. // listed in data-sharing attributes clauses, except for the cases
  960. // listed below. For these exceptions only, listing a predetermined
  961. // variable in a data-sharing attribute clause is allowed and overrides
  962. // the variable's predetermined data-sharing attributes.
  963. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  964. // in a Construct, C/C++, p.2]
  965. // Variables with const-qualified type having no mutable member may be
  966. // listed in a firstprivate clause, even if they are static data members.
  967. if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
  968. DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
  969. Diag(ELoc, diag::err_omp_wrong_dsa)
  970. << getOpenMPClauseName(DVar.CKind)
  971. << getOpenMPClauseName(OMPC_firstprivate);
  972. Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
  973. << getOpenMPClauseName(DVar.CKind);
  974. continue;
  975. }
  976. // OpenMP [2.9.3.4, Restrictions, p.2]
  977. // A list item that is private within a parallel region must not appear
  978. // in a firstprivate clause on a worksharing construct if any of the
  979. // worksharing regions arising from the worksharing construct ever bind
  980. // to any of the parallel regions arising from the parallel construct.
  981. // OpenMP [2.9.3.4, Restrictions, p.3]
  982. // A list item that appears in a reduction clause of a parallel construct
  983. // must not appear in a firstprivate clause on a worksharing or task
  984. // construct if any of the worksharing or task regions arising from the
  985. // worksharing or task construct ever bind to any of the parallel regions
  986. // arising from the parallel construct.
  987. // OpenMP [2.9.3.4, Restrictions, p.4]
  988. // A list item that appears in a reduction clause in worksharing
  989. // construct must not appear in a firstprivate clause in a task construct
  990. // encountered during execution of any of the worksharing regions arising
  991. // from the worksharing construct.
  992. // TODO:
  993. }
  994. DSAStack->addDSA(VD, DE, OMPC_firstprivate);
  995. Vars.push_back(DE);
  996. }
  997. if (Vars.empty()) return 0;
  998. return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  999. Vars);
  1000. }
  1001. OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
  1002. SourceLocation StartLoc,
  1003. SourceLocation LParenLoc,
  1004. SourceLocation EndLoc) {
  1005. SmallVector<Expr *, 8> Vars;
  1006. for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
  1007. I != E; ++I) {
  1008. assert(*I && "NULL expr in OpenMP shared clause.");
  1009. if (isa<DependentScopeDeclRefExpr>(*I)) {
  1010. // It will be analyzed later.
  1011. Vars.push_back(*I);
  1012. continue;
  1013. }
  1014. SourceLocation ELoc = (*I)->getExprLoc();
  1015. // OpenMP [2.1, C/C++]
  1016. // A list item is a variable name.
  1017. // OpenMP [2.9.3.4, Restrictions, p.1]
  1018. // A variable that is part of another variable (as an array or
  1019. // structure element) cannot appear in a private clause.
  1020. DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
  1021. if (!DE || !isa<VarDecl>(DE->getDecl())) {
  1022. Diag(ELoc, diag::err_omp_expected_var_name)
  1023. << (*I)->getSourceRange();
  1024. continue;
  1025. }
  1026. Decl *D = DE->getDecl();
  1027. VarDecl *VD = cast<VarDecl>(D);
  1028. QualType Type = VD->getType();
  1029. if (Type->isDependentType() || Type->isInstantiationDependentType()) {
  1030. // It will be analyzed later.
  1031. Vars.push_back(DE);
  1032. continue;
  1033. }
  1034. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1035. // in a Construct]
  1036. // Variables with the predetermined data-sharing attributes may not be
  1037. // listed in data-sharing attributes clauses, except for the cases
  1038. // listed below. For these exceptions only, listing a predetermined
  1039. // variable in a data-sharing attribute clause is allowed and overrides
  1040. // the variable's predetermined data-sharing attributes.
  1041. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
  1042. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
  1043. Diag(ELoc, diag::err_omp_wrong_dsa)
  1044. << getOpenMPClauseName(DVar.CKind)
  1045. << getOpenMPClauseName(OMPC_shared);
  1046. Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
  1047. << getOpenMPClauseName(DVar.CKind);
  1048. continue;
  1049. }
  1050. DSAStack->addDSA(VD, DE, OMPC_shared);
  1051. Vars.push_back(DE);
  1052. }
  1053. if (Vars.empty()) return 0;
  1054. return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
  1055. }
  1056. #undef DSAStack