| 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 "../ExprConstShared.h" |
| 11 | #include "ByteCodeEmitter.h" |
| 12 | #include "Context.h" |
| 13 | #include "FixedPoint.h" |
| 14 | #include "Floating.h" |
| 15 | #include "Function.h" |
| 16 | #include "InterpShared.h" |
| 17 | #include "PrimType.h" |
| 18 | #include "Program.h" |
| 19 | #include "clang/AST/Attr.h" |
| 20 | #include "clang/AST/DynamicRecursiveASTVisitor.h" |
| 21 | #include "llvm/Support/SaveAndRestore.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace clang::interp; |
| 25 | |
| 26 | using APSInt = llvm::APSInt; |
| 27 | |
| 28 | namespace clang { |
| 29 | namespace interp { |
| 30 | |
| 31 | static std::optional<bool> getBoolValue(const Expr *E) { |
| 32 | if (const auto *CE = dyn_cast_if_present<ConstantExpr>(Val: E); |
| 33 | CE && CE->hasAPValueResult() && |
| 34 | CE->getResultAPValueKind() == APValue::ValueKind::Int) { |
| 35 | return CE->getResultAsAPSInt().getBoolValue(); |
| 36 | } |
| 37 | |
| 38 | return std::nullopt; |
| 39 | } |
| 40 | |
| 41 | /// Check if \c E has side-effects. This is used to avoid some temporary |
| 42 | /// variables and is supposed to be a quick check, not exhaustive. That's why |
| 43 | /// we're not using Expr::HasSideEffects(). |
| 44 | static bool isSideEffectFree(const Expr *E) { |
| 45 | if (isa<IntegerLiteral, FloatingLiteral, CharacterLiteral, |
| 46 | CXXBoolLiteralExpr>(Val: E)) |
| 47 | return true; |
| 48 | if (isa<DeclRefExpr>(Val: E)) |
| 49 | return true; |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | /// Whether the CheckArraySize op rejects an array with \p NumElems elements. |
| 55 | static bool exceedsArraySizeLimit(const LangOptions &LangOpts, |
| 56 | uint64_t NumElems) { |
| 57 | if (NumElems > std::numeric_limits<unsigned>::max()) |
| 58 | return true; |
| 59 | uint64_t Limit = LangOpts.ConstexprStepLimit; |
| 60 | return Limit != 0 && NumElems > Limit; |
| 61 | } |
| 62 | |
| 63 | /// Scope chain managing the variable lifetimes. |
| 64 | template <class Emitter> class VariableScope { |
| 65 | public: |
| 66 | VariableScope(Compiler<Emitter> *Ctx, ScopeKind Kind = ScopeKind::Block) |
| 67 | : Ctx(Ctx), Parent(Ctx->VarScope), Kind(Kind) { |
| 68 | if (Parent) |
| 69 | this->LocalsAlwaysEnabled = Parent->LocalsAlwaysEnabled; |
| 70 | Ctx->VarScope = this; |
| 71 | } |
| 72 | |
| 73 | virtual ~VariableScope() { Ctx->VarScope = this->Parent; } |
| 74 | |
| 75 | virtual void addLocal(Scope::Local Local) { |
| 76 | llvm_unreachable("Shouldn't be called" ); |
| 77 | } |
| 78 | /// Like addExtended, but adds to the nearest scope of the given kind. |
| 79 | void addForScopeKind(const Scope::Local &Local, ScopeKind Kind) { |
| 80 | VariableScope *P = this; |
| 81 | while (P) { |
| 82 | // We found the right scope kind. |
| 83 | if (P->Kind == Kind) { |
| 84 | P->addLocal(Local); |
| 85 | return; |
| 86 | } |
| 87 | // If we reached the root scope and we're looking for a Block scope, |
| 88 | // attach it to the root instead of the current scope. |
| 89 | if (!P->Parent && Kind == ScopeKind::Block) { |
| 90 | P->addLocal(Local); |
| 91 | return; |
| 92 | } |
| 93 | P = P->Parent; |
| 94 | if (!P) |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | // Add to this scope. |
| 99 | this->addLocal(Local); |
| 100 | } |
| 101 | |
| 102 | virtual bool emitDestructors(const Expr *E = nullptr) { return true; } |
| 103 | virtual bool destroyLocals(const Expr *E = nullptr) { return true; } |
| 104 | virtual void forceInit() {} |
| 105 | VariableScope *getParent() const { return Parent; } |
| 106 | ScopeKind getKind() const { return Kind; } |
| 107 | |
| 108 | /// Whether locals added to this scope are enabled by default. |
| 109 | /// This is almost always true, except for the two branches |
| 110 | /// of a conditional operator. |
| 111 | bool LocalsAlwaysEnabled = true; |
| 112 | |
| 113 | protected: |
| 114 | /// Compiler instance. |
| 115 | Compiler<Emitter> *Ctx; |
| 116 | /// Link to the parent scope. |
| 117 | VariableScope *Parent; |
| 118 | ScopeKind Kind; |
| 119 | }; |
| 120 | |
| 121 | /// Generic scope for local variables. |
| 122 | template <class Emitter> class LocalScope : public VariableScope<Emitter> { |
| 123 | public: |
| 124 | LocalScope(Compiler<Emitter> *Ctx, ScopeKind Kind = ScopeKind::Block) |
| 125 | : VariableScope<Emitter>(Ctx, Kind) {} |
| 126 | |
| 127 | /// Emit a Destroy op for this scope. |
| 128 | ~LocalScope() override { |
| 129 | if (!Idx || ExplicitlyDestroyed) |
| 130 | return; |
| 131 | this->Ctx->emitDestroy(*Idx, SourceInfo{}); |
| 132 | removeStoredOpaqueValues(); |
| 133 | } |
| 134 | /// Explicit destruction of local variables. |
| 135 | bool destroyLocals(const Expr *E = nullptr) override { |
| 136 | if (!Idx) |
| 137 | return true; |
| 138 | |
| 139 | // NB: We are *not* resetting Idx here as to allow multiple |
| 140 | // calls to destroyLocals(). |
| 141 | bool Success = this->emitDestructors(E); |
| 142 | this->Ctx->emitDestroy(*Idx, E); |
| 143 | ExplicitlyDestroyed = true; |
| 144 | return Success; |
| 145 | } |
| 146 | |
| 147 | void addLocal(Scope::Local Local) override { |
| 148 | if (!Idx) { |
| 149 | Idx = static_cast<unsigned>(this->Ctx->Descriptors.size()); |
| 150 | this->Ctx->Descriptors.emplace_back(); |
| 151 | |
| 152 | if constexpr (!std::is_same_v<Emitter, EvalEmitter>) |
| 153 | this->Ctx->emitInitScope(*Idx, {}); |
| 154 | } |
| 155 | |
| 156 | Local.EnabledByDefault = this->LocalsAlwaysEnabled; |
| 157 | this->Ctx->Descriptors[*Idx].emplace_back(Local); |
| 158 | } |
| 159 | |
| 160 | /// Force-initialize this scope. Usually, scopes are lazily initialized when |
| 161 | /// the first local variable is created, but in scenarios with conditonal |
| 162 | /// operators, we need to ensure scope is initialized just in case one of the |
| 163 | /// arms will create a local and the other won't. In such a case, the |
| 164 | /// InitScope() op would be part of the arm that created the local. |
| 165 | void forceInit() override { |
| 166 | if (!Idx) { |
| 167 | Idx = static_cast<unsigned>(this->Ctx->Descriptors.size()); |
| 168 | this->Ctx->Descriptors.emplace_back(); |
| 169 | if constexpr (!std::is_same_v<Emitter, EvalEmitter>) |
| 170 | this->Ctx->emitInitScope(*Idx, {}); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool emitDestructors(const Expr *E = nullptr) override { |
| 175 | if (!Idx) |
| 176 | return true; |
| 177 | |
| 178 | // Emit destructor calls for local variables of record |
| 179 | // type with a destructor. |
| 180 | for (Scope::Local &Local : llvm::reverse(this->Ctx->Descriptors[*Idx])) { |
| 181 | if (Local.Desc->hasTrivialDtor()) |
| 182 | continue; |
| 183 | |
| 184 | if (!Local.EnabledByDefault) { |
| 185 | typename Emitter::LabelTy EndLabel = this->Ctx->getLabel(); |
| 186 | if (!this->Ctx->emitGetLocalEnabled(Local.Offset, E)) |
| 187 | return false; |
| 188 | if (!this->Ctx->jumpFalse(EndLabel, E)) |
| 189 | return false; |
| 190 | |
| 191 | if (!this->Ctx->emitGetPtrLocal(Local.Offset, E)) |
| 192 | return false; |
| 193 | |
| 194 | if (!this->Ctx->emitDestructionPop(Local.Desc, Local.Desc->getLoc())) |
| 195 | return false; |
| 196 | |
| 197 | this->Ctx->fallthrough(EndLabel); |
| 198 | this->Ctx->emitLabel(EndLabel); |
| 199 | } else { |
| 200 | if (!this->Ctx->emitGetPtrLocal(Local.Offset, E)) |
| 201 | return false; |
| 202 | if (!this->Ctx->emitDestructionPop(Local.Desc, Local.Desc->getLoc())) |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | removeIfStoredOpaqueValue(Local); |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | void removeStoredOpaqueValues() { |
| 212 | if (!Idx) |
| 213 | return; |
| 214 | |
| 215 | for (const Scope::Local &Local : this->Ctx->Descriptors[*Idx]) { |
| 216 | removeIfStoredOpaqueValue(Local); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void removeIfStoredOpaqueValue(const Scope::Local &Local) { |
| 221 | if (const auto *OVE = |
| 222 | llvm::dyn_cast_if_present<OpaqueValueExpr>(Val: Local.Desc->asExpr())) { |
| 223 | this->Ctx->OpaqueExprs.erase(OVE); |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | /// Index of the scope in the chain. |
| 228 | UnsignedOrNone Idx = std::nullopt; |
| 229 | bool ExplicitlyDestroyed = false; |
| 230 | }; |
| 231 | |
| 232 | template <class Emitter> class ArrayIndexScope final { |
| 233 | public: |
| 234 | ArrayIndexScope(Compiler<Emitter> *Ctx, uint64_t Index) : Ctx(Ctx) { |
| 235 | OldArrayIndex = Ctx->ArrayIndex; |
| 236 | Ctx->ArrayIndex = Index; |
| 237 | } |
| 238 | |
| 239 | ~ArrayIndexScope() { Ctx->ArrayIndex = OldArrayIndex; } |
| 240 | |
| 241 | private: |
| 242 | Compiler<Emitter> *Ctx; |
| 243 | std::optional<uint64_t> OldArrayIndex; |
| 244 | }; |
| 245 | |
| 246 | template <class Emitter> class SourceLocScope final { |
| 247 | public: |
| 248 | SourceLocScope(Compiler<Emitter> *Ctx, const Expr *DefaultExpr) : Ctx(Ctx) { |
| 249 | assert(DefaultExpr); |
| 250 | // We only switch if the current SourceLocDefaultExpr is null. |
| 251 | if (!Ctx->SourceLocDefaultExpr) { |
| 252 | Enabled = true; |
| 253 | Ctx->SourceLocDefaultExpr = DefaultExpr; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | ~SourceLocScope() { |
| 258 | if (Enabled) |
| 259 | Ctx->SourceLocDefaultExpr = nullptr; |
| 260 | } |
| 261 | |
| 262 | private: |
| 263 | Compiler<Emitter> *Ctx; |
| 264 | bool Enabled = false; |
| 265 | }; |
| 266 | |
| 267 | template <class Emitter> class InitLinkScope final { |
| 268 | public: |
| 269 | InitLinkScope(Compiler<Emitter> *Ctx, InitLink &&Link) : Ctx(Ctx) { |
| 270 | Ctx->InitStack.push_back(std::move(Link)); |
| 271 | } |
| 272 | |
| 273 | ~InitLinkScope() { this->Ctx->InitStack.pop_back(); } |
| 274 | |
| 275 | public: |
| 276 | Compiler<Emitter> *Ctx; |
| 277 | }; |
| 278 | |
| 279 | template <class Emitter> class InitStackScope final { |
| 280 | public: |
| 281 | InitStackScope(Compiler<Emitter> *Ctx, bool Active) |
| 282 | : Ctx(Ctx), OldValue(Ctx->InitStackActive), Active(Active) { |
| 283 | // An explicit initializer nested in a default member initializer still |
| 284 | // needs the surrounding default initializer's `this` reconstruction. |
| 285 | Ctx->InitStackActive = OldValue || Active; |
| 286 | if (Active) |
| 287 | Ctx->InitStack.push_back(InitLink::DIE()); |
| 288 | } |
| 289 | |
| 290 | ~InitStackScope() { |
| 291 | this->Ctx->InitStackActive = OldValue; |
| 292 | if (Active) |
| 293 | Ctx->InitStack.pop_back(); |
| 294 | } |
| 295 | |
| 296 | private: |
| 297 | Compiler<Emitter> *Ctx; |
| 298 | bool OldValue; |
| 299 | bool Active; |
| 300 | }; |
| 301 | |
| 302 | /// Scope used to handle temporaries in toplevel variable declarations. |
| 303 | template <class Emitter> class DeclScope final : public LocalScope<Emitter> { |
| 304 | public: |
| 305 | DeclScope(Compiler<Emitter> *Ctx, const VarDecl *VD) |
| 306 | : LocalScope<Emitter>(Ctx), Scope(Ctx->P), |
| 307 | OldInitializingDecl(Ctx->InitializingDecl) { |
| 308 | Ctx->InitializingDecl = VD; |
| 309 | Ctx->InitStack.push_back(InitLink::Decl(D: VD)); |
| 310 | } |
| 311 | |
| 312 | ~DeclScope() { |
| 313 | this->Ctx->InitializingDecl = OldInitializingDecl; |
| 314 | this->Ctx->InitStack.pop_back(); |
| 315 | } |
| 316 | |
| 317 | private: |
| 318 | Program::DeclScope Scope; |
| 319 | const VarDecl *OldInitializingDecl; |
| 320 | }; |
| 321 | |
| 322 | /// Scope used to handle initialization methods. |
| 323 | template <class Emitter> class OptionScope final { |
| 324 | public: |
| 325 | /// Root constructor, compiling or discarding primitives. |
| 326 | OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult, |
| 327 | bool NewInitializing, bool NewToLValue) |
| 328 | : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult), |
| 329 | OldInitializing(Ctx->Initializing), OldToLValue(Ctx->ToLValue) { |
| 330 | Ctx->DiscardResult = NewDiscardResult; |
| 331 | Ctx->Initializing = NewInitializing; |
| 332 | Ctx->ToLValue = NewToLValue; |
| 333 | } |
| 334 | |
| 335 | ~OptionScope() { |
| 336 | Ctx->DiscardResult = OldDiscardResult; |
| 337 | Ctx->Initializing = OldInitializing; |
| 338 | Ctx->ToLValue = OldToLValue; |
| 339 | } |
| 340 | |
| 341 | private: |
| 342 | /// Parent context. |
| 343 | Compiler<Emitter> *Ctx; |
| 344 | /// Old discard flag to restore. |
| 345 | bool OldDiscardResult; |
| 346 | bool OldInitializing; |
| 347 | bool OldToLValue; |
| 348 | }; |
| 349 | |
| 350 | template <class Emitter> |
| 351 | bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const { |
| 352 | switch (Kind) { |
| 353 | case K_This: |
| 354 | return Ctx->emitThis(E); |
| 355 | case K_Field: |
| 356 | // We're assuming there's a base pointer on the stack already. |
| 357 | return Ctx->emitGetPtrFieldPop(Offset, E); |
| 358 | case K_Base: |
| 359 | return Ctx->emitGetPtrBasePop(Offset, false, E); |
| 360 | case K_Temp: |
| 361 | return Ctx->emitGetPtrLocal(Offset, E); |
| 362 | case K_Decl: |
| 363 | return Ctx->visitDeclRef(D, E); |
| 364 | case K_Elem: |
| 365 | if (!Ctx->emitConstUint32(Offset, E)) |
| 366 | return false; |
| 367 | return Ctx->emitArrayElemPtrPopUint32(E); |
| 368 | case K_RVO: |
| 369 | return Ctx->emitRVOPtr(E); |
| 370 | case K_InitList: |
| 371 | return true; |
| 372 | default: |
| 373 | llvm_unreachable("Unhandled InitLink kind" ); |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | /// Sets the context for break/continue statements. |
| 379 | template <class Emitter> class LoopScope final { |
| 380 | public: |
| 381 | using LabelTy = typename Compiler<Emitter>::LabelTy; |
| 382 | using OptLabelTy = typename Compiler<Emitter>::OptLabelTy; |
| 383 | using LabelInfo = typename Compiler<Emitter>::LabelInfo; |
| 384 | |
| 385 | LoopScope(Compiler<Emitter> *Ctx, const Stmt *Name, LabelTy BreakLabel, |
| 386 | LabelTy ContinueLabel) |
| 387 | : Ctx(Ctx) { |
| 388 | #ifndef NDEBUG |
| 389 | for (const LabelInfo &LI : Ctx->LabelInfoStack) |
| 390 | assert(LI.Name != Name); |
| 391 | #endif |
| 392 | |
| 393 | this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel, ContinueLabel, |
| 394 | /*DefaultLabel=*/std::nullopt, |
| 395 | Ctx->VarScope); |
| 396 | } |
| 397 | |
| 398 | ~LoopScope() { this->Ctx->LabelInfoStack.pop_back(); } |
| 399 | |
| 400 | private: |
| 401 | Compiler<Emitter> *Ctx; |
| 402 | }; |
| 403 | |
| 404 | // Sets the context for a switch scope, mapping labels. |
| 405 | template <class Emitter> class SwitchScope final { |
| 406 | public: |
| 407 | using LabelTy = typename Compiler<Emitter>::LabelTy; |
| 408 | using OptLabelTy = typename Compiler<Emitter>::OptLabelTy; |
| 409 | using CaseMap = typename Compiler<Emitter>::CaseMap; |
| 410 | using LabelInfo = typename Compiler<Emitter>::LabelInfo; |
| 411 | |
| 412 | SwitchScope(Compiler<Emitter> *Ctx, const Stmt *Name, CaseMap &&CaseLabels, |
| 413 | LabelTy BreakLabel, OptLabelTy DefaultLabel) |
| 414 | : Ctx(Ctx), OldCaseLabels(std::move(this->Ctx->CaseLabels)) { |
| 415 | #ifndef NDEBUG |
| 416 | for (const LabelInfo &LI : Ctx->LabelInfoStack) |
| 417 | assert(LI.Name != Name); |
| 418 | #endif |
| 419 | |
| 420 | this->Ctx->CaseLabels = std::move(CaseLabels); |
| 421 | this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel, |
| 422 | /*ContinueLabel=*/std::nullopt, |
| 423 | DefaultLabel, Ctx->VarScope); |
| 424 | } |
| 425 | |
| 426 | ~SwitchScope() { |
| 427 | this->Ctx->CaseLabels = std::move(OldCaseLabels); |
| 428 | this->Ctx->LabelInfoStack.pop_back(); |
| 429 | } |
| 430 | |
| 431 | private: |
| 432 | Compiler<Emitter> *Ctx; |
| 433 | CaseMap OldCaseLabels; |
| 434 | }; |
| 435 | |
| 436 | /// When generating code for e.g. implicit field initializers in constructors, |
| 437 | /// we don't have anything to point to in case the initializer causes an error. |
| 438 | /// In that case, we need to disable location tracking for the initializer so |
| 439 | /// we later point to the call range instead. |
| 440 | template <class Emitter> class LocOverrideScope final { |
| 441 | public: |
| 442 | LocOverrideScope(Compiler<Emitter> *Ctx, SourceInfo NewValue, |
| 443 | bool Enabled = true) |
| 444 | : Ctx(Ctx), OldFlag(Ctx->LocOverride), Enabled(Enabled) { |
| 445 | |
| 446 | if (Enabled) |
| 447 | Ctx->LocOverride = NewValue; |
| 448 | } |
| 449 | |
| 450 | ~LocOverrideScope() { |
| 451 | if (Enabled) |
| 452 | Ctx->LocOverride = OldFlag; |
| 453 | } |
| 454 | |
| 455 | private: |
| 456 | Compiler<Emitter> *Ctx; |
| 457 | std::optional<SourceInfo> OldFlag; |
| 458 | bool Enabled; |
| 459 | }; |
| 460 | |
| 461 | } // namespace interp |
| 462 | } // namespace clang |
| 463 | |
| 464 | template <class Emitter> |
| 465 | bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) { |
| 466 | const Expr *SubExpr = E->getSubExpr(); |
| 467 | |
| 468 | if (DiscardResult) |
| 469 | return this->delegate(E: SubExpr); |
| 470 | |
| 471 | switch (E->getCastKind()) { |
| 472 | case CK_LValueToRValue: { |
| 473 | // This *could* work I guess, but the current interpreter rejects (via |
| 474 | // checkLiteralType). |
| 475 | if (!Ctx.getLangOpts().HLSL && E->getType()->isConstantMatrixType()) |
| 476 | return false; |
| 477 | |
| 478 | if (ToLValue && E->getType()->isPointerType()) { |
| 479 | assert(!DiscardResult); |
| 480 | if (!this->visit(E: SubExpr)) |
| 481 | return false; |
| 482 | return this->emitLoadPopL(E); |
| 483 | } |
| 484 | |
| 485 | if (SubExpr->getType().isVolatileQualified()) |
| 486 | return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, E); |
| 487 | |
| 488 | OptPrimType SubExprT = classify(SubExpr->getType()); |
| 489 | // Try to load the value directly. This is purely a performance |
| 490 | // optimization. |
| 491 | if (SubExprT) { |
| 492 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: SubExpr)) { |
| 493 | const ValueDecl *D = DRE->getDecl(); |
| 494 | bool IsReference = D->getType()->isReferenceType(); |
| 495 | |
| 496 | if (!IsReference) { |
| 497 | if (Context::shouldBeGloballyIndexed(VD: D)) { |
| 498 | if (auto GlobalIndex = P.getGlobal(VD: D)) |
| 499 | return this->emitGetGlobal(*SubExprT, *GlobalIndex, E); |
| 500 | } else if (auto It = Locals.find(Val: D); It != Locals.end()) { |
| 501 | return this->emitGetLocal(*SubExprT, It->second.Offset, E); |
| 502 | } else if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: D)) { |
| 503 | if (auto It = this->Params.find(PVD); It != this->Params.end()) { |
| 504 | return this->emitGetParam(*SubExprT, It->second.Index, E); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Prepare storage for the result. |
| 512 | if (!Initializing && !SubExprT) { |
| 513 | UnsignedOrNone LocalIndex = allocateLocal(Decl: SubExpr); |
| 514 | if (!LocalIndex) |
| 515 | return false; |
| 516 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | if (!this->visit(E: SubExpr)) |
| 521 | return false; |
| 522 | |
| 523 | if (SubExprT) |
| 524 | return this->emitLoadPop(*SubExprT, E); |
| 525 | |
| 526 | // If the subexpr type is not primitive, we need to perform a copy here. |
| 527 | // This happens for example in C when dereferencing a pointer of struct |
| 528 | // type. |
| 529 | return this->emitMemcpy(E); |
| 530 | } |
| 531 | |
| 532 | case CK_DerivedToBaseMemberPointer: { |
| 533 | if (E->containsErrors()) |
| 534 | return false; |
| 535 | assert(classifyPrim(E) == PT_MemberPtr); |
| 536 | assert(classifyPrim(SubExpr) == PT_MemberPtr); |
| 537 | |
| 538 | if (!this->delegate(E: SubExpr)) |
| 539 | return false; |
| 540 | |
| 541 | const CXXRecordDecl *CurDecl = SubExpr->getType() |
| 542 | ->castAs<MemberPointerType>() |
| 543 | ->getMostRecentCXXRecordDecl(); |
| 544 | for (const CXXBaseSpecifier *B : E->path()) { |
| 545 | const CXXRecordDecl *ToDecl = B->getType()->getAsCXXRecordDecl(); |
| 546 | unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: ToDecl, DerivedDecl: CurDecl); |
| 547 | |
| 548 | if (!this->emitCastMemberPtrBasePop(DerivedOffset, ToDecl, E)) |
| 549 | return false; |
| 550 | CurDecl = ToDecl; |
| 551 | } |
| 552 | |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | case CK_BaseToDerivedMemberPointer: { |
| 557 | if (E->containsErrors()) |
| 558 | return false; |
| 559 | assert(classifyPrim(E) == PT_MemberPtr); |
| 560 | assert(classifyPrim(SubExpr) == PT_MemberPtr); |
| 561 | |
| 562 | if (!this->delegate(E: SubExpr)) |
| 563 | return false; |
| 564 | |
| 565 | const CXXRecordDecl *CurDecl = SubExpr->getType() |
| 566 | ->castAs<MemberPointerType>() |
| 567 | ->getMostRecentCXXRecordDecl(); |
| 568 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 569 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 570 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 571 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 572 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 573 | PathI != PathE; ++PathI) { |
| 574 | const CXXRecordDecl *ToDecl = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 575 | unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: CurDecl, DerivedDecl: ToDecl); |
| 576 | |
| 577 | if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E)) |
| 578 | return false; |
| 579 | CurDecl = ToDecl; |
| 580 | } |
| 581 | |
| 582 | const CXXRecordDecl *ToDecl = |
| 583 | E->getType()->castAs<MemberPointerType>()->getMostRecentCXXRecordDecl(); |
| 584 | assert(ToDecl != CurDecl); |
| 585 | unsigned DerivedOffset = Ctx.collectBaseOffset(BaseDecl: CurDecl, DerivedDecl: ToDecl); |
| 586 | |
| 587 | if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E)) |
| 588 | return false; |
| 589 | |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | case CK_UncheckedDerivedToBase: |
| 594 | case CK_DerivedToBase: { |
| 595 | if (!this->delegate(E: SubExpr)) |
| 596 | return false; |
| 597 | |
| 598 | const auto = [](QualType Ty) -> const CXXRecordDecl * { |
| 599 | if (const auto *PT = dyn_cast<PointerType>(Val&: Ty)) |
| 600 | return PT->getPointeeType()->getAsCXXRecordDecl(); |
| 601 | return Ty->getAsCXXRecordDecl(); |
| 602 | }; |
| 603 | |
| 604 | // FIXME: We can express a series of non-virtual casts as a single |
| 605 | // GetPtrBasePop op. |
| 606 | QualType CurType = SubExpr->getType(); |
| 607 | for (const CXXBaseSpecifier *B : E->path()) { |
| 608 | if (B->isVirtual()) { |
| 609 | if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), E)) |
| 610 | return false; |
| 611 | CurType = B->getType(); |
| 612 | } else { |
| 613 | unsigned DerivedOffset = collectBaseOffset(BaseType: B->getType(), DerivedType: CurType); |
| 614 | if (!this->emitGetPtrBasePop( |
| 615 | DerivedOffset, /*NullOK=*/E->getType()->isPointerType(), E)) |
| 616 | return false; |
| 617 | CurType = B->getType(); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | case CK_BaseToDerived: { |
| 625 | if (!this->delegate(E: SubExpr)) |
| 626 | return false; |
| 627 | unsigned DerivedOffset = |
| 628 | collectBaseOffset(BaseType: SubExpr->getType(), DerivedType: E->getType()); |
| 629 | |
| 630 | const Type *TargetType = E->getType().getTypePtr(); |
| 631 | if (TargetType->isPointerOrReferenceType()) |
| 632 | TargetType = TargetType->getPointeeType().getTypePtr(); |
| 633 | return this->emitGetPtrDerivedPop(DerivedOffset, |
| 634 | /*NullOK=*/E->getType()->isPointerType(), |
| 635 | TargetType, E); |
| 636 | } |
| 637 | |
| 638 | case CK_FloatingCast: { |
| 639 | // HLSL uses CK_FloatingCast to cast between vectors. |
| 640 | if (E->getType()->isVectorType()) |
| 641 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 642 | if (!SubExpr->getType()->isFloatingType() || |
| 643 | !E->getType()->isFloatingType()) |
| 644 | return false; |
| 645 | if (!this->visit(E: SubExpr)) |
| 646 | return false; |
| 647 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType()); |
| 648 | return this->emitCastFP(TargetSemantics, getRoundingMode(E), E); |
| 649 | } |
| 650 | |
| 651 | case CK_IntegralToFloating: { |
| 652 | if (E->getType()->isVectorType()) |
| 653 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 654 | if (!E->getType()->isRealFloatingType()) |
| 655 | return false; |
| 656 | if (!this->visit(E: SubExpr)) |
| 657 | return false; |
| 658 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType()); |
| 659 | return this->emitCastIntegralFloating(classifyPrim(SubExpr), |
| 660 | TargetSemantics, getFPOptions(E), E); |
| 661 | } |
| 662 | |
| 663 | case CK_FloatingToBoolean: { |
| 664 | if (E->getType()->isVectorType()) |
| 665 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 666 | if (!SubExpr->getType()->isRealFloatingType() || |
| 667 | !E->getType()->hasBooleanRepresentation()) |
| 668 | return false; |
| 669 | if (const auto *FL = dyn_cast<FloatingLiteral>(Val: SubExpr)) |
| 670 | return this->emitConstBool(FL->getValue().isNonZero(), E); |
| 671 | if (!this->visit(E: SubExpr)) |
| 672 | return false; |
| 673 | return this->emitCastFloatingIntegralBool(getFPOptions(E), E); |
| 674 | } |
| 675 | |
| 676 | case CK_FloatingToIntegral: { |
| 677 | if (E->getType()->isVectorType()) |
| 678 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 679 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 680 | return false; |
| 681 | if (!this->visit(E: SubExpr)) |
| 682 | return false; |
| 683 | PrimType ToT = classifyPrim(E); |
| 684 | if (ToT == PT_IntAP) |
| 685 | return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(T: E->getType()), |
| 686 | getFPOptions(E), E); |
| 687 | if (ToT == PT_IntAPS) |
| 688 | return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(T: E->getType()), |
| 689 | getFPOptions(E), E); |
| 690 | |
| 691 | return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E); |
| 692 | } |
| 693 | |
| 694 | case CK_NullToPointer: |
| 695 | case CK_NullToMemberPointer: { |
| 696 | if (!this->discard(E: SubExpr)) |
| 697 | return false; |
| 698 | uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType()); |
| 699 | return this->emitNull(classifyPrim(E->getType()), Val, |
| 700 | E->getType().getTypePtr(), E); |
| 701 | } |
| 702 | |
| 703 | case CK_PointerToIntegral: { |
| 704 | if (!this->visit(E: SubExpr)) |
| 705 | return false; |
| 706 | |
| 707 | // If SubExpr doesn't result in a pointer, make it one. |
| 708 | if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) { |
| 709 | assert(isPtrType(FromT)); |
| 710 | if (!this->emitDecayPtr(FromT, PT_Ptr, E)) |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | PrimType T = classifyPrim(E->getType()); |
| 715 | if (T == PT_IntAP) |
| 716 | return this->emitCastPointerIntegralAP(Ctx.getBitWidth(T: E->getType()), E); |
| 717 | if (T == PT_IntAPS) |
| 718 | return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(T: E->getType()), E); |
| 719 | return this->emitCastPointerIntegral(T, E); |
| 720 | } |
| 721 | |
| 722 | case CK_ArrayToPointerDecay: { |
| 723 | if (!this->visit(E: SubExpr)) |
| 724 | return false; |
| 725 | return this->emitArrayDecay(E); |
| 726 | } |
| 727 | |
| 728 | case CK_IntegralToPointer: { |
| 729 | QualType IntType = SubExpr->getType(); |
| 730 | assert(IntType->isIntegralOrEnumerationType()); |
| 731 | if (!this->visit(E: SubExpr)) |
| 732 | return false; |
| 733 | // FIXME: I think the discard is wrong since the int->ptr cast might cause a |
| 734 | // diagnostic. |
| 735 | PrimType T = classifyPrim(IntType); |
| 736 | if (!this->emitGetIntPtr(T, E->getType().getTypePtr(), E)) |
| 737 | return false; |
| 738 | |
| 739 | QualType PtrType = E->getType(); |
| 740 | PrimType DestPtrT = classifyPrim(PtrType); |
| 741 | if (DestPtrT == PT_Ptr) |
| 742 | return true; |
| 743 | |
| 744 | // In case we're converting the integer to a non-Pointer. |
| 745 | return this->emitDecayPtr(PT_Ptr, DestPtrT, E); |
| 746 | } |
| 747 | |
| 748 | case CK_AtomicToNonAtomic: |
| 749 | case CK_ConstructorConversion: |
| 750 | case CK_FunctionToPointerDecay: |
| 751 | case CK_NonAtomicToAtomic: |
| 752 | case CK_NoOp: |
| 753 | case CK_UserDefinedConversion: |
| 754 | case CK_CPointerToObjCPointerCast: |
| 755 | return this->delegate(E: SubExpr); |
| 756 | |
| 757 | case CK_AddressSpaceConversion: { |
| 758 | if (E->containsErrors()) |
| 759 | return false; |
| 760 | |
| 761 | if (!this->visit(E: SubExpr)) |
| 762 | return false; |
| 763 | |
| 764 | uint64_t Val; |
| 765 | if (E->getType()->isPointerType()) |
| 766 | Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType()); |
| 767 | else |
| 768 | Val = 0; |
| 769 | |
| 770 | if (!this->emitCastAddressSpace(Val, E->getType().getTypePtr(), E)) |
| 771 | return false; |
| 772 | if (DiscardResult) |
| 773 | return this->emitPopPtr(E); |
| 774 | return true; |
| 775 | } |
| 776 | |
| 777 | case CK_BitCast: { |
| 778 | if (E->containsErrors()) |
| 779 | return false; |
| 780 | QualType ETy = E->getType(); |
| 781 | // Reject bitcasts to atomic types. |
| 782 | if (ETy->isAtomicType()) { |
| 783 | if (!this->discard(E: SubExpr)) |
| 784 | return false; |
| 785 | return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E); |
| 786 | } |
| 787 | QualType SubExprTy = SubExpr->getType(); |
| 788 | OptPrimType FromT = classify(SubExprTy); |
| 789 | // Casts from integer/vector to vector. |
| 790 | if (E->getType()->isVectorType()) |
| 791 | return this->emitBuiltinBitCast(E); |
| 792 | |
| 793 | OptPrimType ToT = classify(E->getType()); |
| 794 | if (!FromT || !ToT) |
| 795 | return false; |
| 796 | |
| 797 | assert(isPtrType(*FromT)); |
| 798 | assert(isPtrType(*ToT)); |
| 799 | bool SrcIsVoidPtr = SubExprTy->isVoidPointerType(); |
| 800 | if (FromT == ToT) { |
| 801 | if (E->getType()->isVoidPointerType() && |
| 802 | !SubExprTy->isFunctionPointerType()) { |
| 803 | return this->delegate(E: SubExpr); |
| 804 | } |
| 805 | |
| 806 | if (!this->visit(E: SubExpr)) |
| 807 | return false; |
| 808 | if (!this->emitCheckBitCast(ETy->getPointeeType().getTypePtr(), |
| 809 | SrcIsVoidPtr, E)) |
| 810 | return false; |
| 811 | |
| 812 | if (E->getType()->isFunctionPointerType() || |
| 813 | SubExprTy->isFunctionPointerType()) { |
| 814 | return this->emitFnPtrCast(E); |
| 815 | } |
| 816 | if (FromT == PT_Ptr) |
| 817 | return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), |
| 818 | E->getType().getTypePtr(), E); |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | if (!this->visit(E: SubExpr)) |
| 823 | return false; |
| 824 | return this->emitDecayPtr(*FromT, *ToT, E); |
| 825 | } |
| 826 | case CK_IntegralToBoolean: |
| 827 | case CK_FixedPointToBoolean: { |
| 828 | if (E->getType()->isVectorType()) |
| 829 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 830 | // HLSL uses this to cast to one-element vectors. |
| 831 | OptPrimType FromT = classify(SubExpr->getType()); |
| 832 | if (!FromT) |
| 833 | return false; |
| 834 | |
| 835 | if (const auto *IL = dyn_cast<IntegerLiteral>(Val: SubExpr)) |
| 836 | return this->emitConst(IL->getValue(), E); |
| 837 | if (!this->visit(E: SubExpr)) |
| 838 | return false; |
| 839 | return this->emitCast(*FromT, classifyPrim(E), E); |
| 840 | } |
| 841 | |
| 842 | case CK_IntegralCast: |
| 843 | if (E->getType()->isVectorType()) |
| 844 | return this->emitVectorConversion(Src: E->getSubExpr(), E); |
| 845 | [[fallthrough]]; |
| 846 | case CK_BooleanToSignedIntegral: { |
| 847 | OptPrimType FromT = classify(SubExpr->getType()); |
| 848 | OptPrimType ToT = classify(E->getType()); |
| 849 | if (!FromT || !ToT) |
| 850 | return false; |
| 851 | |
| 852 | // Try to emit a casted known constant value directly. |
| 853 | if (const auto *IL = dyn_cast<IntegerLiteral>(Val: SubExpr)) { |
| 854 | if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP && |
| 855 | FromT != PT_IntAPS && !E->getType()->isEnumeralType()) |
| 856 | return this->emitConst(APSInt(IL->getValue(), !isSignedType(T: *FromT)), |
| 857 | E); |
| 858 | if (!this->emitConst(IL->getValue(), SubExpr)) |
| 859 | return false; |
| 860 | } else { |
| 861 | if (!this->visit(E: SubExpr)) |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | // Possibly diagnose casts to enum types if the target type does not |
| 866 | // have a fixed size. |
| 867 | if (Ctx.getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) { |
| 868 | const auto *ED = E->getType()->castAsEnumDecl(); |
| 869 | if (!ED->isFixed()) { |
| 870 | if (!this->emitCheckEnumValue(*FromT, ED, E)) |
| 871 | return false; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | if (ToT == PT_IntAP) { |
| 876 | if (!this->emitCastAP(*FromT, Ctx.getBitWidth(T: E->getType()), E)) |
| 877 | return false; |
| 878 | } else if (ToT == PT_IntAPS) { |
| 879 | if (!this->emitCastAPS(*FromT, Ctx.getBitWidth(T: E->getType()), E)) |
| 880 | return false; |
| 881 | } else { |
| 882 | if (FromT == ToT) |
| 883 | return true; |
| 884 | if (!this->emitCast(*FromT, *ToT, E)) |
| 885 | return false; |
| 886 | } |
| 887 | if (E->getCastKind() == CK_BooleanToSignedIntegral) |
| 888 | return this->emitNeg(*ToT, E); |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | case CK_PointerToBoolean: |
| 893 | if (!this->visit(E: SubExpr)) |
| 894 | return false; |
| 895 | return this->emitIsNonNullPtr(E); |
| 896 | |
| 897 | case CK_MemberPointerToBoolean: |
| 898 | if (!this->visit(E: SubExpr)) |
| 899 | return false; |
| 900 | return this->emitIsNonNullMemberPtr(E); |
| 901 | |
| 902 | case CK_IntegralComplexToBoolean: |
| 903 | case CK_FloatingComplexToBoolean: { |
| 904 | if (!this->visit(E: SubExpr)) |
| 905 | return false; |
| 906 | return this->emitComplexBoolCast(E: SubExpr); |
| 907 | } |
| 908 | |
| 909 | case CK_IntegralComplexToReal: |
| 910 | case CK_FloatingComplexToReal: |
| 911 | return this->emitComplexReal(SubExpr); |
| 912 | |
| 913 | case CK_IntegralRealToComplex: |
| 914 | case CK_FloatingRealToComplex: { |
| 915 | // We're creating a complex value here, so we need to |
| 916 | // allocate storage for it. |
| 917 | if (!Initializing) { |
| 918 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 919 | if (!LocalIndex) |
| 920 | return false; |
| 921 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | PrimType T = classifyPrim(SubExpr->getType()); |
| 926 | // Init the complex value to {SubExpr, 0}. |
| 927 | if (!this->visitArrayElemInit(ElemIndex: 0, Init: SubExpr, InitT: T)) |
| 928 | return false; |
| 929 | // Zero-init the second element. |
| 930 | if (!this->visitZeroInitializer(T, QT: SubExpr->getType(), E: SubExpr)) |
| 931 | return false; |
| 932 | return this->emitInitElem(T, 1, SubExpr); |
| 933 | } |
| 934 | |
| 935 | case CK_IntegralComplexCast: |
| 936 | case CK_FloatingComplexCast: |
| 937 | case CK_IntegralComplexToFloatingComplex: |
| 938 | case CK_FloatingComplexToIntegralComplex: { |
| 939 | assert(E->getType()->isAnyComplexType()); |
| 940 | assert(SubExpr->getType()->isAnyComplexType()); |
| 941 | if (!Initializing) { |
| 942 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 943 | if (!LocalIndex) |
| 944 | return false; |
| 945 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | // Location for the SubExpr. |
| 950 | // Since SubExpr is of complex type, visiting it results in a pointer |
| 951 | // anyway, so we just create a temporary pointer variable. |
| 952 | unsigned SubExprOffset = |
| 953 | allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 954 | if (!this->visit(E: SubExpr)) |
| 955 | return false; |
| 956 | if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E)) |
| 957 | return false; |
| 958 | |
| 959 | PrimType SourceElemT = classifyComplexElementType(T: SubExpr->getType()); |
| 960 | QualType DestElemType = |
| 961 | E->getType()->getAs<ComplexType>()->getElementType(); |
| 962 | PrimType DestElemT = classifyPrim(DestElemType); |
| 963 | // Cast both elements individually. |
| 964 | for (unsigned I = 0; I != 2; ++I) { |
| 965 | if (!this->emitGetLocal(PT_Ptr, SubExprOffset, E)) |
| 966 | return false; |
| 967 | if (!this->emitArrayElemPop(SourceElemT, I, E)) |
| 968 | return false; |
| 969 | |
| 970 | // Do the cast. |
| 971 | if (!this->emitPrimCast(FromT: SourceElemT, ToT: DestElemT, ToQT: DestElemType, E)) |
| 972 | return false; |
| 973 | |
| 974 | // Save the value. |
| 975 | if (!this->emitInitElem(DestElemT, I, E)) |
| 976 | return false; |
| 977 | } |
| 978 | return true; |
| 979 | } |
| 980 | |
| 981 | case CK_VectorSplat: { |
| 982 | assert(!canClassify(E->getType())); |
| 983 | assert(E->getType()->isVectorType()); |
| 984 | |
| 985 | if (!canClassify(SubExpr->getType())) |
| 986 | return false; |
| 987 | |
| 988 | if (!Initializing) { |
| 989 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 990 | if (!LocalIndex) |
| 991 | return false; |
| 992 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 993 | return false; |
| 994 | } |
| 995 | |
| 996 | const auto *VT = E->getType()->getAs<VectorType>(); |
| 997 | PrimType ElemT = classifyPrim(SubExpr->getType()); |
| 998 | unsigned ElemOffset = |
| 999 | allocateLocalPrimitive(Decl: SubExpr, Ty: ElemT, /*IsConst=*/true); |
| 1000 | |
| 1001 | // Prepare a local variable for the scalar value. |
| 1002 | if (!this->visit(E: SubExpr)) |
| 1003 | return false; |
| 1004 | if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, E)) |
| 1005 | return false; |
| 1006 | |
| 1007 | if (!this->emitSetLocal(ElemT, ElemOffset, E)) |
| 1008 | return false; |
| 1009 | |
| 1010 | for (unsigned I = 0; I != VT->getNumElements(); ++I) { |
| 1011 | if (!this->emitGetLocal(ElemT, ElemOffset, E)) |
| 1012 | return false; |
| 1013 | if (!this->emitInitElem(ElemT, I, E)) |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | case CK_HLSLVectorTruncation: { |
| 1021 | assert(SubExpr->getType()->isVectorType()); |
| 1022 | if (OptPrimType ResultT = classify(E)) { |
| 1023 | assert(!DiscardResult); |
| 1024 | // Result must be either a float or integer. Take the first element. |
| 1025 | if (!this->visit(E: SubExpr)) |
| 1026 | return false; |
| 1027 | return this->emitArrayElemPop(*ResultT, 0, E); |
| 1028 | } |
| 1029 | // Otherwise, this truncates from one vector type to another. |
| 1030 | assert(E->getType()->isVectorType()); |
| 1031 | |
| 1032 | if (!Initializing) { |
| 1033 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1034 | if (!LocalIndex) |
| 1035 | return false; |
| 1036 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1037 | return false; |
| 1038 | } |
| 1039 | unsigned ToSize = E->getType()->getAs<VectorType>()->getNumElements(); |
| 1040 | assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize); |
| 1041 | if (!this->visit(E: SubExpr)) |
| 1042 | return false; |
| 1043 | return this->emitCopyArray(classifyVectorElementType(T: E->getType()), 0, 0, |
| 1044 | ToSize, E); |
| 1045 | }; |
| 1046 | |
| 1047 | case CK_IntegralToFixedPoint: { |
| 1048 | if (!this->visit(E: SubExpr)) |
| 1049 | return false; |
| 1050 | |
| 1051 | auto Sem = |
| 1052 | Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 1053 | if (!this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), Sem, |
| 1054 | E)) |
| 1055 | return false; |
| 1056 | if (DiscardResult) |
| 1057 | return this->emitPopFixedPoint(E); |
| 1058 | return true; |
| 1059 | } |
| 1060 | case CK_FloatingToFixedPoint: { |
| 1061 | if (!this->visit(E: SubExpr)) |
| 1062 | return false; |
| 1063 | |
| 1064 | auto Sem = |
| 1065 | Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 1066 | if (!this->emitCastFloatingFixedPoint(Sem, E)) |
| 1067 | return false; |
| 1068 | if (DiscardResult) |
| 1069 | return this->emitPopFixedPoint(E); |
| 1070 | return true; |
| 1071 | } |
| 1072 | case CK_FixedPointToFloating: { |
| 1073 | if (!this->visit(E: SubExpr)) |
| 1074 | return false; |
| 1075 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: E->getType()); |
| 1076 | if (!this->emitCastFixedPointFloating(TargetSemantics, E)) |
| 1077 | return false; |
| 1078 | if (DiscardResult) |
| 1079 | return this->emitPopFloat(E); |
| 1080 | return true; |
| 1081 | } |
| 1082 | case CK_FixedPointToIntegral: { |
| 1083 | if (!this->visit(E: SubExpr)) |
| 1084 | return false; |
| 1085 | PrimType IntegralT = classifyPrim(E->getType()); |
| 1086 | if (!this->emitCastFixedPointIntegral(IntegralT, E)) |
| 1087 | return false; |
| 1088 | if (DiscardResult) |
| 1089 | return this->emitPop(IntegralT, E); |
| 1090 | return true; |
| 1091 | } |
| 1092 | case CK_FixedPointCast: { |
| 1093 | if (!this->visit(E: SubExpr)) |
| 1094 | return false; |
| 1095 | auto Sem = |
| 1096 | Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 1097 | if (!this->emitCastFixedPoint(Sem, E)) |
| 1098 | return false; |
| 1099 | if (DiscardResult) |
| 1100 | return this->emitPopFixedPoint(E); |
| 1101 | return true; |
| 1102 | } |
| 1103 | |
| 1104 | case CK_ToVoid: |
| 1105 | return discard(E: SubExpr); |
| 1106 | |
| 1107 | case CK_Dynamic: |
| 1108 | llvm_unreachable("CXXDynamicCastExpr has its own function" ); |
| 1109 | |
| 1110 | case CK_LValueBitCast: |
| 1111 | if (!this->emitInvalidCast(CastKind::ReinterpretLike, /*Fatal=*/false, E)) |
| 1112 | return false; |
| 1113 | return this->delegate(E: SubExpr); |
| 1114 | |
| 1115 | case CK_HLSLArrayRValue: { |
| 1116 | // Non-decaying array rvalue cast - creates an rvalue copy of an lvalue |
| 1117 | // array, similar to LValueToRValue for composite types. |
| 1118 | if (!Initializing) { |
| 1119 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 1120 | if (!LocalIndex) |
| 1121 | return false; |
| 1122 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1123 | return false; |
| 1124 | } |
| 1125 | if (!this->visit(E: SubExpr)) |
| 1126 | return false; |
| 1127 | return this->emitMemcpy(E); |
| 1128 | } |
| 1129 | |
| 1130 | case CK_HLSLMatrixTruncation: { |
| 1131 | assert(SubExpr->getType()->isConstantMatrixType()); |
| 1132 | if (OptPrimType ResultT = classify(E)) { |
| 1133 | assert(!DiscardResult); |
| 1134 | // Result must be either a float or integer. Take the first element. |
| 1135 | if (!this->visit(E: SubExpr)) |
| 1136 | return false; |
| 1137 | return this->emitArrayElemPop(*ResultT, 0, E); |
| 1138 | } |
| 1139 | // Otherwise, this truncates to a a constant matrix type. |
| 1140 | assert(E->getType()->isConstantMatrixType()); |
| 1141 | |
| 1142 | if (!Initializing) { |
| 1143 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1144 | if (!LocalIndex) |
| 1145 | return false; |
| 1146 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1147 | return false; |
| 1148 | } |
| 1149 | unsigned ToSize = |
| 1150 | E->getType()->getAs<ConstantMatrixType>()->getNumElementsFlattened(); |
| 1151 | if (!this->visit(E: SubExpr)) |
| 1152 | return false; |
| 1153 | return this->emitCopyArray(classifyMatrixElementType(T: SubExpr->getType()), 0, |
| 1154 | 0, ToSize, E); |
| 1155 | } |
| 1156 | |
| 1157 | case CK_HLSLAggregateSplatCast: { |
| 1158 | // Aggregate splat cast: convert a scalar value to one of an aggregate type |
| 1159 | // by replicating and casting the scalar to every element of the destination |
| 1160 | // aggregate (vector, matrix, array, or struct). |
| 1161 | assert(canClassify(SubExpr->getType())); |
| 1162 | |
| 1163 | if (!Initializing) { |
| 1164 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 1165 | if (!LocalIndex) |
| 1166 | return false; |
| 1167 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | // The scalar to be splatted is stored in a local to be repeatedly loaded |
| 1172 | // once for every scalar element of the destination. |
| 1173 | PrimType SrcElemT = classifyPrim(SubExpr->getType()); |
| 1174 | unsigned SrcOffset = |
| 1175 | allocateLocalPrimitive(Decl: SubExpr, Ty: SrcElemT, /*IsConst=*/true); |
| 1176 | |
| 1177 | if (!this->visit(E: SubExpr)) |
| 1178 | return false; |
| 1179 | if (!this->emitSetLocal(SrcElemT, SrcOffset, E)) |
| 1180 | return false; |
| 1181 | |
| 1182 | // Recursively splat the scalar into every element of the destination. |
| 1183 | return emitHLSLAggregateSplat(SrcT: SrcElemT, SrcOffset, DestType: E->getType(), E); |
| 1184 | } |
| 1185 | |
| 1186 | case CK_HLSLElementwiseCast: { |
| 1187 | // Elementwise cast: flatten the elements of one aggregate source type and |
| 1188 | // store to a destination scalar or aggregate type of the same or fewer |
| 1189 | // number of elements. Casts are inserted element-wise to convert each |
| 1190 | // source scalar element to its corresponding destination scalar element. |
| 1191 | QualType SrcType = SubExpr->getType(); |
| 1192 | QualType DestType = E->getType(); |
| 1193 | |
| 1194 | if (OptPrimType DestT = classify(DestType)) { |
| 1195 | // When the destination is a scalar, we only need the first scalar |
| 1196 | // element of the source. |
| 1197 | unsigned SrcPtrOffset = |
| 1198 | allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 1199 | if (!this->visit(E: SubExpr)) |
| 1200 | return false; |
| 1201 | if (!this->emitSetLocal(PT_Ptr, SrcPtrOffset, E)) |
| 1202 | return false; |
| 1203 | |
| 1204 | SmallVector<HLSLFlatElement, 1> Elements; |
| 1205 | if (!emitHLSLFlattenAggregate(SrcType, SrcPtrOffset, Elements, MaxElements: 1, E)) |
| 1206 | return false; |
| 1207 | if (Elements.empty()) |
| 1208 | return false; |
| 1209 | |
| 1210 | const HLSLFlatElement &Src = Elements[0]; |
| 1211 | if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E)) |
| 1212 | return false; |
| 1213 | return this->emitPrimCast(FromT: Src.Type, ToT: *DestT, ToQT: DestType, E); |
| 1214 | } |
| 1215 | |
| 1216 | if (!Initializing) { |
| 1217 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 1218 | if (!LocalIndex) |
| 1219 | return false; |
| 1220 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | unsigned SrcOffset = |
| 1225 | allocateLocalPrimitive(Decl: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 1226 | if (!this->visit(E: SubExpr)) |
| 1227 | return false; |
| 1228 | if (!this->emitSetLocal(PT_Ptr, SrcOffset, E)) |
| 1229 | return false; |
| 1230 | |
| 1231 | // Only flatten as many source elements as the destination requires. |
| 1232 | unsigned ElemCount = countHLSLFlatElements(Ty: DestType); |
| 1233 | |
| 1234 | SmallVector<HLSLFlatElement, 16> Elements; |
| 1235 | Elements.reserve(ElemCount); |
| 1236 | if (!emitHLSLFlattenAggregate(SrcType, SrcPtrOffset: SrcOffset, Elements, MaxElements: ElemCount, E)) |
| 1237 | return false; |
| 1238 | |
| 1239 | // Sema is expected to reject an elementwise cast whose source has fewer |
| 1240 | // scalar elements than the destination. |
| 1241 | assert(Elements.size() == ElemCount && |
| 1242 | "Source type has fewer scalar elements than the destination type" ); |
| 1243 | |
| 1244 | return emitHLSLConstructAggregate(DestType, Elements, E); |
| 1245 | } |
| 1246 | |
| 1247 | case CK_ToUnion: { |
| 1248 | const FieldDecl *UnionField = E->getTargetUnionField(); |
| 1249 | const Record *R = this->getRecord(E->getType()); |
| 1250 | assert(R); |
| 1251 | const Record::Field *RF = R->getField(FD: UnionField); |
| 1252 | |
| 1253 | if (OptPrimType PT = RF->T) { |
| 1254 | if (!this->visit(E: SubExpr)) |
| 1255 | return false; |
| 1256 | if (RF->isBitField()) |
| 1257 | return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(), |
| 1258 | E); |
| 1259 | return this->emitInitFieldActivate(*PT, RF->Offset, E); |
| 1260 | } |
| 1261 | |
| 1262 | if (!this->emitGetPtrField(RF->Offset, E)) |
| 1263 | return false; |
| 1264 | if (!this->emitActivate(E)) |
| 1265 | return false; |
| 1266 | return this->visitInitializerPop(E: SubExpr); |
| 1267 | } |
| 1268 | |
| 1269 | default: |
| 1270 | return this->emitInvalid(E); |
| 1271 | } |
| 1272 | llvm_unreachable("Unhandled clang::CastKind enum" ); |
| 1273 | } |
| 1274 | |
| 1275 | template <class Emitter> |
| 1276 | bool Compiler<Emitter>::VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
| 1277 | return this->emitBuiltinBitCast(E); |
| 1278 | } |
| 1279 | |
| 1280 | template <class Emitter> |
| 1281 | bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) { |
| 1282 | if (DiscardResult) |
| 1283 | return true; |
| 1284 | |
| 1285 | return this->emitConst(LE->getValue(), LE); |
| 1286 | } |
| 1287 | |
| 1288 | template <class Emitter> |
| 1289 | bool Compiler<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1290 | if (DiscardResult) |
| 1291 | return true; |
| 1292 | |
| 1293 | APFloat F = E->getValue(); |
| 1294 | return this->emitFloat(F, Info: E); |
| 1295 | } |
| 1296 | |
| 1297 | template <class Emitter> |
| 1298 | bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 1299 | assert(E->getType()->isAnyComplexType()); |
| 1300 | if (DiscardResult) |
| 1301 | return true; |
| 1302 | |
| 1303 | if (!Initializing) { |
| 1304 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1305 | if (!LocalIndex) |
| 1306 | return false; |
| 1307 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1308 | return false; |
| 1309 | } |
| 1310 | |
| 1311 | const Expr *SubExpr = E->getSubExpr(); |
| 1312 | PrimType SubExprT = classifyPrim(SubExpr->getType()); |
| 1313 | |
| 1314 | if (!this->visitZeroInitializer(T: SubExprT, QT: SubExpr->getType(), E: SubExpr)) |
| 1315 | return false; |
| 1316 | if (!this->emitInitElem(SubExprT, 0, SubExpr)) |
| 1317 | return false; |
| 1318 | return this->visitArrayElemInit(ElemIndex: 1, Init: SubExpr, InitT: SubExprT); |
| 1319 | } |
| 1320 | |
| 1321 | template <class Emitter> |
| 1322 | bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) { |
| 1323 | assert(E->getType()->isFixedPointType()); |
| 1324 | assert(classifyPrim(E) == PT_FixedPoint); |
| 1325 | |
| 1326 | if (DiscardResult) |
| 1327 | return true; |
| 1328 | |
| 1329 | auto Sem = Ctx.getASTContext().getFixedPointSemantics(Ty: E->getType()); |
| 1330 | APInt Value = E->getValue(); |
| 1331 | return this->emitConstFixedPoint(FixedPoint(Value, Sem), E); |
| 1332 | } |
| 1333 | |
| 1334 | template <class Emitter> |
| 1335 | bool Compiler<Emitter>::VisitParenExpr(const ParenExpr *E) { |
| 1336 | return this->delegate(E: E->getSubExpr()); |
| 1337 | } |
| 1338 | |
| 1339 | template <class Emitter> |
| 1340 | bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *E) { |
| 1341 | // Need short-circuiting for these. |
| 1342 | if (E->isLogicalOp() && !E->getType()->isVectorType()) |
| 1343 | return this->VisitLogicalBinOp(E); |
| 1344 | |
| 1345 | const Expr *LHS = E->getLHS(); |
| 1346 | const Expr *RHS = E->getRHS(); |
| 1347 | |
| 1348 | // Handle comma operators. Just discard the LHS |
| 1349 | // and delegate to RHS. |
| 1350 | if (E->isCommaOp()) { |
| 1351 | if (!this->discard(E: LHS)) |
| 1352 | return false; |
| 1353 | if (RHS->getType()->isVoidType()) |
| 1354 | return this->discard(E: RHS); |
| 1355 | |
| 1356 | return this->delegate(E: RHS); |
| 1357 | } |
| 1358 | |
| 1359 | if (E->getType()->isAnyComplexType()) |
| 1360 | return this->VisitComplexBinOp(E); |
| 1361 | if (E->getType()->isVectorType()) |
| 1362 | return this->VisitVectorBinOp(E); |
| 1363 | if ((LHS->getType()->isAnyComplexType() || |
| 1364 | RHS->getType()->isAnyComplexType()) && |
| 1365 | E->isComparisonOp()) |
| 1366 | return this->emitComplexComparison(LHS, RHS, E); |
| 1367 | if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType()) |
| 1368 | return this->VisitFixedPointBinOp(E); |
| 1369 | |
| 1370 | if (E->isPtrMemOp()) { |
| 1371 | if (E->containsErrors()) |
| 1372 | return false; |
| 1373 | |
| 1374 | if (!this->visit(E: LHS)) |
| 1375 | return false; |
| 1376 | |
| 1377 | if (!this->visit(E: RHS)) |
| 1378 | return false; |
| 1379 | |
| 1380 | if (!this->emitToMemberPtr(E)) |
| 1381 | return false; |
| 1382 | |
| 1383 | if (classifyPrim(E) == PT_MemberPtr) |
| 1384 | return true; |
| 1385 | |
| 1386 | if (!this->emitCastMemberPtrPtr(E)) |
| 1387 | return false; |
| 1388 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 1389 | } |
| 1390 | |
| 1391 | // Typecheck the args. |
| 1392 | OptPrimType LT = classify(LHS); |
| 1393 | OptPrimType RT = classify(RHS); |
| 1394 | OptPrimType T = classify(E->getType()); |
| 1395 | |
| 1396 | // Special case for C++'s three-way/spaceship operator <=>, which |
| 1397 | // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't |
| 1398 | // have a PrimType). |
| 1399 | if (!T && E->getOpcode() == BO_Cmp) { |
| 1400 | if (DiscardResult) |
| 1401 | return true; |
| 1402 | const ComparisonCategoryInfo *CmpInfo = |
| 1403 | Ctx.getASTContext().CompCategories.lookupInfoForType(Ty: E->getType()); |
| 1404 | assert(CmpInfo); |
| 1405 | |
| 1406 | // We need a temporary variable holding our return value. |
| 1407 | if (!Initializing) { |
| 1408 | UnsignedOrNone ResultIndex = this->allocateLocal(Decl: E); |
| 1409 | if (!this->emitGetPtrLocal(*ResultIndex, E)) |
| 1410 | return false; |
| 1411 | } |
| 1412 | |
| 1413 | if (!visit(E: LHS) || !visit(E: RHS)) |
| 1414 | return false; |
| 1415 | |
| 1416 | return this->emitCMP3(*LT, CmpInfo, E); |
| 1417 | } |
| 1418 | |
| 1419 | if (!LT || !RT || !T) |
| 1420 | return false; |
| 1421 | |
| 1422 | // Pointer arithmetic special case. |
| 1423 | if (E->getOpcode() == BO_Add || E->getOpcode() == BO_Sub) { |
| 1424 | if (isPtrType(T: *T) || (isPtrType(T: *LT) && isPtrType(T: *RT))) |
| 1425 | return this->VisitPointerArithBinOp(E); |
| 1426 | } |
| 1427 | |
| 1428 | if (E->getOpcode() == BO_Assign) |
| 1429 | return this->visitAssignment(LHS, RHS, E); |
| 1430 | |
| 1431 | if (!visit(E: LHS) || !visit(E: RHS)) |
| 1432 | return false; |
| 1433 | |
| 1434 | // For languages such as C, cast the result of one |
| 1435 | // of our comparision opcodes to T (which is usually int). |
| 1436 | auto MaybeCastToBool = [this, T, E](bool Result) { |
| 1437 | if (!Result) |
| 1438 | return false; |
| 1439 | if (DiscardResult) |
| 1440 | return this->emitPopBool(E); |
| 1441 | if (T != PT_Bool) |
| 1442 | return this->emitCast(PT_Bool, *T, E); |
| 1443 | return true; |
| 1444 | }; |
| 1445 | |
| 1446 | auto Discard = [this, T, E](bool Result) { |
| 1447 | if (!Result) |
| 1448 | return false; |
| 1449 | return DiscardResult ? this->emitPop(*T, E) : true; |
| 1450 | }; |
| 1451 | |
| 1452 | switch (E->getOpcode()) { |
| 1453 | case BO_EQ: |
| 1454 | return MaybeCastToBool(this->emitEQ(*LT, E)); |
| 1455 | case BO_NE: |
| 1456 | return MaybeCastToBool(this->emitNE(*LT, E)); |
| 1457 | case BO_LT: |
| 1458 | return MaybeCastToBool(this->emitLT(*LT, E)); |
| 1459 | case BO_LE: |
| 1460 | return MaybeCastToBool(this->emitLE(*LT, E)); |
| 1461 | case BO_GT: |
| 1462 | return MaybeCastToBool(this->emitGT(*LT, E)); |
| 1463 | case BO_GE: |
| 1464 | return MaybeCastToBool(this->emitGE(*LT, E)); |
| 1465 | case BO_Sub: |
| 1466 | if (E->getType()->isFloatingType()) |
| 1467 | return Discard(this->emitSubf(getFPOptions(E), E)); |
| 1468 | return Discard(this->emitSub(*T, E)); |
| 1469 | case BO_Add: |
| 1470 | if (E->getType()->isFloatingType()) |
| 1471 | return Discard(this->emitAddf(getFPOptions(E), E)); |
| 1472 | return Discard(this->emitAdd(*T, E)); |
| 1473 | case BO_Mul: |
| 1474 | if (E->getType()->isFloatingType()) |
| 1475 | return Discard(this->emitMulf(getFPOptions(E), E)); |
| 1476 | return Discard(this->emitMul(*T, E)); |
| 1477 | case BO_Rem: |
| 1478 | return Discard(this->emitRem(*T, E)); |
| 1479 | case BO_Div: |
| 1480 | if (E->getType()->isFloatingType()) |
| 1481 | return Discard(this->emitDivf(getFPOptions(E), E)); |
| 1482 | return Discard(this->emitDiv(*T, E)); |
| 1483 | case BO_And: |
| 1484 | return Discard(this->emitBitAnd(*T, E)); |
| 1485 | case BO_Or: |
| 1486 | return Discard(this->emitBitOr(*T, E)); |
| 1487 | case BO_Shl: |
| 1488 | return Discard(this->emitShl(*LT, *RT, E)); |
| 1489 | case BO_Shr: |
| 1490 | return Discard(this->emitShr(*LT, *RT, E)); |
| 1491 | case BO_Xor: |
| 1492 | return Discard(this->emitBitXor(*T, E)); |
| 1493 | case BO_LOr: |
| 1494 | case BO_LAnd: |
| 1495 | llvm_unreachable("Already handled earlier" ); |
| 1496 | default: |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | llvm_unreachable("Unhandled binary op" ); |
| 1501 | } |
| 1502 | |
| 1503 | /// Perform addition/subtraction of a pointer and an integer or |
| 1504 | /// subtraction of two pointers. |
| 1505 | template <class Emitter> |
| 1506 | bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) { |
| 1507 | BinaryOperatorKind Op = E->getOpcode(); |
| 1508 | const Expr *LHS = E->getLHS(); |
| 1509 | const Expr *RHS = E->getRHS(); |
| 1510 | |
| 1511 | if ((Op != BO_Add && Op != BO_Sub) || |
| 1512 | (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType())) |
| 1513 | return false; |
| 1514 | |
| 1515 | OptPrimType LT = classify(LHS); |
| 1516 | OptPrimType RT = classify(RHS); |
| 1517 | |
| 1518 | if (!LT || !RT) |
| 1519 | return false; |
| 1520 | |
| 1521 | // Visit the given pointer expression and optionally convert to a PT_Ptr. |
| 1522 | auto visitAsPointer = [&](const Expr *E, PrimType T) -> bool { |
| 1523 | if (!this->visit(E)) |
| 1524 | return false; |
| 1525 | if (T != PT_Ptr) |
| 1526 | return this->emitDecayPtr(T, PT_Ptr, E); |
| 1527 | return true; |
| 1528 | }; |
| 1529 | |
| 1530 | if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) { |
| 1531 | if (Op != BO_Sub) |
| 1532 | return false; |
| 1533 | |
| 1534 | assert(E->getType()->isIntegerType()); |
| 1535 | if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT)) |
| 1536 | return false; |
| 1537 | |
| 1538 | QualType ElemType = LHS->getType()->getPointeeType(); |
| 1539 | CharUnits ElemTypeSize; |
| 1540 | if (ElemType->isVoidType() || ElemType->isFunctionType()) |
| 1541 | ElemTypeSize = CharUnits::One(); |
| 1542 | else |
| 1543 | ElemTypeSize = Ctx.getASTContext().getTypeSizeInChars(T: ElemType); |
| 1544 | |
| 1545 | PrimType IntT = classifyPrim(E->getType()); |
| 1546 | if (!this->emitSubPtr(IntT, ElemTypeSize.getQuantity(), E)) |
| 1547 | return false; |
| 1548 | return DiscardResult ? this->emitPop(IntT, E) : true; |
| 1549 | } |
| 1550 | |
| 1551 | PrimType OffsetType; |
| 1552 | if (LHS->getType()->isIntegerType()) { |
| 1553 | if (!visitAsPointer(RHS, *RT)) |
| 1554 | return false; |
| 1555 | if (!this->visit(E: LHS)) |
| 1556 | return false; |
| 1557 | OffsetType = *LT; |
| 1558 | } else if (RHS->getType()->isIntegerType()) { |
| 1559 | if (!visitAsPointer(LHS, *LT)) |
| 1560 | return false; |
| 1561 | if (!this->visit(E: RHS)) |
| 1562 | return false; |
| 1563 | OffsetType = *RT; |
| 1564 | } else { |
| 1565 | return false; |
| 1566 | } |
| 1567 | |
| 1568 | // Do the operation and optionally transform to |
| 1569 | // result pointer type. |
| 1570 | switch (Op) { |
| 1571 | case BO_Add: |
| 1572 | if (!this->emitAddOffset(OffsetType, E)) |
| 1573 | return false; |
| 1574 | break; |
| 1575 | case BO_Sub: |
| 1576 | if (!this->emitSubOffset(OffsetType, E)) |
| 1577 | return false; |
| 1578 | break; |
| 1579 | default: |
| 1580 | return false; |
| 1581 | } |
| 1582 | |
| 1583 | PrimType ExprT = classifyPrim(E); |
| 1584 | if (ExprT != PT_Ptr) { |
| 1585 | if (!this->emitDecayPtr(PT_Ptr, ExprT, E)) |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
| 1589 | if (DiscardResult) |
| 1590 | return this->emitPop(ExprT, E); |
| 1591 | return true; |
| 1592 | } |
| 1593 | |
| 1594 | template <class Emitter> |
| 1595 | bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) { |
| 1596 | assert(E->isLogicalOp()); |
| 1597 | BinaryOperatorKind Op = E->getOpcode(); |
| 1598 | const Expr *LHS = E->getLHS(); |
| 1599 | const Expr *RHS = E->getRHS(); |
| 1600 | |
| 1601 | if (Op == BO_LOr) { |
| 1602 | // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE. |
| 1603 | LabelTy LabelTrue = this->getLabel(); |
| 1604 | LabelTy LabelEnd = this->getLabel(); |
| 1605 | |
| 1606 | if (!this->visitBool(E: LHS)) |
| 1607 | return false; |
| 1608 | if (!this->jumpTrue(LabelTrue, E)) |
| 1609 | return false; |
| 1610 | |
| 1611 | if (!this->visitBool(E: RHS)) |
| 1612 | return false; |
| 1613 | if (!this->jump(LabelEnd, E)) |
| 1614 | return false; |
| 1615 | |
| 1616 | this->emitLabel(LabelTrue); |
| 1617 | this->emitConstBool(true, E); |
| 1618 | this->fallthrough(LabelEnd); |
| 1619 | this->emitLabel(LabelEnd); |
| 1620 | |
| 1621 | } else { |
| 1622 | assert(Op == BO_LAnd); |
| 1623 | // Logical AND. |
| 1624 | // Visit LHS. Only visit RHS if LHS was TRUE. |
| 1625 | LabelTy LabelFalse = this->getLabel(); |
| 1626 | LabelTy LabelEnd = this->getLabel(); |
| 1627 | |
| 1628 | if (!this->visitBool(E: LHS)) |
| 1629 | return false; |
| 1630 | if (!this->jumpFalse(LabelFalse, E)) |
| 1631 | return false; |
| 1632 | |
| 1633 | if (!this->visitBool(E: RHS)) |
| 1634 | return false; |
| 1635 | if (!this->jump(LabelEnd, E)) |
| 1636 | return false; |
| 1637 | |
| 1638 | this->emitLabel(LabelFalse); |
| 1639 | this->emitConstBool(false, E); |
| 1640 | this->fallthrough(LabelEnd); |
| 1641 | this->emitLabel(LabelEnd); |
| 1642 | } |
| 1643 | |
| 1644 | if (DiscardResult) |
| 1645 | return this->emitPopBool(E); |
| 1646 | |
| 1647 | // For C, cast back to integer type. |
| 1648 | if (!E->getType()->isBooleanType()) { |
| 1649 | PrimType T = classifyPrim(E->getType()); |
| 1650 | return this->emitCast(PT_Bool, T, E); |
| 1651 | } |
| 1652 | return true; |
| 1653 | } |
| 1654 | |
| 1655 | template <class Emitter> |
| 1656 | bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) { |
| 1657 | // Prepare storage for result. |
| 1658 | if (!Initializing) { |
| 1659 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1660 | if (!LocalIndex) |
| 1661 | return false; |
| 1662 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1663 | return false; |
| 1664 | } |
| 1665 | |
| 1666 | // Both LHS and RHS might _not_ be of complex type, but one of them |
| 1667 | // needs to be. |
| 1668 | const Expr *LHS = E->getLHS(); |
| 1669 | const Expr *RHS = E->getRHS(); |
| 1670 | |
| 1671 | PrimType ResultElemT = this->classifyComplexElementType(T: E->getType()); |
| 1672 | unsigned ResultOffset = ~0u; |
| 1673 | if (!DiscardResult) |
| 1674 | ResultOffset = this->allocateLocalPrimitive(Decl: E, Ty: PT_Ptr, /*IsConst=*/true); |
| 1675 | |
| 1676 | // Save result pointer in ResultOffset |
| 1677 | if (!this->DiscardResult) { |
| 1678 | if (!this->emitDupPtr(E)) |
| 1679 | return false; |
| 1680 | if (!this->emitSetLocal(PT_Ptr, ResultOffset, E)) |
| 1681 | return false; |
| 1682 | } |
| 1683 | QualType LHSType = LHS->getType(); |
| 1684 | if (const auto *AT = LHSType->getAs<AtomicType>()) |
| 1685 | LHSType = AT->getValueType(); |
| 1686 | QualType RHSType = RHS->getType(); |
| 1687 | if (const auto *AT = RHSType->getAs<AtomicType>()) |
| 1688 | RHSType = AT->getValueType(); |
| 1689 | |
| 1690 | bool LHSIsComplex = LHSType->isAnyComplexType(); |
| 1691 | unsigned LHSOffset; |
| 1692 | bool RHSIsComplex = RHSType->isAnyComplexType(); |
| 1693 | |
| 1694 | // For ComplexComplex Mul, we have special ops to make their implementation |
| 1695 | // easier. |
| 1696 | BinaryOperatorKind Op = E->getOpcode(); |
| 1697 | if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) { |
| 1698 | assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) == |
| 1699 | classifyPrim(RHSType->getAs<ComplexType>()->getElementType())); |
| 1700 | PrimType ElemT = |
| 1701 | classifyPrim(LHSType->getAs<ComplexType>()->getElementType()); |
| 1702 | if (!this->visit(E: LHS)) |
| 1703 | return false; |
| 1704 | if (!this->visit(E: RHS)) |
| 1705 | return false; |
| 1706 | if (!this->emitMulc(ElemT, E)) |
| 1707 | return false; |
| 1708 | if (DiscardResult) |
| 1709 | return this->emitPopPtr(E); |
| 1710 | return true; |
| 1711 | } |
| 1712 | |
| 1713 | if (Op == BO_Div && RHSIsComplex) { |
| 1714 | QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType(); |
| 1715 | PrimType ElemT = classifyPrim(ElemQT); |
| 1716 | // If the LHS is not complex, we still need to do the full complex |
| 1717 | // division, so just stub create a complex value and stub it out with |
| 1718 | // the LHS and a zero. |
| 1719 | |
| 1720 | if (!LHSIsComplex) { |
| 1721 | // This is using the RHS type for the fake-complex LHS. |
| 1722 | UnsignedOrNone LocalIndex = allocateTemporary(E: RHS); |
| 1723 | if (!LocalIndex) |
| 1724 | return false; |
| 1725 | LHSOffset = *LocalIndex; |
| 1726 | |
| 1727 | if (!this->emitGetPtrLocal(LHSOffset, E)) |
| 1728 | return false; |
| 1729 | |
| 1730 | if (!this->visit(E: LHS)) |
| 1731 | return false; |
| 1732 | // real is LHS |
| 1733 | if (!this->emitInitElem(ElemT, 0, E)) |
| 1734 | return false; |
| 1735 | // imag is zero |
| 1736 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 1737 | return false; |
| 1738 | if (!this->emitInitElem(ElemT, 1, E)) |
| 1739 | return false; |
| 1740 | } else { |
| 1741 | if (!this->visit(E: LHS)) |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
| 1745 | if (!this->visit(E: RHS)) |
| 1746 | return false; |
| 1747 | if (!this->emitDivc(ElemT, E)) |
| 1748 | return false; |
| 1749 | if (DiscardResult) |
| 1750 | return this->emitPopPtr(E); |
| 1751 | return true; |
| 1752 | } |
| 1753 | |
| 1754 | // Evaluate LHS and save value to LHSOffset. |
| 1755 | if (LHSType->isAnyComplexType()) { |
| 1756 | LHSOffset = this->allocateLocalPrimitive(Decl: LHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1757 | if (!this->visit(E: LHS)) |
| 1758 | return false; |
| 1759 | if (!this->emitSetLocal(PT_Ptr, LHSOffset, E)) |
| 1760 | return false; |
| 1761 | } else { |
| 1762 | PrimType LHST = classifyPrim(LHSType); |
| 1763 | LHSOffset = this->allocateLocalPrimitive(Decl: LHS, Ty: LHST, /*IsConst=*/true); |
| 1764 | if (!this->visit(E: LHS)) |
| 1765 | return false; |
| 1766 | if (!this->emitSetLocal(LHST, LHSOffset, E)) |
| 1767 | return false; |
| 1768 | } |
| 1769 | |
| 1770 | // Same with RHS. |
| 1771 | unsigned RHSOffset; |
| 1772 | if (RHSType->isAnyComplexType()) { |
| 1773 | RHSOffset = this->allocateLocalPrimitive(Decl: RHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1774 | if (!this->visit(E: RHS)) |
| 1775 | return false; |
| 1776 | if (!this->emitSetLocal(PT_Ptr, RHSOffset, E)) |
| 1777 | return false; |
| 1778 | } else { |
| 1779 | PrimType RHST = classifyPrim(RHSType); |
| 1780 | RHSOffset = this->allocateLocalPrimitive(Decl: RHS, Ty: RHST, /*IsConst=*/true); |
| 1781 | if (!this->visit(E: RHS)) |
| 1782 | return false; |
| 1783 | if (!this->emitSetLocal(RHST, RHSOffset, E)) |
| 1784 | return false; |
| 1785 | } |
| 1786 | |
| 1787 | // For both LHS and RHS, either load the value from the complex pointer, or |
| 1788 | // directly from the local variable. For index 1 (i.e. the imaginary part), |
| 1789 | // just load 0 and do the operation anyway. |
| 1790 | auto loadComplexValue = [this](bool IsComplex, bool LoadZero, |
| 1791 | unsigned ElemIndex, unsigned Offset, |
| 1792 | const Expr *E) -> bool { |
| 1793 | if (IsComplex) { |
| 1794 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 1795 | return false; |
| 1796 | return this->emitArrayElemPop(classifyComplexElementType(T: E->getType()), |
| 1797 | ElemIndex, E); |
| 1798 | } |
| 1799 | if (ElemIndex == 0 || !LoadZero) |
| 1800 | return this->emitGetLocal(classifyPrim(E->getType()), Offset, E); |
| 1801 | return this->visitZeroInitializer(T: classifyPrim(E->getType()), QT: E->getType(), |
| 1802 | E); |
| 1803 | }; |
| 1804 | |
| 1805 | // Now we can get pointers to the LHS and RHS from the offsets above. |
| 1806 | for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) { |
| 1807 | // Result pointer for the store later. |
| 1808 | if (!this->DiscardResult) { |
| 1809 | if (!this->emitGetLocal(PT_Ptr, ResultOffset, E)) |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
| 1813 | // The actual operation. |
| 1814 | switch (Op) { |
| 1815 | case BO_Add: |
| 1816 | if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS)) |
| 1817 | return false; |
| 1818 | |
| 1819 | if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS)) |
| 1820 | return false; |
| 1821 | if (ResultElemT == PT_Float) { |
| 1822 | if (!this->emitAddf(getFPOptions(E), E)) |
| 1823 | return false; |
| 1824 | } else { |
| 1825 | if (!this->emitAdd(ResultElemT, E)) |
| 1826 | return false; |
| 1827 | } |
| 1828 | break; |
| 1829 | case BO_Sub: |
| 1830 | if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS)) |
| 1831 | return false; |
| 1832 | |
| 1833 | if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS)) |
| 1834 | return false; |
| 1835 | if (ResultElemT == PT_Float) { |
| 1836 | if (!this->emitSubf(getFPOptions(E), E)) |
| 1837 | return false; |
| 1838 | } else { |
| 1839 | if (!this->emitSub(ResultElemT, E)) |
| 1840 | return false; |
| 1841 | } |
| 1842 | break; |
| 1843 | case BO_Mul: |
| 1844 | if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS)) |
| 1845 | return false; |
| 1846 | |
| 1847 | if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS)) |
| 1848 | return false; |
| 1849 | |
| 1850 | if (ResultElemT == PT_Float) { |
| 1851 | if (!this->emitMulf(getFPOptions(E), E)) |
| 1852 | return false; |
| 1853 | } else { |
| 1854 | if (!this->emitMul(ResultElemT, E)) |
| 1855 | return false; |
| 1856 | } |
| 1857 | break; |
| 1858 | case BO_Div: |
| 1859 | assert(!RHSIsComplex); |
| 1860 | if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS)) |
| 1861 | return false; |
| 1862 | |
| 1863 | if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS)) |
| 1864 | return false; |
| 1865 | |
| 1866 | if (ResultElemT == PT_Float) { |
| 1867 | if (!this->emitDivf(getFPOptions(E), E)) |
| 1868 | return false; |
| 1869 | } else { |
| 1870 | if (!this->emitDiv(ResultElemT, E)) |
| 1871 | return false; |
| 1872 | } |
| 1873 | break; |
| 1874 | |
| 1875 | default: |
| 1876 | return false; |
| 1877 | } |
| 1878 | |
| 1879 | if (!this->DiscardResult) { |
| 1880 | // Initialize array element with the value we just computed. |
| 1881 | if (!this->emitInitElemPop(ResultElemT, ElemIndex, E)) |
| 1882 | return false; |
| 1883 | } else { |
| 1884 | if (!this->emitPop(ResultElemT, E)) |
| 1885 | return false; |
| 1886 | // Remove the Complex temporary pointer we created ourselves at the |
| 1887 | // beginning of this function. |
| 1888 | if (!Initializing) |
| 1889 | return this->emitPopPtr(E); |
| 1890 | } |
| 1891 | } |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| 1895 | template <class Emitter> |
| 1896 | bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) { |
| 1897 | const Expr *LHS = E->getLHS(); |
| 1898 | const Expr *RHS = E->getRHS(); |
| 1899 | assert(!E->isCommaOp() && |
| 1900 | "Comma op should be handled in VisitBinaryOperator" ); |
| 1901 | |
| 1902 | QualType LHSType = LHS->getType(); |
| 1903 | if (const auto *AT = LHSType->getAs<AtomicType>()) |
| 1904 | LHSType = AT->getValueType(); |
| 1905 | QualType RHSType = RHS->getType(); |
| 1906 | if (const auto *AT = RHSType->getAs<AtomicType>()) |
| 1907 | RHSType = AT->getValueType(); |
| 1908 | assert(E->getType()->isVectorType()); |
| 1909 | assert(LHSType->isVectorType()); |
| 1910 | assert(RHSType->isVectorType()); |
| 1911 | |
| 1912 | // We can only handle vectors with primitive element types. |
| 1913 | if (!canClassify(LHSType->castAs<VectorType>()->getElementType())) |
| 1914 | return false; |
| 1915 | |
| 1916 | // Prepare storage for result. |
| 1917 | if (!Initializing && !E->isCompoundAssignmentOp() && !E->isAssignmentOp()) { |
| 1918 | UnsignedOrNone LocalIndex = allocateTemporary(E); |
| 1919 | if (!LocalIndex) |
| 1920 | return false; |
| 1921 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
| 1925 | const auto *VecTy = E->getType()->getAs<VectorType>(); |
| 1926 | auto Op = E->isCompoundAssignmentOp() |
| 1927 | ? BinaryOperator::getOpForCompoundAssignment(Opc: E->getOpcode()) |
| 1928 | : E->getOpcode(); |
| 1929 | |
| 1930 | PrimType ElemT = this->classifyVectorElementType(T: LHSType); |
| 1931 | PrimType RHSElemT = this->classifyVectorElementType(T: RHSType); |
| 1932 | PrimType ResultElemT = this->classifyVectorElementType(T: E->getType()); |
| 1933 | |
| 1934 | if (E->getOpcode() == BO_Assign) { |
| 1935 | assert(Ctx.getASTContext().hasSameUnqualifiedType( |
| 1936 | LHSType->castAs<VectorType>()->getElementType(), |
| 1937 | RHSType->castAs<VectorType>()->getElementType())); |
| 1938 | if (!this->visit(E: LHS)) |
| 1939 | return false; |
| 1940 | if (!this->visit(E: RHS)) |
| 1941 | return false; |
| 1942 | if (!this->emitCopyArray(ElemT, 0, 0, VecTy->getNumElements(), E)) |
| 1943 | return false; |
| 1944 | if (DiscardResult) |
| 1945 | return this->emitPopPtr(E); |
| 1946 | return true; |
| 1947 | } |
| 1948 | |
| 1949 | // Evaluate LHS and save value to LHSOffset. |
| 1950 | unsigned LHSOffset = |
| 1951 | this->allocateLocalPrimitive(Decl: LHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1952 | if (!this->visit(E: LHS)) |
| 1953 | return false; |
| 1954 | if (!this->emitSetLocal(PT_Ptr, LHSOffset, E)) |
| 1955 | return false; |
| 1956 | |
| 1957 | // Evaluate RHS and save value to RHSOffset. |
| 1958 | unsigned RHSOffset = |
| 1959 | this->allocateLocalPrimitive(Decl: RHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 1960 | if (!this->visit(E: RHS)) |
| 1961 | return false; |
| 1962 | if (!this->emitSetLocal(PT_Ptr, RHSOffset, E)) |
| 1963 | return false; |
| 1964 | |
| 1965 | if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E)) |
| 1966 | return false; |
| 1967 | |
| 1968 | // BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the |
| 1969 | // integer promotion. |
| 1970 | bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp()); |
| 1971 | QualType PromotTy; |
| 1972 | PrimType PromotT = PT_Bool; |
| 1973 | PrimType OpT = ElemT; |
| 1974 | if (NeedIntPromot) { |
| 1975 | PromotTy = |
| 1976 | Ctx.getASTContext().getPromotedIntegerType(PromotableType: Ctx.getASTContext().BoolTy); |
| 1977 | PromotT = classifyPrim(PromotTy); |
| 1978 | OpT = PromotT; |
| 1979 | } |
| 1980 | |
| 1981 | auto getElem = [=](unsigned Offset, PrimType ElemT, unsigned Index) { |
| 1982 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 1983 | return false; |
| 1984 | if (!this->emitArrayElemPop(ElemT, Index, E)) |
| 1985 | return false; |
| 1986 | if (E->isLogicalOp()) { |
| 1987 | if (!this->emitPrimCast(FromT: ElemT, ToT: PT_Bool, ToQT: Ctx.getASTContext().BoolTy, E)) |
| 1988 | return false; |
| 1989 | if (!this->emitPrimCast(FromT: PT_Bool, ToT: ResultElemT, ToQT: VecTy->getElementType(), E)) |
| 1990 | return false; |
| 1991 | } else if (NeedIntPromot) { |
| 1992 | if (!this->emitPrimCast(FromT: ElemT, ToT: PromotT, ToQT: PromotTy, E)) |
| 1993 | return false; |
| 1994 | } |
| 1995 | return true; |
| 1996 | }; |
| 1997 | |
| 1998 | #define EMIT_ARITH_OP(OP) \ |
| 1999 | { \ |
| 2000 | if (ElemT == PT_Float) { \ |
| 2001 | if (!this->emit##OP##f(getFPOptions(E), E)) \ |
| 2002 | return false; \ |
| 2003 | } else { \ |
| 2004 | if (!this->emit##OP(ElemT, E)) \ |
| 2005 | return false; \ |
| 2006 | } \ |
| 2007 | break; \ |
| 2008 | } |
| 2009 | |
| 2010 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 2011 | if (!getElem(LHSOffset, ElemT, I)) |
| 2012 | return false; |
| 2013 | if (!getElem(RHSOffset, RHSElemT, I)) |
| 2014 | return false; |
| 2015 | switch (Op) { |
| 2016 | case BO_Add: |
| 2017 | EMIT_ARITH_OP(Add) |
| 2018 | case BO_Sub: |
| 2019 | EMIT_ARITH_OP(Sub) |
| 2020 | case BO_Mul: |
| 2021 | EMIT_ARITH_OP(Mul) |
| 2022 | case BO_Div: |
| 2023 | EMIT_ARITH_OP(Div) |
| 2024 | case BO_Rem: |
| 2025 | if (!this->emitRem(ElemT, E)) |
| 2026 | return false; |
| 2027 | break; |
| 2028 | case BO_And: |
| 2029 | if (!this->emitBitAnd(OpT, E)) |
| 2030 | return false; |
| 2031 | break; |
| 2032 | case BO_Or: |
| 2033 | if (!this->emitBitOr(OpT, E)) |
| 2034 | return false; |
| 2035 | break; |
| 2036 | case BO_Xor: |
| 2037 | if (!this->emitBitXor(OpT, E)) |
| 2038 | return false; |
| 2039 | break; |
| 2040 | case BO_Shl: |
| 2041 | if (!this->emitShl(OpT, RHSElemT, E)) |
| 2042 | return false; |
| 2043 | break; |
| 2044 | case BO_Shr: |
| 2045 | if (!this->emitShr(OpT, RHSElemT, E)) |
| 2046 | return false; |
| 2047 | break; |
| 2048 | case BO_EQ: |
| 2049 | if (!this->emitEQ(ElemT, E)) |
| 2050 | return false; |
| 2051 | break; |
| 2052 | case BO_NE: |
| 2053 | if (!this->emitNE(ElemT, E)) |
| 2054 | return false; |
| 2055 | break; |
| 2056 | case BO_LE: |
| 2057 | if (!this->emitLE(ElemT, E)) |
| 2058 | return false; |
| 2059 | break; |
| 2060 | case BO_LT: |
| 2061 | if (!this->emitLT(ElemT, E)) |
| 2062 | return false; |
| 2063 | break; |
| 2064 | case BO_GE: |
| 2065 | if (!this->emitGE(ElemT, E)) |
| 2066 | return false; |
| 2067 | break; |
| 2068 | case BO_GT: |
| 2069 | if (!this->emitGT(ElemT, E)) |
| 2070 | return false; |
| 2071 | break; |
| 2072 | case BO_LAnd: |
| 2073 | // a && b is equivalent to a!=0 & b!=0 |
| 2074 | if (!this->emitBitAnd(ResultElemT, E)) |
| 2075 | return false; |
| 2076 | break; |
| 2077 | case BO_LOr: |
| 2078 | // a || b is equivalent to a!=0 | b!=0 |
| 2079 | if (!this->emitBitOr(ResultElemT, E)) |
| 2080 | return false; |
| 2081 | break; |
| 2082 | default: |
| 2083 | return this->emitInvalid(E); |
| 2084 | } |
| 2085 | |
| 2086 | // The result of the comparison is a vector of the same width and number |
| 2087 | // of elements as the comparison operands with a signed integral element |
| 2088 | // type. |
| 2089 | // |
| 2090 | // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html |
| 2091 | if (E->isComparisonOp()) { |
| 2092 | if (!this->emitPrimCast(FromT: PT_Bool, ToT: ResultElemT, ToQT: VecTy->getElementType(), E)) |
| 2093 | return false; |
| 2094 | if (!this->emitNeg(ResultElemT, E)) |
| 2095 | return false; |
| 2096 | } |
| 2097 | |
| 2098 | // If we performed an integer promotion, we need to cast the compute result |
| 2099 | // into result vector element type. |
| 2100 | if (NeedIntPromot && |
| 2101 | !this->emitPrimCast(FromT: PromotT, ToT: ResultElemT, ToQT: VecTy->getElementType(), E)) |
| 2102 | return false; |
| 2103 | |
| 2104 | // Initialize array element with the value we just computed. |
| 2105 | if (!this->emitInitElem(ResultElemT, I, E)) |
| 2106 | return false; |
| 2107 | } |
| 2108 | |
| 2109 | if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E)) |
| 2110 | return false; |
| 2111 | return true; |
| 2112 | } |
| 2113 | |
| 2114 | template <class Emitter> |
| 2115 | bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) { |
| 2116 | const Expr *LHS = E->getLHS(); |
| 2117 | const Expr *RHS = E->getRHS(); |
| 2118 | const ASTContext &ASTCtx = Ctx.getASTContext(); |
| 2119 | |
| 2120 | assert(LHS->getType()->isFixedPointType() || |
| 2121 | RHS->getType()->isFixedPointType()); |
| 2122 | |
| 2123 | auto LHSSema = ASTCtx.getFixedPointSemantics(Ty: LHS->getType()); |
| 2124 | auto LHSSemaInt = LHSSema.toOpaqueInt(); |
| 2125 | auto RHSSema = ASTCtx.getFixedPointSemantics(Ty: RHS->getType()); |
| 2126 | auto RHSSemaInt = RHSSema.toOpaqueInt(); |
| 2127 | |
| 2128 | if (!this->visit(E: LHS)) |
| 2129 | return false; |
| 2130 | if (!LHS->getType()->isFixedPointType()) { |
| 2131 | if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), |
| 2132 | LHSSemaInt, E)) |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
| 2136 | if (!this->visit(E: RHS)) |
| 2137 | return false; |
| 2138 | if (!RHS->getType()->isFixedPointType()) { |
| 2139 | if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), |
| 2140 | RHSSemaInt, E)) |
| 2141 | return false; |
| 2142 | } |
| 2143 | |
| 2144 | // Convert the result to the target semantics. |
| 2145 | auto ConvertResult = [&](bool R) -> bool { |
| 2146 | if (!R) |
| 2147 | return false; |
| 2148 | auto ResultSema = ASTCtx.getFixedPointSemantics(Ty: E->getType()).toOpaqueInt(); |
| 2149 | auto CommonSema = LHSSema.getCommonSemantics(Other: RHSSema).toOpaqueInt(); |
| 2150 | if (ResultSema != CommonSema) |
| 2151 | return this->emitCastFixedPoint(ResultSema, E); |
| 2152 | return true; |
| 2153 | }; |
| 2154 | |
| 2155 | auto MaybeCastToBool = [&](bool Result) { |
| 2156 | if (!Result) |
| 2157 | return false; |
| 2158 | PrimType T = classifyPrim(E); |
| 2159 | if (DiscardResult) |
| 2160 | return this->emitPop(T, E); |
| 2161 | if (T != PT_Bool) |
| 2162 | return this->emitCast(PT_Bool, T, E); |
| 2163 | return true; |
| 2164 | }; |
| 2165 | |
| 2166 | switch (E->getOpcode()) { |
| 2167 | case BO_EQ: |
| 2168 | return MaybeCastToBool(this->emitEQFixedPoint(E)); |
| 2169 | case BO_NE: |
| 2170 | return MaybeCastToBool(this->emitNEFixedPoint(E)); |
| 2171 | case BO_LT: |
| 2172 | return MaybeCastToBool(this->emitLTFixedPoint(E)); |
| 2173 | case BO_LE: |
| 2174 | return MaybeCastToBool(this->emitLEFixedPoint(E)); |
| 2175 | case BO_GT: |
| 2176 | return MaybeCastToBool(this->emitGTFixedPoint(E)); |
| 2177 | case BO_GE: |
| 2178 | return MaybeCastToBool(this->emitGEFixedPoint(E)); |
| 2179 | case BO_Add: |
| 2180 | return ConvertResult(this->emitAddFixedPoint(E)); |
| 2181 | case BO_Sub: |
| 2182 | return ConvertResult(this->emitSubFixedPoint(E)); |
| 2183 | case BO_Mul: |
| 2184 | return ConvertResult(this->emitMulFixedPoint(E)); |
| 2185 | case BO_Div: |
| 2186 | return ConvertResult(this->emitDivFixedPoint(E)); |
| 2187 | case BO_Shl: |
| 2188 | return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E)); |
| 2189 | case BO_Shr: |
| 2190 | return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E)); |
| 2191 | |
| 2192 | default: |
| 2193 | return this->emitInvalid(E); |
| 2194 | } |
| 2195 | |
| 2196 | llvm_unreachable("unhandled binop opcode" ); |
| 2197 | } |
| 2198 | |
| 2199 | template <class Emitter> |
| 2200 | bool Compiler<Emitter>::VisitFixedPointUnaryOperator(const UnaryOperator *E) { |
| 2201 | const Expr *SubExpr = E->getSubExpr(); |
| 2202 | assert(SubExpr->getType()->isFixedPointType()); |
| 2203 | |
| 2204 | switch (E->getOpcode()) { |
| 2205 | case UO_Plus: |
| 2206 | return this->delegate(E: SubExpr); |
| 2207 | case UO_Minus: |
| 2208 | if (!this->visit(E: SubExpr)) |
| 2209 | return false; |
| 2210 | if (!this->emitNegFixedPoint(E)) |
| 2211 | return false; |
| 2212 | if (DiscardResult) |
| 2213 | return this->emitPopFixedPoint(E); |
| 2214 | return true; |
| 2215 | default: |
| 2216 | return false; |
| 2217 | } |
| 2218 | |
| 2219 | llvm_unreachable("Unhandled unary opcode" ); |
| 2220 | } |
| 2221 | |
| 2222 | template <class Emitter> |
| 2223 | bool Compiler<Emitter>::VisitImplicitValueInitExpr( |
| 2224 | const ImplicitValueInitExpr *E) { |
| 2225 | if (DiscardResult) |
| 2226 | return true; |
| 2227 | |
| 2228 | QualType QT = E->getType(); |
| 2229 | |
| 2230 | if (OptPrimType T = classify(QT)) |
| 2231 | return this->visitZeroInitializer(T: *T, QT, E); |
| 2232 | |
| 2233 | if (QT->isRecordType()) { |
| 2234 | const RecordDecl *RD = QT->getAsRecordDecl(); |
| 2235 | assert(RD); |
| 2236 | if (RD->isInvalidDecl()) |
| 2237 | return false; |
| 2238 | |
| 2239 | const Record *R = getRecord(QT); |
| 2240 | if (!R) |
| 2241 | return false; |
| 2242 | |
| 2243 | assert(Initializing); |
| 2244 | return this->visitZeroRecordInitializer(R, E); |
| 2245 | } |
| 2246 | |
| 2247 | if (QT->isIncompleteArrayType()) |
| 2248 | return true; |
| 2249 | |
| 2250 | if (QT->isArrayType()) |
| 2251 | return this->visitZeroArrayInitializer(T: QT, E); |
| 2252 | |
| 2253 | if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) { |
| 2254 | assert(Initializing); |
| 2255 | QualType ElemQT = ComplexTy->getElementType(); |
| 2256 | PrimType ElemT = classifyPrim(ElemQT); |
| 2257 | for (unsigned I = 0; I < 2; ++I) { |
| 2258 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2259 | return false; |
| 2260 | if (!this->emitInitElem(ElemT, I, E)) |
| 2261 | return false; |
| 2262 | } |
| 2263 | return true; |
| 2264 | } |
| 2265 | |
| 2266 | if (const auto *VecT = E->getType()->getAs<VectorType>()) { |
| 2267 | unsigned NumVecElements = VecT->getNumElements(); |
| 2268 | QualType ElemQT = VecT->getElementType(); |
| 2269 | PrimType ElemT = classifyPrim(ElemQT); |
| 2270 | |
| 2271 | for (unsigned I = 0; I < NumVecElements; ++I) { |
| 2272 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2273 | return false; |
| 2274 | if (!this->emitInitElem(ElemT, I, E)) |
| 2275 | return false; |
| 2276 | } |
| 2277 | return true; |
| 2278 | } |
| 2279 | |
| 2280 | if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) { |
| 2281 | unsigned NumElems = MT->getNumElementsFlattened(); |
| 2282 | QualType ElemQT = MT->getElementType(); |
| 2283 | PrimType ElemT = classifyPrim(ElemQT); |
| 2284 | |
| 2285 | for (unsigned I = 0; I != NumElems; ++I) { |
| 2286 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2287 | return false; |
| 2288 | if (!this->emitInitElem(ElemT, I, E)) |
| 2289 | return false; |
| 2290 | } |
| 2291 | return true; |
| 2292 | } |
| 2293 | |
| 2294 | return false; |
| 2295 | } |
| 2296 | |
| 2297 | template <class Emitter> |
| 2298 | bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
| 2299 | if (E->getType()->isVoidType() || E->containsErrors()) |
| 2300 | return false; |
| 2301 | |
| 2302 | const Expr *LHS = E->getLHS(); |
| 2303 | const Expr *RHS = E->getRHS(); |
| 2304 | const Expr *Index = E->getIdx(); |
| 2305 | const Expr *Base = E->getBase(); |
| 2306 | |
| 2307 | // C++17's rules require us to evaluate the LHS first, regardless of which |
| 2308 | // side is the base. |
| 2309 | bool Success = true; |
| 2310 | for (const Expr *SubExpr : {LHS, RHS}) { |
| 2311 | if (!this->visit(E: SubExpr)) { |
| 2312 | Success = false; |
| 2313 | continue; |
| 2314 | } |
| 2315 | |
| 2316 | // Expand the base if this is a subscript on a |
| 2317 | // pointer expression. |
| 2318 | if (SubExpr == Base && Base->getType()->isPointerType()) { |
| 2319 | if (!this->emitExpandPtr(E)) |
| 2320 | Success = false; |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | if (!Success) |
| 2325 | return false; |
| 2326 | |
| 2327 | OptPrimType IndexT = classify(Index->getType()); |
| 2328 | // In error-recovery cases, the index expression has a dependent type. |
| 2329 | if (!IndexT) |
| 2330 | return this->emitError(E); |
| 2331 | // If the index is first, we need to change that. |
| 2332 | if (LHS == Index) { |
| 2333 | if (!this->emitFlip(PT_Ptr, *IndexT, E)) |
| 2334 | return false; |
| 2335 | } |
| 2336 | |
| 2337 | if (!this->emitArrayElemPtrPop(*IndexT, E)) |
| 2338 | return false; |
| 2339 | if (DiscardResult) |
| 2340 | return this->emitPopPtr(E); |
| 2341 | |
| 2342 | if (E->isGLValue()) |
| 2343 | return true; |
| 2344 | |
| 2345 | OptPrimType T = classifyPrim(E); |
| 2346 | return this->emitLoadPop(*T, E); |
| 2347 | } |
| 2348 | |
| 2349 | template <class Emitter> |
| 2350 | bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits, |
| 2351 | const Expr *ArrayFiller, const Expr *E) { |
| 2352 | InitLinkScope<Emitter> ILS(this, InitLink::InitList()); |
| 2353 | |
| 2354 | QualType QT = E->getType(); |
| 2355 | if (const auto *AT = QT->getAs<AtomicType>()) |
| 2356 | QT = AT->getValueType(); |
| 2357 | |
| 2358 | if (QT->isVoidType()) { |
| 2359 | if (Inits.size() == 0) |
| 2360 | return true; |
| 2361 | return this->emitInvalid(E); |
| 2362 | } |
| 2363 | |
| 2364 | // Primitive values. A discarded one can simply discard each initializer; |
| 2365 | // there is no object to establish. |
| 2366 | if (OptPrimType T = classify(QT)) { |
| 2367 | if (DiscardResult) { |
| 2368 | for (const Expr *Init : Inits) { |
| 2369 | if (!this->discard(E: Init)) |
| 2370 | return false; |
| 2371 | } |
| 2372 | return true; |
| 2373 | } |
| 2374 | if (Inits.size() == 0) |
| 2375 | return this->visitZeroInitializer(T: *T, QT, E); |
| 2376 | assert(Inits.size() == 1); |
| 2377 | return this->delegate(E: Inits[0]); |
| 2378 | } |
| 2379 | |
| 2380 | assert(!canClassify(E->getType())); |
| 2381 | |
| 2382 | // A composite prvalue needs somewhere to live even when it is discarded: a |
| 2383 | // default member initializer may read subobjects initialized earlier in this |
| 2384 | // same list, so those have to actually be written and `this` has to denote |
| 2385 | // the object. Materialize one and initialize into it. |
| 2386 | if (DiscardResult && !Initializing) { |
| 2387 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 2388 | if (!LocalIndex) |
| 2389 | return false; |
| 2390 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 2391 | return false; |
| 2392 | InitLinkScope<Emitter> ILS2(this, InitLink::Temp(Offset: *LocalIndex)); |
| 2393 | return this->visitInitializerPop(E); |
| 2394 | } |
| 2395 | |
| 2396 | if (QT->isRecordType()) { |
| 2397 | const Record *R = getRecord(QT); |
| 2398 | |
| 2399 | if (Inits.size() == 1 && E->getType() == Inits[0]->getType()) |
| 2400 | return this->delegate(E: Inits[0]); |
| 2401 | |
| 2402 | if (!R) |
| 2403 | return false; |
| 2404 | |
| 2405 | auto initPrimitiveField = [=](const Record::Field *FieldToInit, |
| 2406 | const Expr *Init, PrimType T, |
| 2407 | bool Activate = false) -> bool { |
| 2408 | bool DefaultInit = isa<CXXDefaultInitExpr>(Val: Init); |
| 2409 | InitStackScope<Emitter> ISS(this, DefaultInit); |
| 2410 | |
| 2411 | if (DefaultInit && !this->emitStartFieldInit(FieldToInit->Offset, Init)) |
| 2412 | return false; |
| 2413 | |
| 2414 | if (!this->visit(E: Init)) |
| 2415 | return false; |
| 2416 | |
| 2417 | if (DefaultInit && !this->emitEndInit(Init)) |
| 2418 | return false; |
| 2419 | |
| 2420 | bool BitField = FieldToInit->isBitField(); |
| 2421 | if (BitField && Activate) |
| 2422 | return this->emitInitBitFieldActivate(T, FieldToInit->Offset, |
| 2423 | FieldToInit->bitWidth(), E); |
| 2424 | if (BitField) |
| 2425 | return this->emitInitBitField(T, FieldToInit->Offset, |
| 2426 | FieldToInit->bitWidth(), E); |
| 2427 | if (Activate) |
| 2428 | return this->emitInitFieldActivate(T, FieldToInit->Offset, E); |
| 2429 | return this->emitInitField(T, FieldToInit->Offset, E); |
| 2430 | }; |
| 2431 | |
| 2432 | auto initCompositeField = [=](const Record::Field *FieldToInit, |
| 2433 | const Expr *Init, |
| 2434 | bool Activate = false) -> bool { |
| 2435 | InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: Init)); |
| 2436 | InitLinkScope<Emitter> ILS(this, InitLink::Field(Offset: FieldToInit->Offset)); |
| 2437 | |
| 2438 | // Non-primitive case. Get a pointer to the field-to-initialize |
| 2439 | // on the stack and recurse into visitInitializer(). |
| 2440 | if (!this->emitGetPtrField(FieldToInit->Offset, Init)) |
| 2441 | return false; |
| 2442 | |
| 2443 | if (Activate && !this->emitActivate(E)) |
| 2444 | return false; |
| 2445 | |
| 2446 | if (!this->emitStartInit(Init)) |
| 2447 | return false; |
| 2448 | |
| 2449 | return this->visitInitializerPop(E: Init) && this->emitEndInit(Init); |
| 2450 | }; |
| 2451 | |
| 2452 | if (R->isUnion()) { |
| 2453 | if (Inits.size() == 0) { |
| 2454 | if (!this->visitZeroRecordInitializer(R, E)) |
| 2455 | return false; |
| 2456 | } else { |
| 2457 | const Expr *Init = Inits[0]; |
| 2458 | const FieldDecl *FToInit = nullptr; |
| 2459 | if (const auto *ILE = dyn_cast<InitListExpr>(Val: E)) |
| 2460 | FToInit = ILE->getInitializedFieldInUnion(); |
| 2461 | else |
| 2462 | FToInit = cast<CXXParenListInitExpr>(Val: E)->getInitializedFieldInUnion(); |
| 2463 | |
| 2464 | const Record::Field *FieldToInit = R->getField(FD: FToInit); |
| 2465 | if (OptPrimType T = classify(Init)) { |
| 2466 | if (!initPrimitiveField(FieldToInit, Init, *T, /*Activate=*/true)) |
| 2467 | return false; |
| 2468 | } else { |
| 2469 | if (!initCompositeField(FieldToInit, Init, /*Activate=*/true)) |
| 2470 | return false; |
| 2471 | } |
| 2472 | } |
| 2473 | return this->emitFinishInit(E); |
| 2474 | } |
| 2475 | |
| 2476 | assert(!R->isUnion()); |
| 2477 | for (unsigned BI = 0; BI != R->getNumBases(); ++BI) { |
| 2478 | const Expr *Init = Inits[BI]; |
| 2479 | const Record::Base *B = R->getBase(I: BI); |
| 2480 | InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: Init)); |
| 2481 | InitLinkScope<Emitter> ILS(this, InitLink::Base(Offset: B->Offset)); |
| 2482 | if (!this->emitGetPtrBase(B->Offset, Init)) |
| 2483 | return false; |
| 2484 | if (!this->visitInitializerPop(E: Init)) |
| 2485 | return false; |
| 2486 | } |
| 2487 | |
| 2488 | unsigned FieldIndex = 0; |
| 2489 | for (unsigned FI = R->getNumBases(); FI != Inits.size();) { |
| 2490 | const Record::Field *FieldToInit = R->getField(I: FieldIndex); |
| 2491 | if (FieldToInit->isUnnamedBitField()) { |
| 2492 | ++FieldIndex; |
| 2493 | continue; |
| 2494 | } |
| 2495 | |
| 2496 | const Expr *Init = Inits[FI]; |
| 2497 | // If this is a child of a DesignatedInitUpdateExpr, skip elements which |
| 2498 | // aren't supposed to be modified. |
| 2499 | if (isa<NoInitExpr>(Val: Init)) { |
| 2500 | ++FieldIndex; |
| 2501 | ++FI; |
| 2502 | continue; |
| 2503 | } |
| 2504 | |
| 2505 | if (OptPrimType T = classify(Init)) { |
| 2506 | if (!initPrimitiveField(FieldToInit, Init, *T)) |
| 2507 | return false; |
| 2508 | } else if (!initCompositeField(FieldToInit, Init)) { |
| 2509 | return false; |
| 2510 | } |
| 2511 | |
| 2512 | ++FI; |
| 2513 | ++FieldIndex; |
| 2514 | } |
| 2515 | |
| 2516 | assert(R->getNumVirtualBases() == 0); |
| 2517 | |
| 2518 | return this->emitFinishInit(E); |
| 2519 | } |
| 2520 | |
| 2521 | if (QT->isArrayType()) { |
| 2522 | const ConstantArrayType *CAT = |
| 2523 | Ctx.getASTContext().getAsConstantArrayType(T: QT); |
| 2524 | uint64_t NumElems = CAT->getZExtSize(); |
| 2525 | |
| 2526 | if (Initializing && |
| 2527 | (!InitializingDecl || InitializingDecl->hasLocalStorage()) && |
| 2528 | !this->emitCheckArrayDestSize(NumElems, E)) |
| 2529 | return false; |
| 2530 | |
| 2531 | if (Inits.size() == 1 && QT == Inits[0]->getType()) |
| 2532 | return this->delegate(E: Inits[0]); |
| 2533 | |
| 2534 | OptPrimType InitT = classify(CAT->getElementType()); |
| 2535 | unsigned ElementIndex = 0; |
| 2536 | for (const Expr *Init : Inits) { |
| 2537 | if (const auto *EmbedS = |
| 2538 | dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) { |
| 2539 | PrimType TargetT = classifyPrim(Init->getType()); |
| 2540 | |
| 2541 | auto Eval = [&](const IntegerLiteral *IL, unsigned ElemIndex) { |
| 2542 | if (TargetT == PT_Float) { |
| 2543 | if (!this->emitConst(IL->getValue(), classifyPrim(IL), Init)) |
| 2544 | return false; |
| 2545 | const auto *Sem = &Ctx.getFloatSemantics(T: CAT->getElementType()); |
| 2546 | if (!this->emitCastIntegralFloating(classifyPrim(IL), Sem, |
| 2547 | getFPOptions(E), E)) |
| 2548 | return false; |
| 2549 | } else { |
| 2550 | if (!this->emitConst(IL->getValue(), TargetT, Init)) |
| 2551 | return false; |
| 2552 | } |
| 2553 | return this->emitInitElem(TargetT, ElemIndex, IL); |
| 2554 | }; |
| 2555 | if (!EmbedS->doForEachDataElement(Eval, ElementIndex)) |
| 2556 | return false; |
| 2557 | } else if (isa<NoInitExpr>(Val: Init)) { |
| 2558 | // If this is a child of a DesignatedInitUpdateExpr, skip elements which |
| 2559 | // aren't supposed to be modified. |
| 2560 | ++ElementIndex; |
| 2561 | } else { |
| 2562 | if (!this->visitArrayElemInit(ElemIndex: ElementIndex, Init, InitT)) |
| 2563 | return false; |
| 2564 | ++ElementIndex; |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | // Expand the filler expression. |
| 2569 | // FIXME: This should go away. |
| 2570 | if (ArrayFiller && !isa<NoInitExpr>(Val: ArrayFiller)) { |
| 2571 | for (; ElementIndex != NumElems; ++ElementIndex) { |
| 2572 | if (!this->visitArrayElemInit(ElemIndex: ElementIndex, Init: ArrayFiller, InitT)) |
| 2573 | return false; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | return this->emitFinishInit(E); |
| 2578 | } |
| 2579 | |
| 2580 | if (const auto *ComplexTy = QT->getAs<ComplexType>()) { |
| 2581 | unsigned NumInits = Inits.size(); |
| 2582 | |
| 2583 | if (NumInits == 1) |
| 2584 | return this->delegate(E: Inits[0]); |
| 2585 | |
| 2586 | QualType ElemQT = ComplexTy->getElementType(); |
| 2587 | PrimType ElemT = classifyPrim(ElemQT); |
| 2588 | if (NumInits == 0) { |
| 2589 | // Zero-initialize both elements. |
| 2590 | for (unsigned I = 0; I < 2; ++I) { |
| 2591 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2592 | return false; |
| 2593 | if (!this->emitInitElem(ElemT, I, E)) |
| 2594 | return false; |
| 2595 | } |
| 2596 | } else if (NumInits == 2) { |
| 2597 | unsigned InitIndex = 0; |
| 2598 | for (const Expr *Init : Inits) { |
| 2599 | if (!this->visit(E: Init)) |
| 2600 | return false; |
| 2601 | |
| 2602 | if (!this->emitInitElem(ElemT, InitIndex, E)) |
| 2603 | return false; |
| 2604 | ++InitIndex; |
| 2605 | } |
| 2606 | } |
| 2607 | return true; |
| 2608 | } |
| 2609 | |
| 2610 | if (const auto *VecT = QT->getAs<VectorType>()) { |
| 2611 | unsigned NumVecElements = VecT->getNumElements(); |
| 2612 | assert(NumVecElements >= Inits.size()); |
| 2613 | |
| 2614 | QualType ElemQT = VecT->getElementType(); |
| 2615 | PrimType ElemT = classifyPrim(ElemQT); |
| 2616 | |
| 2617 | // All initializer elements. |
| 2618 | unsigned InitIndex = 0; |
| 2619 | for (const Expr *Init : Inits) { |
| 2620 | if (!this->visit(E: Init)) |
| 2621 | return false; |
| 2622 | |
| 2623 | // If the initializer is of vector type itself, we have to deconstruct |
| 2624 | // that and initialize all the target fields from the initializer fields. |
| 2625 | if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) { |
| 2626 | if (!this->emitCopyArray(ElemT, 0, InitIndex, |
| 2627 | InitVecT->getNumElements(), E)) |
| 2628 | return false; |
| 2629 | InitIndex += InitVecT->getNumElements(); |
| 2630 | } else { |
| 2631 | if (!this->emitInitElem(ElemT, InitIndex, E)) |
| 2632 | return false; |
| 2633 | ++InitIndex; |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | assert(InitIndex <= NumVecElements); |
| 2638 | |
| 2639 | // Fill the rest with zeroes. |
| 2640 | for (; InitIndex != NumVecElements; ++InitIndex) { |
| 2641 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 2642 | return false; |
| 2643 | if (!this->emitInitElem(ElemT, InitIndex, E)) |
| 2644 | return false; |
| 2645 | } |
| 2646 | return true; |
| 2647 | } |
| 2648 | |
| 2649 | if (const auto *MT = QT->getAs<ConstantMatrixType>()) { |
| 2650 | unsigned NumElems = MT->getNumElementsFlattened(); |
| 2651 | assert(Inits.size() == NumElems); |
| 2652 | |
| 2653 | QualType ElemQT = MT->getElementType(); |
| 2654 | PrimType ElemT = classifyPrim(ElemQT); |
| 2655 | |
| 2656 | // Matrix initializer list elements are in row-major order, which matches |
| 2657 | // the matrix APValue convention and therefore no index remapping is |
| 2658 | // required. |
| 2659 | for (unsigned I = 0; I != NumElems; ++I) { |
| 2660 | if (!this->visit(E: Inits[I])) |
| 2661 | return false; |
| 2662 | if (!this->emitInitElem(ElemT, I, E)) |
| 2663 | return false; |
| 2664 | } |
| 2665 | return true; |
| 2666 | } |
| 2667 | |
| 2668 | return false; |
| 2669 | } |
| 2670 | |
| 2671 | /// Pointer to the array(not the element!) must be on the stack when calling |
| 2672 | /// this. |
| 2673 | template <class Emitter> |
| 2674 | bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init, |
| 2675 | OptPrimType InitT) { |
| 2676 | if (InitT) { |
| 2677 | // Visit the primitive element like normal. |
| 2678 | if (!this->visit(E: Init)) |
| 2679 | return false; |
| 2680 | return this->emitInitElem(*InitT, ElemIndex, Init); |
| 2681 | } |
| 2682 | |
| 2683 | InitLinkScope<Emitter> ILS(this, InitLink::Elem(Index: ElemIndex)); |
| 2684 | // Advance the pointer currently on the stack to the given |
| 2685 | // dimension. |
| 2686 | if (!this->emitConstUint32(ElemIndex, Init)) |
| 2687 | return false; |
| 2688 | if (!this->emitArrayElemPtrUint32(Init)) |
| 2689 | return false; |
| 2690 | return this->visitInitializerPop(E: Init); |
| 2691 | } |
| 2692 | |
| 2693 | template <class Emitter> |
| 2694 | bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args, |
| 2695 | const FunctionDecl *FuncDecl, |
| 2696 | bool Activate, bool IsOperatorCall) { |
| 2697 | assert(VarScope->getKind() == ScopeKind::Call); |
| 2698 | llvm::BitVector NonNullArgs; |
| 2699 | if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>()) |
| 2700 | NonNullArgs = collectNonNullArgs(F: FuncDecl, Args); |
| 2701 | |
| 2702 | bool ExplicitMemberFn = false; |
| 2703 | if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl)) |
| 2704 | ExplicitMemberFn = MD->isExplicitObjectMemberFunction(); |
| 2705 | |
| 2706 | unsigned ArgIndex = 0; |
| 2707 | for (const Expr *Arg : Args) { |
| 2708 | if (canClassify(Arg)) { |
| 2709 | if (!this->visit(E: Arg)) |
| 2710 | return false; |
| 2711 | } else { |
| 2712 | |
| 2713 | DeclOrExpr Source = Arg; |
| 2714 | if (FuncDecl) { |
| 2715 | // Try to use the parameter declaration instead of the argument |
| 2716 | // expression as a source. |
| 2717 | unsigned DeclIndex = ArgIndex - IsOperatorCall + ExplicitMemberFn; |
| 2718 | if (DeclIndex < FuncDecl->getNumParams()) |
| 2719 | Source = FuncDecl->getParamDecl(i: ArgIndex - IsOperatorCall + |
| 2720 | ExplicitMemberFn); |
| 2721 | } |
| 2722 | |
| 2723 | UnsignedOrNone LocalIndex = |
| 2724 | allocateLocal(Decl: Source, Ty: Arg->getType(), ScopeKind::Call); |
| 2725 | if (!LocalIndex) |
| 2726 | return false; |
| 2727 | |
| 2728 | if (!this->emitGetPtrLocal(*LocalIndex, Arg)) |
| 2729 | return false; |
| 2730 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex)); |
| 2731 | if (!this->visitInitializer(E: Arg)) |
| 2732 | return false; |
| 2733 | } |
| 2734 | |
| 2735 | if (ArgIndex == 1 && Activate) { |
| 2736 | if (!this->emitActivate(Arg)) |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
| 2740 | if (!NonNullArgs.empty() && NonNullArgs[ArgIndex]) { |
| 2741 | PrimType ArgT = classify(Arg).value_or(PT_Ptr); |
| 2742 | if (ArgT == PT_Ptr) { |
| 2743 | if (!this->emitCheckNonNullArg(ArgT, Arg)) |
| 2744 | return false; |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | ++ArgIndex; |
| 2749 | } |
| 2750 | |
| 2751 | return true; |
| 2752 | } |
| 2753 | |
| 2754 | template <class Emitter> |
| 2755 | bool Compiler<Emitter>::VisitInitListExpr(const InitListExpr *E) { |
| 2756 | return this->visitInitList(Inits: E->inits(), ArrayFiller: E->getArrayFiller(), E); |
| 2757 | } |
| 2758 | |
| 2759 | template <class Emitter> |
| 2760 | bool Compiler<Emitter>::VisitCXXParenListInitExpr( |
| 2761 | const CXXParenListInitExpr *E) { |
| 2762 | return this->visitInitList(Inits: E->getInitExprs(), ArrayFiller: E->getArrayFiller(), E); |
| 2763 | } |
| 2764 | |
| 2765 | template <class Emitter> |
| 2766 | bool Compiler<Emitter>::VisitSubstNonTypeTemplateParmExpr( |
| 2767 | const SubstNonTypeTemplateParmExpr *E) { |
| 2768 | return this->delegate(E: E->getReplacement()); |
| 2769 | } |
| 2770 | |
| 2771 | template <class Emitter> |
| 2772 | bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) { |
| 2773 | if (!E->hasAPValueResult()) |
| 2774 | return this->delegate(E: E->getSubExpr()); |
| 2775 | |
| 2776 | if (OptPrimType T = classify(E)) { |
| 2777 | // Try to emit the APValue directly, without visiting the subexpr. |
| 2778 | // This will only fail if we can't emit the APValue, so won't emit any |
| 2779 | // diagnostics or any double values. |
| 2780 | if (DiscardResult) |
| 2781 | return true; |
| 2782 | return this->visitAPValue(Val: E->getAPValueResult(), ValType: *T, Info: E); |
| 2783 | } |
| 2784 | |
| 2785 | // Fall back to the subexpr for non-primitive APValues. |
| 2786 | return this->delegate(E: E->getSubExpr()); |
| 2787 | } |
| 2788 | |
| 2789 | template <class Emitter> |
| 2790 | bool Compiler<Emitter>::VisitEmbedExpr(const EmbedExpr *E) { |
| 2791 | auto It = E->begin(); |
| 2792 | return this->visit(E: *It); |
| 2793 | } |
| 2794 | |
| 2795 | static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx, |
| 2796 | UnaryExprOrTypeTrait Kind) { |
| 2797 | bool AlignOfReturnsPreferred = |
| 2798 | ASTCtx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7); |
| 2799 | |
| 2800 | // C++ [expr.alignof]p3: |
| 2801 | // When alignof is applied to a reference type, the result is the |
| 2802 | // alignment of the referenced type. |
| 2803 | if (const auto *Ref = T->getAs<ReferenceType>()) |
| 2804 | T = Ref->getPointeeType(); |
| 2805 | |
| 2806 | if (T.getQualifiers().hasUnaligned()) |
| 2807 | return CharUnits::One(); |
| 2808 | |
| 2809 | // __alignof is defined to return the preferred alignment. |
| 2810 | // Before 8, clang returned the preferred alignment for alignof and |
| 2811 | // _Alignof as well. |
| 2812 | if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
| 2813 | return ASTCtx.toCharUnitsFromBits(BitSize: ASTCtx.getPreferredTypeAlign(T)); |
| 2814 | |
| 2815 | return ASTCtx.getTypeAlignInChars(T); |
| 2816 | } |
| 2817 | |
| 2818 | template <class Emitter> |
| 2819 | bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr( |
| 2820 | const UnaryExprOrTypeTraitExpr *E) { |
| 2821 | |
| 2822 | UnaryExprOrTypeTrait Kind = E->getKind(); |
| 2823 | const ASTContext &ASTCtx = Ctx.getASTContext(); |
| 2824 | |
| 2825 | if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) { |
| 2826 | QualType ArgType = E->getTypeOfArgument(); |
| 2827 | |
| 2828 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2829 | // the result is the size of the referenced type." |
| 2830 | if (const auto *Ref = ArgType->getAs<ReferenceType>()) |
| 2831 | ArgType = Ref->getPointeeType(); |
| 2832 | |
| 2833 | CharUnits Size; |
| 2834 | if (ArgType->isVoidType() || ArgType->isFunctionType()) |
| 2835 | Size = CharUnits::One(); |
| 2836 | else { |
| 2837 | if (ArgType->isDependentType() || !ArgType->isConstantSizeType()) |
| 2838 | return this->emitInvalid(E); |
| 2839 | |
| 2840 | if (Kind == UETT_SizeOf) |
| 2841 | Size = ASTCtx.getTypeSizeInChars(T: ArgType); |
| 2842 | else |
| 2843 | Size = ASTCtx.getTypeInfoDataSizeInChars(T: ArgType).Width; |
| 2844 | } |
| 2845 | |
| 2846 | if (DiscardResult) |
| 2847 | return true; |
| 2848 | |
| 2849 | return this->emitConst(Size.getQuantity(), E); |
| 2850 | } |
| 2851 | |
| 2852 | if (Kind == UETT_CountOf) { |
| 2853 | QualType Ty = E->getTypeOfArgument(); |
| 2854 | assert(Ty->isArrayType()); |
| 2855 | |
| 2856 | // We don't need to worry about array element qualifiers, so getting the |
| 2857 | // unsafe array type is fine. |
| 2858 | if (const auto *CAT = |
| 2859 | dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) { |
| 2860 | if (DiscardResult) |
| 2861 | return true; |
| 2862 | return this->emitConst(CAT->getSize(), E); |
| 2863 | } |
| 2864 | |
| 2865 | assert(!Ty->isConstantSizeType()); |
| 2866 | |
| 2867 | // If it's a variable-length array type, we need to check whether it is a |
| 2868 | // multidimensional array. If so, we need to check the size expression of |
| 2869 | // the VLA to see if it's a constant size. If so, we can return that value. |
| 2870 | const auto *VAT = ASTCtx.getAsVariableArrayType(T: Ty); |
| 2871 | assert(VAT); |
| 2872 | if (VAT->getElementType()->isArrayType()) { |
| 2873 | std::optional<APSInt> Res = |
| 2874 | VAT->getSizeExpr() |
| 2875 | ? VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: ASTCtx) |
| 2876 | : std::nullopt; |
| 2877 | if (Res) { |
| 2878 | if (DiscardResult) |
| 2879 | return true; |
| 2880 | return this->emitConst(*Res, E); |
| 2881 | } |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) { |
| 2886 | CharUnits Size; |
| 2887 | |
| 2888 | if (E->isArgumentType()) { |
| 2889 | QualType ArgType = E->getTypeOfArgument(); |
| 2890 | |
| 2891 | Size = AlignOfType(T: ArgType, ASTCtx, Kind); |
| 2892 | } else { |
| 2893 | // Argument is an expression, not a type. |
| 2894 | const Expr *Arg = E->getArgumentExpr()->IgnoreParens(); |
| 2895 | |
| 2896 | if (Arg->getType()->isDependentType()) |
| 2897 | return false; |
| 2898 | |
| 2899 | // The kinds of expressions that we have special-case logic here for |
| 2900 | // should be kept up to date with the special checks for those |
| 2901 | // expressions in Sema. |
| 2902 | |
| 2903 | // alignof decl is always accepted, even if it doesn't make sense: we |
| 2904 | // default to 1 in those cases. |
| 2905 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Arg)) |
| 2906 | Size = ASTCtx.getDeclAlign(D: DRE->getDecl(), |
| 2907 | /*RefAsPointee*/ ForAlignof: true); |
| 2908 | else if (const auto *ME = dyn_cast<MemberExpr>(Val: Arg)) |
| 2909 | Size = ASTCtx.getDeclAlign(D: ME->getMemberDecl(), |
| 2910 | /*RefAsPointee*/ ForAlignof: true); |
| 2911 | else |
| 2912 | Size = AlignOfType(T: Arg->getType(), ASTCtx, Kind); |
| 2913 | } |
| 2914 | |
| 2915 | if (DiscardResult) |
| 2916 | return true; |
| 2917 | |
| 2918 | return this->emitConst(Size.getQuantity(), E); |
| 2919 | } |
| 2920 | |
| 2921 | if (Kind == UETT_VectorElements) { |
| 2922 | if (E->containsErrors()) |
| 2923 | return false; |
| 2924 | |
| 2925 | if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) |
| 2926 | return this->emitConst(VT->getNumElements(), E); |
| 2927 | assert(E->getTypeOfArgument()->isSizelessVectorType()); |
| 2928 | return this->emitSizelessVectorElementSize(E); |
| 2929 | } |
| 2930 | |
| 2931 | if (Kind == UETT_VecStep) { |
| 2932 | if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) { |
| 2933 | unsigned N = VT->getNumElements(); |
| 2934 | |
| 2935 | // The vec_step built-in functions that take a 3-component |
| 2936 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 2937 | if (N == 3) |
| 2938 | N = 4; |
| 2939 | |
| 2940 | return this->emitConst(N, E); |
| 2941 | } |
| 2942 | return this->emitConst(1, E); |
| 2943 | } |
| 2944 | |
| 2945 | if (Kind == UETT_OpenMPRequiredSimdAlign) { |
| 2946 | if (E->containsErrors()) |
| 2947 | return false; |
| 2948 | assert(E->isArgumentType()); |
| 2949 | unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()); |
| 2950 | |
| 2951 | return this->emitConst(ASTCtx.toCharUnitsFromBits(BitSize: Bits).getQuantity(), E); |
| 2952 | } |
| 2953 | |
| 2954 | if (Kind == UETT_PtrAuthTypeDiscriminator) { |
| 2955 | if (E->getArgumentType()->isDependentType()) |
| 2956 | return this->emitInvalid(E); |
| 2957 | |
| 2958 | return this->emitConst( |
| 2959 | const_cast<ASTContext &>(ASTCtx).getPointerAuthTypeDiscriminator( |
| 2960 | T: E->getArgumentType()), |
| 2961 | E); |
| 2962 | } |
| 2963 | |
| 2964 | return false; |
| 2965 | } |
| 2966 | |
| 2967 | template <class Emitter> |
| 2968 | bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) { |
| 2969 | // 'Base.Member' |
| 2970 | const Expr *Base = E->getBase(); |
| 2971 | const ValueDecl *Member = E->getMemberDecl(); |
| 2972 | |
| 2973 | if (DiscardResult) |
| 2974 | return this->discard(E: Base); |
| 2975 | |
| 2976 | if (const auto *VD = dyn_cast<VarDecl>(Val: Member)) { |
| 2977 | // If the member is a VarDecl, this is a static variable. |
| 2978 | // We need to try to lazily evaluate its initializer here since the |
| 2979 | // variable might've been deserialized and not registered |
| 2980 | // as a global variable yet. |
| 2981 | if (VD->getInit() && !VD->getInit()->isValueDependent()) |
| 2982 | VD->evaluateValue(); |
| 2983 | if (auto GlobalIndex = P.getGlobal(VD)) { |
| 2984 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 2985 | return false; |
| 2986 | if (Member->getType()->isReferenceType()) |
| 2987 | return this->emitLoadPopPtr(E); |
| 2988 | return true; |
| 2989 | } |
| 2990 | return false; |
| 2991 | } |
| 2992 | |
| 2993 | if (!isa<FieldDecl>(Val: Member)) { |
| 2994 | // A non-static member function access only makes sense as part of the |
| 2995 | // enclosing call here. Don't try to evaluate it in isolation. |
| 2996 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Member); |
| 2997 | MD && !MD->isStatic()) { |
| 2998 | return false; |
| 2999 | } |
| 3000 | |
| 3001 | if (!this->discard(E: Base) && !this->emitSideEffect(E)) |
| 3002 | return false; |
| 3003 | |
| 3004 | return this->visitDeclRef(D: Member, E); |
| 3005 | } |
| 3006 | |
| 3007 | if (!this->visit(E: Base)) |
| 3008 | return false; |
| 3009 | |
| 3010 | // Base above gives us a pointer on the stack. |
| 3011 | const auto *FD = cast<FieldDecl>(Val: Member); |
| 3012 | const RecordDecl *RD = FD->getParent(); |
| 3013 | const Record *R = getRecord(RD); |
| 3014 | if (!R) |
| 3015 | return false; |
| 3016 | const Record::Field *F = R->getField(FD); |
| 3017 | |
| 3018 | // MemberExprs are almost always lvalues, in which case we don't need to |
| 3019 | // do the load. But sometimes they aren't. |
| 3020 | const auto maybeLoadValue = [&]() -> bool { |
| 3021 | if (E->isGLValue()) |
| 3022 | return true; |
| 3023 | if (OptPrimType T = classify(E)) |
| 3024 | return this->emitLoadPop(*T, E); |
| 3025 | return false; |
| 3026 | }; |
| 3027 | |
| 3028 | // Leave a pointer to the field on the stack. |
| 3029 | if (F->Decl->getType()->isReferenceType()) |
| 3030 | return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue(); |
| 3031 | return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue(); |
| 3032 | } |
| 3033 | |
| 3034 | template <class Emitter> |
| 3035 | bool Compiler<Emitter>::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
| 3036 | assert(!DiscardResult); |
| 3037 | // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated |
| 3038 | // stand-alone, e.g. via EvaluateAsInt(). |
| 3039 | if (!ArrayIndex) |
| 3040 | return false; |
| 3041 | return this->emitConst(*ArrayIndex, E); |
| 3042 | } |
| 3043 | |
| 3044 | template <class Emitter> |
| 3045 | bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
| 3046 | assert(Initializing); |
| 3047 | assert(!DiscardResult); |
| 3048 | |
| 3049 | const Expr *Common = E->getCommonExpr(); |
| 3050 | const Expr *SubExpr = E->getSubExpr(); |
| 3051 | OptPrimType SubExprT = classify(SubExpr); |
| 3052 | size_t Size = E->getArraySize().getZExtValue(); |
| 3053 | if (exceedsArraySizeLimit(LangOpts: Ctx.getLangOpts(), NumElems: Size)) |
| 3054 | return this->emitCheckArraySize(Size, E); |
| 3055 | |
| 3056 | if (SubExprT) { |
| 3057 | // Unwrap the OpaqueValueExpr so we don't cache something we won't reuse. |
| 3058 | Common = cast<OpaqueValueExpr>(Val: Common)->getSourceExpr(); |
| 3059 | |
| 3060 | if (!this->visit(E: Common)) |
| 3061 | return false; |
| 3062 | return this->emitCopyArray(*SubExprT, 0, 0, Size, E); |
| 3063 | } |
| 3064 | |
| 3065 | // We visit the common opaque expression here once so we have its value |
| 3066 | // cached. |
| 3067 | if (!this->discard(E: Common)) |
| 3068 | return false; |
| 3069 | |
| 3070 | // TODO: This compiles to quite a lot of bytecode if the array is larger. |
| 3071 | // Investigate compiling this to a loop. |
| 3072 | |
| 3073 | // So, every iteration, we execute an assignment here |
| 3074 | // where the LHS is on the stack (the target array) |
| 3075 | // and the RHS is our SubExpr. |
| 3076 | for (size_t I = 0; I != Size; ++I) { |
| 3077 | ArrayIndexScope<Emitter> IndexScope(this, I); |
| 3078 | LocalScope<Emitter> BS(this, ScopeKind::FullExpression); |
| 3079 | |
| 3080 | if (!this->visitArrayElemInit(ElemIndex: I, Init: SubExpr, InitT: SubExprT)) |
| 3081 | return false; |
| 3082 | if (!BS.destroyLocals()) |
| 3083 | return false; |
| 3084 | } |
| 3085 | return true; |
| 3086 | } |
| 3087 | |
| 3088 | template <class Emitter> |
| 3089 | bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
| 3090 | const Expr *SourceExpr = E->getSourceExpr(); |
| 3091 | if (!SourceExpr) |
| 3092 | return false; |
| 3093 | |
| 3094 | if (Initializing) { |
| 3095 | assert(!DiscardResult); |
| 3096 | return this->visitInitializer(E: SourceExpr); |
| 3097 | } |
| 3098 | |
| 3099 | PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr); |
| 3100 | if (auto It = OpaqueExprs.find(Val: E); It != OpaqueExprs.end()) { |
| 3101 | if (DiscardResult) |
| 3102 | return true; |
| 3103 | return this->emitGetLocal(SubExprT, It->second, E); |
| 3104 | } |
| 3105 | |
| 3106 | if (!this->visit(E: SourceExpr)) |
| 3107 | return false; |
| 3108 | |
| 3109 | // At this point we either have the evaluated source expression or a pointer |
| 3110 | // to an object on the stack. We want to create a local variable that stores |
| 3111 | // this value. |
| 3112 | unsigned LocalIndex = allocateLocalPrimitive(Decl: E, Ty: SubExprT, /*IsConst=*/true); |
| 3113 | if (!this->emitSetLocal(SubExprT, LocalIndex, E)) |
| 3114 | return false; |
| 3115 | |
| 3116 | // This is cleaned up when the local variable is destroyed. |
| 3117 | OpaqueExprs.insert(KV: {E, LocalIndex}); |
| 3118 | |
| 3119 | // Here the local variable is created but the value is removed from the stack, |
| 3120 | // so we put it back if the caller needs it. |
| 3121 | if (!DiscardResult) |
| 3122 | return this->emitGetLocal(SubExprT, LocalIndex, E); |
| 3123 | return true; |
| 3124 | } |
| 3125 | |
| 3126 | template <class Emitter> |
| 3127 | bool Compiler<Emitter>::VisitAbstractConditionalOperator( |
| 3128 | const AbstractConditionalOperator *E) { |
| 3129 | const Expr *Condition = E->getCond(); |
| 3130 | const Expr *TrueExpr = E->getTrueExpr(); |
| 3131 | const Expr *FalseExpr = E->getFalseExpr(); |
| 3132 | |
| 3133 | if (std::optional<bool> BoolValue = getBoolValue(E: Condition)) { |
| 3134 | if (*BoolValue) |
| 3135 | return this->delegate(E: TrueExpr); |
| 3136 | return this->delegate(E: FalseExpr); |
| 3137 | } |
| 3138 | |
| 3139 | bool IsBcpCall = false; |
| 3140 | if (const auto *CE = dyn_cast<CallExpr>(Val: Condition->IgnoreParenCasts()); |
| 3141 | CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) { |
| 3142 | IsBcpCall = true; |
| 3143 | } |
| 3144 | |
| 3145 | LabelTy LabelEnd = this->getLabel(); // Label after the operator. |
| 3146 | LabelTy LabelFalse = this->getLabel(); // Label for the false expr. |
| 3147 | |
| 3148 | if (IsBcpCall) { |
| 3149 | if (!this->emitPushIgnoreDiags(E)) |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
| 3153 | if (!this->visitBool(E: Condition)) { |
| 3154 | // If the condition failed and we're checking for undefined behavior |
| 3155 | // (which only happens with EvalEmitter) check the TrueExpr and FalseExpr |
| 3156 | // as well. |
| 3157 | if (this->checkingForUndefinedBehavior()) { |
| 3158 | if (!this->discard(E: TrueExpr)) |
| 3159 | return false; |
| 3160 | if (!this->discard(E: FalseExpr)) |
| 3161 | return false; |
| 3162 | } |
| 3163 | return false; |
| 3164 | } |
| 3165 | |
| 3166 | // Force-init the scope, which creates a InitScope op. This is necessary so |
| 3167 | // the scope is not only initialized in one arm of the conditional operator. |
| 3168 | this->VarScope->forceInit(); |
| 3169 | // The TrueExpr and FalseExpr of a conditional operator do _not_ create a |
| 3170 | // scope, which means the local variables created within them unconditionally |
| 3171 | // always exist. However, we need to later differentiate which branch was |
| 3172 | // taken and only destroy the varibles of the active branch. This is what the |
| 3173 | // "enabled" flags on local variables are used for. |
| 3174 | llvm::SaveAndRestore LAAA(this->VarScope->LocalsAlwaysEnabled, |
| 3175 | /*NewValue=*/false); |
| 3176 | |
| 3177 | if (!this->jumpFalse(LabelFalse, E)) |
| 3178 | return false; |
| 3179 | if (!this->delegate(E: TrueExpr)) |
| 3180 | return false; |
| 3181 | |
| 3182 | if (!this->jump(LabelEnd, E)) |
| 3183 | return false; |
| 3184 | this->emitLabel(LabelFalse); |
| 3185 | if (!this->delegate(E: FalseExpr)) |
| 3186 | return false; |
| 3187 | |
| 3188 | this->fallthrough(LabelEnd); |
| 3189 | this->emitLabel(LabelEnd); |
| 3190 | |
| 3191 | if (IsBcpCall) |
| 3192 | return this->emitPopIgnoreDiags(E); |
| 3193 | return true; |
| 3194 | } |
| 3195 | |
| 3196 | template <class Emitter> |
| 3197 | bool Compiler<Emitter>::VisitStringLiteral(const StringLiteral *E) { |
| 3198 | if (DiscardResult) |
| 3199 | return true; |
| 3200 | |
| 3201 | if (!Initializing) |
| 3202 | return this->emitGetStringPtr(E, E); |
| 3203 | |
| 3204 | // We are initializing an array on the stack. |
| 3205 | const ConstantArrayType *CAT = |
| 3206 | Ctx.getASTContext().getAsConstantArrayType(T: E->getType()); |
| 3207 | assert(CAT && "a string literal that's not a constant array?" ); |
| 3208 | |
| 3209 | // If the initializer string is too long, a diagnostic has already been |
| 3210 | // emitted. Read only the array length from the string literal. |
| 3211 | unsigned ArraySize = CAT->getZExtSize(); |
| 3212 | unsigned N = std::min(a: ArraySize, b: E->getLength()); |
| 3213 | unsigned CharWidth = E->getCharByteWidth(); |
| 3214 | |
| 3215 | for (unsigned I = 0; I != N; ++I) { |
| 3216 | uint32_t CodeUnit = E->getCodeUnit(I); |
| 3217 | |
| 3218 | if (CharWidth == 1) { |
| 3219 | this->emitConstSint8(CodeUnit, E); |
| 3220 | this->emitInitElemSint8(I, E); |
| 3221 | } else if (CharWidth == 2) { |
| 3222 | this->emitConstUint16(CodeUnit, E); |
| 3223 | this->emitInitElemUint16(I, E); |
| 3224 | } else if (CharWidth == 4) { |
| 3225 | this->emitConstUint32(CodeUnit, E); |
| 3226 | this->emitInitElemUint32(I, E); |
| 3227 | } else { |
| 3228 | llvm_unreachable("unsupported character width" ); |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | // Fill up the rest of the char array with NUL bytes. |
| 3233 | for (unsigned I = N; I != ArraySize; ++I) { |
| 3234 | if (CharWidth == 1) { |
| 3235 | this->emitConstSint8(0, E); |
| 3236 | this->emitInitElemSint8(I, E); |
| 3237 | } else if (CharWidth == 2) { |
| 3238 | this->emitConstUint16(0, E); |
| 3239 | this->emitInitElemUint16(I, E); |
| 3240 | } else if (CharWidth == 4) { |
| 3241 | this->emitConstUint32(0, E); |
| 3242 | this->emitInitElemUint32(I, E); |
| 3243 | } else { |
| 3244 | llvm_unreachable("unsupported character width" ); |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | return true; |
| 3249 | } |
| 3250 | |
| 3251 | template <class Emitter> |
| 3252 | bool Compiler<Emitter>::VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 3253 | if (DiscardResult) |
| 3254 | return true; |
| 3255 | return this->emitDummyPtr(D: E, E); |
| 3256 | } |
| 3257 | |
| 3258 | template <class Emitter> |
| 3259 | bool Compiler<Emitter>::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
| 3260 | auto &A = Ctx.getASTContext(); |
| 3261 | std::string Str; |
| 3262 | A.getObjCEncodingForType(T: E->getEncodedType(), S&: Str); |
| 3263 | StringLiteral *SL = |
| 3264 | StringLiteral::Create(Ctx: A, Str, Kind: StringLiteralKind::Ordinary, |
| 3265 | /*Pascal=*/false, Ty: E->getType(), Locs: E->getAtLoc()); |
| 3266 | return this->delegate(E: SL); |
| 3267 | } |
| 3268 | |
| 3269 | template <class Emitter> |
| 3270 | bool Compiler<Emitter>::VisitSYCLUniqueStableNameExpr( |
| 3271 | const SYCLUniqueStableNameExpr *E) { |
| 3272 | if (DiscardResult) |
| 3273 | return true; |
| 3274 | |
| 3275 | assert(!Initializing); |
| 3276 | |
| 3277 | auto &A = Ctx.getASTContext(); |
| 3278 | std::string ResultStr = E->ComputeName(Context&: A); |
| 3279 | |
| 3280 | QualType CharTy = A.CharTy.withConst(); |
| 3281 | APInt Size(A.getTypeSize(T: A.getSizeType()), ResultStr.size() + 1); |
| 3282 | QualType ArrayTy = A.getConstantArrayType(EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, |
| 3283 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
| 3284 | |
| 3285 | StringLiteral *SL = |
| 3286 | StringLiteral::Create(Ctx: A, Str: ResultStr, Kind: StringLiteralKind::Ordinary, |
| 3287 | /*Pascal=*/false, Ty: ArrayTy, Locs: E->getLocation()); |
| 3288 | return this->emitGetStringPtr(SL, E); |
| 3289 | } |
| 3290 | |
| 3291 | template <class Emitter> |
| 3292 | bool Compiler<Emitter>::VisitCharacterLiteral(const CharacterLiteral *E) { |
| 3293 | if (DiscardResult) |
| 3294 | return true; |
| 3295 | return this->emitConst(E->getValue(), E); |
| 3296 | } |
| 3297 | |
| 3298 | template <class Emitter> |
| 3299 | bool Compiler<Emitter>::VisitFloatCompoundAssignOperator( |
| 3300 | const CompoundAssignOperator *E) { |
| 3301 | |
| 3302 | const Expr *LHS = E->getLHS(); |
| 3303 | const Expr *RHS = E->getRHS(); |
| 3304 | QualType LHSType = LHS->getType(); |
| 3305 | QualType LHSComputationType = E->getComputationLHSType(); |
| 3306 | QualType ResultType = E->getComputationResultType(); |
| 3307 | OptPrimType LT = classify(LHSComputationType); |
| 3308 | OptPrimType RT = classify(ResultType); |
| 3309 | |
| 3310 | assert(ResultType->isFloatingType()); |
| 3311 | |
| 3312 | if (!LT || !RT) |
| 3313 | return false; |
| 3314 | |
| 3315 | PrimType LHST = classifyPrim(LHSType); |
| 3316 | |
| 3317 | if (isSideEffectFree(E: RHS)) { |
| 3318 | if (!visit(E: LHS)) |
| 3319 | return false; |
| 3320 | if (!this->emitLoad(LHST, E)) |
| 3321 | return false; |
| 3322 | // If necessary, convert LHS to its computation type. |
| 3323 | if (!this->emitPrimCast(FromT: LHST, ToT: classifyPrim(LHSComputationType), |
| 3324 | ToQT: LHSComputationType, E)) |
| 3325 | return false; |
| 3326 | if (!visit(E: RHS)) |
| 3327 | return false; |
| 3328 | |
| 3329 | } else { |
| 3330 | // C++17 onwards require that we evaluate the RHS first. |
| 3331 | // Compute RHS and save it in a temporary variable so we can |
| 3332 | // load it again later. |
| 3333 | if (!visit(E: RHS)) |
| 3334 | return false; |
| 3335 | |
| 3336 | unsigned TempOffset = |
| 3337 | this->allocateLocalPrimitive(Decl: E, Ty: *RT, /*IsConst=*/true); |
| 3338 | if (!this->emitSetLocal(*RT, TempOffset, E)) |
| 3339 | return false; |
| 3340 | |
| 3341 | // First, visit LHS. |
| 3342 | if (!visit(E: LHS)) |
| 3343 | return false; |
| 3344 | if (!this->emitLoad(LHST, E)) |
| 3345 | return false; |
| 3346 | |
| 3347 | // If necessary, convert LHS to its computation type. |
| 3348 | if (!this->emitPrimCast(FromT: LHST, ToT: classifyPrim(LHSComputationType), |
| 3349 | ToQT: LHSComputationType, E)) |
| 3350 | return false; |
| 3351 | |
| 3352 | // Now load RHS. |
| 3353 | if (!this->emitGetLocal(*RT, TempOffset, E)) |
| 3354 | return false; |
| 3355 | } |
| 3356 | |
| 3357 | switch (E->getOpcode()) { |
| 3358 | case BO_AddAssign: |
| 3359 | if (!this->emitAddf(getFPOptions(E), E)) |
| 3360 | return false; |
| 3361 | break; |
| 3362 | case BO_SubAssign: |
| 3363 | if (!this->emitSubf(getFPOptions(E), E)) |
| 3364 | return false; |
| 3365 | break; |
| 3366 | case BO_MulAssign: |
| 3367 | if (!this->emitMulf(getFPOptions(E), E)) |
| 3368 | return false; |
| 3369 | break; |
| 3370 | case BO_DivAssign: |
| 3371 | if (!this->emitDivf(getFPOptions(E), E)) |
| 3372 | return false; |
| 3373 | break; |
| 3374 | default: |
| 3375 | return false; |
| 3376 | } |
| 3377 | |
| 3378 | if (!this->emitPrimCast(FromT: classifyPrim(ResultType), ToT: LHST, ToQT: LHS->getType(), E)) |
| 3379 | return false; |
| 3380 | |
| 3381 | if (DiscardResult) |
| 3382 | return this->emitStorePop(LHST, E); |
| 3383 | return this->emitStore(LHST, E); |
| 3384 | } |
| 3385 | |
| 3386 | template <class Emitter> |
| 3387 | bool Compiler<Emitter>::VisitPointerCompoundAssignOperator( |
| 3388 | const CompoundAssignOperator *E) { |
| 3389 | BinaryOperatorKind Op = E->getOpcode(); |
| 3390 | const Expr *LHS = E->getLHS(); |
| 3391 | const Expr *RHS = E->getRHS(); |
| 3392 | OptPrimType LT = classify(LHS->getType()); |
| 3393 | OptPrimType RT = classify(RHS->getType()); |
| 3394 | |
| 3395 | if (Op != BO_AddAssign && Op != BO_SubAssign) |
| 3396 | return false; |
| 3397 | |
| 3398 | if (!LT || !RT) |
| 3399 | return false; |
| 3400 | |
| 3401 | if (!visit(E: LHS)) |
| 3402 | return false; |
| 3403 | |
| 3404 | if (!this->emitLoad(*LT, LHS)) |
| 3405 | return false; |
| 3406 | |
| 3407 | if (!visit(E: RHS)) |
| 3408 | return false; |
| 3409 | |
| 3410 | if (Op == BO_AddAssign) { |
| 3411 | if (!this->emitAddOffset(*RT, E)) |
| 3412 | return false; |
| 3413 | } else { |
| 3414 | if (!this->emitSubOffset(*RT, E)) |
| 3415 | return false; |
| 3416 | } |
| 3417 | |
| 3418 | if (DiscardResult) |
| 3419 | return this->emitStorePopPtr(E); |
| 3420 | return this->emitStorePtr(E); |
| 3421 | } |
| 3422 | |
| 3423 | template <class Emitter> |
| 3424 | bool Compiler<Emitter>::VisitCompoundAssignOperator( |
| 3425 | const CompoundAssignOperator *E) { |
| 3426 | if (E->getType()->isVectorType()) |
| 3427 | return VisitVectorBinOp(E); |
| 3428 | |
| 3429 | const Expr *LHS = E->getLHS(); |
| 3430 | const Expr *RHS = E->getRHS(); |
| 3431 | OptPrimType LHSComputationT = classify(E->getComputationLHSType()); |
| 3432 | OptPrimType LT = classify(LHS->getType()); |
| 3433 | OptPrimType RT = classify(RHS->getType()); |
| 3434 | OptPrimType ResultT = classify(E->getType()); |
| 3435 | |
| 3436 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 3437 | return this->visit(E: RHS) && this->visit(E: LHS) && this->emitError(E); |
| 3438 | |
| 3439 | if (!LT || !RT || !ResultT || !LHSComputationT) |
| 3440 | return false; |
| 3441 | |
| 3442 | // Handle floating point operations separately here, since they |
| 3443 | // require special care. |
| 3444 | if (ResultT == PT_Float || RT == PT_Float) |
| 3445 | return VisitFloatCompoundAssignOperator(E); |
| 3446 | |
| 3447 | if (E->getType()->isPointerType()) |
| 3448 | return VisitPointerCompoundAssignOperator(E); |
| 3449 | |
| 3450 | assert(!E->getType()->isPointerType() && "Handled above" ); |
| 3451 | assert(!E->getType()->isFloatingType() && "Handled above" ); |
| 3452 | |
| 3453 | if (isSideEffectFree(E: RHS)) { |
| 3454 | if (!visit(E: LHS)) |
| 3455 | return false; |
| 3456 | if (!this->emitLoad(*LT, E)) |
| 3457 | return false; |
| 3458 | if (LT != LHSComputationT && |
| 3459 | !this->emitIntegralCast(FromT: *LT, ToT: *LHSComputationT, |
| 3460 | ToQT: E->getComputationLHSType(), E)) |
| 3461 | return false; |
| 3462 | if (!visit(E: RHS)) |
| 3463 | return false; |
| 3464 | } else { |
| 3465 | // C++17 onwards require that we evaluate the RHS first. |
| 3466 | // Compute RHS and save it in a temporary variable so we can |
| 3467 | // load it again later. |
| 3468 | // FIXME: Compound assignments are unsequenced in C, so we might |
| 3469 | // have to figure out how to reject them. |
| 3470 | if (!visit(E: RHS)) |
| 3471 | return false; |
| 3472 | |
| 3473 | unsigned TempOffset = |
| 3474 | this->allocateLocalPrimitive(Decl: E, Ty: *RT, /*IsConst=*/true); |
| 3475 | |
| 3476 | if (!this->emitSetLocal(*RT, TempOffset, E)) |
| 3477 | return false; |
| 3478 | |
| 3479 | // Get LHS pointer, load its value and cast it to the |
| 3480 | // computation type if necessary. |
| 3481 | if (!visit(E: LHS)) |
| 3482 | return false; |
| 3483 | if (!this->emitLoad(*LT, E)) |
| 3484 | return false; |
| 3485 | if (LT != LHSComputationT && |
| 3486 | !this->emitIntegralCast(FromT: *LT, ToT: *LHSComputationT, |
| 3487 | ToQT: E->getComputationLHSType(), E)) |
| 3488 | return false; |
| 3489 | |
| 3490 | // Get the RHS value on the stack. |
| 3491 | if (!this->emitGetLocal(*RT, TempOffset, E)) |
| 3492 | return false; |
| 3493 | } |
| 3494 | |
| 3495 | // Perform operation. |
| 3496 | switch (E->getOpcode()) { |
| 3497 | case BO_AddAssign: |
| 3498 | if (!this->emitAdd(*LHSComputationT, E)) |
| 3499 | return false; |
| 3500 | break; |
| 3501 | case BO_SubAssign: |
| 3502 | if (!this->emitSub(*LHSComputationT, E)) |
| 3503 | return false; |
| 3504 | break; |
| 3505 | case BO_MulAssign: |
| 3506 | if (!this->emitMul(*LHSComputationT, E)) |
| 3507 | return false; |
| 3508 | break; |
| 3509 | case BO_DivAssign: |
| 3510 | if (!this->emitDiv(*LHSComputationT, E)) |
| 3511 | return false; |
| 3512 | break; |
| 3513 | case BO_RemAssign: |
| 3514 | if (!this->emitRem(*LHSComputationT, E)) |
| 3515 | return false; |
| 3516 | break; |
| 3517 | case BO_ShlAssign: |
| 3518 | if (!this->emitShl(*LHSComputationT, *RT, E)) |
| 3519 | return false; |
| 3520 | break; |
| 3521 | case BO_ShrAssign: |
| 3522 | if (!this->emitShr(*LHSComputationT, *RT, E)) |
| 3523 | return false; |
| 3524 | break; |
| 3525 | case BO_AndAssign: |
| 3526 | if (!this->emitBitAnd(*LHSComputationT, E)) |
| 3527 | return false; |
| 3528 | break; |
| 3529 | case BO_XorAssign: |
| 3530 | if (!this->emitBitXor(*LHSComputationT, E)) |
| 3531 | return false; |
| 3532 | break; |
| 3533 | case BO_OrAssign: |
| 3534 | if (!this->emitBitOr(*LHSComputationT, E)) |
| 3535 | return false; |
| 3536 | break; |
| 3537 | default: |
| 3538 | llvm_unreachable("Unimplemented compound assign operator" ); |
| 3539 | } |
| 3540 | |
| 3541 | // And now cast from LHSComputationT to ResultT. |
| 3542 | if (ResultT != LHSComputationT && |
| 3543 | !this->emitIntegralCast(FromT: *LHSComputationT, ToT: *ResultT, ToQT: E->getType(), E)) |
| 3544 | return false; |
| 3545 | |
| 3546 | // And store the result in LHS. |
| 3547 | if (DiscardResult) { |
| 3548 | if (LHS->refersToBitField()) |
| 3549 | return this->emitStoreBitFieldPop(*ResultT, E); |
| 3550 | return this->emitStorePop(*ResultT, E); |
| 3551 | } |
| 3552 | if (LHS->refersToBitField()) |
| 3553 | return this->emitStoreBitField(*ResultT, E); |
| 3554 | return this->emitStore(*ResultT, E); |
| 3555 | } |
| 3556 | |
| 3557 | template <class Emitter> |
| 3558 | bool Compiler<Emitter>::VisitExprWithCleanups(const ExprWithCleanups *E) { |
| 3559 | LocalScope<Emitter> ES(this, ScopeKind::FullExpression); |
| 3560 | const Expr *SubExpr = E->getSubExpr(); |
| 3561 | |
| 3562 | return this->delegate(E: SubExpr) && ES.destroyLocals(E); |
| 3563 | } |
| 3564 | |
| 3565 | template <class Emitter> |
| 3566 | bool Compiler<Emitter>::VisitMaterializeTemporaryExpr( |
| 3567 | const MaterializeTemporaryExpr *E) { |
| 3568 | if (Initializing) { |
| 3569 | // We already have a value, just initialize that. |
| 3570 | return this->delegate(E: E->getSubExpr()); |
| 3571 | } |
| 3572 | // If we don't end up using the materialized temporary anyway, don't |
| 3573 | // bother creating it. |
| 3574 | if (DiscardResult) |
| 3575 | return this->discard(E: E->getSubExpr()); |
| 3576 | |
| 3577 | SmallVector<const Expr *, 2> CommaLHSs; |
| 3578 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 3579 | const Expr *Inner; |
| 3580 | if (!Ctx.getLangOpts().CPlusPlus11) |
| 3581 | Inner = |
| 3582 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments); |
| 3583 | else |
| 3584 | Inner = E->getSubExpr(); |
| 3585 | |
| 3586 | // If we passed any comma operators, evaluate their LHSs. |
| 3587 | for (const Expr *LHS : CommaLHSs) { |
| 3588 | if (!this->discard(E: LHS)) |
| 3589 | return false; |
| 3590 | } |
| 3591 | |
| 3592 | // FIXME: Find a test case where Adjustments matters. |
| 3593 | |
| 3594 | // When we're extending a global variable *or* the storage duration of |
| 3595 | // the temporary is explicitly static, create a global variable. |
| 3596 | OptPrimType InnerT = classify(Inner); |
| 3597 | const ValueDecl *ExtendingDecl = E->getExtendingDecl(); |
| 3598 | bool IsStatic = E->getStorageDuration() == SD_Static; |
| 3599 | if (IsStatic || |
| 3600 | (ExtendingDecl && Context::shouldBeGloballyIndexed(VD: ExtendingDecl))) { |
| 3601 | |
| 3602 | if (this->constantFolding()) |
| 3603 | return false; |
| 3604 | |
| 3605 | UnsignedOrNone GlobalIndex = P.createGlobal(E, ExprType: Inner->getType()); |
| 3606 | if (!GlobalIndex) |
| 3607 | return false; |
| 3608 | |
| 3609 | const LifetimeExtendedTemporaryDecl *TempDecl = |
| 3610 | E->getLifetimeExtendedTemporaryDecl(); |
| 3611 | |
| 3612 | if (InnerT) { |
| 3613 | if (!this->visit(E: Inner)) |
| 3614 | return false; |
| 3615 | |
| 3616 | if (IsStatic) { |
| 3617 | assert(TempDecl); |
| 3618 | if (!this->emitInitGlobalTemp(*InnerT, *GlobalIndex, TempDecl, E)) |
| 3619 | return false; |
| 3620 | } else { |
| 3621 | if (!this->emitInitGlobal(*InnerT, *GlobalIndex, E)) |
| 3622 | return false; |
| 3623 | } |
| 3624 | return this->emitGetPtrGlobal(*GlobalIndex, E); |
| 3625 | } |
| 3626 | |
| 3627 | if (!this->checkLiteralType(E: Inner)) |
| 3628 | return false; |
| 3629 | // Non-primitive values. |
| 3630 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 3631 | return false; |
| 3632 | if (!this->visitInitializer(E: Inner)) |
| 3633 | return false; |
| 3634 | if (IsStatic) { |
| 3635 | assert(TempDecl); |
| 3636 | return this->emitInitGlobalTempComp(TempDecl, E); |
| 3637 | } |
| 3638 | return true; |
| 3639 | } |
| 3640 | |
| 3641 | ScopeKind VarScope = E->getStorageDuration() == SD_FullExpression |
| 3642 | ? ScopeKind::FullExpression |
| 3643 | : ScopeKind::Block; |
| 3644 | |
| 3645 | // For everyhing else, use local variables. |
| 3646 | if (InnerT) { |
| 3647 | bool IsConst = Inner->getType().isConstQualified(); |
| 3648 | bool IsVolatile = Inner->getType().isVolatileQualified(); |
| 3649 | unsigned LocalIndex = |
| 3650 | allocateLocalPrimitive(Decl: E, Ty: *InnerT, IsConst, IsVolatile, SC: VarScope); |
| 3651 | if (!this->VarScope->LocalsAlwaysEnabled && |
| 3652 | !this->emitEnableLocal(LocalIndex, E)) |
| 3653 | return false; |
| 3654 | |
| 3655 | if (!this->visit(E: Inner)) |
| 3656 | return false; |
| 3657 | if (!this->emitSetLocal(*InnerT, LocalIndex, E)) |
| 3658 | return false; |
| 3659 | |
| 3660 | return this->emitGetPtrLocal(LocalIndex, E); |
| 3661 | } |
| 3662 | |
| 3663 | if (!this->checkLiteralType(E: Inner)) |
| 3664 | return false; |
| 3665 | |
| 3666 | if (UnsignedOrNone LocalIndex = |
| 3667 | allocateLocal(Decl: E, Ty: Inner->getType(), VarScope)) { |
| 3668 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex)); |
| 3669 | |
| 3670 | if (!this->VarScope->LocalsAlwaysEnabled && |
| 3671 | !this->emitEnableLocal(*LocalIndex, E)) |
| 3672 | return false; |
| 3673 | |
| 3674 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 3675 | return false; |
| 3676 | return this->visitInitializer(E: Inner); |
| 3677 | } |
| 3678 | return false; |
| 3679 | } |
| 3680 | |
| 3681 | template <class Emitter> |
| 3682 | bool Compiler<Emitter>::VisitCXXBindTemporaryExpr( |
| 3683 | const CXXBindTemporaryExpr *E) { |
| 3684 | const Expr *SubExpr = E->getSubExpr(); |
| 3685 | |
| 3686 | if (Initializing) |
| 3687 | return this->delegate(E: SubExpr); |
| 3688 | |
| 3689 | // Make sure we create a temporary even if we're discarding, since that will |
| 3690 | // make sure we will also call the destructor. |
| 3691 | |
| 3692 | if (!this->visit(E: SubExpr)) |
| 3693 | return false; |
| 3694 | |
| 3695 | if (DiscardResult) |
| 3696 | return this->emitPopPtr(E); |
| 3697 | return true; |
| 3698 | } |
| 3699 | |
| 3700 | template <class Emitter> |
| 3701 | bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 3702 | const Expr *Init = E->getInitializer(); |
| 3703 | if (DiscardResult) |
| 3704 | return this->discard(E: Init); |
| 3705 | |
| 3706 | if (Initializing) { |
| 3707 | // We already have a value, just initialize that. |
| 3708 | return this->visitInitializer(E: Init); |
| 3709 | } |
| 3710 | |
| 3711 | OptPrimType T = classify(E->getType()); |
| 3712 | if (E->isFileScope()) { |
| 3713 | // Avoid creating a variable if this is a primitive RValue anyway. |
| 3714 | if (T && !E->isLValue()) |
| 3715 | return this->delegate(E: Init); |
| 3716 | |
| 3717 | UnsignedOrNone GlobalIndex = P.createGlobal(E, ExprType: E->getType()); |
| 3718 | if (!GlobalIndex) |
| 3719 | return false; |
| 3720 | |
| 3721 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 3722 | return false; |
| 3723 | |
| 3724 | // Since this is a global variable, we might've already seen, |
| 3725 | // don't do it again. |
| 3726 | if (P.isGlobalInitialized(Index: *GlobalIndex)) |
| 3727 | return true; |
| 3728 | |
| 3729 | if (T) { |
| 3730 | if (!this->visit(E: Init)) |
| 3731 | return false; |
| 3732 | return this->emitInitGlobal(*T, *GlobalIndex, E); |
| 3733 | } |
| 3734 | |
| 3735 | return this->visitInitializer(E: Init); |
| 3736 | } |
| 3737 | |
| 3738 | // Otherwise, use a local variable. |
| 3739 | if (T && !E->isLValue()) { |
| 3740 | // For primitive types, we just visit the initializer. |
| 3741 | return this->delegate(E: Init); |
| 3742 | } |
| 3743 | |
| 3744 | unsigned LocalIndex; |
| 3745 | if (T) |
| 3746 | LocalIndex = this->allocateLocalPrimitive(Decl: Init, Ty: *T, /*IsConst=*/false); |
| 3747 | else if (UnsignedOrNone MaybeIndex = this->allocateLocal(Decl: Init)) |
| 3748 | LocalIndex = *MaybeIndex; |
| 3749 | else |
| 3750 | return false; |
| 3751 | |
| 3752 | if (!this->emitGetPtrLocal(LocalIndex, E)) |
| 3753 | return false; |
| 3754 | |
| 3755 | if (T) |
| 3756 | return this->visit(E: Init) && this->emitInit(*T, E); |
| 3757 | return this->visitInitializer(E: Init); |
| 3758 | } |
| 3759 | |
| 3760 | template <class Emitter> |
| 3761 | bool Compiler<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 3762 | if (DiscardResult) |
| 3763 | return true; |
| 3764 | if (E->isStoredAsBoolean()) { |
| 3765 | if (E->getType()->isBooleanType()) |
| 3766 | return this->emitConstBool(E->getBoolValue(), E); |
| 3767 | return this->emitConst(E->getBoolValue(), E); |
| 3768 | } |
| 3769 | if (E->isStoredAsComparisonResult()) { |
| 3770 | const ComparisonCategoryInfo &CmpInfo = |
| 3771 | Ctx.getASTContext().CompCategories.getInfoForType(Ty: E->getType()); |
| 3772 | const auto Result = |
| 3773 | ComparisonCategoryResult(E->getAPValue().getInt().getZExtValue()); |
| 3774 | const Record *R = getRecord(E->getType()); |
| 3775 | if (!R || R->getNumFields() == 0) |
| 3776 | return false; |
| 3777 | const Record::Field *Field = R->getField(I: 0U); |
| 3778 | assert(Field->T); |
| 3779 | if (!this->emitConst(CmpInfo.getValueInfo(ValueKind: Result)->getIntValue(), *Field->T, |
| 3780 | E)) |
| 3781 | return false; |
| 3782 | return this->emitInitField(*Field->T, Field->Offset, E); |
| 3783 | } |
| 3784 | |
| 3785 | PrimType T = classifyPrim(E->getType()); |
| 3786 | return this->visitAPValue(Val: E->getAPValue(), ValType: T, Info: E); |
| 3787 | } |
| 3788 | |
| 3789 | template <class Emitter> |
| 3790 | bool Compiler<Emitter>::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 3791 | if (DiscardResult) |
| 3792 | return true; |
| 3793 | return this->emitConst(E->getValue(), E); |
| 3794 | } |
| 3795 | |
| 3796 | template <class Emitter> |
| 3797 | bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) { |
| 3798 | if (DiscardResult) |
| 3799 | return true; |
| 3800 | |
| 3801 | assert(Initializing); |
| 3802 | const Record *R = P.getOrCreateRecord(RD: E->getLambdaClass()); |
| 3803 | if (!R) |
| 3804 | return false; |
| 3805 | |
| 3806 | auto *CaptureInitIt = E->capture_init_begin(); |
| 3807 | // Initialize all fields (which represent lambda captures) of the |
| 3808 | // record with their initializers. |
| 3809 | for (const Record::Field &F : R->fields()) { |
| 3810 | const Expr *Init = *CaptureInitIt; |
| 3811 | if (!Init || Init->containsErrors()) |
| 3812 | continue; |
| 3813 | ++CaptureInitIt; |
| 3814 | |
| 3815 | if (OptPrimType T = classify(Init)) { |
| 3816 | if (!this->visit(E: Init)) |
| 3817 | return false; |
| 3818 | |
| 3819 | if (!this->emitInitField(*T, F.Offset, E)) |
| 3820 | return false; |
| 3821 | } else { |
| 3822 | if (!this->emitGetPtrField(F.Offset, E)) |
| 3823 | return false; |
| 3824 | |
| 3825 | if (!this->visitInitializerPop(E: Init)) |
| 3826 | return false; |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | return true; |
| 3831 | } |
| 3832 | |
| 3833 | template <class Emitter> |
| 3834 | bool Compiler<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) { |
| 3835 | if (DiscardResult) |
| 3836 | return true; |
| 3837 | |
| 3838 | if (!Initializing) |
| 3839 | return this->emitGetStringPtr(E, E); |
| 3840 | return this->delegate(E: E->getFunctionName()); |
| 3841 | } |
| 3842 | |
| 3843 | template <class Emitter> |
| 3844 | bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) { |
| 3845 | if (E->getSubExpr() && !this->discard(E: E->getSubExpr())) |
| 3846 | return false; |
| 3847 | |
| 3848 | return this->emitInvalid(E); |
| 3849 | } |
| 3850 | |
| 3851 | template <class Emitter> |
| 3852 | bool Compiler<Emitter>::VisitCXXReinterpretCastExpr( |
| 3853 | const CXXReinterpretCastExpr *E) { |
| 3854 | const Expr *SubExpr = E->getSubExpr(); |
| 3855 | |
| 3856 | OptPrimType FromT = classify(SubExpr); |
| 3857 | OptPrimType ToT = classify(E); |
| 3858 | |
| 3859 | if (!FromT || !ToT) |
| 3860 | return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E); |
| 3861 | |
| 3862 | if (FromT == PT_Ptr || ToT == PT_Ptr) { |
| 3863 | auto CastKind = isIntegerType(T: *ToT) ? CastKind::ReinterpretPtrToInt |
| 3864 | : CastKind::Reinterpret; |
| 3865 | if (!this->emitInvalidCast(CastKind, /*Fatal=*/false, E)) |
| 3866 | return false; |
| 3867 | if (E->getCastKind() == CK_LValueBitCast) |
| 3868 | return this->delegate(E: SubExpr); |
| 3869 | return this->VisitCastExpr(E); |
| 3870 | } |
| 3871 | |
| 3872 | // Try to actually do the cast. |
| 3873 | bool Fatal = (ToT != FromT); |
| 3874 | if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E)) |
| 3875 | return false; |
| 3876 | |
| 3877 | return this->VisitCastExpr(E); |
| 3878 | } |
| 3879 | |
| 3880 | template <class Emitter> |
| 3881 | bool Compiler<Emitter>::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 3882 | if (!Ctx.getLangOpts().CPlusPlus20) { |
| 3883 | if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E)) |
| 3884 | return false; |
| 3885 | } |
| 3886 | |
| 3887 | if (E->getCastKind() != CK_Dynamic) |
| 3888 | return this->VisitCastExpr(E); |
| 3889 | |
| 3890 | QualType DestType = E->getType(); |
| 3891 | // "target type must be a reference or pointer type to a defined class" |
| 3892 | if (DestType->isRecordType()) { |
| 3893 | assert(E->isGLValue()); |
| 3894 | } else { |
| 3895 | assert(DestType->isPointerOrReferenceType()); |
| 3896 | assert(DestType->isVoidPointerType() || |
| 3897 | DestType->getPointeeType()->isRecordType()); |
| 3898 | DestType = DestType->getPointeeType(); |
| 3899 | } |
| 3900 | |
| 3901 | if (!this->visit(E: E->getSubExpr())) |
| 3902 | return false; |
| 3903 | if (!this->emitDynamicCast(DestType.getTypePtr(), |
| 3904 | /*IsReferenceCast=*/E->isGLValue(), E)) |
| 3905 | return false; |
| 3906 | |
| 3907 | if (DiscardResult) |
| 3908 | return this->emitPopPtr(E); |
| 3909 | return true; |
| 3910 | } |
| 3911 | |
| 3912 | template <class Emitter> |
| 3913 | bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 3914 | assert(E->getType()->isBooleanType()); |
| 3915 | |
| 3916 | if (DiscardResult) |
| 3917 | return true; |
| 3918 | return this->emitConstBool(E->getValue(), E); |
| 3919 | } |
| 3920 | |
| 3921 | template <class Emitter> |
| 3922 | bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3923 | QualType T = E->getType(); |
| 3924 | assert(!canClassify(T)); |
| 3925 | |
| 3926 | if (T->isRecordType()) { |
| 3927 | const CXXConstructorDecl *Ctor = E->getConstructor(); |
| 3928 | |
| 3929 | // If we're discarding a construct expression, we still need |
| 3930 | // to allocate a variable and call the constructor and destructor. |
| 3931 | if (DiscardResult) { |
| 3932 | if (Ctor->isTrivial()) |
| 3933 | return true; |
| 3934 | assert(!Initializing); |
| 3935 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 3936 | |
| 3937 | if (!LocalIndex) |
| 3938 | return false; |
| 3939 | |
| 3940 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 3941 | return false; |
| 3942 | } |
| 3943 | |
| 3944 | // Trivial copy/move constructor. Avoid copy. |
| 3945 | if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() && |
| 3946 | Ctor->isTrivial() && |
| 3947 | E->getArg(Arg: 0)->isTemporaryObject(Ctx&: Ctx.getASTContext(), |
| 3948 | TempTy: T->getAsCXXRecordDecl())) |
| 3949 | return this->visitInitializer(E: E->getArg(Arg: 0)); |
| 3950 | |
| 3951 | // Zero initialization. |
| 3952 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3953 | if (ZeroInit) { |
| 3954 | const Record *R = getRecord(E->getType()); |
| 3955 | if (!R) |
| 3956 | return false; |
| 3957 | |
| 3958 | if (!this->visitZeroRecordInitializer(R, E)) |
| 3959 | return false; |
| 3960 | |
| 3961 | // If the constructor is trivial anyway, we're done. |
| 3962 | if (Ctor->isTrivial()) |
| 3963 | return true; |
| 3964 | } |
| 3965 | |
| 3966 | // Trivial default constructors might never be implicitly defined by the |
| 3967 | // AST, so we need to special-case them here. |
| 3968 | if (Ctor->isTrivial() && Ctor->isDefaultConstructor()) { |
| 3969 | if (!this->emitDefaultInit(Ctor, E)) |
| 3970 | return false; |
| 3971 | if (DiscardResult) |
| 3972 | return this->emitPopPtr(E); |
| 3973 | return true; |
| 3974 | } |
| 3975 | |
| 3976 | // Avoid materializing a temporary for an elidable copy/move constructor. |
| 3977 | if (!ZeroInit && E->isElidable()) { |
| 3978 | const Expr *SrcObj = E->getArg(Arg: 0); |
| 3979 | assert(SrcObj->isTemporaryObject(Ctx.getASTContext(), Ctor->getParent())); |
| 3980 | assert(Ctx.getASTContext().hasSameUnqualifiedType(E->getType(), |
| 3981 | SrcObj->getType())); |
| 3982 | if (const auto *ME = dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj)) { |
| 3983 | if (!this->emitCheckFunctionDecl(Ctor, E)) |
| 3984 | return false; |
| 3985 | return this->visitInitializer(E: ME->getSubExpr()); |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | const Function *Func = getFunction(FD: Ctor); |
| 3990 | |
| 3991 | if (!Func) |
| 3992 | return false; |
| 3993 | |
| 3994 | assert(Func->hasThisPointer()); |
| 3995 | assert(!Func->hasRVO()); |
| 3996 | |
| 3997 | // The This pointer is already on the stack because this is an initializer, |
| 3998 | // but we need to dup() so the call() below has its own copy. |
| 3999 | if (!this->emitDupPtr(E)) |
| 4000 | return false; |
| 4001 | |
| 4002 | // Constructor arguments. |
| 4003 | for (const auto *Arg : E->arguments()) { |
| 4004 | if (!this->visit(E: Arg)) |
| 4005 | return false; |
| 4006 | } |
| 4007 | |
| 4008 | if (Func->isVariadic()) { |
| 4009 | uint32_t VarArgSize = 0; |
| 4010 | unsigned NumParams = Func->getNumWrittenParams(); |
| 4011 | for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) { |
| 4012 | VarArgSize += |
| 4013 | align(primSize(classify(E->getArg(Arg: I)->getType()).value_or(PT_Ptr))); |
| 4014 | } |
| 4015 | if (!this->emitCallVar(Func, VarArgSize, E)) |
| 4016 | return false; |
| 4017 | } else { |
| 4018 | if (!this->emitCall(Func, 0, E)) { |
| 4019 | // When discarding, we don't need the result anyway, so clean up |
| 4020 | // the instance dup we did earlier in case surrounding code wants |
| 4021 | // to keep evaluating. |
| 4022 | if (DiscardResult) |
| 4023 | (void)this->emitPopPtr(E); |
| 4024 | return false; |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | if (DiscardResult) |
| 4029 | return this->emitPopPtr(E); |
| 4030 | return true; |
| 4031 | } |
| 4032 | |
| 4033 | if (T->isArrayType()) { |
| 4034 | const Function *Func = getFunction(FD: E->getConstructor()); |
| 4035 | if (!Func) |
| 4036 | return false; |
| 4037 | |
| 4038 | if (!this->emitDupPtr(E)) |
| 4039 | return false; |
| 4040 | |
| 4041 | std::function<bool(QualType)> initArrayDimension; |
| 4042 | initArrayDimension = [&](QualType T) -> bool { |
| 4043 | if (!T->isArrayType()) { |
| 4044 | // Constructor arguments. |
| 4045 | for (const auto *Arg : E->arguments()) { |
| 4046 | if (!this->visit(E: Arg)) |
| 4047 | return false; |
| 4048 | } |
| 4049 | |
| 4050 | return this->emitCall(Func, 0, E); |
| 4051 | } |
| 4052 | |
| 4053 | const ConstantArrayType *CAT = |
| 4054 | Ctx.getASTContext().getAsConstantArrayType(T); |
| 4055 | if (!CAT) |
| 4056 | return false; |
| 4057 | QualType ElemTy = CAT->getElementType(); |
| 4058 | uint64_t NumElems = CAT->getZExtSize(); |
| 4059 | if (exceedsArraySizeLimit(LangOpts: Ctx.getLangOpts(), NumElems)) |
| 4060 | return this->emitCheckArraySize(NumElems, E); |
| 4061 | for (uint64_t I = 0; I != NumElems; ++I) { |
| 4062 | if (!this->emitConstUint64(I, E)) |
| 4063 | return false; |
| 4064 | if (!this->emitArrayElemPtrUint64(E)) |
| 4065 | return false; |
| 4066 | if (!initArrayDimension(ElemTy)) |
| 4067 | return false; |
| 4068 | } |
| 4069 | return this->emitPopPtr(E); |
| 4070 | }; |
| 4071 | |
| 4072 | return initArrayDimension(E->getType()); |
| 4073 | } |
| 4074 | |
| 4075 | return false; |
| 4076 | } |
| 4077 | |
| 4078 | template <class Emitter> |
| 4079 | bool Compiler<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) { |
| 4080 | if (DiscardResult) |
| 4081 | return true; |
| 4082 | |
| 4083 | const APValue Val = |
| 4084 | E->EvaluateInContext(Ctx: Ctx.getASTContext(), DefaultExpr: SourceLocDefaultExpr); |
| 4085 | |
| 4086 | // Things like __builtin_LINE(). |
| 4087 | if (E->getType()->isIntegerType()) { |
| 4088 | assert(Val.isInt()); |
| 4089 | const APSInt &I = Val.getInt(); |
| 4090 | return this->emitConst(I, E); |
| 4091 | } |
| 4092 | // Otherwise, the APValue is an LValue, with only one element. |
| 4093 | // Theoretically, we don't need the APValue at all of course. |
| 4094 | assert(E->getType()->isPointerType()); |
| 4095 | assert(Val.isLValue()); |
| 4096 | const APValue::LValueBase &Base = Val.getLValueBase(); |
| 4097 | if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>()) |
| 4098 | return this->visit(E: LValueExpr); |
| 4099 | |
| 4100 | // Otherwise, we have a decl (which is the case for |
| 4101 | // __builtin_source_location). |
| 4102 | assert(Base.is<const ValueDecl *>()); |
| 4103 | assert(Val.getLValuePath().size() == 0); |
| 4104 | const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>(); |
| 4105 | assert(BaseDecl); |
| 4106 | |
| 4107 | auto *UGCD = cast<UnnamedGlobalConstantDecl>(Val: BaseDecl); |
| 4108 | |
| 4109 | UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(VD: UGCD); |
| 4110 | if (!GlobalIndex) |
| 4111 | return false; |
| 4112 | |
| 4113 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 4114 | return false; |
| 4115 | |
| 4116 | const Record *R = getRecord(E->getType()); |
| 4117 | const APValue &V = UGCD->getValue(); |
| 4118 | for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) { |
| 4119 | const Record::Field *F = R->getField(I); |
| 4120 | const APValue &FieldValue = V.getStructField(i: I); |
| 4121 | |
| 4122 | if (!this->visitAPValue(Val: FieldValue, ValType: *F->T, Info: E)) |
| 4123 | return false; |
| 4124 | if (!this->emitInitField(*F->T, F->Offset, E)) |
| 4125 | return false; |
| 4126 | } |
| 4127 | |
| 4128 | // Leave the pointer to the global on the stack. |
| 4129 | return true; |
| 4130 | } |
| 4131 | |
| 4132 | template <class Emitter> |
| 4133 | bool Compiler<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
| 4134 | unsigned N = E->getNumComponents(); |
| 4135 | if (N == 0) |
| 4136 | return false; |
| 4137 | |
| 4138 | for (unsigned I = 0; I != N; ++I) { |
| 4139 | const OffsetOfNode &Node = E->getComponent(Idx: I); |
| 4140 | if (Node.getKind() == OffsetOfNode::Array) { |
| 4141 | const Expr *ArrayIndexExpr = E->getIndexExpr(Idx: Node.getArrayExprIndex()); |
| 4142 | PrimType IndexT = classifyPrim(ArrayIndexExpr->getType()); |
| 4143 | |
| 4144 | if (DiscardResult) { |
| 4145 | if (!this->discard(E: ArrayIndexExpr)) |
| 4146 | return false; |
| 4147 | continue; |
| 4148 | } |
| 4149 | |
| 4150 | if (IndexT == PT_IntAP || IndexT == PT_IntAPS) { |
| 4151 | if (!this->visit(E: ArrayIndexExpr)) |
| 4152 | return false; |
| 4153 | if (!this->emitCastAPToOffsetIndex(IndexT, E)) |
| 4154 | return false; |
| 4155 | continue; |
| 4156 | } |
| 4157 | if (!this->visit(E: ArrayIndexExpr)) |
| 4158 | return false; |
| 4159 | // Cast to Sint64. |
| 4160 | if (IndexT != PT_Sint64) { |
| 4161 | if (!this->emitCast(IndexT, PT_Sint64, E)) |
| 4162 | return false; |
| 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | if (DiscardResult) |
| 4168 | return true; |
| 4169 | |
| 4170 | PrimType T = classifyPrim(E->getType()); |
| 4171 | return this->emitOffsetOf(T, E, E); |
| 4172 | } |
| 4173 | |
| 4174 | template <class Emitter> |
| 4175 | bool Compiler<Emitter>::VisitCXXScalarValueInitExpr( |
| 4176 | const CXXScalarValueInitExpr *E) { |
| 4177 | QualType Ty = E->getType(); |
| 4178 | |
| 4179 | if (DiscardResult || Ty->isVoidType()) |
| 4180 | return true; |
| 4181 | |
| 4182 | if (OptPrimType T = classify(Ty)) |
| 4183 | return this->visitZeroInitializer(T: *T, QT: Ty, E); |
| 4184 | |
| 4185 | if (Ty->isAnyComplexType() || Ty->isVectorType()) { |
| 4186 | if (!Initializing) { |
| 4187 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 4188 | if (!LocalIndex) |
| 4189 | return false; |
| 4190 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4191 | return false; |
| 4192 | } |
| 4193 | |
| 4194 | QualType ElemQT; |
| 4195 | unsigned NumElems; |
| 4196 | if (const auto *CT = Ty->getAs<ComplexType>()) { |
| 4197 | NumElems = 2; |
| 4198 | ElemQT = CT->getElementType(); |
| 4199 | } else { |
| 4200 | const auto *VT = Ty->castAs<VectorType>(); |
| 4201 | NumElems = VT->getNumElements(); |
| 4202 | ElemQT = VT->getElementType(); |
| 4203 | } |
| 4204 | |
| 4205 | PrimType ElemT = classifyPrim(ElemQT); |
| 4206 | |
| 4207 | // Initialize all fields to 0. |
| 4208 | for (unsigned I = 0; I != NumElems; ++I) { |
| 4209 | if (!this->visitZeroInitializer(T: ElemT, QT: ElemQT, E)) |
| 4210 | return false; |
| 4211 | if (!this->emitInitElem(ElemT, I, E)) |
| 4212 | return false; |
| 4213 | } |
| 4214 | return true; |
| 4215 | } |
| 4216 | |
| 4217 | return false; |
| 4218 | } |
| 4219 | |
| 4220 | template <class Emitter> |
| 4221 | bool Compiler<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 4222 | return this->emitConst(E->getPackLength(), E); |
| 4223 | } |
| 4224 | |
| 4225 | template <class Emitter> |
| 4226 | bool Compiler<Emitter>::VisitGenericSelectionExpr( |
| 4227 | const GenericSelectionExpr *E) { |
| 4228 | return this->delegate(E: E->getResultExpr()); |
| 4229 | } |
| 4230 | |
| 4231 | template <class Emitter> |
| 4232 | bool Compiler<Emitter>::VisitChooseExpr(const ChooseExpr *E) { |
| 4233 | return this->delegate(E: E->getChosenSubExpr()); |
| 4234 | } |
| 4235 | |
| 4236 | template <class Emitter> |
| 4237 | bool Compiler<Emitter>::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 4238 | if (DiscardResult) |
| 4239 | return true; |
| 4240 | |
| 4241 | return this->emitConst(E->getValue(), E); |
| 4242 | } |
| 4243 | |
| 4244 | template <class Emitter> |
| 4245 | bool Compiler<Emitter>::VisitCXXInheritedCtorInitExpr( |
| 4246 | const CXXInheritedCtorInitExpr *E) { |
| 4247 | const CXXConstructorDecl *Ctor = E->getConstructor(); |
| 4248 | |
| 4249 | if (Ctor->isTrivial()) |
| 4250 | return true; |
| 4251 | |
| 4252 | const Function *F = this->getFunction(FD: Ctor); |
| 4253 | if (!F) |
| 4254 | return false; |
| 4255 | assert(!F->hasRVO()); |
| 4256 | assert(F->hasThisPointer()); |
| 4257 | |
| 4258 | if (!this->emitDupPtr(SourceInfo{})) |
| 4259 | return false; |
| 4260 | |
| 4261 | // Forward all arguments of the current function (which should be a |
| 4262 | // constructor itself) to the inherited ctor. |
| 4263 | // This is necessary because the calling code has pushed the pointer |
| 4264 | // of the correct base for us already, but the arguments need |
| 4265 | // to come after. |
| 4266 | unsigned ParamIndex = 0; |
| 4267 | for (const ParmVarDecl *PD : Ctor->parameters()) { |
| 4268 | PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr); |
| 4269 | |
| 4270 | if (!this->emitGetParam(PT, ParamIndex, E)) |
| 4271 | return false; |
| 4272 | ++ParamIndex; |
| 4273 | } |
| 4274 | |
| 4275 | return this->emitCall(F, 0, E); |
| 4276 | } |
| 4277 | |
| 4278 | // FIXME: This function has become rather unwieldy, especially |
| 4279 | // the part where we initialize an array allocation of dynamic size. |
| 4280 | template <class Emitter> |
| 4281 | bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) { |
| 4282 | assert(classifyPrim(E->getType()) == PT_Ptr); |
| 4283 | const Expr *Init = E->getInitializer(); |
| 4284 | QualType ElementType = E->getAllocatedType(); |
| 4285 | OptPrimType ElemT = classify(ElementType); |
| 4286 | unsigned PlacementArgs = E->getNumPlacementArgs(); |
| 4287 | const FunctionDecl *OperatorNew = E->getOperatorNew(); |
| 4288 | const Expr *PlacementDest = nullptr; |
| 4289 | bool IsNoThrow = false; |
| 4290 | |
| 4291 | if (E->containsErrors()) |
| 4292 | return false; |
| 4293 | |
| 4294 | if (PlacementArgs != 0) { |
| 4295 | // FIXME: There is no restriction on this, but it's not clear that any |
| 4296 | // other form makes any sense. We get here for cases such as: |
| 4297 | // |
| 4298 | // new (std::align_val_t{N}) X(int) |
| 4299 | // |
| 4300 | // (which should presumably be valid only if N is a multiple of |
| 4301 | // alignof(int), and in any case can't be deallocated unless N is |
| 4302 | // alignof(X) and X has new-extended alignment). |
| 4303 | if (PlacementArgs == 1) { |
| 4304 | const Expr *Arg1 = E->getPlacementArg(I: 0); |
| 4305 | if (OperatorNew->isReservedGlobalPlacementOperator()) { |
| 4306 | if (!this->emitCheckPlacementNew(E, E)) |
| 4307 | return false; |
| 4308 | PlacementDest = Arg1; |
| 4309 | } else if ( |
| 4310 | Arg1->getType()->isNothrowT() && |
| 4311 | OperatorNew |
| 4312 | ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
| 4313 | if (!this->discard(E: Arg1)) |
| 4314 | return false; |
| 4315 | IsNoThrow = true; |
| 4316 | } else { |
| 4317 | // Any other placement list is invalid. This includes a user-declared |
| 4318 | // allocation function taking std::nothrow_t, e.g. by value. |
| 4319 | return this->emitInvalidNewDeleteExpr(E, E); |
| 4320 | } |
| 4321 | } else { |
| 4322 | // Always invalid. |
| 4323 | return this->emitInvalid(E); |
| 4324 | } |
| 4325 | } else if (!OperatorNew |
| 4326 | ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) |
| 4327 | return this->emitInvalidNewDeleteExpr(E, E); |
| 4328 | |
| 4329 | const Descriptor *Desc; |
| 4330 | if (!PlacementDest) { |
| 4331 | if (ElemT) { |
| 4332 | if (E->isArray()) |
| 4333 | Desc = nullptr; // We're not going to use it in this case. |
| 4334 | else |
| 4335 | Desc = P.createDescriptor(D: E, T: *ElemT); |
| 4336 | } else { |
| 4337 | Desc = P.createDescriptor(D: E, Ty: ElementType.getTypePtr(), /*IsConst=*/false, |
| 4338 | /*IsTemporary=*/false, /*IsMutable=*/false, |
| 4339 | /*IsVolatile=*/false, Init); |
| 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | if (E->isArray()) { |
| 4344 | std::optional<const Expr *> ArraySizeExpr = E->getArraySize(); |
| 4345 | if (!ArraySizeExpr) |
| 4346 | return false; |
| 4347 | |
| 4348 | const Expr *Stripped = *ArraySizeExpr; |
| 4349 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped); |
| 4350 | Stripped = ICE->getSubExpr()) |
| 4351 | if (ICE->getCastKind() != CK_NoOp && |
| 4352 | ICE->getCastKind() != CK_IntegralCast) |
| 4353 | break; |
| 4354 | |
| 4355 | PrimType SizeT = classifyPrim(Stripped->getType()); |
| 4356 | |
| 4357 | // Save evaluated array size to a variable. |
| 4358 | unsigned ArrayLen = |
| 4359 | allocateLocalPrimitive(Decl: Stripped, Ty: SizeT, /*IsConst=*/false); |
| 4360 | if (!this->visit(E: Stripped)) |
| 4361 | return false; |
| 4362 | if (!this->emitSetLocal(SizeT, ArrayLen, E)) |
| 4363 | return false; |
| 4364 | |
| 4365 | if (PlacementDest) { |
| 4366 | if (!this->visit(E: PlacementDest)) |
| 4367 | return false; |
| 4368 | if (!this->emitGetLocal(SizeT, ArrayLen, E)) |
| 4369 | return false; |
| 4370 | if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E)) |
| 4371 | return false; |
| 4372 | } else { |
| 4373 | if (!this->emitGetLocal(SizeT, ArrayLen, E)) |
| 4374 | return false; |
| 4375 | |
| 4376 | if (ElemT) { |
| 4377 | // N primitive elements. |
| 4378 | if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E)) |
| 4379 | return false; |
| 4380 | } else { |
| 4381 | // N Composite elements. |
| 4382 | if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E)) |
| 4383 | return false; |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | if (Init) { |
| 4388 | QualType InitType = Init->getType(); |
| 4389 | size_t StaticInitElems = 0; |
| 4390 | const Expr *DynamicInit = nullptr; |
| 4391 | OptPrimType ElemT; |
| 4392 | |
| 4393 | if (const ConstantArrayType *CAT = |
| 4394 | Ctx.getASTContext().getAsConstantArrayType(T: InitType)) { |
| 4395 | StaticInitElems = CAT->getZExtSize(); |
| 4396 | // Initialize the first S element from the initializer. |
| 4397 | if (!this->visitInitializer(E: Init)) |
| 4398 | return false; |
| 4399 | |
| 4400 | if (const auto *ILE = dyn_cast<InitListExpr>(Val: Init)) { |
| 4401 | if (ILE->hasArrayFiller()) |
| 4402 | DynamicInit = ILE->getArrayFiller(); |
| 4403 | else if (StaticInitElems > 0 && isa<StringLiteral>(Val: ILE->getInit(Init: 0))) |
| 4404 | ElemT = classifyPrim(CAT->getElementType()); |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | // The initializer initializes a certain number of elements, S. |
| 4409 | // However, the complete number of elements, N, might be larger than that. |
| 4410 | // In this case, we need to get an initializer for the remaining elements. |
| 4411 | // There are three cases: |
| 4412 | // 1) For the form 'new Struct[n];', the initializer is a |
| 4413 | // CXXConstructExpr and its type is an IncompleteArrayType. |
| 4414 | // 2) For the form 'new Struct[n]{1,2,3}', the initializer is an |
| 4415 | // InitListExpr and the initializer for the remaining elements |
| 4416 | // is the array filler. |
| 4417 | // 3) StringLiterals don't have an array filler, so we need to zero |
| 4418 | // the remaining elements. |
| 4419 | |
| 4420 | if (DynamicInit || ElemT || InitType->isIncompleteArrayType()) { |
| 4421 | const Function *CtorFunc = nullptr; |
| 4422 | if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: Init)) { |
| 4423 | CtorFunc = getFunction(FD: CE->getConstructor()); |
| 4424 | if (!CtorFunc) |
| 4425 | return false; |
| 4426 | } else if (!DynamicInit && !ElemT) |
| 4427 | DynamicInit = Init; |
| 4428 | |
| 4429 | LabelTy EndLabel = this->getLabel(); |
| 4430 | LabelTy StartLabel = this->getLabel(); |
| 4431 | |
| 4432 | // In the nothrow case, the alloc above might have returned nullptr. |
| 4433 | // Don't call any constructors that case. |
| 4434 | if (IsNoThrow) { |
| 4435 | if (!this->emitDupPtr(E)) |
| 4436 | return false; |
| 4437 | if (!this->emitIsNonNullPtr(E)) |
| 4438 | return false; |
| 4439 | if (!this->jumpFalse(EndLabel, E)) |
| 4440 | return false; |
| 4441 | } |
| 4442 | |
| 4443 | // Create loop variables. |
| 4444 | unsigned Iter = |
| 4445 | allocateLocalPrimitive(Decl: Stripped, Ty: SizeT, /*IsConst=*/false); |
| 4446 | if (!this->emitConst(StaticInitElems, SizeT, E)) |
| 4447 | return false; |
| 4448 | if (!this->emitSetLocal(SizeT, Iter, E)) |
| 4449 | return false; |
| 4450 | |
| 4451 | this->fallthrough(StartLabel); |
| 4452 | this->emitLabel(StartLabel); |
| 4453 | // Condition. Iter < ArrayLen? |
| 4454 | if (!this->emitGetLocal(SizeT, Iter, E)) |
| 4455 | return false; |
| 4456 | if (!this->emitGetLocal(SizeT, ArrayLen, E)) |
| 4457 | return false; |
| 4458 | if (!this->emitLT(SizeT, E)) |
| 4459 | return false; |
| 4460 | if (!this->jumpFalse(EndLabel, E)) |
| 4461 | return false; |
| 4462 | |
| 4463 | // Pointer to the allocated array is already on the stack. |
| 4464 | if (!this->emitGetLocal(SizeT, Iter, E)) |
| 4465 | return false; |
| 4466 | if (!this->emitArrayElemPtr(SizeT, E)) |
| 4467 | return false; |
| 4468 | |
| 4469 | if (isa_and_nonnull<ImplicitValueInitExpr>(Val: DynamicInit) && |
| 4470 | DynamicInit->getType()->isArrayType()) { |
| 4471 | QualType ElemType = |
| 4472 | DynamicInit->getType()->getAsArrayTypeUnsafe()->getElementType(); |
| 4473 | if (OptPrimType InitT = classify(ElemType)) { |
| 4474 | if (!this->visitZeroInitializer(T: *InitT, QT: ElemType, E)) |
| 4475 | return false; |
| 4476 | if (!this->emitStorePop(*InitT, E)) |
| 4477 | return false; |
| 4478 | } else { |
| 4479 | assert(ElemType->isArrayType()); |
| 4480 | if (!this->visitZeroArrayInitializer(T: ElemType, E)) |
| 4481 | return false; |
| 4482 | } |
| 4483 | } else if (DynamicInit) { |
| 4484 | if (OptPrimType InitT = classify(DynamicInit)) { |
| 4485 | if (!this->visit(E: DynamicInit)) |
| 4486 | return false; |
| 4487 | if (!this->emitStorePop(*InitT, E)) |
| 4488 | return false; |
| 4489 | } else { |
| 4490 | if (!this->visitInitializerPop(E: DynamicInit)) |
| 4491 | return false; |
| 4492 | } |
| 4493 | } else if (ElemT) { |
| 4494 | if (!this->visitZeroInitializer( |
| 4495 | T: *ElemT, QT: InitType->getAsArrayTypeUnsafe()->getElementType(), |
| 4496 | E: Init)) |
| 4497 | return false; |
| 4498 | if (!this->emitStorePop(*ElemT, E)) |
| 4499 | return false; |
| 4500 | } else { |
| 4501 | assert(CtorFunc); |
| 4502 | if (!this->emitCall(CtorFunc, 0, E)) |
| 4503 | return false; |
| 4504 | } |
| 4505 | |
| 4506 | // ++Iter; |
| 4507 | if (!this->emitGetPtrLocal(Iter, E)) |
| 4508 | return false; |
| 4509 | if (!this->emitIncPop(SizeT, false, E)) |
| 4510 | return false; |
| 4511 | |
| 4512 | if (!this->jump(StartLabel, E)) |
| 4513 | return false; |
| 4514 | |
| 4515 | this->fallthrough(EndLabel); |
| 4516 | this->emitLabel(EndLabel); |
| 4517 | } |
| 4518 | } |
| 4519 | } else { // Non-array. |
| 4520 | if (PlacementDest) { |
| 4521 | if (!this->visit(E: PlacementDest)) |
| 4522 | return false; |
| 4523 | if (!this->emitCheckNewTypeMismatch(E, E)) |
| 4524 | return false; |
| 4525 | |
| 4526 | } else { |
| 4527 | // Allocate just one element. |
| 4528 | if (!this->emitAlloc(Desc, E)) |
| 4529 | return false; |
| 4530 | } |
| 4531 | |
| 4532 | if (Init) { |
| 4533 | if (ElemT) { |
| 4534 | if (!this->visit(E: Init)) |
| 4535 | return false; |
| 4536 | |
| 4537 | if (!this->emitInit(*ElemT, E)) |
| 4538 | return false; |
| 4539 | } else { |
| 4540 | // Composite. |
| 4541 | if (!this->visitInitializer(E: Init)) |
| 4542 | return false; |
| 4543 | } |
| 4544 | } |
| 4545 | } |
| 4546 | |
| 4547 | if (DiscardResult) |
| 4548 | return this->emitPopPtr(E); |
| 4549 | |
| 4550 | return true; |
| 4551 | } |
| 4552 | |
| 4553 | template <class Emitter> |
| 4554 | bool Compiler<Emitter>::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 4555 | if (E->containsErrors()) |
| 4556 | return false; |
| 4557 | const FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
| 4558 | |
| 4559 | if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) |
| 4560 | return this->emitInvalidNewDeleteExpr(E, E); |
| 4561 | |
| 4562 | // Arg must be an lvalue. |
| 4563 | if (!this->visit(E: E->getArgument())) |
| 4564 | return false; |
| 4565 | |
| 4566 | return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E); |
| 4567 | } |
| 4568 | |
| 4569 | template <class Emitter> |
| 4570 | bool Compiler<Emitter>::VisitBlockExpr(const BlockExpr *E) { |
| 4571 | if (DiscardResult) |
| 4572 | return true; |
| 4573 | |
| 4574 | const Function *Func = nullptr; |
| 4575 | if (const Function *F = Ctx.getOrCreateObjCBlock(E)) |
| 4576 | Func = F; |
| 4577 | |
| 4578 | if (!Func) |
| 4579 | return false; |
| 4580 | return this->emitGetFnPtr(Func, E); |
| 4581 | } |
| 4582 | |
| 4583 | template <class Emitter> |
| 4584 | bool Compiler<Emitter>::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 4585 | const Type *TypeInfoType = E->getType().getTypePtr(); |
| 4586 | |
| 4587 | auto canonType = [](const Type *T) { |
| 4588 | return T->getCanonicalTypeUnqualified().getTypePtr(); |
| 4589 | }; |
| 4590 | |
| 4591 | if (!E->isPotentiallyEvaluated()) { |
| 4592 | if (DiscardResult) |
| 4593 | return true; |
| 4594 | |
| 4595 | if (E->isTypeOperand()) |
| 4596 | return this->emitGetTypeid( |
| 4597 | canonType(E->getTypeOperand(Context: Ctx.getASTContext()).getTypePtr()), |
| 4598 | TypeInfoType, E); |
| 4599 | |
| 4600 | return this->emitGetTypeid( |
| 4601 | canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType, |
| 4602 | E); |
| 4603 | } |
| 4604 | |
| 4605 | // Otherwise, we need to evaluate the expression operand. |
| 4606 | assert(E->getExprOperand()); |
| 4607 | assert(E->getExprOperand()->isLValue()); |
| 4608 | |
| 4609 | if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E)) |
| 4610 | return false; |
| 4611 | |
| 4612 | if (!this->visit(E: E->getExprOperand())) |
| 4613 | return false; |
| 4614 | |
| 4615 | if (!this->emitGetTypeidPtr(TypeInfoType, E)) |
| 4616 | return false; |
| 4617 | if (DiscardResult) |
| 4618 | return this->emitPopPtr(E); |
| 4619 | return true; |
| 4620 | } |
| 4621 | |
| 4622 | template <class Emitter> |
| 4623 | bool Compiler<Emitter>::VisitObjCDictionaryLiteral( |
| 4624 | const ObjCDictionaryLiteral *E) { |
| 4625 | if (E->isExpressibleAsConstantInitializer()) |
| 4626 | return this->emitDummyPtr(D: E, E); |
| 4627 | return this->emitError(E); |
| 4628 | } |
| 4629 | |
| 4630 | template <class Emitter> |
| 4631 | bool Compiler<Emitter>::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) { |
| 4632 | if (E->isExpressibleAsConstantInitializer()) |
| 4633 | return this->emitDummyPtr(D: E, E); |
| 4634 | return this->emitError(E); |
| 4635 | } |
| 4636 | |
| 4637 | template <class Emitter> |
| 4638 | bool Compiler<Emitter>::VisitCXXReflectExpr(const CXXReflectExpr *E) { |
| 4639 | if (DiscardResult) |
| 4640 | return true; |
| 4641 | |
| 4642 | switch (E->getKind()) { |
| 4643 | case ReflectionKind::Null: { |
| 4644 | assert(false && "null reflection can't be constructed from parsing a " |
| 4645 | "reflection operand" ); |
| 4646 | return false; |
| 4647 | } |
| 4648 | case ReflectionKind::Type: { |
| 4649 | return this->emitReflectValue(E->getKind(), E->getOpaqueValue(), E); |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | assert(false && "unknown or unimplemented reflection entities" ); |
| 4654 | return false; |
| 4655 | } |
| 4656 | |
| 4657 | template <class Emitter> |
| 4658 | bool Compiler<Emitter>::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 4659 | assert(Ctx.getLangOpts().CPlusPlus); |
| 4660 | return this->emitConstBool(E->getValue(), E); |
| 4661 | } |
| 4662 | |
| 4663 | template <class Emitter> |
| 4664 | bool Compiler<Emitter>::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4665 | if (DiscardResult) |
| 4666 | return true; |
| 4667 | assert(!Initializing); |
| 4668 | |
| 4669 | const MSGuidDecl *GuidDecl = E->getGuidDecl(); |
| 4670 | const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl(); |
| 4671 | assert(RD); |
| 4672 | // If the definiton of the result type is incomplete, just return a dummy. |
| 4673 | // If (and when) that is read from, we will fail, but not now. |
| 4674 | if (!RD->isCompleteDefinition()) |
| 4675 | return this->emitDummyPtr(D: GuidDecl, E); |
| 4676 | |
| 4677 | UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(VD: GuidDecl); |
| 4678 | if (!GlobalIndex) |
| 4679 | return false; |
| 4680 | if (!this->emitGetPtrGlobal(*GlobalIndex, E)) |
| 4681 | return false; |
| 4682 | |
| 4683 | assert(this->getRecord(E->getType())); |
| 4684 | |
| 4685 | const APValue &V = GuidDecl->getAsAPValue(); |
| 4686 | if (V.getKind() == APValue::None) |
| 4687 | return true; |
| 4688 | |
| 4689 | assert(V.isStruct()); |
| 4690 | assert(V.getStructNumBases() == 0); |
| 4691 | if (!this->visitAPValueInitializer(Val: V, Info: E, T: E->getType())) |
| 4692 | return false; |
| 4693 | |
| 4694 | return this->emitFinishInit(E); |
| 4695 | } |
| 4696 | |
| 4697 | template <class Emitter> |
| 4698 | bool Compiler<Emitter>::VisitRequiresExpr(const RequiresExpr *E) { |
| 4699 | assert(classifyPrim(E->getType()) == PT_Bool); |
| 4700 | if (E->isValueDependent()) |
| 4701 | return false; |
| 4702 | if (DiscardResult) |
| 4703 | return true; |
| 4704 | return this->emitConstBool(E->isSatisfied(), E); |
| 4705 | } |
| 4706 | |
| 4707 | template <class Emitter> |
| 4708 | bool Compiler<Emitter>::VisitConceptSpecializationExpr( |
| 4709 | const ConceptSpecializationExpr *E) { |
| 4710 | assert(classifyPrim(E->getType()) == PT_Bool); |
| 4711 | if (DiscardResult) |
| 4712 | return true; |
| 4713 | return this->emitConstBool(E->isSatisfied(), E); |
| 4714 | } |
| 4715 | |
| 4716 | template <class Emitter> |
| 4717 | bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator( |
| 4718 | const CXXRewrittenBinaryOperator *E) { |
| 4719 | return this->delegate(E: E->getSemanticForm()); |
| 4720 | } |
| 4721 | |
| 4722 | template <class Emitter> |
| 4723 | bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
| 4724 | |
| 4725 | for (const Expr *SemE : E->semantics()) { |
| 4726 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) { |
| 4727 | if (SemE == E->getResultExpr()) |
| 4728 | return false; |
| 4729 | |
| 4730 | if (OVE->isUnique()) |
| 4731 | continue; |
| 4732 | |
| 4733 | if (!this->discard(E: OVE)) |
| 4734 | return false; |
| 4735 | } else if (SemE == E->getResultExpr()) { |
| 4736 | if (!this->delegate(E: SemE)) |
| 4737 | return false; |
| 4738 | } else { |
| 4739 | if (!this->discard(E: SemE)) |
| 4740 | return false; |
| 4741 | } |
| 4742 | } |
| 4743 | return true; |
| 4744 | } |
| 4745 | |
| 4746 | template <class Emitter> |
| 4747 | bool Compiler<Emitter>::VisitPackIndexingExpr(const PackIndexingExpr *E) { |
| 4748 | return this->delegate(E: E->getSelectedExpr()); |
| 4749 | } |
| 4750 | |
| 4751 | template <class Emitter> |
| 4752 | bool Compiler<Emitter>::VisitRecoveryExpr(const RecoveryExpr *E) { |
| 4753 | return this->emitError(E); |
| 4754 | } |
| 4755 | |
| 4756 | template <class Emitter> |
| 4757 | bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
| 4758 | assert(E->getType()->isVoidPointerType()); |
| 4759 | if (DiscardResult) |
| 4760 | return true; |
| 4761 | |
| 4762 | return this->emitDummyPtr(D: E, E); |
| 4763 | } |
| 4764 | |
| 4765 | template <class Emitter> |
| 4766 | bool Compiler<Emitter>::emitVectorConversion(const Expr *Src, const Expr *E) { |
| 4767 | if (Src->containsErrors()) |
| 4768 | return false; |
| 4769 | |
| 4770 | const auto *VT = E->getType()->castAs<VectorType>(); |
| 4771 | QualType ElemType = VT->getElementType(); |
| 4772 | PrimType ElemT = classifyPrim(ElemType); |
| 4773 | QualType SrcType = Src->getType(); |
| 4774 | PrimType SrcElemT = classifyVectorElementType(T: SrcType); |
| 4775 | |
| 4776 | if (!Initializing) { |
| 4777 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 4778 | if (!LocalIndex) |
| 4779 | return false; |
| 4780 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4781 | return false; |
| 4782 | } |
| 4783 | |
| 4784 | unsigned SrcOffset = |
| 4785 | this->allocateLocalPrimitive(Decl: Src, Ty: PT_Ptr, /*IsConst=*/true); |
| 4786 | if (!this->visit(E: Src)) |
| 4787 | return false; |
| 4788 | if (!this->emitSetLocal(PT_Ptr, SrcOffset, E)) |
| 4789 | return false; |
| 4790 | |
| 4791 | for (unsigned I = 0; I != VT->getNumElements(); ++I) { |
| 4792 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 4793 | return false; |
| 4794 | if (!this->emitArrayElemPop(SrcElemT, I, E)) |
| 4795 | return false; |
| 4796 | |
| 4797 | // Cast to the desired result element type. |
| 4798 | if (SrcElemT != ElemT) { |
| 4799 | if (!this->emitPrimCast(FromT: SrcElemT, ToT: ElemT, ToQT: ElemType, E)) |
| 4800 | return false; |
| 4801 | } else if (ElemType->isFloatingType() && SrcType != ElemType) { |
| 4802 | const auto *TargetSemantics = &Ctx.getFloatSemantics(T: ElemType); |
| 4803 | if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E)) |
| 4804 | return false; |
| 4805 | } |
| 4806 | if (!this->emitInitElem(ElemT, I, E)) |
| 4807 | return false; |
| 4808 | } |
| 4809 | return true; |
| 4810 | } |
| 4811 | |
| 4812 | template <class Emitter> |
| 4813 | bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) { |
| 4814 | return emitVectorConversion(Src: E->getSrcExpr(), E); |
| 4815 | } |
| 4816 | |
| 4817 | template <class Emitter> |
| 4818 | bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) { |
| 4819 | // FIXME: Unary shuffle with mask not currently supported. |
| 4820 | if (E->getNumSubExprs() == 2) |
| 4821 | return this->emitInvalid(E); |
| 4822 | |
| 4823 | assert(E->getNumSubExprs() > 2); |
| 4824 | |
| 4825 | const Expr *Vecs[] = {E->getExpr(Index: 0), E->getExpr(Index: 1)}; |
| 4826 | const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>(); |
| 4827 | PrimType ElemT = classifyPrim(VT->getElementType()); |
| 4828 | unsigned NumInputElems = VT->getNumElements(); |
| 4829 | unsigned NumOutputElems = E->getNumSubExprs() - 2; |
| 4830 | assert(NumOutputElems > 0); |
| 4831 | |
| 4832 | if (!Initializing) { |
| 4833 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 4834 | if (!LocalIndex) |
| 4835 | return false; |
| 4836 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4837 | return false; |
| 4838 | } |
| 4839 | |
| 4840 | // Save both input vectors to a local variable. |
| 4841 | unsigned VectorOffsets[2]; |
| 4842 | for (unsigned I = 0; I != 2; ++I) { |
| 4843 | VectorOffsets[I] = |
| 4844 | this->allocateLocalPrimitive(Decl: Vecs[I], Ty: PT_Ptr, /*IsConst=*/true); |
| 4845 | if (!this->visit(E: Vecs[I])) |
| 4846 | return false; |
| 4847 | if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E)) |
| 4848 | return false; |
| 4849 | } |
| 4850 | for (unsigned I = 0; I != NumOutputElems; ++I) { |
| 4851 | APSInt ShuffleIndex = E->getShuffleMaskIdx(N: I); |
| 4852 | assert(ShuffleIndex >= -1); |
| 4853 | if (ShuffleIndex == -1) |
| 4854 | return this->emitInvalidShuffleVectorIndex(I, E); |
| 4855 | |
| 4856 | assert(ShuffleIndex < (NumInputElems * 2)); |
| 4857 | if (!this->emitGetLocal(PT_Ptr, |
| 4858 | VectorOffsets[ShuffleIndex >= NumInputElems], E)) |
| 4859 | return false; |
| 4860 | unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems; |
| 4861 | if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E)) |
| 4862 | return false; |
| 4863 | |
| 4864 | if (!this->emitInitElem(ElemT, I, E)) |
| 4865 | return false; |
| 4866 | } |
| 4867 | |
| 4868 | if (DiscardResult) |
| 4869 | return this->emitPopPtr(E); |
| 4870 | |
| 4871 | return true; |
| 4872 | } |
| 4873 | |
| 4874 | template <class Emitter> |
| 4875 | bool Compiler<Emitter>::VisitExtVectorElementExpr( |
| 4876 | const ExtVectorElementExpr *E) { |
| 4877 | const Expr *Base = E->getBase(); |
| 4878 | assert( |
| 4879 | Base->getType()->isVectorType() || |
| 4880 | Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType()); |
| 4881 | |
| 4882 | SmallVector<uint32_t, 4> Indices; |
| 4883 | E->getEncodedElementAccess(Elts&: Indices); |
| 4884 | |
| 4885 | if (Indices.size() == 1) { |
| 4886 | if (!this->visit(E: Base)) |
| 4887 | return false; |
| 4888 | |
| 4889 | if (E->isGLValue()) { |
| 4890 | if (!this->emitConstUint32(Indices[0], E)) |
| 4891 | return false; |
| 4892 | return this->emitArrayElemPtrPop(PT_Uint32, E); |
| 4893 | } |
| 4894 | // Else, also load the value. |
| 4895 | return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E); |
| 4896 | } |
| 4897 | |
| 4898 | // Create a local variable for the base. |
| 4899 | unsigned BaseOffset = allocateLocalPrimitive(Decl: Base, Ty: PT_Ptr, /*IsConst=*/true); |
| 4900 | if (!this->visit(E: Base)) |
| 4901 | return false; |
| 4902 | if (!this->emitSetLocal(PT_Ptr, BaseOffset, E)) |
| 4903 | return false; |
| 4904 | |
| 4905 | // Now the vector variable for the return value. |
| 4906 | if (!Initializing) { |
| 4907 | UnsignedOrNone ResultIndex = allocateLocal(Decl: E); |
| 4908 | if (!ResultIndex) |
| 4909 | return false; |
| 4910 | if (!this->emitGetPtrLocal(*ResultIndex, E)) |
| 4911 | return false; |
| 4912 | } |
| 4913 | |
| 4914 | assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements()); |
| 4915 | |
| 4916 | PrimType ElemT = |
| 4917 | classifyPrim(E->getType()->getAs<VectorType>()->getElementType()); |
| 4918 | uint32_t DstIndex = 0; |
| 4919 | for (uint32_t I : Indices) { |
| 4920 | if (!this->emitGetLocal(PT_Ptr, BaseOffset, E)) |
| 4921 | return false; |
| 4922 | if (!this->emitArrayElemPop(ElemT, I, E)) |
| 4923 | return false; |
| 4924 | if (!this->emitInitElem(ElemT, DstIndex, E)) |
| 4925 | return false; |
| 4926 | ++DstIndex; |
| 4927 | } |
| 4928 | |
| 4929 | // Leave the result pointer on the stack. |
| 4930 | assert(!DiscardResult); |
| 4931 | return true; |
| 4932 | } |
| 4933 | |
| 4934 | template <class Emitter> |
| 4935 | bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
| 4936 | const Expr *SubExpr = E->getSubExpr(); |
| 4937 | if (!E->isExpressibleAsConstantInitializer()) |
| 4938 | return this->discard(E: SubExpr) && this->emitInvalid(E); |
| 4939 | |
| 4940 | if (DiscardResult) |
| 4941 | return true; |
| 4942 | |
| 4943 | assert(classifyPrim(E) == PT_Ptr); |
| 4944 | return this->emitDummyPtr(D: E, E); |
| 4945 | } |
| 4946 | |
| 4947 | template <class Emitter> |
| 4948 | bool Compiler<Emitter>::VisitCXXStdInitializerListExpr( |
| 4949 | const CXXStdInitializerListExpr *E) { |
| 4950 | const Expr *SubExpr = E->getSubExpr(); |
| 4951 | const ConstantArrayType *ArrayType = |
| 4952 | Ctx.getASTContext().getAsConstantArrayType(T: SubExpr->getType()); |
| 4953 | const Record *R = getRecord(E->getType()); |
| 4954 | assert(SubExpr->isGLValue()); |
| 4955 | assert(!canClassify(E->getType())); |
| 4956 | |
| 4957 | if (!Initializing) { |
| 4958 | UnsignedOrNone LocalIndex = allocateLocal(Decl: E); |
| 4959 | if (!LocalIndex) |
| 4960 | return false; |
| 4961 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 4962 | return false; |
| 4963 | } |
| 4964 | |
| 4965 | if (!this->visit(E: SubExpr)) |
| 4966 | return false; |
| 4967 | if (!this->emitConstUint8(0, E)) |
| 4968 | return false; |
| 4969 | if (!this->emitArrayElemPtrPopUint8(E)) |
| 4970 | return false; |
| 4971 | if (!this->emitInitFieldPtr(R->getField(I: 0u)->Offset, E)) |
| 4972 | return false; |
| 4973 | |
| 4974 | PrimType SecondFieldT = *R->getField(I: 1u)->T; |
| 4975 | if (isIntegerOrBoolType(T: SecondFieldT)) { |
| 4976 | if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E)) |
| 4977 | return false; |
| 4978 | if (!this->emitInitField(SecondFieldT, R->getField(I: 1u)->Offset, E)) |
| 4979 | return false; |
| 4980 | if (DiscardResult) |
| 4981 | return this->emitPopPtr(E); |
| 4982 | return true; |
| 4983 | } |
| 4984 | assert(SecondFieldT == PT_Ptr); |
| 4985 | |
| 4986 | if (!this->emitGetFieldPtr(R->getField(I: 0u)->Offset, E)) |
| 4987 | return false; |
| 4988 | if (!this->emitExpandPtr(E)) |
| 4989 | return false; |
| 4990 | if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E)) |
| 4991 | return false; |
| 4992 | if (!this->emitArrayElemPtrPop(PT_Uint64, E)) |
| 4993 | return false; |
| 4994 | |
| 4995 | if (!this->emitInitFieldPtr(R->getField(I: 1u)->Offset, E)) |
| 4996 | return false; |
| 4997 | if (DiscardResult) |
| 4998 | return this->emitPopPtr(E); |
| 4999 | return true; |
| 5000 | } |
| 5001 | |
| 5002 | template <class Emitter> |
| 5003 | bool Compiler<Emitter>::VisitStmtExpr(const StmtExpr *E) { |
| 5004 | LocalScope<Emitter> BS(this); |
| 5005 | llvm::SaveAndRestore StmtExprSAR(this->InStmtExpr, true); |
| 5006 | |
| 5007 | const CompoundStmt *CS = E->getSubStmt(); |
| 5008 | const Stmt *Result = CS->body_back(); |
| 5009 | for (const Stmt *S : CS->body()) { |
| 5010 | if (S != Result) { |
| 5011 | if (!this->visitStmt(S)) |
| 5012 | return false; |
| 5013 | continue; |
| 5014 | } |
| 5015 | |
| 5016 | assert(S == Result); |
| 5017 | if (const Expr *ResultExpr = dyn_cast<Expr>(Val: S)) |
| 5018 | return this->delegate(E: ResultExpr); |
| 5019 | if (!this->visitStmt(S)) |
| 5020 | return false; |
| 5021 | return this->emitUnsupported(E); |
| 5022 | } |
| 5023 | |
| 5024 | return BS.destroyLocals(); |
| 5025 | } |
| 5026 | |
| 5027 | template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) { |
| 5028 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true, |
| 5029 | /*NewInitializing=*/false, /*ToLValue=*/false); |
| 5030 | return this->Visit(E); |
| 5031 | } |
| 5032 | |
| 5033 | template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) { |
| 5034 | // We're basically doing: |
| 5035 | // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue); |
| 5036 | // but that's unnecessary of course. |
| 5037 | return this->Visit(E); |
| 5038 | } |
| 5039 | |
| 5040 | static const Expr *stripCheckedDerivedToBaseCasts(const Expr *E) { |
| 5041 | if (const auto *PE = dyn_cast<ParenExpr>(Val: E)) |
| 5042 | return stripCheckedDerivedToBaseCasts(E: PE->getSubExpr()); |
| 5043 | |
| 5044 | if (const auto *CE = dyn_cast<CastExpr>(Val: E); |
| 5045 | CE && |
| 5046 | (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp)) |
| 5047 | return stripCheckedDerivedToBaseCasts(E: CE->getSubExpr()); |
| 5048 | |
| 5049 | return E; |
| 5050 | } |
| 5051 | |
| 5052 | static const Expr *stripDerivedToBaseCasts(const Expr *E) { |
| 5053 | if (const auto *PE = dyn_cast<ParenExpr>(Val: E)) |
| 5054 | return stripDerivedToBaseCasts(E: PE->getSubExpr()); |
| 5055 | |
| 5056 | if (const auto *CE = dyn_cast<CastExpr>(Val: E); |
| 5057 | CE && (CE->getCastKind() == CK_DerivedToBase || |
| 5058 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 5059 | CE->getCastKind() == CK_NoOp)) |
| 5060 | return stripDerivedToBaseCasts(E: CE->getSubExpr()); |
| 5061 | |
| 5062 | return E; |
| 5063 | } |
| 5064 | |
| 5065 | template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) { |
| 5066 | if (E->getType().isNull()) |
| 5067 | return false; |
| 5068 | |
| 5069 | if (E->getType()->isVoidType()) |
| 5070 | return this->discard(E); |
| 5071 | |
| 5072 | // Create local variable to hold the return value. |
| 5073 | if (!E->isGLValue() && !canClassify(E->getType())) { |
| 5074 | UnsignedOrNone LocalIndex = allocateLocal( |
| 5075 | Decl: stripDerivedToBaseCasts(E), Ty: QualType(), ScopeKind::FullExpression); |
| 5076 | if (!LocalIndex) |
| 5077 | return false; |
| 5078 | |
| 5079 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 5080 | return false; |
| 5081 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalIndex)); |
| 5082 | return this->visitInitializer(E); |
| 5083 | } |
| 5084 | |
| 5085 | // Otherwise,we have a primitive return value, produce the value directly |
| 5086 | // and push it on the stack. |
| 5087 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5088 | /*NewInitializing=*/false, /*ToLValue=*/ToLValue); |
| 5089 | return this->Visit(E); |
| 5090 | } |
| 5091 | |
| 5092 | template <class Emitter> |
| 5093 | bool Compiler<Emitter>::visitInitializer(const Expr *E) { |
| 5094 | assert(!canClassify(E->getType())); |
| 5095 | |
| 5096 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5097 | /*NewInitializing=*/true, /*ToLValue=*/false); |
| 5098 | return this->Visit(E) && this->emitFinishInit(E); |
| 5099 | } |
| 5100 | |
| 5101 | template <class Emitter> |
| 5102 | bool Compiler<Emitter>::visitInitializerPop(const Expr *E) { |
| 5103 | assert(!canClassify(E->getType())); |
| 5104 | |
| 5105 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5106 | /*NewInitializing=*/true, /*ToLValue=*/false); |
| 5107 | return this->Visit(E) && this->emitFinishInitPop(E); |
| 5108 | } |
| 5109 | |
| 5110 | template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) { |
| 5111 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5112 | /*NewInitializing=*/false, /*ToLValue=*/true); |
| 5113 | return this->Visit(E); |
| 5114 | } |
| 5115 | |
| 5116 | template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) { |
| 5117 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5118 | /*NewInitializing=*/false, /*ToLValue=*/ToLValue); |
| 5119 | |
| 5120 | OptPrimType T = classify(E->getType()); |
| 5121 | if (!T) { |
| 5122 | // Convert complex values to bool. |
| 5123 | if (E->getType()->isAnyComplexType()) { |
| 5124 | if (!this->visit(E)) |
| 5125 | return false; |
| 5126 | return this->emitComplexBoolCast(E); |
| 5127 | } |
| 5128 | return false; |
| 5129 | } |
| 5130 | |
| 5131 | if (!this->visit(E)) |
| 5132 | return false; |
| 5133 | |
| 5134 | if (T == PT_Bool) |
| 5135 | return true; |
| 5136 | |
| 5137 | // Convert pointers to bool. |
| 5138 | if (T == PT_Ptr) |
| 5139 | return this->emitIsNonNullPtr(E); |
| 5140 | |
| 5141 | // Or Floats. |
| 5142 | if (T == PT_Float) |
| 5143 | return this->emitCastFloatingIntegralBool(getFPOptions(E), E); |
| 5144 | |
| 5145 | // Or anything else we can. |
| 5146 | return this->emitCast(*T, PT_Bool, E); |
| 5147 | } |
| 5148 | |
| 5149 | template <class Emitter> |
| 5150 | bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT, |
| 5151 | const Expr *E) { |
| 5152 | if (const auto *AT = QT->getAs<AtomicType>()) |
| 5153 | QT = AT->getValueType(); |
| 5154 | |
| 5155 | switch (T) { |
| 5156 | case PT_Bool: |
| 5157 | return this->emitZeroBool(E); |
| 5158 | case PT_Sint8: |
| 5159 | return this->emitZeroSint8(E); |
| 5160 | case PT_Uint8: |
| 5161 | return this->emitZeroUint8(E); |
| 5162 | case PT_Sint16: |
| 5163 | return this->emitZeroSint16(E); |
| 5164 | case PT_Uint16: |
| 5165 | return this->emitZeroUint16(E); |
| 5166 | case PT_Sint32: |
| 5167 | return this->emitZeroSint32(E); |
| 5168 | case PT_Uint32: |
| 5169 | return this->emitZeroUint32(E); |
| 5170 | case PT_Sint64: |
| 5171 | return this->emitZeroSint64(E); |
| 5172 | case PT_Uint64: |
| 5173 | return this->emitZeroUint64(E); |
| 5174 | case PT_IntAP: |
| 5175 | return this->emitZeroIntAP(Ctx.getBitWidth(T: QT), E); |
| 5176 | case PT_IntAPS: |
| 5177 | return this->emitZeroIntAPS(Ctx.getBitWidth(T: QT), E); |
| 5178 | case PT_Ptr: |
| 5179 | return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT), |
| 5180 | nullptr, E); |
| 5181 | case PT_MemberPtr: |
| 5182 | return this->emitNullMemberPtr(0, nullptr, E); |
| 5183 | case PT_Float: { |
| 5184 | APFloat F = APFloat::getZero(Sem: Ctx.getFloatSemantics(T: QT)); |
| 5185 | return this->emitFloat(F, Info: E); |
| 5186 | } |
| 5187 | case PT_FixedPoint: { |
| 5188 | auto Sem = Ctx.getASTContext().getFixedPointSemantics(Ty: QT); |
| 5189 | return this->emitConstFixedPoint(FixedPoint::zero(Sem), E); |
| 5190 | } |
| 5191 | case PT_Reflect: |
| 5192 | return this->emitReflectValue(ReflectionKind::Null, nullptr, E); |
| 5193 | } |
| 5194 | llvm_unreachable("unknown primitive type" ); |
| 5195 | } |
| 5196 | |
| 5197 | template <class Emitter> |
| 5198 | bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R, |
| 5199 | const Expr *E, |
| 5200 | bool IsCompleteClass) { |
| 5201 | assert(E); |
| 5202 | assert(R); |
| 5203 | // Fields |
| 5204 | for (const Record::Field &Field : R->fields()) { |
| 5205 | if (Field.isUnnamedBitField()) |
| 5206 | continue; |
| 5207 | |
| 5208 | const Descriptor *D = Field.Desc; |
| 5209 | if (D->isPrimitive()) { |
| 5210 | QualType QT = D->getType(); |
| 5211 | PrimType T = D->getPrimType(); |
| 5212 | if (!this->visitZeroInitializer(T, QT, E)) |
| 5213 | return false; |
| 5214 | if (R->isUnion()) { |
| 5215 | if (!this->emitInitFieldActivate(T, Field.Offset, E)) |
| 5216 | return false; |
| 5217 | break; |
| 5218 | } |
| 5219 | if (!this->emitInitField(T, Field.Offset, E)) |
| 5220 | return false; |
| 5221 | continue; |
| 5222 | } |
| 5223 | |
| 5224 | if (!this->emitGetPtrField(Field.Offset, E)) |
| 5225 | return false; |
| 5226 | |
| 5227 | if (D->isPrimitiveArray()) { |
| 5228 | QualType ET = D->getElemQualType(); |
| 5229 | PrimType T = D->getPrimType(); |
| 5230 | for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) { |
| 5231 | if (!this->visitZeroInitializer(T, QT: ET, E)) |
| 5232 | return false; |
| 5233 | if (!this->emitInitElem(T, I, E)) |
| 5234 | return false; |
| 5235 | } |
| 5236 | } else if (D->isCompositeArray()) { |
| 5237 | // Can't be a vector or complex field. |
| 5238 | if (!this->visitZeroArrayInitializer(T: D->getType(), E)) |
| 5239 | return false; |
| 5240 | } else if (D->isRecord()) { |
| 5241 | if (!this->visitZeroRecordInitializer(R: D->ElemRecord, E)) |
| 5242 | return false; |
| 5243 | } else |
| 5244 | return false; |
| 5245 | |
| 5246 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 5247 | // object's first non-static named data member is zero-initialized |
| 5248 | if (R->isUnion()) { |
| 5249 | if (!this->emitFinishInitActivatePop(E)) |
| 5250 | return false; |
| 5251 | break; |
| 5252 | } |
| 5253 | if (!this->emitFinishInitPop(E)) |
| 5254 | return false; |
| 5255 | } |
| 5256 | |
| 5257 | for (const Record::Base &B : R->bases()) { |
| 5258 | if (!this->emitGetPtrBase(B.Offset, E)) |
| 5259 | return false; |
| 5260 | if (!this->visitZeroRecordInitializer(R: B.R, E, /*IsCompleteClass=*/false)) |
| 5261 | return false; |
| 5262 | if (!this->emitFinishInitPop(E)) |
| 5263 | return false; |
| 5264 | } |
| 5265 | |
| 5266 | if (IsCompleteClass) { |
| 5267 | for (const Record::Base &B : R->virtual_bases()) { |
| 5268 | if (!this->emitGetPtrVirtBase(cast<CXXRecordDecl>(Val: B.R->getDecl()), E)) |
| 5269 | return false; |
| 5270 | if (!this->visitZeroRecordInitializer(R: B.R, E, /*IsCompleteClass=*/false)) |
| 5271 | return false; |
| 5272 | if (!this->emitFinishInitPop(E)) |
| 5273 | return false; |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | return true; |
| 5278 | } |
| 5279 | |
| 5280 | template <class Emitter> |
| 5281 | bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) { |
| 5282 | assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType()); |
| 5283 | const ArrayType *AT = T->getAsArrayTypeUnsafe(); |
| 5284 | QualType ElemType = AT->getElementType(); |
| 5285 | size_t NumElems = cast<ConstantArrayType>(Val: AT)->getZExtSize(); |
| 5286 | |
| 5287 | if (OptPrimType ElemT = classify(ElemType)) { |
| 5288 | for (size_t I = 0; I != NumElems; ++I) { |
| 5289 | if (!this->visitZeroInitializer(T: *ElemT, QT: ElemType, E)) |
| 5290 | return false; |
| 5291 | if (!this->emitInitElem(*ElemT, I, E)) |
| 5292 | return false; |
| 5293 | } |
| 5294 | return true; |
| 5295 | } |
| 5296 | if (ElemType->isRecordType()) { |
| 5297 | const Record *R = getRecord(ElemType); |
| 5298 | if (!R) |
| 5299 | return false; |
| 5300 | |
| 5301 | for (size_t I = 0; I != NumElems; ++I) { |
| 5302 | if (!this->emitConstUint32(I, E)) |
| 5303 | return false; |
| 5304 | if (!this->emitArrayElemPtr(PT_Uint32, E)) |
| 5305 | return false; |
| 5306 | if (!this->visitZeroRecordInitializer(R, E)) |
| 5307 | return false; |
| 5308 | if (!this->emitPopPtr(E)) |
| 5309 | return false; |
| 5310 | } |
| 5311 | return true; |
| 5312 | } |
| 5313 | if (ElemType->isArrayType()) { |
| 5314 | for (size_t I = 0; I != NumElems; ++I) { |
| 5315 | if (!this->emitConstUint32(I, E)) |
| 5316 | return false; |
| 5317 | if (!this->emitArrayElemPtr(PT_Uint32, E)) |
| 5318 | return false; |
| 5319 | if (!this->visitZeroArrayInitializer(T: ElemType, E)) |
| 5320 | return false; |
| 5321 | if (!this->emitPopPtr(E)) |
| 5322 | return false; |
| 5323 | } |
| 5324 | return true; |
| 5325 | } |
| 5326 | |
| 5327 | return false; |
| 5328 | } |
| 5329 | |
| 5330 | template <class Emitter> |
| 5331 | bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS, |
| 5332 | const Expr *E) { |
| 5333 | if (!canClassify(E->getType())) |
| 5334 | return false; |
| 5335 | |
| 5336 | bool NeedsFlip = !isSideEffectFree(E: RHS); |
| 5337 | if (!NeedsFlip) { |
| 5338 | if (!this->visit(E: LHS)) |
| 5339 | return false; |
| 5340 | if (!this->visit(E: RHS)) |
| 5341 | return false; |
| 5342 | } else { |
| 5343 | if (!this->visit(E: RHS)) |
| 5344 | return false; |
| 5345 | if (!this->visit(E: LHS)) |
| 5346 | return false; |
| 5347 | } |
| 5348 | |
| 5349 | if (LHS->getType().isVolatileQualified()) |
| 5350 | return this->emitInvalidStore(LHS->getType().getTypePtr(), E); |
| 5351 | |
| 5352 | // We don't support assignments in C. |
| 5353 | if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E)) |
| 5354 | return false; |
| 5355 | |
| 5356 | PrimType RHT = classifyPrim(RHS); |
| 5357 | bool Activates = refersToUnion(E: LHS); |
| 5358 | bool BitField = LHS->refersToBitField(); |
| 5359 | |
| 5360 | if (NeedsFlip && !this->emitFlip(PT_Ptr, RHT, E)) |
| 5361 | return false; |
| 5362 | |
| 5363 | if (DiscardResult) { |
| 5364 | if (BitField && Activates) |
| 5365 | return this->emitStoreBitFieldActivatePop(RHT, E); |
| 5366 | if (BitField) |
| 5367 | return this->emitStoreBitFieldPop(RHT, E); |
| 5368 | if (Activates) |
| 5369 | return this->emitStoreActivatePop(RHT, E); |
| 5370 | // Otherwise, regular non-activating store. |
| 5371 | return this->emitStorePop(RHT, E); |
| 5372 | } |
| 5373 | |
| 5374 | auto maybeLoad = [&](bool Result) -> bool { |
| 5375 | if (!Result) |
| 5376 | return false; |
| 5377 | // Assignments aren't necessarily lvalues in C. |
| 5378 | // Load from them in that case. |
| 5379 | if (!E->isLValue()) |
| 5380 | return this->emitLoadPop(RHT, E); |
| 5381 | return true; |
| 5382 | }; |
| 5383 | |
| 5384 | if (BitField && Activates) |
| 5385 | return maybeLoad(this->emitStoreBitFieldActivate(RHT, E)); |
| 5386 | if (BitField) |
| 5387 | return maybeLoad(this->emitStoreBitField(RHT, E)); |
| 5388 | if (Activates) |
| 5389 | return maybeLoad(this->emitStoreActivate(RHT, E)); |
| 5390 | // Otherwise, regular non-activating store. |
| 5391 | return maybeLoad(this->emitStore(RHT, E)); |
| 5392 | } |
| 5393 | |
| 5394 | template <class Emitter> |
| 5395 | template <typename T> |
| 5396 | bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, SourceInfo Info) { |
| 5397 | switch (Ty) { |
| 5398 | case PT_Sint8: |
| 5399 | return this->emitConstSint8(Value, Info); |
| 5400 | case PT_Uint8: |
| 5401 | return this->emitConstUint8(Value, Info); |
| 5402 | case PT_Sint16: |
| 5403 | return this->emitConstSint16(Value, Info); |
| 5404 | case PT_Uint16: |
| 5405 | return this->emitConstUint16(Value, Info); |
| 5406 | case PT_Sint32: |
| 5407 | return this->emitConstSint32(Value, Info); |
| 5408 | case PT_Uint32: |
| 5409 | return this->emitConstUint32(Value, Info); |
| 5410 | case PT_Sint64: |
| 5411 | return this->emitConstSint64(Value, Info); |
| 5412 | case PT_Uint64: |
| 5413 | return this->emitConstUint64(Value, Info); |
| 5414 | case PT_Bool: |
| 5415 | return this->emitConstBool(Value, Info); |
| 5416 | case PT_Ptr: |
| 5417 | case PT_MemberPtr: |
| 5418 | case PT_Float: |
| 5419 | case PT_IntAP: |
| 5420 | case PT_IntAPS: |
| 5421 | case PT_FixedPoint: |
| 5422 | case PT_Reflect: |
| 5423 | llvm_unreachable("Invalid integral type" ); |
| 5424 | break; |
| 5425 | } |
| 5426 | llvm_unreachable("unknown primitive type" ); |
| 5427 | } |
| 5428 | |
| 5429 | template <class Emitter> |
| 5430 | template <typename T> |
| 5431 | bool Compiler<Emitter>::emitConst(T Value, const Expr *E) { |
| 5432 | return this->emitConst(Value, classifyPrim(E->getType()), E); |
| 5433 | } |
| 5434 | |
| 5435 | template <class Emitter> |
| 5436 | bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty, |
| 5437 | SourceInfo Info) { |
| 5438 | if (Ty == PT_IntAPS) |
| 5439 | return this->emitConstIntAPS(Value, Info); |
| 5440 | if (Ty == PT_IntAP) |
| 5441 | return this->emitConstIntAP(Value, Info); |
| 5442 | |
| 5443 | if (Value.isSigned()) |
| 5444 | return this->emitConst(Value.getSExtValue(), Ty, Info); |
| 5445 | return this->emitConst(Value.getZExtValue(), Ty, Info); |
| 5446 | } |
| 5447 | |
| 5448 | template <class Emitter> |
| 5449 | bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty, |
| 5450 | SourceInfo Info) { |
| 5451 | if (Ty == PT_IntAPS) |
| 5452 | return this->emitConstIntAPS(Value, Info); |
| 5453 | if (Ty == PT_IntAP) |
| 5454 | return this->emitConstIntAP(Value, Info); |
| 5455 | |
| 5456 | if (isSignedType(T: Ty)) |
| 5457 | return this->emitConst(Value.getSExtValue(), Ty, Info); |
| 5458 | return this->emitConst(Value.getZExtValue(), Ty, Info); |
| 5459 | } |
| 5460 | |
| 5461 | template <class Emitter> |
| 5462 | bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) { |
| 5463 | return this->emitConst(Value, classifyPrim(E->getType()), E); |
| 5464 | } |
| 5465 | |
| 5466 | template <class Emitter> |
| 5467 | unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclOrExpr Src, PrimType Ty, |
| 5468 | bool IsConst, |
| 5469 | bool IsVolatile, |
| 5470 | ScopeKind SC) { |
| 5471 | // FIXME: There are cases where Src.isExpr() is wrong, e.g. |
| 5472 | // (int){12} in C. Consider using Expr::isTemporaryObject() instead |
| 5473 | // or isa<MaterializeTemporaryExpr>(). |
| 5474 | Descriptor *D = P.createDescriptor(D: Src, T: Ty, SourceTy: nullptr, IsConst, IsTemporary: Src.isExpr(), |
| 5475 | /*IsMutable=*/false, IsVolatile); |
| 5476 | D->IsConstexprUnknown = this->VariablesAreConstexprUnknown; |
| 5477 | Scope::Local Local = this->createLocal(D); |
| 5478 | if (auto *VD = Src.asValueDecl()) |
| 5479 | Locals.insert(KV: {VD, Local}); |
| 5480 | VarScope->addForScopeKind(Local, SC); |
| 5481 | return Local.Offset; |
| 5482 | } |
| 5483 | |
| 5484 | template <class Emitter> |
| 5485 | UnsignedOrNone Compiler<Emitter>::allocateLocal(DeclOrExpr Src, QualType Ty, |
| 5486 | ScopeKind SC) { |
| 5487 | const ValueDecl *Key = nullptr; |
| 5488 | const Expr *Init = nullptr; |
| 5489 | bool IsTemporary = false; |
| 5490 | if (auto *VD = Src.asValueDecl()) { |
| 5491 | Key = VD; |
| 5492 | |
| 5493 | if (const auto *VarD = dyn_cast<VarDecl>(Val: VD)) |
| 5494 | Init = VarD->getInit(); |
| 5495 | } |
| 5496 | if (const auto *E = Src.asExpr()) { |
| 5497 | IsTemporary = true; |
| 5498 | if (Ty.isNull()) |
| 5499 | Ty = E->getType(); |
| 5500 | } |
| 5501 | |
| 5502 | Descriptor *D = P.createDescriptor( |
| 5503 | D: Src, Ty: Ty.getTypePtr(), IsConst: Ty.isConstQualified(), IsTemporary, |
| 5504 | /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(), Init); |
| 5505 | if (!D) |
| 5506 | return std::nullopt; |
| 5507 | D->IsConstexprUnknown = this->VariablesAreConstexprUnknown; |
| 5508 | |
| 5509 | Scope::Local Local = this->createLocal(D); |
| 5510 | if (Key) |
| 5511 | Locals.insert(KV: {Key, Local}); |
| 5512 | VarScope->addForScopeKind(Local, SC); |
| 5513 | return Local.Offset; |
| 5514 | } |
| 5515 | |
| 5516 | template <class Emitter> |
| 5517 | UnsignedOrNone Compiler<Emitter>::allocateTemporary(const Expr *E) { |
| 5518 | QualType Ty = E->getType(); |
| 5519 | assert(!Ty->isRecordType()); |
| 5520 | |
| 5521 | Descriptor *D = P.createDescriptor(D: E, Ty: Ty.getTypePtr(), IsConst: Ty.isConstQualified(), |
| 5522 | /*IsTemporary=*/true); |
| 5523 | |
| 5524 | if (!D) |
| 5525 | return std::nullopt; |
| 5526 | |
| 5527 | Scope::Local Local = this->createLocal(D); |
| 5528 | VariableScope<Emitter> *S = VarScope; |
| 5529 | assert(S); |
| 5530 | // Attach to topmost scope. |
| 5531 | while (S->getParent()) |
| 5532 | S = S->getParent(); |
| 5533 | assert(S && !S->getParent()); |
| 5534 | S->addLocal(Local); |
| 5535 | return Local.Offset; |
| 5536 | } |
| 5537 | |
| 5538 | template <class Emitter> |
| 5539 | const RecordType *Compiler<Emitter>::getRecordTy(QualType Ty) { |
| 5540 | if (const PointerType *PT = dyn_cast<PointerType>(Val&: Ty)) |
| 5541 | return PT->getPointeeType()->getAsCanonical<RecordType>(); |
| 5542 | return Ty->getAsCanonical<RecordType>(); |
| 5543 | } |
| 5544 | |
| 5545 | template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) { |
| 5546 | if (const auto *RecordTy = getRecordTy(Ty)) |
| 5547 | return getRecord(RecordTy->getDecl()->getDefinitionOrSelf()); |
| 5548 | return nullptr; |
| 5549 | } |
| 5550 | |
| 5551 | template <class Emitter> |
| 5552 | Record *Compiler<Emitter>::getRecord(const RecordDecl *RD) { |
| 5553 | return P.getOrCreateRecord(RD); |
| 5554 | } |
| 5555 | |
| 5556 | template <class Emitter> |
| 5557 | const Function *Compiler<Emitter>::getFunction(const FunctionDecl *FD) { |
| 5558 | return Ctx.getOrCreateFunction(FuncDecl: FD); |
| 5559 | } |
| 5560 | |
| 5561 | template <class Emitter> |
| 5562 | bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) { |
| 5563 | assert(E); |
| 5564 | assert(!E->getType().isNull()); |
| 5565 | LocalScope<Emitter> RootScope(this, ScopeKind::FullExpression); |
| 5566 | |
| 5567 | auto maybeDestroyLocals = [&]() -> bool { |
| 5568 | if (DestroyToplevelScope) |
| 5569 | return RootScope.destroyLocals() && this->emitCheckAllocations(E); |
| 5570 | return this->emitCheckAllocations(E); |
| 5571 | }; |
| 5572 | |
| 5573 | // Void expressions. |
| 5574 | if (E->getType()->isVoidType()) { |
| 5575 | if (!visit(E)) |
| 5576 | return false; |
| 5577 | return this->emitRetVoid(E) && maybeDestroyLocals(); |
| 5578 | } |
| 5579 | |
| 5580 | // Expressions with a primitive return type. |
| 5581 | if (OptPrimType T = classify(E)) { |
| 5582 | if (!visit(E)) |
| 5583 | return false; |
| 5584 | |
| 5585 | return this->emitRet(*T, E) && maybeDestroyLocals(); |
| 5586 | } |
| 5587 | |
| 5588 | // Expressions with a composite return type. |
| 5589 | // For us, that means everything we don't |
| 5590 | // have a PrimType for. |
| 5591 | if (UnsignedOrNone LocalOffset = this->allocateLocal(Src: E)) { |
| 5592 | InitLinkScope<Emitter> ILS(this, InitLink::Temp(Offset: *LocalOffset)); |
| 5593 | if (!this->emitGetPtrLocal(*LocalOffset, E)) |
| 5594 | return false; |
| 5595 | |
| 5596 | if (!visitInitializer(E)) |
| 5597 | return false; |
| 5598 | // We are destroying the locals AFTER the Ret op. |
| 5599 | // The Ret op needs to copy the (alive) values, but the |
| 5600 | // destructors may still turn the entire expression invalid. |
| 5601 | return this->emitRetValue(E) && maybeDestroyLocals(); |
| 5602 | } |
| 5603 | |
| 5604 | return maybeDestroyLocals() && false; |
| 5605 | } |
| 5606 | |
| 5607 | template <class Emitter> |
| 5608 | bool Compiler<Emitter>::visitLValueExpr(const Expr *E, |
| 5609 | bool DestroyToplevelScope) { |
| 5610 | OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, |
| 5611 | /*NewInitializing=*/false, /*ToLValue=*/true); |
| 5612 | |
| 5613 | return this->visitExpr(E, DestroyToplevelScope); |
| 5614 | } |
| 5615 | |
| 5616 | template <class Emitter> |
| 5617 | VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD) { |
| 5618 | |
| 5619 | auto R = this->visitVarDecl(VD, Init: VD->getInit(), /*Toplevel=*/true); |
| 5620 | |
| 5621 | if (R.notCreated()) |
| 5622 | return R; |
| 5623 | |
| 5624 | if (R) |
| 5625 | return true; |
| 5626 | |
| 5627 | if (!R && Context::shouldBeGloballyIndexed(VD)) { |
| 5628 | if (auto GlobalIndex = P.getGlobal(VD)) { |
| 5629 | Block *GlobalBlock = P.getGlobal(Idx: *GlobalIndex); |
| 5630 | auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>(); |
| 5631 | |
| 5632 | GD.InitState = GlobalInitState::InitializerFailed; |
| 5633 | GlobalBlock->invokeDtor(); |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | return R; |
| 5638 | } |
| 5639 | |
| 5640 | /// Toplevel visitDeclAndReturn(). |
| 5641 | /// We get here from evaluateAsInitializer(). |
| 5642 | /// We need to evaluate the initializer and return its value. |
| 5643 | template <class Emitter> |
| 5644 | bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init, |
| 5645 | bool ConstantContext) { |
| 5646 | // We only create variables if we're evaluating in a constant context. |
| 5647 | // Otherwise, just evaluate the initializer and return it. |
| 5648 | if (!ConstantContext) { |
| 5649 | DeclScope<Emitter> LS(this, VD); |
| 5650 | if (!this->visit(E: Init)) |
| 5651 | return false; |
| 5652 | return this->emitRet(classify(Init).value_or(PT_Ptr), VD) && |
| 5653 | LS.destroyLocals() && this->emitCheckAllocations(VD); |
| 5654 | } |
| 5655 | |
| 5656 | LocalScope<Emitter> VDScope(this); |
| 5657 | if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true)) |
| 5658 | return false; |
| 5659 | |
| 5660 | OptPrimType VarT = classify(VD->getType()); |
| 5661 | bool IsReference = VD->getType()->isReferenceType(); |
| 5662 | if (Context::shouldBeGloballyIndexed(VD)) { |
| 5663 | auto GlobalIndex = P.getGlobal(VD); |
| 5664 | assert(GlobalIndex); // visitVarDecl() didn't return false. |
| 5665 | if (VarT) { |
| 5666 | if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD)) |
| 5667 | return false; |
| 5668 | } else { |
| 5669 | if (!this->emitGetPtrGlobal(*GlobalIndex, VD)) |
| 5670 | return false; |
| 5671 | } |
| 5672 | } else { |
| 5673 | auto Local = Locals.find(Val: VD); |
| 5674 | assert(Local != Locals.end()); // Same here. |
| 5675 | if (VarT) { |
| 5676 | if (IsReference) { |
| 5677 | if (!this->emitGetRefLocal(Local->second.Offset, VD)) |
| 5678 | return false; |
| 5679 | } else if (!this->emitGetLocal(*VarT, Local->second.Offset, VD)) |
| 5680 | return false; |
| 5681 | } else { |
| 5682 | if (!this->emitGetPtrLocal(Local->second.Offset, VD)) |
| 5683 | return false; |
| 5684 | } |
| 5685 | } |
| 5686 | |
| 5687 | // Return the value. |
| 5688 | if (!this->emitRet(VarT.value_or(PT: PT_Ptr), VD)) { |
| 5689 | // If the Ret above failed and this is a global variable. Mark it as |
| 5690 | // uninitialized, even if everything else succeeded. |
| 5691 | if (Context::shouldBeGloballyIndexed(VD)) { |
| 5692 | auto GlobalIndex = P.getGlobal(VD); |
| 5693 | assert(GlobalIndex); |
| 5694 | Block *GlobalBlock = P.getGlobal(Idx: *GlobalIndex); |
| 5695 | auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>(); |
| 5696 | |
| 5697 | GD.InitState = GlobalInitState::InitializerFailed; |
| 5698 | GlobalBlock->invokeDtor(); |
| 5699 | } |
| 5700 | return false; |
| 5701 | } |
| 5702 | |
| 5703 | return VDScope.destroyLocals() && this->emitCheckAllocations(VD); |
| 5704 | } |
| 5705 | |
| 5706 | template <class Emitter> |
| 5707 | VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD, |
| 5708 | const Expr *Init, |
| 5709 | bool Toplevel) { |
| 5710 | QualType VarTy = VD->getType(); |
| 5711 | // We don't know what to do with these, so just return false. |
| 5712 | if (VarTy.isNull()) |
| 5713 | return false; |
| 5714 | |
| 5715 | // This case is EvalEmitter-only. If we won't create any instructions for the |
| 5716 | // initializer anyway, don't bother creating the variable in the first place. |
| 5717 | if (!this->isActive()) |
| 5718 | return VarCreationState::NotCreated(); |
| 5719 | |
| 5720 | OptPrimType VarT = classify(VD->getType()); |
| 5721 | |
| 5722 | if (Init && Init->isValueDependent()) |
| 5723 | return false; |
| 5724 | |
| 5725 | if (Context::shouldBeGloballyIndexed(VD)) { |
| 5726 | auto checkDecl = [&]() -> bool { |
| 5727 | bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal(); |
| 5728 | return !NeedsOp || this->emitCheckDecl(VD, VD); |
| 5729 | }; |
| 5730 | |
| 5731 | DeclScope<Emitter> LocalScope(this, VD); |
| 5732 | UnsignedOrNone GlobalIndex = P.getGlobal(VD); |
| 5733 | if (GlobalIndex) { |
| 5734 | // The global was previously created but the initializer failed. |
| 5735 | if (!P.getGlobal(Idx: *GlobalIndex)->isInitialized()) |
| 5736 | return false; |
| 5737 | // We've already seen and initialized this global. |
| 5738 | if (P.isGlobalInitialized(Index: *GlobalIndex)) |
| 5739 | return checkDecl(); |
| 5740 | // The previous attempt at initialization might've been unsuccessful, |
| 5741 | // so let's try this one. |
| 5742 | } else if ((GlobalIndex = |
| 5743 | P.createGlobal(VD, Init, IsConstexprUnknown: VariablesAreConstexprUnknown))) { |
| 5744 | } else { |
| 5745 | return false; |
| 5746 | } |
| 5747 | if (!Init) |
| 5748 | return true; |
| 5749 | |
| 5750 | if (!checkDecl()) |
| 5751 | return false; |
| 5752 | |
| 5753 | if (VarT) { |
| 5754 | if (!this->visit(E: Init)) |
| 5755 | return false; |
| 5756 | |
| 5757 | return this->emitInitGlobal(*VarT, *GlobalIndex, VD); |
| 5758 | } |
| 5759 | |
| 5760 | if (!this->emitGetPtrGlobal(*GlobalIndex, Init)) |
| 5761 | return false; |
| 5762 | |
| 5763 | if (!this->emitStartInit(Init)) |
| 5764 | return false; |
| 5765 | |
| 5766 | if (!visitInitializer(E: Init)) |
| 5767 | return false; |
| 5768 | |
| 5769 | if (!this->emitEndInit(Init)) |
| 5770 | return false; |
| 5771 | |
| 5772 | return this->emitFinishInitGlobal(Init); |
| 5773 | } |
| 5774 | // Local variables. |
| 5775 | InitLinkScope<Emitter> ILS(this, InitLink::Decl(D: VD)); |
| 5776 | |
| 5777 | if (VarT) { |
| 5778 | unsigned Offset = this->allocateLocalPrimitive( |
| 5779 | Src: VD, Ty: *VarT, IsConst: VarTy.isConstQualified(), IsVolatile: VarTy.isVolatileQualified(), |
| 5780 | SC: ScopeKind::Block); |
| 5781 | |
| 5782 | if (!Init || Init->getType()->isVoidType()) |
| 5783 | return true; |
| 5784 | |
| 5785 | // If this is a toplevel declaration, create a scope for the |
| 5786 | // initializer. |
| 5787 | if (Toplevel) { |
| 5788 | LocalScope<Emitter> Scope(this); |
| 5789 | if (!this->visit(E: Init)) |
| 5790 | return false; |
| 5791 | return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals(); |
| 5792 | } |
| 5793 | if (!this->visit(E: Init)) |
| 5794 | return false; |
| 5795 | |
| 5796 | if (VarTy->isReferenceType()) { |
| 5797 | // [C++26][decl.ref] |
| 5798 | // The object designated by such a glvalue can be outside its lifetime |
| 5799 | // Because a null pointer value or a pointer past the end of an object |
| 5800 | // does not point to an object, a reference in a well-defined program |
| 5801 | // cannot refer to such things; |
| 5802 | assert(classifyPrim(VarTy) == PT_Ptr); |
| 5803 | if (!this->emitCheckRefInit(Init)) |
| 5804 | return false; |
| 5805 | } |
| 5806 | |
| 5807 | return this->emitSetLocal(*VarT, Offset, VD); |
| 5808 | } |
| 5809 | // Local composite variables. |
| 5810 | if (UnsignedOrNone Offset = |
| 5811 | this->allocateLocal(Src: VD, Ty: VarTy, SC: ScopeKind::Block)) { |
| 5812 | if (!Init) |
| 5813 | return true; |
| 5814 | |
| 5815 | if (!this->emitGetPtrLocal(*Offset, Init)) |
| 5816 | return false; |
| 5817 | |
| 5818 | return visitInitializerPop(E: Init); |
| 5819 | } |
| 5820 | return false; |
| 5821 | } |
| 5822 | |
| 5823 | template <class Emitter> |
| 5824 | bool Compiler<Emitter>::visitDtorCall(const VarDecl *VD, const APValue &Value) { |
| 5825 | assert(!canClassify(VD->getType())); |
| 5826 | |
| 5827 | DeclScope<Emitter> LocalScope(this, VD); |
| 5828 | // Create a local variable to use as the instance. |
| 5829 | QualType Ty = VD->getType(); |
| 5830 | Descriptor *D = |
| 5831 | P.createDescriptor(D: VD, Ty: Ty.getTypePtr(), /*IsConst=*/Ty.isConstQualified(), |
| 5832 | /*IsTemporary=*/false, /*IsMutable=*/false, |
| 5833 | /*IsVolatile=*/Ty.isVolatileQualified(), Init: nullptr); |
| 5834 | if (!D) |
| 5835 | return false; |
| 5836 | |
| 5837 | // FIXME: Would be nice if we didn't allocate the descriptor at all in this |
| 5838 | // case. |
| 5839 | if (D->hasTrivialDtor()) |
| 5840 | return true; |
| 5841 | |
| 5842 | Scope::Local Local = this->createLocal(D); |
| 5843 | Locals.insert(KV: {VD, Local}); |
| 5844 | VarScope->addForScopeKind(Local, ScopeKind::Block); |
| 5845 | |
| 5846 | if (!this->emitGetPtrLocal(Local.Offset, VD)) |
| 5847 | return false; |
| 5848 | |
| 5849 | if (!this->visitAPValueInitializer(Val: Value, Info: VD, T: Ty)) |
| 5850 | return false; |
| 5851 | |
| 5852 | return this->emitDestructionPop(Desc: D, Loc: VD); |
| 5853 | } |
| 5854 | |
| 5855 | class ParamFinder : public ConstDynamicRecursiveASTVisitor { |
| 5856 | public: |
| 5857 | llvm::SmallPtrSet<const ParmVarDecl *, 1> FoundParams; |
| 5858 | explicit ParamFinder() {} |
| 5859 | |
| 5860 | bool VisitDeclRefExpr(const DeclRefExpr *E) override { |
| 5861 | if (const auto *P = dyn_cast<ParmVarDecl>(Val: E->getDecl())) |
| 5862 | FoundParams.insert(Ptr: P); |
| 5863 | return true; |
| 5864 | } |
| 5865 | }; |
| 5866 | |
| 5867 | /// Evaluate the \p Condition as if it was in the body of \p Callee. |
| 5868 | /// Specifically, all the parameters of the callee are available to use |
| 5869 | /// for the condition, and their values are given by \p Args (and \p This). |
| 5870 | /// |
| 5871 | // Since this is a somewhat niche feature, we're abusing a few other mechanisms |
| 5872 | // to implement this. |
| 5873 | // |
| 5874 | // We don't create an actual function frame but instead register the parameters |
| 5875 | // as local variables. |
| 5876 | // |
| 5877 | // So we evaluate something like: |
| 5878 | // |
| 5879 | // bool thisfunc() { |
| 5880 | // auto Arg0 = Args[0]; |
| 5881 | // ... |
| 5882 | // return Condition; |
| 5883 | // } |
| 5884 | // |
| 5885 | template <class Emitter> |
| 5886 | bool Compiler<Emitter>::visitWithSubstitutions(const FunctionDecl *Callee, |
| 5887 | ArrayRef<const Expr *> Args, |
| 5888 | const Expr *This, |
| 5889 | const Expr *Condition) { |
| 5890 | // Instead of evaluating all parameters and trying to ignore failure, |
| 5891 | // we collect all the parameters used in the condition and only evaluate |
| 5892 | // those. Note that we still ignore failure in the loop below because the |
| 5893 | // failure might be inconsequential in the end, |
| 5894 | // e.g. in the case of `true || x`. |
| 5895 | ParamFinder PF; |
| 5896 | PF.TraverseStmt(S: Condition); |
| 5897 | |
| 5898 | LocalScope<Emitter> ArgScope(this); |
| 5899 | for (const ParmVarDecl *PVD : PF.FoundParams) { |
| 5900 | unsigned ParamIndex = 0; |
| 5901 | for (const ParmVarDecl *P : Callee->parameters()) { |
| 5902 | if (P == PVD) |
| 5903 | break; |
| 5904 | ++ParamIndex; |
| 5905 | } |
| 5906 | |
| 5907 | const Expr *Arg = Args[ParamIndex]; |
| 5908 | const ParmVarDecl *Param = Callee->getParamDecl(i: ParamIndex); |
| 5909 | if (OptPrimType ParamT = classify(Param->getType())) { |
| 5910 | unsigned ArgOffset = |
| 5911 | allocateLocalPrimitive(Src: Param, Ty: *ParamT, /*IsConst=*/true); |
| 5912 | if (!this->visit(E: Arg)) |
| 5913 | continue; |
| 5914 | if (!this->emitSetLocal(*ParamT, ArgOffset, Arg)) |
| 5915 | return false; |
| 5916 | } else { |
| 5917 | UnsignedOrNone ArgOffset = this->allocateLocal(Src: Param, Ty: Param->getType()); |
| 5918 | if (!ArgOffset) |
| 5919 | return false; |
| 5920 | if (!this->emitGetPtrLocal(*ArgOffset, Arg)) |
| 5921 | return false; |
| 5922 | if (!this->visitInitializerPop(E: Arg)) |
| 5923 | continue; |
| 5924 | } |
| 5925 | } |
| 5926 | |
| 5927 | if (This) { |
| 5928 | // We abuse the init stack for this and tell it to use |
| 5929 | // either a local variable or another decl for the This pointer. |
| 5930 | this->InitStackActive = true; |
| 5931 | |
| 5932 | if (This->getType()->isPointerType()) { |
| 5933 | // Nothing to do here, the evaluation will fail if the instance |
| 5934 | // pointer is used. |
| 5935 | } else if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: This)) { |
| 5936 | InitStack.push_back(Elt: InitLink::Decl(D: DRE->getDecl())); |
| 5937 | } else { |
| 5938 | assert(!canClassify(This->getType())); |
| 5939 | UnsignedOrNone ArgOffset = this->allocateLocal(Src: This, Ty: This->getType()); |
| 5940 | if (!ArgOffset) |
| 5941 | return false; |
| 5942 | if (!this->emitGetPtrLocal(*ArgOffset, This)) |
| 5943 | return false; |
| 5944 | if (!this->visitInitializerPop(E: This)) |
| 5945 | return false; |
| 5946 | this->InitStack.push_back(Elt: InitLink::Temp(Offset: *ArgOffset)); |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | // Destruction of the argument values is part of the callee frame, |
| 5951 | // so we simply ignore them here. |
| 5952 | this->VarScope = nullptr; |
| 5953 | |
| 5954 | LocalScope<Emitter> RetScope(this); |
| 5955 | if (!this->visit(E: Condition)) |
| 5956 | return false; |
| 5957 | if (!RetScope.destroyLocals()) |
| 5958 | return false; |
| 5959 | |
| 5960 | // Result of the condition should be on the stack. |
| 5961 | return this->emitRet(PT_Bool, Condition); |
| 5962 | } |
| 5963 | |
| 5964 | template <class Emitter> |
| 5965 | bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType, |
| 5966 | SourceInfo Info) { |
| 5967 | assert(!Val.isIndeterminate() && "Needs to be checked before" ); |
| 5968 | assert(!DiscardResult); |
| 5969 | if (Val.isInt()) |
| 5970 | return this->emitConst(Val.getInt(), ValType, Info); |
| 5971 | if (Val.isFloat()) |
| 5972 | return this->emitFloat(F: Val.getFloat(), Info); |
| 5973 | |
| 5974 | if (Val.isMemberPointer()) { |
| 5975 | if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) { |
| 5976 | if (!this->emitGetMemberPtr(MemberDecl, Info)) |
| 5977 | return false; |
| 5978 | |
| 5979 | bool IsDerived = Val.isMemberPointerToDerivedMember(); |
| 5980 | // Apply the member pointer path. |
| 5981 | for (const CXXRecordDecl *PathEntry : Val.getMemberPointerPath()) { |
| 5982 | if (!this->emitCopyMemberPtrPath(PathEntry, IsDerived, Info)) |
| 5983 | return false; |
| 5984 | } |
| 5985 | |
| 5986 | return true; |
| 5987 | } |
| 5988 | return this->emitNullMemberPtr(0, nullptr, Info); |
| 5989 | } |
| 5990 | |
| 5991 | if (Val.isLValue()) { |
| 5992 | if (Val.isNullPointer()) |
| 5993 | return this->emitNull(ValType, 0, nullptr, Info); |
| 5994 | |
| 5995 | APValue::LValueBase Base = Val.getLValueBase(); |
| 5996 | QualType EntryType; |
| 5997 | if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>()) { |
| 5998 | if (!this->visit(E: BaseExpr)) |
| 5999 | return false; |
| 6000 | EntryType = BaseExpr->getType(); |
| 6001 | } else if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) { |
| 6002 | if (!this->visitDeclRef(D: VD, E: Info.asExpr())) |
| 6003 | return false; |
| 6004 | EntryType = VD->getType(); |
| 6005 | } else if (Base.is<TypeInfoLValue>()) { |
| 6006 | // The type_info object for the type the lvalue describes. |
| 6007 | const Type *OperandType = Base.get<TypeInfoLValue>() |
| 6008 | .getType() |
| 6009 | ->getCanonicalTypeUnqualified() |
| 6010 | .getTypePtr(); |
| 6011 | EntryType = Base.getTypeInfoType(); |
| 6012 | if (!this->emitGetTypeid(OperandType, EntryType.getTypePtr(), Info)) |
| 6013 | return false; |
| 6014 | } else if (!Base) { |
| 6015 | // An integer cast to a pointer. |
| 6016 | const Expr *E = Info.asExpr(); |
| 6017 | if (!E) |
| 6018 | return false; |
| 6019 | QualType PtrType = E->isGLValue() |
| 6020 | ? Ctx.getASTContext().getPointerType(T: E->getType()) |
| 6021 | : E->getType(); |
| 6022 | uint64_t Offset = Val.getLValueOffset().getQuantity(); |
| 6023 | if (!this->emitConstUint64(Offset, Info)) |
| 6024 | return false; |
| 6025 | if (!this->emitGetIntPtrUint64(PtrType.getTypePtr(), Info)) |
| 6026 | return false; |
| 6027 | EntryType = PtrType->getPointeeType(); |
| 6028 | } else { |
| 6029 | return false; |
| 6030 | } |
| 6031 | |
| 6032 | if (Val.hasLValuePath()) { |
| 6033 | for (auto &Entry : Val.getLValuePath()) { |
| 6034 | if (EntryType->isArrayType()) { |
| 6035 | uint64_t Index = Entry.getAsArrayIndex(); |
| 6036 | QualType ElemType = |
| 6037 | EntryType->getAsArrayTypeUnsafe()->getElementType(); |
| 6038 | if (!this->emitConst(Index, PT_Uint64, Info)) |
| 6039 | return false; |
| 6040 | if (!this->emitArrayElemPtrPop(PT_Uint64, Info)) |
| 6041 | return false; |
| 6042 | EntryType = ElemType; |
| 6043 | } else { |
| 6044 | assert(EntryType->isRecordType()); |
| 6045 | const Record *EntryRecord = getRecord(EntryType); |
| 6046 | if (!EntryRecord) |
| 6047 | return false; |
| 6048 | |
| 6049 | const Decl *BaseOrMember = Entry.getAsBaseOrMember().getPointer(); |
| 6050 | if (const auto *FD = dyn_cast<FieldDecl>(Val: BaseOrMember)) { |
| 6051 | unsigned EntryOffset = EntryRecord->getField(FD)->Offset; |
| 6052 | if (!this->emitGetPtrFieldPop(EntryOffset, Info)) |
| 6053 | return false; |
| 6054 | EntryType = FD->getType(); |
| 6055 | } else { |
| 6056 | const auto *Base = cast<CXXRecordDecl>(Val: BaseOrMember); |
| 6057 | if (const Record::Base *B = EntryRecord->getBaseOrNull(RD: Base)) { |
| 6058 | if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, Info)) |
| 6059 | return false; |
| 6060 | } else { |
| 6061 | // Must be a virtual base. |
| 6062 | assert(EntryRecord->findVirtualBase(Base)); |
| 6063 | if (!this->emitGetPtrVirtBasePop(Base, Info)) |
| 6064 | return false; |
| 6065 | } |
| 6066 | EntryType = Ctx.getASTContext().getCanonicalTagType(TD: Base); |
| 6067 | } |
| 6068 | } |
| 6069 | } |
| 6070 | } |
| 6071 | |
| 6072 | if (isPtrType(T: ValType)) |
| 6073 | return true; |
| 6074 | |
| 6075 | // A pointer cast to an integer is stored as an lvalue; cast it the same |
| 6076 | // way the source expression does. |
| 6077 | if (ValType == PT_IntAP || ValType == PT_IntAPS) { |
| 6078 | const Expr *E = Info.asExpr(); |
| 6079 | if (!E) |
| 6080 | return false; |
| 6081 | uint32_t BitWidth = Ctx.getBitWidth(T: E->getType()); |
| 6082 | return ValType == PT_IntAP |
| 6083 | ? this->emitCastPointerIntegralAP(BitWidth, Info) |
| 6084 | : this->emitCastPointerIntegralAPS(BitWidth, Info); |
| 6085 | } |
| 6086 | return this->emitCastPointerIntegral(ValType, Info); |
| 6087 | } |
| 6088 | |
| 6089 | return false; |
| 6090 | } |
| 6091 | |
| 6092 | template <class Emitter> |
| 6093 | bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val, |
| 6094 | SourceInfo Info, QualType T, |
| 6095 | bool IsCompleteClass) { |
| 6096 | if (Val.isStruct()) { |
| 6097 | const Record *R = this->getRecord(T); |
| 6098 | assert(R); |
| 6099 | |
| 6100 | assert(R->getNumBases() == Val.getStructNumBases()); |
| 6101 | if (IsCompleteClass) |
| 6102 | assert(R->getNumVirtualBases() == Val.getStructNumVirtualBases()); |
| 6103 | |
| 6104 | for (unsigned I = 0, N = Val.getStructNumBases(); I != N; ++I) { |
| 6105 | const APValue &B = Val.getStructBase(i: I); |
| 6106 | if (B.isIndeterminate()) |
| 6107 | continue; |
| 6108 | const Record::Base *RB = R->getBase(I); |
| 6109 | QualType BaseType = Ctx.getASTContext().getCanonicalTagType(TD: RB->Decl); |
| 6110 | |
| 6111 | if (!this->emitGetPtrBase(RB->Offset, Info)) |
| 6112 | return false; |
| 6113 | if (!this->visitAPValueInitializer(Val: B, Info, T: BaseType, |
| 6114 | /*IsCompleteClass=*/false)) |
| 6115 | return false; |
| 6116 | if (!this->emitFinishInitPop(Info)) |
| 6117 | return false; |
| 6118 | } |
| 6119 | |
| 6120 | for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) { |
| 6121 | const APValue &F = Val.getStructField(i: I); |
| 6122 | if (F.isIndeterminate()) |
| 6123 | continue; |
| 6124 | const Record::Field *RF = R->getField(I); |
| 6125 | QualType FieldType = RF->Decl->getType(); |
| 6126 | // Fields. |
| 6127 | if (OptPrimType PT = RF->T) { |
| 6128 | if (!this->visitAPValue(Val: F, ValType: *PT, Info)) |
| 6129 | return false; |
| 6130 | if (!this->emitInitField(*PT, RF->Offset, Info)) |
| 6131 | return false; |
| 6132 | } else { |
| 6133 | if (!this->emitGetPtrField(RF->Offset, Info)) |
| 6134 | return false; |
| 6135 | if (!this->visitAPValueInitializer(Val: F, Info, T: FieldType)) |
| 6136 | return false; |
| 6137 | if (!this->emitFinishInitPop(Info)) |
| 6138 | return false; |
| 6139 | } |
| 6140 | } |
| 6141 | |
| 6142 | // Virtual Bases. |
| 6143 | if (IsCompleteClass) { |
| 6144 | for (unsigned I = 0, N = Val.getStructNumVirtualBases(); I != N; ++I) { |
| 6145 | const APValue &B = Val.getStructVirtualBase(i: I); |
| 6146 | if (B.isIndeterminate()) |
| 6147 | continue; |
| 6148 | const Record::Base *RB = R->getVirtualBase(I); |
| 6149 | QualType BaseType = Ctx.getASTContext().getCanonicalTagType(TD: RB->Decl); |
| 6150 | |
| 6151 | if (!this->emitGetPtrVirtBase(cast<CXXRecordDecl>(Val: RB->R->getDecl()), |
| 6152 | Info)) |
| 6153 | return false; |
| 6154 | if (!this->visitAPValueInitializer(Val: B, Info, T: BaseType, |
| 6155 | /*IsCompleteClass=*/false)) |
| 6156 | return false; |
| 6157 | if (!this->emitFinishInitPop(Info)) |
| 6158 | return false; |
| 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | return true; |
| 6163 | } |
| 6164 | if (Val.isUnion()) { |
| 6165 | const FieldDecl *UnionField = Val.getUnionField(); |
| 6166 | if (!UnionField) |
| 6167 | return true; |
| 6168 | const Record *R = this->getRecord(T); |
| 6169 | assert(R); |
| 6170 | const APValue &F = Val.getUnionValue(); |
| 6171 | if (F.isIndeterminate()) |
| 6172 | return true; |
| 6173 | const Record::Field *RF = R->getField(FD: UnionField); |
| 6174 | QualType FieldType = RF->Decl->getType(); |
| 6175 | |
| 6176 | if (OptPrimType PT = RF->T) { |
| 6177 | if (!this->visitAPValue(Val: F, ValType: *PT, Info)) |
| 6178 | return false; |
| 6179 | if (RF->isBitField()) |
| 6180 | return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(), |
| 6181 | Info); |
| 6182 | return this->emitInitFieldActivate(*PT, RF->Offset, Info); |
| 6183 | } |
| 6184 | |
| 6185 | if (!this->emitGetPtrField(RF->Offset, Info)) |
| 6186 | return false; |
| 6187 | if (!this->emitActivate(Info)) |
| 6188 | return false; |
| 6189 | if (!this->visitAPValueInitializer(Val: F, Info, T: FieldType)) |
| 6190 | return false; |
| 6191 | return this->emitPopPtr(Info); |
| 6192 | } |
| 6193 | if (Val.isArray()) { |
| 6194 | unsigned InitializedElems = Val.getArrayInitializedElts(); |
| 6195 | const auto *ArrType = T->getAsArrayTypeUnsafe(); |
| 6196 | QualType ElemType = ArrType->getElementType(); |
| 6197 | OptPrimType ElemT = classify(ElemType); |
| 6198 | |
| 6199 | for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) { |
| 6200 | const APValue &Elem = A >= InitializedElems |
| 6201 | ? Val.getArrayFiller() |
| 6202 | : Val.getArrayInitializedElt(I: A); |
| 6203 | if (Elem.isIndeterminate()) |
| 6204 | continue; |
| 6205 | |
| 6206 | if (ElemT) { |
| 6207 | if (!this->visitAPValue(Val: Elem, ValType: *ElemT, Info)) |
| 6208 | return false; |
| 6209 | if (!this->emitInitElem(*ElemT, A, Info)) |
| 6210 | return false; |
| 6211 | } else { |
| 6212 | if (!this->emitConstUint32(A, Info)) |
| 6213 | return false; |
| 6214 | if (!this->emitArrayElemPtrUint32(Info)) |
| 6215 | return false; |
| 6216 | if (!this->visitAPValueInitializer(Val: Elem, Info, T: ElemType)) |
| 6217 | return false; |
| 6218 | if (!this->emitPopPtr(Info)) |
| 6219 | return false; |
| 6220 | } |
| 6221 | } |
| 6222 | return true; |
| 6223 | } |
| 6224 | // TODO: Other types. |
| 6225 | |
| 6226 | return false; |
| 6227 | } |
| 6228 | |
| 6229 | template <class Emitter> |
| 6230 | bool Compiler<Emitter>::registerRedecl(const VarDecl *VD, const APValue &Val) { |
| 6231 | if (P.getGlobal(VD)) |
| 6232 | return true; |
| 6233 | |
| 6234 | UnsignedOrNone GlobalIndex = P.createGlobal(VD, /*Init=*/nullptr); |
| 6235 | if (!GlobalIndex) { |
| 6236 | llvm_unreachable("Why didn't that work?" ); |
| 6237 | } |
| 6238 | |
| 6239 | assert(canClassify(VD->getType()) && |
| 6240 | "registerRedecl should only be called with primitive values" ); |
| 6241 | |
| 6242 | PrimType T = classifyPrim(VD->getType()); |
| 6243 | if (!visitAPValue(Val, ValType: T, Info: VD)) |
| 6244 | return false; |
| 6245 | return this->emitInitGlobal(T, *GlobalIndex, {}); |
| 6246 | } |
| 6247 | |
| 6248 | template <class Emitter> |
| 6249 | bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E, |
| 6250 | unsigned BuiltinID) { |
| 6251 | const ASTContext &ASTCtx = Ctx.getASTContext(); |
| 6252 | |
| 6253 | // BuiltinID is the raw ID baked into the bytecode. The "is constant |
| 6254 | // evaluated" gate needs the raw ID so that auxiliary-target IDs resolve into |
| 6255 | // the correct (aux-target) builtin records. |
| 6256 | if (!Ctx.getASTContext().BuiltinInfo.isConstantEvaluated(ID: BuiltinID)) |
| 6257 | return this->emitInvalid(E); |
| 6258 | |
| 6259 | // Convert an auxiliary x86 target builtin ID to its canonical X86::BI* value |
| 6260 | // so the target-specific cases below (and the handlers they call) match. This |
| 6261 | // is a cheap integer operation (a single comparison for the common, |
| 6262 | // target-independent case); we deliberately avoid re-deriving the ID from the |
| 6263 | // call expression, which is comparatively slow. |
| 6264 | BuiltinID = ConvertBuiltinIDToX86BuiltinID(Ctx: ASTCtx, BuiltinID); |
| 6265 | |
| 6266 | if (BuiltinID == Builtin::BI__builtin_constant_p) { |
| 6267 | // Void argument is always invalid and harder to handle later. |
| 6268 | if (E->getArg(Arg: 0)->getType()->isVoidType()) { |
| 6269 | if (DiscardResult) |
| 6270 | return true; |
| 6271 | return this->emitConst(0, E); |
| 6272 | } |
| 6273 | |
| 6274 | if (!this->emitStartSpeculation(E)) |
| 6275 | return false; |
| 6276 | LabelTy EndLabel = this->getLabel(); |
| 6277 | if (!this->speculate(E, EndLabel)) |
| 6278 | return false; |
| 6279 | if (!this->emitEndSpeculation(E)) |
| 6280 | return false; |
| 6281 | this->fallthrough(EndLabel); |
| 6282 | if (DiscardResult) |
| 6283 | return this->emitPop(classifyPrim(E), E); |
| 6284 | return true; |
| 6285 | } |
| 6286 | |
| 6287 | // For these, we're expected to ultimately return an APValue pointing |
| 6288 | // to the CallExpr. This is needed to get the correct codegen. |
| 6289 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 6290 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString || |
| 6291 | BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant || |
| 6292 | BuiltinID == Builtin::BI__builtin_function_start) { |
| 6293 | if (DiscardResult) |
| 6294 | return true; |
| 6295 | return this->emitDummyPtr(D: E, E); |
| 6296 | } |
| 6297 | |
| 6298 | QualType ReturnType = E->getType(); |
| 6299 | OptPrimType ReturnT = classify(E); |
| 6300 | |
| 6301 | // Non-primitive return type. Prepare storage. |
| 6302 | if (!Initializing && !ReturnT && !ReturnType->isVoidType()) { |
| 6303 | UnsignedOrNone LocalIndex = allocateLocal(Src: E); |
| 6304 | if (!LocalIndex) |
| 6305 | return false; |
| 6306 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 6307 | return false; |
| 6308 | } |
| 6309 | |
| 6310 | // Prepare function arguments including special cases. |
| 6311 | switch (BuiltinID) { |
| 6312 | case Builtin::BI__builtin_object_size: |
| 6313 | case Builtin::BI__builtin_dynamic_object_size: { |
| 6314 | assert(E->getNumArgs() == 2); |
| 6315 | const Expr *Arg0 = E->getArg(Arg: 0); |
| 6316 | if (Arg0->isGLValue()) { |
| 6317 | if (!this->visit(E: Arg0)) |
| 6318 | return false; |
| 6319 | |
| 6320 | } else { |
| 6321 | if (!this->visitAsLValue(E: ignorePointerCastsAndParens(E: Arg0))) |
| 6322 | return false; |
| 6323 | } |
| 6324 | if (!this->visit(E: E->getArg(Arg: 1))) |
| 6325 | return false; |
| 6326 | |
| 6327 | } break; |
| 6328 | case Builtin::BI__assume: |
| 6329 | case Builtin::BI__builtin_assume: |
| 6330 | // Argument is not evaluated. |
| 6331 | break; |
| 6332 | case Builtin::BI__atomic_is_lock_free: |
| 6333 | case Builtin::BI__atomic_always_lock_free: { |
| 6334 | assert(E->getNumArgs() == 2); |
| 6335 | if (!this->visit(E: E->getArg(Arg: 0))) |
| 6336 | return false; |
| 6337 | if (!this->visitAsLValue(E: E->getArg(Arg: 1))) |
| 6338 | return false; |
| 6339 | } break; |
| 6340 | |
| 6341 | default: |
| 6342 | if (!Context::isUnevaluatedBuiltin(ID: BuiltinID)) { |
| 6343 | // Put arguments on the stack. |
| 6344 | for (const auto *Arg : E->arguments()) { |
| 6345 | if (!this->visit(E: Arg)) |
| 6346 | return false; |
| 6347 | } |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | if (!this->emitCallBI(E, BuiltinID, E)) |
| 6352 | return false; |
| 6353 | |
| 6354 | if (DiscardResult && !ReturnType->isVoidType()) |
| 6355 | return this->emitPop(ReturnT.value_or(PT: PT_Ptr), E); |
| 6356 | |
| 6357 | return true; |
| 6358 | } |
| 6359 | |
| 6360 | static bool isTrivialMemoryOperation(const CXXMethodDecl *MD) { |
| 6361 | if (!MD || !MD->isDefaulted()) |
| 6362 | return false; |
| 6363 | if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator()) |
| 6364 | return false; |
| 6365 | return MD->getParent()->isUnion() || |
| 6366 | (MD->isTrivial() && isReadByLvalueToRvalueConversion(RD: MD->getParent())); |
| 6367 | } |
| 6368 | |
| 6369 | template <class Emitter> |
| 6370 | bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) { |
| 6371 | if (E->containsErrors()) |
| 6372 | return false; |
| 6373 | const FunctionDecl *FuncDecl = E->getDirectCallee(); |
| 6374 | |
| 6375 | if (FuncDecl) { |
| 6376 | if (unsigned BuiltinID = FuncDecl->getBuiltinID()) |
| 6377 | return VisitBuiltinCallExpr(E, BuiltinID); |
| 6378 | |
| 6379 | // Calls to replaceable operator new/operator delete. |
| 6380 | if (FuncDecl->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
| 6381 | if (FuncDecl->getDeclName().isAnyOperatorNew()) |
| 6382 | return VisitBuiltinCallExpr(E, BuiltinID: Builtin::BI__builtin_operator_new); |
| 6383 | assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 6384 | FuncDecl->getDeclName().getCXXOverloadedOperator() == |
| 6385 | OO_Array_Delete); |
| 6386 | return VisitBuiltinCallExpr(E, BuiltinID: Builtin::BI__builtin_operator_delete); |
| 6387 | } |
| 6388 | |
| 6389 | // Explicit calls to trivial destructors |
| 6390 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(Val: FuncDecl); |
| 6391 | DD && DD->isTrivial()) { |
| 6392 | const auto *MemberCall = cast<CXXMemberCallExpr>(Val: E); |
| 6393 | if (!this->visit(E: MemberCall->getImplicitObjectArgument())) |
| 6394 | return false; |
| 6395 | return this->emitCheckDestruction(E) && this->emitEndLifetime(E) && |
| 6396 | this->emitPopPtr(E); |
| 6397 | } |
| 6398 | } |
| 6399 | |
| 6400 | LocalScope<Emitter> CallScope(this, ScopeKind::Call); |
| 6401 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
| 6402 | bool ActivateLHS = false; |
| 6403 | |
| 6404 | // Emit a special op for trivial copy/move operators. |
| 6405 | if (isTrivialMemoryOperation(MD: dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl))) { |
| 6406 | const Function *Func = getFunction(FD: FuncDecl); |
| 6407 | if (!Func) |
| 6408 | return false; |
| 6409 | |
| 6410 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E); |
| 6411 | OCE && OCE->isAssignmentOp()) { |
| 6412 | const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl(); |
| 6413 | ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor(); |
| 6414 | } |
| 6415 | if (const auto *MCE = dyn_cast<CXXMemberCallExpr>(Val: E)) |
| 6416 | if (!this->visit(E: MCE->getImplicitObjectArgument())) |
| 6417 | return false; |
| 6418 | |
| 6419 | if (!this->visitCallArgs(Args, FuncDecl, /*ActivateLHS=*/Activate: ActivateLHS, |
| 6420 | IsOperatorCall: isa<CXXOperatorCallExpr>(Val: E))) |
| 6421 | return false; |
| 6422 | |
| 6423 | if (!this->emitTrivialCopy(ActivateLHS, Func, E)) |
| 6424 | return false; |
| 6425 | |
| 6426 | if (!DiscardResult) |
| 6427 | return CallScope.destroyLocals(); |
| 6428 | return this->emitPopPtr(E) && CallScope.destroyLocals(); |
| 6429 | } |
| 6430 | |
| 6431 | QualType ReturnType = E->getCallReturnType(Ctx: Ctx.getASTContext()); |
| 6432 | OptPrimType T = classify(ReturnType); |
| 6433 | bool HasRVO = !ReturnType->isVoidType() && !T; |
| 6434 | |
| 6435 | if (HasRVO) { |
| 6436 | if (DiscardResult) { |
| 6437 | // If we need to discard the return value but the function returns its |
| 6438 | // value via an RVO pointer, we need to create one such pointer just |
| 6439 | // for this call. |
| 6440 | if (UnsignedOrNone LocalIndex = allocateLocal(Src: E)) { |
| 6441 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 6442 | return false; |
| 6443 | } |
| 6444 | } else { |
| 6445 | // We need the result. Prepare a pointer to return or |
| 6446 | // dup the current one. |
| 6447 | if (!Initializing) { |
| 6448 | if (UnsignedOrNone LocalIndex = allocateLocal(Src: E)) { |
| 6449 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 6450 | return false; |
| 6451 | } |
| 6452 | } |
| 6453 | if (!this->emitDupPtr(E)) |
| 6454 | return false; |
| 6455 | } |
| 6456 | } |
| 6457 | |
| 6458 | const Expr *ReversedArgs[2]; |
| 6459 | bool IsAssignmentOperatorCall = false; |
| 6460 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E); |
| 6461 | OCE && OCE->isAssignmentOp()) { |
| 6462 | // Just like with regular assignments, we need to special-case assignment |
| 6463 | // operators here and evaluate the RHS (the second arg) before the LHS (the |
| 6464 | // first arg). We fix this by using a Flip op later. |
| 6465 | assert(Args.size() == 2); |
| 6466 | const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl(); |
| 6467 | ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor(); |
| 6468 | IsAssignmentOperatorCall = true; |
| 6469 | ReversedArgs[0] = Args[1]; |
| 6470 | ReversedArgs[1] = Args[0]; |
| 6471 | Args = ReversedArgs; |
| 6472 | } |
| 6473 | |
| 6474 | // Calling a static operator will still |
| 6475 | // pass the instance, but we don't need it. |
| 6476 | // Discard it here. |
| 6477 | if (isa<CXXOperatorCallExpr>(Val: E)) { |
| 6478 | if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: FuncDecl); |
| 6479 | MD && MD->isStatic()) { |
| 6480 | if (!this->discard(E: E->getArg(Arg: 0))) |
| 6481 | return false; |
| 6482 | // Drop first arg. |
| 6483 | Args = Args.drop_front(); |
| 6484 | } |
| 6485 | } |
| 6486 | |
| 6487 | bool Devirtualized = false; |
| 6488 | UnsignedOrNone CalleeOffset = std::nullopt; |
| 6489 | // Add the (optional, implicit) This pointer. |
| 6490 | if (const auto *MC = dyn_cast<CXXMemberCallExpr>(Val: E)) { |
| 6491 | if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) { |
| 6492 | // If we end up creating a CallPtr op for this, we need the base of the |
| 6493 | // member pointer as the instance pointer, and later extract the function |
| 6494 | // decl as the function pointer. |
| 6495 | const Expr *Callee = E->getCallee(); |
| 6496 | CalleeOffset = |
| 6497 | this->allocateLocalPrimitive(Src: Callee, Ty: PT_MemberPtr, /*IsConst=*/true); |
| 6498 | if (!this->visit(E: Callee)) |
| 6499 | return false; |
| 6500 | if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E)) |
| 6501 | return false; |
| 6502 | if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E)) |
| 6503 | return false; |
| 6504 | if (!this->emitGetMemberPtrBase(E)) |
| 6505 | return false; |
| 6506 | } else { |
| 6507 | const auto *InstancePtr = MC->getImplicitObjectArgument(); |
| 6508 | if (isa_and_nonnull<CXXDestructorDecl>(Val: CompilingFunction) || |
| 6509 | isa_and_nonnull<CXXConstructorDecl>(Val: CompilingFunction)) { |
| 6510 | const auto *Stripped = stripCheckedDerivedToBaseCasts(E: InstancePtr); |
| 6511 | if (isa<CXXThisExpr>(Val: Stripped)) { |
| 6512 | FuncDecl = |
| 6513 | cast<CXXMethodDecl>(Val: FuncDecl)->getCorrespondingMethodInClass( |
| 6514 | RD: Stripped->getType()->getPointeeType()->getAsCXXRecordDecl()); |
| 6515 | Devirtualized = true; |
| 6516 | if (!this->visit(E: Stripped)) |
| 6517 | return false; |
| 6518 | } else { |
| 6519 | if (!this->visit(E: InstancePtr)) |
| 6520 | return false; |
| 6521 | } |
| 6522 | } else { |
| 6523 | if (!this->visit(E: InstancePtr)) |
| 6524 | return false; |
| 6525 | } |
| 6526 | } |
| 6527 | } else if (const auto *PD = |
| 6528 | dyn_cast<CXXPseudoDestructorExpr>(Val: E->getCallee())) { |
| 6529 | if (!this->emitCheckPseudoDtor(E)) |
| 6530 | return false; |
| 6531 | const Expr *Base = PD->getBase(); |
| 6532 | // E.g. `using T = int; 0.~T();`. |
| 6533 | if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr) |
| 6534 | return this->discard(E: Base); |
| 6535 | if (!this->visit(E: Base)) |
| 6536 | return false; |
| 6537 | return this->emitPseudoDtor(E); |
| 6538 | } else if (!FuncDecl) { |
| 6539 | const Expr *Callee = E->getCallee(); |
| 6540 | CalleeOffset = |
| 6541 | this->allocateLocalPrimitive(Src: Callee, Ty: PT_Ptr, /*IsConst=*/true); |
| 6542 | if (!this->visit(E: Callee)) |
| 6543 | return false; |
| 6544 | if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E)) |
| 6545 | return false; |
| 6546 | } |
| 6547 | |
| 6548 | if (!this->visitCallArgs(Args, FuncDecl, Activate: ActivateLHS, |
| 6549 | IsOperatorCall: isa<CXXOperatorCallExpr>(Val: E))) |
| 6550 | return false; |
| 6551 | |
| 6552 | // Undo the argument reversal we did earlier. |
| 6553 | if (IsAssignmentOperatorCall) { |
| 6554 | assert(Args.size() == 2); |
| 6555 | PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr); |
| 6556 | PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr); |
| 6557 | if (!this->emitFlip(Arg2T, Arg1T, E)) |
| 6558 | return false; |
| 6559 | } |
| 6560 | |
| 6561 | if (FuncDecl) { |
| 6562 | const Function *Func = getFunction(FD: FuncDecl); |
| 6563 | if (!Func) |
| 6564 | return false; |
| 6565 | |
| 6566 | // In error cases, the function may be called with fewer arguments than |
| 6567 | // parameters. |
| 6568 | if (E->getNumArgs() < Func->getNumWrittenParams()) |
| 6569 | return false; |
| 6570 | |
| 6571 | assert(HasRVO == Func->hasRVO()); |
| 6572 | |
| 6573 | bool HasQualifier = false; |
| 6574 | if (const auto *ME = dyn_cast<MemberExpr>(Val: E->getCallee())) |
| 6575 | HasQualifier = ME->hasQualifier(); |
| 6576 | |
| 6577 | bool IsVirtual = false; |
| 6578 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FuncDecl)) |
| 6579 | IsVirtual = !Devirtualized && MD->isVirtual(); |
| 6580 | |
| 6581 | // In any case call the function. The return value will end up on the stack |
| 6582 | // and if the function has RVO, we already have the pointer on the stack to |
| 6583 | // write the result into. |
| 6584 | if (IsVirtual && !HasQualifier) { |
| 6585 | uint32_t VarArgSize = 0; |
| 6586 | unsigned NumParams = |
| 6587 | Func->getNumWrittenParams() + |
| 6588 | (isa<CXXOperatorCallExpr>(Val: E) && Func->hasImplicitThisPointer()); |
| 6589 | for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) |
| 6590 | VarArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr))); |
| 6591 | |
| 6592 | if (!this->emitCallVirt(Func, VarArgSize, E)) |
| 6593 | return false; |
| 6594 | } else if (Func->isVariadic()) { |
| 6595 | uint32_t VarArgSize = 0; |
| 6596 | unsigned NumParams = |
| 6597 | Func->getNumWrittenParams() + |
| 6598 | (isa<CXXOperatorCallExpr>(Val: E) && Func->hasImplicitThisPointer()); |
| 6599 | for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) |
| 6600 | VarArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr))); |
| 6601 | if (!this->emitCallVar(Func, VarArgSize, E)) |
| 6602 | return false; |
| 6603 | } else { |
| 6604 | if (!this->emitCall(Func, 0, E)) |
| 6605 | return false; |
| 6606 | } |
| 6607 | } else { |
| 6608 | // Indirect call. Visit the callee, which will leave a FunctionPointer on |
| 6609 | // the stack. Cleanup of the returned value if necessary will be done after |
| 6610 | // the function call completed. |
| 6611 | |
| 6612 | // Sum the size of all args from the call expr. |
| 6613 | uint32_t ArgSize = 0; |
| 6614 | for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) |
| 6615 | ArgSize += align(primSize(classify(E->getArg(Arg: I)).value_or(PT_Ptr))); |
| 6616 | |
| 6617 | // Get the callee, either from a member pointer or function pointer saved in |
| 6618 | // CalleeOffset. |
| 6619 | if (isa<CXXMemberCallExpr>(Val: E) && CalleeOffset) { |
| 6620 | if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E)) |
| 6621 | return false; |
| 6622 | if (!this->emitGetMemberPtrDecl(E)) |
| 6623 | return false; |
| 6624 | } else { |
| 6625 | if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E)) |
| 6626 | return false; |
| 6627 | } |
| 6628 | if (!this->emitCallPtr(ArgSize, E, E)) |
| 6629 | return false; |
| 6630 | } |
| 6631 | |
| 6632 | // Cleanup for discarded return values. |
| 6633 | if (DiscardResult && !ReturnType->isVoidType() && T) |
| 6634 | return this->emitPop(*T, E) && CallScope.destroyLocals(); |
| 6635 | |
| 6636 | return CallScope.destroyLocals(); |
| 6637 | } |
| 6638 | |
| 6639 | template <class Emitter> |
| 6640 | bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
| 6641 | SourceLocScope<Emitter> SLS(this, E); |
| 6642 | |
| 6643 | return this->delegate(E: E->getExpr()); |
| 6644 | } |
| 6645 | |
| 6646 | template <class Emitter> |
| 6647 | bool Compiler<Emitter>::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
| 6648 | SourceLocScope<Emitter> SLS(this, E); |
| 6649 | |
| 6650 | return this->delegate(E: E->getExpr()); |
| 6651 | } |
| 6652 | |
| 6653 | template <class Emitter> |
| 6654 | bool Compiler<Emitter>::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| 6655 | if (DiscardResult) |
| 6656 | return true; |
| 6657 | |
| 6658 | return this->emitConstBool(E->getValue(), E); |
| 6659 | } |
| 6660 | |
| 6661 | template <class Emitter> |
| 6662 | bool Compiler<Emitter>::VisitCXXNullPtrLiteralExpr( |
| 6663 | const CXXNullPtrLiteralExpr *E) { |
| 6664 | if (DiscardResult) |
| 6665 | return true; |
| 6666 | |
| 6667 | uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(QT: E->getType()); |
| 6668 | return this->emitNullPtr(Val, nullptr, E); |
| 6669 | } |
| 6670 | |
| 6671 | template <class Emitter> |
| 6672 | bool Compiler<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) { |
| 6673 | if (DiscardResult) |
| 6674 | return true; |
| 6675 | |
| 6676 | assert(E->getType()->isIntegerType()); |
| 6677 | |
| 6678 | PrimType T = classifyPrim(E->getType()); |
| 6679 | return this->emitZero(T, E); |
| 6680 | } |
| 6681 | |
| 6682 | template <class Emitter> |
| 6683 | bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) { |
| 6684 | if (DiscardResult) |
| 6685 | return true; |
| 6686 | |
| 6687 | if constexpr (!std::is_same_v<Emitter, EvalEmitter>) { |
| 6688 | if (this->LambdaThisCapture.Offset > 0) { |
| 6689 | if (this->LambdaThisCapture.IsPtr) |
| 6690 | return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E); |
| 6691 | return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E); |
| 6692 | } |
| 6693 | } |
| 6694 | |
| 6695 | // In some circumstances, the 'this' pointer does not actually refer to the |
| 6696 | // instance pointer of the current function frame, but e.g. to the declaration |
| 6697 | // currently being initialized. Here we emit the necessary instruction(s) for |
| 6698 | // this scenario. |
| 6699 | if (!InitStackActive || InitStack.empty()) |
| 6700 | return this->emitThis(E); |
| 6701 | |
| 6702 | // If our init stack is, for example: |
| 6703 | // 0 Stack: 3 (decl) |
| 6704 | // 1 Stack: 6 (init list) |
| 6705 | // 2 Stack: 1 (field) |
| 6706 | // 3 Stack: 6 (init list) |
| 6707 | // 4 Stack: 1 (field) |
| 6708 | // |
| 6709 | // We want to find the LAST element in it that's an init list, |
| 6710 | // which is marked with the K_InitList marker. The index right |
| 6711 | // before that points to an init list. We need to find the |
| 6712 | // elements before the K_InitList element that point to a base |
| 6713 | // (e.g. a decl or This), optionally followed by field, elem, etc. |
| 6714 | // In the example above, we want to emit elements [0..2]. |
| 6715 | unsigned StartIndex = 0; |
| 6716 | unsigned EndIndex = 0; |
| 6717 | // Find the init list. |
| 6718 | for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) { |
| 6719 | if (InitStack[StartIndex].Kind == InitLink::K_DIE) { |
| 6720 | EndIndex = StartIndex; |
| 6721 | --StartIndex; |
| 6722 | break; |
| 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | // Walk backwards to find the base. |
| 6727 | for (; StartIndex > 0; --StartIndex) { |
| 6728 | if (InitStack[StartIndex].Kind == InitLink::K_InitList) |
| 6729 | continue; |
| 6730 | |
| 6731 | if (InitStack[StartIndex].Kind != InitLink::K_Field && |
| 6732 | InitStack[StartIndex].Kind != InitLink::K_Elem && |
| 6733 | InitStack[StartIndex].Kind != InitLink::K_Base && |
| 6734 | InitStack[StartIndex].Kind != InitLink::K_DIE) |
| 6735 | break; |
| 6736 | } |
| 6737 | |
| 6738 | if (StartIndex == 0 && EndIndex == 0) |
| 6739 | EndIndex = InitStack.size() - 1; |
| 6740 | |
| 6741 | assert(InitStack[StartIndex].Kind == InitLink::K_Decl || |
| 6742 | InitStack[StartIndex].Kind == InitLink::K_This || |
| 6743 | InitStack[StartIndex].Kind == InitLink::K_Temp || |
| 6744 | InitStack[StartIndex].Kind == InitLink::K_RVO); |
| 6745 | |
| 6746 | // NOTE: This could be StartIndex < EndIndex, but we're also abusing the |
| 6747 | // InitStack mechanism in visitWithSubstitutions to have the This pointer |
| 6748 | // _just_ be a local variable. |
| 6749 | assert(StartIndex <= EndIndex); |
| 6750 | |
| 6751 | // Emit the instructions. |
| 6752 | for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) { |
| 6753 | if (InitStack[I].Kind == InitLink::K_InitList || |
| 6754 | InitStack[I].Kind == InitLink::K_DIE) |
| 6755 | continue; |
| 6756 | if (!InitStack[I].template emit<Emitter>(this, E)) |
| 6757 | return false; |
| 6758 | } |
| 6759 | return true; |
| 6760 | } |
| 6761 | |
| 6762 | template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) { |
| 6763 | switch (S->getStmtClass()) { |
| 6764 | case Stmt::CompoundStmtClass: |
| 6765 | return visitCompoundStmt(S: cast<CompoundStmt>(Val: S)); |
| 6766 | case Stmt::DeclStmtClass: |
| 6767 | return visitDeclStmt(DS: cast<DeclStmt>(Val: S), /*EvaluateConditionDecl=*/true); |
| 6768 | case Stmt::ReturnStmtClass: |
| 6769 | return visitReturnStmt(RS: cast<ReturnStmt>(Val: S)); |
| 6770 | case Stmt::IfStmtClass: |
| 6771 | return visitIfStmt(IS: cast<IfStmt>(Val: S)); |
| 6772 | case Stmt::WhileStmtClass: |
| 6773 | return visitWhileStmt(S: cast<WhileStmt>(Val: S)); |
| 6774 | case Stmt::DoStmtClass: |
| 6775 | return visitDoStmt(S: cast<DoStmt>(Val: S)); |
| 6776 | case Stmt::ForStmtClass: |
| 6777 | return visitForStmt(S: cast<ForStmt>(Val: S)); |
| 6778 | case Stmt::CXXForRangeStmtClass: |
| 6779 | return visitCXXForRangeStmt(S: cast<CXXForRangeStmt>(Val: S)); |
| 6780 | case Stmt::BreakStmtClass: |
| 6781 | return visitBreakStmt(S: cast<BreakStmt>(Val: S)); |
| 6782 | case Stmt::ContinueStmtClass: |
| 6783 | return visitContinueStmt(S: cast<ContinueStmt>(Val: S)); |
| 6784 | case Stmt::SwitchStmtClass: |
| 6785 | return visitSwitchStmt(S: cast<SwitchStmt>(Val: S)); |
| 6786 | case Stmt::CaseStmtClass: |
| 6787 | return visitCaseStmt(S: cast<CaseStmt>(Val: S)); |
| 6788 | case Stmt::DefaultStmtClass: |
| 6789 | return visitDefaultStmt(S: cast<DefaultStmt>(Val: S)); |
| 6790 | case Stmt::AttributedStmtClass: |
| 6791 | return visitAttributedStmt(S: cast<AttributedStmt>(Val: S)); |
| 6792 | case Stmt::CXXTryStmtClass: |
| 6793 | return visitCXXTryStmt(S: cast<CXXTryStmt>(Val: S)); |
| 6794 | case Stmt::NullStmtClass: |
| 6795 | return true; |
| 6796 | // Always invalid statements. |
| 6797 | case Stmt::GCCAsmStmtClass: |
| 6798 | case Stmt::MSAsmStmtClass: |
| 6799 | case Stmt::GotoStmtClass: |
| 6800 | return this->emitInvalid(S); |
| 6801 | case Stmt::LabelStmtClass: |
| 6802 | return this->visitStmt(S: cast<LabelStmt>(Val: S)->getSubStmt()); |
| 6803 | case Stmt::CXXExpansionStmtInstantiationClass: |
| 6804 | return this->visitCXXExpansionStmtInstantiation( |
| 6805 | S: cast<CXXExpansionStmtInstantiation>(Val: S)); |
| 6806 | default: { |
| 6807 | if (const auto *E = dyn_cast<Expr>(Val: S)) |
| 6808 | return this->discard(E); |
| 6809 | return false; |
| 6810 | } |
| 6811 | } |
| 6812 | } |
| 6813 | |
| 6814 | template <class Emitter> |
| 6815 | bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) { |
| 6816 | LocalScope<Emitter> Scope(this); |
| 6817 | for (const auto *InnerStmt : S->body()) |
| 6818 | if (!visitStmt(S: InnerStmt)) |
| 6819 | return false; |
| 6820 | return Scope.destroyLocals(); |
| 6821 | } |
| 6822 | |
| 6823 | template <class Emitter> |
| 6824 | bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) { |
| 6825 | if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) { |
| 6826 | for (auto *BD : DD->flat_bindings()) |
| 6827 | if (auto *KD = BD->getHoldingVar(); |
| 6828 | KD && !this->visitVarDecl(VD: KD, Init: KD->getInit())) |
| 6829 | return false; |
| 6830 | } |
| 6831 | return true; |
| 6832 | } |
| 6833 | |
| 6834 | static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) { |
| 6835 | assert(FD); |
| 6836 | assert(FD->getParent()->isUnion()); |
| 6837 | const CXXRecordDecl *CXXRD = |
| 6838 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6839 | return !CXXRD || CXXRD->hasTrivialDefaultConstructor(); |
| 6840 | } |
| 6841 | |
| 6842 | template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) { |
| 6843 | for (;;) { |
| 6844 | if (const auto *ME = dyn_cast<MemberExpr>(Val: E)) { |
| 6845 | if (const auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl()); |
| 6846 | FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD)) |
| 6847 | return true; |
| 6848 | E = ME->getBase(); |
| 6849 | continue; |
| 6850 | } |
| 6851 | |
| 6852 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) { |
| 6853 | E = ASE->getBase()->IgnoreImplicit(); |
| 6854 | continue; |
| 6855 | } |
| 6856 | |
| 6857 | if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E); |
| 6858 | ICE && (ICE->getCastKind() == CK_NoOp || |
| 6859 | ICE->getCastKind() == CK_DerivedToBase || |
| 6860 | ICE->getCastKind() == CK_UncheckedDerivedToBase)) { |
| 6861 | E = ICE->getSubExpr(); |
| 6862 | continue; |
| 6863 | } |
| 6864 | |
| 6865 | if (const auto *This = dyn_cast<CXXThisExpr>(Val: E)) { |
| 6866 | const auto *ThisRecord = |
| 6867 | This->getType()->getPointeeType()->getAsRecordDecl(); |
| 6868 | if (!ThisRecord->isUnion()) |
| 6869 | return false; |
| 6870 | // Otherwise, always activate if we're in the ctor. |
| 6871 | if (const auto *Ctor = |
| 6872 | dyn_cast_if_present<CXXConstructorDecl>(Val: CompilingFunction)) |
| 6873 | return Ctor->getParent() == ThisRecord; |
| 6874 | return false; |
| 6875 | } |
| 6876 | |
| 6877 | break; |
| 6878 | } |
| 6879 | return false; |
| 6880 | } |
| 6881 | |
| 6882 | template <class Emitter> |
| 6883 | bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS, |
| 6884 | bool EvaluateConditionDecl) { |
| 6885 | for (const auto *D : DS->decls()) { |
| 6886 | if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl, BaseUsingDecl, |
| 6887 | FunctionDecl, NamespaceAliasDecl, UsingDirectiveDecl>(Val: D)) |
| 6888 | continue; |
| 6889 | |
| 6890 | if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(Val: D)) { |
| 6891 | assert(ESD->getInstantiations() && "not expanded?" ); |
| 6892 | if (!this->visitStmt(S: ESD->getInstantiations())) |
| 6893 | return false; |
| 6894 | continue; |
| 6895 | } |
| 6896 | |
| 6897 | const auto *VD = dyn_cast<VarDecl>(Val: D); |
| 6898 | if (!VD) |
| 6899 | return false; |
| 6900 | if (!this->visitVarDecl(VD, Init: VD->getInit())) |
| 6901 | return false; |
| 6902 | |
| 6903 | // Register decomposition decl holding vars. |
| 6904 | if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD)) |
| 6905 | return false; |
| 6906 | } |
| 6907 | |
| 6908 | return true; |
| 6909 | } |
| 6910 | |
| 6911 | template <class Emitter> |
| 6912 | bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) { |
| 6913 | if (this->InStmtExpr) |
| 6914 | return this->emitUnsupported(RS); |
| 6915 | |
| 6916 | if (const Expr *RE = RS->getRetValue()) { |
| 6917 | LocalScope<Emitter> RetScope(this); |
| 6918 | if (ReturnType) { |
| 6919 | // Primitive types are simply returned. |
| 6920 | if (!this->visit(E: RE)) |
| 6921 | return false; |
| 6922 | this->emitCleanup(); |
| 6923 | return this->emitRet(*ReturnType, RS); |
| 6924 | } |
| 6925 | |
| 6926 | if (RE->getType()->isVoidType()) { |
| 6927 | if (!this->visit(E: RE)) |
| 6928 | return false; |
| 6929 | } else { |
| 6930 | if (RE->containsErrors()) |
| 6931 | return false; |
| 6932 | |
| 6933 | InitLinkScope<Emitter> ILS(this, InitLink::RVO()); |
| 6934 | // RVO - construct the value in the return location. |
| 6935 | if (!this->emitRVOPtr(RE)) |
| 6936 | return false; |
| 6937 | if (!this->visitInitializerPop(E: RE)) |
| 6938 | return false; |
| 6939 | |
| 6940 | this->emitCleanup(); |
| 6941 | return this->emitRetVoid(RS); |
| 6942 | } |
| 6943 | } |
| 6944 | |
| 6945 | // Void return. |
| 6946 | this->emitCleanup(); |
| 6947 | return this->emitRetVoid(RS); |
| 6948 | } |
| 6949 | |
| 6950 | template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) { |
| 6951 | LocalScope<Emitter> IfScope(this); |
| 6952 | |
| 6953 | auto visitChildStmt = [&](const Stmt *S) -> bool { |
| 6954 | LocalScope<Emitter> SScope(this); |
| 6955 | if (!visitStmt(S)) |
| 6956 | return false; |
| 6957 | return SScope.destroyLocals(); |
| 6958 | }; |
| 6959 | |
| 6960 | if (auto *CondInit = IS->getInit()) { |
| 6961 | if (!visitStmt(S: CondInit)) |
| 6962 | return false; |
| 6963 | } |
| 6964 | |
| 6965 | if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) { |
| 6966 | if (!visitDeclStmt(DS: CondDecl)) |
| 6967 | return false; |
| 6968 | } |
| 6969 | |
| 6970 | // Save ourselves compiling some code and the jumps, etc. if the condition is |
| 6971 | // stataically known to be either true or false. We could look at more cases |
| 6972 | // here, but I think all the ones that actually happen are using a |
| 6973 | // ConstantExpr. |
| 6974 | if (std::optional<bool> BoolValue = getBoolValue(E: IS->getCond())) { |
| 6975 | if (*BoolValue) |
| 6976 | return visitChildStmt(IS->getThen()); |
| 6977 | if (const Stmt *Else = IS->getElse()) |
| 6978 | return visitChildStmt(Else); |
| 6979 | return true; |
| 6980 | } |
| 6981 | |
| 6982 | // Otherwise, compile the condition. |
| 6983 | if (IS->isNonNegatedConsteval()) { |
| 6984 | if (!this->emitIsConstantContext(IS)) |
| 6985 | return false; |
| 6986 | } else if (IS->isNegatedConsteval()) { |
| 6987 | if (!this->emitIsConstantContext(IS)) |
| 6988 | return false; |
| 6989 | if (!this->emitInv(IS)) |
| 6990 | return false; |
| 6991 | } else { |
| 6992 | LocalScope<Emitter> CondScope(this, ScopeKind::FullExpression); |
| 6993 | if (!this->visitBool(E: IS->getCond())) |
| 6994 | return false; |
| 6995 | if (!CondScope.destroyLocals()) |
| 6996 | return false; |
| 6997 | } |
| 6998 | |
| 6999 | if (!this->maybeEmitDeferredVarInit(VD: IS->getConditionVariable())) |
| 7000 | return false; |
| 7001 | |
| 7002 | if (const Stmt *Else = IS->getElse()) { |
| 7003 | LabelTy LabelElse = this->getLabel(); |
| 7004 | LabelTy LabelEnd = this->getLabel(); |
| 7005 | if (!this->jumpFalse(LabelElse, IS)) |
| 7006 | return false; |
| 7007 | if (!visitChildStmt(IS->getThen())) |
| 7008 | return false; |
| 7009 | if (!this->jump(LabelEnd, IS)) |
| 7010 | return false; |
| 7011 | this->emitLabel(LabelElse); |
| 7012 | if (!visitChildStmt(Else)) |
| 7013 | return false; |
| 7014 | this->emitLabel(LabelEnd); |
| 7015 | } else { |
| 7016 | LabelTy LabelEnd = this->getLabel(); |
| 7017 | if (!this->jumpFalse(LabelEnd, IS)) |
| 7018 | return false; |
| 7019 | if (!visitChildStmt(IS->getThen())) |
| 7020 | return false; |
| 7021 | this->emitLabel(LabelEnd); |
| 7022 | } |
| 7023 | |
| 7024 | if (!IfScope.destroyLocals()) |
| 7025 | return false; |
| 7026 | |
| 7027 | return true; |
| 7028 | } |
| 7029 | |
| 7030 | template <class Emitter> |
| 7031 | bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) { |
| 7032 | const Expr *Cond = S->getCond(); |
| 7033 | const Stmt *Body = S->getBody(); |
| 7034 | |
| 7035 | LabelTy CondLabel = this->getLabel(); // Label before the condition. |
| 7036 | LabelTy EndLabel = this->getLabel(); // Label after the loop. |
| 7037 | LocalScope<Emitter> WholeLoopScope(this); |
| 7038 | LoopScope<Emitter> LS(this, S, EndLabel, CondLabel); |
| 7039 | |
| 7040 | this->fallthrough(CondLabel); |
| 7041 | this->emitLabel(CondLabel); |
| 7042 | |
| 7043 | // Start of the loop body { |
| 7044 | LocalScope<Emitter> CondScope(this); |
| 7045 | |
| 7046 | if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) { |
| 7047 | if (!visitDeclStmt(DS: CondDecl)) |
| 7048 | return false; |
| 7049 | } |
| 7050 | |
| 7051 | if (!this->visitBool(E: Cond)) |
| 7052 | return false; |
| 7053 | |
| 7054 | if (!this->maybeEmitDeferredVarInit(VD: S->getConditionVariable())) |
| 7055 | return false; |
| 7056 | |
| 7057 | if (!this->jumpFalse(EndLabel, S)) |
| 7058 | return false; |
| 7059 | |
| 7060 | if (!this->visitStmt(S: Body)) |
| 7061 | return false; |
| 7062 | |
| 7063 | if (!CondScope.destroyLocals()) |
| 7064 | return false; |
| 7065 | // } End of loop body. |
| 7066 | |
| 7067 | if (!this->jump(CondLabel, S)) |
| 7068 | return false; |
| 7069 | this->fallthrough(EndLabel); |
| 7070 | this->emitLabel(EndLabel); |
| 7071 | |
| 7072 | return CondScope.destroyLocals() && WholeLoopScope.destroyLocals(); |
| 7073 | } |
| 7074 | |
| 7075 | template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) { |
| 7076 | const Expr *Cond = S->getCond(); |
| 7077 | const Stmt *Body = S->getBody(); |
| 7078 | |
| 7079 | LabelTy StartLabel = this->getLabel(); |
| 7080 | LabelTy EndLabel = this->getLabel(); |
| 7081 | LabelTy CondLabel = this->getLabel(); |
| 7082 | LocalScope<Emitter> WholeLoopScope(this); |
| 7083 | LoopScope<Emitter> LS(this, S, EndLabel, CondLabel); |
| 7084 | |
| 7085 | this->fallthrough(StartLabel); |
| 7086 | this->emitLabel(StartLabel); |
| 7087 | |
| 7088 | { |
| 7089 | LocalScope<Emitter> CondScope(this); |
| 7090 | if (!this->visitStmt(S: Body)) |
| 7091 | return false; |
| 7092 | this->fallthrough(CondLabel); |
| 7093 | this->emitLabel(CondLabel); |
| 7094 | if (!this->visitBool(E: Cond)) |
| 7095 | return false; |
| 7096 | |
| 7097 | if (!CondScope.destroyLocals()) |
| 7098 | return false; |
| 7099 | } |
| 7100 | if (!this->jumpTrue(StartLabel, S)) |
| 7101 | return false; |
| 7102 | |
| 7103 | this->fallthrough(EndLabel); |
| 7104 | this->emitLabel(EndLabel); |
| 7105 | return WholeLoopScope.destroyLocals(); |
| 7106 | } |
| 7107 | |
| 7108 | template <class Emitter> |
| 7109 | bool Compiler<Emitter>::visitForStmt(const ForStmt *S) { |
| 7110 | // for (Init; Cond; Inc) { Body } |
| 7111 | const Stmt *Init = S->getInit(); |
| 7112 | const Expr *Cond = S->getCond(); |
| 7113 | const Expr *Inc = S->getInc(); |
| 7114 | const Stmt *Body = S->getBody(); |
| 7115 | |
| 7116 | LabelTy EndLabel = this->getLabel(); |
| 7117 | LabelTy CondLabel = this->getLabel(); |
| 7118 | LabelTy IncLabel = this->getLabel(); |
| 7119 | |
| 7120 | LocalScope<Emitter> WholeLoopScope(this); |
| 7121 | if (Init && !this->visitStmt(S: Init)) |
| 7122 | return false; |
| 7123 | |
| 7124 | // Start of the loop body { |
| 7125 | this->fallthrough(CondLabel); |
| 7126 | this->emitLabel(CondLabel); |
| 7127 | |
| 7128 | LocalScope<Emitter> CondScope(this); |
| 7129 | LoopScope<Emitter> LS(this, S, EndLabel, IncLabel); |
| 7130 | if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) { |
| 7131 | if (!visitDeclStmt(DS: CondDecl)) |
| 7132 | return false; |
| 7133 | } |
| 7134 | |
| 7135 | if (Cond) { |
| 7136 | if (!this->visitBool(E: Cond)) |
| 7137 | return false; |
| 7138 | if (!this->jumpFalse(EndLabel, S)) |
| 7139 | return false; |
| 7140 | } |
| 7141 | if (!this->maybeEmitDeferredVarInit(VD: S->getConditionVariable())) |
| 7142 | return false; |
| 7143 | |
| 7144 | if (Body && !this->visitStmt(S: Body)) |
| 7145 | return false; |
| 7146 | |
| 7147 | this->fallthrough(IncLabel); |
| 7148 | this->emitLabel(IncLabel); |
| 7149 | if (Inc && !this->discard(E: Inc)) |
| 7150 | return false; |
| 7151 | |
| 7152 | if (!CondScope.destroyLocals()) |
| 7153 | return false; |
| 7154 | if (!this->jump(CondLabel, S)) |
| 7155 | return false; |
| 7156 | // } End of loop body. |
| 7157 | |
| 7158 | this->emitLabel(EndLabel); |
| 7159 | // If we jumped out of the loop above, we still need to clean up the condition |
| 7160 | // scope. |
| 7161 | return CondScope.destroyLocals() && WholeLoopScope.destroyLocals(); |
| 7162 | } |
| 7163 | |
| 7164 | template <class Emitter> |
| 7165 | bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) { |
| 7166 | const Stmt *Init = S->getInit(); |
| 7167 | const Expr *Cond = S->getCond(); |
| 7168 | const Expr *Inc = S->getInc(); |
| 7169 | const Stmt *Body = S->getBody(); |
| 7170 | const Stmt *BeginStmt = S->getBeginStmt(); |
| 7171 | const Stmt *RangeStmt = S->getRangeStmt(); |
| 7172 | const Stmt *EndStmt = S->getEndStmt(); |
| 7173 | |
| 7174 | LabelTy EndLabel = this->getLabel(); |
| 7175 | LabelTy CondLabel = this->getLabel(); |
| 7176 | LabelTy IncLabel = this->getLabel(); |
| 7177 | LocalScope<Emitter> WholeLoopScope(this); |
| 7178 | LoopScope<Emitter> LS(this, S, EndLabel, IncLabel); |
| 7179 | |
| 7180 | // Emit declarations needed in the loop. |
| 7181 | if (Init && !this->visitStmt(S: Init)) |
| 7182 | return false; |
| 7183 | if (!this->visitStmt(S: RangeStmt)) |
| 7184 | return false; |
| 7185 | if (!this->visitStmt(S: BeginStmt)) |
| 7186 | return false; |
| 7187 | if (!this->visitStmt(S: EndStmt)) |
| 7188 | return false; |
| 7189 | |
| 7190 | LocalScope<Emitter> CondScope(this); |
| 7191 | // Now the condition as well as the loop variable assignment. |
| 7192 | this->fallthrough(CondLabel); |
| 7193 | this->emitLabel(CondLabel); |
| 7194 | if (!this->visitBool(E: Cond)) |
| 7195 | return false; |
| 7196 | if (!this->jumpFalse(EndLabel, S)) |
| 7197 | return false; |
| 7198 | |
| 7199 | if (!this->visitDeclStmt(DS: S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true)) |
| 7200 | return false; |
| 7201 | |
| 7202 | // Body. |
| 7203 | { |
| 7204 | if (!this->visitStmt(S: Body)) |
| 7205 | return false; |
| 7206 | |
| 7207 | this->fallthrough(IncLabel); |
| 7208 | this->emitLabel(IncLabel); |
| 7209 | if (!this->discard(E: Inc)) |
| 7210 | return false; |
| 7211 | } |
| 7212 | |
| 7213 | if (!CondScope.destroyLocals()) |
| 7214 | return false; |
| 7215 | if (!this->jump(CondLabel, S)) |
| 7216 | return false; |
| 7217 | |
| 7218 | this->fallthrough(EndLabel); |
| 7219 | this->emitLabel(EndLabel); |
| 7220 | return WholeLoopScope.destroyLocals(); |
| 7221 | } |
| 7222 | |
| 7223 | template <class Emitter> |
| 7224 | bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) { |
| 7225 | if (LabelInfoStack.empty()) |
| 7226 | return false; |
| 7227 | |
| 7228 | OptLabelTy TargetLabel = std::nullopt; |
| 7229 | const Stmt *TargetLoop = S->getNamedLoopOrSwitch(); |
| 7230 | const VariableScope<Emitter> *BreakScope = nullptr; |
| 7231 | |
| 7232 | if (!TargetLoop) { |
| 7233 | for (const auto &LI : llvm::reverse(LabelInfoStack)) { |
| 7234 | if (LI.BreakLabel) { |
| 7235 | TargetLabel = *LI.BreakLabel; |
| 7236 | BreakScope = LI.BreakOrContinueScope; |
| 7237 | break; |
| 7238 | } |
| 7239 | } |
| 7240 | } else { |
| 7241 | for (const auto &LI : LabelInfoStack) { |
| 7242 | if (LI.Name == TargetLoop) { |
| 7243 | TargetLabel = *LI.BreakLabel; |
| 7244 | BreakScope = LI.BreakOrContinueScope; |
| 7245 | break; |
| 7246 | } |
| 7247 | } |
| 7248 | } |
| 7249 | |
| 7250 | // Faulty break statement (e.g. label redefined or named loops disabled). |
| 7251 | if (!TargetLabel) |
| 7252 | return false; |
| 7253 | |
| 7254 | for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope; |
| 7255 | C = C->getParent()) { |
| 7256 | if (!C->destroyLocals()) |
| 7257 | return false; |
| 7258 | } |
| 7259 | |
| 7260 | return this->jump(*TargetLabel, S); |
| 7261 | } |
| 7262 | |
| 7263 | template <class Emitter> |
| 7264 | bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) { |
| 7265 | if (LabelInfoStack.empty()) |
| 7266 | return false; |
| 7267 | |
| 7268 | OptLabelTy TargetLabel = std::nullopt; |
| 7269 | const Stmt *TargetLoop = S->getNamedLoopOrSwitch(); |
| 7270 | const VariableScope<Emitter> *ContinueScope = nullptr; |
| 7271 | |
| 7272 | if (!TargetLoop) { |
| 7273 | for (const auto &LI : llvm::reverse(LabelInfoStack)) { |
| 7274 | if (LI.ContinueLabel) { |
| 7275 | TargetLabel = *LI.ContinueLabel; |
| 7276 | ContinueScope = LI.BreakOrContinueScope; |
| 7277 | break; |
| 7278 | } |
| 7279 | } |
| 7280 | } else { |
| 7281 | for (auto LI : LabelInfoStack) { |
| 7282 | if (LI.Name == TargetLoop) { |
| 7283 | TargetLabel = *LI.ContinueLabel; |
| 7284 | ContinueScope = LI.BreakOrContinueScope; |
| 7285 | break; |
| 7286 | } |
| 7287 | } |
| 7288 | } |
| 7289 | |
| 7290 | if (!TargetLabel) |
| 7291 | return false; |
| 7292 | |
| 7293 | for (VariableScope<Emitter> *C = VarScope; C != ContinueScope; |
| 7294 | C = C->getParent()) { |
| 7295 | if (!C->destroyLocals()) |
| 7296 | return false; |
| 7297 | } |
| 7298 | |
| 7299 | return this->jump(*TargetLabel, S); |
| 7300 | } |
| 7301 | |
| 7302 | template <class Emitter> |
| 7303 | bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) { |
| 7304 | const Expr *Cond = S->getCond(); |
| 7305 | if (Cond->containsErrors()) |
| 7306 | return false; |
| 7307 | |
| 7308 | PrimType CondT = this->classifyPrim(Cond->getType()); |
| 7309 | LocalScope<Emitter> LS(this); |
| 7310 | llvm::SaveAndRestore StmtExprSAR(this->SwitchInStmtExpr, this->InStmtExpr); |
| 7311 | |
| 7312 | LabelTy EndLabel = this->getLabel(); |
| 7313 | UnsignedOrNone DefaultLabel = std::nullopt; |
| 7314 | unsigned CondVar = |
| 7315 | this->allocateLocalPrimitive(Src: Cond, Ty: CondT, /*IsConst=*/true); |
| 7316 | |
| 7317 | if (const auto *CondInit = S->getInit()) |
| 7318 | if (!visitStmt(S: CondInit)) |
| 7319 | return false; |
| 7320 | |
| 7321 | if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) |
| 7322 | if (!visitDeclStmt(DS: CondDecl)) |
| 7323 | return false; |
| 7324 | |
| 7325 | // Initialize condition variable. |
| 7326 | if (!this->visit(E: Cond)) |
| 7327 | return false; |
| 7328 | if (!this->emitSetLocal(CondT, CondVar, S)) |
| 7329 | return false; |
| 7330 | |
| 7331 | if (!this->maybeEmitDeferredVarInit(VD: S->getConditionVariable())) |
| 7332 | return false; |
| 7333 | |
| 7334 | CaseMap CaseLabels; |
| 7335 | // Create labels and comparison ops for all case statements. |
| 7336 | for (const SwitchCase *SC = S->getSwitchCaseList(); SC; |
| 7337 | SC = SC->getNextSwitchCase()) { |
| 7338 | if (const auto *CS = dyn_cast<CaseStmt>(Val: SC)) { |
| 7339 | CaseLabels[SC] = this->getLabel(); |
| 7340 | |
| 7341 | if (CS->caseStmtIsGNURange()) { |
| 7342 | LabelTy EndOfRangeCheck = this->getLabel(); |
| 7343 | const Expr *Low = CS->getLHS(); |
| 7344 | const Expr *High = CS->getRHS(); |
| 7345 | if (Low->isValueDependent() || High->isValueDependent()) |
| 7346 | return false; |
| 7347 | |
| 7348 | if (!this->emitGetLocal(CondT, CondVar, CS)) |
| 7349 | return false; |
| 7350 | if (!this->visit(E: Low)) |
| 7351 | return false; |
| 7352 | PrimType LT = this->classifyPrim(Low->getType()); |
| 7353 | if (!this->emitGE(LT, S)) |
| 7354 | return false; |
| 7355 | if (!this->jumpFalse(EndOfRangeCheck, S)) |
| 7356 | return false; |
| 7357 | |
| 7358 | if (!this->emitGetLocal(CondT, CondVar, CS)) |
| 7359 | return false; |
| 7360 | if (!this->visit(E: High)) |
| 7361 | return false; |
| 7362 | PrimType HT = this->classifyPrim(High->getType()); |
| 7363 | if (!this->emitLE(HT, S)) |
| 7364 | return false; |
| 7365 | if (!this->jumpTrue(CaseLabels[CS], S)) |
| 7366 | return false; |
| 7367 | this->emitLabel(EndOfRangeCheck); |
| 7368 | continue; |
| 7369 | } |
| 7370 | |
| 7371 | const Expr *Value = CS->getLHS(); |
| 7372 | if (Value->isValueDependent()) |
| 7373 | return false; |
| 7374 | PrimType ValueT = this->classifyPrim(Value->getType()); |
| 7375 | |
| 7376 | // Compare the case statement's value to the switch condition. |
| 7377 | if (!this->emitGetLocal(CondT, CondVar, CS)) |
| 7378 | return false; |
| 7379 | if (!this->visit(E: Value)) |
| 7380 | return false; |
| 7381 | |
| 7382 | // Compare and jump to the case label. |
| 7383 | if (!this->emitEQ(ValueT, S)) |
| 7384 | return false; |
| 7385 | if (!this->jumpTrue(CaseLabels[CS], S)) |
| 7386 | return false; |
| 7387 | } else { |
| 7388 | assert(!DefaultLabel); |
| 7389 | DefaultLabel = this->getLabel(); |
| 7390 | } |
| 7391 | } |
| 7392 | |
| 7393 | // If none of the conditions above were true, fall through to the default |
| 7394 | // statement or jump after the switch statement. |
| 7395 | if (DefaultLabel) { |
| 7396 | if (!this->jump(*DefaultLabel, S)) |
| 7397 | return false; |
| 7398 | } else { |
| 7399 | if (!this->jump(EndLabel, S)) |
| 7400 | return false; |
| 7401 | } |
| 7402 | |
| 7403 | SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel, |
| 7404 | DefaultLabel); |
| 7405 | if (!this->visitStmt(S: S->getBody())) |
| 7406 | return false; |
| 7407 | this->fallthrough(EndLabel); |
| 7408 | this->emitLabel(EndLabel); |
| 7409 | |
| 7410 | return LS.destroyLocals(); |
| 7411 | } |
| 7412 | |
| 7413 | template <class Emitter> |
| 7414 | bool Compiler<Emitter>::visitCaseStmt(const CaseStmt *S) { |
| 7415 | this->fallthrough(CaseLabels[S]); |
| 7416 | this->emitLabel(CaseLabels[S]); |
| 7417 | |
| 7418 | // We can't jump from an outer switch statement to a case label |
| 7419 | // that's inside a StmtExpr. |
| 7420 | if (this->InStmtExpr && !this->SwitchInStmtExpr) |
| 7421 | return this->emitUnsupported(S); |
| 7422 | |
| 7423 | return this->visitStmt(S: S->getSubStmt()); |
| 7424 | } |
| 7425 | |
| 7426 | template <class Emitter> |
| 7427 | bool Compiler<Emitter>::visitDefaultStmt(const DefaultStmt *S) { |
| 7428 | if (LabelInfoStack.empty()) |
| 7429 | return false; |
| 7430 | |
| 7431 | LabelTy DefaultLabel; |
| 7432 | for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) { |
| 7433 | if (LI.DefaultLabel) { |
| 7434 | DefaultLabel = *LI.DefaultLabel; |
| 7435 | break; |
| 7436 | } |
| 7437 | } |
| 7438 | |
| 7439 | this->emitLabel(DefaultLabel); |
| 7440 | return this->visitStmt(S: S->getSubStmt()); |
| 7441 | } |
| 7442 | |
| 7443 | template <class Emitter> |
| 7444 | bool Compiler<Emitter>::visitAttributedStmt(const AttributedStmt *S) { |
| 7445 | const Stmt *SubStmt = S->getSubStmt(); |
| 7446 | |
| 7447 | bool IsMSVCConstexprAttr = isa<ReturnStmt>(Val: SubStmt) && |
| 7448 | hasSpecificAttr<MSConstexprAttr>(container: S->getAttrs()); |
| 7449 | |
| 7450 | if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S)) |
| 7451 | return false; |
| 7452 | |
| 7453 | if (this->Ctx.getLangOpts().CXXAssumptions && |
| 7454 | !this->Ctx.getLangOpts().MSVCCompat) { |
| 7455 | for (const Attr *A : S->getAttrs()) { |
| 7456 | auto *AA = dyn_cast<CXXAssumeAttr>(Val: A); |
| 7457 | if (!AA) |
| 7458 | continue; |
| 7459 | |
| 7460 | assert(isa<NullStmt>(SubStmt)); |
| 7461 | |
| 7462 | const Expr *Assumption = AA->getAssumption(); |
| 7463 | if (Assumption->isValueDependent()) |
| 7464 | return false; |
| 7465 | |
| 7466 | if (Assumption->HasSideEffects(Ctx: this->Ctx.getASTContext())) |
| 7467 | continue; |
| 7468 | |
| 7469 | // Evaluate assumption. |
| 7470 | if (!this->visitBool(E: Assumption)) |
| 7471 | return false; |
| 7472 | |
| 7473 | if (!this->emitAssume(Assumption)) |
| 7474 | return false; |
| 7475 | } |
| 7476 | } |
| 7477 | |
| 7478 | // Ignore other attributes. |
| 7479 | if (!this->visitStmt(S: SubStmt)) |
| 7480 | return false; |
| 7481 | |
| 7482 | if (IsMSVCConstexprAttr) |
| 7483 | return this->emitPopMSVCCE(S); |
| 7484 | return true; |
| 7485 | } |
| 7486 | |
| 7487 | template <class Emitter> |
| 7488 | bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) { |
| 7489 | // Ignore all handlers. |
| 7490 | return this->visitStmt(S: S->getTryBlock()); |
| 7491 | } |
| 7492 | |
| 7493 | /// template for (auto x : {1, 2}) {} |
| 7494 | /// |
| 7495 | /// This is not a loop from an AST perspective at all since it has already |
| 7496 | /// been instantiated to a list of compound statements. |
| 7497 | /// |
| 7498 | /// Since we can have control flow in those compound statements, we need to |
| 7499 | /// handle it mostly like a loop though. |
| 7500 | template <class Emitter> |
| 7501 | bool Compiler<Emitter>::visitCXXExpansionStmtInstantiation( |
| 7502 | const CXXExpansionStmtInstantiation *S) { |
| 7503 | LocalScope<Emitter> WholeLoopScope(this, ScopeKind::Block); |
| 7504 | |
| 7505 | for (const Stmt *PreambleStmt : S->getPreambleStmts()) { |
| 7506 | if (!this->visitDeclStmt(DS: cast<DeclStmt>(Val: PreambleStmt), EvaluateConditionDecl: true)) |
| 7507 | return false; |
| 7508 | } |
| 7509 | |
| 7510 | LabelTy EndLabel = this->getLabel(); |
| 7511 | for (const Stmt *Instantiation : S->getInstantiations()) { |
| 7512 | LabelTy ContinueLabel = this->getLabel(); |
| 7513 | LoopScope<Emitter> LS(this, S, EndLabel, ContinueLabel); |
| 7514 | |
| 7515 | if (!this->visitStmt(S: Instantiation)) |
| 7516 | return false; |
| 7517 | this->emitLabel(ContinueLabel); |
| 7518 | } |
| 7519 | |
| 7520 | this->emitLabel(EndLabel); |
| 7521 | |
| 7522 | return WholeLoopScope.destroyLocals(); |
| 7523 | } |
| 7524 | |
| 7525 | template <class Emitter> |
| 7526 | bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) { |
| 7527 | assert(MD->isLambdaStaticInvoker()); |
| 7528 | assert(MD->hasBody()); |
| 7529 | assert(cast<CompoundStmt>(MD->getBody())->body_empty()); |
| 7530 | |
| 7531 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
| 7532 | const FunctionDecl *LambdaCallOp; |
| 7533 | assert(ClosureClass->captures().empty()); |
| 7534 | if (ClosureClass->isGenericLambda()) { |
| 7535 | LambdaCallOp = ClosureClass->getLambdaCallOperator(); |
| 7536 | assert(MD->isFunctionTemplateSpecialization() && |
| 7537 | "A generic lambda's static-invoker function must be a " |
| 7538 | "template specialization" ); |
| 7539 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 7540 | FunctionTemplateDecl *CallOpTemplate = |
| 7541 | LambdaCallOp->getDescribedFunctionTemplate(); |
| 7542 | llvm::FoldingSetInsertToken InsertToken; |
| 7543 | const FunctionDecl *CorrespondingCallOpSpecialization = |
| 7544 | CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertToken); |
| 7545 | assert(CorrespondingCallOpSpecialization); |
| 7546 | LambdaCallOp = CorrespondingCallOpSpecialization; |
| 7547 | } else { |
| 7548 | LambdaCallOp = ClosureClass->getLambdaCallOperator(); |
| 7549 | } |
| 7550 | assert(ClosureClass->captures().empty()); |
| 7551 | const Function *Func = this->getFunction(FD: LambdaCallOp); |
| 7552 | if (!Func) |
| 7553 | return false; |
| 7554 | assert(Func->hasThisPointer()); |
| 7555 | assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO())); |
| 7556 | |
| 7557 | if (Func->hasRVO()) { |
| 7558 | if (!this->emitRVOPtr(MD)) |
| 7559 | return false; |
| 7560 | } |
| 7561 | |
| 7562 | // The lambda call operator needs an instance pointer, but we don't have |
| 7563 | // one here, and we don't need one either because the lambda cannot have |
| 7564 | // any captures, as verified above. Emit a null pointer. This is then |
| 7565 | // special-cased when interpreting to not emit any misleading diagnostics. |
| 7566 | if (!this->emitNullPtr(0, nullptr, MD)) |
| 7567 | return false; |
| 7568 | |
| 7569 | // Forward all arguments from the static invoker to the lambda call operator. |
| 7570 | for (const ParmVarDecl *PVD : MD->parameters()) { |
| 7571 | auto It = this->Params.find(PVD); |
| 7572 | assert(It != this->Params.end()); |
| 7573 | |
| 7574 | // We do the lvalue-to-rvalue conversion manually here, so no need |
| 7575 | // to care about references. |
| 7576 | PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr); |
| 7577 | if (!this->emitGetParam(ParamType, It->second.Index, MD)) |
| 7578 | return false; |
| 7579 | } |
| 7580 | |
| 7581 | if (!this->emitCall(Func, 0, LambdaCallOp)) |
| 7582 | return false; |
| 7583 | |
| 7584 | this->emitCleanup(); |
| 7585 | if (ReturnType) |
| 7586 | return this->emitRet(*ReturnType, MD); |
| 7587 | |
| 7588 | // Nothing to do, since we emitted the RVO pointer above. |
| 7589 | return this->emitRetVoid(MD); |
| 7590 | } |
| 7591 | |
| 7592 | template <class Emitter> |
| 7593 | bool Compiler<Emitter>::checkLiteralType(const Expr *E) { |
| 7594 | if (Ctx.getLangOpts().CPlusPlus23) |
| 7595 | return true; |
| 7596 | |
| 7597 | if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Ctx.getASTContext())) |
| 7598 | return true; |
| 7599 | |
| 7600 | return this->emitCheckLiteralType(E->getType().getTypePtr(), E); |
| 7601 | } |
| 7602 | |
| 7603 | static bool initNeedsOverridenLoc(const CXXCtorInitializer *Init) { |
| 7604 | const Expr *InitExpr = Init->getInit(); |
| 7605 | |
| 7606 | if (!Init->isWritten() && !Init->isInClassMemberInitializer() && |
| 7607 | !isa<CXXConstructExpr>(Val: InitExpr)) |
| 7608 | return true; |
| 7609 | |
| 7610 | if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: InitExpr)) { |
| 7611 | const CXXConstructorDecl *Ctor = CE->getConstructor(); |
| 7612 | if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() && |
| 7613 | Ctor->isTrivial()) |
| 7614 | return true; |
| 7615 | } |
| 7616 | |
| 7617 | return false; |
| 7618 | } |
| 7619 | |
| 7620 | template <class Emitter> |
| 7621 | bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) { |
| 7622 | assert(!ReturnType); |
| 7623 | |
| 7624 | // Only start the lifetime of the instance pointer. |
| 7625 | if (!this->emitStartThisLifetime1(Ctor)) |
| 7626 | return false; |
| 7627 | |
| 7628 | auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset, |
| 7629 | const Expr *InitExpr, |
| 7630 | bool Activate = false) -> bool { |
| 7631 | // We don't know what to do with these, so just return false. |
| 7632 | if (InitExpr->getType().isNull()) |
| 7633 | return false; |
| 7634 | |
| 7635 | if (OptPrimType T = this->classify(InitExpr)) { |
| 7636 | if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr)) |
| 7637 | return false; |
| 7638 | |
| 7639 | if (!this->visit(E: InitExpr)) |
| 7640 | return false; |
| 7641 | |
| 7642 | if (F->isBitField()) |
| 7643 | return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(), |
| 7644 | InitExpr); |
| 7645 | return this->emitInitThisField(*T, FieldOffset, InitExpr); |
| 7646 | } |
| 7647 | // Non-primitive case. Get a pointer to the field-to-initialize |
| 7648 | // on the stack and call visitInitialzer() for it. |
| 7649 | InitLinkScope<Emitter> FieldScope(this, InitLink::Field(Offset: F->Offset)); |
| 7650 | if (!this->emitGetPtrThisField(FieldOffset, InitExpr)) |
| 7651 | return false; |
| 7652 | |
| 7653 | if (Activate && !this->emitActivate(InitExpr)) |
| 7654 | return false; |
| 7655 | |
| 7656 | return this->visitInitializerPop(E: InitExpr); |
| 7657 | }; |
| 7658 | |
| 7659 | const RecordDecl *RD = Ctor->getParent(); |
| 7660 | const Record *R = this->getRecord(RD); |
| 7661 | if (!R) |
| 7662 | return false; |
| 7663 | bool IsUnion = R->isUnion(); |
| 7664 | |
| 7665 | // Default union copy and move ctors are special. |
| 7666 | if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) { |
| 7667 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}); |
| 7668 | |
| 7669 | // No special case for NumFields == 0 here, so the Memcpy op |
| 7670 | // below also does its checks in those cases. |
| 7671 | |
| 7672 | assert(cast<CompoundStmt>(Ctor->getBody())->body_empty()); |
| 7673 | if (!this->emitThis(Ctor)) |
| 7674 | return false; |
| 7675 | |
| 7676 | if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor)) |
| 7677 | return false; |
| 7678 | |
| 7679 | return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) && |
| 7680 | this->emitRetVoid(Ctor); |
| 7681 | } |
| 7682 | |
| 7683 | unsigned FieldInits = 0; |
| 7684 | InitLinkScope<Emitter> InitScope(this, InitLink::This()); |
| 7685 | // First, initialize virtual bases if the records has them. |
| 7686 | if (R->getNumVirtualBases() > 0) { |
| 7687 | if (!this->emitThis(Ctor)) |
| 7688 | return false; |
| 7689 | LabelTy AfterVirtBasesLabel = this->getLabel(); |
| 7690 | |
| 7691 | // If the instance pointer is a base class, skip the virtual bases. |
| 7692 | if (!this->emitIsBaseClass({})) |
| 7693 | return false; |
| 7694 | if (!this->jumpTrue(AfterVirtBasesLabel, {})) |
| 7695 | return false; |
| 7696 | |
| 7697 | for (const auto *Init : Ctor->inits()) { |
| 7698 | if (const Type *Base = Init->getBaseClass(); |
| 7699 | Base && Init->isBaseVirtual()) { |
| 7700 | const auto *BaseDecl = Base->getAsCXXRecordDecl(); |
| 7701 | assert(BaseDecl); |
| 7702 | assert(R->findVirtualBase(BaseDecl)); |
| 7703 | if (!this->emitGetPtrThisVirtBase(BaseDecl, Ctor)) |
| 7704 | return false; |
| 7705 | if (!this->visitInitializerPop(E: Init->getInit())) |
| 7706 | return false; |
| 7707 | } |
| 7708 | } |
| 7709 | |
| 7710 | this->fallthrough(AfterVirtBasesLabel); |
| 7711 | this->emitLabel(AfterVirtBasesLabel); |
| 7712 | |
| 7713 | if (!this->emitPopPtr(Ctor)) |
| 7714 | return false; |
| 7715 | } |
| 7716 | |
| 7717 | for (const auto *Init : Ctor->inits()) { |
| 7718 | // Scope needed for the initializers. |
| 7719 | LocalScope<Emitter> Scope(this, ScopeKind::FullExpression); |
| 7720 | |
| 7721 | const Expr *InitExpr = Init->getInit(); |
| 7722 | if (const FieldDecl *Member = Init->getMember()) { |
| 7723 | const Record::Field *F = R->getField(FD: Member); |
| 7724 | |
| 7725 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}, |
| 7726 | initNeedsOverridenLoc(Init)); |
| 7727 | if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion)) |
| 7728 | return false; |
| 7729 | ++FieldInits; |
| 7730 | } else if (const Type *Base = Init->getBaseClass()) { |
| 7731 | const auto *BaseDecl = Base->getAsCXXRecordDecl(); |
| 7732 | assert(BaseDecl); |
| 7733 | |
| 7734 | if (Init->isBaseVirtual()) { |
| 7735 | // See above. |
| 7736 | continue; |
| 7737 | } else { |
| 7738 | // Base class initializer. |
| 7739 | // Get This Base and call initializer on it. |
| 7740 | const Record::Base *B = R->getBase(RD: BaseDecl); |
| 7741 | assert(B); |
| 7742 | if (!this->emitGetPtrThisBase(B->Offset, InitExpr)) |
| 7743 | return false; |
| 7744 | } |
| 7745 | |
| 7746 | if (!this->visitInitializerPop(E: InitExpr)) |
| 7747 | return false; |
| 7748 | } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) { |
| 7749 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}, |
| 7750 | initNeedsOverridenLoc(Init)); |
| 7751 | unsigned ChainSize = IFD->getChainingSize(); |
| 7752 | assert(ChainSize >= 2); |
| 7753 | |
| 7754 | unsigned NestedFieldOffset = 0; |
| 7755 | const Record::Field *NestedField = nullptr; |
| 7756 | for (unsigned I = 0; I != ChainSize; ++I) { |
| 7757 | const auto *FD = cast<FieldDecl>(Val: IFD->chain()[I]); |
| 7758 | const Record *FieldRecord = this->P.getOrCreateRecord(RD: FD->getParent()); |
| 7759 | assert(FieldRecord); |
| 7760 | |
| 7761 | NestedField = FieldRecord->getField(FD); |
| 7762 | assert(NestedField); |
| 7763 | IsUnion = IsUnion || FieldRecord->isUnion(); |
| 7764 | |
| 7765 | NestedFieldOffset += NestedField->Offset; |
| 7766 | |
| 7767 | // Add a new InitChainLink for the record, but not for the final field. |
| 7768 | if (I != ChainSize - 1) |
| 7769 | InitStack.push_back(Elt: InitLink::Field(Offset: NestedField->Offset)); |
| 7770 | } |
| 7771 | assert(NestedField); |
| 7772 | |
| 7773 | InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Val: InitExpr)); |
| 7774 | if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr, |
| 7775 | IsUnion)) |
| 7776 | return false; |
| 7777 | |
| 7778 | // Mark all chain links as initialized. |
| 7779 | unsigned InitFieldOffset = 0; |
| 7780 | for (const NamedDecl *ND : IFD->chain().drop_back()) { |
| 7781 | const auto *FD = cast<FieldDecl>(Val: ND); |
| 7782 | const Record *FieldRecord = this->P.getOrCreateRecord(RD: FD->getParent()); |
| 7783 | assert(FieldRecord); |
| 7784 | NestedField = FieldRecord->getField(FD); |
| 7785 | InitFieldOffset += NestedField->Offset; |
| 7786 | assert(NestedField); |
| 7787 | if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr)) |
| 7788 | return false; |
| 7789 | if (!this->emitFinishInitPop(InitExpr)) |
| 7790 | return false; |
| 7791 | } |
| 7792 | |
| 7793 | InitStack.pop_back_n(NumItems: ChainSize - 1); |
| 7794 | |
| 7795 | } else { |
| 7796 | assert(Init->isDelegatingInitializer()); |
| 7797 | if (!this->emitThis(InitExpr)) |
| 7798 | return false; |
| 7799 | if (!this->visitInitializerPop(E: Init->getInit())) |
| 7800 | return false; |
| 7801 | } |
| 7802 | |
| 7803 | if (!Scope.destroyLocals()) |
| 7804 | return false; |
| 7805 | } |
| 7806 | |
| 7807 | if (FieldInits != R->getNumFields()) { |
| 7808 | assert(FieldInits < R->getNumFields()); |
| 7809 | // Start the lifetime of all members. |
| 7810 | if (!this->emitStartThisLifetime(Ctor)) |
| 7811 | return false; |
| 7812 | } |
| 7813 | |
| 7814 | if (const Stmt *Body = Ctor->getBody()) { |
| 7815 | // Only emit the CtorCheck op for non-empty CompoundStmt bodies. |
| 7816 | // For non-CompoundStmts, always assume they are non-empty and emit it. |
| 7817 | if (const auto *CS = dyn_cast<CompoundStmt>(Val: Body)) { |
| 7818 | if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{})) |
| 7819 | return false; |
| 7820 | } else { |
| 7821 | if (!this->emitCtorCheck(SourceInfo{})) |
| 7822 | return false; |
| 7823 | } |
| 7824 | |
| 7825 | if (!visitStmt(S: Body)) |
| 7826 | return false; |
| 7827 | } |
| 7828 | |
| 7829 | return this->emitRetVoid(SourceInfo{}); |
| 7830 | } |
| 7831 | |
| 7832 | template <class Emitter> |
| 7833 | bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) { |
| 7834 | const RecordDecl *RD = Dtor->getParent(); |
| 7835 | const Record *R = this->getRecord(RD); |
| 7836 | if (!R) |
| 7837 | return false; |
| 7838 | |
| 7839 | if (!Dtor->isTrivial() && Dtor->getBody()) { |
| 7840 | if (!this->visitStmt(S: Dtor->getBody())) |
| 7841 | return false; |
| 7842 | } |
| 7843 | |
| 7844 | if (!this->emitThis(Dtor)) |
| 7845 | return false; |
| 7846 | |
| 7847 | if (!this->emitCheckDestruction(Dtor)) |
| 7848 | return false; |
| 7849 | |
| 7850 | assert(R); |
| 7851 | if (!R->isUnion()) { |
| 7852 | |
| 7853 | LocOverrideScope<Emitter> LOS(this, SourceInfo{}); |
| 7854 | // First, destroy all fields. |
| 7855 | for (const Record::Field &Field : llvm::reverse(C: R->fields())) { |
| 7856 | const Descriptor *D = Field.Desc; |
| 7857 | if (D->hasTrivialDtor()) |
| 7858 | continue; |
| 7859 | if (!this->emitGetPtrField(Field.Offset, SourceInfo{})) |
| 7860 | return false; |
| 7861 | if (!this->emitDestructionPop(Desc: D, Loc: SourceInfo{})) |
| 7862 | return false; |
| 7863 | } |
| 7864 | } |
| 7865 | |
| 7866 | for (const Record::Base &Base : llvm::reverse(C: R->bases())) { |
| 7867 | if (Base.R->hasTrivialDtor()) |
| 7868 | continue; |
| 7869 | if (!this->emitGetPtrBase(Base.Offset, SourceInfo{})) |
| 7870 | return false; |
| 7871 | if (!this->emitRecordDestructionPop(R: Base.R, Loc: {})) |
| 7872 | return false; |
| 7873 | } |
| 7874 | |
| 7875 | if (R->getNumVirtualBases() > 0) { |
| 7876 | LabelTy EndLabel = this->getLabel(); |
| 7877 | // If this is a base class, skip the virtual bases. |
| 7878 | if (!this->emitIsBaseClass({})) |
| 7879 | return false; |
| 7880 | if (!this->jumpTrue(EndLabel, {})) |
| 7881 | return false; |
| 7882 | |
| 7883 | for (const Record::Base &Base : llvm::reverse(C: R->virtual_bases())) { |
| 7884 | if (Base.R->hasTrivialDtor()) |
| 7885 | continue; |
| 7886 | if (!this->emitGetPtrVirtBase(cast<CXXRecordDecl>(Val: Base.R->getDecl()), |
| 7887 | SourceInfo{})) |
| 7888 | return false; |
| 7889 | if (!this->emitRecordDestructionPop(R: Base.R, Loc: {})) |
| 7890 | return false; |
| 7891 | } |
| 7892 | |
| 7893 | this->fallthrough(EndLabel); |
| 7894 | this->emitLabel(EndLabel); |
| 7895 | } |
| 7896 | |
| 7897 | if (!this->emitMarkDestroyed(Dtor)) |
| 7898 | return false; |
| 7899 | |
| 7900 | return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor); |
| 7901 | } |
| 7902 | |
| 7903 | template <class Emitter> |
| 7904 | bool Compiler<Emitter>::compileUnionAssignmentOperator( |
| 7905 | const CXXMethodDecl *MD) { |
| 7906 | if (!this->emitThis(MD)) |
| 7907 | return false; |
| 7908 | |
| 7909 | if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD)) |
| 7910 | return false; |
| 7911 | |
| 7912 | return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD); |
| 7913 | } |
| 7914 | |
| 7915 | template <class Emitter> |
| 7916 | bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) { |
| 7917 | if (F->getReturnType()->isDependentType()) |
| 7918 | return false; |
| 7919 | |
| 7920 | // Classify the return type. |
| 7921 | ReturnType = this->classify(F->getReturnType()); |
| 7922 | |
| 7923 | this->CompilingFunction = F; |
| 7924 | |
| 7925 | if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: F)) |
| 7926 | return this->compileConstructor(Ctor); |
| 7927 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(Val: F)) |
| 7928 | return this->compileDestructor(Dtor); |
| 7929 | |
| 7930 | // Emit custom code if this is a lambda static invoker. |
| 7931 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: F)) { |
| 7932 | const RecordDecl *RD = MD->getParent(); |
| 7933 | |
| 7934 | if (RD->isUnion() && |
| 7935 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())) |
| 7936 | return this->compileUnionAssignmentOperator(MD); |
| 7937 | |
| 7938 | if (MD->isLambdaStaticInvoker()) |
| 7939 | return this->emitLambdaStaticInvokerBody(MD); |
| 7940 | } |
| 7941 | |
| 7942 | // Regular functions. |
| 7943 | if (const auto *Body = F->getBody()) |
| 7944 | if (!visitStmt(S: Body)) |
| 7945 | return false; |
| 7946 | |
| 7947 | // Emit a guard return to protect against a code path missing one. |
| 7948 | if (F->getReturnType()->isVoidType()) |
| 7949 | return this->emitRetVoid(SourceInfo{}); |
| 7950 | return this->emitNoRet(SourceInfo{}); |
| 7951 | } |
| 7952 | |
| 7953 | static uint32_t getBitWidth(const Expr *E) { |
| 7954 | assert(E->refersToBitField()); |
| 7955 | const auto *ME = cast<MemberExpr>(Val: E); |
| 7956 | const auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl()); |
| 7957 | return FD->getBitWidthValue(); |
| 7958 | } |
| 7959 | |
| 7960 | template <class Emitter> |
| 7961 | bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) { |
| 7962 | if (E->containsErrors()) |
| 7963 | return false; |
| 7964 | |
| 7965 | const Expr *SubExpr = E->getSubExpr(); |
| 7966 | if (SubExpr->getType()->isAnyComplexType()) |
| 7967 | return this->VisitComplexUnaryOperator(E); |
| 7968 | if (SubExpr->getType()->isVectorType()) |
| 7969 | return this->VisitVectorUnaryOperator(E); |
| 7970 | if (SubExpr->getType()->isFixedPointType()) |
| 7971 | return this->VisitFixedPointUnaryOperator(E); |
| 7972 | OptPrimType T = classify(SubExpr->getType()); |
| 7973 | |
| 7974 | switch (E->getOpcode()) { |
| 7975 | case UO_PostInc: { // x++ |
| 7976 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 7977 | return this->emitInvalid(E); |
| 7978 | if (!T) |
| 7979 | return this->emitError(E); |
| 7980 | |
| 7981 | if (!this->visit(E: SubExpr)) |
| 7982 | return false; |
| 7983 | |
| 7984 | if (T == PT_Ptr) { |
| 7985 | if (!this->emitIncPtr(E)) |
| 7986 | return false; |
| 7987 | |
| 7988 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 7989 | } |
| 7990 | |
| 7991 | if (T == PT_Float) |
| 7992 | return DiscardResult ? this->emitIncfPop(getFPOptions(E), E) |
| 7993 | : this->emitIncf(getFPOptions(E), E); |
| 7994 | |
| 7995 | if (SubExpr->refersToBitField()) |
| 7996 | return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(), |
| 7997 | getBitWidth(E: SubExpr), E) |
| 7998 | : this->emitIncBitfield(*T, E->canOverflow(), |
| 7999 | getBitWidth(E: SubExpr), E); |
| 8000 | |
| 8001 | return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E) |
| 8002 | : this->emitInc(*T, E->canOverflow(), E); |
| 8003 | } |
| 8004 | case UO_PostDec: { // x-- |
| 8005 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 8006 | return this->emitInvalid(E); |
| 8007 | if (!T) |
| 8008 | return this->emitError(E); |
| 8009 | |
| 8010 | if (!this->visit(E: SubExpr)) |
| 8011 | return false; |
| 8012 | |
| 8013 | if (T == PT_Ptr) { |
| 8014 | if (!this->emitDecPtr(E)) |
| 8015 | return false; |
| 8016 | |
| 8017 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 8018 | } |
| 8019 | |
| 8020 | if (T == PT_Float) |
| 8021 | return DiscardResult ? this->emitDecfPop(getFPOptions(E), E) |
| 8022 | : this->emitDecf(getFPOptions(E), E); |
| 8023 | |
| 8024 | if (SubExpr->refersToBitField()) { |
| 8025 | return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(), |
| 8026 | getBitWidth(E: SubExpr), E) |
| 8027 | : this->emitDecBitfield(*T, E->canOverflow(), |
| 8028 | getBitWidth(E: SubExpr), E); |
| 8029 | } |
| 8030 | |
| 8031 | return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E) |
| 8032 | : this->emitDec(*T, E->canOverflow(), E); |
| 8033 | } |
| 8034 | case UO_PreInc: { // ++x |
| 8035 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 8036 | return this->emitInvalid(E); |
| 8037 | if (!T) |
| 8038 | return this->emitError(E); |
| 8039 | |
| 8040 | if (!this->visit(E: SubExpr)) |
| 8041 | return false; |
| 8042 | |
| 8043 | if (T == PT_Ptr) { |
| 8044 | if (!this->emitLoadPtr(E)) |
| 8045 | return false; |
| 8046 | if (!this->emitConstUint8(1, E)) |
| 8047 | return false; |
| 8048 | if (!this->emitAddOffsetUint8(E)) |
| 8049 | return false; |
| 8050 | return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E); |
| 8051 | } |
| 8052 | |
| 8053 | // Post-inc and pre-inc are the same if the value is to be discarded. |
| 8054 | if (DiscardResult) { |
| 8055 | if (T == PT_Float) |
| 8056 | return this->emitIncfPop(getFPOptions(E), E); |
| 8057 | if (SubExpr->refersToBitField()) |
| 8058 | return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(), |
| 8059 | getBitWidth(E: SubExpr), E) |
| 8060 | : this->emitIncBitfield(*T, E->canOverflow(), |
| 8061 | getBitWidth(E: SubExpr), E); |
| 8062 | return this->emitIncPop(*T, E->canOverflow(), E); |
| 8063 | } |
| 8064 | |
| 8065 | if (T == PT_Float) { |
| 8066 | const auto &TargetSemantics = Ctx.getFloatSemantics(T: E->getType()); |
| 8067 | if (!this->emitLoadFloat(E)) |
| 8068 | return false; |
| 8069 | APFloat F(TargetSemantics, 1); |
| 8070 | if (!this->emitFloat(F, Info: E)) |
| 8071 | return false; |
| 8072 | |
| 8073 | if (!this->emitAddf(getFPOptions(E), E)) |
| 8074 | return false; |
| 8075 | if (!this->emitStoreFloat(E)) |
| 8076 | return false; |
| 8077 | } else if (SubExpr->refersToBitField()) { |
| 8078 | assert(isIntegerOrBoolType(*T)); |
| 8079 | if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(E: SubExpr), |
| 8080 | E)) |
| 8081 | return false; |
| 8082 | } else { |
| 8083 | assert(isIntegerOrBoolType(*T)); |
| 8084 | if (!this->emitPreInc(*T, E->canOverflow(), E)) |
| 8085 | return false; |
| 8086 | } |
| 8087 | return E->isGLValue() || this->emitLoadPop(*T, E); |
| 8088 | } |
| 8089 | case UO_PreDec: { // --x |
| 8090 | if (!Ctx.getLangOpts().CPlusPlus14) |
| 8091 | return this->emitInvalid(E); |
| 8092 | if (!T) |
| 8093 | return this->emitError(E); |
| 8094 | |
| 8095 | if (!this->visit(E: SubExpr)) |
| 8096 | return false; |
| 8097 | |
| 8098 | if (T == PT_Ptr) { |
| 8099 | if (!this->emitLoadPtr(E)) |
| 8100 | return false; |
| 8101 | if (!this->emitConstUint8(1, E)) |
| 8102 | return false; |
| 8103 | if (!this->emitSubOffsetUint8(E)) |
| 8104 | return false; |
| 8105 | return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E); |
| 8106 | } |
| 8107 | |
| 8108 | // Post-dec and pre-dec are the same if the value is to be discarded. |
| 8109 | if (DiscardResult) { |
| 8110 | if (T == PT_Float) |
| 8111 | return this->emitDecfPop(getFPOptions(E), E); |
| 8112 | if (SubExpr->refersToBitField()) |
| 8113 | return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(), |
| 8114 | getBitWidth(E: SubExpr), E) |
| 8115 | : this->emitDecBitfield(*T, E->canOverflow(), |
| 8116 | getBitWidth(E: SubExpr), E); |
| 8117 | return this->emitDecPop(*T, E->canOverflow(), E); |
| 8118 | } |
| 8119 | |
| 8120 | if (T == PT_Float) { |
| 8121 | const auto &TargetSemantics = Ctx.getFloatSemantics(T: E->getType()); |
| 8122 | if (!this->emitLoadFloat(E)) |
| 8123 | return false; |
| 8124 | APFloat F(TargetSemantics, 1); |
| 8125 | if (!this->emitFloat(F, Info: E)) |
| 8126 | return false; |
| 8127 | |
| 8128 | if (!this->emitSubf(getFPOptions(E), E)) |
| 8129 | return false; |
| 8130 | if (!this->emitStoreFloat(E)) |
| 8131 | return false; |
| 8132 | } else if (SubExpr->refersToBitField()) { |
| 8133 | assert(isIntegerOrBoolType(*T)); |
| 8134 | if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(E: SubExpr), |
| 8135 | E)) |
| 8136 | return false; |
| 8137 | } else { |
| 8138 | assert(isIntegerOrBoolType(*T)); |
| 8139 | if (!this->emitPreDec(*T, E->canOverflow(), E)) |
| 8140 | return false; |
| 8141 | } |
| 8142 | return E->isGLValue() || this->emitLoadPop(*T, E); |
| 8143 | } |
| 8144 | case UO_LNot: // !x |
| 8145 | if (!T) |
| 8146 | return this->emitError(E); |
| 8147 | |
| 8148 | if (DiscardResult) |
| 8149 | return this->discard(E: SubExpr); |
| 8150 | |
| 8151 | if (!this->visitBool(E: SubExpr)) |
| 8152 | return false; |
| 8153 | |
| 8154 | if (!this->emitInv(E)) |
| 8155 | return false; |
| 8156 | |
| 8157 | if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool) |
| 8158 | return this->emitCast(PT_Bool, ET, E); |
| 8159 | return true; |
| 8160 | case UO_Minus: // -x |
| 8161 | if (!T) |
| 8162 | return this->emitError(E); |
| 8163 | |
| 8164 | if (!this->visit(E: SubExpr)) |
| 8165 | return false; |
| 8166 | return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E); |
| 8167 | case UO_Plus: // +x |
| 8168 | if (!T) |
| 8169 | return this->emitError(E); |
| 8170 | |
| 8171 | if (!this->visit(E: SubExpr)) // noop |
| 8172 | return false; |
| 8173 | return DiscardResult ? this->emitPop(*T, E) : true; |
| 8174 | case UO_AddrOf: // &x |
| 8175 | if (E->getType()->isMemberPointerType()) { |
| 8176 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 8177 | // member can be formed. |
| 8178 | if (DiscardResult) |
| 8179 | return true; |
| 8180 | return this->emitGetMemberPtr(cast<DeclRefExpr>(Val: SubExpr)->getDecl(), E); |
| 8181 | } |
| 8182 | // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*' |
| 8183 | // operator, neither operator is evaluated and the result is as if both |
| 8184 | // were omitted. So '&*q' is just 'q' with no dereference; delegate to the |
| 8185 | // pointer operand directly instead of to the '*' (which would emit a null |
| 8186 | // check), so that e.g. '&*(int *)0' is not rejected. |
| 8187 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 8188 | const Expr *Sub = SubExpr->IgnoreParens(); |
| 8189 | |
| 8190 | if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub); |
| 8191 | Deref && Deref->getOpcode() == UO_Deref) { |
| 8192 | if (DiscardResult) |
| 8193 | return this->discard(E: Deref->getSubExpr()); |
| 8194 | return this->visit(E: Deref->getSubExpr()) && this->emitAddrOf(E); |
| 8195 | } |
| 8196 | } |
| 8197 | // We should already have a pointer when we get here. |
| 8198 | if (DiscardResult) |
| 8199 | return this->discard(E: SubExpr); |
| 8200 | return this->delegate(E: SubExpr) && this->emitAddrOf(E); |
| 8201 | case UO_Deref: // *x |
| 8202 | if (DiscardResult) |
| 8203 | return this->discard(E: SubExpr); |
| 8204 | |
| 8205 | if (!this->visit(E: SubExpr)) |
| 8206 | return false; |
| 8207 | |
| 8208 | if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E)) |
| 8209 | return false; |
| 8210 | |
| 8211 | if (classifyPrim(SubExpr) == PT_Ptr) |
| 8212 | return this->emitNarrowPtr(E); |
| 8213 | return true; |
| 8214 | |
| 8215 | case UO_Not: // ~x |
| 8216 | if (!T) |
| 8217 | return this->emitError(E); |
| 8218 | |
| 8219 | if (!this->visit(E: SubExpr)) |
| 8220 | return false; |
| 8221 | return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E); |
| 8222 | case UO_Real: // __real x |
| 8223 | if (!T) |
| 8224 | return false; |
| 8225 | return this->delegate(E: SubExpr); |
| 8226 | case UO_Imag: { // __imag x |
| 8227 | if (!T) |
| 8228 | return false; |
| 8229 | if (!this->discard(E: SubExpr)) |
| 8230 | return false; |
| 8231 | return DiscardResult |
| 8232 | ? true |
| 8233 | : this->visitZeroInitializer(T: *T, QT: SubExpr->getType(), E: SubExpr); |
| 8234 | } |
| 8235 | case UO_Extension: |
| 8236 | return this->delegate(E: SubExpr); |
| 8237 | case UO_Coawait: |
| 8238 | assert(false && "Unhandled opcode" ); |
| 8239 | } |
| 8240 | |
| 8241 | return false; |
| 8242 | } |
| 8243 | |
| 8244 | template <class Emitter> |
| 8245 | bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) { |
| 8246 | const Expr *SubExpr = E->getSubExpr(); |
| 8247 | assert(SubExpr->getType()->isAnyComplexType()); |
| 8248 | |
| 8249 | if (DiscardResult) |
| 8250 | return this->discard(E: SubExpr); |
| 8251 | |
| 8252 | OptPrimType ResT = classify(E); |
| 8253 | auto prepareResult = [=]() -> bool { |
| 8254 | if (!ResT && !Initializing) { |
| 8255 | UnsignedOrNone LocalIndex = allocateLocal(Src: SubExpr); |
| 8256 | if (!LocalIndex) |
| 8257 | return false; |
| 8258 | return this->emitGetPtrLocal(*LocalIndex, E); |
| 8259 | } |
| 8260 | |
| 8261 | return true; |
| 8262 | }; |
| 8263 | |
| 8264 | // The offset of the temporary, if we created one. |
| 8265 | unsigned SubExprOffset = ~0u; |
| 8266 | auto createTemp = [=, &SubExprOffset]() -> bool { |
| 8267 | SubExprOffset = |
| 8268 | this->allocateLocalPrimitive(Src: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 8269 | if (!this->visit(E: SubExpr)) |
| 8270 | return false; |
| 8271 | return this->emitSetLocal(PT_Ptr, SubExprOffset, E); |
| 8272 | }; |
| 8273 | |
| 8274 | PrimType ElemT = classifyComplexElementType(T: SubExpr->getType()); |
| 8275 | auto getElem = [=](unsigned Offset, unsigned Index) -> bool { |
| 8276 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 8277 | return false; |
| 8278 | return this->emitArrayElemPop(ElemT, Index, E); |
| 8279 | }; |
| 8280 | |
| 8281 | switch (E->getOpcode()) { |
| 8282 | case UO_Minus: // -x |
| 8283 | if (!prepareResult()) |
| 8284 | return false; |
| 8285 | if (!createTemp()) |
| 8286 | return false; |
| 8287 | for (unsigned I = 0; I != 2; ++I) { |
| 8288 | if (!getElem(SubExprOffset, I)) |
| 8289 | return false; |
| 8290 | if (!this->emitNeg(ElemT, E)) |
| 8291 | return false; |
| 8292 | if (!this->emitInitElem(ElemT, I, E)) |
| 8293 | return false; |
| 8294 | } |
| 8295 | break; |
| 8296 | |
| 8297 | case UO_Plus: // +x |
| 8298 | case UO_AddrOf: // &x |
| 8299 | case UO_Deref: // *x |
| 8300 | return this->delegate(E: SubExpr); |
| 8301 | |
| 8302 | case UO_LNot: |
| 8303 | if (!this->visit(E: SubExpr)) |
| 8304 | return false; |
| 8305 | if (!this->emitComplexBoolCast(E: SubExpr)) |
| 8306 | return false; |
| 8307 | if (!this->emitInv(E)) |
| 8308 | return false; |
| 8309 | if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool) |
| 8310 | return this->emitCast(PT_Bool, ET, E); |
| 8311 | return true; |
| 8312 | |
| 8313 | case UO_Real: |
| 8314 | return this->emitComplexReal(SubExpr); |
| 8315 | |
| 8316 | case UO_Imag: |
| 8317 | if (!this->visit(E: SubExpr)) |
| 8318 | return false; |
| 8319 | |
| 8320 | if (SubExpr->isLValue()) { |
| 8321 | if (!this->emitConstUint8(1, E)) |
| 8322 | return false; |
| 8323 | return this->emitArrayElemPtrPopUint8(E); |
| 8324 | } |
| 8325 | |
| 8326 | // Since our _Complex implementation does not map to a primitive type, |
| 8327 | // we sometimes have to do the lvalue-to-rvalue conversion here manually. |
| 8328 | return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E); |
| 8329 | |
| 8330 | case UO_Not: // ~x |
| 8331 | if (!this->delegate(E: SubExpr)) |
| 8332 | return false; |
| 8333 | // Negate the imaginary component. |
| 8334 | if (!this->emitArrayElem(ElemT, 1, E)) |
| 8335 | return false; |
| 8336 | if (!this->emitNeg(ElemT, E)) |
| 8337 | return false; |
| 8338 | if (!this->emitInitElem(ElemT, 1, E)) |
| 8339 | return false; |
| 8340 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 8341 | |
| 8342 | case UO_Extension: |
| 8343 | return this->delegate(E: SubExpr); |
| 8344 | |
| 8345 | default: |
| 8346 | return this->emitInvalid(E); |
| 8347 | } |
| 8348 | |
| 8349 | return true; |
| 8350 | } |
| 8351 | |
| 8352 | template <class Emitter> |
| 8353 | bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) { |
| 8354 | const Expr *SubExpr = E->getSubExpr(); |
| 8355 | assert(SubExpr->getType()->isVectorType()); |
| 8356 | |
| 8357 | if (DiscardResult) |
| 8358 | return this->discard(E: SubExpr); |
| 8359 | |
| 8360 | auto UnaryOp = E->getOpcode(); |
| 8361 | if (UnaryOp == UO_Extension) |
| 8362 | return this->delegate(E: SubExpr); |
| 8363 | |
| 8364 | if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot && |
| 8365 | UnaryOp != UO_Not && UnaryOp != UO_AddrOf) |
| 8366 | return this->emitInvalid(E); |
| 8367 | |
| 8368 | // Nothing to do here. |
| 8369 | if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf) |
| 8370 | return this->delegate(E: SubExpr); |
| 8371 | |
| 8372 | if (!Initializing) { |
| 8373 | UnsignedOrNone LocalIndex = allocateLocal(Src: SubExpr); |
| 8374 | if (!LocalIndex) |
| 8375 | return false; |
| 8376 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 8377 | return false; |
| 8378 | } |
| 8379 | |
| 8380 | // The offset of the temporary, if we created one. |
| 8381 | unsigned SubExprOffset = |
| 8382 | this->allocateLocalPrimitive(Src: SubExpr, Ty: PT_Ptr, /*IsConst=*/true); |
| 8383 | if (!this->visit(E: SubExpr)) |
| 8384 | return false; |
| 8385 | if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E)) |
| 8386 | return false; |
| 8387 | |
| 8388 | const auto *VecTy = SubExpr->getType()->getAs<VectorType>(); |
| 8389 | PrimType ElemT = classifyVectorElementType(T: SubExpr->getType()); |
| 8390 | auto getElem = [=](unsigned Offset, unsigned Index) -> bool { |
| 8391 | if (!this->emitGetLocal(PT_Ptr, Offset, E)) |
| 8392 | return false; |
| 8393 | return this->emitArrayElemPop(ElemT, Index, E); |
| 8394 | }; |
| 8395 | |
| 8396 | switch (UnaryOp) { |
| 8397 | case UO_Minus: |
| 8398 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 8399 | if (!getElem(SubExprOffset, I)) |
| 8400 | return false; |
| 8401 | if (!this->emitNeg(ElemT, E)) |
| 8402 | return false; |
| 8403 | if (!this->emitInitElem(ElemT, I, E)) |
| 8404 | return false; |
| 8405 | } |
| 8406 | break; |
| 8407 | case UO_LNot: { // !x |
| 8408 | // In C++, the logic operators !, &&, || are available for vectors. !v is |
| 8409 | // equivalent to v == 0. |
| 8410 | // |
| 8411 | // The result of the comparison is a vector of the same width and number of |
| 8412 | // elements as the comparison operands with a signed integral element type. |
| 8413 | // |
| 8414 | // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html |
| 8415 | QualType ResultVecTy = E->getType(); |
| 8416 | PrimType ResultVecElemT = |
| 8417 | classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType()); |
| 8418 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 8419 | if (!getElem(SubExprOffset, I)) |
| 8420 | return false; |
| 8421 | // operator ! on vectors returns -1 for 'truth', so negate it. |
| 8422 | if (!this->emitPrimCast(FromT: ElemT, ToT: PT_Bool, ToQT: Ctx.getASTContext().BoolTy, E)) |
| 8423 | return false; |
| 8424 | if (!this->emitInv(E)) |
| 8425 | return false; |
| 8426 | if (!this->emitPrimCast(FromT: PT_Bool, ToT: ElemT, ToQT: VecTy->getElementType(), E)) |
| 8427 | return false; |
| 8428 | if (!this->emitNeg(ElemT, E)) |
| 8429 | return false; |
| 8430 | if (ElemT != ResultVecElemT && |
| 8431 | !this->emitPrimCast(FromT: ElemT, ToT: ResultVecElemT, ToQT: ResultVecTy, E)) |
| 8432 | return false; |
| 8433 | if (!this->emitInitElem(ResultVecElemT, I, E)) |
| 8434 | return false; |
| 8435 | } |
| 8436 | break; |
| 8437 | } |
| 8438 | case UO_Not: // ~x |
| 8439 | for (unsigned I = 0; I != VecTy->getNumElements(); ++I) { |
| 8440 | if (!getElem(SubExprOffset, I)) |
| 8441 | return false; |
| 8442 | if (ElemT == PT_Bool) { |
| 8443 | if (!this->emitInv(E)) |
| 8444 | return false; |
| 8445 | } else { |
| 8446 | if (!this->emitComp(ElemT, E)) |
| 8447 | return false; |
| 8448 | } |
| 8449 | if (!this->emitInitElem(ElemT, I, E)) |
| 8450 | return false; |
| 8451 | } |
| 8452 | break; |
| 8453 | default: |
| 8454 | llvm_unreachable("Unsupported unary operators should be handled up front" ); |
| 8455 | } |
| 8456 | return true; |
| 8457 | } |
| 8458 | |
| 8459 | template <class Emitter> |
| 8460 | bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) { |
| 8461 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(Val: D)) { |
| 8462 | if (DiscardResult) |
| 8463 | return true; |
| 8464 | return this->emitConst(ECD->getInitVal(), E); |
| 8465 | } |
| 8466 | if (const auto *FuncDecl = dyn_cast<FunctionDecl>(Val: D)) { |
| 8467 | if (DiscardResult) |
| 8468 | return true; |
| 8469 | const Function *F = getFunction(FD: FuncDecl); |
| 8470 | return F && this->emitGetFnPtr(F, E); |
| 8471 | } |
| 8472 | if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Val: D)) { |
| 8473 | TPOD = TPOD->getFirstDecl(); |
| 8474 | if (DiscardResult) |
| 8475 | return true; |
| 8476 | if (UnsignedOrNone GlobalIndex = P.getGlobal(VD: TPOD)) |
| 8477 | return this->emitGetPtrGlobal(*GlobalIndex, E); |
| 8478 | |
| 8479 | if (UnsignedOrNone Index = P.getOrCreateGlobal(VD: TPOD)) { |
| 8480 | if (OptPrimType T = classify(TPOD->getType())) { |
| 8481 | if (!this->visitAPValue(Val: TPOD->getValue(), ValType: *T, Info: E)) |
| 8482 | return false; |
| 8483 | return this->emitInitGlobal(*T, *Index, E); |
| 8484 | } |
| 8485 | |
| 8486 | if (!this->emitGetPtrGlobal(*Index, E)) |
| 8487 | return false; |
| 8488 | if (!this->visitAPValueInitializer(Val: TPOD->getValue(), Info: E, T: TPOD->getType())) |
| 8489 | return false; |
| 8490 | return this->emitFinishInit(E); |
| 8491 | } |
| 8492 | return false; |
| 8493 | } |
| 8494 | |
| 8495 | // References are implemented via pointers, so when we see a DeclRefExpr |
| 8496 | // pointing to a reference, we need to get its value directly (i.e. the |
| 8497 | // pointer to the actual value) instead of a pointer to the pointer to the |
| 8498 | // value. |
| 8499 | QualType DeclType = D->getType(); |
| 8500 | bool IsReference = DeclType->isReferenceType(); |
| 8501 | |
| 8502 | auto maybePopPtr = [&]() -> bool { |
| 8503 | if (DiscardResult) |
| 8504 | return this->emitPopPtr(E); |
| 8505 | return true; |
| 8506 | }; |
| 8507 | |
| 8508 | // Function parameters. |
| 8509 | // Note that it's important to check them first since we might have a local |
| 8510 | // variable created for a ParmVarDecl as well. |
| 8511 | if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: D)) { |
| 8512 | if (DiscardResult) |
| 8513 | return true; |
| 8514 | |
| 8515 | if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 && |
| 8516 | !DeclType->isIntegralOrEnumerationType()) { |
| 8517 | return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E), |
| 8518 | /*InitializerFailed=*/false, E); |
| 8519 | } |
| 8520 | if (auto It = this->Params.find(PVD); It != this->Params.end()) { |
| 8521 | if (IsReference || !It->second.IsPtr) |
| 8522 | return this->emitGetParam(classifyPrim(E), It->second.Index, E); |
| 8523 | |
| 8524 | return this->emitGetPtrParam(It->second.Index, E); |
| 8525 | } |
| 8526 | |
| 8527 | if (!Ctx.getLangOpts().CPlusPlus23 && IsReference && !Locals.contains(Val: D)) |
| 8528 | return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E), |
| 8529 | /*InitializerFailed=*/false, E); |
| 8530 | } |
| 8531 | |
| 8532 | // Local variables. |
| 8533 | if (auto It = Locals.find(Val: D); It != Locals.end()) { |
| 8534 | const unsigned Offset = It->second.Offset; |
| 8535 | if (IsReference) { |
| 8536 | assert(classifyPrim(E) == PT_Ptr); |
| 8537 | return this->emitGetRefLocal(Offset, E) && maybePopPtr(); |
| 8538 | } |
| 8539 | return this->emitGetPtrLocal(Offset, E) && maybePopPtr(); |
| 8540 | } |
| 8541 | // Global variables. |
| 8542 | if (auto GlobalIndex = P.getGlobal(VD: D)) { |
| 8543 | if (IsReference) { |
| 8544 | if (!Ctx.getLangOpts().CPlusPlus11) |
| 8545 | return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E); |
| 8546 | if (!Ctx.getLangOpts().CPlusPlus23) |
| 8547 | return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E); |
| 8548 | |
| 8549 | return this->emitGetRefGlobal(*GlobalIndex, E) && maybePopPtr(); |
| 8550 | } |
| 8551 | |
| 8552 | return this->emitGetPtrGlobal(*GlobalIndex, E) && maybePopPtr(); |
| 8553 | } |
| 8554 | |
| 8555 | // In case we need to re-visit a declaration. |
| 8556 | auto revisit = [&](const VarDecl *VD, |
| 8557 | bool IsConstexprUnknown = true) -> bool { |
| 8558 | llvm::SaveAndRestore CURS(this->VariablesAreConstexprUnknown, |
| 8559 | IsConstexprUnknown); |
| 8560 | if constexpr (std::is_same_v<Emitter, EvalEmitter>) { |
| 8561 | if (!this->emitPushCC(VD->hasConstantInitialization(), E)) |
| 8562 | return false; |
| 8563 | } |
| 8564 | auto VarState = this->visitDecl(VD); |
| 8565 | |
| 8566 | if constexpr (std::is_same_v<Emitter, EvalEmitter>) { |
| 8567 | if (!this->emitPopCC(E)) |
| 8568 | return false; |
| 8569 | } |
| 8570 | |
| 8571 | if (VarState.notCreated()) |
| 8572 | return true; |
| 8573 | if (!VarState) |
| 8574 | return false; |
| 8575 | // Retry. |
| 8576 | return this->visitDeclRef(D, E); |
| 8577 | }; |
| 8578 | |
| 8579 | if constexpr (!std::is_same_v<Emitter, EvalEmitter>) { |
| 8580 | // Lambda captures. |
| 8581 | if (auto It = this->LambdaCaptures.find(D); |
| 8582 | It != this->LambdaCaptures.end()) { |
| 8583 | auto [Offset, IsPtr] = It->second; |
| 8584 | |
| 8585 | if (IsPtr) |
| 8586 | return this->emitGetThisFieldPtr(Offset, E) && maybePopPtr(); |
| 8587 | return this->emitGetPtrThisField(Offset, E) && maybePopPtr(); |
| 8588 | } |
| 8589 | } |
| 8590 | |
| 8591 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E); |
| 8592 | DRE && DRE->refersToEnclosingVariableOrCapture()) { |
| 8593 | if (const auto *VD = dyn_cast<VarDecl>(Val: D); VD && VD->isInitCapture()) |
| 8594 | return revisit(VD); |
| 8595 | } |
| 8596 | |
| 8597 | if (const auto *BD = dyn_cast<BindingDecl>(Val: D)) |
| 8598 | return this->delegate(E: BD->getBinding()); |
| 8599 | |
| 8600 | // Avoid infinite recursion. |
| 8601 | if (D == InitializingDecl) { |
| 8602 | if (DiscardResult) |
| 8603 | return true; |
| 8604 | return this->emitDummyPtr(D, E); |
| 8605 | } |
| 8606 | |
| 8607 | // Try to lazily visit (or emit dummy pointers for) declarations |
| 8608 | // we haven't seen yet. |
| 8609 | const auto *VD = dyn_cast<VarDecl>(Val: D); |
| 8610 | if (!VD) |
| 8611 | return this->emitError(E); |
| 8612 | |
| 8613 | // For C. |
| 8614 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 8615 | if (VD->getInit() && !VD->getInit()->isValueDependent() && |
| 8616 | DeclType.isConstant(Ctx: Ctx.getASTContext()) && !VD->isWeak() && |
| 8617 | VD->evaluateValue()) |
| 8618 | return revisit(VD, /*IsConstexprUnknown=*/false); |
| 8619 | |
| 8620 | if (DiscardResult) |
| 8621 | return true; |
| 8622 | return this->emitDummyPtr(D, E); |
| 8623 | } |
| 8624 | |
| 8625 | // ... and C++. |
| 8626 | const auto typeShouldBeVisited = [&](QualType T) -> bool { |
| 8627 | if (T.isConstant(Ctx: Ctx.getASTContext())) |
| 8628 | return true; |
| 8629 | return T->isReferenceType(); |
| 8630 | }; |
| 8631 | |
| 8632 | if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) && |
| 8633 | typeShouldBeVisited(DeclType)) { |
| 8634 | if (const Expr *Init = VD->getAnyInitializer(); |
| 8635 | Init && !Init->isValueDependent()) { |
| 8636 | // Whether or not the evaluation is successul doesn't really matter |
| 8637 | // here -- we will create a global variable in any case, and that |
| 8638 | // will have the state of initializer evaluation attached. |
| 8639 | Expr::EvalResult Result; |
| 8640 | (void)Init->EvaluateAsInitializer(Ctx: Ctx.getASTContext(), VD, Result, IsConstantInitializer: true); |
| 8641 | return this->visitDeclRef(D, E); |
| 8642 | } |
| 8643 | return revisit(VD, !VD->isConstexpr() && DeclType->isReferenceType()); |
| 8644 | } |
| 8645 | |
| 8646 | // FIXME: The evaluateValue() check here is a little ridiculous, since |
| 8647 | // it will ultimately call into Context::evaluateAsInitializer(). In |
| 8648 | // other words, we're evaluating the initializer, just to know if we can |
| 8649 | // evaluate the initializer. |
| 8650 | if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() && |
| 8651 | !VD->getInit()->isValueDependent()) { |
| 8652 | if (VD->evaluateValue()) { |
| 8653 | bool IsConstexprUnknown = !DeclType.isConstant(Ctx: Ctx.getASTContext()) && |
| 8654 | !DeclType->isReferenceType(); |
| 8655 | // Revisit the variable declaration, but make sure it's associated with a |
| 8656 | // different evaluation, so e.g. mutable reads don't work on it. |
| 8657 | EvalIDScope _(Ctx); |
| 8658 | return revisit(VD, IsConstexprUnknown); |
| 8659 | } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference) |
| 8660 | return revisit(VD, /*IsConstexprUnknown=*/true); |
| 8661 | |
| 8662 | if (IsReference) |
| 8663 | return this->emitInvalidDeclRef(cast<DeclRefExpr>(Val: E), |
| 8664 | /*InitializerFailed=*/true, E); |
| 8665 | } |
| 8666 | |
| 8667 | if (DiscardResult) |
| 8668 | return true; |
| 8669 | return this->emitDummyPtr( |
| 8670 | D, E, CU: Ctx.getLangOpts().CPlusPlus23 && DeclType->isReferenceType()); |
| 8671 | } |
| 8672 | |
| 8673 | template <class Emitter> |
| 8674 | bool Compiler<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 8675 | const auto *D = E->getDecl(); |
| 8676 | return this->visitDeclRef(D, E); |
| 8677 | } |
| 8678 | |
| 8679 | template <class Emitter> |
| 8680 | bool Compiler<Emitter>::VisitDesignatedInitUpdateExpr( |
| 8681 | const DesignatedInitUpdateExpr *E) { |
| 8682 | if (!this->visitInitializer(E: E->getBase())) |
| 8683 | return false; |
| 8684 | return this->visitInitializer(E: E->getUpdater()); |
| 8685 | } |
| 8686 | |
| 8687 | template <class Emitter> bool Compiler<Emitter>::emitCleanup() { |
| 8688 | for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) { |
| 8689 | if (!C->destroyLocals()) |
| 8690 | return false; |
| 8691 | } |
| 8692 | return true; |
| 8693 | } |
| 8694 | |
| 8695 | template <class Emitter> |
| 8696 | unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType, |
| 8697 | const QualType DerivedType) { |
| 8698 | const auto = [](QualType Ty) -> const CXXRecordDecl * { |
| 8699 | if (const auto *R = Ty->getPointeeCXXRecordDecl()) |
| 8700 | return R; |
| 8701 | return Ty->getAsCXXRecordDecl(); |
| 8702 | }; |
| 8703 | const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType); |
| 8704 | const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType); |
| 8705 | |
| 8706 | return Ctx.collectBaseOffset(BaseDecl, DerivedDecl); |
| 8707 | } |
| 8708 | |
| 8709 | /// Emit casts from a PrimType to another PrimType. |
| 8710 | template <class Emitter> |
| 8711 | bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT, |
| 8712 | QualType ToQT, const Expr *E) { |
| 8713 | |
| 8714 | if (FromT == PT_Float) { |
| 8715 | // Floating to floating. |
| 8716 | if (ToT == PT_Float) { |
| 8717 | const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(T: ToQT); |
| 8718 | return this->emitCastFP(ToSem, getRoundingMode(E), E); |
| 8719 | } |
| 8720 | |
| 8721 | if (ToT == PT_IntAP) |
| 8722 | return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(T: ToQT), |
| 8723 | getFPOptions(E), E); |
| 8724 | if (ToT == PT_IntAPS) |
| 8725 | return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(T: ToQT), |
| 8726 | getFPOptions(E), E); |
| 8727 | |
| 8728 | // Float to integral. |
| 8729 | if (isIntegerOrBoolType(T: ToT) || ToT == PT_Bool) |
| 8730 | return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E); |
| 8731 | } |
| 8732 | |
| 8733 | if (isIntegerOrBoolType(T: FromT) || FromT == PT_Bool) { |
| 8734 | if (ToT == PT_IntAP) |
| 8735 | return this->emitCastAP(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 8736 | if (ToT == PT_IntAPS) |
| 8737 | return this->emitCastAPS(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 8738 | |
| 8739 | // Integral to integral. |
| 8740 | if (isIntegerOrBoolType(T: ToT) || ToT == PT_Bool) |
| 8741 | return FromT != ToT ? this->emitCast(FromT, ToT, E) : true; |
| 8742 | |
| 8743 | if (ToT == PT_Float) { |
| 8744 | // Integral to floating. |
| 8745 | const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(T: ToQT); |
| 8746 | return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E); |
| 8747 | } |
| 8748 | } |
| 8749 | |
| 8750 | return false; |
| 8751 | } |
| 8752 | |
| 8753 | template <class Emitter> |
| 8754 | bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT, |
| 8755 | QualType ToQT, const Expr *E) { |
| 8756 | assert(FromT != ToT); |
| 8757 | |
| 8758 | if (ToT == PT_IntAP) |
| 8759 | return this->emitCastAP(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 8760 | if (ToT == PT_IntAPS) |
| 8761 | return this->emitCastAPS(FromT, Ctx.getBitWidth(T: ToQT), E); |
| 8762 | |
| 8763 | return this->emitCast(FromT, ToT, E); |
| 8764 | } |
| 8765 | |
| 8766 | /// Emits __real(SubExpr) |
| 8767 | template <class Emitter> |
| 8768 | bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) { |
| 8769 | assert(SubExpr->getType()->isAnyComplexType()); |
| 8770 | |
| 8771 | if (DiscardResult) |
| 8772 | return this->discard(E: SubExpr); |
| 8773 | |
| 8774 | if (!this->visit(E: SubExpr)) |
| 8775 | return false; |
| 8776 | if (SubExpr->isLValue()) { |
| 8777 | if (!this->emitConstUint8(0, SubExpr)) |
| 8778 | return false; |
| 8779 | return this->emitArrayElemPtrPopUint8(SubExpr); |
| 8780 | } |
| 8781 | |
| 8782 | // Rvalue, load the actual element. |
| 8783 | return this->emitArrayElemPop(classifyComplexElementType(T: SubExpr->getType()), |
| 8784 | 0, SubExpr); |
| 8785 | } |
| 8786 | |
| 8787 | template <class Emitter> |
| 8788 | bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) { |
| 8789 | assert(!DiscardResult); |
| 8790 | PrimType ElemT = classifyComplexElementType(T: E->getType()); |
| 8791 | // We emit the expression (__real(E) != 0 || __imag(E) != 0) |
| 8792 | // for us, that means (bool)E[0] || (bool)E[1] |
| 8793 | if (!this->emitArrayElem(ElemT, 0, E)) |
| 8794 | return false; |
| 8795 | if (ElemT == PT_Float) { |
| 8796 | if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E)) |
| 8797 | return false; |
| 8798 | } else { |
| 8799 | if (!this->emitCast(ElemT, PT_Bool, E)) |
| 8800 | return false; |
| 8801 | } |
| 8802 | |
| 8803 | // We now have the bool value of E[0] on the stack. |
| 8804 | LabelTy LabelTrue = this->getLabel(); |
| 8805 | if (!this->jumpTrue(LabelTrue, E)) |
| 8806 | return false; |
| 8807 | |
| 8808 | if (!this->emitArrayElemPop(ElemT, 1, E)) |
| 8809 | return false; |
| 8810 | if (ElemT == PT_Float) { |
| 8811 | if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E)) |
| 8812 | return false; |
| 8813 | } else { |
| 8814 | if (!this->emitCast(ElemT, PT_Bool, E)) |
| 8815 | return false; |
| 8816 | } |
| 8817 | // Leave the boolean value of E[1] on the stack. |
| 8818 | LabelTy EndLabel = this->getLabel(); |
| 8819 | this->jump(EndLabel, E); |
| 8820 | |
| 8821 | this->emitLabel(LabelTrue); |
| 8822 | if (!this->emitPopPtr(E)) |
| 8823 | return false; |
| 8824 | if (!this->emitConstBool(true, E)) |
| 8825 | return false; |
| 8826 | |
| 8827 | this->fallthrough(EndLabel); |
| 8828 | this->emitLabel(EndLabel); |
| 8829 | |
| 8830 | return true; |
| 8831 | } |
| 8832 | |
| 8833 | template <class Emitter> |
| 8834 | bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS, |
| 8835 | const BinaryOperator *E) { |
| 8836 | assert(E->isComparisonOp()); |
| 8837 | assert(!Initializing); |
| 8838 | if (DiscardResult) |
| 8839 | return this->discard(E: LHS) && this->discard(E: RHS); |
| 8840 | |
| 8841 | PrimType ElemT; |
| 8842 | bool LHSIsComplex; |
| 8843 | unsigned LHSOffset; |
| 8844 | if (LHS->getType()->isAnyComplexType()) { |
| 8845 | LHSIsComplex = true; |
| 8846 | ElemT = classifyComplexElementType(T: LHS->getType()); |
| 8847 | LHSOffset = allocateLocalPrimitive(Src: LHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 8848 | if (!this->visit(E: LHS)) |
| 8849 | return false; |
| 8850 | if (!this->emitSetLocal(PT_Ptr, LHSOffset, E)) |
| 8851 | return false; |
| 8852 | } else { |
| 8853 | LHSIsComplex = false; |
| 8854 | PrimType LHST = classifyPrim(LHS->getType()); |
| 8855 | LHSOffset = this->allocateLocalPrimitive(Src: LHS, Ty: LHST, /*IsConst=*/true); |
| 8856 | if (!this->visit(E: LHS)) |
| 8857 | return false; |
| 8858 | if (!this->emitSetLocal(LHST, LHSOffset, E)) |
| 8859 | return false; |
| 8860 | } |
| 8861 | |
| 8862 | bool RHSIsComplex; |
| 8863 | unsigned RHSOffset; |
| 8864 | if (RHS->getType()->isAnyComplexType()) { |
| 8865 | RHSIsComplex = true; |
| 8866 | ElemT = classifyComplexElementType(T: RHS->getType()); |
| 8867 | RHSOffset = allocateLocalPrimitive(Src: RHS, Ty: PT_Ptr, /*IsConst=*/true); |
| 8868 | if (!this->visit(E: RHS)) |
| 8869 | return false; |
| 8870 | if (!this->emitSetLocal(PT_Ptr, RHSOffset, E)) |
| 8871 | return false; |
| 8872 | } else { |
| 8873 | RHSIsComplex = false; |
| 8874 | PrimType RHST = classifyPrim(RHS->getType()); |
| 8875 | RHSOffset = this->allocateLocalPrimitive(Src: RHS, Ty: RHST, /*IsConst=*/true); |
| 8876 | if (!this->visit(E: RHS)) |
| 8877 | return false; |
| 8878 | if (!this->emitSetLocal(RHST, RHSOffset, E)) |
| 8879 | return false; |
| 8880 | } |
| 8881 | |
| 8882 | auto getElem = [&](unsigned LocalOffset, unsigned Index, |
| 8883 | bool IsComplex) -> bool { |
| 8884 | if (IsComplex) { |
| 8885 | if (!this->emitGetLocal(PT_Ptr, LocalOffset, E)) |
| 8886 | return false; |
| 8887 | return this->emitArrayElemPop(ElemT, Index, E); |
| 8888 | } |
| 8889 | return this->emitGetLocal(ElemT, LocalOffset, E); |
| 8890 | }; |
| 8891 | |
| 8892 | for (unsigned I = 0; I != 2; ++I) { |
| 8893 | // Get both values. |
| 8894 | if (!getElem(LHSOffset, I, LHSIsComplex)) |
| 8895 | return false; |
| 8896 | if (!getElem(RHSOffset, I, RHSIsComplex)) |
| 8897 | return false; |
| 8898 | // And compare them. |
| 8899 | if (!this->emitEQ(ElemT, E)) |
| 8900 | return false; |
| 8901 | |
| 8902 | if (!this->emitCastBoolUint8(E)) |
| 8903 | return false; |
| 8904 | } |
| 8905 | |
| 8906 | // We now have two bool values on the stack. Compare those. |
| 8907 | if (!this->emitAddUint8(E)) |
| 8908 | return false; |
| 8909 | if (!this->emitConstUint8(2, E)) |
| 8910 | return false; |
| 8911 | |
| 8912 | if (E->getOpcode() == BO_EQ) { |
| 8913 | if (!this->emitEQUint8(E)) |
| 8914 | return false; |
| 8915 | } else if (E->getOpcode() == BO_NE) { |
| 8916 | if (!this->emitNEUint8(E)) |
| 8917 | return false; |
| 8918 | } else |
| 8919 | return false; |
| 8920 | |
| 8921 | // In C, this returns an int. |
| 8922 | if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool) |
| 8923 | return this->emitCast(PT_Bool, ResT, E); |
| 8924 | return true; |
| 8925 | } |
| 8926 | |
| 8927 | /// When calling this, we have a pointer of the local-to-destroy |
| 8928 | /// on the stack. |
| 8929 | /// Emit destruction of record types (or arrays of record types). |
| 8930 | template <class Emitter> |
| 8931 | bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R, |
| 8932 | SourceInfo Loc) { |
| 8933 | assert(R); |
| 8934 | assert(!R->hasTrivialDtor()); |
| 8935 | const CXXDestructorDecl *Dtor = R->getDestructor(); |
| 8936 | assert(Dtor); |
| 8937 | const Function *DtorFunc = getFunction(FD: Dtor); |
| 8938 | if (!DtorFunc) |
| 8939 | return false; |
| 8940 | assert(DtorFunc->hasThisPointer()); |
| 8941 | assert(DtorFunc->getNumParams() == 1); |
| 8942 | return this->emitCall(DtorFunc, 0, Loc); |
| 8943 | } |
| 8944 | /// When calling this, we have a pointer of the local-to-destroy |
| 8945 | /// on the stack. |
| 8946 | /// Emit destruction of record types (or arrays of record types). |
| 8947 | template <class Emitter> |
| 8948 | bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc, |
| 8949 | SourceInfo Loc) { |
| 8950 | assert(Desc); |
| 8951 | assert(!Desc->hasTrivialDtor()); |
| 8952 | |
| 8953 | // Arrays. |
| 8954 | if (Desc->isArray()) { |
| 8955 | const Descriptor *ElemDesc = Desc->ElemDesc; |
| 8956 | assert(ElemDesc); |
| 8957 | |
| 8958 | unsigned N = Desc->getNumElems(); |
| 8959 | if (N == 0) |
| 8960 | return this->emitPopPtr(Loc); |
| 8961 | |
| 8962 | for (ssize_t I = N - 1; I >= 1; --I) { |
| 8963 | if (!this->emitConstUint64(I, Loc)) |
| 8964 | return false; |
| 8965 | if (!this->emitArrayElemPtrUint64(Loc)) |
| 8966 | return false; |
| 8967 | if (!this->emitDestructionPop(Desc: ElemDesc, Loc)) |
| 8968 | return false; |
| 8969 | } |
| 8970 | // Last iteration, removes the instance pointer from the stack. |
| 8971 | if (!this->emitConstUint64(0, Loc)) |
| 8972 | return false; |
| 8973 | if (!this->emitArrayElemPtrPopUint64(Loc)) |
| 8974 | return false; |
| 8975 | return this->emitDestructionPop(Desc: ElemDesc, Loc); |
| 8976 | } |
| 8977 | |
| 8978 | assert(Desc->ElemRecord); |
| 8979 | assert(!Desc->ElemRecord->hasTrivialDtor()); |
| 8980 | return this->emitRecordDestructionPop(R: Desc->ElemRecord, Loc); |
| 8981 | } |
| 8982 | |
| 8983 | /// Create a dummy pointer for the given decl (or expr) and |
| 8984 | /// push a pointer to it on the stack. |
| 8985 | template <class Emitter> |
| 8986 | bool Compiler<Emitter>::emitDummyPtr(DeclOrExpr D, const Expr *E, bool CU) { |
| 8987 | assert(!DiscardResult && "Should've been checked before" ); |
| 8988 | return this->emitGetOpaquePtr(D, CU, E); |
| 8989 | } |
| 8990 | |
| 8991 | template <class Emitter> |
| 8992 | bool Compiler<Emitter>::emitFloat(const APFloat &F, SourceInfo Info) { |
| 8993 | if (Floating::singleWord(F.getSemantics())) |
| 8994 | return this->emitConstFloat(Floating(F), Info); |
| 8995 | |
| 8996 | APInt I = F.bitcastToAPInt(); |
| 8997 | return this->emitConstFloat( |
| 8998 | Floating(const_cast<uint64_t *>(I.getRawData()), |
| 8999 | llvm::APFloatBase::SemanticsToEnum(Sem: F.getSemantics())), |
| 9000 | Info); |
| 9001 | } |
| 9002 | |
| 9003 | // This function is constexpr if and only if To, From, and the types of |
| 9004 | // all subobjects of To and From are types T such that... |
| 9005 | // (3.1) - is_union_v<T> is false; |
| 9006 | // (3.2) - is_pointer_v<T> is false; |
| 9007 | // (3.3) - is_member_pointer_v<T> is false; |
| 9008 | // (3.4) - is_volatile_v<T> is false; and |
| 9009 | // (3.5) - T has no non-static data members of reference type |
| 9010 | template <class Emitter> |
| 9011 | bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) { |
| 9012 | const Expr *SubExpr = E->getSubExpr(); |
| 9013 | QualType FromType = SubExpr->getType(); |
| 9014 | QualType ToType = E->getType(); |
| 9015 | OptPrimType ToT = classify(ToType); |
| 9016 | |
| 9017 | assert(!ToType->isReferenceType()); |
| 9018 | |
| 9019 | // Prepare storage for the result in case we discard. |
| 9020 | if (DiscardResult && !Initializing && !ToT) { |
| 9021 | UnsignedOrNone LocalIndex = allocateLocal(Src: E); |
| 9022 | if (!LocalIndex) |
| 9023 | return false; |
| 9024 | if (!this->emitGetPtrLocal(*LocalIndex, E)) |
| 9025 | return false; |
| 9026 | } |
| 9027 | |
| 9028 | // Get a pointer to the value-to-cast on the stack. |
| 9029 | // For CK_LValueToRValueBitCast, this is always an lvalue and |
| 9030 | // we later assume it to be one (i.e. a PT_Ptr). However, |
| 9031 | // we call this function for other utility methods where |
| 9032 | // a bitcast might be useful, so convert it to a PT_Ptr in that case. |
| 9033 | if (SubExpr->isGLValue() || FromType->isVectorType()) { |
| 9034 | if (!this->visit(E: SubExpr)) |
| 9035 | return false; |
| 9036 | } else if (OptPrimType FromT = classify(SubExpr)) { |
| 9037 | unsigned TempOffset = |
| 9038 | allocateLocalPrimitive(Src: SubExpr, Ty: *FromT, /*IsConst=*/true); |
| 9039 | if (!this->visit(E: SubExpr)) |
| 9040 | return false; |
| 9041 | if (!this->emitSetLocal(*FromT, TempOffset, E)) |
| 9042 | return false; |
| 9043 | if (!this->emitGetPtrLocal(TempOffset, E)) |
| 9044 | return false; |
| 9045 | } else { |
| 9046 | return false; |
| 9047 | } |
| 9048 | |
| 9049 | if (!ToT) { |
| 9050 | if (!this->emitBitCast(E)) |
| 9051 | return false; |
| 9052 | return DiscardResult ? this->emitPopPtr(E) : true; |
| 9053 | } |
| 9054 | assert(ToT); |
| 9055 | |
| 9056 | const llvm::fltSemantics *TargetSemantics = nullptr; |
| 9057 | if (ToT == PT_Float) |
| 9058 | TargetSemantics = &Ctx.getFloatSemantics(T: ToType); |
| 9059 | |
| 9060 | // Conversion to a primitive type. FromType can be another |
| 9061 | // primitive type, or a record/array. |
| 9062 | bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(K: BuiltinType::UChar) || |
| 9063 | ToType->isSpecificBuiltinType(K: BuiltinType::Char_U)); |
| 9064 | uint32_t ResultBitWidth = std::max(a: Ctx.getBitWidth(T: ToType), b: 8u); |
| 9065 | |
| 9066 | if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(), |
| 9067 | ResultBitWidth, TargetSemantics, |
| 9068 | ToType.getTypePtr(), E)) |
| 9069 | return false; |
| 9070 | |
| 9071 | if (DiscardResult) |
| 9072 | return this->emitPop(*ToT, E); |
| 9073 | |
| 9074 | return true; |
| 9075 | } |
| 9076 | |
| 9077 | /// Replicate a scalar value into every scalar element of an aggregate. |
| 9078 | /// The scalar is stored in a local at \p SrcOffset and a pointer to the |
| 9079 | /// destination must be on top of the interpreter stack. Each element receives |
| 9080 | /// the scalar, cast to its own type. |
| 9081 | template <class Emitter> |
| 9082 | bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT, |
| 9083 | unsigned SrcOffset, |
| 9084 | QualType DestType, |
| 9085 | const Expr *E) { |
| 9086 | // Vectors and matrices are treated as flat sequences of elements. |
| 9087 | unsigned NumElems = 0; |
| 9088 | QualType ElemType; |
| 9089 | if (const auto *VT = DestType->getAs<VectorType>()) { |
| 9090 | NumElems = VT->getNumElements(); |
| 9091 | ElemType = VT->getElementType(); |
| 9092 | } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) { |
| 9093 | NumElems = MT->getNumElementsFlattened(); |
| 9094 | ElemType = MT->getElementType(); |
| 9095 | } |
| 9096 | if (NumElems > 0) { |
| 9097 | PrimType ElemT = classifyPrim(ElemType); |
| 9098 | for (unsigned I = 0; I != NumElems; ++I) { |
| 9099 | if (!this->emitGetLocal(SrcT, SrcOffset, E)) |
| 9100 | return false; |
| 9101 | if (!this->emitPrimCast(FromT: SrcT, ToT: ElemT, ToQT: ElemType, E)) |
| 9102 | return false; |
| 9103 | if (!this->emitInitElem(ElemT, I, E)) |
| 9104 | return false; |
| 9105 | } |
| 9106 | return true; |
| 9107 | } |
| 9108 | |
| 9109 | // Arrays: primitive elements are filled directly; composite elements |
| 9110 | // require recursion into each sub-aggregate. |
| 9111 | if (const auto *AT = DestType->getAsArrayTypeUnsafe()) { |
| 9112 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 9113 | QualType ArrElemType = CAT->getElementType(); |
| 9114 | unsigned ArrSize = CAT->getZExtSize(); |
| 9115 | |
| 9116 | if (OptPrimType ElemT = classify(ArrElemType)) { |
| 9117 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 9118 | if (!this->emitGetLocal(SrcT, SrcOffset, E)) |
| 9119 | return false; |
| 9120 | if (!this->emitPrimCast(FromT: SrcT, ToT: *ElemT, ToQT: ArrElemType, E)) |
| 9121 | return false; |
| 9122 | if (!this->emitInitElem(*ElemT, I, E)) |
| 9123 | return false; |
| 9124 | } |
| 9125 | } else { |
| 9126 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 9127 | if (!this->emitConstUint32(I, E)) |
| 9128 | return false; |
| 9129 | if (!this->emitArrayElemPtrUint32(E)) |
| 9130 | return false; |
| 9131 | if (!emitHLSLAggregateSplat(SrcT, SrcOffset, DestType: ArrElemType, E)) |
| 9132 | return false; |
| 9133 | if (!this->emitFinishInitPop(E)) |
| 9134 | return false; |
| 9135 | } |
| 9136 | } |
| 9137 | return true; |
| 9138 | } |
| 9139 | |
| 9140 | // Records: fill base classes first, then named fields in declaration |
| 9141 | // order. |
| 9142 | if (DestType->isRecordType()) { |
| 9143 | const Record *R = getRecord(DestType); |
| 9144 | if (!R) |
| 9145 | return false; |
| 9146 | |
| 9147 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 9148 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) { |
| 9149 | const Record::Base *B = R->getBase(T: BS.getType()); |
| 9150 | assert(B); |
| 9151 | if (!this->emitGetPtrBase(B->Offset, E)) |
| 9152 | return false; |
| 9153 | if (!emitHLSLAggregateSplat(SrcT, SrcOffset, DestType: BS.getType(), E)) |
| 9154 | return false; |
| 9155 | if (!this->emitFinishInitPop(E)) |
| 9156 | return false; |
| 9157 | } |
| 9158 | } |
| 9159 | |
| 9160 | for (const Record::Field &F : R->fields()) { |
| 9161 | if (F.isUnnamedBitField()) |
| 9162 | continue; |
| 9163 | |
| 9164 | QualType FieldType = F.Decl->getType(); |
| 9165 | if (OptPrimType FieldT = F.T) { |
| 9166 | if (!this->emitGetLocal(SrcT, SrcOffset, E)) |
| 9167 | return false; |
| 9168 | if (!this->emitPrimCast(FromT: SrcT, ToT: *FieldT, ToQT: FieldType, E)) |
| 9169 | return false; |
| 9170 | if (F.isBitField()) { |
| 9171 | if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E)) |
| 9172 | return false; |
| 9173 | } else { |
| 9174 | if (!this->emitInitField(*FieldT, F.Offset, E)) |
| 9175 | return false; |
| 9176 | } |
| 9177 | } else { |
| 9178 | if (!this->emitGetPtrField(F.Offset, E)) |
| 9179 | return false; |
| 9180 | if (!emitHLSLAggregateSplat(SrcT, SrcOffset, DestType: FieldType, E)) |
| 9181 | return false; |
| 9182 | if (!this->emitPopPtr(E)) |
| 9183 | return false; |
| 9184 | } |
| 9185 | } |
| 9186 | return true; |
| 9187 | } |
| 9188 | |
| 9189 | return false; |
| 9190 | } |
| 9191 | |
| 9192 | /// Return the total number of scalar elements in a type. This is used |
| 9193 | /// to cap how many source elements are extracted during an elementwise cast, |
| 9194 | /// so we never flatten more than the destination can hold. |
| 9195 | template <class Emitter> |
| 9196 | unsigned Compiler<Emitter>::countHLSLFlatElements(QualType Ty) { |
| 9197 | // Vector and matrix types are treated as flat sequences of elements. |
| 9198 | if (const auto *VT = Ty->getAs<VectorType>()) |
| 9199 | return VT->getNumElements(); |
| 9200 | if (const auto *MT = Ty->getAs<ConstantMatrixType>()) |
| 9201 | return MT->getNumElementsFlattened(); |
| 9202 | // Arrays: total count is array size * scalar elements per element. |
| 9203 | if (const auto *AT = Ty->getAsArrayTypeUnsafe()) { |
| 9204 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 9205 | return CAT->getZExtSize() * countHLSLFlatElements(Ty: CAT->getElementType()); |
| 9206 | } |
| 9207 | // Records: sum scalar element counts of base classes and named fields. |
| 9208 | if (Ty->isRecordType()) { |
| 9209 | const Record *R = getRecord(Ty); |
| 9210 | if (!R) |
| 9211 | return 0; |
| 9212 | unsigned Count = 0; |
| 9213 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 9214 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) |
| 9215 | Count += countHLSLFlatElements(Ty: BS.getType()); |
| 9216 | } |
| 9217 | for (const Record::Field &F : R->fields()) { |
| 9218 | if (F.isUnnamedBitField()) |
| 9219 | continue; |
| 9220 | Count += countHLSLFlatElements(Ty: F.Decl->getType()); |
| 9221 | } |
| 9222 | return Count; |
| 9223 | } |
| 9224 | // Scalar primitive types contribute one element. |
| 9225 | if (canClassify(Ty)) |
| 9226 | return 1; |
| 9227 | return 0; |
| 9228 | } |
| 9229 | |
| 9230 | /// Walk a source aggregate and extract every scalar element into its own local |
| 9231 | /// variable. The results are appended to \p Elements in declaration order, |
| 9232 | /// stopping once \p MaxElements have been collected. A pointer to the |
| 9233 | /// source aggregate must be stored in the local at \p SrcOffset. |
| 9234 | template <class Emitter> |
| 9235 | bool Compiler<Emitter>::emitHLSLFlattenAggregate( |
| 9236 | QualType SrcType, unsigned SrcOffset, |
| 9237 | SmallVectorImpl<HLSLFlatElement> &Elements, unsigned MaxElements, |
| 9238 | const Expr *E) { |
| 9239 | |
| 9240 | // Save a scalar value from the stack into a new local and record it. |
| 9241 | auto saveToLocal = [&](PrimType T) -> bool { |
| 9242 | unsigned Offset = allocateLocalPrimitive(Src: E, Ty: T, /*IsConst=*/true); |
| 9243 | if (!this->emitSetLocal(T, Offset, E)) |
| 9244 | return false; |
| 9245 | Elements.push_back({Offset, T}); |
| 9246 | return true; |
| 9247 | }; |
| 9248 | |
| 9249 | // Save a pointer from the stack into a new local for later use. |
| 9250 | auto savePtrToLocal = [&]() -> UnsignedOrNone { |
| 9251 | unsigned Offset = allocateLocalPrimitive(Src: E, Ty: PT_Ptr, /*IsConst=*/true); |
| 9252 | if (!this->emitSetLocal(PT_Ptr, Offset, E)) |
| 9253 | return std::nullopt; |
| 9254 | return Offset; |
| 9255 | }; |
| 9256 | |
| 9257 | // Vectors and matrices are flat sequences of elements. |
| 9258 | unsigned NumElems = 0; |
| 9259 | QualType ElemType; |
| 9260 | if (const auto *VT = SrcType->getAs<VectorType>()) { |
| 9261 | NumElems = VT->getNumElements(); |
| 9262 | ElemType = VT->getElementType(); |
| 9263 | } else if (const auto *MT = SrcType->getAs<ConstantMatrixType>()) { |
| 9264 | NumElems = MT->getNumElementsFlattened(); |
| 9265 | ElemType = MT->getElementType(); |
| 9266 | } |
| 9267 | if (NumElems > 0) { |
| 9268 | PrimType ElemT = classifyPrim(ElemType); |
| 9269 | for (unsigned I = 0; I != NumElems && Elements.size() < MaxElements; ++I) { |
| 9270 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 9271 | return false; |
| 9272 | if (!this->emitArrayElemPop(ElemT, I, E)) |
| 9273 | return false; |
| 9274 | if (!saveToLocal(ElemT)) |
| 9275 | return false; |
| 9276 | } |
| 9277 | return true; |
| 9278 | } |
| 9279 | |
| 9280 | // Arrays: primitive elements are extracted directly; composite elements |
| 9281 | // require recursion into each sub-aggregate. |
| 9282 | if (const auto *AT = SrcType->getAsArrayTypeUnsafe()) { |
| 9283 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 9284 | QualType ArrElemType = CAT->getElementType(); |
| 9285 | unsigned ArrSize = CAT->getZExtSize(); |
| 9286 | |
| 9287 | if (OptPrimType ElemT = classify(ArrElemType)) { |
| 9288 | for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) { |
| 9289 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 9290 | return false; |
| 9291 | if (!this->emitArrayElemPop(*ElemT, I, E)) |
| 9292 | return false; |
| 9293 | if (!saveToLocal(*ElemT)) |
| 9294 | return false; |
| 9295 | } |
| 9296 | } else { |
| 9297 | for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) { |
| 9298 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 9299 | return false; |
| 9300 | if (!this->emitConstUint32(I, E)) |
| 9301 | return false; |
| 9302 | if (!this->emitArrayElemPtrPopUint32(E)) |
| 9303 | return false; |
| 9304 | UnsignedOrNone ElemPtrOffset = savePtrToLocal(); |
| 9305 | if (!ElemPtrOffset) |
| 9306 | return false; |
| 9307 | if (!emitHLSLFlattenAggregate(SrcType: ArrElemType, SrcOffset: *ElemPtrOffset, Elements, |
| 9308 | MaxElements, E)) |
| 9309 | return false; |
| 9310 | } |
| 9311 | } |
| 9312 | return true; |
| 9313 | } |
| 9314 | |
| 9315 | // Records: base classes come first, then named fields in declaration |
| 9316 | // order. |
| 9317 | if (SrcType->isRecordType()) { |
| 9318 | const Record *R = getRecord(SrcType); |
| 9319 | if (!R) |
| 9320 | return false; |
| 9321 | |
| 9322 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 9323 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) { |
| 9324 | if (Elements.size() >= MaxElements) |
| 9325 | break; |
| 9326 | const Record::Base *B = R->getBase(T: BS.getType()); |
| 9327 | assert(B); |
| 9328 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 9329 | return false; |
| 9330 | if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, E)) |
| 9331 | return false; |
| 9332 | UnsignedOrNone BasePtrOffset = savePtrToLocal(); |
| 9333 | if (!BasePtrOffset) |
| 9334 | return false; |
| 9335 | if (!emitHLSLFlattenAggregate(SrcType: BS.getType(), SrcOffset: *BasePtrOffset, Elements, |
| 9336 | MaxElements, E)) |
| 9337 | return false; |
| 9338 | } |
| 9339 | } |
| 9340 | |
| 9341 | for (const Record::Field &F : R->fields()) { |
| 9342 | if (Elements.size() >= MaxElements) |
| 9343 | break; |
| 9344 | if (F.isUnnamedBitField()) |
| 9345 | continue; |
| 9346 | |
| 9347 | QualType FieldType = F.Decl->getType(); |
| 9348 | if (!this->emitGetLocal(PT_Ptr, SrcOffset, E)) |
| 9349 | return false; |
| 9350 | if (!this->emitGetPtrFieldPop(F.Offset, E)) |
| 9351 | return false; |
| 9352 | |
| 9353 | if (OptPrimType FieldT = F.T) { |
| 9354 | if (!this->emitLoadPop(*FieldT, E)) |
| 9355 | return false; |
| 9356 | if (!saveToLocal(*FieldT)) |
| 9357 | return false; |
| 9358 | } else { |
| 9359 | UnsignedOrNone FieldPtrOffset = savePtrToLocal(); |
| 9360 | if (!FieldPtrOffset) |
| 9361 | return false; |
| 9362 | if (!emitHLSLFlattenAggregate(SrcType: FieldType, SrcOffset: *FieldPtrOffset, Elements, |
| 9363 | MaxElements, E)) |
| 9364 | return false; |
| 9365 | } |
| 9366 | } |
| 9367 | return true; |
| 9368 | } |
| 9369 | |
| 9370 | return false; |
| 9371 | } |
| 9372 | |
| 9373 | /// Populate an HLSL aggregate from a flat list of previously extracted source |
| 9374 | /// elements, casting each to the corresponding destination element type. |
| 9375 | /// \p ElemIdx tracks the current position in \p Elements and is advanced as |
| 9376 | /// elements are consumed. A pointer to the destination must be on top of the |
| 9377 | /// interpreter stack. |
| 9378 | template <class Emitter> |
| 9379 | bool Compiler<Emitter>::emitHLSLConstructAggregate( |
| 9380 | QualType DestType, ArrayRef<HLSLFlatElement> Elements, unsigned &ElemIdx, |
| 9381 | const Expr *E) { |
| 9382 | |
| 9383 | // Consume the next source element, cast it, and leave it on the stack. |
| 9384 | auto loadAndCast = [&](PrimType DestT, QualType DestQT) -> bool { |
| 9385 | const auto &Src = Elements[ElemIdx++]; |
| 9386 | if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E)) |
| 9387 | return false; |
| 9388 | return this->emitPrimCast(FromT: Src.Type, ToT: DestT, ToQT: DestQT, E); |
| 9389 | }; |
| 9390 | |
| 9391 | // Vectors and matrices are flat sequences of elements. |
| 9392 | unsigned NumElems = 0; |
| 9393 | QualType ElemType; |
| 9394 | if (const auto *VT = DestType->getAs<VectorType>()) { |
| 9395 | NumElems = VT->getNumElements(); |
| 9396 | ElemType = VT->getElementType(); |
| 9397 | } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) { |
| 9398 | NumElems = MT->getNumElementsFlattened(); |
| 9399 | ElemType = MT->getElementType(); |
| 9400 | } |
| 9401 | if (NumElems > 0) { |
| 9402 | PrimType DestElemT = classifyPrim(ElemType); |
| 9403 | for (unsigned I = 0; I != NumElems; ++I) { |
| 9404 | if (!loadAndCast(DestElemT, ElemType)) |
| 9405 | return false; |
| 9406 | if (!this->emitInitElem(DestElemT, I, E)) |
| 9407 | return false; |
| 9408 | } |
| 9409 | return true; |
| 9410 | } |
| 9411 | |
| 9412 | // Arrays: primitive elements are filled directly; composite elements |
| 9413 | // require recursion into each sub-aggregate. |
| 9414 | if (const auto *AT = DestType->getAsArrayTypeUnsafe()) { |
| 9415 | const auto *CAT = cast<ConstantArrayType>(Val: AT); |
| 9416 | QualType ArrElemType = CAT->getElementType(); |
| 9417 | unsigned ArrSize = CAT->getZExtSize(); |
| 9418 | |
| 9419 | if (OptPrimType ElemT = classify(ArrElemType)) { |
| 9420 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 9421 | if (!loadAndCast(*ElemT, ArrElemType)) |
| 9422 | return false; |
| 9423 | if (!this->emitInitElem(*ElemT, I, E)) |
| 9424 | return false; |
| 9425 | } |
| 9426 | } else { |
| 9427 | for (unsigned I = 0; I != ArrSize; ++I) { |
| 9428 | if (!this->emitConstUint32(I, E)) |
| 9429 | return false; |
| 9430 | if (!this->emitArrayElemPtrUint32(E)) |
| 9431 | return false; |
| 9432 | if (!emitHLSLConstructAggregate(ArrElemType, Elements, ElemIdx, E)) |
| 9433 | return false; |
| 9434 | if (!this->emitFinishInitPop(E)) |
| 9435 | return false; |
| 9436 | } |
| 9437 | } |
| 9438 | return true; |
| 9439 | } |
| 9440 | |
| 9441 | // Records: base classes come first, then named fields in declaration |
| 9442 | // order. |
| 9443 | if (DestType->isRecordType()) { |
| 9444 | const Record *R = getRecord(DestType); |
| 9445 | if (!R) |
| 9446 | return false; |
| 9447 | |
| 9448 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: R->getDecl())) { |
| 9449 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) { |
| 9450 | const Record::Base *B = R->getBase(T: BS.getType()); |
| 9451 | assert(B); |
| 9452 | if (!this->emitGetPtrBase(B->Offset, E)) |
| 9453 | return false; |
| 9454 | if (!emitHLSLConstructAggregate(BS.getType(), Elements, ElemIdx, E)) |
| 9455 | return false; |
| 9456 | if (!this->emitFinishInitPop(E)) |
| 9457 | return false; |
| 9458 | } |
| 9459 | } |
| 9460 | |
| 9461 | for (const Record::Field &F : R->fields()) { |
| 9462 | if (F.isUnnamedBitField()) |
| 9463 | continue; |
| 9464 | |
| 9465 | QualType FieldType = F.Decl->getType(); |
| 9466 | if (OptPrimType FieldT = F.T) { |
| 9467 | if (!loadAndCast(*FieldT, FieldType)) |
| 9468 | return false; |
| 9469 | if (F.isBitField()) { |
| 9470 | if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E)) |
| 9471 | return false; |
| 9472 | } else { |
| 9473 | if (!this->emitInitField(*FieldT, F.Offset, E)) |
| 9474 | return false; |
| 9475 | } |
| 9476 | } else { |
| 9477 | if (!this->emitGetPtrField(F.Offset, E)) |
| 9478 | return false; |
| 9479 | if (!emitHLSLConstructAggregate(FieldType, Elements, ElemIdx, E)) |
| 9480 | return false; |
| 9481 | if (!this->emitPopPtr(E)) |
| 9482 | return false; |
| 9483 | } |
| 9484 | } |
| 9485 | return true; |
| 9486 | } |
| 9487 | |
| 9488 | return false; |
| 9489 | } |
| 9490 | |
| 9491 | namespace clang { |
| 9492 | namespace interp { |
| 9493 | |
| 9494 | template class Compiler<ByteCodeEmitter>; |
| 9495 | template class Compiler<EvalEmitter>; |
| 9496 | |
| 9497 | } // namespace interp |
| 9498 | } // namespace clang |
| 9499 | |