| 1 | //===--- Compiler.cpp - Code generator for expressions ---*- C++ -*-===// |
| 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 | |
| 9 | #include "Compiler.h" |
| 10 | #include "ByteCodeEmitter.h" |
| 11 | #include "Context.h" |
| 12 | #include "FixedPoint.h" |
| 13 | #include "Floating.h" |
| 14 | #include "Function.h" |
| 15 | #include "InterpShared.h" |
| 16 | #include "PrimType.h" |
| 17 | #include "Program.h" |
| 18 | #include "clang/AST/Attr.h" |
| 19 | #include "llvm/Support/SaveAndRestore.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace clang::interp; |
| 23 | |
| 24 | using APSInt = llvm::APSInt; |
| 25 | |
| 26 | namespace clang { |
| 27 | namespace interp { |
| 28 | |
| 29 | static std::optional<bool> getBoolValue(const Expr *E) { |
| 30 | if (const auto *CE = dyn_cast_if_present<ConstantExpr>(Val: E); |
| 31 | CE && CE->hasAPValueResult() && |
| 32 | CE->getResultAPValueKind() == APValue::ValueKind::Int) { |
| 33 | return CE->getResultAsAPSInt().getBoolValue(); |
| 34 | } |
| 35 | |
| 36 | return std::nullopt; |
| 37 | } |
| 38 | |
| 39 | /// Scope used to handle temporaries in toplevel variable declarations. |
| 40 | template <class Emitter> class DeclScope final : public LocalScope<Emitter> { |
| 41 | public: |
| 42 | DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD) |
| 43 | : LocalScope<Emitter>(Ctx), Scope(Ctx->P), |
| 44 | OldInitializingDecl(Ctx->InitializingDecl) { |
| 45 | Ctx->InitializingDecl = VD; |
| 46 | Ctx->InitStack.push_back(InitLink::Decl(D: VD)); |
| 47 | } |
| 48 | |
| 49 | ~DeclScope() { |
| 50 | this->Ctx->InitializingDecl = OldInitializingDecl; |
| 51 | this->Ctx->InitStack.pop_back(); |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | Program::DeclScope Scope; |
| 56 | const ValueDecl *OldInitializingDecl; |
| 57 | }; |
| 58 | |
| 59 | /// Scope used to handle initialization methods. |
| 60 | template <class Emitter> class OptionScope final { |
| 61 | public: |
| 62 | /// Root constructor, compiling or discarding primitives. |
| 63 | OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult, |
| 64 | bool NewInitializing, bool NewToLValue) |
| 65 | : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult), |
| 66 | OldInitializing(Ctx->Initializing), OldToLValue(Ctx->ToLValue) { |
| 67 | Ctx->DiscardResult = NewDiscardResult; |
| 68 | Ctx->Initializing = NewInitializing; |
| 69 | Ctx->ToLValue = NewToLValue; |
| 70 | } |
| 71 | |
| 72 | ~OptionScope() { |
| 73 | Ctx->DiscardResult = OldDiscardResult; |
| 74 | Ctx->Initializing = OldInitializing; |
| 75 | Ctx->ToLValue = OldToLValue; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | /// Parent context. |
| 80 | Compiler<Emitter> *Ctx; |
| 81 | /// Old discard flag to restore. |
| 82 | bool OldDiscardResult; |
| 83 | bool OldInitializing; |
| 84 | bool OldToLValue; |
| 85 | }; |
| 86 | |
| 87 | template <class Emitter> |
| 88 | bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const { |
| 89 | switch (Kind) { |
| 90 | case K_This: |
| 91 | return Ctx->emitThis(E); |
| 92 | case K_Field: |
| 93 | // We're assuming there's a base pointer on the stack already. |
| 94 | return Ctx->emitGetPtrFieldPop(Offset, E); |
| 95 | case K_Temp: |
| 96 | return Ctx->emitGetPtrLocal(Offset, E); |
| 97 | case K_Decl: |
| 98 | return Ctx->visitDeclRef(D, E); |
| 99 | case K_Elem: |
| 100 | if (!Ctx->emitConstUint32(Offset, E)) |
| 101 | return false; |
| 102 | return Ctx->emitArrayElemPtrPopUint32(E); |
| 103 | case K_RVO: |
| 104 | return Ctx->emitRVOPtr(E); |
| 105 | case K_InitList: |
| 106 | return true; |
| 107 | default: |
| 108 | llvm_unreachable("Unhandled InitLink kind" ); |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | /// Sets the context for break/continue statements. |
| 114 | template <class Emitter> class LoopScope final { |
| 115 | public: |
| 116 | using LabelTy = typename Compiler<Emitter>::LabelTy; |
| 117 | using OptLabelTy = typename Compiler<Emitter>::OptLabelTy; |
| 118 | using LabelInfo = typename Compiler<Emitter>::LabelInfo; |
| 119 | |
| 120 | LoopScope(Compiler<Emitter> *Ctx, const Stmt *Name, LabelTy BreakLabel, |
| 121 | LabelTy ContinueLabel) |
| 122 | : Ctx(Ctx) { |
| 123 | #ifndef NDEBUG |
| 124 | for (const LabelInfo &LI : Ctx->LabelInfoStack) |
| 125 | assert(LI.Name != Name); |
| 126 | #endif |
| 127 | |
| 128 | this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel, ContinueLabel, |
| 129 | /*DefaultLabel=*/std::nullopt, |
| 130 | Ctx->VarScope); |
| 131 | } |
| 132 | |
| 133 | ~LoopScope() { this->Ctx->LabelInfoStack.pop_back(); } |
| 134 | |
| 135 | private: |
| 136 | Compiler<Emitter> *Ctx; |
| 137 | }; |
| 138 | |
| 139 | // Sets the context for a switch scope, mapping labels. |
| 140 | template <class Emitter> class SwitchScope final { |
| 141 | public: |
| 142 | using LabelTy = typename Compiler<Emitter>::LabelTy; |
| 143 | using OptLabelTy = typename Compiler<Emitter>::OptLabelTy; |
| 144 | using CaseMap = typename Compiler<Emitter>::CaseMap; |
| 145 | using LabelInfo = typename Compiler<Emitter>::LabelInfo; |
| 146 | |
| 147 | SwitchScope(Compiler<Emitter> *Ctx, const Stmt *Name, CaseMap &&CaseLabels, |
| 148 | LabelTy BreakLabel, OptLabelTy DefaultLabel) |
| 149 | : Ctx(Ctx), OldCaseLabels(std::move(this->Ctx->CaseLabels)) { |
| 150 | #ifndef NDEBUG |
| 151 | for (const LabelInfo &LI : Ctx->LabelInfoStack) |
| 152 | assert(LI.Name != Name); |
| 153 | #endif |
| 154 | |
| 155 | this->Ctx->CaseLabels = std::move(CaseLabels); |
| 156 | this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel, |
| 157 | /*ContinueLabel=*/std::nullopt, |
| 158 | DefaultLabel, Ctx->VarScope); |
| 159 | } |
| 160 | |
| 161 | ~SwitchScope() { |
| 162 | this->Ctx->CaseLabels = std::move(OldCaseLabels); |
| 163 | this->Ctx->LabelInfoStack.pop_back(); |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | Compiler<Emitter> *Ctx; |
| 168 | CaseMap OldCaseLabels; |
| 169 | }; |
| 170 | |
| 171 | /// When generating code for e.g. implicit field initializers in constructors, |
| 172 | /// we don't have anything to point to in case the initializer causes an error. |
| 173 | /// In that case, we need to disable location tracking for the initializer so |
| 174 | /// we later point to the call range instead. |
| 175 | template <class Emitter> class LocOverrideScope final { |
| 176 | public: |
| 177 | LocOverrideScope(Compiler<Emitter> *Ctx, SourceInfo NewValue, |
| 178 | bool Enabled = true) |
| 179 | : Ctx(Ctx), OldFlag(Ctx->LocOverride), Enabled(Enabled) { |
| 180 | |
| 181 | if (Enabled) |
| 182 | Ctx->LocOverride = NewValue; |
| 183 | } |
| 184 | |
| 185 | ~LocOverrideScope() { |
| 186 | if (Enabled) |
| 187 | Ctx->LocOverride = OldFlag; |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | Compiler<Emitter> *Ctx; |
| 192 | std::optional<SourceInfo> OldFlag; |
| 193 | bool Enabled; |
| 194 | }; |
| 195 | |
| 196 | } // namespace interp |
| 197 | } // namespace clang |
| 198 | |
| 199 | template <class Emitter> |
| 200 | bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) { |
| 201 | const Expr *SubExpr = E->getSubExpr(); |
| 202 | |
| 203 | if (DiscardResult) |
| 204 | return this->delegate(E: SubExpr); |
| 205 | |
| 206 | switch (E->getCastKind()) { |
| 207 | case CK_LValueToRValue: { |
| 208 | if (ToLValue && E->getType()->isPointerType()) |
| 209 | return this->delegate(E: SubExpr); |
| 210 | |
| 211 | if (SubExpr->getType().isVolatileQualified()) |
| 212 | return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, E); |
| 213 | |
| 214 | OptPrimType SubExprT = classify(SubExpr->getType()); |
| 215 | // Try to load the value directly. This is purely a performance |
| 216 | // optimization. |
| 217 | if (SubExprT) { |
| 218 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: SubExpr)) { |
| 219 | const ValueDecl *D = DRE->getDecl(); |
| 220 | bool IsReference = D->getType()->isReferenceType(); |
| 221 | |
| 222 | if (!IsReference) { |
| 223 | if (Context::shouldBeGloballyIndexed(VD: D)) { |
| 224 | if (auto GlobalIndex = P.getGlobal(VD: D)) |
| 225 | return this->emitGetGlobal(*SubExprT, *GlobalIndex, E); |
| 226 | } else if (auto It = Locals.find(Val: D); It != Locals.end()) { |
| 227 | return this->emitGetLocal(*SubExprT, It->second.Offset, E); |
| 228 | } else if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: D)) { |
| 229 | if (auto It = this->Params.find(PVD); It != this->Params.end()) { |
| 230 | return this->emitGetParam(*SubExprT, It->second.Index, E); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Prepare storage for the result. |
| 238 | if (!Initializing && !SubExprT) { |
| 239 | UnsignedOrNone LocalIndex = allocateLocal(Decl: SubExpr); |
| 240 | if (!LocalIndex) |
| 241 | return false; |
| 242 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | if (!this->visit(E: SubExpr)) |
| 247 | return false; |
| 248 | |
| 249 | if (SubExprT) |
| 250 | return this->emitLoadPop(*SubExprT, E); |
| 251 | |
| 252 | // If the subexpr type is not primitive, we need to perform a copy here. |
| 253 | // This happens for example in C when dereferencing a pointer of struct |
| 254 | // type. |
| 255 | return this->emitMemcpy(E); |
| 256 | } |
| 257 | |
| 258 | case CK_DerivedToBaseMemberPointer: { |
| 259 | if (E->containsErrors()) |
| 260 | return false; |
| 261 | assert(classifyPrim(E) == PT_MemberPtr); |
| 262 | assert(classifyPrim(SubExpr) == PT_MemberPtr); |
| 263 | |
| 264 | if (!this->delegate(E: SubExpr)) |
| 265 | return false; |
| 266 | |
| 267 | const CXXRecordDecl *CurDecl = SubExpr->getType() |
| 268 | ->castAs<MemberPointerType>() |
| 269 | ->getMostRecentCXXRecordDecl(); |
| 270 | for (const CXXBaseSpecifier *B : E->path()) { |
| 271 | const CXXRecordDecl *ToDecl = B->getType()->getAsCXXRecordDecl(); |
| 272 | unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: ToDecl, DerivedDecl: CurDecl); |
| 273 | |
| 274 | if (!this->emitCastMemberPtrBasePop(DerivedOffset, ToDecl, E)) |
| 275 | return false; |
| 276 | CurDecl = ToDecl; |
| 277 | } |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | case CK_BaseToDerivedMemberPointer: { |
| 283 | if (E->containsErrors()) |
| 284 | return false; |
| 285 | assert(classifyPrim(E) == PT_MemberPtr); |
| 286 | assert(classifyPrim(SubExpr) == PT_MemberPtr); |
| 287 | |
| 288 | if (!this->delegate(E: SubExpr)) |
| 289 | return false; |
| 290 | |
| 291 | const CXXRecordDecl *CurDecl = SubExpr->getType() |
| 292 | ->castAs<MemberPointerType>() |
| 293 | ->getMostRecentCXXRecordDecl(); |
| 294 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 295 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 296 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 297 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 298 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 299 | PathI != PathE; ++PathI) { |
| 300 | const CXXRecordDecl *ToDecl = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 301 | unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: CurDecl, DerivedDecl: ToDecl); |
| 302 | |
| 303 | if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E)) |
| 304 | return false; |
| 305 | CurDecl = ToDecl; |
| 306 | } |
| 307 | |
| 308 | const CXXRecordDecl *ToDecl = |
| 309 | E->getType()->castAs<MemberPointerType>()->getMostRecentCXXRecordDecl(); |
| 310 | assert(ToDecl != CurDecl); |
| 311 | unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: CurDecl, DerivedDecl: ToDecl); |
| 312 | |
| 313 | if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E)) |
| 314 | return false; |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | case CK_UncheckedDerivedToBase: |
| 320 | case CK_DerivedToBase: { |
| 321 | if (!this->delegate(E: SubExpr)) |
| 322 | return false; |
| 323 | |
| 324 | const auto = [](QualType Ty) -> const CXXRecordDecl * { |
| 325 | if (const auto *PT = dyn_cast<PointerType>(Val&: Ty)) |
| 326 | return PT->getPointeeType()->getAsCXXRecordDecl(); |
| 327 | return Ty->getAsCXXRecordDecl(); |
| 328 | }; |
| 329 | |
| 330 | // FIXME: We can express a series of non-virtual casts as a single |
| 331 | // GetPtrBasePop op. |
| 332 | QualType CurType = SubExpr->getType(); |
| 333 | for (const CXXBaseSpecifier *B : E->path()) { |
| 334 | if (B->isVirtual()) { |
| 335 | if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), E)) |
| 336 | return false; |
| 337 | CurType = B->getType(); |
| 338 | } else { |
| 339 | unsigned DerivedOffset = collectBaseOffset(BaseType: B->getType(), DerivedType: CurType); |
| 340 | if (!this->emitGetPtrBasePop( |
| 341 | DerivedOffset, /*NullOK=*/E->getType()->isPointerType(), E)) |
| 342 | return false; |
| 343 | CurType = B->getType(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | case CK_BaseToDerived: { |
| 351 | if (!this->delegate(E: SubExpr)) |
| 352 | return false; |
| 353 | unsigned DerivedOffset = |
| 354 | collectBaseOffset(BaseType: SubExpr->getType(), DerivedType: E->getType()); |
| 355 | |
| 356 | const Type *TargetType = E->getType().getTypePtr(); |
| 357 | if (TargetType->isPointerOrReferenceType()) |
| 358 | TargetType = TargetType->getPointeeType().getTypePtr(); |
| 359 | return this->emitGetPtrDerivedPop(DerivedOffset, |
| 360 | /*NullOK=*/E->getType()->isPointerType(), |
| 361 | TargetType, E); |
| 362 | } |
| 363 | |
| 364 | case CK_FloatingCast: { |
| 365 | // HLSL uses CK_FloatingCast to cast between vectors. |
| 366 | if (E->getType()->isVectorType()) |
| 367 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 368 | if (!SubExpr->getType()->isFloatingType() || |
| 369 | !E->getType()->isFloatingType()) |
| 370 | return false; |
| 371 | if (!this->visit(E: SubExpr)) |
| 372 | return false; |
| 373 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType()); |
| 374 | return this->emitCastFP(TargetSemantics, getRoundingMode(E), E); |
| 375 | } |
| 376 | |
| 377 | case CK_IntegralToFloating: { |
| 378 | if (E->getType()->isVectorType()) |
| 379 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 380 | if (!E->getType()->isRealFloatingType()) |
| 381 | return false; |
| 382 | if (!this->visit(E: SubExpr)) |
| 383 | return false; |
| 384 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType()); |
| 385 | return this->emitCastIntegralFloating(classifyPrim(SubExpr), |
| 386 | TargetSemantics, getFPOptions(E), E); |
| 387 | } |
| 388 | |
| 389 | case CK_FloatingToBoolean: { |
| 390 | if (E->getType()->isVectorType()) |
| 391 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 392 | if (!SubExpr->getType()->isRealFloatingType() || |
| 393 | !E->getType()->isBooleanType()) |
| 394 | return false; |
| 395 | if (const auto *FL = dyn_cast<FloatingLiteral>(Val: SubExpr)) |
| 396 | return this->emitConstBool(FL->getValue().isNonZero(), E); |
| 397 | if (!this->visit(E: SubExpr)) |
| 398 | return false; |
| 399 | return this->emitCastFloatingIntegralBool(getFPOptions(E), E); |
| 400 | } |
| 401 | |
| 402 | case CK_FloatingToIntegral: { |
| 403 | if (E->getType()->isVectorType()) |
| 404 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 405 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 406 | return false; |
| 407 | if (!this->visit(E: SubExpr)) |
| 408 | return false; |
| 409 | PrimType ToT = classifyPrim(E); |
| 410 | if (ToT == PT_IntAP) |
| 411 | return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(T: E->getType()), |
| 412 | getFPOptions(E), E); |
| 413 | if (ToT == PT_IntAPS) |
| 414 | return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(T: E->getType()), |
| 415 | getFPOptions(E), E); |
| 416 | |
| 417 | return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E); |
| 418 | } |
| 419 | |
| 420 | case CK_NullToPointer: |
| 421 | case CK_NullToMemberPointer: { |
| 422 | if (!this->discard(E: SubExpr)) |
| 423 | return false; |
| 424 | uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType()); |
| 425 | return this->emitNull(classifyPrim(E->getType()), Val, |
| 426 | E->getType().getTypePtr(), E); |
| 427 | } |
| 428 | |
| 429 | case CK_PointerToIntegral: { |
| 430 | if (!this->visit(E: SubExpr)) |
| 431 | return false; |
| 432 | |
| 433 | // If SubExpr doesn't result in a pointer, make it one. |
| 434 | if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) { |
| 435 | assert(isPtrType(FromT)); |
| 436 | if (!this->emitDecayPtr(FromT, PT_Ptr, E)) |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | PrimType T = classifyPrim(E->getType()); |
| 441 | if (T == PT_IntAP) |
| 442 | return this->emitCastPointerIntegralAP(Ctx.getBitWidth(T: E->getType()), E); |
| 443 | if (T == PT_IntAPS) |
| 444 | return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(T: E->getType()), E); |
| 445 | return this->emitCastPointerIntegral(T, E); |
| 446 | } |
| 447 | |
| 448 | case CK_ArrayToPointerDecay: { |
| 449 | if (!this->visit(E: SubExpr)) |
| 450 | return false; |
| 451 | return this->emitArrayDecay(E); |
| 452 | } |
| 453 | |
| 454 | case CK_IntegralToPointer: { |
| 455 | QualType IntType = SubExpr->getType(); |
| 456 | assert(IntType->isIntegralOrEnumerationType()); |
| 457 | if (!this->visit(E: SubExpr)) |
| 458 | return false; |
| 459 | // FIXME: I think the discard is wrong since the int->ptr cast might cause a |
| 460 | // diagnostic. |
| 461 | PrimType T = classifyPrim(IntType); |
| 462 | if (!this->emitGetIntPtr(T, E->getType().getTypePtr(), E)) |
| 463 | return false; |
| 464 | |
| 465 | QualType PtrType = E->getType(); |
| 466 | PrimType DestPtrT = classifyPrim(PtrType); |
| 467 | if (DestPtrT == PT_Ptr) |
| 468 | return true; |
| 469 | |
| 470 | // In case we're converting the integer to a non-Pointer. |
| 471 | return this->emitDecayPtr(PT_Ptr, DestPtrT, E); |
| 472 | } |
| 473 | |
| 474 | case CK_AtomicToNonAtomic: |
| 475 | case CK_ConstructorConversion: |
| 476 | case CK_FunctionToPointerDecay: |
| 477 | case CK_NonAtomicToAtomic: |
| 478 | case CK_NoOp: |
| 479 | case CK_UserDefinedConversion: |
| 480 | case CK_AddressSpaceConversion: |
| 481 | case CK_CPointerToObjCPointerCast: |
| 482 | return this->delegate(E: SubExpr); |
| 483 | |
| 484 | case CK_BitCast: { |
| 485 | if (E->containsErrors()) |
| 486 | return false; |
| 487 | QualType ETy = E->getType(); |
| 488 | // Reject bitcasts to atomic types. |
| 489 | if (ETy->isAtomicType()) { |
| 490 | if (!this->discard(E: SubExpr)) |
| 491 | return false; |
| 492 | return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E); |
| 493 | } |
| 494 | QualType SubExprTy = SubExpr->getType(); |
| 495 | OptPrimType FromT = classify(SubExprTy); |
| 496 | // Casts from integer/vector to vector. |
| 497 | if (E->getType()->isVectorType()) |
| 498 | return this->emitBuiltinBitCast(E); |
| 499 | |
| 500 | OptPrimType ToT = classify(E->getType()); |
| 501 | if (!FromT || !ToT) |
| 502 | return false; |
| 503 | |
| 504 | assert(isPtrType(*FromT)); |
| 505 | assert(isPtrType(*ToT)); |
| 506 | bool SrcIsVoidPtr = SubExprTy->isVoidPointerType(); |
| 507 | if (FromT == ToT) { |
| 508 | if (E->getType()->isVoidPointerType() && |
| 509 | !SubExprTy->isFunctionPointerType()) { |
| 510 | return this->delegate(E: SubExpr); |
| 511 | } |
| 512 | |
| 513 | if (!this->visit(E: SubExpr)) |
| 514 | return false; |
| 515 | if (!this->emitCheckBitCast(ETy->getPointeeType().getTypePtr(), |
| 516 | SrcIsVoidPtr, E)) |
| 517 | return false; |
| 518 | |
| 519 | if (E->getType()->isFunctionPointerType() || |
| 520 | SubExprTy->isFunctionPointerType()) { |
| 521 | return this->emitFnPtrCast(E); |
| 522 | } |
| 523 | if (FromT == PT_Ptr) |
| 524 | return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), E); |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | if (!this->visit(E: SubExpr)) |
| 529 | return false; |
| 530 | return this->emitDecayPtr(*FromT, *ToT, E); |
| 531 | } |
| 532 | case CK_IntegralToBoolean: |
| 533 | case CK_FixedPointToBoolean: { |
| 534 | if (E->getType()->isVectorType()) |
| 535 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 536 | // HLSL uses this to cast to one-element vectors. |
| 537 | OptPrimType FromT = classify(SubExpr->getType()); |
| 538 | if (!FromT) |
| 539 | return false; |
| 540 | |
| 541 | if (const auto *IL = dyn_cast<IntegerLiteral>(Val: SubExpr)) |
| 542 | return this->emitConst(IL->getValue(), E); |
| 543 | if (!this->visit(E: SubExpr)) |
| 544 | return false; |
| 545 | return this->emitCast(*FromT, classifyPrim(E), E); |
| 546 | } |
| 547 | |
| 548 | case CK_IntegralCast: |
| 549 | if (E->getType()->isVectorType()) |
| 550 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 551 | [[fallthrough]]; |
| 552 | case CK_BooleanToSignedIntegral: { |
| 553 | OptPrimType FromT = classify(SubExpr->getType()); |
| 554 | OptPrimType ToT = classify(E->getType()); |
| 555 | if (!FromT || !ToT) |
| 556 | return false; |
| 557 | |
| 558 | // Try to emit a casted known constant value directly. |
| 559 | if (const auto *IL = dyn_cast<IntegerLiteral>(Val: SubExpr)) { |
| 560 | if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP && |
| 561 | FromT != PT_IntAPS && !E->getType()->isEnumeralType()) |
| 562 | return this->emitConst(APSInt(IL->getValue(), !isSignedType(T: *FromT)), |
| 563 | E); |
| 564 | if (!this->emitConst(IL->getValue(), SubExpr)) |
| 565 | return false; |
| 566 | } else { |
| 567 | if (!this->visit(E: SubExpr)) |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | // Possibly diagnose casts to enum types if the target type does not |
| 572 | // have a fixed size. |
| 573 | if (Ctx.getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) { |
| 574 | const auto *ED = E->getType()->castAsEnumDecl(); |
| 575 | if (!ED->isFixed()) { |
| 576 | if (!this->emitCheckEnumValue(*FromT, ED, E)) |
| 577 | return false; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (ToT == PT_IntAP) { |
| 582 | if (!this->emitCastAP(*FromT, Ctx.getBitWidth(T: E->getType()), E)) |
| 583 | return false; |
| 584 | } else if (ToT == PT_IntAPS) { |
| 585 | if (!this->emitCastAPS(*FromT, Ctx.getBitWidth(T: E->getType()), E)) |
| 586 | return false; |
| 587 | } else { |
| 588 | if (FromT == ToT) |
| 589 | return true; |
| 590 | if (!this->emitCast(*FromT, *ToT, E)) |
| 591 | return false; |
| 592 | } |
| 593 | if (E->getCastKind() == CK_BooleanToSignedIntegral) |
| 594 | return this->emitNeg(*ToT, E); |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | case CK_PointerToBoolean: |
| 599 | case CK_MemberPointerToBoolean: { |
| 600 | PrimType PtrT = classifyPrim(SubExpr->getType()); |
| 601 | |
| 602 | if (!this->visit(E: SubExpr)) |
| 603 | return false; |
| 604 | return this->emitIsNonNull(PtrT, E); |
| 605 | } |
| 606 | |
| 607 | case CK_IntegralComplexToBoolean: |
| 608 | case CK_FloatingComplexToBoolean: { |
| 609 | if (!this->visit(E: SubExpr)) |
| 610 | return false; |
| 611 | return this->emitComplexBoolCast(E: SubExpr); |
| 612 | } |
| 613 | |
| 614 | case CK_IntegralComplexToReal: |
| 615 | case CK_FloatingComplexToReal: |
| 616 | return this->emitComplexReal(SubExpr); |
| 617 | |
| 618 | case CK_IntegralRealToComplex: |
| 619 | case CK_FloatingRealToComplex: { |
| 620 | // We're creating a complex value here, so we need to |
| 621 | // allocate storage for it. |
| 622 | if (!Initializing) { |
| 623 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 624 | if (!LocalIndex) |
| 625 | return false; |
| 626 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | PrimType T = classifyPrim(SubExpr->getType()); |
| 631 | // Init the complex value to {SubExpr, 0}. |
| 632 | if (!this->visitArrayElemInit(ElemIndex: 0, Init: SubExpr, InitT: T)) |
| 633 | return false; |
| 634 | // Zero-init the second element. |
| 635 | if (!this->visitZeroInitializer(T, QT: SubExpr->getType(), E: SubExpr)) |
| 636 | return false; |
| 637 | return this->emitInitElem(T, 1, SubExpr); |
| 638 | } |
| 639 | |
| 640 | case CK_IntegralComplexCast: |
| 641 | case CK_FloatingComplexCast: |
| 642 | case CK_IntegralComplexToFloatingComplex: |
| 643 | case CK_FloatingComplexToIntegralComplex: { |
| 644 | assert(E->getType()->isAnyComplexType()); |
| 645 | assert(SubExpr->getType()->isAnyComplexType()); |
| 646 | if (!Initializing) { |
| 647 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 648 | if (!LocalIndex) |
| 649 | return false; |
| 650 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | // Location for the SubExpr. |
| 655 | // Since SubExpr is of complex type, visiting it results in a pointer |
| 656 | // anyway, so we just create a temporary pointer variable. |
| 657 | unsigned SubExprOffset = |
| 658 | allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 659 | if (!this->visit(E: SubExpr)) |
| 660 | return false; |
| 661 | if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E)) |
| 662 | return false; |
| 663 | |
| 664 | PrimType SourceElemT = classifyComplexElementType(T: SubExpr->getType()); |
| 665 | QualType DestElemType = |
| 666 | E->getType()->getAs<ComplexType>()->getElementType(); |
| 667 | PrimType DestElemT = classifyPrim(DestElemType); |
| 668 | // Cast both elements individually. |
| 669 | for (unsigned I = 0; I != 2; ++I) { |
| 670 | if (!this->emitGetLocal(PT_Ptr, SubExprOffset, E)) |
| 671 | return false; |
| 672 | if (!this->emitArrayElemPop(SourceElemT, I, E)) |
| 673 | return false; |
| 674 | |
| 675 | // Do the cast. |
| 676 | if (!this->emitPrimCast(FromT: SourceElemT, ToT: DestElemT, ToQT: DestElemType, E)) |
| 677 | return false; |
| 678 | |
| 679 | // Save the value. |
| 680 | if (!this->emitInitElem(DestElemT, I, E)) |
| 681 | return false; |
| 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | case CK_VectorSplat: { |
| 687 | assert(!canClassify(E->getType())); |
| 688 | assert(E->getType()->isVectorType()); |
| 689 | |
| 690 | if (!canClassify(SubExpr->getType())) |
| 691 | return false; |
| 692 | |
| 693 | if (!Initializing) { |
| 694 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 695 | if (!LocalIndex) |
| 696 | return false; |
| 697 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 698 | return false; |
| 699 | } |
| 700 | |
| 701 | const auto *VT = E->getType()->getAs<VectorType>(); |
| 702 | PrimType ElemT = classifyPrim(SubExpr->getType()); |
| 703 | unsigned ElemOffset = |
| 704 | allocateLocalPrimitive(Decl: SubExpr, Ty: ElemT, /*IsConst=*/true); |
| 705 | |
| 706 | // Prepare a local variable for the scalar value. |
| 707 | if (!this->visit(E: SubExpr)) |
| 708 | return false; |
| 709 | if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, E)) |
| 710 | return false; |
| 711 | |
| 712 | if (!this->emitSetLocal(ElemT, ElemOffset, E)) |
| 713 | return false; |
| 714 | |
| 715 | for (unsigned I = 0; I != VT->getNumElements(); ++I) { |
| 716 | if (!this->emitGetLocal(ElemT, ElemOffset, E)) |
| 717 | return false; |
| 718 | if (!this->emitInitElem(ElemT, I, E)) |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | case CK_HLSLVectorTruncation: { |
| 726 | assert(SubExpr->getType()->isVectorType()); |
| 727 | if (OptPrimType ResultT = classify(E)) { |
| 728 | assert(!DiscardResult); |
| 729 | // Result must be either a float or integer. Take the first element. |
| 730 | if (!this->visit(E: SubExpr)) |
| 731 | return false; |
| 732 | return this->emitArrayElemPop(*ResultT, 0, E); |
| 733 | } |
| 734 | // Otherwise, this truncates from one vector type to another. |
| 735 | assert(E->getType()->isVectorType()); |
| 736 | |
| 737 | if (!Initializing) { |
| 738 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 739 | if (!LocalIndex) |
| 740 | return false; |
| 741 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 742 | return false; |
| 743 | } |
| 744 | unsigned ToSize = E->getType()->getAs<VectorType>()->getNumElements(); |
| 745 | assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize); |
| 746 | if (!this->visit(E: SubExpr)) |
| 747 | return false; |
| 748 | return this->emitCopyArray(classifyVectorElementType(T: E->getType()), 0, 0, |
| 749 | ToSize, E); |
| 750 | }; |
| 751 | |
| 752 | case CK_IntegralToFixedPoint: { |
| 753 | if (!this->visit(E: SubExpr)) |
| 754 | return false; |
| 755 | |
| 756 | auto Sem = |
| 757 | Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 758 | if (!this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), Sem, |
| 759 | E)) |
| 760 | return false; |
| 761 | if (DiscardResult) |
| 762 | return this->emitPopFixedPoint(E); |
| 763 | return true; |
| 764 | } |
| 765 | case CK_FloatingToFixedPoint: { |
| 766 | if (!this->visit(E: SubExpr)) |
| 767 | return false; |
| 768 | |
| 769 | auto Sem = |
| 770 | Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 771 | if (!this->emitCastFloatingFixedPoint(Sem, E)) |
| 772 | return false; |
| 773 | if (DiscardResult) |
| 774 | return this->emitPopFixedPoint(E); |
| 775 | return true; |
| 776 | } |
| 777 | case CK_FixedPointToFloating: { |
| 778 | if (!this->visit(E: SubExpr)) |
| 779 | return false; |
| 780 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType()); |
| 781 | if (!this->emitCastFixedPointFloating(TargetSemantics, E)) |
| 782 | return false; |
| 783 | if (DiscardResult) |
| 784 | return this->emitPopFloat(E); |
| 785 | return true; |
| 786 | } |
| 787 | case CK_FixedPointToIntegral: { |
| 788 | if (!this->visit(E: SubExpr)) |
| 789 | return false; |
| 790 | PrimType IntegralT = classifyPrim(E->getType()); |
| 791 | if (!this->emitCastFixedPointIntegral(IntegralT, E)) |
| 792 | return false; |
| 793 | if (DiscardResult) |
| 794 | return this->emitPop(IntegralT, E); |
| 795 | return true; |
| 796 | } |
| 797 | case CK_FixedPointCast: { |
| 798 | if (!this->visit(E: SubExpr)) |
| 799 | return false; |
| 800 | auto Sem = |
| 801 | Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 802 | if (!this->emitCastFixedPoint(Sem, E)) |
| 803 | return false; |
| 804 | if (DiscardResult) |
| 805 | return this->emitPopFixedPoint(E); |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | case CK_ToVoid: |
| 810 | return discard(E: SubExpr); |
| 811 | |
| 812 | case CK_Dynamic: |
| 813 | llvm_unreachable("CXXDynamicCastExpr has its own function" ); |
| 814 | |
| 815 | case CK_LValueBitCast: |
| 816 | if (!this->emitInvalidCast(CastKind::ReinterpretLike, /*Fatal=*/false, E)) |
| 817 | return false; |
| 818 | return this->delegate(E: SubExpr); |
| 819 | |
| 820 | case CK_HLSLArrayRValue: { |
| 821 | // Non-decaying array rvalue cast - creates an rvalue copy of an lvalue |
| 822 | // array, similar to LValueToRValue for composite types. |
| 823 | if (!Initializing) { |
| 824 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 825 | if (!LocalIndex) |
| 826 | return false; |
| 827 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 828 | return false; |
| 829 | } |
| 830 | if (!this->visit(E: SubExpr)) |
| 831 | return false; |
| 832 | return this->emitMemcpy(E); |
| 833 | } |
| 834 | |
| 835 | case CK_HLSLMatrixTruncation: { |
| 836 | assert(SubExpr->getType()->isConstantMatrixType()); |
| 837 | if (OptPrimType ResultT = classify(E)) { |
| 838 | assert(!DiscardResult); |
| 839 | // Result must be either a float or integer. Take the first element. |
| 840 | if (!this->visit(E: SubExpr)) |
| 841 | return false; |
| 842 | return this->emitArrayElemPop(*ResultT, 0, E); |
| 843 | } |
| 844 | // Otherwise, this truncates to a a constant matrix type. |
| 845 | assert(E->getType()->isConstantMatrixType()); |
| 846 | |
| 847 | if (!Initializing) { |
| 848 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 849 | if (!LocalIndex) |
| 850 | return false; |
| 851 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 852 | return false; |
| 853 | } |
| 854 | unsigned ToSize = |
| 855 | E->getType()->getAs<ConstantMatrixType>()->getNumElementsFlattened(); |
| 856 | if (!this->visit(E: SubExpr)) |
| 857 | return false; |
| 858 | return this->emitCopyArray(classifyMatrixElementType(T: SubExpr->getType()), 0, |
| 859 | 0, ToSize, E); |
| 860 | } |
| 861 | |
| 862 | case CK_HLSLAggregateSplatCast: { |
| 863 | // Aggregate splat cast: convert a scalar value to one of an aggregate type |
| 864 | // by replicating and casting the scalar to every element of the destination |
| 865 | // aggregate (vector, matrix, array, or struct). |
| 866 | assert(canClassify(SubExpr->getType())); |
| 867 | |
| 868 | if (!Initializing) { |
| 869 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 870 | if (!LocalIndex) |
| 871 | return false; |
| 872 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | // The scalar to be splatted is stored in a local to be repeatedly loaded |
| 877 | // once for every scalar element of the destination. |
| 878 | PrimType SrcElemT = classifyPrim(SubExpr->getType()); |
| 879 | unsigned SrcOffset = |
| 880 | allocateLocalPrimitive(Decl: SubExpr, Ty: SrcElemT, /*IsConst=*/true); |
| 881 | |
| 882 | if (!this->visit(E: SubExpr)) |
| 883 | return false; |
| 884 | if (!this->emitSetLocal(SrcElemT, SrcOffset, E)) |
| 885 | return false; |
| 886 | |
| 887 | // Recursively splat the scalar into every element of the destination. |
| 888 | return emitHLSLAggregateSplat(SrcT: SrcElemT, SrcOffset, DestType: E->getType(), E); |
| 889 | } |
| 890 | |
| 891 | case CK_HLSLElementwiseCast: { |
| 892 | // Elementwise cast: flatten the elements of one aggregate source type and |
| 893 | // store to a destination scalar or aggregate type of the same or fewer |
| 894 | // number of elements. Casts are inserted element-wise to convert each |
| 895 | // source scalar element to its corresponding destination scalar element. |
| 896 | QualType SrcType = SubExpr->getType(); |
| 897 | QualType DestType = E->getType(); |
| 898 | |
| 899 | if (OptPrimType DestT = classify(DestType)) { |
| 900 | // When the destination is a scalar, we only need the first scalar |
| 901 | // element of the source. |
| 902 | unsigned SrcPtrOffset = |
| 903 | allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 904 | if (!this->visit(E: SubExpr)) |
| 905 | return false; |
| 906 | if (!this->emitSetLocal(PT_Ptr, SrcPtrOffset, E)) |
| 907 | return false; |
| 908 | |
| 909 | SmallVector<HLSLFlatElement, 1> Elements; |
| 910 | if (!emitHLSLFlattenAggregate(SrcType, SrcPtrOffset, Elements, MaxElements: 1, E)) |
| 911 | return false; |
| 912 | if (Elements.empty()) |
| 913 | return false; |
| 914 | |
| 915 | const HLSLFlatElement &Src = Elements[0]; |
| 916 | if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E)) |
| 917 | return false; |
| 918 | return this->emitPrimCast(FromT: Src.Type, ToT: *DestT, ToQT: DestType, E); |
| 919 | } |
| 920 | |
| 921 | if (!Initializing) { |
| 922 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 923 | if (!LocalIndex) |
| 924 | return false; |
| 925 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | unsigned SrcOffset = |
| 930 | allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 931 | if (!this->visit(E: SubExpr)) |
| 932 | return false; |
| 933 | if (!this->emitSetLocal(PT_Ptr, SrcOffset, E)) |
| 934 | return false; |
| 935 | |
| 936 | // Only flatten as many source elements as the destination requires. |
| 937 | unsigned ElemCount = countHLSLFlatElements(Ty: DestType); |
| 938 | |
| 939 | SmallVector<HLSLFlatElement, 16> Elements; |
| 940 | Elements.reserve(ElemCount); |
| 941 | if (!emitHLSLFlattenAggregate(SrcType, SrcPtrOffset: SrcOffset, Elements, MaxElements: ElemCount, E)) |
| 942 | return false; |
| 943 | |
| 944 | // Sema is expected to reject an elementwise cast whose source has fewer |
| 945 | // scalar elements than the destination. |
| 946 | assert(Elements.size() == ElemCount && |
| 947 | "Source type has fewer scalar elements than the destination type" ); |
| 948 | |
| 949 | return emitHLSLConstructAggregate(DestType, Elements, E); |
| 950 | } |
| 951 | |
| 952 | case CK_ToUnion: { |
| 953 | const FieldDecl *UnionField = E->getTargetUnionField(); |
| 954 | const Record *R = this->getRecord(E->getType()); |
| 955 | assert(R); |
| 956 | const Record::Field *RF = R->getField(FD: UnionField); |
| 957 | QualType FieldType = RF->Decl->getType(); |
| 958 | |
| 959 | if (OptPrimType PT = classify(FieldType)) { |
| 960 | if (!this->visit(E: SubExpr)) |
| 961 | return false; |
| 962 | if (RF->isBitField()) |
| 963 | return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(), |
| 964 | E); |
| 965 | return this->emitInitFieldActivate(*PT, RF->Offset, E); |
| 966 | } |
| 967 | |
| 968 | if (!this->emitGetPtrField(RF->Offset, E)) |
| 969 | return false; |
| 970 | if (!this->emitActivate(E)) |
| 971 | return false; |
| 972 | return this->visitInitializerPop(E: SubExpr); |
| 973 | } |
| 974 | |
| 975 | default: |
| 976 | return this->emitInvalid(E); |
| 977 | } |
| 978 | llvm_unreachable("Unhandled clang::CastKind enum" ); |
| 979 | } |
| 980 | |
| 981 | template <class Emitter> |
| 982 | bool Compiler<Emitter>::VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
| 983 | return this->emitBuiltinBitCast(E); |
| 984 | } |
| 985 | |
| 986 | template <class Emitter> |
| 987 | bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) { |
| 988 | if (DiscardResult) |
| 989 | return true; |
| 990 | |
| 991 | return this->emitConst(LE->getValue(), LE); |
| 992 | } |
| 993 | |
| 994 | template <class Emitter> |
| 995 | bool Compiler<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 996 | if (DiscardResult) |
| 997 | return true; |
| 998 | |
| 999 | APFloat F = E->getValue(); |
| 1000 | return this->emitFloat(F, Info: E); |
| 1001 | } |
| 1002 | |
| 1003 | template <class Emitter> |
| 1004 | bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 1005 | assert(E->getType()->isAnyComplexType()); |
| 1006 | if (DiscardResult) |
| 1007 | return true; |
| 1008 | |
| 1009 | if (!Initializing) { |
| 1010 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1011 | if (!LocalIndex) |
| 1012 | return false; |
| 1013 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | const Expr *SubExpr = E->getSubExpr(); |
| 1018 | PrimType SubExprT = classifyPrim(SubExpr->getType()); |
| 1019 | |
| 1020 | if (!this->visitZeroInitializer(T: SubExprT, QT: SubExpr->getType(), E: SubExpr)) |
| 1021 | return false; |
| 1022 | if (!this->emitInitElem(SubExprT, 0, SubExpr)) |
| 1023 | return false; |
| 1024 | return this->visitArrayElemInit(ElemIndex: 1, Init: SubExpr, InitT: SubExprT); |
| 1025 | } |
| 1026 | |
| 1027 | template <class Emitter> |
| 1028 | bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) { |
| 1029 | assert(E->getType()->isFixedPointType()); |
| 1030 | assert(classifyPrim(E) == PT_FixedPoint); |
| 1031 | |
| 1032 | if (DiscardResult) |
| 1033 | return true; |
| 1034 | |
| 1035 | auto Sem = Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()); |
| 1036 | APInt Value = E->getValue(); |
| 1037 | return this->emitConstFixedPoint(FixedPoint(Value, Sem), E); |
| 1038 | } |
| 1039 | |
| 1040 | template <class Emitter> |
| 1041 | bool Compiler<Emitter>::VisitParenExpr(const ParenExpr *E) { |
| 1042 | return this->delegate(E: E->getSubExpr()); |
| 1043 | } |
| 1044 | |
| 1045 | template <class Emitter> |
| 1046 | bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *E) { |
| 1047 | // Need short-circuiting for these. |
| 1048 | if (E->isLogicalOp() && !E->getType()->isVectorType()) |
| 1049 | return this->VisitLogicalBinOp(E); |
| 1050 | |
| 1051 | const Expr *LHS = E->getLHS(); |
| 1052 | const Expr *RHS = E->getRHS(); |
| 1053 | |
| 1054 | // Handle comma operators. Just discard the LHS |
| 1055 | // and delegate to RHS. |
| 1056 | if (E->isCommaOp()) { |
| 1057 | if (!this->discard(E: LHS)) |
| 1058 | return false; |
| 1059 | if (RHS->getType()->isVoidType()) |
| 1060 | return this->discard(E: RHS); |
| 1061 | |
| 1062 | return this->delegate(E: RHS); |
| 1063 | } |
| 1064 | |
| 1065 | if (E->getType()->isAnyComplexType()) |
| 1066 | return this->VisitComplexBinOp(E); |
| 1067 | if (E->getType()->isVectorType()) |
| 1068 | return this->VisitVectorBinOp(E); |
| 1069 | if ((LHS->getType()->isAnyComplexType() || |
| 1070 | RHS->getType()->isAnyComplexType()) && |
| 1071 | E->isComparisonOp()) |
| 1072 | return this->emitComplexComparison(LHS, RHS, E); |
| 1073 | if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType()) |
| 1074 | return this->VisitFixedPointBinOp(E); |
| 1075 | |
| 1076 | if (E->isPtrMemOp()) { |
| 1077 | if (E->containsErrors()) |
| 1078 | return false; |
| 1079 | |
| 1080 | if (!this->visit(E: LHS)) |
| 1081 | return false; |
| 1082 | |
| 1083 | if (!this->visit(E: RHS)) |
| 1084 | return false; |
| 1085 | |
| 1086 | if (!this->emitToMemberPtr(E)) |
| 1087 | return false; |
| 1088 | |
| 1089 | if (classifyPrim(E) == PT_MemberPtr) |
| 1090 | return true; |
| 1091 | |
| 1092 | if (!this->emitCastMemberPtrPtr(E)) |
| 1093 | return false; |
| 1094 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 1095 | } |
| 1096 | |
| 1097 | // Typecheck the args. |
| 1098 | OptPrimType LT = classify(LHS); |
| 1099 | OptPrimType RT = classify(RHS); |
| 1100 | OptPrimType T = classify(E->getType()); |
| 1101 | |
| 1102 | // Special case for C++'s three-way/spaceship operator <=>, which |
| 1103 | // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't |
| 1104 | // have a PrimType). |
| 1105 | if (!T && E->getOpcode() == BO_Cmp) { |
| 1106 | if (DiscardResult) |
| 1107 | return true; |
| 1108 | const ComparisonCategoryInfo *CmpInfo = |
| 1109 | Ctx.getASTContext().CompCategories.lookupInfoForType(Ty: E->getType()); |
| 1110 | assert(CmpInfo); |
| 1111 | |
| 1112 | // We need a temporary variable holding our return value. |
| 1113 | if (!Initializing) { |
| 1114 | UnsignedOrNone ResultIndex = this->allocateLocal(Decl: E); |
| 1115 | if (!this->emitGetPtrLocal(*ResultIndex, E)) |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | if (!visit(E: LHS) || !visit(E: RHS)) |
| 1120 | return false; |
| 1121 | |
| 1122 | return this->emitCMP3(*LT, CmpInfo, E); |
| 1123 | } |
| 1124 | |
| 1125 | if (!LT || !RT || !T) |
| 1126 | return false; |
| 1127 | |
| 1128 | // Pointer arithmetic special case. |
| 1129 | if (E->getOpcode() == BO_Add || E->getOpcode() == BO_Sub) { |
| 1130 | if (isPtrType(T: *T) || (isPtrType(T: *LT) && isPtrType(T: *RT))) |
| 1131 | return this->VisitPointerArithBinOp(E); |
| 1132 | } |
| 1133 | |
| 1134 | if (E->getOpcode() == BO_Assign) |
| 1135 | return this->visitAssignment(LHS, RHS, E); |
| 1136 | |
| 1137 | if (!visit(E: LHS) || !visit(E: RHS)) |
| 1138 | return false; |
| 1139 | |
| 1140 | // For languages such as C, cast the result of one |
| 1141 | // of our comparision opcodes to T (which is usually int). |
| 1142 | auto MaybeCastToBool = [this, T, E](bool Result) { |
| 1143 | if (!Result) |
| 1144 | return false; |
| 1145 | if (DiscardResult) |
| 1146 | return this->emitPopBool(E); |
| 1147 | if (T != PT_Bool) |
| 1148 | return this->emitCast(PT_Bool, *T, E); |
| 1149 | return true; |
| 1150 | }; |
| 1151 | |
| 1152 | auto Discard = [this, T, E](bool Result) { |
| 1153 | if (!Result) |
| 1154 | return false; |
| 1155 | return DiscardResult ? this->emitPop(*T, E) : true; |
| 1156 | }; |
| 1157 | |
| 1158 | switch (E->getOpcode()) { |
| 1159 | case BO_EQ: |
| 1160 | return MaybeCastToBool(this->emitEQ(*LT, E)); |
| 1161 | case BO_NE: |
| 1162 | return MaybeCastToBool(this->emitNE(*LT, E)); |
| 1163 | case BO_LT: |
| 1164 | return MaybeCastToBool(this->emitLT(*LT, E)); |
| 1165 | case BO_LE: |
| 1166 | return MaybeCastToBool(this->emitLE(*LT, E)); |
| 1167 | case BO_GT: |
| 1168 | return MaybeCastToBool(this->emitGT(*LT, E)); |
| 1169 | case BO_GE: |
| 1170 | return MaybeCastToBool(this->emitGE(*LT, E)); |
| 1171 | case BO_Sub: |
| 1172 | if (E->getType()->isFloatingType()) |
| 1173 | return Discard(this->emitSubf(getFPOptions(E), E)); |
| 1174 | return Discard(this->emitSub(*T, E)); |
| 1175 | case BO_Add: |
| 1176 | if (E->getType()->isFloatingType()) |
| 1177 | return Discard(this->emitAddf(getFPOptions(E), E)); |
| 1178 | return Discard(this->emitAdd(*T, E)); |
| 1179 | case BO_Mul: |
| 1180 | if (E->getType()->isFloatingType()) |
| 1181 | return Discard(this->emitMulf(getFPOptions(E), E)); |
| 1182 | return Discard(this->emitMul(*T, E)); |
| 1183 | case BO_Rem: |
| 1184 | return Discard(this->emitRem(*T, E)); |
| 1185 | case BO_Div: |
| 1186 | if (E->getType()->isFloatingType()) |
| 1187 | return Discard(this->emitDivf(getFPOptions(E), E)); |
| 1188 | return Discard(this->emitDiv(*T, E)); |
| 1189 | case BO_And: |
| 1190 | return Discard(this->emitBitAnd(*T, E)); |
| 1191 | case BO_Or: |
| 1192 | return Discard(this->emitBitOr(*T, E)); |
| 1193 | case BO_Shl: |
| 1194 | return Discard(this->emitShl(*LT, *RT, E)); |
| 1195 | case BO_Shr: |
| 1196 | return Discard(this->emitShr(*LT, *RT, E)); |
| 1197 | case BO_Xor: |
| 1198 | return Discard(this->emitBitXor(*T, E)); |
| 1199 | case BO_LOr: |
| 1200 | case BO_LAnd: |
| 1201 | llvm_unreachable("Already handled earlier" ); |
| 1202 | default: |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | llvm_unreachable("Unhandled binary op" ); |
| 1207 | } |
| 1208 | |
| 1209 | /// Perform addition/subtraction of a pointer and an integer or |
| 1210 | /// subtraction of two pointers. |
| 1211 | template <class Emitter> |
| 1212 | bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) { |
| 1213 | BinaryOperatorKind Op = E->getOpcode(); |
| 1214 | const Expr *LHS = E->getLHS(); |
| 1215 | const Expr *RHS = E->getRHS(); |
| 1216 | |
| 1217 | if ((Op != BO_Add && Op != BO_Sub) || |
| 1218 | (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType())) |
| 1219 | return false; |
| 1220 | |
| 1221 | OptPrimType LT = classify(LHS); |
| 1222 | OptPrimType RT = classify(RHS); |
| 1223 | |
| 1224 | if (!LT || !RT) |
| 1225 | return false; |
| 1226 | |
| 1227 | // Visit the given pointer expression and optionally convert to a PT_Ptr. |
| 1228 | auto visitAsPointer = [&](const Expr *E, PrimType T) -> bool { |
| 1229 | if (!this->visit(E)) |
| 1230 | return false; |
| 1231 | if (T != PT_Ptr) |
| 1232 | return this->emitDecayPtr(T, PT_Ptr, E); |
| 1233 | return true; |
| 1234 | }; |
| 1235 | |
| 1236 | if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) { |
| 1237 | if (Op != BO_Sub) |
| 1238 | return false; |
| 1239 | |
| 1240 | assert(E->getType()->isIntegerType()); |
| 1241 | if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT)) |
| 1242 | return false; |
| 1243 | |
| 1244 | QualType ElemType = LHS->getType()->getPointeeType(); |
| 1245 | CharUnits ElemTypeSize; |
| 1246 | if (ElemType->isVoidType() || ElemType->isFunctionType()) |
| 1247 | ElemTypeSize = CharUnits::One(); |
| 1248 | else |
| 1249 | ElemTypeSize = Ctx.getASTContext().getTypeSizeInChars(T: ElemType); |
| 1250 | |
| 1251 | PrimType IntT = classifyPrim(E->getType()); |
| 1252 | if (!this->emitSubPtr(IntT, ElemTypeSize.isZero(), E)) |
| 1253 | return false; |
| 1254 | return DiscardResult ? this->emitPop(IntT, E) : true; |
| 1255 | } |
| 1256 | |
| 1257 | PrimType OffsetType; |
| 1258 | if (LHS->getType()->isIntegerType()) { |
| 1259 | if (!visitAsPointer(RHS, *RT)) |
| 1260 | return false; |
| 1261 | if (!this->visit(E: LHS)) |
| 1262 | return false; |
| 1263 | OffsetType = *LT; |
| 1264 | } else if (RHS->getType()->isIntegerType()) { |
| 1265 | if (!visitAsPointer(LHS, *LT)) |
| 1266 | return false; |
| 1267 | if (!this->visit(E: RHS)) |
| 1268 | return false; |
| 1269 | OffsetType = *RT; |
| 1270 | } else { |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
| 1274 | // Do the operation and optionally transform to |
| 1275 | // result pointer type. |
| 1276 | switch (Op) { |
| 1277 | case BO_Add: |
| 1278 | if (!this->emitAddOffset(OffsetType, E)) |
| 1279 | return false; |
| 1280 | break; |
| 1281 | case BO_Sub: |
| 1282 | if (!this->emitSubOffset(OffsetType, E)) |
| 1283 | return false; |
| 1284 | break; |
| 1285 | default: |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
| 1289 | if (classifyPrim(E) != PT_Ptr) { |
| 1290 | if (!this->emitDecayPtr(PT_Ptr, classifyPrim(E), E)) |
| 1291 | return false; |
| 1292 | } |
| 1293 | |
| 1294 | if (DiscardResult) |
| 1295 | return this->emitPop(classifyPrim(E), E); |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
| 1299 | template <class Emitter> |
| 1300 | bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) { |
| 1301 | assert(E->isLogicalOp()); |
| 1302 | BinaryOperatorKind Op = E->getOpcode(); |
| 1303 | const Expr *LHS = E->getLHS(); |
| 1304 | const Expr *RHS = E->getRHS(); |
| 1305 | OptPrimType T = classify(E->getType()); |
| 1306 | |
| 1307 | if (Op == BO_LOr) { |
| 1308 | // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE. |
| 1309 | LabelTy LabelTrue = this->getLabel(); |
| 1310 | LabelTy LabelEnd = this->getLabel(); |
| 1311 | |
| 1312 | if (!this->visitBool(E: LHS)) |
| 1313 | return false; |
| 1314 | if (!this->jumpTrue(LabelTrue, E)) |
| 1315 | return false; |
| 1316 | |
| 1317 | if (!this->visitBool(E: RHS)) |
| 1318 | return false; |
| 1319 | if (!this->jump(LabelEnd, E)) |
| 1320 | return false; |
| 1321 | |
| 1322 | this->emitLabel(LabelTrue); |
| 1323 | this->emitConstBool(true, E); |
| 1324 | this->fallthrough(LabelEnd); |
| 1325 | this->emitLabel(LabelEnd); |
| 1326 | |
| 1327 | } else { |
| 1328 | assert(Op == BO_LAnd); |
| 1329 | // Logical AND. |
| 1330 | // Visit LHS. Only visit RHS if LHS was TRUE. |
| 1331 | LabelTy LabelFalse = this->getLabel(); |
| 1332 | LabelTy LabelEnd = this->getLabel(); |
| 1333 | |
| 1334 | if (!this->visitBool(E: LHS)) |
| 1335 | return false; |
| 1336 | if (!this->jumpFalse(LabelFalse, E)) |
| 1337 | return false; |
| 1338 | |
| 1339 | if (!this->visitBool(E: RHS)) |
| 1340 | return false; |
| 1341 | if (!this->jump(LabelEnd, E)) |
| 1342 | return false; |
| 1343 | |
| 1344 | this->emitLabel(LabelFalse); |
| 1345 | this->emitConstBool(false, E); |
| 1346 | this->fallthrough(LabelEnd); |
| 1347 | this->emitLabel(LabelEnd); |
| 1348 | } |
| 1349 | |
| 1350 | if (DiscardResult) |
| 1351 | return this->emitPopBool(E); |
| 1352 | |
| 1353 | // For C, cast back to integer type. |
| 1354 | assert(T); |
| 1355 | if (T != PT_Bool) |
| 1356 | return this->emitCast(PT_Bool, *T, E); |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
| 1360 | template <class Emitter> |
| 1361 | bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) { |
| 1362 | // Prepare storage for result. |
| 1363 | if (!Initializing) { |
| 1364 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1365 | if (!LocalIndex) |
| 1366 | return false; |
| 1367 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | // Both LHS and RHS might _not_ be of complex type, but one of them |
| 1372 | // needs to be. |
| 1373 | const Expr *LHS = E->getLHS(); |
| 1374 | const Expr *RHS = E->getRHS(); |
| 1375 | |
| 1376 | PrimType ResultElemT = this->classifyComplexElementType(T: E->getType()); |
| 1377 | unsigned ResultOffset = ~0u; |
| 1378 | if (!DiscardResult) |
| 1379 | ResultOffset = this->allocateLocalPrimitive(Decl: E, Ty: PT_Ptr, /*IsConst=*/true); |
| 1380 | |
| 1381 | // Save result pointer in ResultOffset |
| 1382 | if (!this->DiscardResult) { |
| 1383 | if (!this->emitDupPtr(E)) |
| 1384 | return false; |
| 1385 | if (!this->emitSetLocal(PT_Ptr, ResultOffset, E)) |
| 1386 | return false; |
| 1387 | } |
| 1388 | QualType LHSType = LHS->getType(); |
| 1389 | if (const auto *AT = LHSType->getAs<AtomicType>()) |
| 1390 | LHSType = AT->getValueType(); |
| 1391 | QualType RHSType = RHS->getType(); |
| 1392 | if (const auto *AT = RHSType->getAs<AtomicType>()) |
| 1393 | RHSType = AT->getValueType(); |
| 1394 | |
| 1395 | bool LHSIsComplex = LHSType->isAnyComplexType(); |
| 1396 | unsigned LHSOffset; |
| 1397 | bool RHSIsComplex = RHSType->isAnyComplexType(); |
| 1398 | |
| 1399 | // For ComplexComplex Mul, we have special ops to make their implementation |
| 1400 | // easier. |
| 1401 | BinaryOperatorKind Op = E->getOpcode(); |
| 1402 | if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) { |
| 1403 | assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) == |
| 1404 | classifyPrim(RHSType->getAs<ComplexType>()->getElementType())); |
| 1405 | PrimType ElemT = |
| 1406 | classifyPrim(LHSType->getAs<ComplexType>()->getElementType()); |
| 1407 | if (!this->visit(E: LHS)) |
| 1408 | return false; |
| 1409 | if (!this->visit(E: RHS)) |
| 1410 | return false; |
| 1411 | if (!this->emitMulc(ElemT, E)) |
| 1412 | return false; |
| 1413 | if (DiscardResult) |
| 1414 | return this->emitPopPtr(E); |
| 1415 | return true; |
| 1416 | } |
| 1417 | |
| 1418 | if (Op == BO_Div && RHSIsComplex) { |
| 1419 | QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType(); |
| 1420 | PrimType ElemT = classifyPrim(ElemQT); |
| 1421 | // If the LHS is not complex, we still need to do the full complex |
| 1422 | // division, so just stub create a complex value and stub it out with |
| 1423 | // the LHS and a zero. |
| 1424 | |
| 1425 | if (!LHSIsComplex) { |
| 1426 | // This is using the RHS type for the fake-complex LHS. |
| 1427 | UnsignedOrNone LocalIndex = allocateTemporary(E: RHS); |
| 1428 | if (!LocalIndex) |
| 1429 | return false; |
| 1430 | LHSOffset = *LocalIndex; |
| 1431 | |
| 1432 | if (!this->emitGetPtrLocal(LHSOffset, E)) |
| 1433 | return false; |
| 1434 | |
| 1435 | if (!this->visit(E: LHS)) |
| 1436 | return false; |
| 1437 | // real is LHS |
| 1438 | if (!this->emitInitElem(ElemT, 0, E)) |
| 1439 | return false; |
| 1440 | // imag is zero |
| 1441 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 1442 | return false; |
| 1443 | if (!this->emitInitElem(ElemT, 1, E)) |
| 1444 | return false; |
| 1445 | } else { |
| 1446 | if (!this->visit(E: LHS)) |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
| 1450 | if (!this->visit(E: RHS)) |
| 1451 | return false; |
| 1452 | if (!this->emitDivc(ElemT, E)) |
| 1453 | return false; |
| 1454 | if (DiscardResult) |
| 1455 | return this->emitPopPtr(E); |
| 1456 | return true; |
| 1457 | } |
| 1458 | |
| 1459 | // Evaluate LHS and save value to LHSOffset. |
| 1460 | if (LHSType->isAnyComplexType()) { |
| 1461 | LHSOffset = this->allocateLocalPrimitive(Decl: LHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1462 | if (!this->visit(E: LHS)) |
| 1463 | return false; |
| 1464 | if (!this->emitSetLocal(PT_Ptr, LHSOffset, E)) |
| 1465 | return false; |
| 1466 | } else { |
| 1467 | PrimType LHST = classifyPrim(LHSType); |
| 1468 | LHSOffset = this->allocateLocalPrimitive(Decl: LHS, Ty: LHST, /*IsConst=*/true); |
| 1469 | if (!this->visit(E: LHS)) |
| 1470 | return false; |
| 1471 | if (!this->emitSetLocal(LHST, LHSOffset, E)) |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
| 1475 | // Same with RHS. |
| 1476 | unsigned RHSOffset; |
| 1477 | if (RHSType->isAnyComplexType()) { |
| 1478 | RHSOffset = this->allocateLocalPrimitive(Decl: RHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1479 | if (!this->visit(E: RHS)) |
| 1480 | return false; |
| 1481 | if (!this->emitSetLocal(PT_Ptr, RHSOffset, E)) |
| 1482 | return false; |
| 1483 | } else { |
| 1484 | PrimType RHST = classifyPrim(RHSType); |
| 1485 | RHSOffset = this->allocateLocalPrimitive(Decl: RHS, Ty: RHST, /*IsConst=*/true); |
| 1486 | if (!this->visit(E: RHS)) |
| 1487 | return false; |
| 1488 | if (!this->emitSetLocal(RHST, RHSOffset, E)) |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | // For both LHS and RHS, either load the value from the complex pointer, or |
| 1493 | // directly from the local variable. For index 1 (i.e. the imaginary part), |
| 1494 | // just load 0 and do the operation anyway. |
| 1495 | auto loadComplexValue = [this](bool IsComplex, bool LoadZero, |
| 1496 | unsigned ElemIndex, unsigned Offset, |
| 1497 | const Expr *E) -> bool { |
| 1498 | if (IsComplex) { |
| 1499 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 1500 | return false; |
| 1501 | return this->emitArrayElemPop(classifyComplexElementType(T: E->getType()), |
| 1502 | ElemIndex, E); |
| 1503 | } |
| 1504 | if (ElemIndex == 0 || !LoadZero) |
| 1505 | return this->emitGetLocal(classifyPrim(E->getType()), Offset, E); |
| 1506 | return this->visitZeroInitializer(T: classifyPrim(E->getType()), QT: E->getType(), |
| 1507 | E); |
| 1508 | }; |
| 1509 | |
| 1510 | // Now we can get pointers to the LHS and RHS from the offsets above. |
| 1511 | for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) { |
| 1512 | // Result pointer for the store later. |
| 1513 | if (!this->DiscardResult) { |
| 1514 | if (!this->emitGetLocal(PT_Ptr, ResultOffset, E)) |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
| 1518 | // The actual operation. |
| 1519 | switch (Op) { |
| 1520 | case BO_Add: |
| 1521 | if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS)) |
| 1522 | return false; |
| 1523 | |
| 1524 | if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS)) |
| 1525 | return false; |
| 1526 | if (ResultElemT == PT_Float) { |
| 1527 | if (!this->emitAddf(getFPOptions(E), E)) |
| 1528 | return false; |
| 1529 | } else { |
| 1530 | if (!this->emitAdd(ResultElemT, E)) |
| 1531 | return false; |
| 1532 | } |
| 1533 | break; |
| 1534 | case BO_Sub: |
| 1535 | if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS)) |
| 1536 | return false; |
| 1537 | |
| 1538 | if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS)) |
| 1539 | return false; |
| 1540 | if (ResultElemT == PT_Float) { |
| 1541 | if (!this->emitSubf(getFPOptions(E), E)) |
| 1542 | return false; |
| 1543 | } else { |
| 1544 | if (!this->emitSub(ResultElemT, E)) |
| 1545 | return false; |
| 1546 | } |
| 1547 | break; |
| 1548 | case BO_Mul: |
| 1549 | if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS)) |
| 1550 | return false; |
| 1551 | |
| 1552 | if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS)) |
| 1553 | return false; |
| 1554 | |
| 1555 | if (ResultElemT == PT_Float) { |
| 1556 | if (!this->emitMulf(getFPOptions(E), E)) |
| 1557 | return false; |
| 1558 | } else { |
| 1559 | if (!this->emitMul(ResultElemT, E)) |
| 1560 | return false; |
| 1561 | } |
| 1562 | break; |
| 1563 | case BO_Div: |
| 1564 | assert(!RHSIsComplex); |
| 1565 | if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS)) |
| 1566 | return false; |
| 1567 | |
| 1568 | if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS)) |
| 1569 | return false; |
| 1570 | |
| 1571 | if (ResultElemT == PT_Float) { |
| 1572 | if (!this->emitDivf(getFPOptions(E), E)) |
| 1573 | return false; |
| 1574 | } else { |
| 1575 | if (!this->emitDiv(ResultElemT, E)) |
| 1576 | return false; |
| 1577 | } |
| 1578 | break; |
| 1579 | |
| 1580 | default: |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | if (!this->DiscardResult) { |
| 1585 | // Initialize array element with the value we just computed. |
| 1586 | if (!this->emitInitElemPop(ResultElemT, ElemIndex, E)) |
| 1587 | return false; |
| 1588 | } else { |
| 1589 | if (!this->emitPop(ResultElemT, E)) |
| 1590 | return false; |
| 1591 | // Remove the Complex temporary pointer we created ourselves at the |
| 1592 | // beginning of this function. |
| 1593 | if (!Initializing) |
| 1594 | return this->emitPopPtr(E); |
| 1595 | } |
| 1596 | } |
| 1597 | return true; |
| 1598 | } |
| 1599 | |
| 1600 | template <class Emitter> |
| 1601 | bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) { |
| 1602 | const Expr *LHS = E->getLHS(); |
| 1603 | const Expr *RHS = E->getRHS(); |
| 1604 | assert(!E->isCommaOp() && |
| 1605 | "Comma op should be handled in VisitBinaryOperator" ); |
| 1606 | assert(E->getType()->isVectorType()); |
| 1607 | assert(LHS->getType()->isVectorType()); |
| 1608 | assert(RHS->getType()->isVectorType()); |
| 1609 | |
| 1610 | // We can only handle vectors with primitive element types. |
| 1611 | if (!canClassify(LHS->getType()->castAs<VectorType>()->getElementType())) |
| 1612 | return false; |
| 1613 | |
| 1614 | // Prepare storage for result. |
| 1615 | if (!Initializing && !E->isCompoundAssignmentOp() && !E->isAssignmentOp()) { |
| 1616 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1617 | if (!LocalIndex) |
| 1618 | return false; |
| 1619 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1620 | return false; |
| 1621 | } |
| 1622 | |
| 1623 | const auto *VecTy = E->getType()->getAs<VectorType>(); |
| 1624 | auto Op = E->isCompoundAssignmentOp() |
| 1625 | ? BinaryOperator::getOpForCompoundAssignment(Opc: E->getOpcode()) |
| 1626 | : E->getOpcode(); |
| 1627 | |
| 1628 | PrimType ElemT = this->classifyVectorElementType(T: LHS->getType()); |
| 1629 | PrimType RHSElemT = this->classifyVectorElementType(T: RHS->getType()); |
| 1630 | PrimType ResultElemT = this->classifyVectorElementType(T: E->getType()); |
| 1631 | |
| 1632 | if (E->getOpcode() == BO_Assign) { |
| 1633 | assert(Ctx.getASTContext().hasSameUnqualifiedType( |
| 1634 | LHS->getType()->castAs<VectorType>()->getElementType(), |
| 1635 | RHS->getType()->castAs<VectorType>()->getElementType())); |
| 1636 | if (!this->visit(E: LHS)) |
| 1637 | return false; |
| 1638 | if (!this->visit(E: RHS)) |
| 1639 | return false; |
| 1640 | if (!this->emitCopyArray(ElemT, 0, 0, VecTy->getNumElements(), E)) |
| 1641 | return false; |
| 1642 | if (DiscardResult) |
| 1643 | return this->emitPopPtr(E); |
| 1644 | return true; |
| 1645 | } |
| 1646 | |
| 1647 | // Evaluate LHS and save value to LHSOffset. |
| 1648 | unsigned LHSOffset = |
| 1649 | this->allocateLocalPrimitive(Decl: LHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1650 | if (!this->visit(E: LHS)) |
| 1651 | return false; |
| 1652 | if (!this->emitSetLocal(PT_Ptr, LHSOffset, E)) |
| 1653 | return false; |
| 1654 | |
| 1655 | // Evaluate RHS and save value to RHSOffset. |
| 1656 | unsigned RHSOffset = |
| 1657 | this->allocateLocalPrimitive(Decl: RHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1658 | if (!this->visit(E: RHS)) |
| 1659 | return false; |
| 1660 | if (!this->emitSetLocal(PT_Ptr, RHSOffset, E)) |
| 1661 | return false; |
| 1662 | |
| 1663 | if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E)) |
| 1664 | return false; |
| 1665 | |
| 1666 | // BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the |
| 1667 | // integer promotion. |
| 1668 | bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp()); |
| 1669 | QualType PromotTy; |
| 1670 | PrimType PromotT = PT_Bool; |
| 1671 | PrimType OpT = ElemT; |
| 1672 | if (NeedIntPromot) { |
| 1673 | PromotTy = |
| 1674 | Ctx.getASTContext().getPromotedIntegerType(PromotableType: Ctx.getASTContext().BoolTy); |
| 1675 | PromotT = classifyPrim(PromotTy); |
| 1676 | OpT = PromotT; |
| 1677 | } |
| 1678 | |
| 1679 | auto getElem = [=](unsigned Offset, PrimType ElemT, unsigned Index) { |
| 1680 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 1681 | return false; |
| 1682 | if (!this->emitArrayElemPop(ElemT, Index, E)) |
| 1683 | return false; |
| 1684 | if (E->isLogicalOp()) { |
| 1685 | if (!this->emitPrimCast(FromT: ElemT, ToT: PT_Bool, ToQT: Ctx.getASTContext().BoolTy, E)) |
| 1686 | return false; |
| 1687 | if (!this->emitPrimCast(FromT: PT_Bool, ToT: ResultElemT, ToQT: VecTy->getElementType(), E)) |
| 1688 | return false; |
| 1689 | } else if (NeedIntPromot) { |
| 1690 | if (!this->emitPrimCast(FromT: ElemT, ToT: PromotT, ToQT: PromotTy, E)) |
| 1691 | return false; |
| 1692 | } |
| 1693 | return true; |
| 1694 | }; |
| 1695 | |
| 1696 | #define EMIT_ARITH_OP(OP) \ |
| 1697 | { \ |
| 1698 | if (ElemT == PT_Float) { \ |
| 1699 | if (!this->emit##OP##f(getFPOptions(E), E)) \ |
| 1700 | return false; \ |
| 1701 | } else { \ |
| 1702 | if (!this->emit##OP(ElemT, E)) \ |
| 1703 | return false; \ |
| 1704 | } \ |
| 1705 | break; \ |
| 1706 | } |
| 1707 | |
| 1708 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 1709 | if (!getElem(LHSOffset, ElemT, I)) |
| 1710 | return false; |
| 1711 | if (!getElem(RHSOffset, RHSElemT, I)) |
| 1712 | return false; |
| 1713 | switch (Op) { |
| 1714 | case BO_Add: |
| 1715 | EMIT_ARITH_OP(Add) |
| 1716 | case BO_Sub: |
| 1717 | EMIT_ARITH_OP(Sub) |
| 1718 | case BO_Mul: |
| 1719 | EMIT_ARITH_OP(Mul) |
| 1720 | case BO_Div: |
| 1721 | EMIT_ARITH_OP(Div) |
| 1722 | case BO_Rem: |
| 1723 | if (!this->emitRem(ElemT, E)) |
| 1724 | return false; |
| 1725 | break; |
| 1726 | case BO_And: |
| 1727 | if (!this->emitBitAnd(OpT, E)) |
| 1728 | return false; |
| 1729 | break; |
| 1730 | case BO_Or: |
| 1731 | if (!this->emitBitOr(OpT, E)) |
| 1732 | return false; |
| 1733 | break; |
| 1734 | case BO_Xor: |
| 1735 | if (!this->emitBitXor(OpT, E)) |
| 1736 | return false; |
| 1737 | break; |
| 1738 | case BO_Shl: |
| 1739 | if (!this->emitShl(OpT, RHSElemT, E)) |
| 1740 | return false; |
| 1741 | break; |
| 1742 | case BO_Shr: |
| 1743 | if (!this->emitShr(OpT, RHSElemT, E)) |
| 1744 | return false; |
| 1745 | break; |
| 1746 | case BO_EQ: |
| 1747 | if (!this->emitEQ(ElemT, E)) |
| 1748 | return false; |
| 1749 | break; |
| 1750 | case BO_NE: |
| 1751 | if (!this->emitNE(ElemT, E)) |
| 1752 | return false; |
| 1753 | break; |
| 1754 | case BO_LE: |
| 1755 | if (!this->emitLE(ElemT, E)) |
| 1756 | return false; |
| 1757 | break; |
| 1758 | case BO_LT: |
| 1759 | if (!this->emitLT(ElemT, E)) |
| 1760 | return false; |
| 1761 | break; |
| 1762 | case BO_GE: |
| 1763 | if (!this->emitGE(ElemT, E)) |
| 1764 | return false; |
| 1765 | break; |
| 1766 | case BO_GT: |
| 1767 | if (!this->emitGT(ElemT, E)) |
| 1768 | return false; |
| 1769 | break; |
| 1770 | case BO_LAnd: |
| 1771 | // a && b is equivalent to a!=0 & b!=0 |
| 1772 | if (!this->emitBitAnd(ResultElemT, E)) |
| 1773 | return false; |
| 1774 | break; |
| 1775 | case BO_LOr: |
| 1776 | // a || b is equivalent to a!=0 | b!=0 |
| 1777 | if (!this->emitBitOr(ResultElemT, E)) |
| 1778 | return false; |
| 1779 | break; |
| 1780 | default: |
| 1781 | return this->emitInvalid(E); |
| 1782 | } |
| 1783 | |
| 1784 | // The result of the comparison is a vector of the same width and number |
| 1785 | // of elements as the comparison operands with a signed integral element |
| 1786 | // type. |
| 1787 | // |
| 1788 | // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html |
| 1789 | if (E->isComparisonOp()) { |
| 1790 | if (!this->emitPrimCast(FromT: PT_Bool, ToT: ResultElemT, ToQT: VecTy->getElementType(), E)) |
| 1791 | return false; |
| 1792 | if (!this->emitNeg(ResultElemT, E)) |
| 1793 | return false; |
| 1794 | } |
| 1795 | |
| 1796 | // If we performed an integer promotion, we need to cast the compute result |
| 1797 | // into result vector element type. |
| 1798 | if (NeedIntPromot && |
| 1799 | !this->emitPrimCast(FromT: PromotT, ToT: ResultElemT, ToQT: VecTy->getElementType(), E)) |
| 1800 | return false; |
| 1801 | |
| 1802 | // Initialize array element with the value we just computed. |
| 1803 | if (!this->emitInitElem(ResultElemT, I, E)) |
| 1804 | return false; |
| 1805 | } |
| 1806 | |
| 1807 | if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E)) |
| 1808 | return false; |
| 1809 | return true; |
| 1810 | } |
| 1811 | |
| 1812 | template <class Emitter> |
| 1813 | bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) { |
| 1814 | const Expr *LHS = E->getLHS(); |
| 1815 | const Expr *RHS = E->getRHS(); |
| 1816 | const ASTContext &ASTCtx = Ctx.getASTContext(); |
| 1817 | |
| 1818 | assert(LHS->getType()->isFixedPointType() || |
| 1819 | RHS->getType()->isFixedPointType()); |
| 1820 | |
| 1821 | auto LHSSema = ASTCtx.getFixedPointSemantics(Ty: LHS->getType()); |
| 1822 | auto LHSSemaInt = LHSSema.toOpaqueInt(); |
| 1823 | auto RHSSema = ASTCtx.getFixedPointSemantics(Ty: RHS->getType()); |
| 1824 | auto RHSSemaInt = RHSSema.toOpaqueInt(); |
| 1825 | |
| 1826 | if (!this->visit(E: LHS)) |
| 1827 | return false; |
| 1828 | if (!LHS->getType()->isFixedPointType()) { |
| 1829 | if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), |
| 1830 | LHSSemaInt, E)) |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
| 1834 | if (!this->visit(E: RHS)) |
| 1835 | return false; |
| 1836 | if (!RHS->getType()->isFixedPointType()) { |
| 1837 | if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), |
| 1838 | RHSSemaInt, E)) |
| 1839 | return false; |
| 1840 | } |
| 1841 | |
| 1842 | // Convert the result to the target semantics. |
| 1843 | auto ConvertResult = [&](bool R) -> bool { |
| 1844 | if (!R) |
| 1845 | return false; |
| 1846 | auto ResultSema = ASTCtx.getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 1847 | auto CommonSema = LHSSema.getCommonSemantics(Other: RHSSema).toOpaqueInt(); |
| 1848 | if (ResultSema != CommonSema) |
| 1849 | return this->emitCastFixedPoint(ResultSema, E); |
| 1850 | return true; |
| 1851 | }; |
| 1852 | |
| 1853 | auto MaybeCastToBool = [&](bool Result) { |
| 1854 | if (!Result) |
| 1855 | return false; |
| 1856 | PrimType T = classifyPrim(E); |
| 1857 | if (DiscardResult) |
| 1858 | return this->emitPop(T, E); |
| 1859 | if (T != PT_Bool) |
| 1860 | return this->emitCast(PT_Bool, T, E); |
| 1861 | return true; |
| 1862 | }; |
| 1863 | |
| 1864 | switch (E->getOpcode()) { |
| 1865 | case BO_EQ: |
| 1866 | return MaybeCastToBool(this->emitEQFixedPoint(E)); |
| 1867 | case BO_NE: |
| 1868 | return MaybeCastToBool(this->emitNEFixedPoint(E)); |
| 1869 | case BO_LT: |
| 1870 | return MaybeCastToBool(this->emitLTFixedPoint(E)); |
| 1871 | case BO_LE: |
| 1872 | return MaybeCastToBool(this->emitLEFixedPoint(E)); |
| 1873 | case BO_GT: |
| 1874 | return MaybeCastToBool(this->emitGTFixedPoint(E)); |
| 1875 | case BO_GE: |
| 1876 | return MaybeCastToBool(this->emitGEFixedPoint(E)); |
| 1877 | case BO_Add: |
| 1878 | return ConvertResult(this->emitAddFixedPoint(E)); |
| 1879 | case BO_Sub: |
| 1880 | return ConvertResult(this->emitSubFixedPoint(E)); |
| 1881 | case BO_Mul: |
| 1882 | return ConvertResult(this->emitMulFixedPoint(E)); |
| 1883 | case BO_Div: |
| 1884 | return ConvertResult(this->emitDivFixedPoint(E)); |
| 1885 | case BO_Shl: |
| 1886 | return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E)); |
| 1887 | case BO_Shr: |
| 1888 | return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E)); |
| 1889 | |
| 1890 | default: |
| 1891 | return this->emitInvalid(E); |
| 1892 | } |
| 1893 | |
| 1894 | llvm_unreachable("unhandled binop opcode" ); |
| 1895 | } |
| 1896 | |
| 1897 | template <class Emitter> |
| 1898 | bool Compiler<Emitter>::VisitFixedPointUnaryOperator(const UnaryOperator *E) { |
| 1899 | const Expr *SubExpr = E->getSubExpr(); |
| 1900 | assert(SubExpr->getType()->isFixedPointType()); |
| 1901 | |
| 1902 | switch (E->getOpcode()) { |
| 1903 | case UO_Plus: |
| 1904 | return this->delegate(E: SubExpr); |
| 1905 | case UO_Minus: |
| 1906 | if (!this->visit(E: SubExpr)) |
| 1907 | return false; |
| 1908 | if (!this->emitNegFixedPoint(E)) |
| 1909 | return false; |
| 1910 | if (DiscardResult) |
| 1911 | return this->emitPopFixedPoint(E); |
| 1912 | return true; |
| 1913 | default: |
| 1914 | return false; |
| 1915 | } |
| 1916 | |
| 1917 | llvm_unreachable("Unhandled unary opcode" ); |
| 1918 | } |
| 1919 | |
| 1920 | template <class Emitter> |
| 1921 | bool Compiler<Emitter>::VisitImplicitValueInitExpr( |
| 1922 | const ImplicitValueInitExpr *E) { |
| 1923 | if (DiscardResult) |
| 1924 | return true; |
| 1925 | |
| 1926 | QualType QT = E->getType(); |
| 1927 | |
| 1928 | if (OptPrimType T = classify(QT)) |
| 1929 | return this->visitZeroInitializer(T: *T, QT, E); |
| 1930 | |
| 1931 | if (QT->isRecordType()) { |
| 1932 | const RecordDecl *RD = QT->getAsRecordDecl(); |
| 1933 | assert(RD); |
| 1934 | if (RD->isInvalidDecl()) |
| 1935 | return false; |
| 1936 | |
| 1937 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD); |
| 1938 | CXXRD && CXXRD->getNumVBases() > 0) { |
| 1939 | // TODO: Diagnose. |
| 1940 | return false; |
| 1941 | } |
| 1942 | |
| 1943 | const Record *R = getRecord(QT); |
| 1944 | if (!R) |
| 1945 | return false; |
| 1946 | |
| 1947 | assert(Initializing); |
| 1948 | return this->visitZeroRecordInitializer(R, E); |
| 1949 | } |
| 1950 | |
| 1951 | if (QT->isIncompleteArrayType()) |
| 1952 | return true; |
| 1953 | |
| 1954 | if (QT->isArrayType()) |
| 1955 | return this->visitZeroArrayInitializer(T: QT, E); |
| 1956 | |
| 1957 | if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) { |
| 1958 | assert(Initializing); |
| 1959 | QualType ElemQT = ComplexTy->getElementType(); |
| 1960 | PrimType ElemT = classifyPrim(ElemQT); |
| 1961 | for (unsigned I = 0; I < 2; ++I) { |
| 1962 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 1963 | return false; |
| 1964 | if (!this->emitInitElem(ElemT, I, E)) |
| 1965 | return false; |
| 1966 | } |
| 1967 | return true; |
| 1968 | } |
| 1969 | |
| 1970 | if (const auto *VecT = E->getType()->getAs<VectorType>()) { |
| 1971 | unsigned NumVecElements = VecT->getNumElements(); |
| 1972 | QualType ElemQT = VecT->getElementType(); |
| 1973 | PrimType ElemT = classifyPrim(ElemQT); |
| 1974 | |
| 1975 | for (unsigned I = 0; I < NumVecElements; ++I) { |
| 1976 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 1977 | return false; |
| 1978 | if (!this->emitInitElem(ElemT, I, E)) |
| 1979 | return false; |
| 1980 | } |
| 1981 | return true; |
| 1982 | } |
| 1983 | |
| 1984 | if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) { |
| 1985 | unsigned NumElems = MT->getNumElementsFlattened(); |
| 1986 | QualType ElemQT = MT->getElementType(); |
| 1987 | PrimType ElemT = classifyPrim(ElemQT); |
| 1988 | |
| 1989 | for (unsigned I = 0; I != NumElems; ++I) { |
| 1990 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 1991 | return false; |
| 1992 | if (!this->emitInitElem(ElemT, I, E)) |
| 1993 | return false; |
| 1994 | } |
| 1995 | return true; |
| 1996 | } |
| 1997 | |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | template <class Emitter> |
| 2002 | bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
| 2003 | if (E->getType()->isVoidType() || E->containsErrors()) |
| 2004 | return false; |
| 2005 | |
| 2006 | const Expr *LHS = E->getLHS(); |
| 2007 | const Expr *RHS = E->getRHS(); |
| 2008 | const Expr *Index = E->getIdx(); |
| 2009 | const Expr *Base = E->getBase(); |
| 2010 | |
| 2011 | // C++17's rules require us to evaluate the LHS first, regardless of which |
| 2012 | // side is the base. |
| 2013 | bool Success = true; |
| 2014 | for (const Expr *SubExpr : {LHS, RHS}) { |
| 2015 | if (!this->visit(E: SubExpr)) { |
| 2016 | Success = false; |
| 2017 | continue; |
| 2018 | } |
| 2019 | |
| 2020 | // Expand the base if this is a subscript on a |
| 2021 | // pointer expression. |
| 2022 | if (SubExpr == Base && Base->getType()->isPointerType()) { |
| 2023 | if (!this->emitExpandPtr(E)) |
| 2024 | Success = false; |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | if (!Success) |
| 2029 | return false; |
| 2030 | |
| 2031 | OptPrimType IndexT = classify(Index->getType()); |
| 2032 | // In error-recovery cases, the index expression has a dependent type. |
| 2033 | if (!IndexT) |
| 2034 | return this->emitError(E); |
| 2035 | // If the index is first, we need to change that. |
| 2036 | if (LHS == Index) { |
| 2037 | if (!this->emitFlip(PT_Ptr, *IndexT, E)) |
| 2038 | return false; |
| 2039 | } |
| 2040 | |
| 2041 | if (!this->emitArrayElemPtrPop(*IndexT, E)) |
| 2042 | return false; |
| 2043 | if (DiscardResult) |
| 2044 | return this->emitPopPtr(E); |
| 2045 | |
| 2046 | if (E->isGLValue()) |
| 2047 | return true; |
| 2048 | |
| 2049 | OptPrimType T = classifyPrim(E); |
| 2050 | return this->emitLoadPop(*T, E); |
| 2051 | } |
| 2052 | |
| 2053 | template <class Emitter> |
| 2054 | bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits, |
| 2055 | const Expr *ArrayFiller, const Expr *E) { |
| 2056 | InitLinkScope<Emitter> ILS(this, InitLink::InitList()); |
| 2057 | |
| 2058 | QualType QT = E->getType(); |
| 2059 | if (const auto *AT = QT->getAs<AtomicType>()) |
| 2060 | QT = AT->getValueType(); |
| 2061 | |
| 2062 | if (QT->isVoidType()) { |
| 2063 | if (Inits.size() == 0) |
| 2064 | return true; |
| 2065 | return this->emitInvalid(E); |
| 2066 | } |
| 2067 | |
| 2068 | // Handle discarding first. |
| 2069 | if (DiscardResult) { |
| 2070 | for (const Expr *Init : Inits) { |
| 2071 | if (!this->discard(E: Init)) |
| 2072 | return false; |
| 2073 | } |
| 2074 | return true; |
| 2075 | } |
| 2076 | |
| 2077 | // Primitive values. |
| 2078 | if (OptPrimType T = classify(QT)) { |
| 2079 | assert(!DiscardResult); |
| 2080 | if (Inits.size() == 0) |
| 2081 | return this->visitZeroInitializer(T: *T, QT, E); |
| 2082 | assert(Inits.size() == 1); |
| 2083 | return this->delegate(E: Inits[0]); |
| 2084 | } |
| 2085 | |
| 2086 | if (QT->isRecordType()) { |
| 2087 | const Record *R = getRecord(QT); |
| 2088 | |
| 2089 | if (Inits.size() == 1 && E->getType() == Inits[0]->getType()) |
| 2090 | return this->delegate(E: Inits[0]); |
| 2091 | |
| 2092 | if (!R) |
| 2093 | return false; |
| 2094 | |
| 2095 | auto initPrimitiveField = [=](const Record::Field *FieldToInit, |
| 2096 | const Expr *Init, PrimType T, |
| 2097 | bool Activate = false) -> bool { |
| 2098 | InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: Init)); |
| 2099 | if (!this->visit(E: Init)) |
| 2100 | return false; |
| 2101 | |
| 2102 | bool BitField = FieldToInit->isBitField(); |
| 2103 | if (BitField && Activate) |
| 2104 | return this->emitInitBitFieldActivate(T, FieldToInit->Offset, |
| 2105 | FieldToInit->bitWidth(), E); |
| 2106 | if (BitField) |
| 2107 | return this->emitInitBitField(T, FieldToInit->Offset, |
| 2108 | FieldToInit->bitWidth(), E); |
| 2109 | if (Activate) |
| 2110 | return this->emitInitFieldActivate(T, FieldToInit->Offset, E); |
| 2111 | return this->emitInitField(T, FieldToInit->Offset, E); |
| 2112 | }; |
| 2113 | |
| 2114 | auto initCompositeField = [=](const Record::Field *FieldToInit, |
| 2115 | const Expr *Init, |
| 2116 | bool Activate = false) -> bool { |
| 2117 | InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: Init)); |
| 2118 | InitLinkScope<Emitter> ILS(this, InitLink::Field(Offset: FieldToInit->Offset)); |
| 2119 | |
| 2120 | // Non-primitive case. Get a pointer to the field-to-initialize |
| 2121 | // on the stack and recurse into visitInitializer(). |
| 2122 | if (!this->emitGetPtrField(FieldToInit->Offset, Init)) |
| 2123 | return false; |
| 2124 | |
| 2125 | if (Activate && !this->emitActivate(E)) |
| 2126 | return false; |
| 2127 | |
| 2128 | return this->visitInitializerPop(E: Init); |
| 2129 | }; |
| 2130 | |
| 2131 | if (R->isUnion()) { |
| 2132 | if (Inits.size() == 0) { |
| 2133 | if (!this->visitZeroRecordInitializer(R, E)) |
| 2134 | return false; |
| 2135 | } else { |
| 2136 | const Expr *Init = Inits[0]; |
| 2137 | const FieldDecl *FToInit = nullptr; |
| 2138 | if (const auto *ILE = dyn_cast<InitListExpr>(Val: E)) |
| 2139 | FToInit = ILE->getInitializedFieldInUnion(); |
| 2140 | else |
| 2141 | FToInit = cast<CXXParenListInitExpr>(Val: E)->getInitializedFieldInUnion(); |
| 2142 | |
| 2143 | const Record::Field *FieldToInit = R->getField(FD: FToInit); |
| 2144 | if (OptPrimType T = classify(Init)) { |
| 2145 | if (!initPrimitiveField(FieldToInit, Init, *T, /*Activate=*/true)) |
| 2146 | return false; |
| 2147 | } else { |
| 2148 | if (!initCompositeField(FieldToInit, Init, /*Activate=*/true)) |
| 2149 | return false; |
| 2150 | } |
| 2151 | } |
| 2152 | return this->emitFinishInit(E); |
| 2153 | } |
| 2154 | |
| 2155 | assert(!R->isUnion()); |
| 2156 | for (unsigned BI = 0; BI != R->getNumBases(); ++BI) { |
| 2157 | const Expr *Init = Inits[BI]; |
| 2158 | const Record::Base *B = R->getBase(I: BI); |
| 2159 | if (!this->emitGetPtrBase(B->Offset, Init)) |
| 2160 | return false; |
| 2161 | if (!this->visitInitializerPop(E: Init)) |
| 2162 | return false; |
| 2163 | } |
| 2164 | |
| 2165 | unsigned FieldIndex = 0; |
| 2166 | for (unsigned FI = R->getNumBases(); FI != Inits.size();) { |
| 2167 | const Record::Field *FieldToInit = R->getField(I: FieldIndex); |
| 2168 | if (FieldToInit->isUnnamedBitField()) { |
| 2169 | ++FieldIndex; |
| 2170 | continue; |
| 2171 | } |
| 2172 | |
| 2173 | const Expr *Init = Inits[FI]; |
| 2174 | // If this is a child of a DesignatedInitUpdateExpr, skip elements which |
| 2175 | // aren't supposed to be modified. |
| 2176 | if (isa<NoInitExpr>(Val: Init)) { |
| 2177 | ++FieldIndex; |
| 2178 | ++FI; |
| 2179 | continue; |
| 2180 | } |
| 2181 | |
| 2182 | if (OptPrimType T = classify(Init)) { |
| 2183 | if (!initPrimitiveField(FieldToInit, Init, *T)) |
| 2184 | return false; |
| 2185 | } else if (!initCompositeField(FieldToInit, Init)) { |
| 2186 | return false; |
| 2187 | } |
| 2188 | |
| 2189 | ++FI; |
| 2190 | ++FieldIndex; |
| 2191 | } |
| 2192 | |
| 2193 | assert(R->getNumVirtualBases() == 0); |
| 2194 | |
| 2195 | return this->emitFinishInit(E); |
| 2196 | } |
| 2197 | |
| 2198 | if (QT->isArrayType()) { |
| 2199 | const ConstantArrayType *CAT = |
| 2200 | Ctx.getASTContext().getAsConstantArrayType(T: QT); |
| 2201 | uint64_t NumElems = CAT->getZExtSize(); |
| 2202 | |
| 2203 | if (Initializing && !this->emitCheckArrayDestSize(NumElems, E)) |
| 2204 | return false; |
| 2205 | |
| 2206 | if (Inits.size() == 1 && QT == Inits[0]->getType()) |
| 2207 | return this->delegate(E: Inits[0]); |
| 2208 | |
| 2209 | OptPrimType InitT = classify(CAT->getElementType()); |
| 2210 | unsigned ElementIndex = 0; |
| 2211 | for (const Expr *Init : Inits) { |
| 2212 | if (const auto *EmbedS = |
| 2213 | dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) { |
| 2214 | PrimType TargetT = classifyPrim(Init->getType()); |
| 2215 | |
| 2216 | auto Eval = [&](const IntegerLiteral *IL, unsigned ElemIndex) { |
| 2217 | if (TargetT == PT_Float) { |
| 2218 | if (!this->emitConst(IL->getValue(), classifyPrim(IL), Init)) |
| 2219 | return false; |
| 2220 | const auto *Sem = &Ctx.getFloatSemantics(T: CAT->getElementType()); |
| 2221 | if (!this->emitCastIntegralFloating(classifyPrim(IL), Sem, |
| 2222 | getFPOptions(E), E)) |
| 2223 | return false; |
| 2224 | } else { |
| 2225 | if (!this->emitConst(IL->getValue(), TargetT, Init)) |
| 2226 | return false; |
| 2227 | } |
| 2228 | return this->emitInitElem(TargetT, ElemIndex, IL); |
| 2229 | }; |
| 2230 | if (!EmbedS->doForEachDataElement(Eval, ElementIndex)) |
| 2231 | return false; |
| 2232 | } else if (isa<NoInitExpr>(Val: Init)) { |
| 2233 | // If this is a child of a DesignatedInitUpdateExpr, skip elements which |
| 2234 | // aren't supposed to be modified. |
| 2235 | ++ElementIndex; |
| 2236 | } else { |
| 2237 | if (!this->visitArrayElemInit(ElemIndex: ElementIndex, Init, InitT)) |
| 2238 | return false; |
| 2239 | ++ElementIndex; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | // Expand the filler expression. |
| 2244 | // FIXME: This should go away. |
| 2245 | if (ArrayFiller && !isa<NoInitExpr>(Val: ArrayFiller)) { |
| 2246 | for (; ElementIndex != NumElems; ++ElementIndex) { |
| 2247 | if (!this->visitArrayElemInit(ElemIndex: ElementIndex, Init: ArrayFiller, InitT)) |
| 2248 | return false; |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | return this->emitFinishInit(E); |
| 2253 | } |
| 2254 | |
| 2255 | if (const auto *ComplexTy = QT->getAs<ComplexType>()) { |
| 2256 | unsigned NumInits = Inits.size(); |
| 2257 | |
| 2258 | if (NumInits == 1) |
| 2259 | return this->delegate(E: Inits[0]); |
| 2260 | |
| 2261 | QualType ElemQT = ComplexTy->getElementType(); |
| 2262 | PrimType ElemT = classifyPrim(ElemQT); |
| 2263 | if (NumInits == 0) { |
| 2264 | // Zero-initialize both elements. |
| 2265 | for (unsigned I = 0; I < 2; ++I) { |
| 2266 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2267 | return false; |
| 2268 | if (!this->emitInitElem(ElemT, I, E)) |
| 2269 | return false; |
| 2270 | } |
| 2271 | } else if (NumInits == 2) { |
| 2272 | unsigned InitIndex = 0; |
| 2273 | for (const Expr *Init : Inits) { |
| 2274 | if (!this->visit(E: Init)) |
| 2275 | return false; |
| 2276 | |
| 2277 | if (!this->emitInitElem(ElemT, InitIndex, E)) |
| 2278 | return false; |
| 2279 | ++InitIndex; |
| 2280 | } |
| 2281 | } |
| 2282 | return true; |
| 2283 | } |
| 2284 | |
| 2285 | if (const auto *VecT = QT->getAs<VectorType>()) { |
| 2286 | unsigned NumVecElements = VecT->getNumElements(); |
| 2287 | assert(NumVecElements >= Inits.size()); |
| 2288 | |
| 2289 | QualType ElemQT = VecT->getElementType(); |
| 2290 | PrimType ElemT = classifyPrim(ElemQT); |
| 2291 | |
| 2292 | // All initializer elements. |
| 2293 | unsigned InitIndex = 0; |
| 2294 | for (const Expr *Init : Inits) { |
| 2295 | if (!this->visit(E: Init)) |
| 2296 | return false; |
| 2297 | |
| 2298 | // If the initializer is of vector type itself, we have to deconstruct |
| 2299 | // that and initialize all the target fields from the initializer fields. |
| 2300 | if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) { |
| 2301 | if (!this->emitCopyArray(ElemT, 0, InitIndex, |
| 2302 | InitVecT->getNumElements(), E)) |
| 2303 | return false; |
| 2304 | InitIndex += InitVecT->getNumElements(); |
| 2305 | } else { |
| 2306 | if (!this->emitInitElem(ElemT, InitIndex, E)) |
| 2307 | return false; |
| 2308 | ++InitIndex; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | assert(InitIndex <= NumVecElements); |
| 2313 | |
| 2314 | // Fill the rest with zeroes. |
| 2315 | for (; InitIndex != NumVecElements; ++InitIndex) { |
| 2316 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2317 | return false; |
| 2318 | if (!this->emitInitElem(ElemT, InitIndex, E)) |
| 2319 | return false; |
| 2320 | } |
| 2321 | return true; |
| 2322 | } |
| 2323 | |
| 2324 | if (const auto *MT = QT->getAs<ConstantMatrixType>()) { |
| 2325 | unsigned NumElems = MT->getNumElementsFlattened(); |
| 2326 | assert(Inits.size() == NumElems); |
| 2327 | |
| 2328 | QualType ElemQT = MT->getElementType(); |
| 2329 | PrimType ElemT = classifyPrim(ElemQT); |
| 2330 | |
| 2331 | // Matrix initializer list elements are in row-major order, which matches |
| 2332 | // the matrix APValue convention and therefore no index remapping is |
| 2333 | // required. |
| 2334 | for (unsigned I = 0; I != NumElems; ++I) { |
| 2335 | if (!this->visit(E: Inits[I])) |
| 2336 | return false; |
| 2337 | if (!this->emitInitElem(ElemT, I, E)) |
| 2338 | return false; |
| 2339 | } |
| 2340 | return true; |
| 2341 | } |
| 2342 | |
| 2343 | return false; |
| 2344 | } |
| 2345 | |
| 2346 | /// Pointer to the array(not the element!) must be on the stack when calling |
| 2347 | /// this. |
| 2348 | template <class Emitter> |
| 2349 | bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init, |
| 2350 | OptPrimType InitT) { |
| 2351 | if (InitT) { |
| 2352 | // Visit the primitive element like normal. |
| 2353 | if (!this->visit(E: Init)) |
| 2354 | return false; |
| 2355 | return this->emitInitElem(*InitT, ElemIndex, Init); |
| 2356 | } |
| 2357 | |
| 2358 | InitLinkScope<Emitter> ILS(this, InitLink::Elem(Index: ElemIndex)); |
| 2359 | // Advance the pointer currently on the stack to the given |
| 2360 | // dimension. |
| 2361 | if (!this->emitConstUint32(ElemIndex, Init)) |
| 2362 | return false; |
| 2363 | if (!this->emitArrayElemPtrUint32(Init)) |
| 2364 | return false; |
| 2365 | return this->visitInitializerPop(E: Init); |
| 2366 | } |
| 2367 | |
| 2368 | template <class Emitter> |
| 2369 | bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args, |
| 2370 | const FunctionDecl *FuncDecl, |
| 2371 | bool Activate, bool IsOperatorCall) { |
| 2372 | assert(VarScope->getKind() == ScopeKind::Call); |
| 2373 | llvm::BitVector NonNullArgs; |
| 2374 | if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>()) |
| 2375 | NonNullArgs = collectNonNullArgs(F: FuncDecl, Args); |
| 2376 | |
| 2377 | bool ExplicitMemberFn = false; |
| 2378 | if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl)) |
| 2379 | ExplicitMemberFn = MD->isExplicitObjectMemberFunction(); |
| 2380 | |
| 2381 | unsigned ArgIndex = 0; |
| 2382 | for (const Expr *Arg : Args) { |
| 2383 | if (canClassify(Arg)) { |
| 2384 | if (!this->visit(E: Arg)) |
| 2385 | return false; |
| 2386 | } else { |
| 2387 | |
| 2388 | DeclTy Source = Arg; |
| 2389 | if (FuncDecl) { |
| 2390 | // Try to use the parameter declaration instead of the argument |
| 2391 | // expression as a source. |
| 2392 | unsigned DeclIndex = ArgIndex - IsOperatorCall + ExplicitMemberFn; |
| 2393 | if (DeclIndex < FuncDecl->getNumParams()) |
| 2394 | Source = FuncDecl->getParamDecl(i: ArgIndex - IsOperatorCall + |
| 2395 | ExplicitMemberFn); |
| 2396 | } |
| 2397 | |
| 2398 | UnsignedOrNone LocalIndex = |
| 2399 | allocateLocal(Decl: std::move(Source), Ty: Arg->getType(), ScopeKind::Call); |
| 2400 | if (!LocalIndex) |
| 2401 | return false; |
| 2402 | |
| 2403 | if (!this->emitGetPtrLocal(*LocalIndex, Arg)) |
| 2404 | return false; |
| 2405 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex)); |
| 2406 | if (!this->visitInitializer(E: Arg)) |
| 2407 | return false; |
| 2408 | } |
| 2409 | |
| 2410 | if (ArgIndex == 1 && Activate) { |
| 2411 | if (!this->emitActivate(Arg)) |
| 2412 | return false; |
| 2413 | } |
| 2414 | |
| 2415 | if (!NonNullArgs.empty() && NonNullArgs[ArgIndex]) { |
| 2416 | PrimType ArgT = classify(Arg).value_or(PT_Ptr); |
| 2417 | if (ArgT == PT_Ptr) { |
| 2418 | if (!this->emitCheckNonNullArg(ArgT, Arg)) |
| 2419 | return false; |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | ++ArgIndex; |
| 2424 | } |
| 2425 | |
| 2426 | return true; |
| 2427 | } |
| 2428 | |
| 2429 | template <class Emitter> |
| 2430 | bool Compiler<Emitter>::VisitInitListExpr(const InitListExpr *E) { |
| 2431 | return this->visitInitList(Inits: E->inits(), ArrayFiller: E->getArrayFiller(), E); |
| 2432 | } |
| 2433 | |
| 2434 | template <class Emitter> |
| 2435 | bool Compiler<Emitter>::VisitCXXParenListInitExpr( |
| 2436 | const CXXParenListInitExpr *E) { |
| 2437 | return this->visitInitList(Inits: E->getInitExprs(), ArrayFiller: E->getArrayFiller(), E); |
| 2438 | } |
| 2439 | |
| 2440 | template <class Emitter> |
| 2441 | bool Compiler<Emitter>::VisitSubstNonTypeTemplateParmExpr( |
| 2442 | const SubstNonTypeTemplateParmExpr *E) { |
| 2443 | return this->delegate(E: E->getReplacement()); |
| 2444 | } |
| 2445 | |
| 2446 | template <class Emitter> |
| 2447 | bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) { |
| 2448 | if (!E->hasAPValueResult()) |
| 2449 | return this->delegate(E: E->getSubExpr()); |
| 2450 | |
| 2451 | if (OptPrimType T = classify(E)) { |
| 2452 | // Try to emit the APValue directly, without visiting the subexpr. |
| 2453 | // This will only fail if we can't emit the APValue, so won't emit any |
| 2454 | // diagnostics or any double values. |
| 2455 | if (DiscardResult) |
| 2456 | return true; |
| 2457 | return this->visitAPValue(Val: E->getAPValueResult(), ValType: *T, Info: E); |
| 2458 | } |
| 2459 | |
| 2460 | // Fall back to the subexpr for non-primitive APValues. |
| 2461 | return this->delegate(E: E->getSubExpr()); |
| 2462 | } |
| 2463 | |
| 2464 | template <class Emitter> |
| 2465 | bool Compiler<Emitter>::VisitEmbedExpr(const EmbedExpr *E) { |
| 2466 | auto It = E->begin(); |
| 2467 | return this->visit(E: *It); |
| 2468 | } |
| 2469 | |
| 2470 | static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx, |
| 2471 | UnaryExprOrTypeTrait Kind) { |
| 2472 | bool AlignOfReturnsPreferred = |
| 2473 | ASTCtx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7); |
| 2474 | |
| 2475 | // C++ [expr.alignof]p3: |
| 2476 | // When alignof is applied to a reference type, the result is the |
| 2477 | // alignment of the referenced type. |
| 2478 | if (const auto *Ref = T->getAs<ReferenceType>()) |
| 2479 | T = Ref->getPointeeType(); |
| 2480 | |
| 2481 | if (T.getQualifiers().hasUnaligned()) |
| 2482 | return CharUnits::One(); |
| 2483 | |
| 2484 | // __alignof is defined to return the preferred alignment. |
| 2485 | // Before 8, clang returned the preferred alignment for alignof and |
| 2486 | // _Alignof as well. |
| 2487 | if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
| 2488 | return ASTCtx.toCharUnitsFromBits(BitSize: ASTCtx.getPreferredTypeAlign(T)); |
| 2489 | |
| 2490 | return ASTCtx.getTypeAlignInChars(T); |
| 2491 | } |
| 2492 | |
| 2493 | template <class Emitter> |
| 2494 | bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr( |
| 2495 | const UnaryExprOrTypeTraitExpr *E) { |
| 2496 | |
| 2497 | UnaryExprOrTypeTrait Kind = E->getKind(); |
| 2498 | const ASTContext &ASTCtx = Ctx.getASTContext(); |
| 2499 | |
| 2500 | if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) { |
| 2501 | QualType ArgType = E->getTypeOfArgument(); |
| 2502 | |
| 2503 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2504 | // the result is the size of the referenced type." |
| 2505 | if (const auto *Ref = ArgType->getAs<ReferenceType>()) |
| 2506 | ArgType = Ref->getPointeeType(); |
| 2507 | |
| 2508 | CharUnits Size; |
| 2509 | if (ArgType->isVoidType() || ArgType->isFunctionType()) |
| 2510 | Size = CharUnits::One(); |
| 2511 | else { |
| 2512 | if (ArgType->isDependentType() || !ArgType->isConstantSizeType()) |
| 2513 | return this->emitInvalid(E); |
| 2514 | |
| 2515 | if (Kind == UETT_SizeOf) |
| 2516 | Size = ASTCtx.getTypeSizeInChars(T: ArgType); |
| 2517 | else |
| 2518 | Size = ASTCtx.getTypeInfoDataSizeInChars(T: ArgType).Width; |
| 2519 | } |
| 2520 | |
| 2521 | if (DiscardResult) |
| 2522 | return true; |
| 2523 | |
| 2524 | return this->emitConst(Size.getQuantity(), E); |
| 2525 | } |
| 2526 | |
| 2527 | if (Kind == UETT_CountOf) { |
| 2528 | QualType Ty = E->getTypeOfArgument(); |
| 2529 | assert(Ty->isArrayType()); |
| 2530 | |
| 2531 | // We don't need to worry about array element qualifiers, so getting the |
| 2532 | // unsafe array type is fine. |
| 2533 | if (const auto *CAT = |
| 2534 | dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) { |
| 2535 | if (DiscardResult) |
| 2536 | return true; |
| 2537 | return this->emitConst(CAT->getSize(), E); |
| 2538 | } |
| 2539 | |
| 2540 | assert(!Ty->isConstantSizeType()); |
| 2541 | |
| 2542 | // If it's a variable-length array type, we need to check whether it is a |
| 2543 | // multidimensional array. If so, we need to check the size expression of |
| 2544 | // the VLA to see if it's a constant size. If so, we can return that value. |
| 2545 | const auto *VAT = ASTCtx.getAsVariableArrayType(T: Ty); |
| 2546 | assert(VAT); |
| 2547 | if (VAT->getElementType()->isArrayType()) { |
| 2548 | std::optional<APSInt> Res = |
| 2549 | VAT->getSizeExpr() |
| 2550 | ? VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: ASTCtx) |
| 2551 | : std::nullopt; |
| 2552 | if (Res) { |
| 2553 | if (DiscardResult) |
| 2554 | return true; |
| 2555 | return this->emitConst(*Res, E); |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) { |
| 2561 | CharUnits Size; |
| 2562 | |
| 2563 | if (E->isArgumentType()) { |
| 2564 | QualType ArgType = E->getTypeOfArgument(); |
| 2565 | |
| 2566 | Size = AlignOfType(T: ArgType, ASTCtx, Kind); |
| 2567 | } else { |
| 2568 | // Argument is an expression, not a type. |
| 2569 | const Expr *Arg = E->getArgumentExpr()->IgnoreParens(); |
| 2570 | |
| 2571 | if (Arg->getType()->isDependentType()) |
| 2572 | return false; |
| 2573 | |
| 2574 | // The kinds of expressions that we have special-case logic here for |
| 2575 | // should be kept up to date with the special checks for those |
| 2576 | // expressions in Sema. |
| 2577 | |
| 2578 | // alignof decl is always accepted, even if it doesn't make sense: we |
| 2579 | // default to 1 in those cases. |
| 2580 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Arg)) |
| 2581 | Size = ASTCtx.getDeclAlign(D: DRE->getDecl(), |
| 2582 | /*RefAsPointee*/ ForAlignof: true); |
| 2583 | else if (const auto *ME = dyn_cast<MemberExpr>(Val: Arg)) |
| 2584 | Size = ASTCtx.getDeclAlign(D: ME->getMemberDecl(), |
| 2585 | /*RefAsPointee*/ ForAlignof: true); |
| 2586 | else |
| 2587 | Size = AlignOfType(T: Arg->getType(), ASTCtx, Kind); |
| 2588 | } |
| 2589 | |
| 2590 | if (DiscardResult) |
| 2591 | return true; |
| 2592 | |
| 2593 | return this->emitConst(Size.getQuantity(), E); |
| 2594 | } |
| 2595 | |
| 2596 | if (Kind == UETT_VectorElements) { |
| 2597 | if (E->containsErrors()) |
| 2598 | return false; |
| 2599 | |
| 2600 | if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) |
| 2601 | return this->emitConst(VT->getNumElements(), E); |
| 2602 | assert(E->getTypeOfArgument()->isSizelessVectorType()); |
| 2603 | return this->emitSizelessVectorElementSize(E); |
| 2604 | } |
| 2605 | |
| 2606 | if (Kind == UETT_VecStep) { |
| 2607 | if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) { |
| 2608 | unsigned N = VT->getNumElements(); |
| 2609 | |
| 2610 | // The vec_step built-in functions that take a 3-component |
| 2611 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 2612 | if (N == 3) |
| 2613 | N = 4; |
| 2614 | |
| 2615 | return this->emitConst(N, E); |
| 2616 | } |
| 2617 | return this->emitConst(1, E); |
| 2618 | } |
| 2619 | |
| 2620 | if (Kind == UETT_OpenMPRequiredSimdAlign) { |
| 2621 | if (E->containsErrors()) |
| 2622 | return false; |
| 2623 | assert(E->isArgumentType()); |
| 2624 | unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()); |
| 2625 | |
| 2626 | return this->emitConst(ASTCtx.toCharUnitsFromBits(BitSize: Bits).getQuantity(), E); |
| 2627 | } |
| 2628 | |
| 2629 | if (Kind == UETT_PtrAuthTypeDiscriminator) { |
| 2630 | if (E->getArgumentType()->isDependentType()) |
| 2631 | return this->emitInvalid(E); |
| 2632 | |
| 2633 | return this->emitConst( |
| 2634 | const_cast<ASTContext &>(ASTCtx).getPointerAuthTypeDiscriminator( |
| 2635 | T: E->getArgumentType()), |
| 2636 | E); |
| 2637 | } |
| 2638 | |
| 2639 | return false; |
| 2640 | } |
| 2641 | |
| 2642 | template <class Emitter> |
| 2643 | bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) { |
| 2644 | // 'Base.Member' |
| 2645 | const Expr *Base = E->getBase(); |
| 2646 | const ValueDecl *Member = E->getMemberDecl(); |
| 2647 | |
| 2648 | if (DiscardResult) |
| 2649 | return this->discard(E: Base); |
| 2650 | |
| 2651 | if (const auto *VD = dyn_cast<VarDecl>(Val: Member)) { |
| 2652 | // I am almost confident in saying that a var decl must be static |
| 2653 | // and therefore registered as a global variable. |
| 2654 | if (auto GlobalIndex = P.getGlobal(VD)) { |
| 2655 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 2656 | return false; |
| 2657 | if (Member->getType()->isReferenceType()) |
| 2658 | return this->emitLoadPopPtr(E); |
| 2659 | return true; |
| 2660 | } |
| 2661 | return false; |
| 2662 | } |
| 2663 | |
| 2664 | if (!isa<FieldDecl>(Val: Member)) { |
| 2665 | // A non-static member function access only makes sense as part of the |
| 2666 | // enclosing call here. Don't try to evaluate it in isolation. |
| 2667 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Member); |
| 2668 | MD && !MD->isStatic()) { |
| 2669 | return false; |
| 2670 | } |
| 2671 | |
| 2672 | if (!this->discard(E: Base) && !this->emitSideEffect(E)) |
| 2673 | return false; |
| 2674 | |
| 2675 | return this->visitDeclRef(D: Member, E); |
| 2676 | } |
| 2677 | |
| 2678 | if (!this->visit(E: Base)) |
| 2679 | return false; |
| 2680 | |
| 2681 | // Base above gives us a pointer on the stack. |
| 2682 | const auto *FD = cast<FieldDecl>(Val: Member); |
| 2683 | const RecordDecl *RD = FD->getParent(); |
| 2684 | const Record *R = getRecord(RD); |
| 2685 | if (!R) |
| 2686 | return false; |
| 2687 | const Record::Field *F = R->getField(FD); |
| 2688 | |
| 2689 | // MemberExprs are almost always lvalues, in which case we don't need to |
| 2690 | // do the load. But sometimes they aren't. |
| 2691 | const auto maybeLoadValue = [&]() -> bool { |
| 2692 | if (E->isGLValue()) |
| 2693 | return true; |
| 2694 | if (OptPrimType T = classify(E)) |
| 2695 | return this->emitLoadPop(*T, E); |
| 2696 | return false; |
| 2697 | }; |
| 2698 | |
| 2699 | // Leave a pointer to the field on the stack. |
| 2700 | if (F->Decl->getType()->isReferenceType()) |
| 2701 | return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue(); |
| 2702 | return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue(); |
| 2703 | } |
| 2704 | |
| 2705 | template <class Emitter> |
| 2706 | bool Compiler<Emitter>::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
| 2707 | assert(!DiscardResult); |
| 2708 | // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated |
| 2709 | // stand-alone, e.g. via EvaluateAsInt(). |
| 2710 | if (!ArrayIndex) |
| 2711 | return false; |
| 2712 | return this->emitConst(*ArrayIndex, E); |
| 2713 | } |
| 2714 | |
| 2715 | template <class Emitter> |
| 2716 | bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
| 2717 | assert(Initializing); |
| 2718 | assert(!DiscardResult); |
| 2719 | |
| 2720 | const Expr *Common = E->getCommonExpr(); |
| 2721 | const Expr *SubExpr = E->getSubExpr(); |
| 2722 | OptPrimType SubExprT = classify(SubExpr); |
| 2723 | size_t Size = E->getArraySize().getZExtValue(); |
| 2724 | |
| 2725 | if (SubExprT) { |
| 2726 | // Unwrap the OpaqueValueExpr so we don't cache something we won't reuse. |
| 2727 | Common = cast<OpaqueValueExpr>(Val: Common)->getSourceExpr(); |
| 2728 | |
| 2729 | if (!this->visit(E: Common)) |
| 2730 | return false; |
| 2731 | return this->emitCopyArray(*SubExprT, 0, 0, Size, E); |
| 2732 | } |
| 2733 | |
| 2734 | // We visit the common opaque expression here once so we have its value |
| 2735 | // cached. |
| 2736 | if (!this->discard(E: Common)) |
| 2737 | return false; |
| 2738 | |
| 2739 | // TODO: This compiles to quite a lot of bytecode if the array is larger. |
| 2740 | // Investigate compiling this to a loop. |
| 2741 | |
| 2742 | // So, every iteration, we execute an assignment here |
| 2743 | // where the LHS is on the stack (the target array) |
| 2744 | // and the RHS is our SubExpr. |
| 2745 | for (size_t I = 0; I != Size; ++I) { |
| 2746 | ArrayIndexScope<Emitter> IndexScope(this, I); |
| 2747 | LocalScope<Emitter> BS(this, ScopeKind::FullExpression); |
| 2748 | |
| 2749 | if (!this->visitArrayElemInit(ElemIndex: I, Init: SubExpr, InitT: SubExprT)) |
| 2750 | return false; |
| 2751 | if (!BS.destroyLocals()) |
| 2752 | return false; |
| 2753 | } |
| 2754 | return true; |
| 2755 | } |
| 2756 | |
| 2757 | template <class Emitter> |
| 2758 | bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
| 2759 | const Expr *SourceExpr = E->getSourceExpr(); |
| 2760 | if (!SourceExpr) |
| 2761 | return false; |
| 2762 | |
| 2763 | if (Initializing) { |
| 2764 | assert(!DiscardResult); |
| 2765 | return this->visitInitializer(E: SourceExpr); |
| 2766 | } |
| 2767 | |
| 2768 | PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr); |
| 2769 | if (auto It = OpaqueExprs.find(Val: E); It != OpaqueExprs.end()) { |
| 2770 | if (DiscardResult) |
| 2771 | return true; |
| 2772 | return this->emitGetLocal(SubExprT, It->second, E); |
| 2773 | } |
| 2774 | |
| 2775 | if (!this->visit(E: SourceExpr)) |
| 2776 | return false; |
| 2777 | |
| 2778 | // At this point we either have the evaluated source expression or a pointer |
| 2779 | // to an object on the stack. We want to create a local variable that stores |
| 2780 | // this value. |
| 2781 | unsigned LocalIndex = allocateLocalPrimitive(Decl: E, Ty: SubExprT, /*IsConst=*/true); |
| 2782 | if (!this->emitSetLocal(SubExprT, LocalIndex, E)) |
| 2783 | return false; |
| 2784 | |
| 2785 | // This is cleaned up when the local variable is destroyed. |
| 2786 | OpaqueExprs.insert(KV: {E, LocalIndex}); |
| 2787 | |
| 2788 | // Here the local variable is created but the value is removed from the stack, |
| 2789 | // so we put it back if the caller needs it. |
| 2790 | if (!DiscardResult) |
| 2791 | return this->emitGetLocal(SubExprT, LocalIndex, E); |
| 2792 | return true; |
| 2793 | } |
| 2794 | |
| 2795 | template <class Emitter> |
| 2796 | bool Compiler<Emitter>::VisitAbstractConditionalOperator( |
| 2797 | const AbstractConditionalOperator *E) { |
| 2798 | const Expr *Condition = E->getCond(); |
| 2799 | const Expr *TrueExpr = E->getTrueExpr(); |
| 2800 | const Expr *FalseExpr = E->getFalseExpr(); |
| 2801 | |
| 2802 | if (std::optional<bool> BoolValue = getBoolValue(E: Condition)) { |
| 2803 | if (*BoolValue) |
| 2804 | return this->delegate(E: TrueExpr); |
| 2805 | return this->delegate(E: FalseExpr); |
| 2806 | } |
| 2807 | |
| 2808 | bool IsBcpCall = false; |
| 2809 | if (const auto *CE = dyn_cast<CallExpr>(Val: Condition->IgnoreParenCasts()); |
| 2810 | CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) { |
| 2811 | IsBcpCall = true; |
| 2812 | } |
| 2813 | |
| 2814 | LabelTy LabelEnd = this->getLabel(); // Label after the operator. |
| 2815 | LabelTy LabelFalse = this->getLabel(); // Label for the false expr. |
| 2816 | |
| 2817 | if (IsBcpCall) { |
| 2818 | if (!this->emitPushIgnoreDiags(E)) |
| 2819 | return false; |
| 2820 | } |
| 2821 | |
| 2822 | if (!this->visitBool(E: Condition)) { |
| 2823 | // If the condition failed and we're checking for undefined behavior |
| 2824 | // (which only happens with EvalEmitter) check the TrueExpr and FalseExpr |
| 2825 | // as well. |
| 2826 | if (this->checkingForUndefinedBehavior()) { |
| 2827 | if (!this->discard(E: TrueExpr)) |
| 2828 | return false; |
| 2829 | if (!this->discard(E: FalseExpr)) |
| 2830 | return false; |
| 2831 | } |
| 2832 | return false; |
| 2833 | } |
| 2834 | |
| 2835 | // Force-init the scope, which creates a InitScope op. This is necessary so |
| 2836 | // the scope is not only initialized in one arm of the conditional operator. |
| 2837 | this->VarScope->forceInit(); |
| 2838 | // The TrueExpr and FalseExpr of a conditional operator do _not_ create a |
| 2839 | // scope, which means the local variables created within them unconditionally |
| 2840 | // always exist. However, we need to later differentiate which branch was |
| 2841 | // taken and only destroy the varibles of the active branch. This is what the |
| 2842 | // "enabled" flags on local variables are used for. |
| 2843 | llvm::SaveAndRestore LAAA(this->VarScope->LocalsAlwaysEnabled, |
| 2844 | /*NewValue=*/false); |
| 2845 | |
| 2846 | if (!this->jumpFalse(LabelFalse, E)) |
| 2847 | return false; |
| 2848 | if (!this->delegate(E: TrueExpr)) |
| 2849 | return false; |
| 2850 | |
| 2851 | if (!this->jump(LabelEnd, E)) |
| 2852 | return false; |
| 2853 | this->emitLabel(LabelFalse); |
| 2854 | if (!this->delegate(E: FalseExpr)) |
| 2855 | return false; |
| 2856 | |
| 2857 | this->fallthrough(LabelEnd); |
| 2858 | this->emitLabel(LabelEnd); |
| 2859 | |
| 2860 | if (IsBcpCall) |
| 2861 | return this->emitPopIgnoreDiags(E); |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
| 2865 | template <class Emitter> |
| 2866 | bool Compiler<Emitter>::VisitStringLiteral(const StringLiteral *E) { |
| 2867 | if (DiscardResult) |
| 2868 | return true; |
| 2869 | |
| 2870 | if (!Initializing) { |
| 2871 | unsigned StringIndex = P.createGlobalString(S: E); |
| 2872 | return this->emitGetPtrGlobal(StringIndex, E); |
| 2873 | } |
| 2874 | |
| 2875 | // We are initializing an array on the stack. |
| 2876 | const ConstantArrayType *CAT = |
| 2877 | Ctx.getASTContext().getAsConstantArrayType(T: E->getType()); |
| 2878 | assert(CAT && "a string literal that's not a constant array?" ); |
| 2879 | |
| 2880 | // If the initializer string is too long, a diagnostic has already been |
| 2881 | // emitted. Read only the array length from the string literal. |
| 2882 | unsigned ArraySize = CAT->getZExtSize(); |
| 2883 | unsigned N = std::min(a: ArraySize, b: E->getLength()); |
| 2884 | unsigned CharWidth = E->getCharByteWidth(); |
| 2885 | |
| 2886 | for (unsigned I = 0; I != N; ++I) { |
| 2887 | uint32_t CodeUnit = E->getCodeUnit(i: I); |
| 2888 | |
| 2889 | if (CharWidth == 1) { |
| 2890 | this->emitConstSint8(CodeUnit, E); |
| 2891 | this->emitInitElemSint8(I, E); |
| 2892 | } else if (CharWidth == 2) { |
| 2893 | this->emitConstUint16(CodeUnit, E); |
| 2894 | this->emitInitElemUint16(I, E); |
| 2895 | } else if (CharWidth == 4) { |
| 2896 | this->emitConstUint32(CodeUnit, E); |
| 2897 | this->emitInitElemUint32(I, E); |
| 2898 | } else { |
| 2899 | llvm_unreachable("unsupported character width" ); |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | // Fill up the rest of the char array with NUL bytes. |
| 2904 | for (unsigned I = N; I != ArraySize; ++I) { |
| 2905 | if (CharWidth == 1) { |
| 2906 | this->emitConstSint8(0, E); |
| 2907 | this->emitInitElemSint8(I, E); |
| 2908 | } else if (CharWidth == 2) { |
| 2909 | this->emitConstUint16(0, E); |
| 2910 | this->emitInitElemUint16(I, E); |
| 2911 | } else if (CharWidth == 4) { |
| 2912 | this->emitConstUint32(0, E); |
| 2913 | this->emitInitElemUint32(I, E); |
| 2914 | } else { |
| 2915 | llvm_unreachable("unsupported character width" ); |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | return true; |
| 2920 | } |
| 2921 | |
| 2922 | template <class Emitter> |
| 2923 | bool Compiler<Emitter>::VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 2924 | if (DiscardResult) |
| 2925 | return true; |
| 2926 | return this->emitDummyPtr(D: E, E); |
| 2927 | } |
| 2928 | |
| 2929 | template <class Emitter> |
| 2930 | bool Compiler<Emitter>::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
| 2931 | auto &A = Ctx.getASTContext(); |
| 2932 | std::string Str; |
| 2933 | A.getObjCEncodingForType(T: E->getEncodedType(), S&: Str); |
| 2934 | StringLiteral *SL = |
| 2935 | StringLiteral::Create(Ctx: A, Str, Kind: StringLiteralKind::Ordinary, |
| 2936 | /*Pascal=*/false, Ty: E->getType(), Locs: E->getAtLoc()); |
| 2937 | return this->delegate(E: SL); |
| 2938 | } |
| 2939 | |
| 2940 | template <class Emitter> |
| 2941 | bool Compiler<Emitter>::VisitSYCLUniqueStableNameExpr( |
| 2942 | const SYCLUniqueStableNameExpr *E) { |
| 2943 | if (DiscardResult) |
| 2944 | return true; |
| 2945 | |
| 2946 | assert(!Initializing); |
| 2947 | |
| 2948 | auto &A = Ctx.getASTContext(); |
| 2949 | std::string ResultStr = E->ComputeName(Context&: A); |
| 2950 | |
| 2951 | QualType CharTy = A.CharTy.withConst(); |
| 2952 | APInt Size(A.getTypeSize(T: A.getSizeType()), ResultStr.size() + 1); |
| 2953 | QualType ArrayTy = A.getConstantArrayType(EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, |
| 2954 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
| 2955 | |
| 2956 | StringLiteral *SL = |
| 2957 | StringLiteral::Create(Ctx: A, Str: ResultStr, Kind: StringLiteralKind::Ordinary, |
| 2958 | /*Pascal=*/false, Ty: ArrayTy, Locs: E->getLocation()); |
| 2959 | |
| 2960 | unsigned StringIndex = P.createGlobalString(S: SL); |
| 2961 | return this->emitGetPtrGlobal(StringIndex, E); |
| 2962 | } |
| 2963 | |
| 2964 | template <class Emitter> |
| 2965 | bool Compiler<Emitter>::VisitCharacterLiteral(const CharacterLiteral *E) { |
| 2966 | if (DiscardResult) |
| 2967 | return true; |
| 2968 | return this->emitConst(E->getValue(), E); |
| 2969 | } |
| 2970 | |
| 2971 | template <class Emitter> |
| 2972 | bool Compiler<Emitter>::VisitFloatCompoundAssignOperator( |
| 2973 | const CompoundAssignOperator *E) { |
| 2974 | |
| 2975 | const Expr *LHS = E->getLHS(); |
| 2976 | const Expr *RHS = E->getRHS(); |
| 2977 | QualType LHSType = LHS->getType(); |
| 2978 | QualType LHSComputationType = E->getComputationLHSType(); |
| 2979 | QualType ResultType = E->getComputationResultType(); |
| 2980 | OptPrimType LT = classify(LHSComputationType); |
| 2981 | OptPrimType RT = classify(ResultType); |
| 2982 | |
| 2983 | assert(ResultType->isFloatingType()); |
| 2984 | |
| 2985 | if (!LT || !RT) |
| 2986 | return false; |
| 2987 | |
| 2988 | PrimType LHST = classifyPrim(LHSType); |
| 2989 | |
| 2990 | // C++17 onwards require that we evaluate the RHS first. |
| 2991 | // Compute RHS and save it in a temporary variable so we can |
| 2992 | // load it again later. |
| 2993 | if (!visit(E: RHS)) |
| 2994 | return false; |
| 2995 | |
| 2996 | unsigned TempOffset = this->allocateLocalPrimitive(Decl: E, Ty: *RT, /*IsConst=*/true); |
| 2997 | if (!this->emitSetLocal(*RT, TempOffset, E)) |
| 2998 | return false; |
| 2999 | |
| 3000 | // First, visit LHS. |
| 3001 | if (!visit(E: LHS)) |
| 3002 | return false; |
| 3003 | if (!this->emitLoad(LHST, E)) |
| 3004 | return false; |
| 3005 | |
| 3006 | // If necessary, convert LHS to its computation type. |
| 3007 | if (!this->emitPrimCast(FromT: LHST, ToT: classifyPrim(LHSComputationType), |
| 3008 | ToQT: LHSComputationType, E)) |
| 3009 | return false; |
| 3010 | |
| 3011 | // Now load RHS. |
| 3012 | if (!this->emitGetLocal(*RT, TempOffset, E)) |
| 3013 | return false; |
| 3014 | |
| 3015 | switch (E->getOpcode()) { |
| 3016 | case BO_AddAssign: |
| 3017 | if (!this->emitAddf(getFPOptions(E), E)) |
| 3018 | return false; |
| 3019 | break; |
| 3020 | case BO_SubAssign: |
| 3021 | if (!this->emitSubf(getFPOptions(E), E)) |
| 3022 | return false; |
| 3023 | break; |
| 3024 | case BO_MulAssign: |
| 3025 | if (!this->emitMulf(getFPOptions(E), E)) |
| 3026 | return false; |
| 3027 | break; |
| 3028 | case BO_DivAssign: |
| 3029 | if (!this->emitDivf(getFPOptions(E), E)) |
| 3030 | return false; |
| 3031 | break; |
| 3032 | default: |
| 3033 | return false; |
| 3034 | } |
| 3035 | |
| 3036 | if (!this->emitPrimCast(FromT: classifyPrim(ResultType), ToT: LHST, ToQT: LHS->getType(), E)) |
| 3037 | return false; |
| 3038 | |
| 3039 | if (DiscardResult) |
| 3040 | return this->emitStorePop(LHST, E); |
| 3041 | return this->emitStore(LHST, E); |
| 3042 | } |
| 3043 | |
| 3044 | template <class Emitter> |
| 3045 | bool Compiler<Emitter>::VisitPointerCompoundAssignOperator( |
| 3046 | const CompoundAssignOperator *E) { |
| 3047 | BinaryOperatorKind Op = E->getOpcode(); |
| 3048 | const Expr *LHS = E->getLHS(); |
| 3049 | const Expr *RHS = E->getRHS(); |
| 3050 | OptPrimType LT = classify(LHS->getType()); |
| 3051 | OptPrimType RT = classify(RHS->getType()); |
| 3052 | |
| 3053 | if (Op != BO_AddAssign && Op != BO_SubAssign) |
| 3054 | return false; |
| 3055 | |
| 3056 | if (!LT || !RT) |
| 3057 | return false; |
| 3058 | |
| 3059 | if (!visit(E: LHS)) |
| 3060 | return false; |
| 3061 | |
| 3062 | if (!this->emitLoad(*LT, LHS)) |
| 3063 | return false; |
| 3064 | |
| 3065 | if (!visit(E: RHS)) |
| 3066 | return false; |
| 3067 | |
| 3068 | if (Op == BO_AddAssign) { |
| 3069 | if (!this->emitAddOffset(*RT, E)) |
| 3070 | return false; |
| 3071 | } else { |
| 3072 | if (!this->emitSubOffset(*RT, E)) |
| 3073 | return false; |
| 3074 | } |
| 3075 | |
| 3076 | if (DiscardResult) |
| 3077 | return this->emitStorePopPtr(E); |
| 3078 | return this->emitStorePtr(E); |
| 3079 | } |
| 3080 | |
| 3081 | template <class Emitter> |
| 3082 | bool Compiler<Emitter>::VisitCompoundAssignOperator( |
| 3083 | const CompoundAssignOperator *E) { |
| 3084 | if (E->getType()->isVectorType()) |
| 3085 | return VisitVectorBinOp(E); |
| 3086 | |
| 3087 | const Expr *LHS = E->getLHS(); |
| 3088 | const Expr *RHS = E->getRHS(); |
| 3089 | OptPrimType LHSComputationT = classify(E->getComputationLHSType()); |
| 3090 | OptPrimType LT = classify(LHS->getType()); |
| 3091 | OptPrimType RT = classify(RHS->getType()); |
| 3092 | OptPrimType ResultT = classify(E->getType()); |
| 3093 | |
| 3094 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 3095 | return this->visit(E: RHS) && this->visit(E: LHS) && this->emitError(E); |
| 3096 | |
| 3097 | if (!LT || !RT || !ResultT || !LHSComputationT) |
| 3098 | return false; |
| 3099 | |
| 3100 | // Handle floating point operations separately here, since they |
| 3101 | // require special care. |
| 3102 | |
| 3103 | if (ResultT == PT_Float || RT == PT_Float) |
| 3104 | return VisitFloatCompoundAssignOperator(E); |
| 3105 | |
| 3106 | if (E->getType()->isPointerType()) |
| 3107 | return VisitPointerCompoundAssignOperator(E); |
| 3108 | |
| 3109 | assert(!E->getType()->isPointerType() && "Handled above" ); |
| 3110 | assert(!E->getType()->isFloatingType() && "Handled above" ); |
| 3111 | |
| 3112 | // C++17 onwards require that we evaluate the RHS first. |
| 3113 | // Compute RHS and save it in a temporary variable so we can |
| 3114 | // load it again later. |
| 3115 | // FIXME: Compound assignments are unsequenced in C, so we might |
| 3116 | // have to figure out how to reject them. |
| 3117 | if (!visit(E: RHS)) |
| 3118 | return false; |
| 3119 | |
| 3120 | unsigned TempOffset = this->allocateLocalPrimitive(Decl: E, Ty: *RT, /*IsConst=*/true); |
| 3121 | |
| 3122 | if (!this->emitSetLocal(*RT, TempOffset, E)) |
| 3123 | return false; |
| 3124 | |
| 3125 | // Get LHS pointer, load its value and cast it to the |
| 3126 | // computation type if necessary. |
| 3127 | if (!visit(E: LHS)) |
| 3128 | return false; |
| 3129 | if (!this->emitLoad(*LT, E)) |
| 3130 | return false; |
| 3131 | if (LT != LHSComputationT && |
| 3132 | !this->emitIntegralCast(FromT: *LT, ToT: *LHSComputationT, ToQT: E->getComputationLHSType(), |
| 3133 | E)) |
| 3134 | return false; |
| 3135 | |
| 3136 | // Get the RHS value on the stack. |
| 3137 | if (!this->emitGetLocal(*RT, TempOffset, E)) |
| 3138 | return false; |
| 3139 | |
| 3140 | // Perform operation. |
| 3141 | switch (E->getOpcode()) { |
| 3142 | case BO_AddAssign: |
| 3143 | if (!this->emitAdd(*LHSComputationT, E)) |
| 3144 | return false; |
| 3145 | break; |
| 3146 | case BO_SubAssign: |
| 3147 | if (!this->emitSub(*LHSComputationT, E)) |
| 3148 | return false; |
| 3149 | break; |
| 3150 | case BO_MulAssign: |
| 3151 | if (!this->emitMul(*LHSComputationT, E)) |
| 3152 | return false; |
| 3153 | break; |
| 3154 | case BO_DivAssign: |
| 3155 | if (!this->emitDiv(*LHSComputationT, E)) |
| 3156 | return false; |
| 3157 | break; |
| 3158 | case BO_RemAssign: |
| 3159 | if (!this->emitRem(*LHSComputationT, E)) |
| 3160 | return false; |
| 3161 | break; |
| 3162 | case BO_ShlAssign: |
| 3163 | if (!this->emitShl(*LHSComputationT, *RT, E)) |
| 3164 | return false; |
| 3165 | break; |
| 3166 | case BO_ShrAssign: |
| 3167 | if (!this->emitShr(*LHSComputationT, *RT, E)) |
| 3168 | return false; |
| 3169 | break; |
| 3170 | case BO_AndAssign: |
| 3171 | if (!this->emitBitAnd(*LHSComputationT, E)) |
| 3172 | return false; |
| 3173 | break; |
| 3174 | case BO_XorAssign: |
| 3175 | if (!this->emitBitXor(*LHSComputationT, E)) |
| 3176 | return false; |
| 3177 | break; |
| 3178 | case BO_OrAssign: |
| 3179 | if (!this->emitBitOr(*LHSComputationT, E)) |
| 3180 | return false; |
| 3181 | break; |
| 3182 | default: |
| 3183 | llvm_unreachable("Unimplemented compound assign operator" ); |
| 3184 | } |
| 3185 | |
| 3186 | // And now cast from LHSComputationT to ResultT. |
| 3187 | if (ResultT != LHSComputationT && |
| 3188 | !this->emitIntegralCast(FromT: *LHSComputationT, ToT: *ResultT, ToQT: E->getType(), E)) |
| 3189 | return false; |
| 3190 | |
| 3191 | // And store the result in LHS. |
| 3192 | if (DiscardResult) { |
| 3193 | if (LHS->refersToBitField()) |
| 3194 | return this->emitStoreBitFieldPop(*ResultT, E); |
| 3195 | return this->emitStorePop(*ResultT, E); |
| 3196 | } |
| 3197 | if (LHS->refersToBitField()) |
| 3198 | return this->emitStoreBitField(*ResultT, E); |
| 3199 | return this->emitStore(*ResultT, E); |
| 3200 | } |
| 3201 | |
| 3202 | template <class Emitter> |
| 3203 | bool Compiler<Emitter>::VisitExprWithCleanups(const ExprWithCleanups *E) { |
| 3204 | LocalScope<Emitter> ES(this, ScopeKind::FullExpression); |
| 3205 | const Expr *SubExpr = E->getSubExpr(); |
| 3206 | |
| 3207 | return this->delegate(E: SubExpr) && ES.destroyLocals(E); |
| 3208 | } |
| 3209 | |
| 3210 | template <class Emitter> |
| 3211 | bool Compiler<Emitter>::VisitMaterializeTemporaryExpr( |
| 3212 | const MaterializeTemporaryExpr *E) { |
| 3213 | if (Initializing) { |
| 3214 | // We already have a value, just initialize that. |
| 3215 | return this->delegate(E: E->getSubExpr()); |
| 3216 | } |
| 3217 | // If we don't end up using the materialized temporary anyway, don't |
| 3218 | // bother creating it. |
| 3219 | if (DiscardResult) |
| 3220 | return this->discard(E: E->getSubExpr()); |
| 3221 | |
| 3222 | SmallVector<const Expr *, 2> CommaLHSs; |
| 3223 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 3224 | const Expr *Inner; |
| 3225 | if (!Ctx.getLangOpts().CPlusPlus11) |
| 3226 | Inner = |
| 3227 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments); |
| 3228 | else |
| 3229 | Inner = E->getSubExpr(); |
| 3230 | |
| 3231 | // If we passed any comma operators, evaluate their LHSs. |
| 3232 | for (const Expr *LHS : CommaLHSs) { |
| 3233 | if (!this->discard(E: LHS)) |
| 3234 | return false; |
| 3235 | } |
| 3236 | |
| 3237 | // FIXME: Find a test case where Adjustments matters. |
| 3238 | |
| 3239 | // When we're extending a global variable *or* the storage duration of |
| 3240 | // the temporary is explicitly static, create a global variable. |
| 3241 | OptPrimType InnerT = classify(Inner); |
| 3242 | const ValueDecl *ExtendingDecl = E->getExtendingDecl(); |
| 3243 | bool IsStatic = E->getStorageDuration() == SD_Static; |
| 3244 | if (IsStatic || |
| 3245 | (ExtendingDecl && Context::shouldBeGloballyIndexed(VD: ExtendingDecl))) { |
| 3246 | UnsignedOrNone GlobalIndex = P.createGlobal(E, ExprType: Inner->getType()); |
| 3247 | if (!GlobalIndex) |
| 3248 | return false; |
| 3249 | |
| 3250 | const LifetimeExtendedTemporaryDecl *TempDecl = |
| 3251 | E->getLifetimeExtendedTemporaryDecl(); |
| 3252 | |
| 3253 | if (InnerT) { |
| 3254 | if (!this->visit(E: Inner)) |
| 3255 | return false; |
| 3256 | |
| 3257 | if (IsStatic) { |
| 3258 | assert(TempDecl); |
| 3259 | if (!this->emitInitGlobalTemp(*InnerT, *GlobalIndex, TempDecl, E)) |
| 3260 | return false; |
| 3261 | } else { |
| 3262 | if (!this->emitInitGlobal(*InnerT, *GlobalIndex, E)) |
| 3263 | return false; |
| 3264 | } |
| 3265 | return this->emitGetPtrGlobal(*GlobalIndex, E); |
| 3266 | } |
| 3267 | |
| 3268 | if (!this->checkLiteralType(E: Inner)) |
| 3269 | return false; |
| 3270 | // Non-primitive values. |
| 3271 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 3272 | return false; |
| 3273 | if (!this->visitInitializer(E: Inner)) |
| 3274 | return false; |
| 3275 | if (IsStatic) { |
| 3276 | assert(TempDecl); |
| 3277 | return this->emitInitGlobalTempComp(TempDecl, E); |
| 3278 | } |
| 3279 | return true; |
| 3280 | } |
| 3281 | |
| 3282 | ScopeKind VarScope = E->getStorageDuration() == SD_FullExpression |
| 3283 | ? ScopeKind::FullExpression |
| 3284 | : ScopeKind::Block; |
| 3285 | |
| 3286 | // For everyhing else, use local variables. |
| 3287 | if (InnerT) { |
| 3288 | bool IsConst = Inner->getType().isConstQualified(); |
| 3289 | bool IsVolatile = Inner->getType().isVolatileQualified(); |
| 3290 | unsigned LocalIndex = |
| 3291 | allocateLocalPrimitive(Decl: E, Ty: *InnerT, IsConst, IsVolatile, SC: VarScope); |
| 3292 | if (!this->VarScope->LocalsAlwaysEnabled && |
| 3293 | !this->emitEnableLocal(LocalIndex, E)) |
| 3294 | return false; |
| 3295 | |
| 3296 | if (!this->visit(E: Inner)) |
| 3297 | return false; |
| 3298 | if (!this->emitSetLocal(*InnerT, LocalIndex, E)) |
| 3299 | return false; |
| 3300 | |
| 3301 | return this->emitGetPtrLocal(LocalIndex, E); |
| 3302 | } |
| 3303 | |
| 3304 | if (!this->checkLiteralType(E: Inner)) |
| 3305 | return false; |
| 3306 | |
| 3307 | if (UnsignedOrNone LocalIndex = |
| 3308 | allocateLocal(Decl: E, Ty: Inner->getType(), VarScope)) { |
| 3309 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex)); |
| 3310 | |
| 3311 | if (!this->VarScope->LocalsAlwaysEnabled && |
| 3312 | !this->emitEnableLocal(*LocalIndex, E)) |
| 3313 | return false; |
| 3314 | |
| 3315 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 3316 | return false; |
| 3317 | return this->visitInitializer(E: Inner); |
| 3318 | } |
| 3319 | return false; |
| 3320 | } |
| 3321 | |
| 3322 | template <class Emitter> |
| 3323 | bool Compiler<Emitter>::VisitCXXBindTemporaryExpr( |
| 3324 | const CXXBindTemporaryExpr *E) { |
| 3325 | const Expr *SubExpr = E->getSubExpr(); |
| 3326 | |
| 3327 | if (Initializing) |
| 3328 | return this->delegate(E: SubExpr); |
| 3329 | |
| 3330 | // Make sure we create a temporary even if we're discarding, since that will |
| 3331 | // make sure we will also call the destructor. |
| 3332 | |
| 3333 | if (!this->visit(E: SubExpr)) |
| 3334 | return false; |
| 3335 | |
| 3336 | if (DiscardResult) |
| 3337 | return this->emitPopPtr(E); |
| 3338 | return true; |
| 3339 | } |
| 3340 | |
| 3341 | template <class Emitter> |
| 3342 | bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 3343 | const Expr *Init = E->getInitializer(); |
| 3344 | if (DiscardResult) |
| 3345 | return this->discard(E: Init); |
| 3346 | |
| 3347 | if (Initializing) { |
| 3348 | // We already have a value, just initialize that. |
| 3349 | return this->visitInitializer(E: Init); |
| 3350 | } |
| 3351 | |
| 3352 | OptPrimType T = classify(E->getType()); |
| 3353 | if (E->isFileScope()) { |
| 3354 | // Avoid creating a variable if this is a primitive RValue anyway. |
| 3355 | if (T && !E->isLValue()) |
| 3356 | return this->delegate(E: Init); |
| 3357 | |
| 3358 | UnsignedOrNone GlobalIndex = P.createGlobal(E, ExprType: E->getType()); |
| 3359 | if (!GlobalIndex) |
| 3360 | return false; |
| 3361 | |
| 3362 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 3363 | return false; |
| 3364 | |
| 3365 | // Since this is a global variable, we might've already seen, |
| 3366 | // don't do it again. |
| 3367 | if (P.isGlobalInitialized(Index: *GlobalIndex)) |
| 3368 | return true; |
| 3369 | |
| 3370 | if (T) { |
| 3371 | if (!this->visit(E: Init)) |
| 3372 | return false; |
| 3373 | return this->emitInitGlobal(*T, *GlobalIndex, E); |
| 3374 | } |
| 3375 | |
| 3376 | return this->visitInitializer(E: Init); |
| 3377 | } |
| 3378 | |
| 3379 | // Otherwise, use a local variable. |
| 3380 | if (T && !E->isLValue()) { |
| 3381 | // For primitive types, we just visit the initializer. |
| 3382 | return this->delegate(E: Init); |
| 3383 | } |
| 3384 | |
| 3385 | unsigned LocalIndex; |
| 3386 | if (T) |
| 3387 | LocalIndex = this->allocateLocalPrimitive(Decl: Init, Ty: *T, /*IsConst=*/false); |
| 3388 | else if (UnsignedOrNone MaybeIndex = this->allocateLocal(Decl: Init)) |
| 3389 | LocalIndex = *MaybeIndex; |
| 3390 | else |
| 3391 | return false; |
| 3392 | |
| 3393 | if (!this->emitGetPtrLocal(LocalIndex, E)) |
| 3394 | return false; |
| 3395 | |
| 3396 | if (T) |
| 3397 | return this->visit(E: Init) && this->emitInit(*T, E); |
| 3398 | return this->visitInitializer(E: Init); |
| 3399 | } |
| 3400 | |
| 3401 | template <class Emitter> |
| 3402 | bool Compiler<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 3403 | if (DiscardResult) |
| 3404 | return true; |
| 3405 | if (E->isStoredAsBoolean()) { |
| 3406 | if (E->getType()->isBooleanType()) |
| 3407 | return this->emitConstBool(E->getBoolValue(), E); |
| 3408 | return this->emitConst(E->getBoolValue(), E); |
| 3409 | } |
| 3410 | PrimType T = classifyPrim(E->getType()); |
| 3411 | return this->visitAPValue(Val: E->getAPValue(), ValType: T, Info: E); |
| 3412 | } |
| 3413 | |
| 3414 | template <class Emitter> |
| 3415 | bool Compiler<Emitter>::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 3416 | if (DiscardResult) |
| 3417 | return true; |
| 3418 | return this->emitConst(E->getValue(), E); |
| 3419 | } |
| 3420 | |
| 3421 | template <class Emitter> |
| 3422 | bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) { |
| 3423 | if (DiscardResult) |
| 3424 | return true; |
| 3425 | |
| 3426 | assert(Initializing); |
| 3427 | const Record *R = P.getOrCreateRecord(RD: E->getLambdaClass()); |
| 3428 | if (!R) |
| 3429 | return false; |
| 3430 | |
| 3431 | auto *CaptureInitIt = E->capture_init_begin(); |
| 3432 | // Initialize all fields (which represent lambda captures) of the |
| 3433 | // record with their initializers. |
| 3434 | for (const Record::Field &F : R->fields()) { |
| 3435 | const Expr *Init = *CaptureInitIt; |
| 3436 | if (!Init || Init->containsErrors()) |
| 3437 | continue; |
| 3438 | ++CaptureInitIt; |
| 3439 | |
| 3440 | if (OptPrimType T = classify(Init)) { |
| 3441 | if (!this->visit(E: Init)) |
| 3442 | return false; |
| 3443 | |
| 3444 | if (!this->emitInitField(*T, F.Offset, E)) |
| 3445 | return false; |
| 3446 | } else { |
| 3447 | if (!this->emitGetPtrField(F.Offset, E)) |
| 3448 | return false; |
| 3449 | |
| 3450 | if (!this->visitInitializerPop(E: Init)) |
| 3451 | return false; |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | return true; |
| 3456 | } |
| 3457 | |
| 3458 | template <class Emitter> |
| 3459 | bool Compiler<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) { |
| 3460 | if (DiscardResult) |
| 3461 | return true; |
| 3462 | |
| 3463 | if (!Initializing) { |
| 3464 | unsigned StringIndex = P.createGlobalString(S: E->getFunctionName(), Base: E); |
| 3465 | return this->emitGetPtrGlobal(StringIndex, E); |
| 3466 | } |
| 3467 | |
| 3468 | return this->delegate(E: E->getFunctionName()); |
| 3469 | } |
| 3470 | |
| 3471 | template <class Emitter> |
| 3472 | bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) { |
| 3473 | if (E->getSubExpr() && !this->discard(E: E->getSubExpr())) |
| 3474 | return false; |
| 3475 | |
| 3476 | return this->emitInvalid(E); |
| 3477 | } |
| 3478 | |
| 3479 | template <class Emitter> |
| 3480 | bool Compiler<Emitter>::VisitCXXReinterpretCastExpr( |
| 3481 | const CXXReinterpretCastExpr *E) { |
| 3482 | const Expr *SubExpr = E->getSubExpr(); |
| 3483 | |
| 3484 | OptPrimType FromT = classify(SubExpr); |
| 3485 | OptPrimType ToT = classify(E); |
| 3486 | |
| 3487 | if (!FromT || !ToT) |
| 3488 | return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E); |
| 3489 | |
| 3490 | if (FromT == PT_Ptr || ToT == PT_Ptr) { |
| 3491 | if (!this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/false, E)) |
| 3492 | return false; |
| 3493 | if (E->getCastKind() == CK_LValueBitCast) |
| 3494 | return this->delegate(E: SubExpr); |
| 3495 | return this->VisitCastExpr(E); |
| 3496 | } |
| 3497 | |
| 3498 | // Try to actually do the cast. |
| 3499 | bool Fatal = (ToT != FromT); |
| 3500 | if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E)) |
| 3501 | return false; |
| 3502 | |
| 3503 | return this->VisitCastExpr(E); |
| 3504 | } |
| 3505 | |
| 3506 | template <class Emitter> |
| 3507 | bool Compiler<Emitter>::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 3508 | if (!Ctx.getLangOpts().CPlusPlus20) { |
| 3509 | if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E)) |
| 3510 | return false; |
| 3511 | } |
| 3512 | |
| 3513 | if (E->getCastKind() != CK_Dynamic) |
| 3514 | return this->VisitCastExpr(E); |
| 3515 | |
| 3516 | QualType DestType = E->getType(); |
| 3517 | // "target type must be a reference or pointer type to a defined class" |
| 3518 | if (DestType->isRecordType()) { |
| 3519 | assert(E->isGLValue()); |
| 3520 | } else { |
| 3521 | assert(DestType->isPointerOrReferenceType()); |
| 3522 | assert(DestType->isVoidPointerType() || |
| 3523 | DestType->getPointeeType()->isRecordType()); |
| 3524 | DestType = DestType->getPointeeType(); |
| 3525 | } |
| 3526 | |
| 3527 | if (!this->visit(E: E->getSubExpr())) |
| 3528 | return false; |
| 3529 | if (!this->emitDynamicCast(DestType.getTypePtr(), |
| 3530 | /*IsReferenceCast=*/E->isGLValue(), E)) |
| 3531 | return false; |
| 3532 | |
| 3533 | if (DiscardResult) |
| 3534 | return this->emitPopPtr(E); |
| 3535 | return true; |
| 3536 | } |
| 3537 | |
| 3538 | template <class Emitter> |
| 3539 | bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 3540 | assert(E->getType()->isBooleanType()); |
| 3541 | |
| 3542 | if (DiscardResult) |
| 3543 | return true; |
| 3544 | return this->emitConstBool(E->getValue(), E); |
| 3545 | } |
| 3546 | |
| 3547 | template <class Emitter> |
| 3548 | bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3549 | QualType T = E->getType(); |
| 3550 | assert(!canClassify(T)); |
| 3551 | |
| 3552 | if (T->isRecordType()) { |
| 3553 | const CXXConstructorDecl *Ctor = E->getConstructor(); |
| 3554 | |
| 3555 | // If we're discarding a construct expression, we still need |
| 3556 | // to allocate a variable and call the constructor and destructor. |
| 3557 | if (DiscardResult) { |
| 3558 | if (Ctor->isTrivial()) |
| 3559 | return true; |
| 3560 | assert(!Initializing); |
| 3561 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 3562 | |
| 3563 | if (!LocalIndex) |
| 3564 | return false; |
| 3565 | |
| 3566 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 3567 | return false; |
| 3568 | } |
| 3569 | |
| 3570 | // Trivial copy/move constructor. Avoid copy. |
| 3571 | if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() && |
| 3572 | Ctor->isTrivial() && |
| 3573 | E->getArg(Arg: 0)->isTemporaryObject(Ctx&: Ctx.getASTContext(), |
| 3574 | TempTy: T->getAsCXXRecordDecl())) |
| 3575 | return this->visitInitializer(E: E->getArg(Arg: 0)); |
| 3576 | |
| 3577 | // Zero initialization. |
| 3578 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3579 | if (ZeroInit) { |
| 3580 | const Record *R = getRecord(E->getType()); |
| 3581 | if (!R) |
| 3582 | return false; |
| 3583 | |
| 3584 | if (!this->visitZeroRecordInitializer(R, E)) |
| 3585 | return false; |
| 3586 | |
| 3587 | // If the constructor is trivial anyway, we're done. |
| 3588 | if (Ctor->isTrivial()) |
| 3589 | return true; |
| 3590 | } |
| 3591 | |
| 3592 | // Avoid materializing a temporary for an elidable copy/move constructor. |
| 3593 | if (!ZeroInit && E->isElidable()) { |
| 3594 | const Expr *SrcObj = E->getArg(Arg: 0); |
| 3595 | assert(SrcObj->isTemporaryObject(Ctx.getASTContext(), Ctor->getParent())); |
| 3596 | assert(Ctx.getASTContext().hasSameUnqualifiedType(E->getType(), |
| 3597 | SrcObj->getType())); |
| 3598 | if (const auto *ME = dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj)) { |
| 3599 | if (!this->emitCheckFunctionDecl(Ctor, E)) |
| 3600 | return false; |
| 3601 | return this->visitInitializer(E: ME->getSubExpr()); |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | const Function *Func = getFunction(FD: Ctor); |
| 3606 | |
| 3607 | if (!Func) |
| 3608 | return false; |
| 3609 | |
| 3610 | assert(Func->hasThisPointer()); |
| 3611 | assert(!Func->hasRVO()); |
| 3612 | |
| 3613 | // The This pointer is already on the stack because this is an initializer, |
| 3614 | // but we need to dup() so the call() below has its own copy. |
| 3615 | if (!this->emitDupPtr(E)) |
| 3616 | return false; |
| 3617 | |
| 3618 | // Constructor arguments. |
| 3619 | for (const auto *Arg : E->arguments()) { |
| 3620 | if (!this->visit(E: Arg)) |
| 3621 | return false; |
| 3622 | } |
| 3623 | |
| 3624 | if (Func->isVariadic()) { |
| 3625 | uint32_t VarArgSize = 0; |
| 3626 | unsigned NumParams = Func->getNumWrittenParams(); |
| 3627 | for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) { |
| 3628 | VarArgSize += |
| 3629 | align(primSize(classify(E->getArg(Arg: I)->getType()).value_or(PT_Ptr))); |
| 3630 | } |
| 3631 | if (!this->emitCallVar(Func, VarArgSize, E)) |
| 3632 | return false; |
| 3633 | } else { |
| 3634 | if (!this->emitCall(Func, 0, E)) { |
| 3635 | // When discarding, we don't need the result anyway, so clean up |
| 3636 | // the instance dup we did earlier in case surrounding code wants |
| 3637 | // to keep evaluating. |
| 3638 | if (DiscardResult) |
| 3639 | (void)this->emitPopPtr(E); |
| 3640 | return false; |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | if (DiscardResult) |
| 3645 | return this->emitPopPtr(E); |
| 3646 | return true; |
| 3647 | } |
| 3648 | |
| 3649 | if (T->isArrayType()) { |
| 3650 | const Function *Func = getFunction(FD: E->getConstructor()); |
| 3651 | if (!Func) |
| 3652 | return false; |
| 3653 | |
| 3654 | if (!this->emitDupPtr(E)) |
| 3655 | return false; |
| 3656 | |
| 3657 | std::function<bool(QualType)> initArrayDimension; |
| 3658 | initArrayDimension = [&](QualType T) -> bool { |
| 3659 | if (!T->isArrayType()) { |
| 3660 | // Constructor arguments. |
| 3661 | for (const auto *Arg : E->arguments()) { |
| 3662 | if (!this->visit(E: Arg)) |
| 3663 | return false; |
| 3664 | } |
| 3665 | |
| 3666 | return this->emitCall(Func, 0, E); |
| 3667 | } |
| 3668 | |
| 3669 | const ConstantArrayType *CAT = |
| 3670 | Ctx.getASTContext().getAsConstantArrayType(T); |
| 3671 | if (!CAT) |
| 3672 | return false; |
| 3673 | QualType ElemTy = CAT->getElementType(); |
| 3674 | unsigned NumElems = CAT->getZExtSize(); |
| 3675 | for (size_t I = 0; I != NumElems; ++I) { |
| 3676 | if (!this->emitConstUint64(I, E)) |
| 3677 | return false; |
| 3678 | if (!this->emitArrayElemPtrUint64(E)) |
| 3679 | return false; |
| 3680 | if (!initArrayDimension(ElemTy)) |
| 3681 | return false; |
| 3682 | } |
| 3683 | return this->emitPopPtr(E); |
| 3684 | }; |
| 3685 | |
| 3686 | return initArrayDimension(E->getType()); |
| 3687 | } |
| 3688 | |
| 3689 | return false; |
| 3690 | } |
| 3691 | |
| 3692 | template <class Emitter> |
| 3693 | bool Compiler<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) { |
| 3694 | if (DiscardResult) |
| 3695 | return true; |
| 3696 | |
| 3697 | const APValue Val = |
| 3698 | E->EvaluateInContext(Ctx: Ctx.getASTContext(), DefaultExpr: SourceLocDefaultExpr); |
| 3699 | |
| 3700 | // Things like __builtin_LINE(). |
| 3701 | if (E->getType()->isIntegerType()) { |
| 3702 | assert(Val.isInt()); |
| 3703 | const APSInt &I = Val.getInt(); |
| 3704 | return this->emitConst(I, E); |
| 3705 | } |
| 3706 | // Otherwise, the APValue is an LValue, with only one element. |
| 3707 | // Theoretically, we don't need the APValue at all of course. |
| 3708 | assert(E->getType()->isPointerType()); |
| 3709 | assert(Val.isLValue()); |
| 3710 | const APValue::LValueBase &Base = Val.getLValueBase(); |
| 3711 | if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>()) |
| 3712 | return this->visit(E: LValueExpr); |
| 3713 | |
| 3714 | // Otherwise, we have a decl (which is the case for |
| 3715 | // __builtin_source_location). |
| 3716 | assert(Base.is<const ValueDecl *>()); |
| 3717 | assert(Val.getLValuePath().size() == 0); |
| 3718 | const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>(); |
| 3719 | assert(BaseDecl); |
| 3720 | |
| 3721 | auto *UGCD = cast<UnnamedGlobalConstantDecl>(Val: BaseDecl); |
| 3722 | |
| 3723 | UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(VD: UGCD); |
| 3724 | if (!GlobalIndex) |
| 3725 | return false; |
| 3726 | |
| 3727 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 3728 | return false; |
| 3729 | |
| 3730 | const Record *R = getRecord(E->getType()); |
| 3731 | const APValue &V = UGCD->getValue(); |
| 3732 | for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) { |
| 3733 | const Record::Field *F = R->getField(I); |
| 3734 | const APValue &FieldValue = V.getStructField(i: I); |
| 3735 | |
| 3736 | PrimType FieldT = classifyPrim(F->Decl->getType()); |
| 3737 | |
| 3738 | if (!this->visitAPValue(Val: FieldValue, ValType: FieldT, Info: E)) |
| 3739 | return false; |
| 3740 | if (!this->emitInitField(FieldT, F->Offset, E)) |
| 3741 | return false; |
| 3742 | } |
| 3743 | |
| 3744 | // Leave the pointer to the global on the stack. |
| 3745 | return true; |
| 3746 | } |
| 3747 | |
| 3748 | template <class Emitter> |
| 3749 | bool Compiler<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
| 3750 | unsigned N = E->getNumComponents(); |
| 3751 | if (N == 0) |
| 3752 | return false; |
| 3753 | |
| 3754 | for (unsigned I = 0; I != N; ++I) { |
| 3755 | const OffsetOfNode &Node = E->getComponent(Idx: I); |
| 3756 | if (Node.getKind() == OffsetOfNode::Array) { |
| 3757 | const Expr *ArrayIndexExpr = E->getIndexExpr(Idx: Node.getArrayExprIndex()); |
| 3758 | PrimType IndexT = classifyPrim(ArrayIndexExpr->getType()); |
| 3759 | |
| 3760 | if (DiscardResult) { |
| 3761 | if (!this->discard(E: ArrayIndexExpr)) |
| 3762 | return false; |
| 3763 | continue; |
| 3764 | } |
| 3765 | |
| 3766 | if (!this->visit(E: ArrayIndexExpr)) |
| 3767 | return false; |
| 3768 | // Cast to Sint64. |
| 3769 | if (IndexT != PT_Sint64) { |
| 3770 | if (!this->emitCast(IndexT, PT_Sint64, E)) |
| 3771 | return false; |
| 3772 | } |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | if (DiscardResult) |
| 3777 | return true; |
| 3778 | |
| 3779 | PrimType T = classifyPrim(E->getType()); |
| 3780 | return this->emitOffsetOf(T, E, E); |
| 3781 | } |
| 3782 | |
| 3783 | template <class Emitter> |
| 3784 | bool Compiler<Emitter>::VisitCXXScalarValueInitExpr( |
| 3785 | const CXXScalarValueInitExpr *E) { |
| 3786 | QualType Ty = E->getType(); |
| 3787 | |
| 3788 | if (DiscardResult || Ty->isVoidType()) |
| 3789 | return true; |
| 3790 | |
| 3791 | if (OptPrimType T = classify(Ty)) |
| 3792 | return this->visitZeroInitializer(T: *T, QT: Ty, E); |
| 3793 | |
| 3794 | if (Ty->isAnyComplexType() || Ty->isVectorType()) { |
| 3795 | if (!Initializing) { |
| 3796 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 3797 | if (!LocalIndex) |
| 3798 | return false; |
| 3799 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 3800 | return false; |
| 3801 | } |
| 3802 | |
| 3803 | QualType ElemQT; |
| 3804 | unsigned NumElems; |
| 3805 | if (const auto *CT = Ty->getAs<ComplexType>()) { |
| 3806 | NumElems = 2; |
| 3807 | ElemQT = CT->getElementType(); |
| 3808 | } else { |
| 3809 | const auto *VT = Ty->castAs<VectorType>(); |
| 3810 | NumElems = VT->getNumElements(); |
| 3811 | ElemQT = VT->getElementType(); |
| 3812 | } |
| 3813 | |
| 3814 | PrimType ElemT = classifyPrim(ElemQT); |
| 3815 | |
| 3816 | // Initialize all fields to 0. |
| 3817 | for (unsigned I = 0, N = NumElems; I != N; ++I) { |
| 3818 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 3819 | return false; |
| 3820 | if (!this->emitInitElem(ElemT, I, E)) |
| 3821 | return false; |
| 3822 | } |
| 3823 | return true; |
| 3824 | } |
| 3825 | |
| 3826 | return false; |
| 3827 | } |
| 3828 | |
| 3829 | template <class Emitter> |
| 3830 | bool Compiler<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 3831 | return this->emitConst(E->getPackLength(), E); |
| 3832 | } |
| 3833 | |
| 3834 | template <class Emitter> |
| 3835 | bool Compiler<Emitter>::VisitGenericSelectionExpr( |
| 3836 | const GenericSelectionExpr *E) { |
| 3837 | return this->delegate(E: E->getResultExpr()); |
| 3838 | } |
| 3839 | |
| 3840 | template <class Emitter> |
| 3841 | bool Compiler<Emitter>::VisitChooseExpr(const ChooseExpr *E) { |
| 3842 | return this->delegate(E: E->getChosenSubExpr()); |
| 3843 | } |
| 3844 | |
| 3845 | template <class Emitter> |
| 3846 | bool Compiler<Emitter>::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 3847 | if (DiscardResult) |
| 3848 | return true; |
| 3849 | |
| 3850 | return this->emitConst(E->getValue(), E); |
| 3851 | } |
| 3852 | |
| 3853 | template <class Emitter> |
| 3854 | bool Compiler<Emitter>::VisitCXXInheritedCtorInitExpr( |
| 3855 | const CXXInheritedCtorInitExpr *E) { |
| 3856 | const CXXConstructorDecl *Ctor = E->getConstructor(); |
| 3857 | assert(!Ctor->isTrivial() && |
| 3858 | "Trivial CXXInheritedCtorInitExpr, implement. (possible?)" ); |
| 3859 | const Function *F = this->getFunction(FD: Ctor); |
| 3860 | assert(F); |
| 3861 | assert(!F->hasRVO()); |
| 3862 | assert(F->hasThisPointer()); |
| 3863 | |
| 3864 | if (!this->emitDupPtr(SourceInfo{})) |
| 3865 | return false; |
| 3866 | |
| 3867 | // Forward all arguments of the current function (which should be a |
| 3868 | // constructor itself) to the inherited ctor. |
| 3869 | // This is necessary because the calling code has pushed the pointer |
| 3870 | // of the correct base for us already, but the arguments need |
| 3871 | // to come after. |
| 3872 | unsigned ParamIndex = 0; |
| 3873 | for (const ParmVarDecl *PD : Ctor->parameters()) { |
| 3874 | PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr); |
| 3875 | |
| 3876 | if (!this->emitGetParam(PT, ParamIndex, E)) |
| 3877 | return false; |
| 3878 | ++ParamIndex; |
| 3879 | } |
| 3880 | |
| 3881 | return this->emitCall(F, 0, E); |
| 3882 | } |
| 3883 | |
| 3884 | // FIXME: This function has become rather unwieldy, especially |
| 3885 | // the part where we initialize an array allocation of dynamic size. |
| 3886 | template <class Emitter> |
| 3887 | bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) { |
| 3888 | assert(classifyPrim(E->getType()) == PT_Ptr); |
| 3889 | const Expr *Init = E->getInitializer(); |
| 3890 | QualType ElementType = E->getAllocatedType(); |
| 3891 | OptPrimType ElemT = classify(ElementType); |
| 3892 | unsigned PlacementArgs = E->getNumPlacementArgs(); |
| 3893 | const FunctionDecl *OperatorNew = E->getOperatorNew(); |
| 3894 | const Expr *PlacementDest = nullptr; |
| 3895 | bool IsNoThrow = false; |
| 3896 | |
| 3897 | if (E->containsErrors()) |
| 3898 | return false; |
| 3899 | |
| 3900 | if (PlacementArgs != 0) { |
| 3901 | // FIXME: There is no restriction on this, but it's not clear that any |
| 3902 | // other form makes any sense. We get here for cases such as: |
| 3903 | // |
| 3904 | // new (std::align_val_t{N}) X(int) |
| 3905 | // |
| 3906 | // (which should presumably be valid only if N is a multiple of |
| 3907 | // alignof(int), and in any case can't be deallocated unless N is |
| 3908 | // alignof(X) and X has new-extended alignment). |
| 3909 | if (PlacementArgs == 1) { |
| 3910 | const Expr *Arg1 = E->getPlacementArg(I: 0); |
| 3911 | if (Arg1->getType()->isNothrowT()) { |
| 3912 | if (!this->discard(E: Arg1)) |
| 3913 | return false; |
| 3914 | IsNoThrow = true; |
| 3915 | } else { |
| 3916 | // Invalid unless we have C++26 or are in a std:: function. |
| 3917 | if (!this->emitInvalidNewDeleteExpr(E, E)) |
| 3918 | return false; |
| 3919 | |
| 3920 | // If we have a placement-new destination, we'll later use that instead |
| 3921 | // of allocating. |
| 3922 | if (OperatorNew->isReservedGlobalPlacementOperator()) |
| 3923 | PlacementDest = Arg1; |
| 3924 | } |
| 3925 | } else { |
| 3926 | // Always invalid. |
| 3927 | return this->emitInvalid(E); |
| 3928 | } |
| 3929 | } else if (!OperatorNew |
| 3930 | ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) |
| 3931 | return this->emitInvalidNewDeleteExpr(E, E); |
| 3932 | |
| 3933 | const Descriptor *Desc; |
| 3934 | if (!PlacementDest) { |
| 3935 | if (ElemT) { |
| 3936 | if (E->isArray()) |
| 3937 | Desc = nullptr; // We're not going to use it in this case. |
| 3938 | else |
| 3939 | Desc = P.createDescriptor(D: E, T: *ElemT, /*SourceTy=*/nullptr, |
| 3940 | MDSize: Descriptor::InlineDescMD); |
| 3941 | } else { |
| 3942 | Desc = P.createDescriptor( |
| 3943 | D: E, Ty: ElementType.getTypePtr(), |
| 3944 | MDSize: E->isArray() ? std::nullopt : Descriptor::InlineDescMD, |
| 3945 | /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false, |
| 3946 | /*IsVolatile=*/false, Init); |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | if (E->isArray()) { |
| 3951 | std::optional<const Expr *> ArraySizeExpr = E->getArraySize(); |
| 3952 | if (!ArraySizeExpr) |
| 3953 | return false; |
| 3954 | |
| 3955 | const Expr *Stripped = *ArraySizeExpr; |
| 3956 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped); |
| 3957 | Stripped = ICE->getSubExpr()) |
| 3958 | if (ICE->getCastKind() != CK_NoOp && |
| 3959 | ICE->getCastKind() != CK_IntegralCast) |
| 3960 | break; |
| 3961 | |
| 3962 | PrimType SizeT = classifyPrim(Stripped->getType()); |
| 3963 | |
| 3964 | // Save evaluated array size to a variable. |
| 3965 | unsigned ArrayLen = |
| 3966 | allocateLocalPrimitive(Decl: Stripped, Ty: SizeT, /*IsConst=*/false); |
| 3967 | if (!this->visit(E: Stripped)) |
| 3968 | return false; |
| 3969 | if (!this->emitSetLocal(SizeT, ArrayLen, E)) |
| 3970 | return false; |
| 3971 | |
| 3972 | if (PlacementDest) { |
| 3973 | if (!this->visit(E: PlacementDest)) |
| 3974 | return false; |
| 3975 | if (!this->emitGetLocal(SizeT, ArrayLen, E)) |
| 3976 | return false; |
| 3977 | if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E)) |
| 3978 | return false; |
| 3979 | } else { |
| 3980 | if (!this->emitGetLocal(SizeT, ArrayLen, E)) |
| 3981 | return false; |
| 3982 | |
| 3983 | if (ElemT) { |
| 3984 | // N primitive elements. |
| 3985 | if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E)) |
| 3986 | return false; |
| 3987 | } else { |
| 3988 | // N Composite elements. |
| 3989 | if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E)) |
| 3990 | return false; |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | if (Init) { |
| 3995 | QualType InitType = Init->getType(); |
| 3996 | size_t StaticInitElems = 0; |
| 3997 | const Expr *DynamicInit = nullptr; |
| 3998 | OptPrimType ElemT; |
| 3999 | |
| 4000 | if (const ConstantArrayType *CAT = |
| 4001 | Ctx.getASTContext().getAsConstantArrayType(T: InitType)) { |
| 4002 | StaticInitElems = CAT->getZExtSize(); |
| 4003 | // Initialize the first S element from the initializer. |
| 4004 | if (!this->visitInitializer(E: Init)) |
| 4005 | return false; |
| 4006 | |
| 4007 | if (const auto *ILE = dyn_cast<InitListExpr>(Val: Init)) { |
| 4008 | if (ILE->hasArrayFiller()) |
| 4009 | DynamicInit = ILE->getArrayFiller(); |
| 4010 | else if (StaticInitElems > 0 && isa<StringLiteral>(Val: ILE->getInit(Init: 0))) |
| 4011 | ElemT = classifyPrim(CAT->getElementType()); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | // The initializer initializes a certain number of elements, S. |
| 4016 | // However, the complete number of elements, N, might be larger than that. |
| 4017 | // In this case, we need to get an initializer for the remaining elements. |
| 4018 | // There are three cases: |
| 4019 | // 1) For the form 'new Struct[n];', the initializer is a |
| 4020 | // CXXConstructExpr and its type is an IncompleteArrayType. |
| 4021 | // 2) For the form 'new Struct[n]{1,2,3}', the initializer is an |
| 4022 | // InitListExpr and the initializer for the remaining elements |
| 4023 | // is the array filler. |
| 4024 | // 3) StringLiterals don't have an array filler, so we need to zero |
| 4025 | // the remaining elements. |
| 4026 | |
| 4027 | if (DynamicInit || ElemT || InitType->isIncompleteArrayType()) { |
| 4028 | const Function *CtorFunc = nullptr; |
| 4029 | if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: Init)) { |
| 4030 | CtorFunc = getFunction(FD: CE->getConstructor()); |
| 4031 | if (!CtorFunc) |
| 4032 | return false; |
| 4033 | } else if (!DynamicInit && !ElemT) |
| 4034 | DynamicInit = Init; |
| 4035 | |
| 4036 | LabelTy EndLabel = this->getLabel(); |
| 4037 | LabelTy StartLabel = this->getLabel(); |
| 4038 | |
| 4039 | // In the nothrow case, the alloc above might have returned nullptr. |
| 4040 | // Don't call any constructors that case. |
| 4041 | if (IsNoThrow) { |
| 4042 | if (!this->emitDupPtr(E)) |
| 4043 | return false; |
| 4044 | if (!this->emitNullPtr(0, nullptr, E)) |
| 4045 | return false; |
| 4046 | if (!this->emitEQPtr(E)) |
| 4047 | return false; |
| 4048 | if (!this->jumpTrue(EndLabel, E)) |
| 4049 | return false; |
| 4050 | } |
| 4051 | |
| 4052 | // Create loop variables. |
| 4053 | unsigned Iter = |
| 4054 | allocateLocalPrimitive(Decl: Stripped, Ty: SizeT, /*IsConst=*/false); |
| 4055 | if (!this->emitConst(StaticInitElems, SizeT, E)) |
| 4056 | return false; |
| 4057 | if (!this->emitSetLocal(SizeT, Iter, E)) |
| 4058 | return false; |
| 4059 | |
| 4060 | this->fallthrough(StartLabel); |
| 4061 | this->emitLabel(StartLabel); |
| 4062 | // Condition. Iter < ArrayLen? |
| 4063 | if (!this->emitGetLocal(SizeT, Iter, E)) |
| 4064 | return false; |
| 4065 | if (!this->emitGetLocal(SizeT, ArrayLen, E)) |
| 4066 | return false; |
| 4067 | if (!this->emitLT(SizeT, E)) |
| 4068 | return false; |
| 4069 | if (!this->jumpFalse(EndLabel, E)) |
| 4070 | return false; |
| 4071 | |
| 4072 | // Pointer to the allocated array is already on the stack. |
| 4073 | if (!this->emitGetLocal(SizeT, Iter, E)) |
| 4074 | return false; |
| 4075 | if (!this->emitArrayElemPtr(SizeT, E)) |
| 4076 | return false; |
| 4077 | |
| 4078 | if (isa_and_nonnull<ImplicitValueInitExpr>(Val: DynamicInit) && |
| 4079 | DynamicInit->getType()->isArrayType()) { |
| 4080 | QualType ElemType = |
| 4081 | DynamicInit->getType()->getAsArrayTypeUnsafe()->getElementType(); |
| 4082 | if (OptPrimType InitT = classify(ElemType)) { |
| 4083 | if (!this->visitZeroInitializer(T: *InitT, QT: ElemType, E)) |
| 4084 | return false; |
| 4085 | if (!this->emitStorePop(*InitT, E)) |
| 4086 | return false; |
| 4087 | } else { |
| 4088 | assert(ElemType->isArrayType()); |
| 4089 | if (!this->visitZeroArrayInitializer(T: ElemType, E)) |
| 4090 | return false; |
| 4091 | } |
| 4092 | } else if (DynamicInit) { |
| 4093 | if (OptPrimType InitT = classify(DynamicInit)) { |
| 4094 | if (!this->visit(E: DynamicInit)) |
| 4095 | return false; |
| 4096 | if (!this->emitStorePop(*InitT, E)) |
| 4097 | return false; |
| 4098 | } else { |
| 4099 | if (!this->visitInitializerPop(E: DynamicInit)) |
| 4100 | return false; |
| 4101 | } |
| 4102 | } else if (ElemT) { |
| 4103 | if (!this->visitZeroInitializer( |
| 4104 | T: *ElemT, QT: InitType->getAsArrayTypeUnsafe()->getElementType(), |
| 4105 | E: Init)) |
| 4106 | return false; |
| 4107 | if (!this->emitStorePop(*ElemT, E)) |
| 4108 | return false; |
| 4109 | } else { |
| 4110 | assert(CtorFunc); |
| 4111 | if (!this->emitCall(CtorFunc, 0, E)) |
| 4112 | return false; |
| 4113 | } |
| 4114 | |
| 4115 | // ++Iter; |
| 4116 | if (!this->emitGetPtrLocal(Iter, E)) |
| 4117 | return false; |
| 4118 | if (!this->emitIncPop(SizeT, false, E)) |
| 4119 | return false; |
| 4120 | |
| 4121 | if (!this->jump(StartLabel, E)) |
| 4122 | return false; |
| 4123 | |
| 4124 | this->fallthrough(EndLabel); |
| 4125 | this->emitLabel(EndLabel); |
| 4126 | } |
| 4127 | } |
| 4128 | } else { // Non-array. |
| 4129 | if (PlacementDest) { |
| 4130 | if (!this->visit(E: PlacementDest)) |
| 4131 | return false; |
| 4132 | if (!this->emitCheckNewTypeMismatch(E, E)) |
| 4133 | return false; |
| 4134 | |
| 4135 | } else { |
| 4136 | // Allocate just one element. |
| 4137 | if (!this->emitAlloc(Desc, E)) |
| 4138 | return false; |
| 4139 | } |
| 4140 | |
| 4141 | if (Init) { |
| 4142 | if (ElemT) { |
| 4143 | if (!this->visit(E: Init)) |
| 4144 | return false; |
| 4145 | |
| 4146 | if (!this->emitInit(*ElemT, E)) |
| 4147 | return false; |
| 4148 | } else { |
| 4149 | // Composite. |
| 4150 | if (!this->visitInitializer(E: Init)) |
| 4151 | return false; |
| 4152 | } |
| 4153 | } |
| 4154 | } |
| 4155 | |
| 4156 | if (DiscardResult) |
| 4157 | return this->emitPopPtr(E); |
| 4158 | |
| 4159 | return true; |
| 4160 | } |
| 4161 | |
| 4162 | template <class Emitter> |
| 4163 | bool Compiler<Emitter>::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 4164 | if (E->containsErrors()) |
| 4165 | return false; |
| 4166 | const FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
| 4167 | |
| 4168 | if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) |
| 4169 | return this->emitInvalidNewDeleteExpr(E, E); |
| 4170 | |
| 4171 | // Arg must be an lvalue. |
| 4172 | if (!this->visit(E: E->getArgument())) |
| 4173 | return false; |
| 4174 | |
| 4175 | return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E); |
| 4176 | } |
| 4177 | |
| 4178 | template <class Emitter> |
| 4179 | bool Compiler<Emitter>::VisitBlockExpr(const BlockExpr *E) { |
| 4180 | if (DiscardResult) |
| 4181 | return true; |
| 4182 | |
| 4183 | const Function *Func = nullptr; |
| 4184 | if (const Function *F = Ctx.getOrCreateObjCBlock(E)) |
| 4185 | Func = F; |
| 4186 | |
| 4187 | if (!Func) |
| 4188 | return false; |
| 4189 | return this->emitGetFnPtr(Func, E); |
| 4190 | } |
| 4191 | |
| 4192 | template <class Emitter> |
| 4193 | bool Compiler<Emitter>::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 4194 | const Type *TypeInfoType = E->getType().getTypePtr(); |
| 4195 | |
| 4196 | auto canonType = [](const Type *T) { |
| 4197 | return T->getCanonicalTypeUnqualified().getTypePtr(); |
| 4198 | }; |
| 4199 | |
| 4200 | if (!E->isPotentiallyEvaluated()) { |
| 4201 | if (DiscardResult) |
| 4202 | return true; |
| 4203 | |
| 4204 | if (E->isTypeOperand()) |
| 4205 | return this->emitGetTypeid( |
| 4206 | canonType(E->getTypeOperand(Context: Ctx.getASTContext()).getTypePtr()), |
| 4207 | TypeInfoType, E); |
| 4208 | |
| 4209 | return this->emitGetTypeid( |
| 4210 | canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType, |
| 4211 | E); |
| 4212 | } |
| 4213 | |
| 4214 | // Otherwise, we need to evaluate the expression operand. |
| 4215 | assert(E->getExprOperand()); |
| 4216 | assert(E->getExprOperand()->isLValue()); |
| 4217 | |
| 4218 | if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E)) |
| 4219 | return false; |
| 4220 | |
| 4221 | if (!this->visit(E: E->getExprOperand())) |
| 4222 | return false; |
| 4223 | |
| 4224 | if (!this->emitGetTypeidPtr(TypeInfoType, E)) |
| 4225 | return false; |
| 4226 | if (DiscardResult) |
| 4227 | return this->emitPopPtr(E); |
| 4228 | return true; |
| 4229 | } |
| 4230 | |
| 4231 | template <class Emitter> |
| 4232 | bool Compiler<Emitter>::VisitObjCDictionaryLiteral( |
| 4233 | const ObjCDictionaryLiteral *E) { |
| 4234 | if (E->isExpressibleAsConstantInitializer()) |
| 4235 | return this->emitDummyPtr(D: E, E); |
| 4236 | return this->emitError(E); |
| 4237 | } |
| 4238 | |
| 4239 | template <class Emitter> |
| 4240 | bool Compiler<Emitter>::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) { |
| 4241 | if (E->isExpressibleAsConstantInitializer()) |
| 4242 | return this->emitDummyPtr(D: E, E); |
| 4243 | return this->emitError(E); |
| 4244 | } |
| 4245 | |
| 4246 | template <class Emitter> |
| 4247 | bool Compiler<Emitter>::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 4248 | assert(Ctx.getLangOpts().CPlusPlus); |
| 4249 | return this->emitConstBool(E->getValue(), E); |
| 4250 | } |
| 4251 | |
| 4252 | template <class Emitter> |
| 4253 | bool Compiler<Emitter>::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4254 | if (DiscardResult) |
| 4255 | return true; |
| 4256 | assert(!Initializing); |
| 4257 | |
| 4258 | const MSGuidDecl *GuidDecl = E->getGuidDecl(); |
| 4259 | const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl(); |
| 4260 | assert(RD); |
| 4261 | // If the definiton of the result type is incomplete, just return a dummy. |
| 4262 | // If (and when) that is read from, we will fail, but not now. |
| 4263 | if (!RD->isCompleteDefinition()) |
| 4264 | return this->emitDummyPtr(D: GuidDecl, E); |
| 4265 | |
| 4266 | UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(VD: GuidDecl); |
| 4267 | if (!GlobalIndex) |
| 4268 | return false; |
| 4269 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 4270 | return false; |
| 4271 | |
| 4272 | assert(this->getRecord(E->getType())); |
| 4273 | |
| 4274 | const APValue &V = GuidDecl->getAsAPValue(); |
| 4275 | if (V.getKind() == APValue::None) |
| 4276 | return true; |
| 4277 | |
| 4278 | assert(V.isStruct()); |
| 4279 | assert(V.getStructNumBases() == 0); |
| 4280 | if (!this->visitAPValueInitializer(Val: V, Info: E, T: E->getType())) |
| 4281 | return false; |
| 4282 | |
| 4283 | return this->emitFinishInit(E); |
| 4284 | } |
| 4285 | |
| 4286 | template <class Emitter> |
| 4287 | bool Compiler<Emitter>::VisitRequiresExpr(const RequiresExpr *E) { |
| 4288 | assert(classifyPrim(E->getType()) == PT_Bool); |
| 4289 | if (E->isValueDependent()) |
| 4290 | return false; |
| 4291 | if (DiscardResult) |
| 4292 | return true; |
| 4293 | return this->emitConstBool(E->isSatisfied(), E); |
| 4294 | } |
| 4295 | |
| 4296 | template <class Emitter> |
| 4297 | bool Compiler<Emitter>::VisitConceptSpecializationExpr( |
| 4298 | const ConceptSpecializationExpr *E) { |
| 4299 | assert(classifyPrim(E->getType()) == PT_Bool); |
| 4300 | if (DiscardResult) |
| 4301 | return true; |
| 4302 | return this->emitConstBool(E->isSatisfied(), E); |
| 4303 | } |
| 4304 | |
| 4305 | template <class Emitter> |
| 4306 | bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator( |
| 4307 | const CXXRewrittenBinaryOperator *E) { |
| 4308 | return this->delegate(E: E->getSemanticForm()); |
| 4309 | } |
| 4310 | |
| 4311 | template <class Emitter> |
| 4312 | bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
| 4313 | |
| 4314 | for (const Expr *SemE : E->semantics()) { |
| 4315 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) { |
| 4316 | if (SemE == E->getResultExpr()) |
| 4317 | return false; |
| 4318 | |
| 4319 | if (OVE->isUnique()) |
| 4320 | continue; |
| 4321 | |
| 4322 | if (!this->discard(E: OVE)) |
| 4323 | return false; |
| 4324 | } else if (SemE == E->getResultExpr()) { |
| 4325 | if (!this->delegate(E: SemE)) |
| 4326 | return false; |
| 4327 | } else { |
| 4328 | if (!this->discard(E: SemE)) |
| 4329 | return false; |
| 4330 | } |
| 4331 | } |
| 4332 | return true; |
| 4333 | } |
| 4334 | |
| 4335 | template <class Emitter> |
| 4336 | bool Compiler<Emitter>::VisitPackIndexingExpr(const PackIndexingExpr *E) { |
| 4337 | return this->delegate(E: E->getSelectedExpr()); |
| 4338 | } |
| 4339 | |
| 4340 | template <class Emitter> |
| 4341 | bool Compiler<Emitter>::VisitRecoveryExpr(const RecoveryExpr *E) { |
| 4342 | return this->emitError(E); |
| 4343 | } |
| 4344 | |
| 4345 | template <class Emitter> |
| 4346 | bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
| 4347 | assert(E->getType()->isVoidPointerType()); |
| 4348 | if (DiscardResult) |
| 4349 | return true; |
| 4350 | |
| 4351 | return this->emitDummyPtr(D: E, E); |
| 4352 | } |
| 4353 | |
| 4354 | template <class Emitter> |
| 4355 | bool Compiler<Emitter>::emitVectorConversion(const Expr *Src, const Expr *E) { |
| 4356 | if (Src->containsErrors()) |
| 4357 | return false; |
| 4358 | |
| 4359 | const auto *VT = E->getType()->castAs<VectorType>(); |
| 4360 | QualType ElemType = VT->getElementType(); |
| 4361 | PrimType ElemT = classifyPrim(ElemType); |
| 4362 | QualType SrcType = Src->getType(); |
| 4363 | PrimType SrcElemT = classifyVectorElementType(T: SrcType); |
| 4364 | |
| 4365 | if (!Initializing) { |
| 4366 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 4367 | if (!LocalIndex) |
| 4368 | return false; |
| 4369 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4370 | return false; |
| 4371 | } |
| 4372 | |
| 4373 | unsigned SrcOffset = |
| 4374 | this->allocateLocalPrimitive(Decl: Src, Ty: PT_Ptr, /*IsConst=*/true); |
| 4375 | if (!this->visit(E: Src)) |
| 4376 | return false; |
| 4377 | if (!this->emitSetLocal(PT_Ptr, SrcOffset, E)) |
| 4378 | return false; |
| 4379 | |
| 4380 | for (unsigned I = 0; I != VT->getNumElements(); ++I) { |
| 4381 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 4382 | return false; |
| 4383 | if (!this->emitArrayElemPop(SrcElemT, I, E)) |
| 4384 | return false; |
| 4385 | |
| 4386 | // Cast to the desired result element type. |
| 4387 | if (SrcElemT != ElemT) { |
| 4388 | if (!this->emitPrimCast(FromT: SrcElemT, ToT: ElemT, ToQT: ElemType, E)) |
| 4389 | return false; |
| 4390 | } else if (ElemType->isFloatingType() && SrcType != ElemType) { |
| 4391 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: ElemType); |
| 4392 | if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E)) |
| 4393 | return false; |
| 4394 | } |
| 4395 | if (!this->emitInitElem(ElemT, I, E)) |
| 4396 | return false; |
| 4397 | } |
| 4398 | return true; |
| 4399 | } |
| 4400 | |
| 4401 | template <class Emitter> |
| 4402 | bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) { |
| 4403 | return emitVectorConversion(Src: E->getSrcExpr(), E); |
| 4404 | } |
| 4405 | |
| 4406 | template <class Emitter> |
| 4407 | bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) { |
| 4408 | // FIXME: Unary shuffle with mask not currently supported. |
| 4409 | if (E->getNumSubExprs() == 2) |
| 4410 | return this->emitInvalid(E); |
| 4411 | |
| 4412 | assert(E->getNumSubExprs() > 2); |
| 4413 | |
| 4414 | const Expr *Vecs[] = {E->getExpr(Index: 0), E->getExpr(Index: 1)}; |
| 4415 | const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>(); |
| 4416 | PrimType ElemT = classifyPrim(VT->getElementType()); |
| 4417 | unsigned NumInputElems = VT->getNumElements(); |
| 4418 | unsigned NumOutputElems = E->getNumSubExprs() - 2; |
| 4419 | assert(NumOutputElems > 0); |
| 4420 | |
| 4421 | if (!Initializing) { |
| 4422 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 4423 | if (!LocalIndex) |
| 4424 | return false; |
| 4425 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4426 | return false; |
| 4427 | } |
| 4428 | |
| 4429 | // Save both input vectors to a local variable. |
| 4430 | unsigned VectorOffsets[2]; |
| 4431 | for (unsigned I = 0; I != 2; ++I) { |
| 4432 | VectorOffsets[I] = |
| 4433 | this->allocateLocalPrimitive(Decl: Vecs[I], Ty: PT_Ptr, /*IsConst=*/true); |
| 4434 | if (!this->visit(E: Vecs[I])) |
| 4435 | return false; |
| 4436 | if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E)) |
| 4437 | return false; |
| 4438 | } |
| 4439 | for (unsigned I = 0; I != NumOutputElems; ++I) { |
| 4440 | APSInt ShuffleIndex = E->getShuffleMaskIdx(N: I); |
| 4441 | assert(ShuffleIndex >= -1); |
| 4442 | if (ShuffleIndex == -1) |
| 4443 | return this->emitInvalidShuffleVectorIndex(I, E); |
| 4444 | |
| 4445 | assert(ShuffleIndex < (NumInputElems * 2)); |
| 4446 | if (!this->emitGetLocal(PT_Ptr, |
| 4447 | VectorOffsets[ShuffleIndex >= NumInputElems], E)) |
| 4448 | return false; |
| 4449 | unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems; |
| 4450 | if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E)) |
| 4451 | return false; |
| 4452 | |
| 4453 | if (!this->emitInitElem(ElemT, I, E)) |
| 4454 | return false; |
| 4455 | } |
| 4456 | |
| 4457 | if (DiscardResult) |
| 4458 | return this->emitPopPtr(E); |
| 4459 | |
| 4460 | return true; |
| 4461 | } |
| 4462 | |
| 4463 | template <class Emitter> |
| 4464 | bool Compiler<Emitter>::VisitExtVectorElementExpr( |
| 4465 | const ExtVectorElementExpr *E) { |
| 4466 | const Expr *Base = E->getBase(); |
| 4467 | assert( |
| 4468 | Base->getType()->isVectorType() || |
| 4469 | Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType()); |
| 4470 | |
| 4471 | SmallVector<uint32_t, 4> Indices; |
| 4472 | E->getEncodedElementAccess(Elts&: Indices); |
| 4473 | |
| 4474 | if (Indices.size() == 1) { |
| 4475 | if (!this->visit(E: Base)) |
| 4476 | return false; |
| 4477 | |
| 4478 | if (E->isGLValue()) { |
| 4479 | if (!this->emitConstUint32(Indices[0], E)) |
| 4480 | return false; |
| 4481 | return this->emitArrayElemPtrPop(PT_Uint32, E); |
| 4482 | } |
| 4483 | // Else, also load the value. |
| 4484 | return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E); |
| 4485 | } |
| 4486 | |
| 4487 | // Create a local variable for the base. |
| 4488 | unsigned BaseOffset = allocateLocalPrimitive(Decl: Base, Ty: PT_Ptr, /*IsConst=*/true); |
| 4489 | if (!this->visit(E: Base)) |
| 4490 | return false; |
| 4491 | if (!this->emitSetLocal(PT_Ptr, BaseOffset, E)) |
| 4492 | return false; |
| 4493 | |
| 4494 | // Now the vector variable for the return value. |
| 4495 | if (!Initializing) { |
| 4496 | UnsignedOrNone ResultIndex = allocateLocal(Decl: E); |
| 4497 | if (!ResultIndex) |
| 4498 | return false; |
| 4499 | if (!this->emitGetPtrLocal(*ResultIndex, E)) |
| 4500 | return false; |
| 4501 | } |
| 4502 | |
| 4503 | assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements()); |
| 4504 | |
| 4505 | PrimType ElemT = |
| 4506 | classifyPrim(E->getType()->getAs<VectorType>()->getElementType()); |
| 4507 | uint32_t DstIndex = 0; |
| 4508 | for (uint32_t I : Indices) { |
| 4509 | if (!this->emitGetLocal(PT_Ptr, BaseOffset, E)) |
| 4510 | return false; |
| 4511 | if (!this->emitArrayElemPop(ElemT, I, E)) |
| 4512 | return false; |
| 4513 | if (!this->emitInitElem(ElemT, DstIndex, E)) |
| 4514 | return false; |
| 4515 | ++DstIndex; |
| 4516 | } |
| 4517 | |
| 4518 | // Leave the result pointer on the stack. |
| 4519 | assert(!DiscardResult); |
| 4520 | return true; |
| 4521 | } |
| 4522 | |
| 4523 | template <class Emitter> |
| 4524 | bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
| 4525 | const Expr *SubExpr = E->getSubExpr(); |
| 4526 | if (!E->isExpressibleAsConstantInitializer()) |
| 4527 | return this->discard(E: SubExpr) && this->emitInvalid(E); |
| 4528 | |
| 4529 | if (DiscardResult) |
| 4530 | return true; |
| 4531 | |
| 4532 | assert(classifyPrim(E) == PT_Ptr); |
| 4533 | return this->emitDummyPtr(D: E, E); |
| 4534 | } |
| 4535 | |
| 4536 | template <class Emitter> |
| 4537 | bool Compiler<Emitter>::VisitCXXStdInitializerListExpr( |
| 4538 | const CXXStdInitializerListExpr *E) { |
| 4539 | const Expr *SubExpr = E->getSubExpr(); |
| 4540 | const ConstantArrayType *ArrayType = |
| 4541 | Ctx.getASTContext().getAsConstantArrayType(T: SubExpr->getType()); |
| 4542 | const Record *R = getRecord(E->getType()); |
| 4543 | assert(Initializing); |
| 4544 | assert(SubExpr->isGLValue()); |
| 4545 | |
| 4546 | if (!this->visit(E: SubExpr)) |
| 4547 | return false; |
| 4548 | if (!this->emitConstUint8(0, E)) |
| 4549 | return false; |
| 4550 | if (!this->emitArrayElemPtrPopUint8(E)) |
| 4551 | return false; |
| 4552 | if (!this->emitInitFieldPtr(R->getField(I: 0u)->Offset, E)) |
| 4553 | return false; |
| 4554 | |
| 4555 | PrimType SecondFieldT = classifyPrim(R->getField(I: 1u)->Decl->getType()); |
| 4556 | if (isIntegerOrBoolType(T: SecondFieldT)) { |
| 4557 | if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E)) |
| 4558 | return false; |
| 4559 | return this->emitInitField(SecondFieldT, R->getField(I: 1u)->Offset, E); |
| 4560 | } |
| 4561 | assert(SecondFieldT == PT_Ptr); |
| 4562 | |
| 4563 | if (!this->emitGetFieldPtr(R->getField(I: 0u)->Offset, E)) |
| 4564 | return false; |
| 4565 | if (!this->emitExpandPtr(E)) |
| 4566 | return false; |
| 4567 | if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E)) |
| 4568 | return false; |
| 4569 | if (!this->emitArrayElemPtrPop(PT_Uint64, E)) |
| 4570 | return false; |
| 4571 | return this->emitInitFieldPtr(R->getField(I: 1u)->Offset, E); |
| 4572 | } |
| 4573 | |
| 4574 | template <class Emitter> |
| 4575 | bool Compiler<Emitter>::VisitStmtExpr(const StmtExpr *E) { |
| 4576 | LocalScope<Emitter> BS(this); |
| 4577 | llvm::SaveAndRestore StmtExprSAR(this->InStmtExpr, true); |
| 4578 | |
| 4579 | const CompoundStmt *CS = E->getSubStmt(); |
| 4580 | const Stmt *Result = CS->body_back(); |
| 4581 | for (const Stmt *S : CS->body()) { |
| 4582 | if (S != Result) { |
| 4583 | if (!this->visitStmt(S)) |
| 4584 | return false; |
| 4585 | continue; |
| 4586 | } |
| 4587 | |
| 4588 | assert(S == Result); |
| 4589 | if (const Expr *ResultExpr = dyn_cast<Expr>(Val: S)) |
| 4590 | return this->delegate(E: ResultExpr); |
| 4591 | if (!this->visitStmt(S)) |
| 4592 | return false; |
| 4593 | return this->emitUnsupported(E); |
| 4594 | } |
| 4595 | |
| 4596 | return BS.destroyLocals(); |
| 4597 | } |
| 4598 | |
| 4599 | template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) { |
| 4600 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true, |
| 4601 | /*NewInitializing=*/false, /*ToLValue=*/false); |
| 4602 | return this->Visit(E); |
| 4603 | } |
| 4604 | |
| 4605 | template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) { |
| 4606 | // We're basically doing: |
| 4607 | // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue); |
| 4608 | // but that's unnecessary of course. |
| 4609 | return this->Visit(E); |
| 4610 | } |
| 4611 | |
| 4612 | static const Expr *stripCheckedDerivedToBaseCasts(const Expr *E) { |
| 4613 | if (const auto *PE = dyn_cast<ParenExpr>(Val: E)) |
| 4614 | return stripCheckedDerivedToBaseCasts(E: PE->getSubExpr()); |
| 4615 | |
| 4616 | if (const auto *CE = dyn_cast<CastExpr>(Val: E); |
| 4617 | CE && |
| 4618 | (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp)) |
| 4619 | return stripCheckedDerivedToBaseCasts(E: CE->getSubExpr()); |
| 4620 | |
| 4621 | return E; |
| 4622 | } |
| 4623 | |
| 4624 | static const Expr *stripDerivedToBaseCasts(const Expr *E) { |
| 4625 | if (const auto *PE = dyn_cast<ParenExpr>(Val: E)) |
| 4626 | return stripDerivedToBaseCasts(E: PE->getSubExpr()); |
| 4627 | |
| 4628 | if (const auto *CE = dyn_cast<CastExpr>(Val: E); |
| 4629 | CE && (CE->getCastKind() == CK_DerivedToBase || |
| 4630 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 4631 | CE->getCastKind() == CK_NoOp)) |
| 4632 | return stripDerivedToBaseCasts(E: CE->getSubExpr()); |
| 4633 | |
| 4634 | return E; |
| 4635 | } |
| 4636 | |
| 4637 | template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) { |
| 4638 | if (E->getType().isNull()) |
| 4639 | return false; |
| 4640 | |
| 4641 | if (E->getType()->isVoidType()) |
| 4642 | return this->discard(E); |
| 4643 | |
| 4644 | // Create local variable to hold the return value. |
| 4645 | if (!E->isGLValue() && !canClassify(E->getType())) { |
| 4646 | UnsignedOrNone LocalIndex = allocateLocal( |
| 4647 | Decl: stripDerivedToBaseCasts(E), Ty: QualType(), ScopeKind::FullExpression); |
| 4648 | if (!LocalIndex) |
| 4649 | return false; |
| 4650 | |
| 4651 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4652 | return false; |
| 4653 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex)); |
| 4654 | return this->visitInitializer(E); |
| 4655 | } |
| 4656 | |
| 4657 | // Otherwise,we have a primitive return value, produce the value directly |
| 4658 | // and push it on the stack. |
| 4659 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 4660 | /*NewInitializing=*/false, /*ToLValue=*/ToLValue); |
| 4661 | return this->Visit(E); |
| 4662 | } |
| 4663 | |
| 4664 | template <class Emitter> |
| 4665 | bool Compiler<Emitter>::visitInitializer(const Expr *E) { |
| 4666 | assert(!canClassify(E->getType())); |
| 4667 | |
| 4668 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 4669 | /*NewInitializing=*/true, /*ToLValue=*/false); |
| 4670 | return this->Visit(E) && this->emitFinishInit(E); |
| 4671 | } |
| 4672 | |
| 4673 | template <class Emitter> |
| 4674 | bool Compiler<Emitter>::visitInitializerPop(const Expr *E) { |
| 4675 | assert(!canClassify(E->getType())); |
| 4676 | |
| 4677 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 4678 | /*NewInitializing=*/true, /*ToLValue=*/false); |
| 4679 | return this->Visit(E) && this->emitFinishInitPop(E); |
| 4680 | } |
| 4681 | |
| 4682 | template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) { |
| 4683 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 4684 | /*NewInitializing=*/false, /*ToLValue=*/true); |
| 4685 | return this->Visit(E); |
| 4686 | } |
| 4687 | |
| 4688 | template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) { |
| 4689 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 4690 | /*NewInitializing=*/false, /*ToLValue=*/ToLValue); |
| 4691 | |
| 4692 | OptPrimType T = classify(E->getType()); |
| 4693 | if (!T) { |
| 4694 | // Convert complex values to bool. |
| 4695 | if (E->getType()->isAnyComplexType()) { |
| 4696 | if (!this->visit(E)) |
| 4697 | return false; |
| 4698 | return this->emitComplexBoolCast(E); |
| 4699 | } |
| 4700 | return false; |
| 4701 | } |
| 4702 | |
| 4703 | if (!this->visit(E)) |
| 4704 | return false; |
| 4705 | |
| 4706 | if (T == PT_Bool) |
| 4707 | return true; |
| 4708 | |
| 4709 | // Convert pointers to bool. |
| 4710 | if (T == PT_Ptr) |
| 4711 | return this->emitIsNonNullPtr(E); |
| 4712 | |
| 4713 | // Or Floats. |
| 4714 | if (T == PT_Float) |
| 4715 | return this->emitCastFloatingIntegralBool(getFPOptions(E), E); |
| 4716 | |
| 4717 | // Or anything else we can. |
| 4718 | return this->emitCast(*T, PT_Bool, E); |
| 4719 | } |
| 4720 | |
| 4721 | template <class Emitter> |
| 4722 | bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT, |
| 4723 | const Expr *E) { |
| 4724 | if (const auto *AT = QT->getAs<AtomicType>()) |
| 4725 | QT = AT->getValueType(); |
| 4726 | |
| 4727 | switch (T) { |
| 4728 | case PT_Bool: |
| 4729 | return this->emitZeroBool(E); |
| 4730 | case PT_Sint8: |
| 4731 | return this->emitZeroSint8(E); |
| 4732 | case PT_Uint8: |
| 4733 | return this->emitZeroUint8(E); |
| 4734 | case PT_Sint16: |
| 4735 | return this->emitZeroSint16(E); |
| 4736 | case PT_Uint16: |
| 4737 | return this->emitZeroUint16(E); |
| 4738 | case PT_Sint32: |
| 4739 | return this->emitZeroSint32(E); |
| 4740 | case PT_Uint32: |
| 4741 | return this->emitZeroUint32(E); |
| 4742 | case PT_Sint64: |
| 4743 | return this->emitZeroSint64(E); |
| 4744 | case PT_Uint64: |
| 4745 | return this->emitZeroUint64(E); |
| 4746 | case PT_IntAP: |
| 4747 | return this->emitZeroIntAP(Ctx.getBitWidth(T: QT), E); |
| 4748 | case PT_IntAPS: |
| 4749 | return this->emitZeroIntAPS(Ctx.getBitWidth(T: QT), E); |
| 4750 | case PT_Ptr: |
| 4751 | return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT), |
| 4752 | nullptr, E); |
| 4753 | case PT_MemberPtr: |
| 4754 | return this->emitNullMemberPtr(0, nullptr, E); |
| 4755 | case PT_Float: { |
| 4756 | APFloat F = APFloat::getZero(Sem: Ctx.getFloatSemantics(T: QT)); |
| 4757 | return this->emitFloat(F, Info: E); |
| 4758 | } |
| 4759 | case PT_FixedPoint: { |
| 4760 | auto Sem = Ctx.getASTContext().getFixedPointSemantics(Ty: QT); |
| 4761 | return this->emitConstFixedPoint(FixedPoint::zero(Sem), E); |
| 4762 | } |
| 4763 | } |
| 4764 | llvm_unreachable("unknown primitive type" ); |
| 4765 | } |
| 4766 | |
| 4767 | template <class Emitter> |
| 4768 | bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R, |
| 4769 | const Expr *E) { |
| 4770 | assert(E); |
| 4771 | assert(R); |
| 4772 | // Fields |
| 4773 | for (const Record::Field &Field : R->fields()) { |
| 4774 | if (Field.isUnnamedBitField()) |
| 4775 | continue; |
| 4776 | |
| 4777 | const Descriptor *D = Field.Desc; |
| 4778 | if (D->isPrimitive()) { |
| 4779 | QualType QT = D->getType(); |
| 4780 | PrimType T = D->getPrimType(); |
| 4781 | if (!this->visitZeroInitializer(T, QT, E)) |
| 4782 | return false; |
| 4783 | if (R->isUnion()) { |
| 4784 | if (!this->emitInitFieldActivate(T, Field.Offset, E)) |
| 4785 | return false; |
| 4786 | break; |
| 4787 | } |
| 4788 | if (!this->emitInitField(T, Field.Offset, E)) |
| 4789 | return false; |
| 4790 | continue; |
| 4791 | } |
| 4792 | |
| 4793 | if (!this->emitGetPtrField(Field.Offset, E)) |
| 4794 | return false; |
| 4795 | |
| 4796 | if (D->isPrimitiveArray()) { |
| 4797 | QualType ET = D->getElemQualType(); |
| 4798 | PrimType T = D->getPrimType(); |
| 4799 | for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) { |
| 4800 | if (!this->visitZeroInitializer(T, QT: ET, E)) |
| 4801 | return false; |
| 4802 | if (!this->emitInitElem(T, I, E)) |
| 4803 | return false; |
| 4804 | } |
| 4805 | } else if (D->isCompositeArray()) { |
| 4806 | // Can't be a vector or complex field. |
| 4807 | if (!this->visitZeroArrayInitializer(T: D->getType(), E)) |
| 4808 | return false; |
| 4809 | } else if (D->isRecord()) { |
| 4810 | if (!this->visitZeroRecordInitializer(R: D->ElemRecord, E)) |
| 4811 | return false; |
| 4812 | } else |
| 4813 | return false; |
| 4814 | |
| 4815 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 4816 | // object's first non-static named data member is zero-initialized |
| 4817 | if (R->isUnion()) { |
| 4818 | if (!this->emitFinishInitActivatePop(E)) |
| 4819 | return false; |
| 4820 | break; |
| 4821 | } |
| 4822 | if (!this->emitFinishInitPop(E)) |
| 4823 | return false; |
| 4824 | } |
| 4825 | |
| 4826 | for (const Record::Base &B : R->bases()) { |
| 4827 | if (!this->emitGetPtrBase(B.Offset, E)) |
| 4828 | return false; |
| 4829 | if (!this->visitZeroRecordInitializer(R: B.R, E)) |
| 4830 | return false; |
| 4831 | if (!this->emitFinishInitPop(E)) |
| 4832 | return false; |
| 4833 | } |
| 4834 | |
| 4835 | // FIXME: Virtual bases. |
| 4836 | |
| 4837 | return true; |
| 4838 | } |
| 4839 | |
| 4840 | template <class Emitter> |
| 4841 | bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) { |
| 4842 | assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType()); |
| 4843 | const ArrayType *AT = T->getAsArrayTypeUnsafe(); |
| 4844 | QualType ElemType = AT->getElementType(); |
| 4845 | size_t NumElems = cast<ConstantArrayType>(Val: AT)->getZExtSize(); |
| 4846 | |
| 4847 | if (OptPrimType ElemT = classify(ElemType)) { |
| 4848 | for (size_t I = 0; I != NumElems; ++I) { |
| 4849 | if (!this->visitZeroInitializer(T: *ElemT, QT: ElemType, E)) |
| 4850 | return false; |
| 4851 | if (!this->emitInitElem(*ElemT, I, E)) |
| 4852 | return false; |
| 4853 | } |
| 4854 | return true; |
| 4855 | } |
| 4856 | if (ElemType->isRecordType()) { |
| 4857 | const Record *R = getRecord(ElemType); |
| 4858 | if (!R) |
| 4859 | return false; |
| 4860 | |
| 4861 | for (size_t I = 0; I != NumElems; ++I) { |
| 4862 | if (!this->emitConstUint32(I, E)) |
| 4863 | return false; |
| 4864 | if (!this->emitArrayElemPtr(PT_Uint32, E)) |
| 4865 | return false; |
| 4866 | if (!this->visitZeroRecordInitializer(R, E)) |
| 4867 | return false; |
| 4868 | if (!this->emitPopPtr(E)) |
| 4869 | return false; |
| 4870 | } |
| 4871 | return true; |
| 4872 | } |
| 4873 | if (ElemType->isArrayType()) { |
| 4874 | for (size_t I = 0; I != NumElems; ++I) { |
| 4875 | if (!this->emitConstUint32(I, E)) |
| 4876 | return false; |
| 4877 | if (!this->emitArrayElemPtr(PT_Uint32, E)) |
| 4878 | return false; |
| 4879 | if (!this->visitZeroArrayInitializer(T: ElemType, E)) |
| 4880 | return false; |
| 4881 | if (!this->emitPopPtr(E)) |
| 4882 | return false; |
| 4883 | } |
| 4884 | return true; |
| 4885 | } |
| 4886 | |
| 4887 | return false; |
| 4888 | } |
| 4889 | |
| 4890 | template <class Emitter> |
| 4891 | bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS, |
| 4892 | const Expr *E) { |
| 4893 | if (!canClassify(E->getType())) |
| 4894 | return false; |
| 4895 | |
| 4896 | if (!this->visit(E: RHS)) |
| 4897 | return false; |
| 4898 | if (!this->visit(E: LHS)) |
| 4899 | return false; |
| 4900 | |
| 4901 | if (LHS->getType().isVolatileQualified()) |
| 4902 | return this->emitInvalidStore(LHS->getType().getTypePtr(), E); |
| 4903 | |
| 4904 | // We don't support assignments in C. |
| 4905 | if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E)) |
| 4906 | return false; |
| 4907 | |
| 4908 | PrimType RHT = classifyPrim(RHS); |
| 4909 | bool Activates = refersToUnion(E: LHS); |
| 4910 | bool BitField = LHS->refersToBitField(); |
| 4911 | |
| 4912 | if (!this->emitFlip(PT_Ptr, RHT, E)) |
| 4913 | return false; |
| 4914 | |
| 4915 | if (DiscardResult) { |
| 4916 | if (BitField && Activates) |
| 4917 | return this->emitStoreBitFieldActivatePop(RHT, E); |
| 4918 | if (BitField) |
| 4919 | return this->emitStoreBitFieldPop(RHT, E); |
| 4920 | if (Activates) |
| 4921 | return this->emitStoreActivatePop(RHT, E); |
| 4922 | // Otherwise, regular non-activating store. |
| 4923 | return this->emitStorePop(RHT, E); |
| 4924 | } |
| 4925 | |
| 4926 | auto maybeLoad = [&](bool Result) -> bool { |
| 4927 | if (!Result) |
| 4928 | return false; |
| 4929 | // Assignments aren't necessarily lvalues in C. |
| 4930 | // Load from them in that case. |
| 4931 | if (!E->isLValue()) |
| 4932 | return this->emitLoadPop(RHT, E); |
| 4933 | return true; |
| 4934 | }; |
| 4935 | |
| 4936 | if (BitField && Activates) |
| 4937 | return maybeLoad(this->emitStoreBitFieldActivate(RHT, E)); |
| 4938 | if (BitField) |
| 4939 | return maybeLoad(this->emitStoreBitField(RHT, E)); |
| 4940 | if (Activates) |
| 4941 | return maybeLoad(this->emitStoreActivate(RHT, E)); |
| 4942 | // Otherwise, regular non-activating store. |
| 4943 | return maybeLoad(this->emitStore(RHT, E)); |
| 4944 | } |
| 4945 | |
| 4946 | template <class Emitter> |
| 4947 | template <typename T> |
| 4948 | bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, SourceInfo Info) { |
| 4949 | switch (Ty) { |
| 4950 | case PT_Sint8: |
| 4951 | return this->emitConstSint8(Value, Info); |
| 4952 | case PT_Uint8: |
| 4953 | return this->emitConstUint8(Value, Info); |
| 4954 | case PT_Sint16: |
| 4955 | return this->emitConstSint16(Value, Info); |
| 4956 | case PT_Uint16: |
| 4957 | return this->emitConstUint16(Value, Info); |
| 4958 | case PT_Sint32: |
| 4959 | return this->emitConstSint32(Value, Info); |
| 4960 | case PT_Uint32: |
| 4961 | return this->emitConstUint32(Value, Info); |
| 4962 | case PT_Sint64: |
| 4963 | return this->emitConstSint64(Value, Info); |
| 4964 | case PT_Uint64: |
| 4965 | return this->emitConstUint64(Value, Info); |
| 4966 | case PT_Bool: |
| 4967 | return this->emitConstBool(Value, Info); |
| 4968 | case PT_Ptr: |
| 4969 | case PT_MemberPtr: |
| 4970 | case PT_Float: |
| 4971 | case PT_IntAP: |
| 4972 | case PT_IntAPS: |
| 4973 | case PT_FixedPoint: |
| 4974 | llvm_unreachable("Invalid integral type" ); |
| 4975 | break; |
| 4976 | } |
| 4977 | llvm_unreachable("unknown primitive type" ); |
| 4978 | } |
| 4979 | |
| 4980 | template <class Emitter> |
| 4981 | template <typename T> |
| 4982 | bool Compiler<Emitter>::emitConst(T Value, const Expr *E) { |
| 4983 | return this->emitConst(Value, classifyPrim(E->getType()), E); |
| 4984 | } |
| 4985 | |
| 4986 | template <class Emitter> |
| 4987 | bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty, |
| 4988 | SourceInfo Info) { |
| 4989 | if (Ty == PT_IntAPS) |
| 4990 | return this->emitConstIntAPS(Value, Info); |
| 4991 | if (Ty == PT_IntAP) |
| 4992 | return this->emitConstIntAP(Value, Info); |
| 4993 | |
| 4994 | if (Value.isSigned()) |
| 4995 | return this->emitConst(Value.getSExtValue(), Ty, Info); |
| 4996 | return this->emitConst(Value.getZExtValue(), Ty, Info); |
| 4997 | } |
| 4998 | |
| 4999 | template <class Emitter> |
| 5000 | bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty, |
| 5001 | SourceInfo Info) { |
| 5002 | if (Ty == PT_IntAPS) |
| 5003 | return this->emitConstIntAPS(Value, Info); |
| 5004 | if (Ty == PT_IntAP) |
| 5005 | return this->emitConstIntAP(Value, Info); |
| 5006 | |
| 5007 | if (isSignedType(T: Ty)) |
| 5008 | return this->emitConst(Value.getSExtValue(), Ty, Info); |
| 5009 | return this->emitConst(Value.getZExtValue(), Ty, Info); |
| 5010 | } |
| 5011 | |
| 5012 | template <class Emitter> |
| 5013 | bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) { |
| 5014 | return this->emitConst(Value, classifyPrim(E->getType()), E); |
| 5015 | } |
| 5016 | |
| 5017 | template <class Emitter> |
| 5018 | unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty, |
| 5019 | bool IsConst, |
| 5020 | bool IsVolatile, |
| 5021 | ScopeKind SC) { |
| 5022 | // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g. |
| 5023 | // (int){12} in C. Consider using Expr::isTemporaryObject() instead |
| 5024 | // or isa<MaterializeTemporaryExpr>(). |
| 5025 | Descriptor *D = P.createDescriptor(D: Src, T: Ty, SourceTy: nullptr, MDSize: Descriptor::InlineDescMD, |
| 5026 | IsConst, IsTemporary: isa<const Expr *>(Val: Src), |
| 5027 | /*IsMutable=*/false, IsVolatile); |
| 5028 | D->IsConstexprUnknown = this->VariablesAreConstexprUnknown; |
| 5029 | Scope::Local Local = this->createLocal(D); |
| 5030 | if (auto *VD = dyn_cast_if_present<ValueDecl>(Val: Src.dyn_cast<const Decl *>())) |
| 5031 | Locals.insert(KV: {VD, Local}); |
| 5032 | VarScope->addForScopeKind(Local, SC); |
| 5033 | return Local.Offset; |
| 5034 | } |
| 5035 | |
| 5036 | template <class Emitter> |
| 5037 | UnsignedOrNone Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty, |
| 5038 | ScopeKind SC) { |
| 5039 | const ValueDecl *Key = nullptr; |
| 5040 | const Expr *Init = nullptr; |
| 5041 | bool IsTemporary = false; |
| 5042 | if (auto *VD = dyn_cast_if_present<ValueDecl>(Val: Src.dyn_cast<const Decl *>())) { |
| 5043 | Key = VD; |
| 5044 | |
| 5045 | if (const auto *VarD = dyn_cast<VarDecl>(Val: VD)) |
| 5046 | Init = VarD->getInit(); |
| 5047 | } |
| 5048 | if (auto *E = Src.dyn_cast<const Expr *>()) { |
| 5049 | IsTemporary = true; |
| 5050 | if (Ty.isNull()) |
| 5051 | Ty = E->getType(); |
| 5052 | } |
| 5053 | |
| 5054 | Descriptor *D = P.createDescriptor( |
| 5055 | D: Src, Ty: Ty.getTypePtr(), MDSize: Descriptor::InlineDescMD, IsConst: Ty.isConstQualified(), |
| 5056 | IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(), |
| 5057 | Init); |
| 5058 | if (!D) |
| 5059 | return std::nullopt; |
| 5060 | D->IsConstexprUnknown = this->VariablesAreConstexprUnknown; |
| 5061 | |
| 5062 | Scope::Local Local = this->createLocal(D); |
| 5063 | if (Key) |
| 5064 | Locals.insert(KV: {Key, Local}); |
| 5065 | VarScope->addForScopeKind(Local, SC); |
| 5066 | return Local.Offset; |
| 5067 | } |
| 5068 | |
| 5069 | template <class Emitter> |
| 5070 | UnsignedOrNone Compiler<Emitter>::allocateTemporary(const Expr *E) { |
| 5071 | QualType Ty = E->getType(); |
| 5072 | assert(!Ty->isRecordType()); |
| 5073 | |
| 5074 | Descriptor *D = P.createDescriptor( |
| 5075 | D: E, Ty: Ty.getTypePtr(), MDSize: Descriptor::InlineDescMD, IsConst: Ty.isConstQualified(), |
| 5076 | /*IsTemporary=*/true); |
| 5077 | |
| 5078 | if (!D) |
| 5079 | return std::nullopt; |
| 5080 | |
| 5081 | Scope::Local Local = this->createLocal(D); |
| 5082 | VariableScope<Emitter> *S = VarScope; |
| 5083 | assert(S); |
| 5084 | // Attach to topmost scope. |
| 5085 | while (S->getParent()) |
| 5086 | S = S->getParent(); |
| 5087 | assert(S && !S->getParent()); |
| 5088 | S->addLocal(Local); |
| 5089 | return Local.Offset; |
| 5090 | } |
| 5091 | |
| 5092 | template <class Emitter> |
| 5093 | const RecordType *Compiler<Emitter>::getRecordTy(QualType Ty) { |
| 5094 | if (const PointerType *PT = dyn_cast<PointerType>(Val&: Ty)) |
| 5095 | return PT->getPointeeType()->getAsCanonical<RecordType>(); |
| 5096 | return Ty->getAsCanonical<RecordType>(); |
| 5097 | } |
| 5098 | |
| 5099 | template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) { |
| 5100 | if (const auto *RecordTy = getRecordTy(Ty)) |
| 5101 | return getRecord(RecordTy->getDecl()->getDefinitionOrSelf()); |
| 5102 | return nullptr; |
| 5103 | } |
| 5104 | |
| 5105 | template <class Emitter> |
| 5106 | Record *Compiler<Emitter>::getRecord(const RecordDecl *RD) { |
| 5107 | return P.getOrCreateRecord(RD); |
| 5108 | } |
| 5109 | |
| 5110 | template <class Emitter> |
| 5111 | const Function *Compiler<Emitter>::getFunction(const FunctionDecl *FD) { |
| 5112 | return Ctx.getOrCreateFunction(FuncDecl: FD); |
| 5113 | } |
| 5114 | |
| 5115 | template <class Emitter> |
| 5116 | bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) { |
| 5117 | LocalScope<Emitter> RootScope(this, ScopeKind::FullExpression); |
| 5118 | |
| 5119 | auto maybeDestroyLocals = [&]() -> bool { |
| 5120 | if (DestroyToplevelScope) |
| 5121 | return RootScope.destroyLocals() && this->emitCheckAllocations(E); |
| 5122 | return this->emitCheckAllocations(E); |
| 5123 | }; |
| 5124 | |
| 5125 | // Void expressions. |
| 5126 | if (E->getType()->isVoidType()) { |
| 5127 | if (!visit(E)) |
| 5128 | return false; |
| 5129 | return this->emitRetVoid(E) && maybeDestroyLocals(); |
| 5130 | } |
| 5131 | |
| 5132 | // Expressions with a primitive return type. |
| 5133 | if (OptPrimType T = classify(E)) { |
| 5134 | if (!visit(E)) |
| 5135 | return false; |
| 5136 | |
| 5137 | return this->emitRet(*T, E) && maybeDestroyLocals(); |
| 5138 | } |
| 5139 | |
| 5140 | // Expressions with a composite return type. |
| 5141 | // For us, that means everything we don't |
| 5142 | // have a PrimType for. |
| 5143 | if (UnsignedOrNone LocalOffset = this->allocateLocal(Src: E)) { |
| 5144 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalOffset)); |
| 5145 | if (!this->emitGetPtrLocal(*LocalOffset, E)) |
| 5146 | return false; |
| 5147 | |
| 5148 | if (!visitInitializer(E)) |
| 5149 | return false; |
| 5150 | // We are destroying the locals AFTER the Ret op. |
| 5151 | // The Ret op needs to copy the (alive) values, but the |
| 5152 | // destructors may still turn the entire expression invalid. |
| 5153 | return this->emitRetValue(E) && maybeDestroyLocals(); |
| 5154 | } |
| 5155 | |
| 5156 | return maybeDestroyLocals() && false; |
| 5157 | } |
| 5158 | |
| 5159 | template <class Emitter> |
| 5160 | bool Compiler<Emitter>::visitLValueExpr(const Expr *E, |
| 5161 | bool DestroyToplevelScope) { |
| 5162 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5163 | /*NewInitializing=*/false, /*ToLValue=*/true); |
| 5164 | |
| 5165 | return this->visitExpr(E, DestroyToplevelScope); |
| 5166 | } |
| 5167 | |
| 5168 | template <class Emitter> |
| 5169 | VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD) { |
| 5170 | |
| 5171 | auto R = this->visitVarDecl(VD, Init: VD->getInit(), /*Toplevel=*/true); |
| 5172 | |
| 5173 | if (R.notCreated()) |
| 5174 | return R; |
| 5175 | |
| 5176 | if (R) |
| 5177 | return true; |
| 5178 | |
| 5179 | if (!R && Context::shouldBeGloballyIndexed(VD)) { |
| 5180 | if (auto GlobalIndex = P.getGlobal(VD)) { |
| 5181 | Block *GlobalBlock = P.getGlobal(Idx: *GlobalIndex); |
| 5182 | auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>(); |
| 5183 | |
| 5184 | GD.InitState = GlobalInitState::InitializerFailed; |
| 5185 | GlobalBlock->invokeDtor(); |
| 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | return R; |
| 5190 | } |
| 5191 | |
| 5192 | /// Toplevel visitDeclAndReturn(). |
| 5193 | /// We get here from evaluateAsInitializer(). |
| 5194 | /// We need to evaluate the initializer and return its value. |
| 5195 | template <class Emitter> |
| 5196 | bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init, |
| 5197 | bool ConstantContext) { |
| 5198 | // We only create variables if we're evaluating in a constant context. |
| 5199 | // Otherwise, just evaluate the initializer and return it. |
| 5200 | if (!ConstantContext) { |
| 5201 | DeclScope<Emitter> LS(this, VD); |
| 5202 | if (!this->visit(E: Init)) |
| 5203 | return false; |
| 5204 | return this->emitRet(classify(Init).value_or(PT_Ptr), VD) && |
| 5205 | LS.destroyLocals() && this->emitCheckAllocations(VD); |
| 5206 | } |
| 5207 | |
| 5208 | LocalScope<Emitter> VDScope(this); |
| 5209 | if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true)) |
| 5210 | return false; |
| 5211 | |
| 5212 | OptPrimType VarT = classify(VD->getType()); |
| 5213 | if (Context::shouldBeGloballyIndexed(VD)) { |
| 5214 | auto GlobalIndex = P.getGlobal(VD); |
| 5215 | assert(GlobalIndex); // visitVarDecl() didn't return false. |
| 5216 | if (VarT) { |
| 5217 | if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD)) |
| 5218 | return false; |
| 5219 | } else { |
| 5220 | if (!this->emitGetPtrGlobal(*GlobalIndex, VD)) |
| 5221 | return false; |
| 5222 | } |
| 5223 | } else { |
| 5224 | auto Local = Locals.find(Val: VD); |
| 5225 | assert(Local != Locals.end()); // Same here. |
| 5226 | if (VarT) { |
| 5227 | if (!this->emitGetLocal(*VarT, Local->second.Offset, VD)) |
| 5228 | return false; |
| 5229 | } else { |
| 5230 | if (!this->emitGetPtrLocal(Local->second.Offset, VD)) |
| 5231 | return false; |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | // Return the value. |
| 5236 | if (!this->emitRet(VarT.value_or(PT: PT_Ptr), VD)) { |
| 5237 | // If the Ret above failed and this is a global variable, mark it as |
| 5238 | // uninitialized, even everything else succeeded. |
| 5239 | if (Context::shouldBeGloballyIndexed(VD)) { |
| 5240 | auto GlobalIndex = P.getGlobal(VD); |
| 5241 | assert(GlobalIndex); |
| 5242 | Block *GlobalBlock = P.getGlobal(Idx: *GlobalIndex); |
| 5243 | auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>(); |
| 5244 | |
| 5245 | GD.InitState = GlobalInitState::InitializerFailed; |
| 5246 | GlobalBlock->invokeDtor(); |
| 5247 | } |
| 5248 | return false; |
| 5249 | } |
| 5250 | |
| 5251 | return VDScope.destroyLocals() && this->emitCheckAllocations(VD); |
| 5252 | } |
| 5253 | |
| 5254 | template <class Emitter> |
| 5255 | VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD, |
| 5256 | const Expr *Init, |
| 5257 | bool Toplevel) { |
| 5258 | QualType VarTy = VD->getType(); |
| 5259 | // We don't know what to do with these, so just return false. |
| 5260 | if (VarTy.isNull()) |
| 5261 | return false; |
| 5262 | |
| 5263 | // This case is EvalEmitter-only. If we won't create any instructions for the |
| 5264 | // initializer anyway, don't bother creating the variable in the first place. |
| 5265 | if (!this->isActive()) |
| 5266 | return VarCreationState::NotCreated(); |
| 5267 | |
| 5268 | OptPrimType VarT = classify(VD->getType()); |
| 5269 | |
| 5270 | if (Init && Init->isValueDependent()) |
| 5271 | return false; |
| 5272 | |
| 5273 | if (Context::shouldBeGloballyIndexed(VD)) { |
| 5274 | auto checkDecl = [&]() -> bool { |
| 5275 | bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal(); |
| 5276 | return !NeedsOp || this->emitCheckDecl(VD, VD); |
| 5277 | }; |
| 5278 | |
| 5279 | DeclScope<Emitter> LocalScope(this, VD); |
| 5280 | UnsignedOrNone GlobalIndex = P.getGlobal(VD); |
| 5281 | if (GlobalIndex) { |
| 5282 | // The global was previously created but the initializer failed. |
| 5283 | if (!P.getGlobal(Idx: *GlobalIndex)->isInitialized()) |
| 5284 | return false; |
| 5285 | // We've already seen and initialized this global. |
| 5286 | if (P.isGlobalInitialized(Index: *GlobalIndex)) |
| 5287 | return checkDecl(); |
| 5288 | // The previous attempt at initialization might've been unsuccessful, |
| 5289 | // so let's try this one. |
| 5290 | } else if ((GlobalIndex = |
| 5291 | P.createGlobal(VD, Init, IsConstexprUnknown: VariablesAreConstexprUnknown))) { |
| 5292 | } else { |
| 5293 | return false; |
| 5294 | } |
| 5295 | if (!Init) |
| 5296 | return true; |
| 5297 | |
| 5298 | if (!checkDecl()) |
| 5299 | return false; |
| 5300 | |
| 5301 | if (VarT) { |
| 5302 | if (!this->visit(E: Init)) |
| 5303 | return false; |
| 5304 | |
| 5305 | return this->emitInitGlobal(*VarT, *GlobalIndex, VD); |
| 5306 | } |
| 5307 | |
| 5308 | if (!this->emitGetPtrGlobal(*GlobalIndex, Init)) |
| 5309 | return false; |
| 5310 | |
| 5311 | if (!this->emitStartInit(Init)) |
| 5312 | return false; |
| 5313 | |
| 5314 | if (!visitInitializer(E: Init)) |
| 5315 | return false; |
| 5316 | |
| 5317 | if (!this->emitEndInit(Init)) |
| 5318 | return false; |
| 5319 | |
| 5320 | return this->emitFinishInitGlobal(Init); |
| 5321 | } |
| 5322 | // Local variables. |
| 5323 | InitLinkScope<Emitter> ILS(this, InitLink::Decl(D: VD)); |
| 5324 | |
| 5325 | if (VarT) { |
| 5326 | unsigned Offset = this->allocateLocalPrimitive( |
| 5327 | Src: VD, Ty: *VarT, IsConst: VarTy.isConstQualified(), IsVolatile: VarTy.isVolatileQualified(), |
| 5328 | SC: ScopeKind::Block); |
| 5329 | |
| 5330 | if (!Init || Init->getType()->isVoidType()) |
| 5331 | return true; |
| 5332 | |
| 5333 | // If this is a toplevel declaration, create a scope for the |
| 5334 | // initializer. |
| 5335 | if (Toplevel) { |
| 5336 | LocalScope<Emitter> Scope(this); |
| 5337 | if (!this->visit(E: Init)) |
| 5338 | return false; |
| 5339 | return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals(); |
| 5340 | } |
| 5341 | if (!this->visit(E: Init)) |
| 5342 | return false; |
| 5343 | |
| 5344 | if (VarTy->isReferenceType()) { |
| 5345 | // [C++26][decl.ref] |
| 5346 | // The object designated by such a glvalue can be outside its lifetime |
| 5347 | // Because a null pointer value or a pointer past the end of an object |
| 5348 | // does not point to an object, a reference in a well-defined program |
| 5349 | // cannot refer to such things; |
| 5350 | assert(classifyPrim(VarTy) == PT_Ptr); |
| 5351 | if (!this->emitCheckRefInit(Init)) |
| 5352 | return false; |
| 5353 | } |
| 5354 | |
| 5355 | return this->emitSetLocal(*VarT, Offset, VD); |
| 5356 | } |
| 5357 | // Local composite variables. |
| 5358 | if (UnsignedOrNone Offset = |
| 5359 | this->allocateLocal(Src: VD, Ty: VarTy, SC: ScopeKind::Block)) { |
| 5360 | if (!Init) |
| 5361 | return true; |
| 5362 | |
| 5363 | if (!this->emitGetPtrLocal(*Offset, Init)) |
| 5364 | return false; |
| 5365 | |
| 5366 | return visitInitializerPop(E: Init); |
| 5367 | } |
| 5368 | return false; |
| 5369 | } |
| 5370 | |
| 5371 | template <class Emitter> |
| 5372 | bool Compiler<Emitter>::visitDtorCall(const VarDecl *VD, const APValue &Value) { |
| 5373 | assert(!canClassify(VD->getType())); |
| 5374 | |
| 5375 | DeclScope<Emitter> LocalScope(this, VD); |
| 5376 | // Create a local variable to use as the instance. |
| 5377 | QualType Ty = VD->getType(); |
| 5378 | Descriptor *D = |
| 5379 | P.createDescriptor(D: VD, Ty: Ty.getTypePtr(), MDSize: Descriptor::InlineDescMD, |
| 5380 | /*IsConst=*/Ty.isConstQualified(), |
| 5381 | /*IsTemporary=*/false, /*IsMutable=*/false, |
| 5382 | /*IsVolatile=*/Ty.isVolatileQualified(), Init: nullptr); |
| 5383 | if (!D) |
| 5384 | return false; |
| 5385 | |
| 5386 | // FIXME: Would be nice if we didn't allocate the descriptor at all in this |
| 5387 | // case. |
| 5388 | if (D->hasTrivialDtor()) |
| 5389 | return true; |
| 5390 | |
| 5391 | Scope::Local Local = this->createLocal(D); |
| 5392 | Locals.insert(KV: {VD, Local}); |
| 5393 | VarScope->addForScopeKind(Local, ScopeKind::Block); |
| 5394 | |
| 5395 | if (!this->emitGetPtrLocal(Local.Offset, VD)) |
| 5396 | return false; |
| 5397 | |
| 5398 | if (!this->visitAPValueInitializer(Val: Value, Info: VD, T: Ty)) |
| 5399 | return false; |
| 5400 | |
| 5401 | return this->emitDestructionPop(Desc: D, Loc: VD); |
| 5402 | } |
| 5403 | |
| 5404 | template <class Emitter> |
| 5405 | bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType, |
| 5406 | SourceInfo Info) { |
| 5407 | assert(!Val.isIndeterminate() && "Needs to be checked before" ); |
| 5408 | assert(!DiscardResult); |
| 5409 | if (Val.isInt()) |
| 5410 | return this->emitConst(Val.getInt(), ValType, Info); |
| 5411 | if (Val.isFloat()) { |
| 5412 | APFloat F = Val.getFloat(); |
| 5413 | return this->emitFloat(F, Info); |
| 5414 | } |
| 5415 | |
| 5416 | if (Val.isMemberPointer()) { |
| 5417 | if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) { |
| 5418 | if (!this->emitGetMemberPtr(MemberDecl, Info)) |
| 5419 | return false; |
| 5420 | |
| 5421 | bool IsDerived = Val.isMemberPointerToDerivedMember(); |
| 5422 | // Apply the member pointer path. |
| 5423 | for (const CXXRecordDecl *PathEntry : Val.getMemberPointerPath()) { |
| 5424 | if (!this->emitCopyMemberPtrPath(PathEntry, IsDerived, Info)) |
| 5425 | return false; |
| 5426 | } |
| 5427 | |
| 5428 | return true; |
| 5429 | } |
| 5430 | return this->emitNullMemberPtr(0, nullptr, Info); |
| 5431 | } |
| 5432 | |
| 5433 | if (Val.isLValue()) { |
| 5434 | if (Val.isNullPointer()) |
| 5435 | return this->emitNull(ValType, 0, nullptr, Info); |
| 5436 | |
| 5437 | APValue::LValueBase Base = Val.getLValueBase(); |
| 5438 | ArrayRef<APValue::LValuePathEntry> Path = Val.getLValuePath(); |
| 5439 | |
| 5440 | if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>()) |
| 5441 | return this->visit(E: BaseExpr); |
| 5442 | if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) { |
| 5443 | if (!this->visitDeclRef(D: VD, E: Info.asExpr())) |
| 5444 | return false; |
| 5445 | |
| 5446 | QualType EntryType = VD->getType(); |
| 5447 | for (auto &Entry : Path) { |
| 5448 | if (EntryType->isArrayType()) { |
| 5449 | uint64_t Index = Entry.getAsArrayIndex(); |
| 5450 | QualType ElemType = |
| 5451 | EntryType->getAsArrayTypeUnsafe()->getElementType(); |
| 5452 | if (!this->emitConst(Index, PT_Uint64, Info)) |
| 5453 | return false; |
| 5454 | if (!this->emitArrayElemPtrPop(PT_Uint64, Info)) |
| 5455 | return false; |
| 5456 | EntryType = ElemType; |
| 5457 | } else { |
| 5458 | assert(EntryType->isRecordType()); |
| 5459 | const Record *EntryRecord = getRecord(EntryType); |
| 5460 | if (!EntryRecord) { |
| 5461 | assert(false); |
| 5462 | |
| 5463 | return false; |
| 5464 | } |
| 5465 | |
| 5466 | const Decl *BaseOrMember = Entry.getAsBaseOrMember().getPointer(); |
| 5467 | if (const auto *FD = dyn_cast<FieldDecl>(Val: BaseOrMember)) { |
| 5468 | unsigned EntryOffset = EntryRecord->getField(FD)->Offset; |
| 5469 | if (!this->emitGetPtrFieldPop(EntryOffset, Info)) |
| 5470 | return false; |
| 5471 | EntryType = FD->getType(); |
| 5472 | } else { |
| 5473 | const auto *Base = cast<CXXRecordDecl>(Val: BaseOrMember); |
| 5474 | unsigned BaseOffset = EntryRecord->getBase(FD: Base)->Offset; |
| 5475 | if (!this->emitGetPtrBasePop(BaseOffset, /*NullOK=*/false, Info)) |
| 5476 | return false; |
| 5477 | EntryType = Ctx.getASTContext().getCanonicalTagType(TD: Base); |
| 5478 | } |
| 5479 | } |
| 5480 | } |
| 5481 | |
| 5482 | return true; |
| 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | return false; |
| 5487 | } |
| 5488 | |
| 5489 | template <class Emitter> |
| 5490 | bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val, |
| 5491 | SourceInfo Info, QualType T) { |
| 5492 | if (Val.isStruct()) { |
| 5493 | const Record *R = this->getRecord(T); |
| 5494 | assert(R); |
| 5495 | for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) { |
| 5496 | const APValue &F = Val.getStructField(i: I); |
| 5497 | if (F.isIndeterminate()) |
| 5498 | continue; |
| 5499 | const Record::Field *RF = R->getField(I); |
| 5500 | QualType FieldType = RF->Decl->getType(); |
| 5501 | |
| 5502 | // Fields. |
| 5503 | if (OptPrimType PT = classify(FieldType)) { |
| 5504 | if (!this->visitAPValue(Val: F, ValType: *PT, Info)) |
| 5505 | return false; |
| 5506 | if (!this->emitInitField(*PT, RF->Offset, Info)) |
| 5507 | return false; |
| 5508 | } else { |
| 5509 | if (!this->emitGetPtrField(RF->Offset, Info)) |
| 5510 | return false; |
| 5511 | if (!this->visitAPValueInitializer(Val: F, Info, T: FieldType)) |
| 5512 | return false; |
| 5513 | if (!this->emitFinishInitPop(Info)) |
| 5514 | return false; |
| 5515 | } |
| 5516 | } |
| 5517 | |
| 5518 | // Bases. |
| 5519 | for (unsigned I = 0, N = Val.getStructNumBases(); I != N; ++I) { |
| 5520 | // FIXME: APValue doesn't know about virtual bases. |
| 5521 | // We simply assume that if the APValue has more bases than the Record, |
| 5522 | // those additional bases must be virtual. |
| 5523 | if (I >= R->getNumBases()) |
| 5524 | break; |
| 5525 | const APValue &B = Val.getStructBase(i: I); |
| 5526 | if (B.isIndeterminate()) |
| 5527 | continue; |
| 5528 | const Record::Base *RB = R->getBase(I); |
| 5529 | QualType BaseType = Ctx.getASTContext().getCanonicalTagType(TD: RB->Decl); |
| 5530 | |
| 5531 | if (!this->emitGetPtrBase(RB->Offset, Info)) |
| 5532 | return false; |
| 5533 | if (!this->visitAPValueInitializer(Val: B, Info, T: BaseType)) |
| 5534 | return false; |
| 5535 | if (!this->emitFinishInitPop(Info)) |
| 5536 | return false; |
| 5537 | } |
| 5538 | |
| 5539 | return true; |
| 5540 | } |
| 5541 | if (Val.isUnion()) { |
| 5542 | const FieldDecl *UnionField = Val.getUnionField(); |
| 5543 | if (!UnionField) |
| 5544 | return true; |
| 5545 | const Record *R = this->getRecord(T); |
| 5546 | assert(R); |
| 5547 | const APValue &F = Val.getUnionValue(); |
| 5548 | if (F.isIndeterminate()) |
| 5549 | return true; |
| 5550 | const Record::Field *RF = R->getField(FD: UnionField); |
| 5551 | QualType FieldType = RF->Decl->getType(); |
| 5552 | |
| 5553 | if (OptPrimType PT = classify(FieldType)) { |
| 5554 | if (!this->visitAPValue(Val: F, ValType: *PT, Info)) |
| 5555 | return false; |
| 5556 | if (RF->isBitField()) |
| 5557 | return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(), |
| 5558 | Info); |
| 5559 | return this->emitInitFieldActivate(*PT, RF->Offset, Info); |
| 5560 | } |
| 5561 | |
| 5562 | if (!this->emitGetPtrField(RF->Offset, Info)) |
| 5563 | return false; |
| 5564 | if (!this->emitActivate(Info)) |
| 5565 | return false; |
| 5566 | if (!this->visitAPValueInitializer(Val: F, Info, T: FieldType)) |
| 5567 | return false; |
| 5568 | return this->emitPopPtr(Info); |
| 5569 | } |
| 5570 | if (Val.isArray()) { |
| 5571 | unsigned InitializedElems = Val.getArrayInitializedElts(); |
| 5572 | const auto *ArrType = T->getAsArrayTypeUnsafe(); |
| 5573 | QualType ElemType = ArrType->getElementType(); |
| 5574 | OptPrimType ElemT = classify(ElemType); |
| 5575 | |
| 5576 | for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) { |
| 5577 | const APValue &Elem = A >= InitializedElems |
| 5578 | ? Val.getArrayFiller() |
| 5579 | : Val.getArrayInitializedElt(I: A); |
| 5580 | if (Elem.isIndeterminate()) |
| 5581 | continue; |
| 5582 | |
| 5583 | if (ElemT) { |
| 5584 | if (!this->visitAPValue(Val: Elem, ValType: *ElemT, Info)) |
| 5585 | return false; |
| 5586 | if (!this->emitInitElem(*ElemT, A, Info)) |
| 5587 | return false; |
| 5588 | } else { |
| 5589 | if (!this->emitConstUint32(A, Info)) |
| 5590 | return false; |
| 5591 | if (!this->emitArrayElemPtrUint32(Info)) |
| 5592 | return false; |
| 5593 | if (!this->visitAPValueInitializer(Val: Elem, Info, T: ElemType)) |
| 5594 | return false; |
| 5595 | if (!this->emitPopPtr(Info)) |
| 5596 | return false; |
| 5597 | } |
| 5598 | } |
| 5599 | return true; |
| 5600 | } |
| 5601 | // TODO: Other types. |
| 5602 | |
| 5603 | return false; |
| 5604 | } |
| 5605 | |
| 5606 | template <class Emitter> |
| 5607 | bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E, |
| 5608 | unsigned BuiltinID) { |
| 5609 | if (BuiltinID == Builtin::BI__builtin_constant_p) { |
| 5610 | // Void argument is always invalid and harder to handle later. |
| 5611 | if (E->getArg(Arg: 0)->getType()->isVoidType()) { |
| 5612 | if (DiscardResult) |
| 5613 | return true; |
| 5614 | return this->emitConst(0, E); |
| 5615 | } |
| 5616 | |
| 5617 | if (!this->emitStartSpeculation(E)) |
| 5618 | return false; |
| 5619 | LabelTy EndLabel = this->getLabel(); |
| 5620 | if (!this->speculate(E, EndLabel)) |
| 5621 | return false; |
| 5622 | if (!this->emitEndSpeculation(E)) |
| 5623 | return false; |
| 5624 | this->fallthrough(EndLabel); |
| 5625 | if (DiscardResult) |
| 5626 | return this->emitPop(classifyPrim(E), E); |
| 5627 | return true; |
| 5628 | } |
| 5629 | |
| 5630 | // For these, we're expected to ultimately return an APValue pointing |
| 5631 | // to the CallExpr. This is needed to get the correct codegen. |
| 5632 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 5633 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString || |
| 5634 | BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant || |
| 5635 | BuiltinID == Builtin::BI__builtin_function_start) { |
| 5636 | if (DiscardResult) |
| 5637 | return true; |
| 5638 | return this->emitDummyPtr(D: E, E); |
| 5639 | } |
| 5640 | |
| 5641 | QualType ReturnType = E->getType(); |
| 5642 | OptPrimType ReturnT = classify(E); |
| 5643 | |
| 5644 | // Non-primitive return type. Prepare storage. |
| 5645 | if (!Initializing && !ReturnT && !ReturnType->isVoidType()) { |
| 5646 | UnsignedOrNone LocalIndex = allocateLocal(Src: E); |
| 5647 | if (!LocalIndex) |
| 5648 | return false; |
| 5649 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 5650 | return false; |
| 5651 | } |
| 5652 | |
| 5653 | // Prepare function arguments including special cases. |
| 5654 | switch (BuiltinID) { |
| 5655 | case Builtin::BI__builtin_object_size: |
| 5656 | case Builtin::BI__builtin_dynamic_object_size: { |
| 5657 | assert(E->getNumArgs() == 2); |
| 5658 | const Expr *Arg0 = E->getArg(Arg: 0); |
| 5659 | if (Arg0->isGLValue()) { |
| 5660 | if (!this->visit(E: Arg0)) |
| 5661 | return false; |
| 5662 | |
| 5663 | } else { |
| 5664 | if (!this->visitAsLValue(E: Arg0)) |
| 5665 | return false; |
| 5666 | } |
| 5667 | if (!this->visit(E: E->getArg(Arg: 1))) |
| 5668 | return false; |
| 5669 | |
| 5670 | } break; |
| 5671 | case Builtin::BI__assume: |
| 5672 | case Builtin::BI__builtin_assume: |
| 5673 | // Argument is not evaluated. |
| 5674 | break; |
| 5675 | case Builtin::BI__atomic_is_lock_free: |
| 5676 | case Builtin::BI__atomic_always_lock_free: { |
| 5677 | assert(E->getNumArgs() == 2); |
| 5678 | if (!this->visit(E: E->getArg(Arg: 0))) |
| 5679 | return false; |
| 5680 | if (!this->visitAsLValue(E: E->getArg(Arg: 1))) |
| 5681 | return false; |
| 5682 | } break; |
| 5683 | |
| 5684 | default: |
| 5685 | if (!Context::isUnevaluatedBuiltin(ID: BuiltinID)) { |
| 5686 | // Put arguments on the stack. |
| 5687 | for (const auto *Arg : E->arguments()) { |
| 5688 | if (!this->visit(E: Arg)) |
| 5689 | return false; |
| 5690 | } |
| 5691 | } |
| 5692 | } |
| 5693 | |
| 5694 | if (!this->emitCallBI(E, BuiltinID, E)) |
| 5695 | return false; |
| 5696 | |
| 5697 | if (DiscardResult && !ReturnType->isVoidType()) |
| 5698 | return this->emitPop(ReturnT.value_or(PT: PT_Ptr), E); |
| 5699 | |
| 5700 | return true; |
| 5701 | } |
| 5702 | |
| 5703 | template <class Emitter> |
| 5704 | bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) { |
| 5705 | if (E->containsErrors()) |
| 5706 | return false; |
| 5707 | const FunctionDecl *FuncDecl = E->getDirectCallee(); |
| 5708 | |
| 5709 | if (FuncDecl) { |
| 5710 | if (unsigned BuiltinID = FuncDecl->getBuiltinID()) |
| 5711 | return VisitBuiltinCallExpr(E, BuiltinID); |
| 5712 | |
| 5713 | // Calls to replaceable operator new/operator delete. |
| 5714 | if (FuncDecl->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
| 5715 | if (FuncDecl->getDeclName().isAnyOperatorNew()) |
| 5716 | return VisitBuiltinCallExpr(E, BuiltinID: Builtin::BI__builtin_operator_new); |
| 5717 | assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 5718 | FuncDecl->getDeclName().getCXXOverloadedOperator() == |
| 5719 | OO_Array_Delete); |
| 5720 | return VisitBuiltinCallExpr(E, BuiltinID: Builtin::BI__builtin_operator_delete); |
| 5721 | } |
| 5722 | |
| 5723 | // Explicit calls to trivial destructors |
| 5724 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(Val: FuncDecl); |
| 5725 | DD && DD->isTrivial()) { |
| 5726 | const auto *MemberCall = cast<CXXMemberCallExpr>(Val: E); |
| 5727 | if (!this->visit(E: MemberCall->getImplicitObjectArgument())) |
| 5728 | return false; |
| 5729 | return this->emitCheckDestruction(E) && this->emitEndLifetime(E) && |
| 5730 | this->emitPopPtr(E); |
| 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | LocalScope<Emitter> CallScope(this, ScopeKind::Call); |
| 5735 | |
| 5736 | QualType ReturnType = E->getCallReturnType(Ctx: Ctx.getASTContext()); |
| 5737 | OptPrimType T = classify(ReturnType); |
| 5738 | bool HasRVO = !ReturnType->isVoidType() && !T; |
| 5739 | |
| 5740 | if (HasRVO) { |
| 5741 | if (DiscardResult) { |
| 5742 | // If we need to discard the return value but the function returns its |
| 5743 | // value via an RVO pointer, we need to create one such pointer just |
| 5744 | // for this call. |
| 5745 | if (UnsignedOrNone LocalIndex = allocateLocal(Src: E)) { |
| 5746 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 5747 | return false; |
| 5748 | } |
| 5749 | } else { |
| 5750 | // We need the result. Prepare a pointer to return or |
| 5751 | // dup the current one. |
| 5752 | if (!Initializing) { |
| 5753 | if (UnsignedOrNone LocalIndex = allocateLocal(Src: E)) { |
| 5754 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 5755 | return false; |
| 5756 | } |
| 5757 | } |
| 5758 | if (!this->emitDupPtr(E)) |
| 5759 | return false; |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | SmallVector<const Expr *, 8> Args(ArrayRef(E->getArgs(), E->getNumArgs())); |
| 5764 | |
| 5765 | bool IsAssignmentOperatorCall = false; |
| 5766 | bool ActivateLHS = false; |
| 5767 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E); |
| 5768 | OCE && OCE->isAssignmentOp()) { |
| 5769 | // Just like with regular assignments, we need to special-case assignment |
| 5770 | // operators here and evaluate the RHS (the second arg) before the LHS (the |
| 5771 | // first arg). We fix this by using a Flip op later. |
| 5772 | assert(Args.size() == 2); |
| 5773 | const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl(); |
| 5774 | ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor(); |
| 5775 | IsAssignmentOperatorCall = true; |
| 5776 | std::reverse(first: Args.begin(), last: Args.end()); |
| 5777 | } |
| 5778 | // Calling a static operator will still |
| 5779 | // pass the instance, but we don't need it. |
| 5780 | // Discard it here. |
| 5781 | if (isa<CXXOperatorCallExpr>(Val: E)) { |
| 5782 | if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl); |
| 5783 | MD && MD->isStatic()) { |
| 5784 | if (!this->discard(E: E->getArg(Arg: 0))) |
| 5785 | return false; |
| 5786 | // Drop first arg. |
| 5787 | Args.erase(CI: Args.begin()); |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | bool Devirtualized = false; |
| 5792 | UnsignedOrNone CalleeOffset = std::nullopt; |
| 5793 | // Add the (optional, implicit) This pointer. |
| 5794 | if (const auto *MC = dyn_cast<CXXMemberCallExpr>(Val: E)) { |
| 5795 | if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) { |
| 5796 | // If we end up creating a CallPtr op for this, we need the base of the |
| 5797 | // member pointer as the instance pointer, and later extract the function |
| 5798 | // decl as the function pointer. |
| 5799 | const Expr *Callee = E->getCallee(); |
| 5800 | CalleeOffset = |
| 5801 | this->allocateLocalPrimitive(Src: Callee, Ty: PT_MemberPtr, /*IsConst=*/true); |
| 5802 | if (!this->visit(E: Callee)) |
| 5803 | return false; |
| 5804 | if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E)) |
| 5805 | return false; |
| 5806 | if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E)) |
| 5807 | return false; |
| 5808 | if (!this->emitGetMemberPtrBase(E)) |
| 5809 | return false; |
| 5810 | } else { |
| 5811 | const auto *InstancePtr = MC->getImplicitObjectArgument(); |
| 5812 | if (isa_and_nonnull<CXXDestructorDecl>(Val: CompilingFunction) || |
| 5813 | isa_and_nonnull<CXXConstructorDecl>(Val: CompilingFunction)) { |
| 5814 | const auto *Stripped = stripCheckedDerivedToBaseCasts(E: InstancePtr); |
| 5815 | if (isa<CXXThisExpr>(Val: Stripped)) { |
| 5816 | FuncDecl = |
| 5817 | cast<CXXMethodDecl>(Val: FuncDecl)->getCorrespondingMethodInClass( |
| 5818 | RD: Stripped->getType()->getPointeeType()->getAsCXXRecordDecl()); |
| 5819 | Devirtualized = true; |
| 5820 | if (!this->visit(E: Stripped)) |
| 5821 | return false; |
| 5822 | } else { |
| 5823 | if (!this->visit(E: InstancePtr)) |
| 5824 | return false; |
| 5825 | } |
| 5826 | } else { |
| 5827 | if (!this->visit(E: InstancePtr)) |
| 5828 | return false; |
| 5829 | } |
| 5830 | } |
| 5831 | } else if (const auto *PD = |
| 5832 | dyn_cast<CXXPseudoDestructorExpr>(Val: E->getCallee())) { |
| 5833 | if (!this->emitCheckPseudoDtor(E)) |
| 5834 | return false; |
| 5835 | const Expr *Base = PD->getBase(); |
| 5836 | // E.g. `using T = int; 0.~T();`. |
| 5837 | if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr) |
| 5838 | return this->discard(E: Base); |
| 5839 | if (!this->visit(E: Base)) |
| 5840 | return false; |
| 5841 | return this->emitEndLifetimePop(E); |
| 5842 | } else if (!FuncDecl) { |
| 5843 | const Expr *Callee = E->getCallee(); |
| 5844 | CalleeOffset = |
| 5845 | this->allocateLocalPrimitive(Src: Callee, Ty: PT_Ptr, /*IsConst=*/true); |
| 5846 | if (!this->visit(E: Callee)) |
| 5847 | return false; |
| 5848 | if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E)) |
| 5849 | return false; |
| 5850 | } |
| 5851 | |
| 5852 | if (!this->visitCallArgs(Args, FuncDecl, Activate: ActivateLHS, |
| 5853 | IsOperatorCall: isa<CXXOperatorCallExpr>(Val: E))) |
| 5854 | return false; |
| 5855 | |
| 5856 | // Undo the argument reversal we did earlier. |
| 5857 | if (IsAssignmentOperatorCall) { |
| 5858 | assert(Args.size() == 2); |
| 5859 | PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr); |
| 5860 | PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr); |
| 5861 | if (!this->emitFlip(Arg2T, Arg1T, E)) |
| 5862 | return false; |
| 5863 | } |
| 5864 | |
| 5865 | if (FuncDecl) { |
| 5866 | const Function *Func = getFunction(FD: FuncDecl); |
| 5867 | if (!Func) |
| 5868 | return false; |
| 5869 | |
| 5870 | // In error cases, the function may be called with fewer arguments than |
| 5871 | // parameters. |
| 5872 | if (E->getNumArgs() < Func->getNumWrittenParams()) |
| 5873 | return false; |
| 5874 | |
| 5875 | assert(HasRVO == Func->hasRVO()); |
| 5876 | |
| 5877 | bool HasQualifier = false; |
| 5878 | if (const auto *ME = dyn_cast<MemberExpr>(Val: E->getCallee())) |
| 5879 | HasQualifier = ME->hasQualifier(); |
| 5880 | |
| 5881 | bool IsVirtual = false; |
| 5882 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FuncDecl)) |
| 5883 | IsVirtual = !Devirtualized && MD->isVirtual(); |
| 5884 | |
| 5885 | // In any case call the function. The return value will end up on the stack |
| 5886 | // and if the function has RVO, we already have the pointer on the stack to |
| 5887 | // write the result into. |
| 5888 | if (IsVirtual && !HasQualifier) { |
| 5889 | uint32_t VarArgSize = 0; |
| 5890 | unsigned NumParams = |
| 5891 | Func->getNumWrittenParams() + |
| 5892 | (isa<CXXOperatorCallExpr>(Val: E) && Func->hasImplicitThisParam()); |
| 5893 | for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) |
| 5894 | VarArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr))); |
| 5895 | |
| 5896 | if (!this->emitCallVirt(Func, VarArgSize, E)) |
| 5897 | return false; |
| 5898 | } else if (Func->isVariadic()) { |
| 5899 | uint32_t VarArgSize = 0; |
| 5900 | unsigned NumParams = |
| 5901 | Func->getNumWrittenParams() + |
| 5902 | (isa<CXXOperatorCallExpr>(Val: E) && Func->hasImplicitThisParam()); |
| 5903 | for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) |
| 5904 | VarArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr))); |
| 5905 | if (!this->emitCallVar(Func, VarArgSize, E)) |
| 5906 | return false; |
| 5907 | } else { |
| 5908 | if (!this->emitCall(Func, 0, E)) |
| 5909 | return false; |
| 5910 | } |
| 5911 | } else { |
| 5912 | // Indirect call. Visit the callee, which will leave a FunctionPointer on |
| 5913 | // the stack. Cleanup of the returned value if necessary will be done after |
| 5914 | // the function call completed. |
| 5915 | |
| 5916 | // Sum the size of all args from the call expr. |
| 5917 | uint32_t ArgSize = 0; |
| 5918 | for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) |
| 5919 | ArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr))); |
| 5920 | |
| 5921 | // Get the callee, either from a member pointer or function pointer saved in |
| 5922 | // CalleeOffset. |
| 5923 | if (isa<CXXMemberCallExpr>(Val: E) && CalleeOffset) { |
| 5924 | if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E)) |
| 5925 | return false; |
| 5926 | if (!this->emitGetMemberPtrDecl(E)) |
| 5927 | return false; |
| 5928 | } else { |
| 5929 | if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E)) |
| 5930 | return false; |
| 5931 | } |
| 5932 | if (!this->emitCallPtr(ArgSize, E, E)) |
| 5933 | return false; |
| 5934 | } |
| 5935 | |
| 5936 | // Cleanup for discarded return values. |
| 5937 | if (DiscardResult && !ReturnType->isVoidType() && T) |
| 5938 | return this->emitPop(*T, E) && CallScope.destroyLocals(); |
| 5939 | |
| 5940 | return CallScope.destroyLocals(); |
| 5941 | } |
| 5942 | |
| 5943 | template <class Emitter> |
| 5944 | bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
| 5945 | SourceLocScope<Emitter> SLS(this, E); |
| 5946 | |
| 5947 | return this->delegate(E: E->getExpr()); |
| 5948 | } |
| 5949 | |
| 5950 | template <class Emitter> |
| 5951 | bool Compiler<Emitter>::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
| 5952 | SourceLocScope<Emitter> SLS(this, E); |
| 5953 | |
| 5954 | return this->delegate(E: E->getExpr()); |
| 5955 | } |
| 5956 | |
| 5957 | template <class Emitter> |
| 5958 | bool Compiler<Emitter>::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| 5959 | if (DiscardResult) |
| 5960 | return true; |
| 5961 | |
| 5962 | return this->emitConstBool(E->getValue(), E); |
| 5963 | } |
| 5964 | |
| 5965 | template <class Emitter> |
| 5966 | bool Compiler<Emitter>::VisitCXXNullPtrLiteralExpr( |
| 5967 | const CXXNullPtrLiteralExpr *E) { |
| 5968 | if (DiscardResult) |
| 5969 | return true; |
| 5970 | |
| 5971 | uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType()); |
| 5972 | return this->emitNullPtr(Val, nullptr, E); |
| 5973 | } |
| 5974 | |
| 5975 | template <class Emitter> |
| 5976 | bool Compiler<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) { |
| 5977 | if (DiscardResult) |
| 5978 | return true; |
| 5979 | |
| 5980 | assert(E->getType()->isIntegerType()); |
| 5981 | |
| 5982 | PrimType T = classifyPrim(E->getType()); |
| 5983 | return this->emitZero(T, E); |
| 5984 | } |
| 5985 | |
| 5986 | template <class Emitter> |
| 5987 | bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) { |
| 5988 | if (DiscardResult) |
| 5989 | return true; |
| 5990 | |
| 5991 | if constexpr (!std::is_same_v<Emitter, EvalEmitter>) { |
| 5992 | if (this->LambdaThisCapture.Offset > 0) { |
| 5993 | if (this->LambdaThisCapture.IsPtr) |
| 5994 | return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E); |
| 5995 | return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E); |
| 5996 | } |
| 5997 | } |
| 5998 | |
| 5999 | // In some circumstances, the 'this' pointer does not actually refer to the |
| 6000 | // instance pointer of the current function frame, but e.g. to the declaration |
| 6001 | // currently being initialized. Here we emit the necessary instruction(s) for |
| 6002 | // this scenario. |
| 6003 | if (!InitStackActive || InitStack.empty()) |
| 6004 | return this->emitThis(E); |
| 6005 | |
| 6006 | // If our init stack is, for example: |
| 6007 | // 0 Stack: 3 (decl) |
| 6008 | // 1 Stack: 6 (init list) |
| 6009 | // 2 Stack: 1 (field) |
| 6010 | // 3 Stack: 6 (init list) |
| 6011 | // 4 Stack: 1 (field) |
| 6012 | // |
| 6013 | // We want to find the LAST element in it that's an init list, |
| 6014 | // which is marked with the K_InitList marker. The index right |
| 6015 | // before that points to an init list. We need to find the |
| 6016 | // elements before the K_InitList element that point to a base |
| 6017 | // (e.g. a decl or This), optionally followed by field, elem, etc. |
| 6018 | // In the example above, we want to emit elements [0..2]. |
| 6019 | unsigned StartIndex = 0; |
| 6020 | unsigned EndIndex = 0; |
| 6021 | // Find the init list. |
| 6022 | for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) { |
| 6023 | if (InitStack[StartIndex].Kind == InitLink::K_DIE) { |
| 6024 | EndIndex = StartIndex; |
| 6025 | --StartIndex; |
| 6026 | break; |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | // Walk backwards to find the base. |
| 6031 | for (; StartIndex > 0; --StartIndex) { |
| 6032 | if (InitStack[StartIndex].Kind == InitLink::K_InitList) |
| 6033 | continue; |
| 6034 | |
| 6035 | if (InitStack[StartIndex].Kind != InitLink::K_Field && |
| 6036 | InitStack[StartIndex].Kind != InitLink::K_Elem && |
| 6037 | InitStack[StartIndex].Kind != InitLink::K_DIE) |
| 6038 | break; |
| 6039 | } |
| 6040 | |
| 6041 | if (StartIndex == 0 && EndIndex == 0) |
| 6042 | EndIndex = InitStack.size() - 1; |
| 6043 | |
| 6044 | assert(StartIndex < EndIndex); |
| 6045 | |
| 6046 | // Emit the instructions. |
| 6047 | for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) { |
| 6048 | if (InitStack[I].Kind == InitLink::K_InitList || |
| 6049 | InitStack[I].Kind == InitLink::K_DIE) |
| 6050 | continue; |
| 6051 | if (!InitStack[I].template emit<Emitter>(this, E)) |
| 6052 | return false; |
| 6053 | } |
| 6054 | return true; |
| 6055 | } |
| 6056 | |
| 6057 | template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) { |
| 6058 | switch (S->getStmtClass()) { |
| 6059 | case Stmt::CompoundStmtClass: |
| 6060 | return visitCompoundStmt(S: cast<CompoundStmt>(Val: S)); |
| 6061 | case Stmt::DeclStmtClass: |
| 6062 | return visitDeclStmt(DS: cast<DeclStmt>(Val: S), /*EvaluateConditionDecl=*/true); |
| 6063 | case Stmt::ReturnStmtClass: |
| 6064 | return visitReturnStmt(RS: cast<ReturnStmt>(Val: S)); |
| 6065 | case Stmt::IfStmtClass: |
| 6066 | return visitIfStmt(IS: cast<IfStmt>(Val: S)); |
| 6067 | case Stmt::WhileStmtClass: |
| 6068 | return visitWhileStmt(S: cast<WhileStmt>(Val: S)); |
| 6069 | case Stmt::DoStmtClass: |
| 6070 | return visitDoStmt(S: cast<DoStmt>(Val: S)); |
| 6071 | case Stmt::ForStmtClass: |
| 6072 | return visitForStmt(S: cast<ForStmt>(Val: S)); |
| 6073 | case Stmt::CXXForRangeStmtClass: |
| 6074 | return visitCXXForRangeStmt(S: cast<CXXForRangeStmt>(Val: S)); |
| 6075 | case Stmt::BreakStmtClass: |
| 6076 | return visitBreakStmt(S: cast<BreakStmt>(Val: S)); |
| 6077 | case Stmt::ContinueStmtClass: |
| 6078 | return visitContinueStmt(S: cast<ContinueStmt>(Val: S)); |
| 6079 | case Stmt::SwitchStmtClass: |
| 6080 | return visitSwitchStmt(S: cast<SwitchStmt>(Val: S)); |
| 6081 | case Stmt::CaseStmtClass: |
| 6082 | return visitCaseStmt(S: cast<CaseStmt>(Val: S)); |
| 6083 | case Stmt::DefaultStmtClass: |
| 6084 | return visitDefaultStmt(S: cast<DefaultStmt>(Val: S)); |
| 6085 | case Stmt::AttributedStmtClass: |
| 6086 | return visitAttributedStmt(S: cast<AttributedStmt>(Val: S)); |
| 6087 | case Stmt::CXXTryStmtClass: |
| 6088 | return visitCXXTryStmt(S: cast<CXXTryStmt>(Val: S)); |
| 6089 | case Stmt::NullStmtClass: |
| 6090 | return true; |
| 6091 | // Always invalid statements. |
| 6092 | case Stmt::GCCAsmStmtClass: |
| 6093 | case Stmt::MSAsmStmtClass: |
| 6094 | case Stmt::GotoStmtClass: |
| 6095 | return this->emitInvalid(S); |
| 6096 | case Stmt::LabelStmtClass: |
| 6097 | return this->visitStmt(S: cast<LabelStmt>(Val: S)->getSubStmt()); |
| 6098 | default: { |
| 6099 | if (const auto *E = dyn_cast<Expr>(Val: S)) |
| 6100 | return this->discard(E); |
| 6101 | return false; |
| 6102 | } |
| 6103 | } |
| 6104 | } |
| 6105 | |
| 6106 | template <class Emitter> |
| 6107 | bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) { |
| 6108 | LocalScope<Emitter> Scope(this); |
| 6109 | for (const auto *InnerStmt : S->body()) |
| 6110 | if (!visitStmt(S: InnerStmt)) |
| 6111 | return false; |
| 6112 | return Scope.destroyLocals(); |
| 6113 | } |
| 6114 | |
| 6115 | template <class Emitter> |
| 6116 | bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) { |
| 6117 | if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) { |
| 6118 | for (auto *BD : DD->flat_bindings()) |
| 6119 | if (auto *KD = BD->getHoldingVar(); |
| 6120 | KD && !this->visitVarDecl(VD: KD, Init: KD->getInit())) |
| 6121 | return false; |
| 6122 | } |
| 6123 | return true; |
| 6124 | } |
| 6125 | |
| 6126 | static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) { |
| 6127 | assert(FD); |
| 6128 | assert(FD->getParent()->isUnion()); |
| 6129 | const CXXRecordDecl *CXXRD = |
| 6130 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6131 | return !CXXRD || CXXRD->hasTrivialDefaultConstructor(); |
| 6132 | } |
| 6133 | |
| 6134 | template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) { |
| 6135 | for (;;) { |
| 6136 | if (const auto *ME = dyn_cast<MemberExpr>(Val: E)) { |
| 6137 | if (const auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl()); |
| 6138 | FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD)) |
| 6139 | return true; |
| 6140 | E = ME->getBase(); |
| 6141 | continue; |
| 6142 | } |
| 6143 | |
| 6144 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) { |
| 6145 | E = ASE->getBase()->IgnoreImplicit(); |
| 6146 | continue; |
| 6147 | } |
| 6148 | |
| 6149 | if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E); |
| 6150 | ICE && (ICE->getCastKind() == CK_NoOp || |
| 6151 | ICE->getCastKind() == CK_DerivedToBase || |
| 6152 | ICE->getCastKind() == CK_UncheckedDerivedToBase)) { |
| 6153 | E = ICE->getSubExpr(); |
| 6154 | continue; |
| 6155 | } |
| 6156 | |
| 6157 | if (const auto *This = dyn_cast<CXXThisExpr>(Val: E)) { |
| 6158 | const auto *ThisRecord = |
| 6159 | This->getType()->getPointeeType()->getAsRecordDecl(); |
| 6160 | if (!ThisRecord->isUnion()) |
| 6161 | return false; |
| 6162 | // Otherwise, always activate if we're in the ctor. |
| 6163 | if (const auto *Ctor = |
| 6164 | dyn_cast_if_present<CXXConstructorDecl>(Val: CompilingFunction)) |
| 6165 | return Ctor->getParent() == ThisRecord; |
| 6166 | return false; |
| 6167 | } |
| 6168 | |
| 6169 | break; |
| 6170 | } |
| 6171 | return false; |
| 6172 | } |
| 6173 | |
| 6174 | template <class Emitter> |
| 6175 | bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS, |
| 6176 | bool EvaluateConditionDecl) { |
| 6177 | for (const auto *D : DS->decls()) { |
| 6178 | if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl, BaseUsingDecl, |
| 6179 | FunctionDecl, NamespaceAliasDecl, UsingDirectiveDecl>(Val: D)) |
| 6180 | continue; |
| 6181 | |
| 6182 | const auto *VD = dyn_cast<VarDecl>(Val: D); |
| 6183 | if (!VD) |
| 6184 | return false; |
| 6185 | if (!this->visitVarDecl(VD, Init: VD->getInit())) |
| 6186 | return false; |
| 6187 | |
| 6188 | // Register decomposition decl holding vars. |
| 6189 | if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD)) |
| 6190 | return false; |
| 6191 | } |
| 6192 | |
| 6193 | return true; |
| 6194 | } |
| 6195 | |
| 6196 | template <class Emitter> |
| 6197 | bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) { |
| 6198 | if (this->InStmtExpr) |
| 6199 | return this->emitUnsupported(RS); |
| 6200 | |
| 6201 | if (const Expr *RE = RS->getRetValue()) { |
| 6202 | LocalScope<Emitter> RetScope(this); |
| 6203 | if (ReturnType) { |
| 6204 | // Primitive types are simply returned. |
| 6205 | if (!this->visit(E: RE)) |
| 6206 | return false; |
| 6207 | this->emitCleanup(); |
| 6208 | return this->emitRet(*ReturnType, RS); |
| 6209 | } |
| 6210 | |
| 6211 | if (RE->getType()->isVoidType()) { |
| 6212 | if (!this->visit(E: RE)) |
| 6213 | return false; |
| 6214 | } else { |
| 6215 | if (RE->containsErrors()) |
| 6216 | return false; |
| 6217 | |
| 6218 | InitLinkScope<Emitter> ILS(this, InitLink::RVO()); |
| 6219 | // RVO - construct the value in the return location. |
| 6220 | if (!this->emitRVOPtr(RE)) |
| 6221 | return false; |
| 6222 | if (!this->visitInitializerPop(E: RE)) |
| 6223 | return false; |
| 6224 | |
| 6225 | this->emitCleanup(); |
| 6226 | return this->emitRetVoid(RS); |
| 6227 | } |
| 6228 | } |
| 6229 | |
| 6230 | // Void return. |
| 6231 | this->emitCleanup(); |
| 6232 | return this->emitRetVoid(RS); |
| 6233 | } |
| 6234 | |
| 6235 | template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) { |
| 6236 | LocalScope<Emitter> IfScope(this); |
| 6237 | |
| 6238 | auto visitChildStmt = [&](const Stmt *S) -> bool { |
| 6239 | LocalScope<Emitter> SScope(this); |
| 6240 | if (!visitStmt(S)) |
| 6241 | return false; |
| 6242 | return SScope.destroyLocals(); |
| 6243 | }; |
| 6244 | |
| 6245 | if (auto *CondInit = IS->getInit()) { |
| 6246 | if (!visitStmt(S: CondInit)) |
| 6247 | return false; |
| 6248 | } |
| 6249 | |
| 6250 | if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) { |
| 6251 | if (!visitDeclStmt(DS: CondDecl)) |
| 6252 | return false; |
| 6253 | } |
| 6254 | |
| 6255 | // Save ourselves compiling some code and the jumps, etc. if the condition is |
| 6256 | // stataically known to be either true or false. We could look at more cases |
| 6257 | // here, but I think all the ones that actually happen are using a |
| 6258 | // ConstantExpr. |
| 6259 | if (std::optional<bool> BoolValue = getBoolValue(E: IS->getCond())) { |
| 6260 | if (*BoolValue) |
| 6261 | return visitChildStmt(IS->getThen()); |
| 6262 | if (const Stmt *Else = IS->getElse()) |
| 6263 | return visitChildStmt(Else); |
| 6264 | return true; |
| 6265 | } |
| 6266 | |
| 6267 | // Otherwise, compile the condition. |
| 6268 | if (IS->isNonNegatedConsteval()) { |
| 6269 | if (!this->emitIsConstantContext(IS)) |
| 6270 | return false; |
| 6271 | } else if (IS->isNegatedConsteval()) { |
| 6272 | if (!this->emitIsConstantContext(IS)) |
| 6273 | return false; |
| 6274 | if (!this->emitInv(IS)) |
| 6275 | return false; |
| 6276 | } else { |
| 6277 | LocalScope<Emitter> CondScope(this, ScopeKind::FullExpression); |
| 6278 | if (!this->visitBool(E: IS->getCond())) |
| 6279 | return false; |
| 6280 | if (!CondScope.destroyLocals()) |
| 6281 | return false; |
| 6282 | } |
| 6283 | |
| 6284 | if (!this->maybeEmitDeferredVarInit(VD: IS->getConditionVariable())) |
| 6285 | return false; |
| 6286 | |
| 6287 | if (const Stmt *Else = IS->getElse()) { |
| 6288 | LabelTy LabelElse = this->getLabel(); |
| 6289 | LabelTy LabelEnd = this->getLabel(); |
| 6290 | if (!this->jumpFalse(LabelElse, IS)) |
| 6291 | return false; |
| 6292 | if (!visitChildStmt(IS->getThen())) |
| 6293 | return false; |
| 6294 | if (!this->jump(LabelEnd, IS)) |
| 6295 | return false; |
| 6296 | this->emitLabel(LabelElse); |
| 6297 | if (!visitChildStmt(Else)) |
| 6298 | return false; |
| 6299 | this->emitLabel(LabelEnd); |
| 6300 | } else { |
| 6301 | LabelTy LabelEnd = this->getLabel(); |
| 6302 | if (!this->jumpFalse(LabelEnd, IS)) |
| 6303 | return false; |
| 6304 | if (!visitChildStmt(IS->getThen())) |
| 6305 | return false; |
| 6306 | this->emitLabel(LabelEnd); |
| 6307 | } |
| 6308 | |
| 6309 | if (!IfScope.destroyLocals()) |
| 6310 | return false; |
| 6311 | |
| 6312 | return true; |
| 6313 | } |
| 6314 | |
| 6315 | template <class Emitter> |
| 6316 | bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) { |
| 6317 | const Expr *Cond = S->getCond(); |
| 6318 | const Stmt *Body = S->getBody(); |
| 6319 | |
| 6320 | LabelTy CondLabel = this->getLabel(); // Label before the condition. |
| 6321 | LabelTy EndLabel = this->getLabel(); // Label after the loop. |
| 6322 | LocalScope<Emitter> WholeLoopScope(this); |
| 6323 | LoopScope<Emitter> LS(this, S, EndLabel, CondLabel); |
| 6324 | |
| 6325 | this->fallthrough(CondLabel); |
| 6326 | this->emitLabel(CondLabel); |
| 6327 | |
| 6328 | // Start of the loop body { |
| 6329 | LocalScope<Emitter> CondScope(this); |
| 6330 | |
| 6331 | if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) { |
| 6332 | if (!visitDeclStmt(DS: CondDecl)) |
| 6333 | return false; |
| 6334 | } |
| 6335 | |
| 6336 | if (!this->visitBool(E: Cond)) |
| 6337 | return false; |
| 6338 | |
| 6339 | if (!this->maybeEmitDeferredVarInit(VD: S->getConditionVariable())) |
| 6340 | return false; |
| 6341 | |
| 6342 | if (!this->jumpFalse(EndLabel, S)) |
| 6343 | return false; |
| 6344 | |
| 6345 | if (!this->visitStmt(S: Body)) |
| 6346 | return false; |
| 6347 | |
| 6348 | if (!CondScope.destroyLocals()) |
| 6349 | return false; |
| 6350 | // } End of loop body. |
| 6351 | |
| 6352 | if (!this->jump(CondLabel, S)) |
| 6353 | return false; |
| 6354 | this->fallthrough(EndLabel); |
| 6355 | this->emitLabel(EndLabel); |
| 6356 | |
| 6357 | return CondScope.destroyLocals() && WholeLoopScope.destroyLocals(); |
| 6358 | } |
| 6359 | |
| 6360 | template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) { |
| 6361 | const Expr *Cond = S->getCond(); |
| 6362 | const Stmt *Body = S->getBody(); |
| 6363 | |
| 6364 | LabelTy StartLabel = this->getLabel(); |
| 6365 | LabelTy EndLabel = this->getLabel(); |
| 6366 | LabelTy CondLabel = this->getLabel(); |
| 6367 | LocalScope<Emitter> WholeLoopScope(this); |
| 6368 | LoopScope<Emitter> LS(this, S, EndLabel, CondLabel); |
| 6369 | |
| 6370 | this->fallthrough(StartLabel); |
| 6371 | this->emitLabel(StartLabel); |
| 6372 | |
| 6373 | { |
| 6374 | LocalScope<Emitter> CondScope(this); |
| 6375 | if (!this->visitStmt(S: Body)) |
| 6376 | return false; |
| 6377 | this->fallthrough(CondLabel); |
| 6378 | this->emitLabel(CondLabel); |
| 6379 | if (!this->visitBool(E: Cond)) |
| 6380 | return false; |
| 6381 | |
| 6382 | if (!CondScope.destroyLocals()) |
| 6383 | return false; |
| 6384 | } |
| 6385 | if (!this->jumpTrue(StartLabel, S)) |
| 6386 | return false; |
| 6387 | |
| 6388 | this->fallthrough(EndLabel); |
| 6389 | this->emitLabel(EndLabel); |
| 6390 | return WholeLoopScope.destroyLocals(); |
| 6391 | } |
| 6392 | |
| 6393 | template <class Emitter> |
| 6394 | bool Compiler<Emitter>::visitForStmt(const ForStmt *S) { |
| 6395 | // for (Init; Cond; Inc) { Body } |
| 6396 | const Stmt *Init = S->getInit(); |
| 6397 | const Expr *Cond = S->getCond(); |
| 6398 | const Expr *Inc = S->getInc(); |
| 6399 | const Stmt *Body = S->getBody(); |
| 6400 | |
| 6401 | LabelTy EndLabel = this->getLabel(); |
| 6402 | LabelTy CondLabel = this->getLabel(); |
| 6403 | LabelTy IncLabel = this->getLabel(); |
| 6404 | |
| 6405 | LocalScope<Emitter> WholeLoopScope(this); |
| 6406 | if (Init && !this->visitStmt(S: Init)) |
| 6407 | return false; |
| 6408 | |
| 6409 | // Start of the loop body { |
| 6410 | this->fallthrough(CondLabel); |
| 6411 | this->emitLabel(CondLabel); |
| 6412 | |
| 6413 | LocalScope<Emitter> CondScope(this); |
| 6414 | LoopScope<Emitter> LS(this, S, EndLabel, IncLabel); |
| 6415 | if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) { |
| 6416 | if (!visitDeclStmt(DS: CondDecl)) |
| 6417 | return false; |
| 6418 | } |
| 6419 | |
| 6420 | if (Cond) { |
| 6421 | if (!this->visitBool(E: Cond)) |
| 6422 | return false; |
| 6423 | if (!this->jumpFalse(EndLabel, S)) |
| 6424 | return false; |
| 6425 | } |
| 6426 | if (!this->maybeEmitDeferredVarInit(VD: S->getConditionVariable())) |
| 6427 | return false; |
| 6428 | |
| 6429 | if (Body && !this->visitStmt(S: Body)) |
| 6430 | return false; |
| 6431 | |
| 6432 | this->fallthrough(IncLabel); |
| 6433 | this->emitLabel(IncLabel); |
| 6434 | if (Inc && !this->discard(E: Inc)) |
| 6435 | return false; |
| 6436 | |
| 6437 | if (!CondScope.destroyLocals()) |
| 6438 | return false; |
| 6439 | if (!this->jump(CondLabel, S)) |
| 6440 | return false; |
| 6441 | // } End of loop body. |
| 6442 | |
| 6443 | this->emitLabel(EndLabel); |
| 6444 | // If we jumped out of the loop above, we still need to clean up the condition |
| 6445 | // scope. |
| 6446 | return CondScope.destroyLocals() && WholeLoopScope.destroyLocals(); |
| 6447 | } |
| 6448 | |
| 6449 | template <class Emitter> |
| 6450 | bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) { |
| 6451 | const Stmt *Init = S->getInit(); |
| 6452 | const Expr *Cond = S->getCond(); |
| 6453 | const Expr *Inc = S->getInc(); |
| 6454 | const Stmt *Body = S->getBody(); |
| 6455 | const Stmt *BeginStmt = S->getBeginStmt(); |
| 6456 | const Stmt *RangeStmt = S->getRangeStmt(); |
| 6457 | const Stmt *EndStmt = S->getEndStmt(); |
| 6458 | |
| 6459 | LabelTy EndLabel = this->getLabel(); |
| 6460 | LabelTy CondLabel = this->getLabel(); |
| 6461 | LabelTy IncLabel = this->getLabel(); |
| 6462 | LocalScope<Emitter> WholeLoopScope(this); |
| 6463 | LoopScope<Emitter> LS(this, S, EndLabel, IncLabel); |
| 6464 | |
| 6465 | // Emit declarations needed in the loop. |
| 6466 | if (Init && !this->visitStmt(S: Init)) |
| 6467 | return false; |
| 6468 | if (!this->visitStmt(S: RangeStmt)) |
| 6469 | return false; |
| 6470 | if (!this->visitStmt(S: BeginStmt)) |
| 6471 | return false; |
| 6472 | if (!this->visitStmt(S: EndStmt)) |
| 6473 | return false; |
| 6474 | |
| 6475 | // Now the condition as well as the loop variable assignment. |
| 6476 | this->fallthrough(CondLabel); |
| 6477 | this->emitLabel(CondLabel); |
| 6478 | if (!this->visitBool(E: Cond)) |
| 6479 | return false; |
| 6480 | if (!this->jumpFalse(EndLabel, S)) |
| 6481 | return false; |
| 6482 | |
| 6483 | if (!this->visitDeclStmt(DS: S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true)) |
| 6484 | return false; |
| 6485 | |
| 6486 | // Body. |
| 6487 | { |
| 6488 | if (!this->visitStmt(S: Body)) |
| 6489 | return false; |
| 6490 | |
| 6491 | this->fallthrough(IncLabel); |
| 6492 | this->emitLabel(IncLabel); |
| 6493 | if (!this->discard(E: Inc)) |
| 6494 | return false; |
| 6495 | } |
| 6496 | |
| 6497 | if (!this->jump(CondLabel, S)) |
| 6498 | return false; |
| 6499 | |
| 6500 | this->fallthrough(EndLabel); |
| 6501 | this->emitLabel(EndLabel); |
| 6502 | return WholeLoopScope.destroyLocals(); |
| 6503 | } |
| 6504 | |
| 6505 | template <class Emitter> |
| 6506 | bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) { |
| 6507 | if (LabelInfoStack.empty()) |
| 6508 | return false; |
| 6509 | |
| 6510 | OptLabelTy TargetLabel = std::nullopt; |
| 6511 | const Stmt *TargetLoop = S->getNamedLoopOrSwitch(); |
| 6512 | const VariableScope<Emitter> *BreakScope = nullptr; |
| 6513 | |
| 6514 | if (!TargetLoop) { |
| 6515 | for (const auto &LI : llvm::reverse(LabelInfoStack)) { |
| 6516 | if (LI.BreakLabel) { |
| 6517 | TargetLabel = *LI.BreakLabel; |
| 6518 | BreakScope = LI.BreakOrContinueScope; |
| 6519 | break; |
| 6520 | } |
| 6521 | } |
| 6522 | } else { |
| 6523 | for (auto LI : LabelInfoStack) { |
| 6524 | if (LI.Name == TargetLoop) { |
| 6525 | TargetLabel = *LI.BreakLabel; |
| 6526 | BreakScope = LI.BreakOrContinueScope; |
| 6527 | break; |
| 6528 | } |
| 6529 | } |
| 6530 | } |
| 6531 | |
| 6532 | // Faulty break statement (e.g. label redefined or named loops disabled). |
| 6533 | if (!TargetLabel) |
| 6534 | return false; |
| 6535 | |
| 6536 | for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope; |
| 6537 | C = C->getParent()) { |
| 6538 | if (!C->destroyLocals()) |
| 6539 | return false; |
| 6540 | } |
| 6541 | |
| 6542 | return this->jump(*TargetLabel, S); |
| 6543 | } |
| 6544 | |
| 6545 | template <class Emitter> |
| 6546 | bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) { |
| 6547 | if (LabelInfoStack.empty()) |
| 6548 | return false; |
| 6549 | |
| 6550 | OptLabelTy TargetLabel = std::nullopt; |
| 6551 | const Stmt *TargetLoop = S->getNamedLoopOrSwitch(); |
| 6552 | const VariableScope<Emitter> *ContinueScope = nullptr; |
| 6553 | |
| 6554 | if (!TargetLoop) { |
| 6555 | for (const auto &LI : llvm::reverse(LabelInfoStack)) { |
| 6556 | if (LI.ContinueLabel) { |
| 6557 | TargetLabel = *LI.ContinueLabel; |
| 6558 | ContinueScope = LI.BreakOrContinueScope; |
| 6559 | break; |
| 6560 | } |
| 6561 | } |
| 6562 | } else { |
| 6563 | for (auto LI : LabelInfoStack) { |
| 6564 | if (LI.Name == TargetLoop) { |
| 6565 | TargetLabel = *LI.ContinueLabel; |
| 6566 | ContinueScope = LI.BreakOrContinueScope; |
| 6567 | break; |
| 6568 | } |
| 6569 | } |
| 6570 | } |
| 6571 | assert(TargetLabel); |
| 6572 | |
| 6573 | for (VariableScope<Emitter> *C = VarScope; C != ContinueScope; |
| 6574 | C = C->getParent()) { |
| 6575 | if (!C->destroyLocals()) |
| 6576 | return false; |
| 6577 | } |
| 6578 | |
| 6579 | return this->jump(*TargetLabel, S); |
| 6580 | } |
| 6581 | |
| 6582 | template <class Emitter> |
| 6583 | bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) { |
| 6584 | const Expr *Cond = S->getCond(); |
| 6585 | if (Cond->containsErrors()) |
| 6586 | return false; |
| 6587 | |
| 6588 | PrimType CondT = this->classifyPrim(Cond->getType()); |
| 6589 | LocalScope<Emitter> LS(this); |
| 6590 | llvm::SaveAndRestore StmtExprSAR(this->SwitchInStmtExpr, this->InStmtExpr); |
| 6591 | |
| 6592 | LabelTy EndLabel = this->getLabel(); |
| 6593 | UnsignedOrNone DefaultLabel = std::nullopt; |
| 6594 | unsigned CondVar = |
| 6595 | this->allocateLocalPrimitive(Src: Cond, Ty: CondT, /*IsConst=*/true); |
| 6596 | |
| 6597 | if (const auto *CondInit = S->getInit()) |
| 6598 | if (!visitStmt(S: CondInit)) |
| 6599 | return false; |
| 6600 | |
| 6601 | if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) |
| 6602 | if (!visitDeclStmt(DS: CondDecl)) |
| 6603 | return false; |
| 6604 | |
| 6605 | // Initialize condition variable. |
| 6606 | if (!this->visit(E: Cond)) |
| 6607 | return false; |
| 6608 | if (!this->emitSetLocal(CondT, CondVar, S)) |
| 6609 | return false; |
| 6610 | |
| 6611 | if (!this->maybeEmitDeferredVarInit(VD: S->getConditionVariable())) |
| 6612 | return false; |
| 6613 | |
| 6614 | CaseMap CaseLabels; |
| 6615 | // Create labels and comparison ops for all case statements. |
| 6616 | for (const SwitchCase *SC = S->getSwitchCaseList(); SC; |
| 6617 | SC = SC->getNextSwitchCase()) { |
| 6618 | if (const auto *CS = dyn_cast<CaseStmt>(Val: SC)) { |
| 6619 | CaseLabels[SC] = this->getLabel(); |
| 6620 | |
| 6621 | if (CS->caseStmtIsGNURange()) { |
| 6622 | LabelTy EndOfRangeCheck = this->getLabel(); |
| 6623 | const Expr *Low = CS->getLHS(); |
| 6624 | const Expr *High = CS->getRHS(); |
| 6625 | if (Low->isValueDependent() || High->isValueDependent()) |
| 6626 | return false; |
| 6627 | |
| 6628 | if (!this->emitGetLocal(CondT, CondVar, CS)) |
| 6629 | return false; |
| 6630 | if (!this->visit(E: Low)) |
| 6631 | return false; |
| 6632 | PrimType LT = this->classifyPrim(Low->getType()); |
| 6633 | if (!this->emitGE(LT, S)) |
| 6634 | return false; |
| 6635 | if (!this->jumpFalse(EndOfRangeCheck, S)) |
| 6636 | return false; |
| 6637 | |
| 6638 | if (!this->emitGetLocal(CondT, CondVar, CS)) |
| 6639 | return false; |
| 6640 | if (!this->visit(E: High)) |
| 6641 | return false; |
| 6642 | PrimType HT = this->classifyPrim(High->getType()); |
| 6643 | if (!this->emitLE(HT, S)) |
| 6644 | return false; |
| 6645 | if (!this->jumpTrue(CaseLabels[CS], S)) |
| 6646 | return false; |
| 6647 | this->emitLabel(EndOfRangeCheck); |
| 6648 | continue; |
| 6649 | } |
| 6650 | |
| 6651 | const Expr *Value = CS->getLHS(); |
| 6652 | if (Value->isValueDependent()) |
| 6653 | return false; |
| 6654 | PrimType ValueT = this->classifyPrim(Value->getType()); |
| 6655 | |
| 6656 | // Compare the case statement's value to the switch condition. |
| 6657 | if (!this->emitGetLocal(CondT, CondVar, CS)) |
| 6658 | return false; |
| 6659 | if (!this->visit(E: Value)) |
| 6660 | return false; |
| 6661 | |
| 6662 | // Compare and jump to the case label. |
| 6663 | if (!this->emitEQ(ValueT, S)) |
| 6664 | return false; |
| 6665 | if (!this->jumpTrue(CaseLabels[CS], S)) |
| 6666 | return false; |
| 6667 | } else { |
| 6668 | assert(!DefaultLabel); |
| 6669 | DefaultLabel = this->getLabel(); |
| 6670 | } |
| 6671 | } |
| 6672 | |
| 6673 | // If none of the conditions above were true, fall through to the default |
| 6674 | // statement or jump after the switch statement. |
| 6675 | if (DefaultLabel) { |
| 6676 | if (!this->jump(*DefaultLabel, S)) |
| 6677 | return false; |
| 6678 | } else { |
| 6679 | if (!this->jump(EndLabel, S)) |
| 6680 | return false; |
| 6681 | } |
| 6682 | |
| 6683 | SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel, |
| 6684 | DefaultLabel); |
| 6685 | if (!this->visitStmt(S: S->getBody())) |
| 6686 | return false; |
| 6687 | this->fallthrough(EndLabel); |
| 6688 | this->emitLabel(EndLabel); |
| 6689 | |
| 6690 | return LS.destroyLocals(); |
| 6691 | } |
| 6692 | |
| 6693 | template <class Emitter> |
| 6694 | bool Compiler<Emitter>::visitCaseStmt(const CaseStmt *S) { |
| 6695 | this->fallthrough(CaseLabels[S]); |
| 6696 | this->emitLabel(CaseLabels[S]); |
| 6697 | |
| 6698 | // We can't jump from an outer switch statement to a case label |
| 6699 | // that's inside a StmtExpr. |
| 6700 | if (this->InStmtExpr && !this->SwitchInStmtExpr) |
| 6701 | return this->emitUnsupported(S); |
| 6702 | |
| 6703 | return this->visitStmt(S: S->getSubStmt()); |
| 6704 | } |
| 6705 | |
| 6706 | template <class Emitter> |
| 6707 | bool Compiler<Emitter>::visitDefaultStmt(const DefaultStmt *S) { |
| 6708 | if (LabelInfoStack.empty()) |
| 6709 | return false; |
| 6710 | |
| 6711 | LabelTy DefaultLabel; |
| 6712 | for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) { |
| 6713 | if (LI.DefaultLabel) { |
| 6714 | DefaultLabel = *LI.DefaultLabel; |
| 6715 | break; |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | this->emitLabel(DefaultLabel); |
| 6720 | return this->visitStmt(S: S->getSubStmt()); |
| 6721 | } |
| 6722 | |
| 6723 | template <class Emitter> |
| 6724 | bool Compiler<Emitter>::visitAttributedStmt(const AttributedStmt *S) { |
| 6725 | const Stmt *SubStmt = S->getSubStmt(); |
| 6726 | |
| 6727 | bool IsMSVCConstexprAttr = isa<ReturnStmt>(Val: SubStmt) && |
| 6728 | hasSpecificAttr<MSConstexprAttr>(container: S->getAttrs()); |
| 6729 | |
| 6730 | if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S)) |
| 6731 | return false; |
| 6732 | |
| 6733 | if (this->Ctx.getLangOpts().CXXAssumptions && |
| 6734 | !this->Ctx.getLangOpts().MSVCCompat) { |
| 6735 | for (const Attr *A : S->getAttrs()) { |
| 6736 | auto *AA = dyn_cast<CXXAssumeAttr>(Val: A); |
| 6737 | if (!AA) |
| 6738 | continue; |
| 6739 | |
| 6740 | assert(isa<NullStmt>(SubStmt)); |
| 6741 | |
| 6742 | const Expr *Assumption = AA->getAssumption(); |
| 6743 | if (Assumption->isValueDependent()) |
| 6744 | return false; |
| 6745 | |
| 6746 | if (Assumption->HasSideEffects(Ctx: this->Ctx.getASTContext())) |
| 6747 | continue; |
| 6748 | |
| 6749 | // Evaluate assumption. |
| 6750 | if (!this->visitBool(E: Assumption)) |
| 6751 | return false; |
| 6752 | |
| 6753 | if (!this->emitAssume(Assumption)) |
| 6754 | return false; |
| 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | // Ignore other attributes. |
| 6759 | if (!this->visitStmt(S: SubStmt)) |
| 6760 | return false; |
| 6761 | |
| 6762 | if (IsMSVCConstexprAttr) |
| 6763 | return this->emitPopMSVCCE(S); |
| 6764 | return true; |
| 6765 | } |
| 6766 | |
| 6767 | template <class Emitter> |
| 6768 | bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) { |
| 6769 | // Ignore all handlers. |
| 6770 | return this->visitStmt(S: S->getTryBlock()); |
| 6771 | } |
| 6772 | |
| 6773 | template <class Emitter> |
| 6774 | bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) { |
| 6775 | assert(MD->isLambdaStaticInvoker()); |
| 6776 | assert(MD->hasBody()); |
| 6777 | assert(cast<CompoundStmt>(MD->getBody())->body_empty()); |
| 6778 | |
| 6779 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
| 6780 | const FunctionDecl *LambdaCallOp; |
| 6781 | assert(ClosureClass->captures().empty()); |
| 6782 | if (ClosureClass->isGenericLambda()) { |
| 6783 | LambdaCallOp = ClosureClass->getLambdaCallOperator(); |
| 6784 | assert(MD->isFunctionTemplateSpecialization() && |
| 6785 | "A generic lambda's static-invoker function must be a " |
| 6786 | "template specialization" ); |
| 6787 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 6788 | FunctionTemplateDecl *CallOpTemplate = |
| 6789 | LambdaCallOp->getDescribedFunctionTemplate(); |
| 6790 | void *InsertPos = nullptr; |
| 6791 | const FunctionDecl *CorrespondingCallOpSpecialization = |
| 6792 | CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos); |
| 6793 | assert(CorrespondingCallOpSpecialization); |
| 6794 | LambdaCallOp = CorrespondingCallOpSpecialization; |
| 6795 | } else { |
| 6796 | LambdaCallOp = ClosureClass->getLambdaCallOperator(); |
| 6797 | } |
| 6798 | assert(ClosureClass->captures().empty()); |
| 6799 | const Function *Func = this->getFunction(FD: LambdaCallOp); |
| 6800 | if (!Func) |
| 6801 | return false; |
| 6802 | assert(Func->hasThisPointer()); |
| 6803 | assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO())); |
| 6804 | |
| 6805 | if (Func->hasRVO()) { |
| 6806 | if (!this->emitRVOPtr(MD)) |
| 6807 | return false; |
| 6808 | } |
| 6809 | |
| 6810 | // The lambda call operator needs an instance pointer, but we don't have |
| 6811 | // one here, and we don't need one either because the lambda cannot have |
| 6812 | // any captures, as verified above. Emit a null pointer. This is then |
| 6813 | // special-cased when interpreting to not emit any misleading diagnostics. |
| 6814 | if (!this->emitNullPtr(0, nullptr, MD)) |
| 6815 | return false; |
| 6816 | |
| 6817 | // Forward all arguments from the static invoker to the lambda call operator. |
| 6818 | for (const ParmVarDecl *PVD : MD->parameters()) { |
| 6819 | auto It = this->Params.find(PVD); |
| 6820 | assert(It != this->Params.end()); |
| 6821 | |
| 6822 | // We do the lvalue-to-rvalue conversion manually here, so no need |
| 6823 | // to care about references. |
| 6824 | PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr); |
| 6825 | if (!this->emitGetParam(ParamType, It->second.Index, MD)) |
| 6826 | return false; |
| 6827 | } |
| 6828 | |
| 6829 | if (!this->emitCall(Func, 0, LambdaCallOp)) |
| 6830 | return false; |
| 6831 | |
| 6832 | this->emitCleanup(); |
| 6833 | if (ReturnType) |
| 6834 | return this->emitRet(*ReturnType, MD); |
| 6835 | |
| 6836 | // Nothing to do, since we emitted the RVO pointer above. |
| 6837 | return this->emitRetVoid(MD); |
| 6838 | } |
| 6839 | |
| 6840 | template <class Emitter> |
| 6841 | bool Compiler<Emitter>::checkLiteralType(const Expr *E) { |
| 6842 | if (Ctx.getLangOpts().CPlusPlus23) |
| 6843 | return true; |
| 6844 | |
| 6845 | if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Ctx.getASTContext())) |
| 6846 | return true; |
| 6847 | |
| 6848 | return this->emitCheckLiteralType(E->getType().getTypePtr(), E); |
| 6849 | } |
| 6850 | |
| 6851 | static bool initNeedsOverridenLoc(const CXXCtorInitializer *Init) { |
| 6852 | const Expr *InitExpr = Init->getInit(); |
| 6853 | |
| 6854 | if (!Init->isWritten() && !Init->isInClassMemberInitializer() && |
| 6855 | !isa<CXXConstructExpr>(Val: InitExpr)) |
| 6856 | return true; |
| 6857 | |
| 6858 | if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: InitExpr)) { |
| 6859 | const CXXConstructorDecl *Ctor = CE->getConstructor(); |
| 6860 | if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() && |
| 6861 | Ctor->isTrivial()) |
| 6862 | return true; |
| 6863 | } |
| 6864 | |
| 6865 | return false; |
| 6866 | } |
| 6867 | |
| 6868 | template <class Emitter> |
| 6869 | bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) { |
| 6870 | assert(!ReturnType); |
| 6871 | |
| 6872 | // Only start the lifetime of the instance pointer. |
| 6873 | if (!this->emitStartThisLifetime1(Ctor)) |
| 6874 | return false; |
| 6875 | |
| 6876 | auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset, |
| 6877 | const Expr *InitExpr, |
| 6878 | bool Activate = false) -> bool { |
| 6879 | // We don't know what to do with these, so just return false. |
| 6880 | if (InitExpr->getType().isNull()) |
| 6881 | return false; |
| 6882 | |
| 6883 | if (OptPrimType T = this->classify(InitExpr)) { |
| 6884 | if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr)) |
| 6885 | return false; |
| 6886 | |
| 6887 | if (!this->visit(E: InitExpr)) |
| 6888 | return false; |
| 6889 | |
| 6890 | if (F->isBitField()) |
| 6891 | return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(), |
| 6892 | InitExpr); |
| 6893 | return this->emitInitThisField(*T, FieldOffset, InitExpr); |
| 6894 | } |
| 6895 | // Non-primitive case. Get a pointer to the field-to-initialize |
| 6896 | // on the stack and call visitInitialzer() for it. |
| 6897 | InitLinkScope<Emitter> FieldScope(this, InitLink::Field(Offset: F->Offset)); |
| 6898 | if (!this->emitGetPtrThisField(FieldOffset, InitExpr)) |
| 6899 | return false; |
| 6900 | |
| 6901 | if (Activate && !this->emitActivate(InitExpr)) |
| 6902 | return false; |
| 6903 | |
| 6904 | return this->visitInitializerPop(E: InitExpr); |
| 6905 | }; |
| 6906 | |
| 6907 | const RecordDecl *RD = Ctor->getParent(); |
| 6908 | const Record *R = this->getRecord(RD); |
| 6909 | if (!R) |
| 6910 | return false; |
| 6911 | bool IsUnion = R->isUnion(); |
| 6912 | |
| 6913 | // Default union copy and move ctors are special. |
| 6914 | if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) { |
| 6915 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}); |
| 6916 | |
| 6917 | // No special case for NumFields == 0 here, so the Memcpy op |
| 6918 | // below also does its checks in those cases. |
| 6919 | |
| 6920 | assert(cast<CompoundStmt>(Ctor->getBody())->body_empty()); |
| 6921 | if (!this->emitThis(Ctor)) |
| 6922 | return false; |
| 6923 | |
| 6924 | if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor)) |
| 6925 | return false; |
| 6926 | |
| 6927 | return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) && |
| 6928 | this->emitRetVoid(Ctor); |
| 6929 | } |
| 6930 | |
| 6931 | unsigned FieldInits = 0; |
| 6932 | InitLinkScope<Emitter> InitScope(this, InitLink::This()); |
| 6933 | for (const auto *Init : Ctor->inits()) { |
| 6934 | // Scope needed for the initializers. |
| 6935 | LocalScope<Emitter> Scope(this, ScopeKind::FullExpression); |
| 6936 | |
| 6937 | const Expr *InitExpr = Init->getInit(); |
| 6938 | if (const FieldDecl *Member = Init->getMember()) { |
| 6939 | const Record::Field *F = R->getField(FD: Member); |
| 6940 | |
| 6941 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}, |
| 6942 | initNeedsOverridenLoc(Init)); |
| 6943 | if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion)) |
| 6944 | return false; |
| 6945 | ++FieldInits; |
| 6946 | } else if (const Type *Base = Init->getBaseClass()) { |
| 6947 | const auto *BaseDecl = Base->getAsCXXRecordDecl(); |
| 6948 | assert(BaseDecl); |
| 6949 | |
| 6950 | if (Init->isBaseVirtual()) { |
| 6951 | assert(R->getVirtualBase(BaseDecl)); |
| 6952 | if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr)) |
| 6953 | return false; |
| 6954 | |
| 6955 | } else { |
| 6956 | // Base class initializer. |
| 6957 | // Get This Base and call initializer on it. |
| 6958 | const Record::Base *B = R->getBase(FD: BaseDecl); |
| 6959 | assert(B); |
| 6960 | if (!this->emitGetPtrThisBase(B->Offset, InitExpr)) |
| 6961 | return false; |
| 6962 | } |
| 6963 | |
| 6964 | if (!this->visitInitializerPop(E: InitExpr)) |
| 6965 | return false; |
| 6966 | } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) { |
| 6967 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}, |
| 6968 | initNeedsOverridenLoc(Init)); |
| 6969 | unsigned ChainSize = IFD->getChainingSize(); |
| 6970 | assert(ChainSize >= 2); |
| 6971 | |
| 6972 | unsigned NestedFieldOffset = 0; |
| 6973 | const Record::Field *NestedField = nullptr; |
| 6974 | for (unsigned I = 0; I != ChainSize; ++I) { |
| 6975 | const auto *FD = cast<FieldDecl>(Val: IFD->chain()[I]); |
| 6976 | const Record *FieldRecord = this->P.getOrCreateRecord(RD: FD->getParent()); |
| 6977 | assert(FieldRecord); |
| 6978 | |
| 6979 | NestedField = FieldRecord->getField(FD); |
| 6980 | assert(NestedField); |
| 6981 | IsUnion = IsUnion || FieldRecord->isUnion(); |
| 6982 | |
| 6983 | NestedFieldOffset += NestedField->Offset; |
| 6984 | |
| 6985 | // Add a new InitChainLink for the record, but not for the final field. |
| 6986 | if (I != ChainSize - 1) |
| 6987 | InitStack.push_back(Elt: InitLink::Field(Offset: NestedField->Offset)); |
| 6988 | } |
| 6989 | assert(NestedField); |
| 6990 | |
| 6991 | InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: InitExpr)); |
| 6992 | if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr, |
| 6993 | IsUnion)) |
| 6994 | return false; |
| 6995 | |
| 6996 | // Mark all chain links as initialized. |
| 6997 | unsigned InitFieldOffset = 0; |
| 6998 | for (const NamedDecl *ND : IFD->chain().drop_back()) { |
| 6999 | const auto *FD = cast<FieldDecl>(Val: ND); |
| 7000 | const Record *FieldRecord = this->P.getOrCreateRecord(RD: FD->getParent()); |
| 7001 | assert(FieldRecord); |
| 7002 | NestedField = FieldRecord->getField(FD); |
| 7003 | InitFieldOffset += NestedField->Offset; |
| 7004 | assert(NestedField); |
| 7005 | if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr)) |
| 7006 | return false; |
| 7007 | if (!this->emitFinishInitPop(InitExpr)) |
| 7008 | return false; |
| 7009 | } |
| 7010 | |
| 7011 | InitStack.pop_back_n(NumItems: ChainSize - 1); |
| 7012 | |
| 7013 | } else { |
| 7014 | assert(Init->isDelegatingInitializer()); |
| 7015 | if (!this->emitThis(InitExpr)) |
| 7016 | return false; |
| 7017 | if (!this->visitInitializerPop(E: Init->getInit())) |
| 7018 | return false; |
| 7019 | } |
| 7020 | |
| 7021 | if (!Scope.destroyLocals()) |
| 7022 | return false; |
| 7023 | } |
| 7024 | |
| 7025 | if (FieldInits != R->getNumFields()) { |
| 7026 | assert(FieldInits < R->getNumFields()); |
| 7027 | // Start the lifetime of all members. |
| 7028 | if (!this->emitStartThisLifetime(Ctor)) |
| 7029 | return false; |
| 7030 | } |
| 7031 | |
| 7032 | if (const Stmt *Body = Ctor->getBody()) { |
| 7033 | // Only emit the CtorCheck op for non-empty CompoundStmt bodies. |
| 7034 | // For non-CompoundStmts, always assume they are non-empty and emit it. |
| 7035 | if (const auto *CS = dyn_cast<CompoundStmt>(Val: Body)) { |
| 7036 | if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{})) |
| 7037 | return false; |
| 7038 | } else { |
| 7039 | if (!this->emitCtorCheck(SourceInfo{})) |
| 7040 | return false; |
| 7041 | } |
| 7042 | |
| 7043 | if (!visitStmt(S: Body)) |
| 7044 | return false; |
| 7045 | } |
| 7046 | |
| 7047 | return this->emitRetVoid(SourceInfo{}); |
| 7048 | } |
| 7049 | |
| 7050 | template <class Emitter> |
| 7051 | bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) { |
| 7052 | const RecordDecl *RD = Dtor->getParent(); |
| 7053 | const Record *R = this->getRecord(RD); |
| 7054 | if (!R) |
| 7055 | return false; |
| 7056 | |
| 7057 | if (!Dtor->isTrivial() && Dtor->getBody()) { |
| 7058 | if (!this->visitStmt(S: Dtor->getBody())) |
| 7059 | return false; |
| 7060 | } |
| 7061 | |
| 7062 | if (!this->emitThis(Dtor)) |
| 7063 | return false; |
| 7064 | |
| 7065 | if (!this->emitCheckDestruction(Dtor)) |
| 7066 | return false; |
| 7067 | |
| 7068 | assert(R); |
| 7069 | if (!R->isUnion()) { |
| 7070 | |
| 7071 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}); |
| 7072 | // First, destroy all fields. |
| 7073 | for (const Record::Field &Field : llvm::reverse(C: R->fields())) { |
| 7074 | const Descriptor *D = Field.Desc; |
| 7075 | if (D->hasTrivialDtor()) |
| 7076 | continue; |
| 7077 | if (!this->emitGetPtrField(Field.Offset, SourceInfo{})) |
| 7078 | return false; |
| 7079 | if (!this->emitDestructionPop(Desc: D, Loc: SourceInfo{})) |
| 7080 | return false; |
| 7081 | } |
| 7082 | } |
| 7083 | |
| 7084 | for (const Record::Base &Base : llvm::reverse(C: R->bases())) { |
| 7085 | if (Base.R->hasTrivialDtor()) |
| 7086 | continue; |
| 7087 | if (!this->emitGetPtrBase(Base.Offset, SourceInfo{})) |
| 7088 | return false; |
| 7089 | if (!this->emitRecordDestructionPop(R: Base.R, Loc: {})) |
| 7090 | return false; |
| 7091 | } |
| 7092 | |
| 7093 | if (!this->emitMarkDestroyed(Dtor)) |
| 7094 | return false; |
| 7095 | |
| 7096 | // FIXME: Virtual bases. |
| 7097 | return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor); |
| 7098 | } |
| 7099 | |
| 7100 | template <class Emitter> |
| 7101 | bool Compiler<Emitter>::compileUnionAssignmentOperator( |
| 7102 | const CXXMethodDecl *MD) { |
| 7103 | if (!this->emitThis(MD)) |
| 7104 | return false; |
| 7105 | |
| 7106 | if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD)) |
| 7107 | return false; |
| 7108 | |
| 7109 | return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD); |
| 7110 | } |
| 7111 | |
| 7112 | template <class Emitter> |
| 7113 | bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) { |
| 7114 | if (F->getReturnType()->isDependentType()) |
| 7115 | return false; |
| 7116 | |
| 7117 | // Classify the return type. |
| 7118 | ReturnType = this->classify(F->getReturnType()); |
| 7119 | |
| 7120 | this->CompilingFunction = F; |
| 7121 | |
| 7122 | if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: F)) |
| 7123 | return this->compileConstructor(Ctor); |
| 7124 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(Val: F)) |
| 7125 | return this->compileDestructor(Dtor); |
| 7126 | |
| 7127 | // Emit custom code if this is a lambda static invoker. |
| 7128 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: F)) { |
| 7129 | const RecordDecl *RD = MD->getParent(); |
| 7130 | |
| 7131 | if (RD->isUnion() && |
| 7132 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())) |
| 7133 | return this->compileUnionAssignmentOperator(MD); |
| 7134 | |
| 7135 | if (MD->isLambdaStaticInvoker()) |
| 7136 | return this->emitLambdaStaticInvokerBody(MD); |
| 7137 | } |
| 7138 | |
| 7139 | // Regular functions. |
| 7140 | if (const auto *Body = F->getBody()) |
| 7141 | if (!visitStmt(S: Body)) |
| 7142 | return false; |
| 7143 | |
| 7144 | // Emit a guard return to protect against a code path missing one. |
| 7145 | if (F->getReturnType()->isVoidType()) |
| 7146 | return this->emitRetVoid(SourceInfo{}); |
| 7147 | return this->emitNoRet(SourceInfo{}); |
| 7148 | } |
| 7149 | |
| 7150 | static uint32_t getBitWidth(const Expr *E) { |
| 7151 | assert(E->refersToBitField()); |
| 7152 | const auto *ME = cast<MemberExpr>(Val: E); |
| 7153 | const auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl()); |
| 7154 | return FD->getBitWidthValue(); |
| 7155 | } |
| 7156 | |
| 7157 | template <class Emitter> |
| 7158 | bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) { |
| 7159 | if (E->containsErrors()) |
| 7160 | return false; |
| 7161 | |
| 7162 | const Expr *SubExpr = E->getSubExpr(); |
| 7163 | if (SubExpr->getType()->isAnyComplexType()) |
| 7164 | return this->VisitComplexUnaryOperator(E); |
| 7165 | if (SubExpr->getType()->isVectorType()) |
| 7166 | return this->VisitVectorUnaryOperator(E); |
| 7167 | if (SubExpr->getType()->isFixedPointType()) |
| 7168 | return this->VisitFixedPointUnaryOperator(E); |
| 7169 | OptPrimType T = classify(SubExpr->getType()); |
| 7170 | |
| 7171 | switch (E->getOpcode()) { |
| 7172 | case UO_PostInc: { // x++ |
| 7173 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 7174 | return this->emitInvalid(E); |
| 7175 | if (!T) |
| 7176 | return this->emitError(E); |
| 7177 | |
| 7178 | if (!this->visit(E: SubExpr)) |
| 7179 | return false; |
| 7180 | |
| 7181 | if (T == PT_Ptr) { |
| 7182 | if (!this->emitIncPtr(E)) |
| 7183 | return false; |
| 7184 | |
| 7185 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 7186 | } |
| 7187 | |
| 7188 | if (T == PT_Float) |
| 7189 | return DiscardResult ? this->emitIncfPop(getFPOptions(E), E) |
| 7190 | : this->emitIncf(getFPOptions(E), E); |
| 7191 | |
| 7192 | if (SubExpr->refersToBitField()) |
| 7193 | return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(), |
| 7194 | getBitWidth(E: SubExpr), E) |
| 7195 | : this->emitIncBitfield(*T, E->canOverflow(), |
| 7196 | getBitWidth(E: SubExpr), E); |
| 7197 | |
| 7198 | return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E) |
| 7199 | : this->emitInc(*T, E->canOverflow(), E); |
| 7200 | } |
| 7201 | case UO_PostDec: { // x-- |
| 7202 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 7203 | return this->emitInvalid(E); |
| 7204 | if (!T) |
| 7205 | return this->emitError(E); |
| 7206 | |
| 7207 | if (!this->visit(E: SubExpr)) |
| 7208 | return false; |
| 7209 | |
| 7210 | if (T == PT_Ptr) { |
| 7211 | if (!this->emitDecPtr(E)) |
| 7212 | return false; |
| 7213 | |
| 7214 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 7215 | } |
| 7216 | |
| 7217 | if (T == PT_Float) |
| 7218 | return DiscardResult ? this->emitDecfPop(getFPOptions(E), E) |
| 7219 | : this->emitDecf(getFPOptions(E), E); |
| 7220 | |
| 7221 | if (SubExpr->refersToBitField()) { |
| 7222 | return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(), |
| 7223 | getBitWidth(E: SubExpr), E) |
| 7224 | : this->emitDecBitfield(*T, E->canOverflow(), |
| 7225 | getBitWidth(E: SubExpr), E); |
| 7226 | } |
| 7227 | |
| 7228 | return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E) |
| 7229 | : this->emitDec(*T, E->canOverflow(), E); |
| 7230 | } |
| 7231 | case UO_PreInc: { // ++x |
| 7232 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 7233 | return this->emitInvalid(E); |
| 7234 | if (!T) |
| 7235 | return this->emitError(E); |
| 7236 | |
| 7237 | if (!this->visit(E: SubExpr)) |
| 7238 | return false; |
| 7239 | |
| 7240 | if (T == PT_Ptr) { |
| 7241 | if (!this->emitLoadPtr(E)) |
| 7242 | return false; |
| 7243 | if (!this->emitConstUint8(1, E)) |
| 7244 | return false; |
| 7245 | if (!this->emitAddOffsetUint8(E)) |
| 7246 | return false; |
| 7247 | return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E); |
| 7248 | } |
| 7249 | |
| 7250 | // Post-inc and pre-inc are the same if the value is to be discarded. |
| 7251 | if (DiscardResult) { |
| 7252 | if (T == PT_Float) |
| 7253 | return this->emitIncfPop(getFPOptions(E), E); |
| 7254 | if (SubExpr->refersToBitField()) |
| 7255 | return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(), |
| 7256 | getBitWidth(E: SubExpr), E) |
| 7257 | : this->emitIncBitfield(*T, E->canOverflow(), |
| 7258 | getBitWidth(E: SubExpr), E); |
| 7259 | return this->emitIncPop(*T, E->canOverflow(), E); |
| 7260 | } |
| 7261 | |
| 7262 | if (T == PT_Float) { |
| 7263 | const auto &TargetSemantics = Ctx.getFloatSemantics(T: E->getType()); |
| 7264 | if (!this->emitLoadFloat(E)) |
| 7265 | return false; |
| 7266 | APFloat F(TargetSemantics, 1); |
| 7267 | if (!this->emitFloat(F, Info: E)) |
| 7268 | return false; |
| 7269 | |
| 7270 | if (!this->emitAddf(getFPOptions(E), E)) |
| 7271 | return false; |
| 7272 | if (!this->emitStoreFloat(E)) |
| 7273 | return false; |
| 7274 | } else if (SubExpr->refersToBitField()) { |
| 7275 | assert(isIntegerOrBoolType(*T)); |
| 7276 | if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(E: SubExpr), |
| 7277 | E)) |
| 7278 | return false; |
| 7279 | } else { |
| 7280 | assert(isIntegerOrBoolType(*T)); |
| 7281 | if (!this->emitPreInc(*T, E->canOverflow(), E)) |
| 7282 | return false; |
| 7283 | } |
| 7284 | return E->isGLValue() || this->emitLoadPop(*T, E); |
| 7285 | } |
| 7286 | case UO_PreDec: { // --x |
| 7287 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 7288 | return this->emitInvalid(E); |
| 7289 | if (!T) |
| 7290 | return this->emitError(E); |
| 7291 | |
| 7292 | if (!this->visit(E: SubExpr)) |
| 7293 | return false; |
| 7294 | |
| 7295 | if (T == PT_Ptr) { |
| 7296 | if (!this->emitLoadPtr(E)) |
| 7297 | return false; |
| 7298 | if (!this->emitConstUint8(1, E)) |
| 7299 | return false; |
| 7300 | if (!this->emitSubOffsetUint8(E)) |
| 7301 | return false; |
| 7302 | return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E); |
| 7303 | } |
| 7304 | |
| 7305 | // Post-dec and pre-dec are the same if the value is to be discarded. |
| 7306 | if (DiscardResult) { |
| 7307 | if (T == PT_Float) |
| 7308 | return this->emitDecfPop(getFPOptions(E), E); |
| 7309 | if (SubExpr->refersToBitField()) |
| 7310 | return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(), |
| 7311 | getBitWidth(E: SubExpr), E) |
| 7312 | : this->emitDecBitfield(*T, E->canOverflow(), |
| 7313 | getBitWidth(E: SubExpr), E); |
| 7314 | return this->emitDecPop(*T, E->canOverflow(), E); |
| 7315 | } |
| 7316 | |
| 7317 | if (T == PT_Float) { |
| 7318 | const auto &TargetSemantics = Ctx.getFloatSemantics(T: E->getType()); |
| 7319 | if (!this->emitLoadFloat(E)) |
| 7320 | return false; |
| 7321 | APFloat F(TargetSemantics, 1); |
| 7322 | if (!this->emitFloat(F, Info: E)) |
| 7323 | return false; |
| 7324 | |
| 7325 | if (!this->emitSubf(getFPOptions(E), E)) |
| 7326 | return false; |
| 7327 | if (!this->emitStoreFloat(E)) |
| 7328 | return false; |
| 7329 | } else if (SubExpr->refersToBitField()) { |
| 7330 | assert(isIntegerOrBoolType(*T)); |
| 7331 | if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(E: SubExpr), |
| 7332 | E)) |
| 7333 | return false; |
| 7334 | } else { |
| 7335 | assert(isIntegerOrBoolType(*T)); |
| 7336 | if (!this->emitPreDec(*T, E->canOverflow(), E)) |
| 7337 | return false; |
| 7338 | } |
| 7339 | return E->isGLValue() || this->emitLoadPop(*T, E); |
| 7340 | } |
| 7341 | case UO_LNot: // !x |
| 7342 | if (!T) |
| 7343 | return this->emitError(E); |
| 7344 | |
| 7345 | if (DiscardResult) |
| 7346 | return this->discard(E: SubExpr); |
| 7347 | |
| 7348 | if (!this->visitBool(E: SubExpr)) |
| 7349 | return false; |
| 7350 | |
| 7351 | if (!this->emitInv(E)) |
| 7352 | return false; |
| 7353 | |
| 7354 | if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool) |
| 7355 | return this->emitCast(PT_Bool, ET, E); |
| 7356 | return true; |
| 7357 | case UO_Minus: // -x |
| 7358 | if (!T) |
| 7359 | return this->emitError(E); |
| 7360 | |
| 7361 | if (!this->visit(E: SubExpr)) |
| 7362 | return false; |
| 7363 | return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E); |
| 7364 | case UO_Plus: // +x |
| 7365 | if (!T) |
| 7366 | return this->emitError(E); |
| 7367 | |
| 7368 | if (!this->visit(E: SubExpr)) // noop |
| 7369 | return false; |
| 7370 | return DiscardResult ? this->emitPop(*T, E) : true; |
| 7371 | case UO_AddrOf: // &x |
| 7372 | if (E->getType()->isMemberPointerType()) { |
| 7373 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 7374 | // member can be formed. |
| 7375 | if (DiscardResult) |
| 7376 | return true; |
| 7377 | return this->emitGetMemberPtr(cast<DeclRefExpr>(Val: SubExpr)->getDecl(), E); |
| 7378 | } |
| 7379 | // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*' |
| 7380 | // operator, neither operator is evaluated and the result is as if both |
| 7381 | // were omitted. So '&*q' is just 'q' with no dereference; delegate to the |
| 7382 | // pointer operand directly instead of to the '*' (which would emit a null |
| 7383 | // check), so that e.g. '&*(int *)0' is not rejected. |
| 7384 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 7385 | const Expr *Sub = SubExpr->IgnoreParens(); |
| 7386 | if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub); |
| 7387 | Deref && Deref->getOpcode() == UO_Deref) |
| 7388 | return this->delegate(E: Deref->getSubExpr()); |
| 7389 | } |
| 7390 | // We should already have a pointer when we get here. |
| 7391 | return this->delegate(E: SubExpr); |
| 7392 | case UO_Deref: // *x |
| 7393 | if (DiscardResult) |
| 7394 | return this->discard(E: SubExpr); |
| 7395 | |
| 7396 | if (!this->visit(E: SubExpr)) |
| 7397 | return false; |
| 7398 | |
| 7399 | if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E)) |
| 7400 | return false; |
| 7401 | |
| 7402 | if (classifyPrim(SubExpr) == PT_Ptr) |
| 7403 | return this->emitNarrowPtr(E); |
| 7404 | return true; |
| 7405 | |
| 7406 | case UO_Not: // ~x |
| 7407 | if (!T) |
| 7408 | return this->emitError(E); |
| 7409 | |
| 7410 | if (!this->visit(E: SubExpr)) |
| 7411 | return false; |
| 7412 | return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E); |
| 7413 | case UO_Real: // __real x |
| 7414 | if (!T) |
| 7415 | return false; |
| 7416 | return this->delegate(E: SubExpr); |
| 7417 | case UO_Imag: { // __imag x |
| 7418 | if (!T) |
| 7419 | return false; |
| 7420 | if (!this->discard(E: SubExpr)) |
| 7421 | return false; |
| 7422 | return DiscardResult |
| 7423 | ? true |
| 7424 | : this->visitZeroInitializer(T: *T, QT: SubExpr->getType(), E: SubExpr); |
| 7425 | } |
| 7426 | case UO_Extension: |
| 7427 | return this->delegate(E: SubExpr); |
| 7428 | case UO_Coawait: |
| 7429 | assert(false && "Unhandled opcode" ); |
| 7430 | } |
| 7431 | |
| 7432 | return false; |
| 7433 | } |
| 7434 | |
| 7435 | template <class Emitter> |
| 7436 | bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) { |
| 7437 | const Expr *SubExpr = E->getSubExpr(); |
| 7438 | assert(SubExpr->getType()->isAnyComplexType()); |
| 7439 | |
| 7440 | if (DiscardResult) |
| 7441 | return this->discard(E: SubExpr); |
| 7442 | |
| 7443 | OptPrimType ResT = classify(E); |
| 7444 | auto prepareResult = [=]() -> bool { |
| 7445 | if (!ResT && !Initializing) { |
| 7446 | UnsignedOrNone LocalIndex = allocateLocal(Src: SubExpr); |
| 7447 | if (!LocalIndex) |
| 7448 | return false; |
| 7449 | return this->emitGetPtrLocal(*LocalIndex, E); |
| 7450 | } |
| 7451 | |
| 7452 | return true; |
| 7453 | }; |
| 7454 | |
| 7455 | // The offset of the temporary, if we created one. |
| 7456 | unsigned SubExprOffset = ~0u; |
| 7457 | auto createTemp = [=, &SubExprOffset]() -> bool { |
| 7458 | SubExprOffset = |
| 7459 | this->allocateLocalPrimitive(Src: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 7460 | if (!this->visit(E: SubExpr)) |
| 7461 | return false; |
| 7462 | return this->emitSetLocal(PT_Ptr, SubExprOffset, E); |
| 7463 | }; |
| 7464 | |
| 7465 | PrimType ElemT = classifyComplexElementType(T: SubExpr->getType()); |
| 7466 | auto getElem = [=](unsigned Offset, unsigned Index) -> bool { |
| 7467 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 7468 | return false; |
| 7469 | return this->emitArrayElemPop(ElemT, Index, E); |
| 7470 | }; |
| 7471 | |
| 7472 | switch (E->getOpcode()) { |
| 7473 | case UO_Minus: // -x |
| 7474 | if (!prepareResult()) |
| 7475 | return false; |
| 7476 | if (!createTemp()) |
| 7477 | return false; |
| 7478 | for (unsigned I = 0; I != 2; ++I) { |
| 7479 | if (!getElem(SubExprOffset, I)) |
| 7480 | return false; |
| 7481 | if (!this->emitNeg(ElemT, E)) |
| 7482 | return false; |
| 7483 | if (!this->emitInitElem(ElemT, I, E)) |
| 7484 | return false; |
| 7485 | } |
| 7486 | break; |
| 7487 | |
| 7488 | case UO_Plus: // +x |
| 7489 | case UO_AddrOf: // &x |
| 7490 | case UO_Deref: // *x |
| 7491 | return this->delegate(E: SubExpr); |
| 7492 | |
| 7493 | case UO_LNot: |
| 7494 | if (!this->visit(E: SubExpr)) |
| 7495 | return false; |
| 7496 | if (!this->emitComplexBoolCast(E: SubExpr)) |
| 7497 | return false; |
| 7498 | if (!this->emitInv(E)) |
| 7499 | return false; |
| 7500 | if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool) |
| 7501 | return this->emitCast(PT_Bool, ET, E); |
| 7502 | return true; |
| 7503 | |
| 7504 | case UO_Real: |
| 7505 | return this->emitComplexReal(SubExpr); |
| 7506 | |
| 7507 | case UO_Imag: |
| 7508 | if (!this->visit(E: SubExpr)) |
| 7509 | return false; |
| 7510 | |
| 7511 | if (SubExpr->isLValue()) { |
| 7512 | if (!this->emitConstUint8(1, E)) |
| 7513 | return false; |
| 7514 | return this->emitArrayElemPtrPopUint8(E); |
| 7515 | } |
| 7516 | |
| 7517 | // Since our _Complex implementation does not map to a primitive type, |
| 7518 | // we sometimes have to do the lvalue-to-rvalue conversion here manually. |
| 7519 | return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E); |
| 7520 | |
| 7521 | case UO_Not: // ~x |
| 7522 | if (!this->delegate(E: SubExpr)) |
| 7523 | return false; |
| 7524 | // Negate the imaginary component. |
| 7525 | if (!this->emitArrayElem(ElemT, 1, E)) |
| 7526 | return false; |
| 7527 | if (!this->emitNeg(ElemT, E)) |
| 7528 | return false; |
| 7529 | if (!this->emitInitElem(ElemT, 1, E)) |
| 7530 | return false; |
| 7531 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 7532 | |
| 7533 | case UO_Extension: |
| 7534 | return this->delegate(E: SubExpr); |
| 7535 | |
| 7536 | default: |
| 7537 | return this->emitInvalid(E); |
| 7538 | } |
| 7539 | |
| 7540 | return true; |
| 7541 | } |
| 7542 | |
| 7543 | template <class Emitter> |
| 7544 | bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) { |
| 7545 | const Expr *SubExpr = E->getSubExpr(); |
| 7546 | assert(SubExpr->getType()->isVectorType()); |
| 7547 | |
| 7548 | if (DiscardResult) |
| 7549 | return this->discard(E: SubExpr); |
| 7550 | |
| 7551 | auto UnaryOp = E->getOpcode(); |
| 7552 | if (UnaryOp == UO_Extension) |
| 7553 | return this->delegate(E: SubExpr); |
| 7554 | |
| 7555 | if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot && |
| 7556 | UnaryOp != UO_Not && UnaryOp != UO_AddrOf) |
| 7557 | return this->emitInvalid(E); |
| 7558 | |
| 7559 | // Nothing to do here. |
| 7560 | if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf) |
| 7561 | return this->delegate(E: SubExpr); |
| 7562 | |
| 7563 | if (!Initializing) { |
| 7564 | UnsignedOrNone LocalIndex = allocateLocal(Src: SubExpr); |
| 7565 | if (!LocalIndex) |
| 7566 | return false; |
| 7567 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 7568 | return false; |
| 7569 | } |
| 7570 | |
| 7571 | // The offset of the temporary, if we created one. |
| 7572 | unsigned SubExprOffset = |
| 7573 | this->allocateLocalPrimitive(Src: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 7574 | if (!this->visit(E: SubExpr)) |
| 7575 | return false; |
| 7576 | if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E)) |
| 7577 | return false; |
| 7578 | |
| 7579 | const auto *VecTy = SubExpr->getType()->getAs<VectorType>(); |
| 7580 | PrimType ElemT = classifyVectorElementType(T: SubExpr->getType()); |
| 7581 | auto getElem = [=](unsigned Offset, unsigned Index) -> bool { |
| 7582 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 7583 | return false; |
| 7584 | return this->emitArrayElemPop(ElemT, Index, E); |
| 7585 | }; |
| 7586 | |
| 7587 | switch (UnaryOp) { |
| 7588 | case UO_Minus: |
| 7589 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 7590 | if (!getElem(SubExprOffset, I)) |
| 7591 | return false; |
| 7592 | if (!this->emitNeg(ElemT, E)) |
| 7593 | return false; |
| 7594 | if (!this->emitInitElem(ElemT, I, E)) |
| 7595 | return false; |
| 7596 | } |
| 7597 | break; |
| 7598 | case UO_LNot: { // !x |
| 7599 | // In C++, the logic operators !, &&, || are available for vectors. !v is |
| 7600 | // equivalent to v == 0. |
| 7601 | // |
| 7602 | // The result of the comparison is a vector of the same width and number of |
| 7603 | // elements as the comparison operands with a signed integral element type. |
| 7604 | // |
| 7605 | // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html |
| 7606 | QualType ResultVecTy = E->getType(); |
| 7607 | PrimType ResultVecElemT = |
| 7608 | classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType()); |
| 7609 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 7610 | if (!getElem(SubExprOffset, I)) |
| 7611 | return false; |
| 7612 | // operator ! on vectors returns -1 for 'truth', so negate it. |
| 7613 | if (!this->emitPrimCast(FromT: ElemT, ToT: PT_Bool, ToQT: Ctx.getASTContext().BoolTy, E)) |
| 7614 | return false; |
| 7615 | if (!this->emitInv(E)) |
| 7616 | return false; |
| 7617 | if (!this->emitPrimCast(FromT: PT_Bool, ToT: ElemT, ToQT: VecTy->getElementType(), E)) |
| 7618 | return false; |
| 7619 | if (!this->emitNeg(ElemT, E)) |
| 7620 | return false; |
| 7621 | if (ElemT != ResultVecElemT && |
| 7622 | !this->emitPrimCast(FromT: ElemT, ToT: ResultVecElemT, ToQT: ResultVecTy, E)) |
| 7623 | return false; |
| 7624 | if (!this->emitInitElem(ResultVecElemT, I, E)) |
| 7625 | return false; |
| 7626 | } |
| 7627 | break; |
| 7628 | } |
| 7629 | case UO_Not: // ~x |
| 7630 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 7631 | if (!getElem(SubExprOffset, I)) |
| 7632 | return false; |
| 7633 | if (ElemT == PT_Bool) { |
| 7634 | if (!this->emitInv(E)) |
| 7635 | return false; |
| 7636 | } else { |
| 7637 | if (!this->emitComp(ElemT, E)) |
| 7638 | return false; |
| 7639 | } |
| 7640 | if (!this->emitInitElem(ElemT, I, E)) |
| 7641 | return false; |
| 7642 | } |
| 7643 | break; |
| 7644 | default: |
| 7645 | llvm_unreachable("Unsupported unary operators should be handled up front" ); |
| 7646 | } |
| 7647 | return true; |
| 7648 | } |
| 7649 | |
| 7650 | template <class Emitter> |
| 7651 | bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) { |
| 7652 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(Val: D)) { |
| 7653 | if (DiscardResult) |
| 7654 | return true; |
| 7655 | return this->emitConst(ECD->getInitVal(), E); |
| 7656 | } |
| 7657 | if (const auto *FuncDecl = dyn_cast<FunctionDecl>(Val: D)) { |
| 7658 | if (DiscardResult) |
| 7659 | return true; |
| 7660 | const Function *F = getFunction(FD: FuncDecl); |
| 7661 | return F && this->emitGetFnPtr(F, E); |
| 7662 | } |
| 7663 | if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Val: D)) { |
| 7664 | if (DiscardResult) |
| 7665 | return true; |
| 7666 | |
| 7667 | if (UnsignedOrNone Index = P.getOrCreateGlobal(VD: D)) { |
| 7668 | if (OptPrimType T = classify(D->getType())) { |
| 7669 | if (!this->visitAPValue(Val: TPOD->getValue(), ValType: *T, Info: E)) |
| 7670 | return false; |
| 7671 | return this->emitInitGlobal(*T, *Index, E); |
| 7672 | } |
| 7673 | |
| 7674 | if (!this->emitGetPtrGlobal(*Index, E)) |
| 7675 | return false; |
| 7676 | if (!this->visitAPValueInitializer(Val: TPOD->getValue(), Info: E, T: TPOD->getType())) |
| 7677 | return false; |
| 7678 | return this->emitFinishInit(E); |
| 7679 | } |
| 7680 | return false; |
| 7681 | } |
| 7682 | |
| 7683 | // References are implemented via pointers, so when we see a DeclRefExpr |
| 7684 | // pointing to a reference, we need to get its value directly (i.e. the |
| 7685 | // pointer to the actual value) instead of a pointer to the pointer to the |
| 7686 | // value. |
| 7687 | QualType DeclType = D->getType(); |
| 7688 | bool IsReference = DeclType->isReferenceType(); |
| 7689 | |
| 7690 | auto maybePopPtr = [&]() -> bool { |
| 7691 | if (DiscardResult) |
| 7692 | return this->emitPopPtr(E); |
| 7693 | return true; |
| 7694 | }; |
| 7695 | |
| 7696 | // Function parameters. |
| 7697 | // Note that it's important to check them first since we might have a local |
| 7698 | // variable created for a ParmVarDecl as well. |
| 7699 | if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: D)) { |
| 7700 | if (DiscardResult) |
| 7701 | return true; |
| 7702 | |
| 7703 | if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 && |
| 7704 | !DeclType->isIntegralOrEnumerationType()) { |
| 7705 | return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E), |
| 7706 | /*InitializerFailed=*/false, E); |
| 7707 | } |
| 7708 | if (auto It = this->Params.find(PVD); It != this->Params.end()) { |
| 7709 | if (IsReference || !It->second.IsPtr) |
| 7710 | return this->emitGetParam(classifyPrim(E), It->second.Index, E); |
| 7711 | |
| 7712 | return this->emitGetPtrParam(It->second.Index, E); |
| 7713 | } |
| 7714 | |
| 7715 | if (!Ctx.getLangOpts().CPlusPlus23 && IsReference) |
| 7716 | return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E), |
| 7717 | /*InitializerFailed=*/false, E); |
| 7718 | } |
| 7719 | // Local variables. |
| 7720 | if (auto It = Locals.find(Val: D); It != Locals.end()) { |
| 7721 | const unsigned Offset = It->second.Offset; |
| 7722 | if (IsReference) { |
| 7723 | assert(classifyPrim(E) == PT_Ptr); |
| 7724 | return this->emitGetRefLocal(Offset, E) && maybePopPtr(); |
| 7725 | } |
| 7726 | return this->emitGetPtrLocal(Offset, E) && maybePopPtr(); |
| 7727 | } |
| 7728 | // Global variables. |
| 7729 | if (auto GlobalIndex = P.getGlobal(VD: D)) { |
| 7730 | if (IsReference) { |
| 7731 | if (!Ctx.getLangOpts().CPlusPlus11) |
| 7732 | return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E); |
| 7733 | if (!Ctx.getLangOpts().CPlusPlus23) |
| 7734 | return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E); |
| 7735 | |
| 7736 | return this->emitGetRefGlobal(*GlobalIndex, E) && maybePopPtr(); |
| 7737 | } |
| 7738 | |
| 7739 | return this->emitGetPtrGlobal(*GlobalIndex, E) && maybePopPtr(); |
| 7740 | } |
| 7741 | |
| 7742 | // In case we need to re-visit a declaration. |
| 7743 | auto revisit = [&](const VarDecl *VD, |
| 7744 | bool IsConstexprUnknown = true) -> bool { |
| 7745 | llvm::SaveAndRestore CURS(this->VariablesAreConstexprUnknown, |
| 7746 | IsConstexprUnknown); |
| 7747 | if (!this->emitPushCC(VD->hasConstantInitialization(), E)) |
| 7748 | return false; |
| 7749 | auto VarState = this->visitDecl(VD); |
| 7750 | |
| 7751 | if (!this->emitPopCC(E)) |
| 7752 | return false; |
| 7753 | |
| 7754 | if (VarState.notCreated()) |
| 7755 | return true; |
| 7756 | if (!VarState) |
| 7757 | return false; |
| 7758 | // Retry. |
| 7759 | return this->visitDeclRef(D, E); |
| 7760 | }; |
| 7761 | |
| 7762 | if constexpr (!std::is_same_v<Emitter, EvalEmitter>) { |
| 7763 | // Lambda captures. |
| 7764 | if (auto It = this->LambdaCaptures.find(D); |
| 7765 | It != this->LambdaCaptures.end()) { |
| 7766 | auto [Offset, IsPtr] = It->second; |
| 7767 | |
| 7768 | if (IsPtr) |
| 7769 | return this->emitGetThisFieldPtr(Offset, E) && maybePopPtr(); |
| 7770 | return this->emitGetPtrThisField(Offset, E) && maybePopPtr(); |
| 7771 | } |
| 7772 | } |
| 7773 | |
| 7774 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E); |
| 7775 | DRE && DRE->refersToEnclosingVariableOrCapture()) { |
| 7776 | if (const auto *VD = dyn_cast<VarDecl>(Val: D); VD && VD->isInitCapture()) |
| 7777 | return revisit(VD); |
| 7778 | } |
| 7779 | |
| 7780 | if (const auto *BD = dyn_cast<BindingDecl>(Val: D)) |
| 7781 | return this->delegate(E: BD->getBinding()); |
| 7782 | |
| 7783 | // Avoid infinite recursion. |
| 7784 | if (D == InitializingDecl) { |
| 7785 | if (DiscardResult) |
| 7786 | return true; |
| 7787 | return this->emitDummyPtr(D, E); |
| 7788 | } |
| 7789 | |
| 7790 | // Try to lazily visit (or emit dummy pointers for) declarations |
| 7791 | // we haven't seen yet. |
| 7792 | const auto *VD = dyn_cast<VarDecl>(Val: D); |
| 7793 | if (!VD) |
| 7794 | return this->emitError(E); |
| 7795 | |
| 7796 | // For C. |
| 7797 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 7798 | if (VD->getInit() && !VD->getInit()->isValueDependent() && |
| 7799 | DeclType.isConstant(Ctx: Ctx.getASTContext()) && !VD->isWeak() && |
| 7800 | VD->evaluateValue()) |
| 7801 | return revisit(VD, /*IsConstexprUnknown=*/false); |
| 7802 | |
| 7803 | if (DiscardResult) |
| 7804 | return true; |
| 7805 | return this->emitDummyPtr(D, E); |
| 7806 | } |
| 7807 | |
| 7808 | // ... and C++. |
| 7809 | const auto typeShouldBeVisited = [&](QualType T) -> bool { |
| 7810 | if (T.isConstant(Ctx: Ctx.getASTContext())) |
| 7811 | return true; |
| 7812 | return T->isReferenceType(); |
| 7813 | }; |
| 7814 | |
| 7815 | if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) && |
| 7816 | typeShouldBeVisited(DeclType)) { |
| 7817 | if (const Expr *Init = VD->getAnyInitializer(); |
| 7818 | Init && !Init->isValueDependent()) { |
| 7819 | // Whether or not the evaluation is successul doesn't really matter |
| 7820 | // here -- we will create a global variable in any case, and that |
| 7821 | // will have the state of initializer evaluation attached. |
| 7822 | Expr::EvalResult Result; |
| 7823 | (void)Init->EvaluateAsInitializer(Ctx: Ctx.getASTContext(), VD, Result, IsConstantInitializer: true); |
| 7824 | return this->visitDeclRef(D, E); |
| 7825 | } |
| 7826 | return revisit(VD, !VD->isConstexpr() && DeclType->isReferenceType()); |
| 7827 | } |
| 7828 | |
| 7829 | // FIXME: The evaluateValue() check here is a little ridiculous, since |
| 7830 | // it will ultimately call into Context::evaluateAsInitializer(). In |
| 7831 | // other words, we're evaluating the initializer, just to know if we can |
| 7832 | // evaluate the initializer. |
| 7833 | if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() && |
| 7834 | !VD->getInit()->isValueDependent()) { |
| 7835 | if (VD->evaluateValue()) { |
| 7836 | bool IsConstexprUnknown = !DeclType.isConstant(Ctx: Ctx.getASTContext()) && |
| 7837 | !DeclType->isReferenceType(); |
| 7838 | // Revisit the variable declaration, but make sure it's associated with a |
| 7839 | // different evaluation, so e.g. mutable reads don't work on it. |
| 7840 | EvalIDScope _(Ctx); |
| 7841 | return revisit(VD, IsConstexprUnknown); |
| 7842 | } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference) |
| 7843 | return revisit(VD, /*IsConstexprUnknown=*/true); |
| 7844 | |
| 7845 | if (IsReference) |
| 7846 | return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E), |
| 7847 | /*InitializerFailed=*/true, E); |
| 7848 | } |
| 7849 | |
| 7850 | if (DiscardResult) |
| 7851 | return true; |
| 7852 | return this->emitDummyPtr( |
| 7853 | D, E, CU: Ctx.getLangOpts().CPlusPlus23 && DeclType->isReferenceType()); |
| 7854 | } |
| 7855 | |
| 7856 | template <class Emitter> |
| 7857 | bool Compiler<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 7858 | const auto *D = E->getDecl(); |
| 7859 | return this->visitDeclRef(D, E); |
| 7860 | } |
| 7861 | |
| 7862 | template <class Emitter> |
| 7863 | bool Compiler<Emitter>::VisitDesignatedInitUpdateExpr( |
| 7864 | const DesignatedInitUpdateExpr *E) { |
| 7865 | if (!this->visitInitializer(E: E->getBase())) |
| 7866 | return false; |
| 7867 | return this->visitInitializer(E: E->getUpdater()); |
| 7868 | } |
| 7869 | |
| 7870 | template <class Emitter> bool Compiler<Emitter>::emitCleanup() { |
| 7871 | for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) { |
| 7872 | if (!C->destroyLocals()) |
| 7873 | return false; |
| 7874 | } |
| 7875 | return true; |
| 7876 | } |
| 7877 | |
| 7878 | template <class Emitter> |
| 7879 | unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType, |
| 7880 | const QualType DerivedType) { |
| 7881 | const auto = [](QualType Ty) -> const CXXRecordDecl * { |
| 7882 | if (const auto *R = Ty->getPointeeCXXRecordDecl()) |
| 7883 | return R; |
| 7884 | return Ty->getAsCXXRecordDecl(); |
| 7885 | }; |
| 7886 | const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType); |
| 7887 | const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType); |
| 7888 | |
| 7889 | return Ctx.collectBaseOffset(BaseDecl, DerivedDecl); |
| 7890 | } |
| 7891 | |
| 7892 | /// Emit casts from a PrimType to another PrimType. |
| 7893 | template <class Emitter> |
| 7894 | bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT, |
| 7895 | QualType ToQT, const Expr *E) { |
| 7896 | |
| 7897 | if (FromT == PT_Float) { |
| 7898 | // Floating to floating. |
| 7899 | if (ToT == PT_Float) { |
| 7900 | const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(T: ToQT); |
| 7901 | return this->emitCastFP(ToSem, getRoundingMode(E), E); |
| 7902 | } |
| 7903 | |
| 7904 | if (ToT == PT_IntAP) |
| 7905 | return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(T: ToQT), |
| 7906 | getFPOptions(E), E); |
| 7907 | if (ToT == PT_IntAPS) |
| 7908 | return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(T: ToQT), |
| 7909 | getFPOptions(E), E); |
| 7910 | |
| 7911 | // Float to integral. |
| 7912 | if (isIntegerOrBoolType(T: ToT) || ToT == PT_Bool) |
| 7913 | return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E); |
| 7914 | } |
| 7915 | |
| 7916 | if (isIntegerOrBoolType(T: FromT) || FromT == PT_Bool) { |
| 7917 | if (ToT == PT_IntAP) |
| 7918 | return this->emitCastAP(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 7919 | if (ToT == PT_IntAPS) |
| 7920 | return this->emitCastAPS(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 7921 | |
| 7922 | // Integral to integral. |
| 7923 | if (isIntegerOrBoolType(T: ToT) || ToT == PT_Bool) |
| 7924 | return FromT != ToT ? this->emitCast(FromT, ToT, E) : true; |
| 7925 | |
| 7926 | if (ToT == PT_Float) { |
| 7927 | // Integral to floating. |
| 7928 | const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(T: ToQT); |
| 7929 | return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E); |
| 7930 | } |
| 7931 | } |
| 7932 | |
| 7933 | return false; |
| 7934 | } |
| 7935 | |
| 7936 | template <class Emitter> |
| 7937 | bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT, |
| 7938 | QualType ToQT, const Expr *E) { |
| 7939 | assert(FromT != ToT); |
| 7940 | |
| 7941 | if (ToT == PT_IntAP) |
| 7942 | return this->emitCastAP(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 7943 | if (ToT == PT_IntAPS) |
| 7944 | return this->emitCastAPS(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 7945 | |
| 7946 | return this->emitCast(FromT, ToT, E); |
| 7947 | } |
| 7948 | |
| 7949 | /// Emits __real(SubExpr) |
| 7950 | template <class Emitter> |
| 7951 | bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) { |
| 7952 | assert(SubExpr->getType()->isAnyComplexType()); |
| 7953 | |
| 7954 | if (DiscardResult) |
| 7955 | return this->discard(E: SubExpr); |
| 7956 | |
| 7957 | if (!this->visit(E: SubExpr)) |
| 7958 | return false; |
| 7959 | if (SubExpr->isLValue()) { |
| 7960 | if (!this->emitConstUint8(0, SubExpr)) |
| 7961 | return false; |
| 7962 | return this->emitArrayElemPtrPopUint8(SubExpr); |
| 7963 | } |
| 7964 | |
| 7965 | // Rvalue, load the actual element. |
| 7966 | return this->emitArrayElemPop(classifyComplexElementType(T: SubExpr->getType()), |
| 7967 | 0, SubExpr); |
| 7968 | } |
| 7969 | |
| 7970 | template <class Emitter> |
| 7971 | bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) { |
| 7972 | assert(!DiscardResult); |
| 7973 | PrimType ElemT = classifyComplexElementType(T: E->getType()); |
| 7974 | // We emit the expression (__real(E) != 0 || __imag(E) != 0) |
| 7975 | // for us, that means (bool)E[0] || (bool)E[1] |
| 7976 | if (!this->emitArrayElem(ElemT, 0, E)) |
| 7977 | return false; |
| 7978 | if (ElemT == PT_Float) { |
| 7979 | if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E)) |
| 7980 | return false; |
| 7981 | } else { |
| 7982 | if (!this->emitCast(ElemT, PT_Bool, E)) |
| 7983 | return false; |
| 7984 | } |
| 7985 | |
| 7986 | // We now have the bool value of E[0] on the stack. |
| 7987 | LabelTy LabelTrue = this->getLabel(); |
| 7988 | if (!this->jumpTrue(LabelTrue, E)) |
| 7989 | return false; |
| 7990 | |
| 7991 | if (!this->emitArrayElemPop(ElemT, 1, E)) |
| 7992 | return false; |
| 7993 | if (ElemT == PT_Float) { |
| 7994 | if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E)) |
| 7995 | return false; |
| 7996 | } else { |
| 7997 | if (!this->emitCast(ElemT, PT_Bool, E)) |
| 7998 | return false; |
| 7999 | } |
| 8000 | // Leave the boolean value of E[1] on the stack. |
| 8001 | LabelTy EndLabel = this->getLabel(); |
| 8002 | this->jump(EndLabel, E); |
| 8003 | |
| 8004 | this->emitLabel(LabelTrue); |
| 8005 | if (!this->emitPopPtr(E)) |
| 8006 | return false; |
| 8007 | if (!this->emitConstBool(true, E)) |
| 8008 | return false; |
| 8009 | |
| 8010 | this->fallthrough(EndLabel); |
| 8011 | this->emitLabel(EndLabel); |
| 8012 | |
| 8013 | return true; |
| 8014 | } |
| 8015 | |
| 8016 | template <class Emitter> |
| 8017 | bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS, |
| 8018 | const BinaryOperator *E) { |
| 8019 | assert(E->isComparisonOp()); |
| 8020 | assert(!Initializing); |
| 8021 | if (DiscardResult) |
| 8022 | return this->discard(E: LHS) && this->discard(E: RHS); |
| 8023 | |
| 8024 | PrimType ElemT; |
| 8025 | bool LHSIsComplex; |
| 8026 | unsigned LHSOffset; |
| 8027 | if (LHS->getType()->isAnyComplexType()) { |
| 8028 | LHSIsComplex = true; |
| 8029 | ElemT = classifyComplexElementType(T: LHS->getType()); |
| 8030 | LHSOffset = allocateLocalPrimitive(Src: LHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 8031 | if (!this->visit(E: LHS)) |
| 8032 | return false; |
| 8033 | if (!this->emitSetLocal(PT_Ptr, LHSOffset, E)) |
| 8034 | return false; |
| 8035 | } else { |
| 8036 | LHSIsComplex = false; |
| 8037 | PrimType LHST = classifyPrim(LHS->getType()); |
| 8038 | LHSOffset = this->allocateLocalPrimitive(Src: LHS, Ty: LHST, /*IsConst=*/true); |
| 8039 | if (!this->visit(E: LHS)) |
| 8040 | return false; |
| 8041 | if (!this->emitSetLocal(LHST, LHSOffset, E)) |
| 8042 | return false; |
| 8043 | } |
| 8044 | |
| 8045 | bool RHSIsComplex; |
| 8046 | unsigned RHSOffset; |
| 8047 | if (RHS->getType()->isAnyComplexType()) { |
| 8048 | RHSIsComplex = true; |
| 8049 | ElemT = classifyComplexElementType(T: RHS->getType()); |
| 8050 | RHSOffset = allocateLocalPrimitive(Src: RHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 8051 | if (!this->visit(E: RHS)) |
| 8052 | return false; |
| 8053 | if (!this->emitSetLocal(PT_Ptr, RHSOffset, E)) |
| 8054 | return false; |
| 8055 | } else { |
| 8056 | RHSIsComplex = false; |
| 8057 | PrimType RHST = classifyPrim(RHS->getType()); |
| 8058 | RHSOffset = this->allocateLocalPrimitive(Src: RHS, Ty: RHST, /*IsConst=*/true); |
| 8059 | if (!this->visit(E: RHS)) |
| 8060 | return false; |
| 8061 | if (!this->emitSetLocal(RHST, RHSOffset, E)) |
| 8062 | return false; |
| 8063 | } |
| 8064 | |
| 8065 | auto getElem = [&](unsigned LocalOffset, unsigned Index, |
| 8066 | bool IsComplex) -> bool { |
| 8067 | if (IsComplex) { |
| 8068 | if (!this->emitGetLocal(PT_Ptr, LocalOffset, E)) |
| 8069 | return false; |
| 8070 | return this->emitArrayElemPop(ElemT, Index, E); |
| 8071 | } |
| 8072 | return this->emitGetLocal(ElemT, LocalOffset, E); |
| 8073 | }; |
| 8074 | |
| 8075 | for (unsigned I = 0; I != 2; ++I) { |
| 8076 | // Get both values. |
| 8077 | if (!getElem(LHSOffset, I, LHSIsComplex)) |
| 8078 | return false; |
| 8079 | if (!getElem(RHSOffset, I, RHSIsComplex)) |
| 8080 | return false; |
| 8081 | // And compare them. |
| 8082 | if (!this->emitEQ(ElemT, E)) |
| 8083 | return false; |
| 8084 | |
| 8085 | if (!this->emitCastBoolUint8(E)) |
| 8086 | return false; |
| 8087 | } |
| 8088 | |
| 8089 | // We now have two bool values on the stack. Compare those. |
| 8090 | if (!this->emitAddUint8(E)) |
| 8091 | return false; |
| 8092 | if (!this->emitConstUint8(2, E)) |
| 8093 | return false; |
| 8094 | |
| 8095 | if (E->getOpcode() == BO_EQ) { |
| 8096 | if (!this->emitEQUint8(E)) |
| 8097 | return false; |
| 8098 | } else if (E->getOpcode() == BO_NE) { |
| 8099 | if (!this->emitNEUint8(E)) |
| 8100 | return false; |
| 8101 | } else |
| 8102 | return false; |
| 8103 | |
| 8104 | // In C, this returns an int. |
| 8105 | if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool) |
| 8106 | return this->emitCast(PT_Bool, ResT, E); |
| 8107 | return true; |
| 8108 | } |
| 8109 | |
| 8110 | /// When calling this, we have a pointer of the local-to-destroy |
| 8111 | /// on the stack. |
| 8112 | /// Emit destruction of record types (or arrays of record types). |
| 8113 | template <class Emitter> |
| 8114 | bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R, |
| 8115 | SourceInfo Loc) { |
| 8116 | assert(R); |
| 8117 | assert(!R->hasTrivialDtor()); |
| 8118 | const CXXDestructorDecl *Dtor = R->getDestructor(); |
| 8119 | assert(Dtor); |
| 8120 | const Function *DtorFunc = getFunction(FD: Dtor); |
| 8121 | if (!DtorFunc) |
| 8122 | return false; |
| 8123 | assert(DtorFunc->hasThisPointer()); |
| 8124 | assert(DtorFunc->getNumParams() == 1); |
| 8125 | return this->emitCall(DtorFunc, 0, Loc); |
| 8126 | } |
| 8127 | /// When calling this, we have a pointer of the local-to-destroy |
| 8128 | /// on the stack. |
| 8129 | /// Emit destruction of record types (or arrays of record types). |
| 8130 | template <class Emitter> |
| 8131 | bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc, |
| 8132 | SourceInfo Loc) { |
| 8133 | assert(Desc); |
| 8134 | assert(!Desc->hasTrivialDtor()); |
| 8135 | |
| 8136 | // Arrays. |
| 8137 | if (Desc->isArray()) { |
| 8138 | const Descriptor *ElemDesc = Desc->ElemDesc; |
| 8139 | assert(ElemDesc); |
| 8140 | |
| 8141 | unsigned N = Desc->getNumElems(); |
| 8142 | if (N == 0) |
| 8143 | return this->emitPopPtr(Loc); |
| 8144 | |
| 8145 | for (ssize_t I = N - 1; I >= 1; --I) { |
| 8146 | if (!this->emitConstUint64(I, Loc)) |
| 8147 | return false; |
| 8148 | if (!this->emitArrayElemPtrUint64(Loc)) |
| 8149 | return false; |
| 8150 | if (!this->emitDestructionPop(Desc: ElemDesc, Loc)) |
| 8151 | return false; |
| 8152 | } |
| 8153 | // Last iteration, removes the instance pointer from the stack. |
| 8154 | if (!this->emitConstUint64(0, Loc)) |
| 8155 | return false; |
| 8156 | if (!this->emitArrayElemPtrPopUint64(Loc)) |
| 8157 | return false; |
| 8158 | return this->emitDestructionPop(Desc: ElemDesc, Loc); |
| 8159 | } |
| 8160 | |
| 8161 | assert(Desc->ElemRecord); |
| 8162 | assert(!Desc->ElemRecord->hasTrivialDtor()); |
| 8163 | return this->emitRecordDestructionPop(R: Desc->ElemRecord, Loc); |
| 8164 | } |
| 8165 | |
| 8166 | /// Create a dummy pointer for the given decl (or expr) and |
| 8167 | /// push a pointer to it on the stack. |
| 8168 | template <class Emitter> |
| 8169 | bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E, bool CU) { |
| 8170 | assert(!DiscardResult && "Should've been checked before" ); |
| 8171 | unsigned DummyID = P.getOrCreateDummy(D, IsConstexprUnknown: CU); |
| 8172 | |
| 8173 | if (!this->emitGetPtrGlobal(DummyID, E)) |
| 8174 | return false; |
| 8175 | if (E->getType()->isVoidType()) |
| 8176 | return true; |
| 8177 | |
| 8178 | // Convert the dummy pointer to another pointer type if we have to. |
| 8179 | if (PrimType PT = classifyPrim(E); PT != PT_Ptr) { |
| 8180 | if (isPtrType(T: PT)) |
| 8181 | return this->emitDecayPtr(PT_Ptr, PT, E); |
| 8182 | return false; |
| 8183 | } |
| 8184 | return true; |
| 8185 | } |
| 8186 | |
| 8187 | template <class Emitter> |
| 8188 | bool Compiler<Emitter>::emitFloat(const APFloat &F, SourceInfo Info) { |
| 8189 | if (Floating::singleWord(F.getSemantics())) |
| 8190 | return this->emitConstFloat(Floating(F), Info); |
| 8191 | |
| 8192 | APInt I = F.bitcastToAPInt(); |
| 8193 | return this->emitConstFloat( |
| 8194 | Floating(const_cast<uint64_t *>(I.getRawData()), |
| 8195 | llvm::APFloatBase::SemanticsToEnum(Sem: F.getSemantics())), |
| 8196 | Info); |
| 8197 | } |
| 8198 | |
| 8199 | // This function is constexpr if and only if To, From, and the types of |
| 8200 | // all subobjects of To and From are types T such that... |
| 8201 | // (3.1) - is_union_v<T> is false; |
| 8202 | // (3.2) - is_pointer_v<T> is false; |
| 8203 | // (3.3) - is_member_pointer_v<T> is false; |
| 8204 | // (3.4) - is_volatile_v<T> is false; and |
| 8205 | // (3.5) - T has no non-static data members of reference type |
| 8206 | template <class Emitter> |
| 8207 | bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) { |
| 8208 | const Expr *SubExpr = E->getSubExpr(); |
| 8209 | QualType FromType = SubExpr->getType(); |
| 8210 | QualType ToType = E->getType(); |
| 8211 | OptPrimType ToT = classify(ToType); |
| 8212 | |
| 8213 | assert(!ToType->isReferenceType()); |
| 8214 | |
| 8215 | // Prepare storage for the result in case we discard. |
| 8216 | if (DiscardResult && !Initializing && !ToT) { |
| 8217 | UnsignedOrNone LocalIndex = allocateLocal(Src: E); |
| 8218 | if (!LocalIndex) |
| 8219 | return false; |
| 8220 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 8221 | return false; |
| 8222 | } |
| 8223 | |
| 8224 | // Get a pointer to the value-to-cast on the stack. |
| 8225 | // For CK_LValueToRValueBitCast, this is always an lvalue and |
| 8226 | // we later assume it to be one (i.e. a PT_Ptr). However, |
| 8227 | // we call this function for other utility methods where |
| 8228 | // a bitcast might be useful, so convert it to a PT_Ptr in that case. |
| 8229 | if (SubExpr->isGLValue() || FromType->isVectorType()) { |
| 8230 | if (!this->visit(E: SubExpr)) |
| 8231 | return false; |
| 8232 | } else if (OptPrimType FromT = classify(SubExpr)) { |
| 8233 | unsigned TempOffset = |
| 8234 | allocateLocalPrimitive(Src: SubExpr, Ty: *FromT, /*IsConst=*/true); |
| 8235 | if (!this->visit(E: SubExpr)) |
| 8236 | return false; |
| 8237 | if (!this->emitSetLocal(*FromT, TempOffset, E)) |
| 8238 | return false; |
| 8239 | if (!this->emitGetPtrLocal(TempOffset, E)) |
| 8240 | return false; |
| 8241 | } else { |
| 8242 | return false; |
| 8243 | } |
| 8244 | |
| 8245 | if (!ToT) { |
| 8246 | if (!this->emitBitCast(E)) |
| 8247 | return false; |
| 8248 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 8249 | } |
| 8250 | assert(ToT); |
| 8251 | |
| 8252 | const llvm::fltSemantics *TargetSemantics = nullptr; |
| 8253 | if (ToT == PT_Float) |
| 8254 | TargetSemantics = &Ctx.getFloatSemantics(T: ToType); |
| 8255 | |
| 8256 | // Conversion to a primitive type. FromType can be another |
| 8257 | // primitive type, or a record/array. |
| 8258 | bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(K: BuiltinType::UChar) || |
| 8259 | ToType->isSpecificBuiltinType(K: BuiltinType::Char_U)); |
| 8260 | uint32_t ResultBitWidth = std::max(a: Ctx.getBitWidth(T: ToType), b: 8u); |
| 8261 | |
| 8262 | if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(), |
| 8263 | ResultBitWidth, TargetSemantics, |
| 8264 | ToType.getTypePtr(), E)) |
| 8265 | return false; |
| 8266 | |
| 8267 | if (DiscardResult) |
| 8268 | return this->emitPop(*ToT, E); |
| 8269 | |
| 8270 | return true; |
| 8271 | } |
| 8272 | |
| 8273 | /// Replicate a scalar value into every scalar element of an aggregate. |
| 8274 | /// The scalar is stored in a local at \p SrcOffset and a pointer to the |
| 8275 | /// destination must be on top of the interpreter stack. Each element receives |
| 8276 | /// the scalar, cast to its own type. |
| 8277 | template <class Emitter> |
| 8278 | bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT, |
| 8279 | unsigned SrcOffset, |
| 8280 | QualType DestType, |
| 8281 | const Expr *E) { |
| 8282 | // Vectors and matrices are treated as flat sequences of elements. |
| 8283 | unsigned NumElems = 0; |
| 8284 | QualType ElemType; |
| 8285 | if (const auto *VT = DestType->getAs<VectorType>()) { |
| 8286 | NumElems = VT->getNumElements(); |
| 8287 | ElemType = VT->getElementType(); |
| 8288 | } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) { |
| 8289 | NumElems = MT->getNumElementsFlattened(); |
| 8290 | ElemType = MT->getElementType(); |
| 8291 | } |
| 8292 | if (NumElems > 0) { |
| 8293 | PrimType ElemT = classifyPrim(ElemType); |
| 8294 | for (unsigned I = 0; I != NumElems; ++I) { |
| 8295 | if (!this->emitGetLocal(SrcT, SrcOffset, E)) |
| 8296 | return false; |
| 8297 | if (!this->emitPrimCast(FromT: SrcT, ToT: ElemT, ToQT: ElemType, E)) |
| 8298 | return false; |
| 8299 | if (!this->emitInitElem(ElemT, I, E)) |
| 8300 | return false; |
| 8301 | } |
| 8302 | return true; |
| 8303 | } |
| 8304 | |
| 8305 | // Arrays: primitive elements are filled directly; composite elements |
| 8306 | // require recursion into each sub-aggregate. |
| 8307 | if (const auto *AT = DestType->getAsArrayTypeUnsafe()) { |
| 8308 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 8309 | QualType ArrElemType = CAT->getElementType(); |
| 8310 | unsigned ArrSize = CAT->getZExtSize(); |
| 8311 | |
| 8312 | if (OptPrimType ElemT = classify(ArrElemType)) { |
| 8313 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 8314 | if (!this->emitGetLocal(SrcT, SrcOffset, E)) |
| 8315 | return false; |
| 8316 | if (!this->emitPrimCast(FromT: SrcT, ToT: *ElemT, ToQT: ArrElemType, E)) |
| 8317 | return false; |
| 8318 | if (!this->emitInitElem(*ElemT, I, E)) |
| 8319 | return false; |
| 8320 | } |
| 8321 | } else { |
| 8322 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 8323 | if (!this->emitConstUint32(I, E)) |
| 8324 | return false; |
| 8325 | if (!this->emitArrayElemPtrUint32(E)) |
| 8326 | return false; |
| 8327 | if (!emitHLSLAggregateSplat(SrcT, SrcOffset, DestType: ArrElemType, E)) |
| 8328 | return false; |
| 8329 | if (!this->emitFinishInitPop(E)) |
| 8330 | return false; |
| 8331 | } |
| 8332 | } |
| 8333 | return true; |
| 8334 | } |
| 8335 | |
| 8336 | // Records: fill base classes first, then named fields in declaration |
| 8337 | // order. |
| 8338 | if (DestType->isRecordType()) { |
| 8339 | const Record *R = getRecord(DestType); |
| 8340 | if (!R) |
| 8341 | return false; |
| 8342 | |
| 8343 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 8344 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) { |
| 8345 | const Record::Base *B = R->getBase(T: BS.getType()); |
| 8346 | assert(B); |
| 8347 | if (!this->emitGetPtrBase(B->Offset, E)) |
| 8348 | return false; |
| 8349 | if (!emitHLSLAggregateSplat(SrcT, SrcOffset, DestType: BS.getType(), E)) |
| 8350 | return false; |
| 8351 | if (!this->emitFinishInitPop(E)) |
| 8352 | return false; |
| 8353 | } |
| 8354 | } |
| 8355 | |
| 8356 | for (const Record::Field &F : R->fields()) { |
| 8357 | if (F.isUnnamedBitField()) |
| 8358 | continue; |
| 8359 | |
| 8360 | QualType FieldType = F.Decl->getType(); |
| 8361 | if (OptPrimType FieldT = classify(FieldType)) { |
| 8362 | if (!this->emitGetLocal(SrcT, SrcOffset, E)) |
| 8363 | return false; |
| 8364 | if (!this->emitPrimCast(FromT: SrcT, ToT: *FieldT, ToQT: FieldType, E)) |
| 8365 | return false; |
| 8366 | if (F.isBitField()) { |
| 8367 | if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E)) |
| 8368 | return false; |
| 8369 | } else { |
| 8370 | if (!this->emitInitField(*FieldT, F.Offset, E)) |
| 8371 | return false; |
| 8372 | } |
| 8373 | } else { |
| 8374 | if (!this->emitGetPtrField(F.Offset, E)) |
| 8375 | return false; |
| 8376 | if (!emitHLSLAggregateSplat(SrcT, SrcOffset, DestType: FieldType, E)) |
| 8377 | return false; |
| 8378 | if (!this->emitPopPtr(E)) |
| 8379 | return false; |
| 8380 | } |
| 8381 | } |
| 8382 | return true; |
| 8383 | } |
| 8384 | |
| 8385 | return false; |
| 8386 | } |
| 8387 | |
| 8388 | /// Return the total number of scalar elements in a type. This is used |
| 8389 | /// to cap how many source elements are extracted during an elementwise cast, |
| 8390 | /// so we never flatten more than the destination can hold. |
| 8391 | template <class Emitter> |
| 8392 | unsigned Compiler<Emitter>::countHLSLFlatElements(QualType Ty) { |
| 8393 | // Vector and matrix types are treated as flat sequences of elements. |
| 8394 | if (const auto *VT = Ty->getAs<VectorType>()) |
| 8395 | return VT->getNumElements(); |
| 8396 | if (const auto *MT = Ty->getAs<ConstantMatrixType>()) |
| 8397 | return MT->getNumElementsFlattened(); |
| 8398 | // Arrays: total count is array size * scalar elements per element. |
| 8399 | if (const auto *AT = Ty->getAsArrayTypeUnsafe()) { |
| 8400 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 8401 | return CAT->getZExtSize() * countHLSLFlatElements(Ty: CAT->getElementType()); |
| 8402 | } |
| 8403 | // Records: sum scalar element counts of base classes and named fields. |
| 8404 | if (Ty->isRecordType()) { |
| 8405 | const Record *R = getRecord(Ty); |
| 8406 | if (!R) |
| 8407 | return 0; |
| 8408 | unsigned Count = 0; |
| 8409 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 8410 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) |
| 8411 | Count += countHLSLFlatElements(Ty: BS.getType()); |
| 8412 | } |
| 8413 | for (const Record::Field &F : R->fields()) { |
| 8414 | if (F.isUnnamedBitField()) |
| 8415 | continue; |
| 8416 | Count += countHLSLFlatElements(Ty: F.Decl->getType()); |
| 8417 | } |
| 8418 | return Count; |
| 8419 | } |
| 8420 | // Scalar primitive types contribute one element. |
| 8421 | if (canClassify(Ty)) |
| 8422 | return 1; |
| 8423 | return 0; |
| 8424 | } |
| 8425 | |
| 8426 | /// Walk a source aggregate and extract every scalar element into its own local |
| 8427 | /// variable. The results are appended to \p Elements in declaration order, |
| 8428 | /// stopping once \p MaxElements have been collected. A pointer to the |
| 8429 | /// source aggregate must be stored in the local at \p SrcOffset. |
| 8430 | template <class Emitter> |
| 8431 | bool Compiler<Emitter>::emitHLSLFlattenAggregate( |
| 8432 | QualType SrcType, unsigned SrcOffset, |
| 8433 | SmallVectorImpl<HLSLFlatElement> &Elements, unsigned MaxElements, |
| 8434 | const Expr *E) { |
| 8435 | |
| 8436 | // Save a scalar value from the stack into a new local and record it. |
| 8437 | auto saveToLocal = [&](PrimType T) -> bool { |
| 8438 | unsigned Offset = allocateLocalPrimitive(Src: E, Ty: T, /*IsConst=*/true); |
| 8439 | if (!this->emitSetLocal(T, Offset, E)) |
| 8440 | return false; |
| 8441 | Elements.push_back({Offset, T}); |
| 8442 | return true; |
| 8443 | }; |
| 8444 | |
| 8445 | // Save a pointer from the stack into a new local for later use. |
| 8446 | auto savePtrToLocal = [&]() -> UnsignedOrNone { |
| 8447 | unsigned Offset = allocateLocalPrimitive(Src: E, Ty: PT_Ptr, /*IsConst=*/true); |
| 8448 | if (!this->emitSetLocal(PT_Ptr, Offset, E)) |
| 8449 | return std::nullopt; |
| 8450 | return Offset; |
| 8451 | }; |
| 8452 | |
| 8453 | // Vectors and matrices are flat sequences of elements. |
| 8454 | unsigned NumElems = 0; |
| 8455 | QualType ElemType; |
| 8456 | if (const auto *VT = SrcType->getAs<VectorType>()) { |
| 8457 | NumElems = VT->getNumElements(); |
| 8458 | ElemType = VT->getElementType(); |
| 8459 | } else if (const auto *MT = SrcType->getAs<ConstantMatrixType>()) { |
| 8460 | NumElems = MT->getNumElementsFlattened(); |
| 8461 | ElemType = MT->getElementType(); |
| 8462 | } |
| 8463 | if (NumElems > 0) { |
| 8464 | PrimType ElemT = classifyPrim(ElemType); |
| 8465 | for (unsigned I = 0; I != NumElems && Elements.size() < MaxElements; ++I) { |
| 8466 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 8467 | return false; |
| 8468 | if (!this->emitArrayElemPop(ElemT, I, E)) |
| 8469 | return false; |
| 8470 | if (!saveToLocal(ElemT)) |
| 8471 | return false; |
| 8472 | } |
| 8473 | return true; |
| 8474 | } |
| 8475 | |
| 8476 | // Arrays: primitive elements are extracted directly; composite elements |
| 8477 | // require recursion into each sub-aggregate. |
| 8478 | if (const auto *AT = SrcType->getAsArrayTypeUnsafe()) { |
| 8479 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 8480 | QualType ArrElemType = CAT->getElementType(); |
| 8481 | unsigned ArrSize = CAT->getZExtSize(); |
| 8482 | |
| 8483 | if (OptPrimType ElemT = classify(ArrElemType)) { |
| 8484 | for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) { |
| 8485 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 8486 | return false; |
| 8487 | if (!this->emitArrayElemPop(*ElemT, I, E)) |
| 8488 | return false; |
| 8489 | if (!saveToLocal(*ElemT)) |
| 8490 | return false; |
| 8491 | } |
| 8492 | } else { |
| 8493 | for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) { |
| 8494 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 8495 | return false; |
| 8496 | if (!this->emitConstUint32(I, E)) |
| 8497 | return false; |
| 8498 | if (!this->emitArrayElemPtrPopUint32(E)) |
| 8499 | return false; |
| 8500 | UnsignedOrNone ElemPtrOffset = savePtrToLocal(); |
| 8501 | if (!ElemPtrOffset) |
| 8502 | return false; |
| 8503 | if (!emitHLSLFlattenAggregate(SrcType: ArrElemType, SrcOffset: *ElemPtrOffset, Elements, |
| 8504 | MaxElements, E)) |
| 8505 | return false; |
| 8506 | } |
| 8507 | } |
| 8508 | return true; |
| 8509 | } |
| 8510 | |
| 8511 | // Records: base classes come first, then named fields in declaration |
| 8512 | // order. |
| 8513 | if (SrcType->isRecordType()) { |
| 8514 | const Record *R = getRecord(SrcType); |
| 8515 | if (!R) |
| 8516 | return false; |
| 8517 | |
| 8518 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 8519 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) { |
| 8520 | if (Elements.size() >= MaxElements) |
| 8521 | break; |
| 8522 | const Record::Base *B = R->getBase(T: BS.getType()); |
| 8523 | assert(B); |
| 8524 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 8525 | return false; |
| 8526 | if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, E)) |
| 8527 | return false; |
| 8528 | UnsignedOrNone BasePtrOffset = savePtrToLocal(); |
| 8529 | if (!BasePtrOffset) |
| 8530 | return false; |
| 8531 | if (!emitHLSLFlattenAggregate(SrcType: BS.getType(), SrcOffset: *BasePtrOffset, Elements, |
| 8532 | MaxElements, E)) |
| 8533 | return false; |
| 8534 | } |
| 8535 | } |
| 8536 | |
| 8537 | for (const Record::Field &F : R->fields()) { |
| 8538 | if (Elements.size() >= MaxElements) |
| 8539 | break; |
| 8540 | if (F.isUnnamedBitField()) |
| 8541 | continue; |
| 8542 | |
| 8543 | QualType FieldType = F.Decl->getType(); |
| 8544 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 8545 | return false; |
| 8546 | if (!this->emitGetPtrFieldPop(F.Offset, E)) |
| 8547 | return false; |
| 8548 | |
| 8549 | if (OptPrimType FieldT = classify(FieldType)) { |
| 8550 | if (!this->emitLoadPop(*FieldT, E)) |
| 8551 | return false; |
| 8552 | if (!saveToLocal(*FieldT)) |
| 8553 | return false; |
| 8554 | } else { |
| 8555 | UnsignedOrNone FieldPtrOffset = savePtrToLocal(); |
| 8556 | if (!FieldPtrOffset) |
| 8557 | return false; |
| 8558 | if (!emitHLSLFlattenAggregate(SrcType: FieldType, SrcOffset: *FieldPtrOffset, Elements, |
| 8559 | MaxElements, E)) |
| 8560 | return false; |
| 8561 | } |
| 8562 | } |
| 8563 | return true; |
| 8564 | } |
| 8565 | |
| 8566 | return false; |
| 8567 | } |
| 8568 | |
| 8569 | /// Populate an HLSL aggregate from a flat list of previously extracted source |
| 8570 | /// elements, casting each to the corresponding destination element type. |
| 8571 | /// \p ElemIdx tracks the current position in \p Elements and is advanced as |
| 8572 | /// elements are consumed. A pointer to the destination must be on top of the |
| 8573 | /// interpreter stack. |
| 8574 | template <class Emitter> |
| 8575 | bool Compiler<Emitter>::emitHLSLConstructAggregate( |
| 8576 | QualType DestType, ArrayRef<HLSLFlatElement> Elements, unsigned &ElemIdx, |
| 8577 | const Expr *E) { |
| 8578 | |
| 8579 | // Consume the next source element, cast it, and leave it on the stack. |
| 8580 | auto loadAndCast = [&](PrimType DestT, QualType DestQT) -> bool { |
| 8581 | const auto &Src = Elements[ElemIdx++]; |
| 8582 | if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E)) |
| 8583 | return false; |
| 8584 | return this->emitPrimCast(FromT: Src.Type, ToT: DestT, ToQT: DestQT, E); |
| 8585 | }; |
| 8586 | |
| 8587 | // Vectors and matrices are flat sequences of elements. |
| 8588 | unsigned NumElems = 0; |
| 8589 | QualType ElemType; |
| 8590 | if (const auto *VT = DestType->getAs<VectorType>()) { |
| 8591 | NumElems = VT->getNumElements(); |
| 8592 | ElemType = VT->getElementType(); |
| 8593 | } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) { |
| 8594 | NumElems = MT->getNumElementsFlattened(); |
| 8595 | ElemType = MT->getElementType(); |
| 8596 | } |
| 8597 | if (NumElems > 0) { |
| 8598 | PrimType DestElemT = classifyPrim(ElemType); |
| 8599 | for (unsigned I = 0; I != NumElems; ++I) { |
| 8600 | if (!loadAndCast(DestElemT, ElemType)) |
| 8601 | return false; |
| 8602 | if (!this->emitInitElem(DestElemT, I, E)) |
| 8603 | return false; |
| 8604 | } |
| 8605 | return true; |
| 8606 | } |
| 8607 | |
| 8608 | // Arrays: primitive elements are filled directly; composite elements |
| 8609 | // require recursion into each sub-aggregate. |
| 8610 | if (const auto *AT = DestType->getAsArrayTypeUnsafe()) { |
| 8611 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 8612 | QualType ArrElemType = CAT->getElementType(); |
| 8613 | unsigned ArrSize = CAT->getZExtSize(); |
| 8614 | |
| 8615 | if (OptPrimType ElemT = classify(ArrElemType)) { |
| 8616 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 8617 | if (!loadAndCast(*ElemT, ArrElemType)) |
| 8618 | return false; |
| 8619 | if (!this->emitInitElem(*ElemT, I, E)) |
| 8620 | return false; |
| 8621 | } |
| 8622 | } else { |
| 8623 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 8624 | if (!this->emitConstUint32(I, E)) |
| 8625 | return false; |
| 8626 | if (!this->emitArrayElemPtrUint32(E)) |
| 8627 | return false; |
| 8628 | if (!emitHLSLConstructAggregate(ArrElemType, Elements, ElemIdx, E)) |
| 8629 | return false; |
| 8630 | if (!this->emitFinishInitPop(E)) |
| 8631 | return false; |
| 8632 | } |
| 8633 | } |
| 8634 | return true; |
| 8635 | } |
| 8636 | |
| 8637 | // Records: base classes come first, then named fields in declaration |
| 8638 | // order. |
| 8639 | if (DestType->isRecordType()) { |
| 8640 | const Record *R = getRecord(DestType); |
| 8641 | if (!R) |
| 8642 | return false; |
| 8643 | |
| 8644 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 8645 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) { |
| 8646 | const Record::Base *B = R->getBase(T: BS.getType()); |
| 8647 | assert(B); |
| 8648 | if (!this->emitGetPtrBase(B->Offset, E)) |
| 8649 | return false; |
| 8650 | if (!emitHLSLConstructAggregate(BS.getType(), Elements, ElemIdx, E)) |
| 8651 | return false; |
| 8652 | if (!this->emitFinishInitPop(E)) |
| 8653 | return false; |
| 8654 | } |
| 8655 | } |
| 8656 | |
| 8657 | for (const Record::Field &F : R->fields()) { |
| 8658 | if (F.isUnnamedBitField()) |
| 8659 | continue; |
| 8660 | |
| 8661 | QualType FieldType = F.Decl->getType(); |
| 8662 | if (OptPrimType FieldT = classify(FieldType)) { |
| 8663 | if (!loadAndCast(*FieldT, FieldType)) |
| 8664 | return false; |
| 8665 | if (F.isBitField()) { |
| 8666 | if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E)) |
| 8667 | return false; |
| 8668 | } else { |
| 8669 | if (!this->emitInitField(*FieldT, F.Offset, E)) |
| 8670 | return false; |
| 8671 | } |
| 8672 | } else { |
| 8673 | if (!this->emitGetPtrField(F.Offset, E)) |
| 8674 | return false; |
| 8675 | if (!emitHLSLConstructAggregate(FieldType, Elements, ElemIdx, E)) |
| 8676 | return false; |
| 8677 | if (!this->emitPopPtr(E)) |
| 8678 | return false; |
| 8679 | } |
| 8680 | } |
| 8681 | return true; |
| 8682 | } |
| 8683 | |
| 8684 | return false; |
| 8685 | } |
| 8686 | |
| 8687 | namespace clang { |
| 8688 | namespace interp { |
| 8689 | |
| 8690 | template class Compiler<ByteCodeEmitter>; |
| 8691 | template class Compiler<EvalEmitter>; |
| 8692 | |
| 8693 | } // namespace interp |
| 8694 | } // namespace clang |
| 8695 | |