DeclOpenMP.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===--- DeclOpenMP.cpp - Declaration OpenMP AST Node Implementation ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// This file implements OMPThreadPrivateDecl, OMPCapturedExprDecl
  10. /// classes.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/DeclBase.h"
  16. #include "clang/AST/DeclOpenMP.h"
  17. #include "clang/AST/Expr.h"
  18. using namespace clang;
  19. //===----------------------------------------------------------------------===//
  20. // OMPThreadPrivateDecl Implementation.
  21. //===----------------------------------------------------------------------===//
  22. void OMPThreadPrivateDecl::anchor() { }
  23. OMPThreadPrivateDecl *OMPThreadPrivateDecl::Create(ASTContext &C,
  24. DeclContext *DC,
  25. SourceLocation L,
  26. ArrayRef<Expr *> VL) {
  27. OMPThreadPrivateDecl *D =
  28. new (C, DC, additionalSizeToAlloc<Expr *>(VL.size()))
  29. OMPThreadPrivateDecl(OMPThreadPrivate, DC, L);
  30. D->NumVars = VL.size();
  31. D->setVars(VL);
  32. return D;
  33. }
  34. OMPThreadPrivateDecl *OMPThreadPrivateDecl::CreateDeserialized(ASTContext &C,
  35. unsigned ID,
  36. unsigned N) {
  37. OMPThreadPrivateDecl *D = new (C, ID, additionalSizeToAlloc<Expr *>(N))
  38. OMPThreadPrivateDecl(OMPThreadPrivate, nullptr, SourceLocation());
  39. D->NumVars = N;
  40. return D;
  41. }
  42. void OMPThreadPrivateDecl::setVars(ArrayRef<Expr *> VL) {
  43. assert(VL.size() == NumVars &&
  44. "Number of variables is not the same as the preallocated buffer");
  45. std::uninitialized_copy(VL.begin(), VL.end(), getTrailingObjects<Expr *>());
  46. }
  47. //===----------------------------------------------------------------------===//
  48. // OMPAllocateDecl Implementation.
  49. //===----------------------------------------------------------------------===//
  50. void OMPAllocateDecl::anchor() { }
  51. OMPAllocateDecl *OMPAllocateDecl::Create(ASTContext &C, DeclContext *DC,
  52. SourceLocation L, ArrayRef<Expr *> VL,
  53. ArrayRef<OMPClause *> CL) {
  54. OMPAllocateDecl *D = new (
  55. C, DC, additionalSizeToAlloc<Expr *, OMPClause *>(VL.size(), CL.size()))
  56. OMPAllocateDecl(OMPAllocate, DC, L);
  57. D->NumVars = VL.size();
  58. D->setVars(VL);
  59. D->NumClauses = CL.size();
  60. D->setClauses(CL);
  61. return D;
  62. }
  63. OMPAllocateDecl *OMPAllocateDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  64. unsigned NVars,
  65. unsigned NClauses) {
  66. OMPAllocateDecl *D =
  67. new (C, ID, additionalSizeToAlloc<Expr *, OMPClause *>(NVars, NClauses))
  68. OMPAllocateDecl(OMPAllocate, nullptr, SourceLocation());
  69. D->NumVars = NVars;
  70. D->NumClauses = NClauses;
  71. return D;
  72. }
  73. void OMPAllocateDecl::setVars(ArrayRef<Expr *> VL) {
  74. assert(VL.size() == NumVars &&
  75. "Number of variables is not the same as the preallocated buffer");
  76. std::uninitialized_copy(VL.begin(), VL.end(), getTrailingObjects<Expr *>());
  77. }
  78. void OMPAllocateDecl::setClauses(ArrayRef<OMPClause *> CL) {
  79. assert(CL.size() == NumClauses &&
  80. "Number of variables is not the same as the preallocated buffer");
  81. std::uninitialized_copy(CL.begin(), CL.end(),
  82. getTrailingObjects<OMPClause *>());
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // OMPRequiresDecl Implementation.
  86. //===----------------------------------------------------------------------===//
  87. void OMPRequiresDecl::anchor() {}
  88. OMPRequiresDecl *OMPRequiresDecl::Create(ASTContext &C, DeclContext *DC,
  89. SourceLocation L,
  90. ArrayRef<OMPClause *> CL) {
  91. OMPRequiresDecl *D =
  92. new (C, DC, additionalSizeToAlloc<OMPClause *>(CL.size()))
  93. OMPRequiresDecl(OMPRequires, DC, L);
  94. D->NumClauses = CL.size();
  95. D->setClauses(CL);
  96. return D;
  97. }
  98. OMPRequiresDecl *OMPRequiresDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  99. unsigned N) {
  100. OMPRequiresDecl *D = new (C, ID, additionalSizeToAlloc<OMPClause *>(N))
  101. OMPRequiresDecl(OMPRequires, nullptr, SourceLocation());
  102. D->NumClauses = N;
  103. return D;
  104. }
  105. void OMPRequiresDecl::setClauses(ArrayRef<OMPClause *> CL) {
  106. assert(CL.size() == NumClauses &&
  107. "Number of clauses is not the same as the preallocated buffer");
  108. std::uninitialized_copy(CL.begin(), CL.end(),
  109. getTrailingObjects<OMPClause *>());
  110. }
  111. //===----------------------------------------------------------------------===//
  112. // OMPDeclareReductionDecl Implementation.
  113. //===----------------------------------------------------------------------===//
  114. OMPDeclareReductionDecl::OMPDeclareReductionDecl(
  115. Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name,
  116. QualType Ty, OMPDeclareReductionDecl *PrevDeclInScope)
  117. : ValueDecl(DK, DC, L, Name, Ty), DeclContext(DK), Combiner(nullptr),
  118. PrevDeclInScope(PrevDeclInScope) {
  119. setInitializer(nullptr, CallInit);
  120. }
  121. void OMPDeclareReductionDecl::anchor() {}
  122. OMPDeclareReductionDecl *OMPDeclareReductionDecl::Create(
  123. ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name,
  124. QualType T, OMPDeclareReductionDecl *PrevDeclInScope) {
  125. return new (C, DC) OMPDeclareReductionDecl(OMPDeclareReduction, DC, L, Name,
  126. T, PrevDeclInScope);
  127. }
  128. OMPDeclareReductionDecl *
  129. OMPDeclareReductionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  130. return new (C, ID) OMPDeclareReductionDecl(
  131. OMPDeclareReduction, /*DC=*/nullptr, SourceLocation(), DeclarationName(),
  132. QualType(), /*PrevDeclInScope=*/nullptr);
  133. }
  134. OMPDeclareReductionDecl *OMPDeclareReductionDecl::getPrevDeclInScope() {
  135. return cast_or_null<OMPDeclareReductionDecl>(
  136. PrevDeclInScope.get(getASTContext().getExternalSource()));
  137. }
  138. const OMPDeclareReductionDecl *
  139. OMPDeclareReductionDecl::getPrevDeclInScope() const {
  140. return cast_or_null<OMPDeclareReductionDecl>(
  141. PrevDeclInScope.get(getASTContext().getExternalSource()));
  142. }
  143. //===----------------------------------------------------------------------===//
  144. // OMPDeclareMapperDecl Implementation.
  145. //===----------------------------------------------------------------------===//
  146. void OMPDeclareMapperDecl::anchor() {}
  147. OMPDeclareMapperDecl *
  148. OMPDeclareMapperDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
  149. DeclarationName Name, QualType T,
  150. DeclarationName VarName,
  151. OMPDeclareMapperDecl *PrevDeclInScope) {
  152. return new (C, DC) OMPDeclareMapperDecl(OMPDeclareMapper, DC, L, Name, T,
  153. VarName, PrevDeclInScope);
  154. }
  155. OMPDeclareMapperDecl *OMPDeclareMapperDecl::CreateDeserialized(ASTContext &C,
  156. unsigned ID,
  157. unsigned N) {
  158. auto *D = new (C, ID)
  159. OMPDeclareMapperDecl(OMPDeclareMapper, /*DC=*/nullptr, SourceLocation(),
  160. DeclarationName(), QualType(), DeclarationName(),
  161. /*PrevDeclInScope=*/nullptr);
  162. if (N) {
  163. auto **ClauseStorage = C.Allocate<OMPClause *>(N);
  164. D->Clauses = llvm::makeMutableArrayRef<OMPClause *>(ClauseStorage, N);
  165. }
  166. return D;
  167. }
  168. /// Creates an array of clauses to this mapper declaration and intializes
  169. /// them. The space used to store clause pointers is dynamically allocated,
  170. /// because we do not know the number of clauses when creating
  171. /// OMPDeclareMapperDecl
  172. void OMPDeclareMapperDecl::CreateClauses(ASTContext &C,
  173. ArrayRef<OMPClause *> CL) {
  174. assert(Clauses.empty() && "Number of clauses should be 0 on initialization");
  175. size_t NumClauses = CL.size();
  176. if (NumClauses) {
  177. auto **ClauseStorage = C.Allocate<OMPClause *>(NumClauses);
  178. Clauses = llvm::makeMutableArrayRef<OMPClause *>(ClauseStorage, NumClauses);
  179. setClauses(CL);
  180. }
  181. }
  182. void OMPDeclareMapperDecl::setClauses(ArrayRef<OMPClause *> CL) {
  183. assert(CL.size() == Clauses.size() &&
  184. "Number of clauses is not the same as the preallocated buffer");
  185. std::uninitialized_copy(CL.begin(), CL.end(), Clauses.data());
  186. }
  187. OMPDeclareMapperDecl *OMPDeclareMapperDecl::getPrevDeclInScope() {
  188. return cast_or_null<OMPDeclareMapperDecl>(
  189. PrevDeclInScope.get(getASTContext().getExternalSource()));
  190. }
  191. const OMPDeclareMapperDecl *OMPDeclareMapperDecl::getPrevDeclInScope() const {
  192. return cast_or_null<OMPDeclareMapperDecl>(
  193. PrevDeclInScope.get(getASTContext().getExternalSource()));
  194. }
  195. //===----------------------------------------------------------------------===//
  196. // OMPCapturedExprDecl Implementation.
  197. //===----------------------------------------------------------------------===//
  198. void OMPCapturedExprDecl::anchor() {}
  199. OMPCapturedExprDecl *OMPCapturedExprDecl::Create(ASTContext &C, DeclContext *DC,
  200. IdentifierInfo *Id, QualType T,
  201. SourceLocation StartLoc) {
  202. return new (C, DC) OMPCapturedExprDecl(
  203. C, DC, Id, T, C.getTrivialTypeSourceInfo(T), StartLoc);
  204. }
  205. OMPCapturedExprDecl *OMPCapturedExprDecl::CreateDeserialized(ASTContext &C,
  206. unsigned ID) {
  207. return new (C, ID) OMPCapturedExprDecl(C, nullptr, nullptr, QualType(),
  208. /*TInfo=*/nullptr, SourceLocation());
  209. }
  210. SourceRange OMPCapturedExprDecl::getSourceRange() const {
  211. assert(hasInit());
  212. return SourceRange(getInit()->getBeginLoc(), getInit()->getEndLoc());
  213. }