1 //===--- OpenMPClause.cpp - Classes for OpenMP clauses --------------------===// 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 // 10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/OpenMPClause.h" 15 16 #include "clang/AST/ASTContext.h" 17 18 using namespace clang; 19 20 OMPClause::child_range OMPClause::children() { 21 switch (getClauseKind()) { 22 default: 23 break; 24 #define OPENMP_CLAUSE(Name, Class) \ 25 case OMPC_##Name: \ 26 return static_cast<Class *>(this)->children(); 27 #include "clang/Basic/OpenMPKinds.def" 28 } 29 llvm_unreachable("unknown OMPClause"); 30 } 31 32 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) { 33 assert(VL.size() == varlist_size() && 34 "Number of private copies is not the same as the preallocated buffer"); 35 std::copy(VL.begin(), VL.end(), varlist_end()); 36 } 37 38 OMPPrivateClause * 39 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc, 40 SourceLocation LParenLoc, SourceLocation EndLoc, 41 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) { 42 // Allocate space for private variables and initializer expressions. 43 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size())); 44 OMPPrivateClause *Clause = 45 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 46 Clause->setVarRefs(VL); 47 Clause->setPrivateCopies(PrivateVL); 48 return Clause; 49 } 50 51 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C, 52 unsigned N) { 53 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N)); 54 return new (Mem) OMPPrivateClause(N); 55 } 56 57 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) { 58 assert(VL.size() == varlist_size() && 59 "Number of private copies is not the same as the preallocated buffer"); 60 std::copy(VL.begin(), VL.end(), varlist_end()); 61 } 62 63 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) { 64 assert(VL.size() == varlist_size() && 65 "Number of inits is not the same as the preallocated buffer"); 66 std::copy(VL.begin(), VL.end(), getPrivateCopies().end()); 67 } 68 69 OMPFirstprivateClause * 70 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc, 71 SourceLocation LParenLoc, SourceLocation EndLoc, 72 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL, 73 ArrayRef<Expr *> InitVL) { 74 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size())); 75 OMPFirstprivateClause *Clause = 76 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 77 Clause->setVarRefs(VL); 78 Clause->setPrivateCopies(PrivateVL); 79 Clause->setInits(InitVL); 80 return Clause; 81 } 82 83 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C, 84 unsigned N) { 85 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N)); 86 return new (Mem) OMPFirstprivateClause(N); 87 } 88 89 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) { 90 assert(PrivateCopies.size() == varlist_size() && 91 "Number of private copies is not the same as the preallocated buffer"); 92 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end()); 93 } 94 95 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) { 96 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is " 97 "not the same as the " 98 "preallocated buffer"); 99 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end()); 100 } 101 102 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) { 103 assert(DstExprs.size() == varlist_size() && "Number of destination " 104 "expressions is not the same as " 105 "the preallocated buffer"); 106 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end()); 107 } 108 109 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) { 110 assert(AssignmentOps.size() == varlist_size() && 111 "Number of assignment expressions is not the same as the preallocated " 112 "buffer"); 113 std::copy(AssignmentOps.begin(), AssignmentOps.end(), 114 getDestinationExprs().end()); 115 } 116 117 OMPLastprivateClause *OMPLastprivateClause::Create( 118 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 119 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs, 120 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) { 121 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size())); 122 OMPLastprivateClause *Clause = 123 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 124 Clause->setVarRefs(VL); 125 Clause->setSourceExprs(SrcExprs); 126 Clause->setDestinationExprs(DstExprs); 127 Clause->setAssignmentOps(AssignmentOps); 128 return Clause; 129 } 130 131 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C, 132 unsigned N) { 133 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N)); 134 return new (Mem) OMPLastprivateClause(N); 135 } 136 137 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C, 138 SourceLocation StartLoc, 139 SourceLocation LParenLoc, 140 SourceLocation EndLoc, 141 ArrayRef<Expr *> VL) { 142 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size())); 143 OMPSharedClause *Clause = 144 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size()); 145 Clause->setVarRefs(VL); 146 return Clause; 147 } 148 149 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) { 150 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N)); 151 return new (Mem) OMPSharedClause(N); 152 } 153 154 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) { 155 assert(PL.size() == varlist_size() && 156 "Number of privates is not the same as the preallocated buffer"); 157 std::copy(PL.begin(), PL.end(), varlist_end()); 158 } 159 160 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) { 161 assert(IL.size() == varlist_size() && 162 "Number of inits is not the same as the preallocated buffer"); 163 std::copy(IL.begin(), IL.end(), getPrivates().end()); 164 } 165 166 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) { 167 assert(UL.size() == varlist_size() && 168 "Number of updates is not the same as the preallocated buffer"); 169 std::copy(UL.begin(), UL.end(), getInits().end()); 170 } 171 172 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) { 173 assert(FL.size() == varlist_size() && 174 "Number of final updates is not the same as the preallocated buffer"); 175 std::copy(FL.begin(), FL.end(), getUpdates().end()); 176 } 177 178 OMPLinearClause *OMPLinearClause::Create( 179 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 180 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, 181 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL, 182 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep) { 183 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions 184 // (Step and CalcStep). 185 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2)); 186 OMPLinearClause *Clause = new (Mem) OMPLinearClause( 187 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size()); 188 Clause->setVarRefs(VL); 189 Clause->setPrivates(PL); 190 Clause->setInits(IL); 191 // Fill update and final expressions with zeroes, they are provided later, 192 // after the directive construction. 193 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(), 194 nullptr); 195 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(), 196 nullptr); 197 Clause->setStep(Step); 198 Clause->setCalcStep(CalcStep); 199 return Clause; 200 } 201 202 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C, 203 unsigned NumVars) { 204 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions 205 // (Step and CalcStep). 206 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2)); 207 return new (Mem) OMPLinearClause(NumVars); 208 } 209 210 OMPAlignedClause * 211 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc, 212 SourceLocation LParenLoc, SourceLocation ColonLoc, 213 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) { 214 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1)); 215 OMPAlignedClause *Clause = new (Mem) 216 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size()); 217 Clause->setVarRefs(VL); 218 Clause->setAlignment(A); 219 return Clause; 220 } 221 222 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C, 223 unsigned NumVars) { 224 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1)); 225 return new (Mem) OMPAlignedClause(NumVars); 226 } 227 228 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) { 229 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is " 230 "not the same as the " 231 "preallocated buffer"); 232 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end()); 233 } 234 235 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) { 236 assert(DstExprs.size() == varlist_size() && "Number of destination " 237 "expressions is not the same as " 238 "the preallocated buffer"); 239 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end()); 240 } 241 242 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) { 243 assert(AssignmentOps.size() == varlist_size() && 244 "Number of assignment expressions is not the same as the preallocated " 245 "buffer"); 246 std::copy(AssignmentOps.begin(), AssignmentOps.end(), 247 getDestinationExprs().end()); 248 } 249 250 OMPCopyinClause *OMPCopyinClause::Create( 251 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 252 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs, 253 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) { 254 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size())); 255 OMPCopyinClause *Clause = 256 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size()); 257 Clause->setVarRefs(VL); 258 Clause->setSourceExprs(SrcExprs); 259 Clause->setDestinationExprs(DstExprs); 260 Clause->setAssignmentOps(AssignmentOps); 261 return Clause; 262 } 263 264 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) { 265 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N)); 266 return new (Mem) OMPCopyinClause(N); 267 } 268 269 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) { 270 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is " 271 "not the same as the " 272 "preallocated buffer"); 273 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end()); 274 } 275 276 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) { 277 assert(DstExprs.size() == varlist_size() && "Number of destination " 278 "expressions is not the same as " 279 "the preallocated buffer"); 280 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end()); 281 } 282 283 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) { 284 assert(AssignmentOps.size() == varlist_size() && 285 "Number of assignment expressions is not the same as the preallocated " 286 "buffer"); 287 std::copy(AssignmentOps.begin(), AssignmentOps.end(), 288 getDestinationExprs().end()); 289 } 290 291 OMPCopyprivateClause *OMPCopyprivateClause::Create( 292 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 293 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs, 294 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) { 295 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size())); 296 OMPCopyprivateClause *Clause = 297 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 298 Clause->setVarRefs(VL); 299 Clause->setSourceExprs(SrcExprs); 300 Clause->setDestinationExprs(DstExprs); 301 Clause->setAssignmentOps(AssignmentOps); 302 return Clause; 303 } 304 305 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C, 306 unsigned N) { 307 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N)); 308 return new (Mem) OMPCopyprivateClause(N); 309 } 310 311 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) { 312 assert(Privates.size() == varlist_size() && 313 "Number of private copies is not the same as the preallocated buffer"); 314 std::copy(Privates.begin(), Privates.end(), varlist_end()); 315 } 316 317 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) { 318 assert( 319 LHSExprs.size() == varlist_size() && 320 "Number of LHS expressions is not the same as the preallocated buffer"); 321 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end()); 322 } 323 324 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) { 325 assert( 326 RHSExprs.size() == varlist_size() && 327 "Number of RHS expressions is not the same as the preallocated buffer"); 328 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end()); 329 } 330 331 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) { 332 assert(ReductionOps.size() == varlist_size() && "Number of reduction " 333 "expressions is not the same " 334 "as the preallocated buffer"); 335 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end()); 336 } 337 338 OMPReductionClause *OMPReductionClause::Create( 339 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 340 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL, 341 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo, 342 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs, 343 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps) { 344 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size())); 345 OMPReductionClause *Clause = new (Mem) OMPReductionClause( 346 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo); 347 Clause->setVarRefs(VL); 348 Clause->setPrivates(Privates); 349 Clause->setLHSExprs(LHSExprs); 350 Clause->setRHSExprs(RHSExprs); 351 Clause->setReductionOps(ReductionOps); 352 return Clause; 353 } 354 355 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C, 356 unsigned N) { 357 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N)); 358 return new (Mem) OMPReductionClause(N); 359 } 360 361 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C, 362 SourceLocation StartLoc, 363 SourceLocation LParenLoc, 364 SourceLocation EndLoc, 365 ArrayRef<Expr *> VL) { 366 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size())); 367 OMPFlushClause *Clause = 368 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size()); 369 Clause->setVarRefs(VL); 370 return Clause; 371 } 372 373 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) { 374 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N)); 375 return new (Mem) OMPFlushClause(N); 376 } 377 378 OMPDependClause * 379 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc, 380 SourceLocation LParenLoc, SourceLocation EndLoc, 381 OpenMPDependClauseKind DepKind, SourceLocation DepLoc, 382 SourceLocation ColonLoc, ArrayRef<Expr *> VL) { 383 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size())); 384 OMPDependClause *Clause = 385 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size()); 386 Clause->setVarRefs(VL); 387 Clause->setDependencyKind(DepKind); 388 Clause->setDependencyLoc(DepLoc); 389 Clause->setColonLoc(ColonLoc); 390 return Clause; 391 } 392 393 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) { 394 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N)); 395 return new (Mem) OMPDependClause(N); 396 } 397 398 OMPMapClause *OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc, 399 SourceLocation LParenLoc, 400 SourceLocation EndLoc, ArrayRef<Expr *> VL, 401 OpenMPMapClauseKind TypeModifier, 402 OpenMPMapClauseKind Type, 403 bool TypeIsImplicit, 404 SourceLocation TypeLoc) { 405 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size())); 406 OMPMapClause *Clause = 407 new (Mem) OMPMapClause(TypeModifier, Type, TypeIsImplicit, TypeLoc, 408 StartLoc, LParenLoc, EndLoc, VL.size()); 409 Clause->setVarRefs(VL); 410 Clause->setMapTypeModifier(TypeModifier); 411 Clause->setMapType(Type); 412 Clause->setMapLoc(TypeLoc); 413 return Clause; 414 } 415 416 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned N) { 417 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N)); 418 return new (Mem) OMPMapClause(N); 419 } 420