1//===----------------------------------------------------------------------===//
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// This contains code dealing with C++ code generation of classes
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenCXXABI.h"
14#include "CIRGenFunction.h"
15#include "CIRGenValue.h"
16
17#include "clang/AST/ExprCXX.h"
18#include "clang/AST/RecordLayout.h"
19#include "clang/AST/Type.h"
20#include "clang/CIR/Dialect/IR/CIRDialect.h"
21#include "clang/CIR/MissingFeatures.h"
22#include "clang/CodeGenUtils/ClassUtils.h"
23#include "clang/CodeGenUtils/RecordLayoutUtils.h"
24
25using namespace clang;
26using namespace clang::CIRGen;
27
28namespace {
29// TODO(cir): a lot of what we see under this namespace is a strong candidate
30// to be shared between LLVM and CIR codegen.
31
32/// RAII object to indicate that codegen is copying the value representation
33/// instead of the object representation. Useful when copying a struct or
34/// class which has uninitialized members and we're only performing
35/// lvalue-to-rvalue conversion on the object but not its members.
36class CopyingValueRepresentation {
37public:
38 explicit CopyingValueRepresentation(CIRGenFunction &cgf)
39 : cgf(cgf), oldSanOpts(cgf.sanOpts) {
40 cgf.sanOpts.set(K: SanitizerKind::Bool, Value: false);
41 cgf.sanOpts.set(K: SanitizerKind::Enum, Value: false);
42 }
43 ~CopyingValueRepresentation() { cgf.sanOpts = oldSanOpts; }
44
45private:
46 CIRGenFunction &cgf;
47 SanitizerSet oldSanOpts;
48};
49
50class FieldMemcpyizer {
51public:
52 FieldMemcpyizer(CIRGenFunction &cgf, const CXXRecordDecl *classDecl,
53 SourceLocation methodLoc, const VarDecl *srcRec)
54 : cgf(cgf), classDecl(classDecl), methodLoc(methodLoc), srcRec(srcRec),
55 recLayout(cgf.getContext().getASTRecordLayout(D: classDecl)),
56 firstField(nullptr), lastField(nullptr), firstFieldOffset(0),
57 lastFieldOffset(0), lastAddedFieldIndex(0) {}
58
59 bool isMemcpyableField(FieldDecl *f) const {
60 // Never memcpy fields when we are adding poised paddings.
61 if (cgf.getContext().getLangOpts().SanitizeAddressFieldPadding)
62 return false;
63 Qualifiers qual = f->getType().getQualifiers();
64 if (qual.hasVolatile() || qual.hasObjCLifetime())
65 return false;
66 assert(!cir::MissingFeatures::pointerAuthentication());
67 assert(!cir::MissingFeatures::pointerFieldProtection());
68 return true;
69 }
70
71 void addMemcpyableField(FieldDecl *f) {
72 if (CodeGenUtils::isEmptyFieldForLayout(Ctx: cgf.getContext(), FD: f))
73 return;
74 if (f->isZeroSize(Ctx: cgf.getContext()))
75 return;
76 if (!firstField)
77 addInitialField(f);
78 else
79 addNextField(f);
80 }
81
82 CharUnits getMemcpySize(uint64_t firstByteOffset) const {
83 ASTContext &astContext = cgf.getContext();
84 unsigned lastFieldSize =
85 lastField->isBitField()
86 ? lastField->getBitWidthValue()
87 : astContext.toBits(
88 CharSize: astContext.getTypeInfoDataSizeInChars(T: lastField->getType())
89 .Width);
90 uint64_t memcpySizeBits = lastFieldOffset + lastFieldSize -
91 firstByteOffset + astContext.getCharWidth() - 1;
92 return astContext.toCharUnitsFromBits(BitSize: memcpySizeBits);
93 }
94
95 void emitMemcpy() {
96 // Give the subclass a chance to bail out if it feels the memcpy isn't worth
97 // it (e.g. Hasn't aggregated enough data).
98 if (!firstField)
99 return;
100
101 uint64_t firstByteOffset;
102 if (firstField->isBitField()) {
103 const CIRGenRecordLayout &rl =
104 cgf.getTypes().getCIRGenRecordLayout(rd: firstField->getParent());
105 const CIRGenBitFieldInfo &bfInfo = rl.getBitFieldInfo(fd: firstField);
106 // firstFieldOffset is not appropriate for bitfields,
107 // we need to use the storage offset instead.
108 firstByteOffset = cgf.getContext().toBits(CharSize: bfInfo.storageOffset);
109 } else {
110 firstByteOffset = firstFieldOffset;
111 }
112
113 CharUnits memcpySize = getMemcpySize(firstByteOffset);
114 CanQualType recordTy = cgf.getContext().getCanonicalTagType(TD: classDecl);
115 Address thisPtr = cgf.loadCXXThisAddress();
116 LValue destLv = cgf.makeAddrLValue(addr: thisPtr, ty: recordTy);
117 LValue dest = cgf.emitLValueForFieldInitialization(base: destLv, field: firstField,
118 fieldName: firstField->getName());
119 cir::LoadOp srcPtr = cgf.getBuilder().createLoad(
120 loc: cgf.getLoc(srcLoc: methodLoc), addr: cgf.getAddrOfLocalVar(vd: srcRec));
121 LValue srcLv = cgf.makeNaturalAlignAddrLValue(val: srcPtr, ty: recordTy);
122 LValue src = cgf.emitLValueForFieldInitialization(base: srcLv, field: firstField,
123 fieldName: firstField->getName());
124
125 emitMemcpyIR(destPtr: dest.isBitField() ? dest.getBitFieldAddress()
126 : dest.getAddress(),
127 srcPtr: src.isBitField() ? src.getBitFieldAddress() : src.getAddress(),
128 size: memcpySize);
129 reset();
130 }
131
132 void reset() { firstField = nullptr; }
133
134protected:
135 CIRGenFunction &cgf;
136 const CXXRecordDecl *classDecl;
137 SourceLocation methodLoc;
138
139private:
140 void emitMemcpyIR(Address destPtr, Address srcPtr, CharUnits size) {
141 mlir::Location loc = cgf.getLoc(srcLoc: methodLoc);
142 CIRGenBuilderTy &builder = cgf.getBuilder();
143 mlir::Value sizeOp =
144 builder.getConstInt(loc, t: cgf.sizeTy, c: size.getQuantity());
145 Address destCast = destPtr.withElementType(builder, ElemTy: cgf.voidTy);
146 Address srcCast = srcPtr.withElementType(builder, ElemTy: cgf.voidTy);
147 builder.createMemCpy(loc, dst: destCast, src: srcCast, len: sizeOp);
148 assert(!cir::MissingFeatures::generateDebugInfo());
149 }
150
151 void addInitialField(FieldDecl *f) {
152 firstField = f;
153 lastField = f;
154 firstFieldOffset = recLayout.getFieldOffset(FieldNo: f->getFieldIndex());
155 lastFieldOffset = firstFieldOffset;
156 lastAddedFieldIndex = f->getFieldIndex();
157 }
158
159 void addNextField(FieldDecl *f) {
160 // For the most part, the following invariant will hold:
161 // f->getFieldIndex() == lastAddedFieldIndex + 1
162 // The one exception is that Sema won't add a copy-initializer for an
163 // unnamed bitfield, which will show up here as a gap in the sequence.
164 assert(f->getFieldIndex() >= lastAddedFieldIndex + 1 &&
165 "Cannot aggregate fields out of order.");
166 lastAddedFieldIndex = f->getFieldIndex();
167
168 // The 'first' and 'last' fields are chosen by offset, rather than field
169 // index. This allows the code to support bitfields, as well as regular
170 // fields.
171 uint64_t fOffset = recLayout.getFieldOffset(FieldNo: f->getFieldIndex());
172 if (fOffset < firstFieldOffset) {
173 firstField = f;
174 firstFieldOffset = fOffset;
175 } else if (fOffset >= lastFieldOffset) {
176 lastField = f;
177 lastFieldOffset = fOffset;
178 }
179 }
180
181 const VarDecl *srcRec;
182 const ASTRecordLayout &recLayout;
183 FieldDecl *firstField;
184 FieldDecl *lastField;
185 uint64_t firstFieldOffset, lastFieldOffset;
186 unsigned lastAddedFieldIndex;
187};
188
189class AssignmentMemcpyizer : public FieldMemcpyizer {
190 // Returns the memcpyable field copied by the given statement, if one
191 // exists. Otherwise returns null.
192 FieldDecl *getMemcpyableField(Stmt *s) {
193 if (!assignmentsMemcpyable)
194 return nullptr;
195 if (auto *bo = dyn_cast<BinaryOperator>(Val: s)) {
196 // Recognise trivial assignments.
197 if (bo->getOpcode() != BO_Assign)
198 return nullptr;
199 auto *me = dyn_cast<MemberExpr>(Val: bo->getLHS());
200 if (!me)
201 return nullptr;
202 auto *field = dyn_cast<FieldDecl>(Val: me->getMemberDecl());
203 if (!field || !isMemcpyableField(f: field))
204 return nullptr;
205 Stmt *rhs = bo->getRHS();
206 if (auto *ec = dyn_cast<ImplicitCastExpr>(Val: rhs))
207 rhs = ec->getSubExpr();
208 if (!rhs)
209 return nullptr;
210 if (auto *me2 = dyn_cast<MemberExpr>(Val: rhs)) {
211 if (me2->getMemberDecl() == field)
212 return field;
213 }
214 return nullptr;
215 } else if (isa<CXXMemberCallExpr>(Val: s)) {
216 // We want to represent all calls explicitly for analysis purposes.
217 return nullptr;
218 } else if (auto *ce = dyn_cast<CallExpr>(Val: s)) {
219 // TODO(cir): https://github.com/llvm/clangir/issues/1177: This can result
220 // in memcpys instead of calls to trivial member functions.
221 auto *fd = dyn_cast<FunctionDecl>(Val: ce->getCalleeDecl());
222 if (!fd || fd->getBuiltinID() != Builtin::BI__builtin_memcpy)
223 return nullptr;
224 Expr *dstPtr = ce->getArg(Arg: 0);
225 if (auto *dc = dyn_cast<ImplicitCastExpr>(Val: dstPtr))
226 dstPtr = dc->getSubExpr();
227 auto *duo = dyn_cast<UnaryOperator>(Val: dstPtr);
228 if (!duo || duo->getOpcode() != UO_AddrOf)
229 return nullptr;
230 auto *me = dyn_cast<MemberExpr>(Val: duo->getSubExpr());
231 if (!me)
232 return nullptr;
233 auto *field = dyn_cast<FieldDecl>(Val: me->getMemberDecl());
234 if (!field || !isMemcpyableField(f: field))
235 return nullptr;
236 Expr *srcPtr = ce->getArg(Arg: 1);
237 if (auto *sc = dyn_cast<ImplicitCastExpr>(Val: srcPtr))
238 srcPtr = sc->getSubExpr();
239 auto *suo = dyn_cast<UnaryOperator>(Val: srcPtr);
240 if (!suo || suo->getOpcode() != UO_AddrOf)
241 return nullptr;
242 auto *me2 = dyn_cast<MemberExpr>(Val: suo->getSubExpr());
243 if (!me2 || field != dyn_cast<FieldDecl>(Val: me2->getMemberDecl()))
244 return nullptr;
245 return field;
246 }
247
248 return nullptr;
249 }
250
251 const bool assignmentsMemcpyable;
252 SmallVector<Stmt *, 16> aggregatedStmts;
253
254public:
255 AssignmentMemcpyizer(CIRGenFunction &cgf, const CXXMethodDecl *ad,
256 FunctionArgList &args)
257 : FieldMemcpyizer(cgf, ad->getParent(), ad->getLocation(),
258 args[args.size() - 1]),
259 assignmentsMemcpyable(cgf.getLangOpts().getGC() == LangOptions::NonGC) {
260 assert(args.size() == 2);
261 }
262
263 void emitAssignment(Stmt *s) {
264 FieldDecl *f = getMemcpyableField(s);
265 if (f) {
266 addMemcpyableField(f);
267 aggregatedStmts.push_back(Elt: s);
268 } else {
269 emitAggregatedStmts();
270 if (cgf.emitStmt(s, /*useCurrentScope=*/true).failed())
271 assert(cgf.cgm.getDiags().hasErrorOccurred());
272 }
273 }
274
275 void emitAggregatedStmts() {
276 if (aggregatedStmts.size() <= 1) {
277 if (!aggregatedStmts.empty()) {
278 CopyingValueRepresentation cvr(cgf);
279 if (cgf.emitStmt(s: aggregatedStmts[0], /*useCurrentScope=*/true).failed())
280 assert(cgf.cgm.getDiags().hasErrorOccurred());
281 }
282 reset();
283 }
284
285 assert(!cir::MissingFeatures::generateDebugInfo());
286 emitMemcpy();
287 aggregatedStmts.clear();
288 }
289
290 void finish() { emitAggregatedStmts(); }
291};
292} // namespace
293
294/// Return the smallest possible amount of storage that might be allocated
295/// starting from the beginning of an object of a particular class.
296///
297/// This may be smaller than sizeof(RD) if RD has virtual base classes.
298CharUnits CIRGenModule::getMinimumClassObjectSize(const CXXRecordDecl *rd) {
299 if (!rd->hasDefinition())
300 return CharUnits::One();
301
302 auto &layout = getASTContext().getASTRecordLayout(D: rd);
303
304 // If the class is final, then we know that the pointer points to an
305 // object of that type and can use the full alignment.
306 if (rd->isEffectivelyFinal())
307 return layout.getSize();
308
309 // Otherwise, we have to assume it could be a subclass.
310 return std::max(a: layout.getNonVirtualSize(), b: CharUnits::One());
311}
312
313/// Checks whether the given constructor is a valid subject for the
314/// complete-to-base constructor delegation optimization, i.e. emitting the
315/// complete constructor as a simple call to the base constructor.
316bool CIRGenFunction::isConstructorDelegationValid(
317 const CXXConstructorDecl *ctor) {
318 // Currently we disable the optimization for classes with virtual bases
319 // because (1) the address of parameter variables need to be consistent across
320 // all initializers but (2) the delegate function call necessarily creates a
321 // second copy of the parameter variable.
322 //
323 // The limiting example (purely theoretical AFAIK):
324 // struct A { A(int &c) { c++; } };
325 // struct A : virtual A {
326 // B(int count) : A(count) { printf("%d\n", count); }
327 // };
328 // ...although even this example could in principle be emitted as a delegation
329 // since the address of the parameter doesn't escape.
330 if (ctor->getParent()->getNumVBases())
331 return false;
332
333 // We also disable the optimization for variadic functions because it's
334 // impossible to "re-pass" varargs.
335 if (ctor->getType()->castAs<FunctionProtoType>()->isVariadic())
336 return false;
337
338 // FIXME: Decide if we can do a delegation of a delegating constructor.
339 if (ctor->isDelegatingConstructor())
340 return false;
341
342 return true;
343}
344
345static void emitLValueForAnyFieldInitialization(CIRGenFunction &cgf,
346 CXXCtorInitializer *memberInit,
347 LValue &lhs) {
348 FieldDecl *field = memberInit->getAnyMember();
349 if (memberInit->isIndirectMemberInitializer()) {
350 // If we are initializing an anonymous union field, drill down to the field.
351 IndirectFieldDecl *indirectField = memberInit->getIndirectMember();
352 for (const auto *nd : indirectField->chain()) {
353 auto *fd = cast<clang::FieldDecl>(Val: nd);
354 lhs = cgf.emitLValueForFieldInitialization(base: lhs, field: fd, fieldName: fd->getName());
355 }
356 } else {
357 lhs = cgf.emitLValueForFieldInitialization(base: lhs, field, fieldName: field->getName());
358 }
359}
360
361static void emitMemberInitializer(CIRGenFunction &cgf,
362 const CXXRecordDecl *classDecl,
363 CXXCtorInitializer *memberInit,
364 const CXXConstructorDecl *constructor,
365 FunctionArgList &args) {
366 assert(memberInit->isAnyMemberInitializer() &&
367 "Must have member initializer!");
368 assert(memberInit->getInit() && "Must have initializer!");
369
370 assert(!cir::MissingFeatures::generateDebugInfo());
371
372 // non-static data member initializers
373 FieldDecl *field = memberInit->getAnyMember();
374 QualType fieldType = field->getType();
375
376 mlir::Value thisPtr = cgf.loadCXXThis();
377 CanQualType recordTy = cgf.getContext().getCanonicalTagType(TD: classDecl);
378
379 // If a base constructor is being emitted, create an LValue that has the
380 // non-virtual alignment.
381 LValue lhs = (cgf.curGD.getCtorType() == Ctor_Base)
382 ? cgf.makeNaturalAlignPointeeAddrLValue(v: thisPtr, t: recordTy)
383 : cgf.makeNaturalAlignAddrLValue(val: thisPtr, ty: recordTy);
384
385 emitLValueForAnyFieldInitialization(cgf, memberInit, lhs);
386
387 // Special case: If we are in a copy or move constructor, and we are copying
388 // an array off PODs or classes with trivial copy constructors, ignore the AST
389 // and perform the copy we know is equivalent.
390 // FIXME: This is hacky at best... if we had a bit more explicit information
391 // in the AST, we could generalize it more easily.
392 const ConstantArrayType *array =
393 cgf.getContext().getAsConstantArrayType(T: fieldType);
394 if (array && constructor->isDefaulted() &&
395 constructor->isCopyOrMoveConstructor()) {
396 QualType baseElementTy = cgf.getContext().getBaseElementType(VAT: array);
397 // NOTE(cir): CodeGen allows record types to be memcpy'd if applicable,
398 // whereas ClangIR wants to represent all object construction explicitly.
399 if (!baseElementTy->isRecordType()) {
400 unsigned srcArgIndex =
401 cgf.cgm.getCXXABI().getSrcArgforCopyCtor(constructor, args);
402 cir::LoadOp srcPtr = cgf.getBuilder().createLoad(
403 loc: cgf.getLoc(srcLoc: memberInit->getSourceLocation()),
404 addr: cgf.getAddrOfLocalVar(vd: args[srcArgIndex]));
405 LValue thisRhslv = cgf.makeNaturalAlignAddrLValue(val: srcPtr, ty: recordTy);
406 LValue src = cgf.emitLValueForFieldInitialization(base: thisRhslv, field,
407 fieldName: field->getName());
408
409 // Copy the aggregate.
410 cgf.emitAggregateCopy(dest: lhs, src, eltTy: fieldType,
411 mayOverlap: cgf.getOverlapForFieldInit(fd: field),
412 isVolatile: lhs.isVolatileQualified());
413 // Ensure that we destroy the objects if an exception is thrown later in
414 // the constructor.
415 assert(!cgf.needsEHCleanup(fieldType.isDestructedType()) &&
416 "Arrays of non-record types shouldn't need EH cleanup");
417 return;
418 }
419 }
420
421 cgf.emitInitializerForField(field, lhs, init: memberInit->getInit());
422}
423
424namespace {
425/// Call the destructor for a direct base class.
426struct CallBaseDtor final : EHScopeStack::Cleanup {
427 const CXXRecordDecl *baseClass;
428 bool baseIsVirtual;
429 CallBaseDtor(const CXXRecordDecl *base, bool baseIsVirtual)
430 : baseClass(base), baseIsVirtual(baseIsVirtual) {}
431
432 void emit(CIRGenFunction &cgf, Flags flags) override {
433 const CXXRecordDecl *derivedClass =
434 cast<CXXMethodDecl>(Val: cgf.curFuncDecl)->getParent();
435
436 const CXXDestructorDecl *d = baseClass->getDestructor();
437 // We are already inside a destructor, so presumably the object being
438 // destroyed should have the expected type.
439 QualType thisTy = d->getFunctionObjectParameterType();
440 assert(cgf.currSrcLoc && "expected source location");
441 Address addr = cgf.getAddressOfDirectBaseInCompleteClass(
442 loc: cgf.getLoc(srcLoc: *cgf.currSrcLoc), value: cgf.loadCXXThisAddress(), derived: derivedClass,
443 base: baseClass, baseIsVirtual);
444 cgf.emitCXXDestructorCall(dd: d, type: Dtor_Base, forVirtualBase: baseIsVirtual,
445 /*delegating=*/false, thisAddr: addr, thisTy);
446 }
447};
448
449/// If the delegating constructor's body throws after the delegated-to
450/// constructor completes, destroy the object (mirrors CGClass.cpp's
451/// CallDelegatingCtorDtor).
452struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup {
453 const CXXDestructorDecl *dtor;
454 Address addr;
455 CXXDtorType type;
456
457 CallDelegatingCtorDtor(const CXXDestructorDecl *dtor, Address addr,
458 CXXDtorType type)
459 : dtor(dtor), addr(addr), type(type) {}
460
461 void emit(CIRGenFunction &cgf, Flags flags) override {
462 QualType thisTy = dtor->getFunctionObjectParameterType();
463 cgf.emitCXXDestructorCall(dd: dtor, type, /*forVirtualBase=*/false,
464 /*delegating=*/true, thisAddr: addr, thisTy);
465 }
466};
467
468} // end anonymous namespace
469
470/// Gets the address of a direct base class within a complete object.
471/// This should only be used for (1) non-virtual bases or (2) virtual bases
472/// when the type is known to be complete (e.g. in complete destructors).
473///
474/// The object pointed to by 'thisAddr' is assumed to be non-null.
475Address CIRGenFunction::getAddressOfDirectBaseInCompleteClass(
476 mlir::Location loc, Address thisAddr, const CXXRecordDecl *derived,
477 const CXXRecordDecl *base, bool baseIsVirtual) {
478 // 'thisAddr' must be a pointer (in some address space) to Derived.
479 assert(thisAddr.getElementType() == convertType(derived));
480
481 // Compute the offset of the virtual base.
482 CharUnits offset;
483 const ASTRecordLayout &layout = getContext().getASTRecordLayout(D: derived);
484 if (baseIsVirtual)
485 offset = layout.getVBaseClassOffset(VBase: base);
486 else
487 offset = layout.getBaseClassOffset(Base: base);
488
489 return builder.createBaseClassAddr(loc, addr: thisAddr, destType: convertType(t: base),
490 offset: offset.getQuantity(),
491 /*assumeNotNull=*/true);
492}
493
494void CIRGenFunction::emitBaseInitializer(mlir::Location loc,
495 const CXXRecordDecl *classDecl,
496 CXXCtorInitializer *baseInit) {
497 assert(curFuncDecl && "loading 'this' without a func declaration?");
498 assert(isa<CXXMethodDecl>(curFuncDecl));
499
500 assert(baseInit->isBaseInitializer() && "Must have base initializer!");
501
502 Address thisPtr = loadCXXThisAddress();
503
504 const Type *baseType = baseInit->getBaseClass();
505 const auto *baseClassDecl = baseType->castAsCXXRecordDecl();
506
507 bool isBaseVirtual = baseInit->isBaseVirtual();
508
509 // If the initializer for the base (other than the constructor
510 // itself) accesses 'this' in any way, we need to initialize the
511 // vtables.
512 if (CodeGenUtils::baseInitializerUsesThis(Ctx&: getContext(), Init: baseInit->getInit()))
513 initializeVTablePointers(loc, rd: classDecl);
514
515 // We can pretend to be a complete class because it only matters for
516 // virtual bases, and we only do virtual bases for complete ctors.
517 Address v = getAddressOfDirectBaseInCompleteClass(
518 loc, thisAddr: thisPtr, derived: classDecl, base: baseClassDecl, baseIsVirtual: isBaseVirtual);
519 assert(!cir::MissingFeatures::aggValueSlotGC());
520 AggValueSlot aggSlot = AggValueSlot::forAddr(
521 addr: v, quals: Qualifiers(), isDestructed: AggValueSlot::IsDestructed, isAliased: AggValueSlot::IsNotAliased,
522 mayOverlap: getOverlapForBaseInit(rd: classDecl, baseRD: baseClassDecl, isVirtual: isBaseVirtual));
523
524 emitAggExpr(e: baseInit->getInit(), slot: aggSlot);
525
526 if (cgm.getLangOpts().Exceptions && !baseClassDecl->hasTrivialDestructor())
527 ehStack.pushCleanup<CallBaseDtor>(kind: EHCleanup, a: baseClassDecl,
528 /*baseIsVirtual=*/a: isBaseVirtual);
529}
530
531/// This routine generates necessary code to initialize base classes and
532/// non-static data members belonging to this constructor.
533void CIRGenFunction::emitCtorPrologue(const CXXConstructorDecl *cd,
534 CXXCtorType ctorType,
535 FunctionArgList &args) {
536 if (cd->isDelegatingConstructor()) {
537 emitDelegatingCXXConstructorCall(ctor: cd, args);
538 return;
539 }
540
541 const CXXRecordDecl *classDecl = cd->getParent();
542
543 // Virtual base initializers aren't needed if:
544 // - This is a base ctor variant
545 // - There are no vbases
546 // - The class is abstract, so a complete object of it cannot be constructed
547 //
548 // The check for an abstract class is necessary because sema may not have
549 // marked virtual base destructors referenced.
550 bool constructVBases = ctorType != Ctor_Base &&
551 classDecl->getNumVBases() != 0 &&
552 !classDecl->isAbstract();
553 if (constructVBases &&
554 !cgm.getTarget().getCXXABI().hasConstructorVariants()) {
555 cgm.errorNYI(cd->getSourceRange(),
556 "emitCtorPrologue: virtual base without variants");
557 return;
558 }
559
560 // Create three separate ranges for the different types of initializers.
561 auto allInits = cd->inits();
562
563 // Find the boundaries between the three groups.
564 auto virtualBaseEnd = std::find_if(
565 first: allInits.begin(), last: allInits.end(), pred: [](const CXXCtorInitializer *Init) {
566 return !(Init->isBaseInitializer() && Init->isBaseVirtual());
567 });
568
569 auto nonVirtualBaseEnd = std::find_if(first: virtualBaseEnd, last: allInits.end(),
570 pred: [](const CXXCtorInitializer *Init) {
571 return !Init->isBaseInitializer();
572 });
573
574 // Create the three ranges.
575 auto virtualBaseInits = llvm::make_range(x: allInits.begin(), y: virtualBaseEnd);
576 auto nonVirtualBaseInits =
577 llvm::make_range(x: virtualBaseEnd, y: nonVirtualBaseEnd);
578 auto memberInits = llvm::make_range(x: nonVirtualBaseEnd, y: allInits.end());
579
580 const mlir::Value oldThisValue = cxxThisValue;
581
582 auto emitInitializer = [&](CXXCtorInitializer *baseInit) {
583 if (cgm.getCodeGenOpts().StrictVTablePointers &&
584 cgm.getCodeGenOpts().OptimizationLevel > 0 &&
585 CodeGenUtils::isInitializerOfDynamicClass(BaseInit: baseInit)) {
586 // It's OK to continue after emitting the error here. The missing code
587 // just "launders" the 'this' pointer.
588 cgm.errorNYI(cd->getSourceRange(),
589 "emitCtorPrologue: strict vtable pointers for vbase");
590 }
591 emitBaseInitializer(loc: getLoc(srcLoc: cd->getBeginLoc()), classDecl, baseInit);
592 };
593
594 // Process virtual base initializers.
595 for (CXXCtorInitializer *virtualBaseInit : virtualBaseInits) {
596 if (!constructVBases)
597 continue;
598 emitInitializer(virtualBaseInit);
599 }
600
601 assert(!cir::MissingFeatures::msabi());
602
603 // Then, non-virtual base initializers.
604 for (CXXCtorInitializer *nonVirtualBaseInit : nonVirtualBaseInits) {
605 assert(!nonVirtualBaseInit->isBaseVirtual());
606 emitInitializer(nonVirtualBaseInit);
607 }
608
609 cxxThisValue = oldThisValue;
610
611 initializeVTablePointers(loc: getLoc(srcLoc: cd->getBeginLoc()), rd: classDecl);
612
613 // Finally, initialize class members.
614 FieldConstructionScope fcs(*this, loadCXXThisAddress());
615 // Classic codegen uses a special class to attempt to replace member
616 // initializers with memcpy. We could possibly defer that to the
617 // lowering or optimization phases to keep the memory accesses more
618 // explicit. For now, we don't insert memcpy at all.
619 assert(!cir::MissingFeatures::ctorMemcpyizer());
620 for (CXXCtorInitializer *member : memberInits) {
621 assert(!member->isBaseInitializer());
622 assert(member->isAnyMemberInitializer() &&
623 "Delegating initializer on non-delegating constructor");
624 emitMemberInitializer(cgf&: *this, classDecl: cd->getParent(), memberInit: member, constructor: cd, args);
625 }
626}
627
628static Address applyNonVirtualAndVirtualOffset(
629 mlir::Location loc, CIRGenFunction &cgf, Address addr,
630 CharUnits nonVirtualOffset, mlir::Value virtualOffset,
631 const CXXRecordDecl *derivedClass, const CXXRecordDecl *nearestVBase,
632 mlir::Type baseValueTy = {}, bool assumeNotNull = true) {
633 // Assert that we have something to do.
634 assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
635
636 // Compute the offset from the static and dynamic components.
637 mlir::Value baseOffset;
638 if (!nonVirtualOffset.isZero()) {
639 if (virtualOffset) {
640 // The virtual offset is always a ptrdiff_t: how it is stored in the
641 // vtable is up to the lowering of cir.vtable.get_offset.
642 baseOffset = cgf.getBuilder().getConstInt(loc, t: cgf.ptrDiffTy,
643 c: nonVirtualOffset.getQuantity());
644 baseOffset = cgf.getBuilder().createAdd(loc, lhs: virtualOffset, rhs: baseOffset);
645 } else {
646 assert(baseValueTy && "expected base type");
647 // If no virtualOffset is present this is the final stop.
648 return cgf.getBuilder().createBaseClassAddr(
649 loc, addr, destType: baseValueTy, offset: nonVirtualOffset.getQuantity(),
650 assumeNotNull);
651 }
652 } else {
653 baseOffset = virtualOffset;
654 }
655
656 // Apply the base offset. cir.ptr_stride adjusts by a number of elements,
657 // not bytes. So the pointer must be cast to a byte pointer and back.
658
659 mlir::Value ptr = addr.getPointer();
660 mlir::Type charPtrType =
661 cgf.getBuilder().getPointerTo(ty: cgf.uInt8Ty, as: addr.getAddressSpace());
662 mlir::Value charPtr = cgf.getBuilder().createBitcast(src: ptr, newTy: charPtrType);
663 mlir::Value adjusted = cir::PtrStrideOp::create(
664 builder&: cgf.getBuilder(), location: loc, result: charPtrType, base: charPtr, stride: baseOffset);
665 ptr = cgf.getBuilder().createBitcast(src: adjusted, newTy: ptr.getType());
666
667 // If we have a virtual component, the alignment of the result will
668 // be relative only to the known alignment of that vbase.
669 CharUnits alignment;
670 if (virtualOffset) {
671 assert(nearestVBase && "virtual offset without vbase?");
672 alignment = cgf.cgm.getVBaseAlignment(derivedAlign: addr.getAlignment(), derived: derivedClass,
673 vbase: nearestVBase);
674 } else {
675 alignment = addr.getAlignment();
676 }
677 alignment = alignment.alignmentAtOffset(offset: nonVirtualOffset);
678
679 return Address(ptr, alignment);
680}
681
682void CIRGenFunction::initializeVTablePointer(mlir::Location loc,
683 const VPtr &vptr) {
684 // Compute the address point.
685 mlir::Value vtableAddressPoint =
686 cgm.getCXXABI().getVTableAddressPointInStructor(
687 cgf&: *this, vtableClass: vptr.vtableClass, base: vptr.base, nearestVBase: vptr.nearestVBase);
688
689 if (!vtableAddressPoint)
690 return;
691
692 // Compute where to store the address point.
693 mlir::Value virtualOffset{};
694 CharUnits nonVirtualOffset = CharUnits::Zero();
695
696 mlir::Type baseValueTy;
697 if (cgm.getCXXABI().isVirtualOffsetNeededForVTableField(cgf&: *this, vptr)) {
698 // We need to use the virtual base offset offset because the virtual base
699 // might have a different offset in the most derived class.
700 virtualOffset = cgm.getCXXABI().getVirtualBaseClassOffset(
701 loc, cgf&: *this, thisAddr: loadCXXThisAddress(), classDecl: vptr.vtableClass, baseClassDecl: vptr.nearestVBase);
702 nonVirtualOffset = vptr.offsetFromNearestVBase;
703 } else {
704 // We can just use the base offset in the complete class.
705 nonVirtualOffset = vptr.base.getBaseOffset();
706 baseValueTy =
707 convertType(t: getContext().getCanonicalTagType(TD: vptr.base.getBase()));
708 }
709
710 // Apply the offsets.
711 Address classAddr = loadCXXThisAddress();
712 if (!nonVirtualOffset.isZero() || virtualOffset) {
713 classAddr = applyNonVirtualAndVirtualOffset(
714 loc, cgf&: *this, addr: classAddr, nonVirtualOffset, virtualOffset,
715 derivedClass: vptr.vtableClass, nearestVBase: vptr.nearestVBase, baseValueTy);
716 }
717
718 // Finally, store the address point. Use the same CIR types as the field.
719 //
720 // vtable field is derived from `this` pointer, therefore they should be in
721 // the same addr space.
722 auto vtablePtr =
723 cir::VTableGetVPtrOp::create(builder, location: loc, src: classAddr.getPointer());
724 Address vtableField = Address(vtablePtr, classAddr.getAlignment());
725 builder.createStore(loc, val: vtableAddressPoint, dst: vtableField);
726 assert(!cir::MissingFeatures::opTBAA());
727 assert(!cir::MissingFeatures::createInvariantGroup());
728}
729
730void CIRGenFunction::initializeVTablePointers(mlir::Location loc,
731 const CXXRecordDecl *rd) {
732 // Ignore classes without a vtable.
733 if (!rd->isDynamicClass())
734 return;
735
736 // Initialize the vtable pointers for this class and all of its bases.
737 if (cgm.getCXXABI().doStructorsInitializeVPtrs(vtableClass: rd))
738 for (const auto &vptr : getVTablePointers(vtableClass: rd))
739 initializeVTablePointer(loc, vptr);
740
741 if (rd->getNumVBases())
742 cgm.getCXXABI().initializeHiddenVirtualInheritanceMembers(cgf&: *this, rd);
743}
744
745CIRGenFunction::VPtrsVector
746CIRGenFunction::getVTablePointers(const CXXRecordDecl *vtableClass) {
747 CIRGenFunction::VPtrsVector vptrsResult;
748 VisitedVirtualBasesSetTy vbases;
749 getVTablePointers(base: BaseSubobject(vtableClass, CharUnits::Zero()),
750 /*NearestVBase=*/nearestVBase: nullptr,
751 /*OffsetFromNearestVBase=*/offsetFromNearestVBase: CharUnits::Zero(),
752 /*BaseIsNonVirtualPrimaryBase=*/baseIsNonVirtualPrimaryBase: false, vtableClass, vbases,
753 vptrs&: vptrsResult);
754 return vptrsResult;
755}
756
757void CIRGenFunction::getVTablePointers(BaseSubobject base,
758 const CXXRecordDecl *nearestVBase,
759 CharUnits offsetFromNearestVBase,
760 bool baseIsNonVirtualPrimaryBase,
761 const CXXRecordDecl *vtableClass,
762 VisitedVirtualBasesSetTy &vbases,
763 VPtrsVector &vptrs) {
764 // If this base is a non-virtual primary base the address point has already
765 // been set.
766 if (!baseIsNonVirtualPrimaryBase) {
767 // Initialize the vtable pointer for this base.
768 VPtr vptr = {.base: base, .nearestVBase: nearestVBase, .offsetFromNearestVBase: offsetFromNearestVBase, .vtableClass: vtableClass};
769 vptrs.push_back(Elt: vptr);
770 }
771
772 const CXXRecordDecl *rd = base.getBase();
773
774 for (const auto &nextBase : rd->bases()) {
775 const auto *baseDecl =
776 cast<CXXRecordDecl>(Val: nextBase.getType()->castAs<RecordType>()->getDecl())
777 ->getDefinitionOrSelf();
778
779 // Ignore classes without a vtable.
780 if (!baseDecl->isDynamicClass())
781 continue;
782
783 CharUnits baseOffset;
784 CharUnits baseOffsetFromNearestVBase;
785 bool baseDeclIsNonVirtualPrimaryBase;
786 const CXXRecordDecl *nextBaseDecl;
787
788 if (nextBase.isVirtual()) {
789 // Check if we've visited this virtual base before.
790 if (!vbases.insert(Ptr: baseDecl).second)
791 continue;
792
793 const ASTRecordLayout &layout =
794 getContext().getASTRecordLayout(D: vtableClass);
795
796 nextBaseDecl = baseDecl;
797 baseOffset = layout.getVBaseClassOffset(VBase: baseDecl);
798 baseOffsetFromNearestVBase = CharUnits::Zero();
799 baseDeclIsNonVirtualPrimaryBase = false;
800 } else {
801 const ASTRecordLayout &layout = getContext().getASTRecordLayout(D: rd);
802
803 nextBaseDecl = nearestVBase;
804 baseOffset = base.getBaseOffset() + layout.getBaseClassOffset(Base: baseDecl);
805 baseOffsetFromNearestVBase =
806 offsetFromNearestVBase + layout.getBaseClassOffset(Base: baseDecl);
807 baseDeclIsNonVirtualPrimaryBase = layout.getPrimaryBase() == baseDecl;
808 }
809
810 getVTablePointers(base: BaseSubobject(baseDecl, baseOffset), nearestVBase: nextBaseDecl,
811 offsetFromNearestVBase: baseOffsetFromNearestVBase,
812 baseIsNonVirtualPrimaryBase: baseDeclIsNonVirtualPrimaryBase, vtableClass, vbases,
813 vptrs);
814 }
815}
816
817Address CIRGenFunction::loadCXXThisAddress() {
818 assert(curFuncDecl && "loading 'this' without a func declaration?");
819 assert(isa<CXXMethodDecl>(curFuncDecl));
820
821 // Lazily compute CXXThisAlignment.
822 if (cxxThisAlignment.isZero()) {
823 // Just use the best known alignment for the parent.
824 // TODO: if we're currently emitting a complete-object ctor/dtor, we can
825 // always use the complete-object alignment.
826 auto rd = cast<CXXMethodDecl>(Val: curFuncDecl)->getParent();
827 cxxThisAlignment = cgm.getClassPointerAlignment(rd);
828 }
829
830 return Address(loadCXXThis(), cxxThisAlignment);
831}
832
833void CIRGenFunction::emitInitializerForField(FieldDecl *field, LValue lhs,
834 Expr *init) {
835 QualType fieldType = field->getType();
836 switch (getEvaluationKind(type: fieldType)) {
837 case cir::TEK_Scalar:
838 if (lhs.isSimple()) {
839 emitExprAsInit(init, d: field, lvalue: lhs, capturedByInit: false);
840 } else {
841 RValue rhs = RValue::get(v: emitScalarExpr(e: init));
842 emitStoreThroughLValue(src: rhs, dst: lhs);
843 }
844 break;
845 case cir::TEK_Complex:
846 emitComplexExprIntoLValue(e: init, dest: lhs, /*isInit=*/true);
847 break;
848 case cir::TEK_Aggregate: {
849 assert(!cir::MissingFeatures::aggValueSlotGC());
850 assert(!cir::MissingFeatures::sanitizers());
851 AggValueSlot slot = AggValueSlot::forLValue(
852 LV: lhs, isDestructed: AggValueSlot::IsDestructed, isAliased: AggValueSlot::IsNotAliased,
853 mayOverlap: getOverlapForFieldInit(fd: field), isZeroed: AggValueSlot::IsNotZeroed);
854 emitAggExpr(e: init, slot);
855 break;
856 }
857 }
858
859 // Ensure that we destroy this object if an exception is thrown later in the
860 // constructor.
861 QualType::DestructionKind dtorKind = fieldType.isDestructedType();
862 pushEHDestroyIfNeeded(dtorKind, addr: lhs.getAddress(), type: fieldType);
863}
864
865Address CIRGenFunction::emitCXXMemberDataPointerAddress(
866 const Expr *e, Address base, mlir::Value memberPtr,
867 const MemberPointerType *memberPtrType, LValueBaseInfo *baseInfo) {
868 assert(!cir::MissingFeatures::cxxABI());
869
870 cir::GetRuntimeMemberOp op = builder.createGetIndirectMember(
871 loc: getLoc(srcLoc: e->getSourceRange()), objectPtr: base.getPointer(), memberPtr);
872
873 QualType memberType = memberPtrType->getPointeeType();
874 assert(!cir::MissingFeatures::opTBAA());
875 CharUnits memberAlign = cgm.getNaturalTypeAlignment(t: memberType, baseInfo);
876 memberAlign = cgm.getDynamicOffsetAlignment(
877 actualBaseAlign: base.getAlignment(), baseDecl: memberPtrType->getMostRecentCXXRecordDecl(),
878 expectedTargetAlign: memberAlign);
879
880 return Address(op, convertTypeForMem(t: memberPtrType->getPointeeType()),
881 memberAlign);
882}
883
884CharUnits
885CIRGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign,
886 const CXXRecordDecl *baseDecl,
887 CharUnits expectedTargetAlign) {
888 // If the base is an incomplete type (which is, alas, possible with
889 // member pointers), be pessimistic.
890 if (!baseDecl->isCompleteDefinition())
891 return std::min(a: actualBaseAlign, b: expectedTargetAlign);
892
893 const ASTRecordLayout &baseLayout =
894 getASTContext().getASTRecordLayout(D: baseDecl);
895 CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment();
896
897 // If the class is properly aligned, assume the target offset is, too.
898 //
899 // This actually isn't necessarily the right thing to do --- if the
900 // class is a complete object, but it's only properly aligned for a
901 // base subobject, then the alignments of things relative to it are
902 // probably off as well. (Note that this requires the alignment of
903 // the target to be greater than the NV alignment of the derived
904 // class.)
905 //
906 // However, our approach to this kind of under-alignment can only
907 // ever be best effort; after all, we're never going to propagate
908 // alignments through variables or parameters. Note, in particular,
909 // that constructing a polymorphic type in an address that's less
910 // than pointer-aligned will generally trap in the constructor,
911 // unless we someday add some sort of attribute to change the
912 // assumed alignment of 'this'. So our goal here is pretty much
913 // just to allow the user to explicitly say that a pointer is
914 // under-aligned and then safely access its fields and vtables.
915 if (actualBaseAlign >= expectedBaseAlign)
916 return expectedTargetAlign;
917
918 // Otherwise, we might be offset by an arbitrary multiple of the
919 // actual alignment. The correct adjustment is to take the min of
920 // the two alignments.
921 return std::min(a: actualBaseAlign, b: expectedTargetAlign);
922}
923
924/// Return the best known alignment for a pointer to a virtual base,
925/// given the alignment of a pointer to the derived class.
926clang::CharUnits
927CIRGenModule::getVBaseAlignment(CharUnits actualDerivedAlign,
928 const CXXRecordDecl *derivedClass,
929 const CXXRecordDecl *vbaseClass) {
930 // The basic idea here is that an underaligned derived pointer might
931 // indicate an underaligned base pointer.
932
933 assert(vbaseClass->isCompleteDefinition());
934 const ASTRecordLayout &baseLayout =
935 getASTContext().getASTRecordLayout(D: vbaseClass);
936 CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment();
937
938 return getDynamicOffsetAlignment(actualBaseAlign: actualDerivedAlign, baseDecl: derivedClass,
939 expectedTargetAlign: expectedVBaseAlign);
940}
941
942/// Emit a loop to call a particular constructor for each of several members
943/// of an array.
944///
945/// \param ctor the constructor to call for each element
946/// \param arrayType the type of the array to initialize
947/// \param arrayBegin an arrayType*
948/// \param zeroInitialize true if each element should be
949/// zero-initialized before it is constructed
950void CIRGenFunction::emitCXXAggrConstructorCall(
951 const CXXConstructorDecl *ctor, const clang::ArrayType *arrayType,
952 Address arrayBegin, const CXXConstructExpr *e, bool newPointerIsChecked,
953 bool zeroInitialize) {
954 QualType elementType;
955 mlir::Value numElements = emitArrayLength(arrayType, baseType&: elementType, addr&: arrayBegin);
956 emitCXXAggrConstructorCall(ctor, numElements, arrayBase: arrayBegin, e,
957 newPointerIsChecked, zeroInitialize,
958 /*endOfInit=*/Address::invalid());
959}
960
961/// Emit a loop to call a particular constructor for each of several members
962/// of an array.
963///
964/// \param ctor the constructor to call for each element
965/// \param numElements the number of elements in the array;
966/// may be zero
967/// \param arrayBase a T*, where T is the type constructed by ctor
968/// \param zeroInitialize true if each element should be
969/// zero-initialized before it is constructed
970/// \param endOfInit if valid, an alloca holding the upper bound of an
971/// already-pushed irregular partial-array EH cleanup. When valid, the
972/// loop body will update this slot before each constructor call so the
973/// caller's cleanup covers loop-constructed elements, and no
974/// partial-destruction region is attached to the resulting
975/// cir::ArrayCtor op.
976void CIRGenFunction::emitCXXAggrConstructorCall(
977 const CXXConstructorDecl *ctor, mlir::Value numElements, Address arrayBase,
978 const CXXConstructExpr *e, bool newPointerIsChecked, bool zeroInitialize,
979 Address endOfInit) {
980 // It's legal for numElements to be zero. This can happen both
981 // dynamically, because x can be zero in 'new A[x]', and statically,
982 // because of GCC extensions that permit zero-length arrays. There
983 // are probably legitimate places where we could assume that this
984 // doesn't happen, but it's not clear that it's worth it.
985
986 // Peel any array types wrapped in the address element type down to the CIR
987 // type of a single constructed object.
988 mlir::Type elementType = arrayBase.getElementType();
989 while (auto maybeArrayTy = mlir::dyn_cast<cir::ArrayType>(Val&: elementType))
990 elementType = maybeArrayTy.getElementType();
991 cir::PointerType ptrToElmType =
992 builder.getPointerTo(ty: elementType, as: arrayBase.getAddressSpace());
993
994 bool useDynamicArrayCtor = true;
995 uint64_t constElementCount = 0;
996 if (auto constantOp = numElements.getDefiningOp<cir::ConstantOp>()) {
997 constElementCount = CIRGenFunction::getZExtIntValueFromConstOp(val: constantOp);
998 if (constElementCount == 0)
999 return;
1000 if (constantOp.use_empty())
1001 constantOp.erase();
1002 useDynamicArrayCtor = false;
1003 }
1004
1005 // Traditional LLVM codegen emits a loop here. CIR lowers to a loop as part of
1006 // LoweringPrepare.
1007
1008 // The alignment of the base, adjusted by the size of a single element,
1009 // provides a conservative estimate of the alignment of every element.
1010 // (This assumes we never start tracking offsetted alignments.)
1011 //
1012 // Note that these are complete objects and so we don't need to
1013 // use the non-virtual size or alignment.
1014 CanQualType type = getContext().getCanonicalTagType(TD: ctor->getParent());
1015 CharUnits eltAlignment = arrayBase.getAlignment().alignmentOfArrayElement(
1016 elementSize: getContext().getTypeSizeInChars(T: type));
1017
1018 mlir::Location loc = getLoc(srcLoc: *currSrcLoc);
1019
1020 mlir::Value dynamicElPtr;
1021 if (useDynamicArrayCtor)
1022 dynamicElPtr =
1023 builder.createPtrBitcast(src: arrayBase.getPointer(), newPointeeTy: elementType);
1024
1025 // When the caller has already pushed an irregular partial-array cleanup
1026 // (signalled by a valid endOfInit), our loop body will keep that cleanup's
1027 // upper bound up to date, so we don't need a separate per-element
1028 // partial-destruction region on the cir::ArrayCtor op.
1029 bool needsPartialArrayCleanup = getLangOpts().Exceptions &&
1030 !ctor->getParent()->hasTrivialDestructor() &&
1031 !endOfInit.isValid();
1032
1033 auto emitCtorBody = [&](mlir::OpBuilder &b, mlir::Location l) {
1034 mlir::BlockArgument arg =
1035 b.getInsertionBlock()->addArgument(type: ptrToElmType, loc: l);
1036 Address curAddr = Address(arg, elementType, eltAlignment);
1037 // Extend the caller's irregular partial-array cleanup to cover the
1038 // element we're about to construct. If this constructor throws, the
1039 // cleanup will destroy every element strictly below this one.
1040 if (endOfInit.isValid())
1041 builder.createStore(loc: l, val: arg, dst: endOfInit);
1042 assert(!cir::MissingFeatures::sanitizers());
1043 if (zeroInitialize)
1044 emitNullInitialization(loc: l, destPtr: curAddr, ty: type);
1045 auto currAVS = AggValueSlot::forAddr(
1046 addr: curAddr, quals: type.getQualifiers(), isDestructed: AggValueSlot::IsDestructed,
1047 isAliased: AggValueSlot::IsNotAliased, mayOverlap: AggValueSlot::DoesNotOverlap,
1048 isZeroed: AggValueSlot::IsNotZeroed);
1049 // C++ [class.temporary]p4:
1050 // There are two contexts in which temporaries are destroyed at a
1051 // different point than the end of the full-expression. The first context
1052 // is when a default constructor is called to initialize an element of an
1053 // array. If the constructor has one or more default arguments, the
1054 // destruction of every temporary created in a default argument expression
1055 // is sequenced before the construction of the next array element, if any.
1056 {
1057 FullExprCleanupScope scope(*this, e);
1058 scope.deferLifetimeExtendedCleanups();
1059 emitCXXConstructorCall(d: ctor, type: Ctor_Complete,
1060 /*ForVirtualBase=*/forVirtualBase: false,
1061 /*Delegating=*/delegating: false, thisAVS: currAVS, e);
1062 }
1063 cir::YieldOp::create(builder&: b, location: l);
1064 };
1065
1066 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>
1067 emitPartialDtorBody = nullptr;
1068 auto partialDtorBuilder = [&](mlir::OpBuilder &b, mlir::Location l) {
1069 mlir::BlockArgument arg =
1070 b.getInsertionBlock()->addArgument(type: ptrToElmType, loc: l);
1071 Address curAddr = Address(arg, elementType, eltAlignment);
1072 emitCXXDestructorCall(dd: ctor->getParent()->getDestructor(), type: Dtor_Complete,
1073 /*forVirtualBase=*/false,
1074 /*delegating=*/false, thisAddr: curAddr, thisTy: type);
1075 cir::YieldOp::create(builder&: b, location: l);
1076 };
1077 if (needsPartialArrayCleanup)
1078 emitPartialDtorBody = partialDtorBuilder;
1079
1080 if (useDynamicArrayCtor) {
1081 cir::ArrayCtor::create(builder, location: loc, addr: dynamicElPtr, num_elements: numElements,
1082 bodyBuilder: emitCtorBody, partialDtorBuilder: emitPartialDtorBody);
1083 } else {
1084 cir::ArrayType arrayTy =
1085 cir::ArrayType::get(elementType, size: constElementCount);
1086 mlir::Value arrayOp =
1087 builder.createPtrBitcast(src: arrayBase.getPointer(), newPointeeTy: arrayTy);
1088 cir::ArrayCtor::create(builder, location: loc, addr: arrayOp, bodyBuilder: emitCtorBody,
1089 partialDtorBuilder: emitPartialDtorBody);
1090 }
1091}
1092
1093void CIRGenFunction::emitDelegateCXXConstructorCall(
1094 const CXXConstructorDecl *ctor, CXXCtorType ctorType,
1095 const FunctionArgList &args, SourceLocation loc) {
1096 CallArgList delegateArgs;
1097
1098 FunctionArgList::const_iterator i = args.begin(), e = args.end();
1099 assert(i != e && "no parameters to constructor");
1100
1101 // this
1102 Address thisAddr = loadCXXThisAddress();
1103 delegateArgs.add(rvalue: RValue::get(v: thisAddr.getPointer()), type: (*i)->getType());
1104 ++i;
1105
1106 // FIXME: The location of the VTT parameter in the parameter list is specific
1107 // to the Itanium ABI and shouldn't be hardcoded here.
1108 if (cgm.getCXXABI().needsVTTParameter(gd: curGD)) {
1109 cgm.errorNYI(loc, "emitDelegateCXXConstructorCall: VTT parameter");
1110 return;
1111 }
1112
1113 // Explicit arguments.
1114 for (; i != e; ++i) {
1115 const VarDecl *param = *i;
1116 // FIXME: per-argument source location
1117 emitDelegateCallArg(args&: delegateArgs, param, loc);
1118 }
1119
1120 assert(!cir::MissingFeatures::sanitizers());
1121
1122 emitCXXConstructorCall(d: ctor, type: ctorType, /*ForVirtualBase=*/forVirtualBase: false,
1123 /*Delegating=*/delegating: true, thisAddr, args&: delegateArgs, loc);
1124}
1125
1126void CIRGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &args) {
1127 const auto *assignOp = cast<CXXMethodDecl>(Val: curGD.getDecl());
1128 assert(assignOp->isCopyAssignmentOperator() ||
1129 assignOp->isMoveAssignmentOperator());
1130 const Stmt *rootS = assignOp->getBody();
1131 assert(isa<CompoundStmt>(rootS) &&
1132 "Body of an implicit assignment operator should be compound stmt.");
1133 const auto *rootCS = cast<CompoundStmt>(Val: rootS);
1134
1135 cgm.setFuncInfoAttr(funcOp: cast<cir::FuncOp>(Val: curFn), funcDecl: assignOp);
1136
1137 assert(!cir::MissingFeatures::incrementProfileCounter());
1138 assert(!cir::MissingFeatures::runCleanupsScope());
1139
1140 AssignmentMemcpyizer AM(*this, assignOp, args);
1141 for (auto *i : rootCS->body())
1142 AM.emitAssignment(s: i);
1143 AM.finish();
1144}
1145
1146void CIRGenFunction::emitForwardingCallToLambda(
1147 const CXXMethodDecl *callOperator, CallArgList &callArgs) {
1148 // Get the address of the call operator.
1149 const CIRGenFunctionInfo &calleeFnInfo =
1150 cgm.getTypes().arrangeCXXMethodDeclaration(md: callOperator);
1151 cir::FuncOp calleePtr = cgm.getAddrOfFunction(
1152 gd: GlobalDecl(callOperator), funcType: cgm.getTypes().getFunctionType(info: calleeFnInfo));
1153
1154 // Prepare the return slot.
1155 const FunctionProtoType *fpt =
1156 callOperator->getType()->castAs<FunctionProtoType>();
1157 QualType resultType = fpt->getReturnType();
1158 ReturnValueSlot returnSlot;
1159 // This should also be tracking volatile, unused, and externally destructed.
1160 assert(!cir::MissingFeatures::returnValueSlotFeatures());
1161 // For aggregate returns, write the callee's result directly into the
1162 // static invoker's return slot. Otherwise emitReturnOfRValue below would
1163 // aggregate-copy a temporary into returnValue, which is incorrect for
1164 // types without a trivial copy/move (e.g. std::string) -- and trips an
1165 // assertion in emitAggregateCopy.
1166 if (!resultType->isVoidType() && hasAggregateEvaluationKind(type: resultType))
1167 returnSlot = ReturnValueSlot(returnValue);
1168
1169 // We don't need to separately arrange the call arguments because
1170 // the call can't be variadic anyway --- it's impossible to forward
1171 // variadic arguments.
1172
1173 // Now emit our call.
1174 CIRGenCallee callee =
1175 CIRGenCallee::forDirect(funcPtr: calleePtr, abstractInfo: GlobalDecl(callOperator));
1176 RValue rv = emitCall(funcInfo: calleeFnInfo, callee, returnValue: returnSlot, args: callArgs,
1177 /*isMustTail=*/false);
1178
1179 // Forward the returned value through the function's return slot.
1180 if (!resultType->isVoidType()) {
1181 if (returnSlot.isNull() && getLangOpts().ObjCAutoRefCount &&
1182 resultType->isObjCRetainableType())
1183 cgm.errorNYI(callOperator->getSourceRange(),
1184 "emitForwardingCallToLambda: ObjCAutoRefCount");
1185 emitReturnOfRValue(loc: getLoc(srcLoc: *currSrcLoc), rv, ty: resultType);
1186 } else {
1187 cir::ReturnOp::create(builder, location: getLoc(srcLoc: *currSrcLoc));
1188 }
1189}
1190
1191void CIRGenFunction::emitLambdaDelegatingInvokeBody(const CXXMethodDecl *md) {
1192 const CXXRecordDecl *lambda = md->getParent();
1193
1194 // Start building arguments for forwarding call
1195 CallArgList callArgs;
1196
1197 QualType lambdaType = getContext().getCanonicalTagType(TD: lambda);
1198 QualType thisType = getContext().getPointerType(T: lambdaType);
1199 Address thisPtr =
1200 createMemTemp(t: lambdaType, loc: getLoc(srcLoc: md->getSourceRange()), name: "unused.capture");
1201 callArgs.add(rvalue: RValue::get(v: thisPtr.getPointer()), type: thisType);
1202
1203 // Add the rest of the parameters.
1204 for (auto *param : md->parameters())
1205 emitDelegateCallArg(args&: callArgs, param, loc: param->getBeginLoc());
1206
1207 const CXXMethodDecl *callOp = lambda->getLambdaCallOperator();
1208 // For a generic lambda, find the corresponding call operator specialization
1209 // to which the call to the static-invoker shall be forwarded.
1210 if (lambda->isGenericLambda()) {
1211 assert(md->isFunctionTemplateSpecialization());
1212 const TemplateArgumentList *tal = md->getTemplateSpecializationArgs();
1213 FunctionTemplateDecl *callOpTemplate =
1214 callOp->getDescribedFunctionTemplate();
1215 llvm::FoldingSetInsertToken InsertToken;
1216 FunctionDecl *correspondingCallOpSpecialization =
1217 callOpTemplate->findSpecialization(Args: tal->asArray(), InsertToken);
1218 assert(correspondingCallOpSpecialization);
1219 callOp = cast<CXXMethodDecl>(Val: correspondingCallOpSpecialization);
1220 }
1221 emitForwardingCallToLambda(callOperator: callOp, callArgs);
1222}
1223
1224void CIRGenFunction::emitLambdaStaticInvokeBody(const CXXMethodDecl *md) {
1225 if (md->isVariadic()) {
1226 // Codgen for LLVM doesn't emit code for this as well, it says:
1227 // FIXME: Making this work correctly is nasty because it requires either
1228 // cloning the body of the call operator or making the call operator
1229 // forward.
1230 cgm.errorNYI(md->getSourceRange(), "emitLambdaStaticInvokeBody: variadic");
1231 }
1232
1233 emitLambdaDelegatingInvokeBody(md);
1234}
1235
1236void CIRGenFunction::destroyCXXObject(CIRGenFunction &cgf, Address addr,
1237 QualType type) {
1238 const auto *record = type->castAsCXXRecordDecl();
1239 const CXXDestructorDecl *dtor = record->getDestructor();
1240 // TODO(cir): Unlike traditional codegen, CIRGen should actually emit trivial
1241 // dtors which shall be removed on later CIR passes. However, only remove this
1242 // assertion after we have a test case to exercise this path.
1243 assert(!dtor->isTrivial());
1244 cgf.emitCXXDestructorCall(dd: dtor, type: Dtor_Complete, /*forVirtualBase*/ false,
1245 /*delegating=*/false, thisAddr: addr, thisTy: type);
1246}
1247
1248mlir::Value CIRGenFunction::loadThisForDtorDelete(const CXXDestructorDecl *dd) {
1249 if (Expr *thisArg = dd->getOperatorDeleteThisArg())
1250 return emitScalarExpr(e: thisArg);
1251 return loadCXXThis();
1252}
1253
1254namespace {
1255/// Call the operator delete associated with the current destructor.
1256struct CallDtorDelete final : EHScopeStack::Cleanup {
1257 CallDtorDelete() {}
1258
1259 void emit(CIRGenFunction &cgf, Flags flags) override {
1260 const CXXDestructorDecl *dtor = cast<CXXDestructorDecl>(Val: cgf.curFuncDecl);
1261 const CXXRecordDecl *classDecl = dtor->getParent();
1262 cgf.emitDeleteCall(deleteFD: dtor->getOperatorDelete(),
1263 ptr: cgf.loadThisForDtorDelete(dd: dtor),
1264 deleteTy: cgf.getContext().getCanonicalTagType(TD: classDecl));
1265 }
1266};
1267
1268class DestroyField final : public EHScopeStack::Cleanup {
1269 const FieldDecl *field;
1270 CIRGenFunction::Destroyer *destroyer;
1271
1272public:
1273 DestroyField(const FieldDecl *field, CIRGenFunction::Destroyer *destroyer)
1274 : field(field), destroyer(destroyer) {}
1275
1276 void emit(CIRGenFunction &cgf, Flags flags) override {
1277 // Find the address of the field.
1278 Address thisValue = cgf.loadCXXThisAddress();
1279 CanQualType recordTy =
1280 cgf.getContext().getCanonicalTagType(TD: field->getParent());
1281 LValue thisLV = cgf.makeAddrLValue(addr: thisValue, ty: recordTy);
1282 LValue lv = cgf.emitLValueForField(base: thisLV, field);
1283 assert(lv.isSimple());
1284
1285 assert(!cir::MissingFeatures::useEHCleanupForArray());
1286 cgf.emitDestroy(addr: lv.getAddress(), type: field->getType(), destroyer);
1287 }
1288};
1289} // namespace
1290
1291/// Emit all code that comes at the end of class's destructor. This is to call
1292/// destructors on members and base classes in reverse order of their
1293/// construction.
1294///
1295/// For a deleting destructor, this pushes the cleanup that calls operator
1296/// delete. The caller handles a destroying operator delete.
1297void CIRGenFunction::enterDtorCleanups(const CXXDestructorDecl *dd,
1298 CXXDtorType dtorType) {
1299 assert((!dd->isTrivial() || dd->hasAttr<DLLExportAttr>()) &&
1300 "Should not emit dtor epilogue for non-exported trivial dtor!");
1301
1302 // The deleting-destructor phase just needs to call the appropriate
1303 // operator delete that Sema picked up.
1304 if (dtorType == Dtor_Deleting) {
1305 assert(dd->getOperatorDelete() &&
1306 "operator delete missing - EnterDtorCleanups");
1307 if (cxxStructorImplicitParamValue) {
1308 // The implicit parameter of a deleting destructor is the Microsoft ABI
1309 // flag word that selects whether and which operator delete is called.
1310 assert(getTarget().getCXXABI().isMicrosoft() &&
1311 "only the Microsoft ABI passes an implicit deleting dtor param");
1312 cgm.errorNYI(dd->getSourceRange(),
1313 "deleting destructor with conditional delete: MSVC ABI");
1314
1315 } else {
1316 assert(!dd->getOperatorDelete()->isDestroyingOperatorDelete() &&
1317 "destroying operator delete is handled by emitDestructorBody");
1318 ehStack.pushCleanup<CallDtorDelete>(kind: NormalAndEHCleanup);
1319 }
1320 return;
1321 }
1322
1323 const CXXRecordDecl *classDecl = dd->getParent();
1324
1325 // Unions have no bases and do not call field destructors.
1326 if (classDecl->isUnion())
1327 return;
1328
1329 // The complete-destructor phase just destructs all the virtual bases.
1330 if (dtorType == Dtor_Complete) {
1331 assert(!cir::MissingFeatures::sanitizers());
1332
1333 // We push them in the forward order so that they'll be popped in
1334 // the reverse order.
1335 for (const CXXBaseSpecifier &base : classDecl->vbases()) {
1336 auto *baseClassDecl = base.getType()->castAsCXXRecordDecl();
1337
1338 if (baseClassDecl->hasTrivialDestructor()) {
1339 // Under SanitizeMemoryUseAfterDtor, poison the trivial base class
1340 // memory. For non-trival base classes the same is done in the class
1341 // destructor.
1342 assert(!cir::MissingFeatures::sanitizers());
1343 } else {
1344 ehStack.pushCleanup<CallBaseDtor>(kind: NormalAndEHCleanup, a: baseClassDecl,
1345 /*baseIsVirtual=*/a: true);
1346 }
1347 }
1348
1349 return;
1350 }
1351
1352 assert(dtorType == Dtor_Base);
1353 assert(!cir::MissingFeatures::sanitizers());
1354
1355 // Destroy non-virtual bases.
1356 for (const CXXBaseSpecifier &base : classDecl->bases()) {
1357 // Ignore virtual bases.
1358 if (base.isVirtual())
1359 continue;
1360
1361 CXXRecordDecl *baseClassDecl = base.getType()->getAsCXXRecordDecl();
1362
1363 if (baseClassDecl->hasTrivialDestructor())
1364 assert(!cir::MissingFeatures::sanitizers());
1365 else
1366 ehStack.pushCleanup<CallBaseDtor>(kind: NormalAndEHCleanup, a: baseClassDecl,
1367 /*baseIsVirtual=*/a: false);
1368 }
1369
1370 assert(!cir::MissingFeatures::sanitizers());
1371
1372 // Destroy direct fields.
1373 for (const FieldDecl *field : classDecl->fields()) {
1374 QualType type = field->getType();
1375 QualType::DestructionKind dtorKind = type.isDestructedType();
1376 if (!dtorKind)
1377 continue;
1378
1379 // Anonymous union members do not have their destructors called.
1380 const RecordType *rt = type->getAsUnionType();
1381 if (rt && rt->getDecl()->isAnonymousStructOrUnion())
1382 continue;
1383
1384 CleanupKind cleanupKind = getCleanupKind(kind: dtorKind);
1385 assert(!cir::MissingFeatures::useEHCleanupForArray());
1386 ehStack.pushCleanup<DestroyField>(kind: cleanupKind, a: field,
1387 a: getDestroyer(kind: dtorKind));
1388 }
1389}
1390
1391void CIRGenFunction::emitDelegatingCXXConstructorCall(
1392 const CXXConstructorDecl *ctor, const FunctionArgList &args) {
1393 assert(ctor->isDelegatingConstructor());
1394
1395 Address thisPtr = loadCXXThisAddress();
1396
1397 assert(!cir::MissingFeatures::objCGC());
1398 assert(!cir::MissingFeatures::sanitizers());
1399 AggValueSlot aggSlot = AggValueSlot::forAddr(
1400 addr: thisPtr, quals: Qualifiers(), isDestructed: AggValueSlot::IsDestructed,
1401 isAliased: AggValueSlot::IsNotAliased, mayOverlap: AggValueSlot::MayOverlap,
1402 isZeroed: AggValueSlot::IsNotZeroed);
1403
1404 emitAggExpr(e: ctor->init_begin()[0]->getInit(), slot: aggSlot);
1405
1406 const CXXRecordDecl *classDecl = ctor->getParent();
1407 if (cgm.getLangOpts().Exceptions && !classDecl->hasTrivialDestructor()) {
1408 CXXDtorType dtorType =
1409 curGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
1410 ehStack.pushCleanup<CallDelegatingCtorDtor>(
1411 kind: EHCleanup, a: classDecl->getDestructor(), a: thisPtr, a: dtorType);
1412 }
1413}
1414
1415void CIRGenFunction::emitCXXDestructorCall(const CXXDestructorDecl *dd,
1416 CXXDtorType type,
1417 bool forVirtualBase, bool delegating,
1418 Address thisAddr, QualType thisTy) {
1419 cgm.getCXXABI().emitDestructorCall(cgf&: *this, dd, type, forVirtualBase,
1420 delegating, thisAddr, thisTy);
1421}
1422
1423mlir::Value CIRGenFunction::getVTTParameter(GlobalDecl gd, bool forVirtualBase,
1424 bool delegating) {
1425 if (!cgm.getCXXABI().needsVTTParameter(gd))
1426 return nullptr;
1427
1428 const CXXRecordDecl *rd = cast<CXXMethodDecl>(Val: curCodeDecl)->getParent();
1429 const CXXRecordDecl *base = cast<CXXMethodDecl>(Val: gd.getDecl())->getParent();
1430
1431 uint64_t subVTTIndex;
1432
1433 if (delegating) {
1434 // If this is a delegating constructor call, just load the VTT.
1435 return loadCXXVTT();
1436 } else if (rd == base) {
1437 // If the record matches the base, this is the complete ctor/dtor
1438 // variant calling the base variant in a class with virtual bases.
1439 assert(!cgm.getCXXABI().needsVTTParameter(curGD) &&
1440 "doing no-op VTT offset in base dtor/ctor?");
1441 assert(!forVirtualBase && "Can't have same class as virtual base!");
1442 subVTTIndex = 0;
1443 } else {
1444 const ASTRecordLayout &layout = getContext().getASTRecordLayout(D: rd);
1445 CharUnits baseOffset = forVirtualBase ? layout.getVBaseClassOffset(VBase: base)
1446 : layout.getBaseClassOffset(Base: base);
1447
1448 subVTTIndex =
1449 cgm.getVTables().getSubVTTIndex(rd, base: BaseSubobject(base, baseOffset));
1450 assert(subVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
1451 }
1452
1453 mlir::Location loc = cgm.getLoc(cLoc: rd->getBeginLoc());
1454 if (cgm.getCXXABI().needsVTTParameter(gd: curGD)) {
1455 // A VTT parameter was passed to the constructor, use it.
1456 mlir::Value vtt = loadCXXVTT();
1457 return builder.createVTTAddrPoint(loc, retTy: vtt.getType(), addr: vtt, offset: subVTTIndex);
1458 } else {
1459 // We're the complete constructor, so get the VTT by name.
1460 cir::GlobalOp vtt = cgm.getVTables().getAddrOfVTT(rd);
1461 mlir::Type voidPtrTy = convertType(t: getContext().VoidPtrTy);
1462 cir::PointerType vttTy =
1463 builder.getPointerTo(ty: voidPtrTy, as: vtt.getAddrSpaceAttr());
1464 auto vttSym = mlir::FlatSymbolRefAttr::get(value: vtt.getSymNameAttr());
1465 return builder.createVTTAddrPoint(loc, retTy: vttTy, sym: vttSym, offset: subVTTIndex);
1466 }
1467}
1468
1469Address CIRGenFunction::getAddressOfDerivedClass(
1470 mlir::Location loc, Address baseAddr, const CXXRecordDecl *derived,
1471 llvm::iterator_range<CastExpr::path_const_iterator> path,
1472 bool nullCheckValue) {
1473 assert(!path.empty() && "Base path should not be empty!");
1474
1475 QualType derivedTy = getContext().getCanonicalTagType(TD: derived);
1476 mlir::Type derivedValueTy = convertType(t: derivedTy);
1477 CharUnits nonVirtualOffset =
1478 cgm.computeNonVirtualBaseClassOffset(derivedClass: derived, path);
1479
1480 // Note that in OG, no offset (nonVirtualOffset.getQuantity() == 0) means it
1481 // just gives the address back. In CIR a `cir.derived_class` is created and
1482 // made into a nop later on during lowering.
1483 return builder.createDerivedClassAddr(loc, addr: baseAddr, destType: derivedValueTy,
1484 offset: nonVirtualOffset.getQuantity(),
1485 /*assumeNotNull=*/!nullCheckValue);
1486}
1487
1488Address CIRGenFunction::getAddressOfBaseClass(
1489 Address value, const CXXRecordDecl *derived,
1490 llvm::iterator_range<CastExpr::path_const_iterator> path,
1491 bool nullCheckValue, SourceLocation loc) {
1492 assert(!path.empty() && "Base path should not be empty!");
1493
1494 CastExpr::path_const_iterator start = path.begin();
1495 const CXXRecordDecl *vBase = nullptr;
1496
1497 if ((*path.begin())->isVirtual()) {
1498 vBase = (*start)->getType()->castAsCXXRecordDecl();
1499 ++start;
1500 }
1501
1502 // Compute the static offset of the ultimate destination within its
1503 // allocating subobject (the virtual base, if there is one, or else
1504 // the "complete" object that we see).
1505 CharUnits nonVirtualOffset = cgm.computeNonVirtualBaseClassOffset(
1506 derivedClass: vBase ? vBase : derived, path: {start, path.end()});
1507
1508 // If there's a virtual step, we can sometimes "devirtualize" it.
1509 // For now, that's limited to when the derived type is final.
1510 // TODO: "devirtualize" this for accesses to known-complete objects.
1511 if (vBase && derived->hasAttr<FinalAttr>()) {
1512 const ASTRecordLayout &layout = getContext().getASTRecordLayout(D: derived);
1513 CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase: vBase);
1514 nonVirtualOffset += vBaseOffset;
1515 vBase = nullptr; // we no longer have a virtual step
1516 }
1517
1518 // Get the base pointer type.
1519 mlir::Type baseValueTy = convertType(t: (path.end()[-1])->getType());
1520
1521 // If there is no virtual base, use cir.base_class_addr. It takes care of
1522 // the adjustment and the null pointer check.
1523 if (nonVirtualOffset.isZero() && !vBase) {
1524 assert(!cir::MissingFeatures::sanitizers());
1525 return builder.createBaseClassAddr(loc: getLoc(srcLoc: loc), addr: value, destType: baseValueTy, offset: 0,
1526 /*assumeNotNull=*/true);
1527 }
1528
1529 assert(!cir::MissingFeatures::sanitizers());
1530
1531 mlir::Location mlirLoc = getLoc(srcLoc: loc);
1532
1533 // Computing the virtual offset requires reading the vtable, which is only
1534 // safe to do once we know the pointer isn't null. Guard the whole
1535 // computation, mirroring classic CodeGen's cast.notnull/cast.end split.
1536 if (vBase && nullCheckValue) {
1537 CharUnits alignment =
1538 cgm.getVBaseAlignment(actualDerivedAlign: value.getAlignment(), derivedClass: derived, vbaseClass: vBase)
1539 .alignmentAtOffset(offset: nonVirtualOffset);
1540 mlir::Type basePtrTy =
1541 builder.getPointerTo(ty: baseValueTy, as: value.getAddressSpace());
1542 mlir::Value ptrIsNull = builder.createPtrIsNull(ptr: value.getPointer());
1543 mlir::Value result =
1544 cir::TernaryOp::create(
1545 builder, location: mlirLoc, cond: ptrIsNull,
1546 trueBuilder: [&](mlir::OpBuilder &, mlir::Location) {
1547 builder.createYield(
1548 loc: mlirLoc, value: builder.getNullPtr(ty: basePtrTy, loc: mlirLoc).getResult());
1549 },
1550 falseBuilder: [&](mlir::OpBuilder &, mlir::Location) {
1551 mlir::Value virtualOffset =
1552 cgm.getCXXABI().getVirtualBaseClassOffset(
1553 loc: mlirLoc, cgf&: *this, thisAddr: value, classDecl: derived, baseClassDecl: vBase);
1554 Address adjusted = applyNonVirtualAndVirtualOffset(
1555 loc: mlirLoc, cgf&: *this, addr: value, nonVirtualOffset, virtualOffset,
1556 derivedClass: derived, nearestVBase: vBase, baseValueTy, /*assumeNotNull=*/true);
1557 adjusted = adjusted.withElementType(builder, ElemTy: baseValueTy);
1558 builder.createYield(loc: mlirLoc, value: adjusted.getPointer());
1559 })
1560 .getResult();
1561 return Address(result, baseValueTy, alignment);
1562 }
1563
1564 // Compute the virtual offset.
1565 mlir::Value virtualOffset = nullptr;
1566 if (vBase) {
1567 virtualOffset = cgm.getCXXABI().getVirtualBaseClassOffset(
1568 loc: mlirLoc, cgf&: *this, thisAddr: value, classDecl: derived, baseClassDecl: vBase);
1569 }
1570
1571 // Apply both offsets.
1572 value = applyNonVirtualAndVirtualOffset(
1573 loc: mlirLoc, cgf&: *this, addr: value, nonVirtualOffset, virtualOffset, derivedClass: derived, nearestVBase: vBase,
1574 baseValueTy, assumeNotNull: not nullCheckValue);
1575
1576 // Cast to the destination type.
1577 value = value.withElementType(builder, ElemTy: baseValueTy);
1578
1579 return value;
1580}
1581
1582// TODO(cir): this can be shared with LLVM codegen.
1583bool CIRGenFunction::shouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *rd) {
1584 assert(!cir::MissingFeatures::hiddenVisibility());
1585 if (!cgm.getCodeGenOpts().WholeProgramVTables)
1586 return false;
1587
1588 if (cgm.getCodeGenOpts().VirtualFunctionElimination)
1589 return true;
1590
1591 assert(!cir::MissingFeatures::sanitizers());
1592
1593 return false;
1594}
1595
1596mlir::Value CIRGenFunction::getVTablePtr(mlir::Location loc, Address thisAddr,
1597 const CXXRecordDecl *rd) {
1598 auto vtablePtr =
1599 cir::VTableGetVPtrOp::create(builder, location: loc, src: thisAddr.getPointer());
1600 Address vtablePtrAddr = Address(vtablePtr, thisAddr.getAlignment());
1601
1602 auto vtable = builder.createLoad(loc, addr: vtablePtrAddr);
1603 assert(!cir::MissingFeatures::opTBAA());
1604
1605 if (cgm.getCodeGenOpts().OptimizationLevel > 0 &&
1606 cgm.getCodeGenOpts().StrictVTablePointers) {
1607 assert(!cir::MissingFeatures::createInvariantGroup());
1608 }
1609
1610 return vtable;
1611}
1612
1613void CIRGenFunction::emitCXXConstructorCall(const clang::CXXConstructorDecl *d,
1614 clang::CXXCtorType type,
1615 bool forVirtualBase,
1616 bool delegating,
1617 AggValueSlot thisAVS,
1618 const clang::CXXConstructExpr *e) {
1619 Address thisAddr = thisAVS.getAddress();
1620 QualType thisType = d->getThisType();
1621 mlir::Value thisPtr = thisAddr.getPointer();
1622
1623 assert(!cir::MissingFeatures::addressSpace());
1624
1625 // If this is a trivial constructor, just emit what's needed. If this is a
1626 // union copy constructor, we must emit a memcpy, because the AST does not
1627 // model that copy.
1628 if (d->isMemcpyEquivalentSpecialMember(Ctx: getContext())) {
1629 assert(e->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
1630 const Expr *arg = e->getArg(Arg: 0);
1631 LValue src = emitLValue(e: arg);
1632 CanQualType destTy = getContext().getCanonicalTagType(TD: d->getParent());
1633 LValue dest = makeAddrLValue(addr: thisAddr, ty: destTy);
1634 emitAggregateCopy(dest, src, eltTy: src.getType(), mayOverlap: thisAVS.mayOverlap());
1635 return;
1636 }
1637
1638 CallArgList args;
1639 args.add(rvalue: RValue::get(v: thisPtr), type: thisType);
1640
1641 const FunctionProtoType *fpt = d->getType()->castAs<FunctionProtoType>();
1642
1643 EvaluationOrder order = e->isListInitialization()
1644 ? EvaluationOrder::ForceLeftToRight
1645 : EvaluationOrder::Default;
1646
1647 if (auto inherited = d->getInheritedConstructor();
1648 !inherited || cgm.getTypes().inheritingCtorHasParams(inherited, type))
1649 emitCallArgs(args, prototype: fpt, argRange: e->arguments(), callee: e->getConstructor(),
1650 /*ParamsToSkip=*/paramsToSkip: 0, order);
1651
1652 assert(!cir::MissingFeatures::sanitizers());
1653 emitCXXConstructorCall(d, type, forVirtualBase, delegating, thisAddr, args,
1654 loc: e->getExprLoc());
1655}
1656
1657static bool canEmitDelegateCallArgs(CIRGenModule &cgm, ASTContext &ctx,
1658 const CXXConstructorDecl *d,
1659 CXXCtorType type) {
1660 // We can't forward a variadic call.
1661 if (d->isVariadic())
1662 return false;
1663
1664 if (ctx.getTargetInfo().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
1665 // FIXME(CIR): It isn't clear to me that this is the right answer here,
1666 // classic-codegen decides the answer is 'false' if there is an inalloca
1667 // argument or if there is a param that needs destruction.
1668 // When we get an understanding of what the the calling-convention code
1669 // needs here, we should be able to replace this with either a 'return
1670 // false' or 'return true'.
1671 // Perhaps we should be checking isParamDestroyedInCallee?
1672 cgm.errorNYI(d->getSourceRange(),
1673 "canEmitDelegateCallArgs: args-destroyed-L-to-R in callee");
1674 }
1675
1676 return true;
1677}
1678
1679void CIRGenFunction::emitInheritedCXXConstructorCall(
1680 const CXXConstructorDecl *d, bool forVirtualBase, Address thisAddr,
1681 bool inheritedFromVBase, const CXXInheritedCtorInitExpr *e) {
1682
1683 CallArgList ctorArgs;
1684 CallArg thisArg(RValue::get(v: getAsNaturalPointerTo(
1685 addr: thisAddr, pointeeType: d->getThisType()->getPointeeType())),
1686 d->getThisType());
1687
1688 if (inheritedFromVBase &&
1689 cgm.getTarget().getCXXABI().hasConstructorVariants()) {
1690 // The base-object variant doesn't construct virtual bases, so the
1691 // inherited constructor's arguments aren't passed.
1692 ctorArgs.push_back(Elt: thisArg);
1693 } else if (!cxxInheritedCtorInitExprArgs.empty()) {
1694 // The inheriting constructor was inlined; just inject its arguments.
1695 assert(cxxInheritedCtorInitExprArgs.size() >= d->getNumParams() &&
1696 "wrong number of parameters for inherited constructor call");
1697 ctorArgs = cxxInheritedCtorInitExprArgs;
1698 ctorArgs[0] = thisArg;
1699 } else {
1700 ctorArgs.push_back(Elt: thisArg);
1701 const auto *outerCtor = cast<CXXConstructorDecl>(Val: curCodeDecl);
1702 assert(outerCtor->getNumParams() == d->getNumParams());
1703 assert(!outerCtor->isVariadic() && "should have been inlined");
1704
1705 for (const ParmVarDecl *param : outerCtor->parameters()) {
1706 assert(getContext().hasSameUnqualifiedType(
1707 outerCtor->getParamDecl(param->getFunctionScopeIndex())->getType(),
1708 param->getType()));
1709 emitDelegateCallArg(args&: ctorArgs, param, loc: e->getLocation());
1710
1711 if (param->hasAttr<PassObjectSizeAttr>())
1712 cgm.errorNYI(
1713 e->getLocation(),
1714 "emitInheritedCXXConstructorCall: pass object size attr argument");
1715 }
1716 }
1717
1718 emitCXXConstructorCall(d, type: Ctor_Base, forVirtualBase, /*delegating=*/false,
1719 thisAddr, args&: ctorArgs, loc: e->getLocation());
1720}
1721
1722void CIRGenFunction::emitInlinedInheritingCXXConstructorCall(
1723 SourceLocation loc, const CXXConstructorDecl *d, CXXCtorType ctorType,
1724 bool forVirtualBase, bool delegating, CallArgList &args) {
1725 GlobalDecl gd(d, ctorType);
1726 assert(!cir::MissingFeatures::generateDebugInfo());
1727 SymTableScopeTy varScope(symbolTable);
1728 InlinedInheritingConstructorScope scope(*this, gd);
1729 RunCleanupsScope RunCleanups(*this);
1730
1731 // Save the arguments to be passed to the inherited constructor.
1732 cxxInheritedCtorInitExprArgs = args;
1733
1734 FunctionArgList params;
1735 QualType retTy = buildFunctionArgList(gd, args&: params);
1736 // FIXME(cir): When we get to the !isVoidType NYI below, this probably is
1737 // going to be important. In the meantime, this is likely not really doing
1738 // anything.
1739 fnRetTy = retTy;
1740
1741 cgm.getCXXABI().addImplicitConstructorArgs(cgf&: *this, d, type: ctorType, forVirtualBase,
1742 delegating, args);
1743
1744 // Emit a simplified prolog. We only need to emit the implicit params.
1745 assert(args.size() >= params.size() && "too few arguments for call");
1746 for (auto [idx, arg, parm] :
1747 llvm::zip_longest(t: llvm::index_range{0, args.size()}, u&: args, args&: params)) {
1748 if (idx < params.size() && isa<ImplicitParamDecl>(Val: *parm)) {
1749 mlir::Location parmLoc = getLoc(srcLoc: (*parm)->getSourceRange());
1750 RValue argVal = arg->getRValue(cgf&: *this, loc: parmLoc);
1751
1752 LValue allocaVal = makeAddrLValue(
1753 addr: createTempAlloca(ty: convertType(t: (*parm)->getType()),
1754 align: getContext().getDeclAlign(D: *parm), loc: parmLoc),
1755 ty: (*parm)->getType());
1756
1757 emitStoreThroughLValue(src: argVal, dst: allocaVal, /*isInit=*/true);
1758
1759 setAddrOfLocalVar(vd: (*parm), addr: allocaVal.getAddress());
1760 }
1761 }
1762
1763 // FIXME(cir): it isn't clear what it takes to get here with a constructor?
1764 // Leave as an NYI until we come across a reproducer.
1765 if (!retTy->isVoidType())
1766 cgm.errorNYI(d->getSourceRange(),
1767 "emitInlinedInheritingCXXConstructorCall: non-void return");
1768
1769 cgm.getCXXABI().emitInstanceFunctionProlog(loc, cgf&: *this);
1770 cxxThisValue = cxxabiThisValue;
1771 emitCtorPrologue(cd: d, ctorType, args&: params);
1772}
1773
1774void CIRGenFunction::emitCXXConstructorCall(
1775 const CXXConstructorDecl *d, CXXCtorType type, bool forVirtualBase,
1776 bool delegating, Address thisAddr, CallArgList &args, SourceLocation loc) {
1777
1778 const CXXRecordDecl *crd = d->getParent();
1779
1780 // If this is a call to a trivial default constructor:
1781 // In LLVM: do nothing.
1782 // In CIR: emit as a regular call, other later passes should lower the
1783 // ctor call into trivial initialization.
1784 assert(!cir::MissingFeatures::isTrivialCtorOrDtor());
1785
1786 // Note: memcpy-equivalent special members are handled in the
1787 // emitCXXConstructorCall overload that takes a CXXConstructExpr.
1788
1789 bool passPrototypeArgs = true;
1790
1791 // Check whether we can actually emit the constructor before trying to do so.
1792 if (auto inherited = d->getInheritedConstructor()) {
1793 passPrototypeArgs = getTypes().inheritingCtorHasParams(inherited, type);
1794 if (passPrototypeArgs &&
1795 !canEmitDelegateCallArgs(cgm, ctx&: cgm.getASTContext(), d, type)) {
1796 emitInlinedInheritingCXXConstructorCall(loc, d, ctorType: type, forVirtualBase,
1797 delegating, args);
1798 return;
1799 }
1800 }
1801
1802 // Insert any ABI-specific implicit constructor arguments.
1803 CIRGenCXXABI::AddedStructorArgCounts extraArgs =
1804 cgm.getCXXABI().addImplicitConstructorArgs(cgf&: *this, d, type, forVirtualBase,
1805 delegating, args);
1806
1807 // Emit the call.
1808 auto calleePtr = cgm.getAddrOfCXXStructor(gd: GlobalDecl(d, type));
1809 const CIRGenFunctionInfo &info = cgm.getTypes().arrangeCXXConstructorCall(
1810 args, d, ctorKind: type, extraPrefixArgs: extraArgs.prefix, extraSuffixArgs: extraArgs.suffix, passProtoArgs: passPrototypeArgs);
1811 CIRGenCallee callee = CIRGenCallee::forDirect(funcPtr: calleePtr, abstractInfo: GlobalDecl(d, type));
1812 cir::CIRCallOpInterface c;
1813 emitCall(funcInfo: info, callee, returnValue: ReturnValueSlot(), args, callOp: &c, /*isMustTail=*/false,
1814 clangLoc: loc);
1815
1816 if (cgm.getCodeGenOpts().OptimizationLevel != 0 && !crd->isDynamicClass() &&
1817 type != Ctor_Base && cgm.getCodeGenOpts().StrictVTablePointers)
1818 cgm.errorNYI(d->getSourceRange(), "vtable assumption loads");
1819}
1820