Debian Codegraph @

clang/lib/CIR/CodeGen/CIRGenClass.cpp

raw
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/EvaluatedExprVisitor.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/AST/Type.h"
21 #include "clang/CIR/Dialect/IR/CIRDialect.h"
22 #include "clang/CIR/MissingFeatures.h"
23
24 using namespace clang;
25 using namespace clang::CIRGen;
26
27 /// Checks whether the given constructor is a valid subject for the
28 /// complete-to-base constructor delegation optimization, i.e. emitting the
29 /// complete constructor as a simple call to the base constructor.
30 bool CIRGenFunction::isConstructorDelegationValid(
31 const CXXConstructorDecl *ctor) {
32 // Currently we disable the optimization for classes with virtual bases
33 // because (1) the address of parameter variables need to be consistent across
34 // all initializers but (2) the delegate function call necessarily creates a
35 // second copy of the parameter variable.
36 //
37 // The limiting example (purely theoretical AFAIK):
38 // struct A { A(int &c) { c++; } };
39 // struct A : virtual A {
40 // B(int count) : A(count) { printf("%d\n", count); }
41 // };
42 // ...although even this example could in principle be emitted as a delegation
43 // since the address of the parameter doesn't escape.
44 if (ctor->getParent()->getNumVBases())
45 return false;
46
47 // We also disable the optimization for variadic functions because it's
48 // impossible to "re-pass" varargs.
49 if (ctor->getType()->castAs<FunctionProtoType>()->isVariadic())
50 return false;
51
52 // FIXME: Decide if we can do a delegation of a delegating constructor.
53 if (ctor->isDelegatingConstructor())
54 return false;
55
56 return true;
57 }
58
59 static void emitLValueForAnyFieldInitialization(CIRGenFunction &cgf,
60 CXXCtorInitializer *memberInit,
61 LValue &lhs) {
62 FieldDecl *field = memberInit->getAnyMember();
63 if (memberInit->isIndirectMemberInitializer()) {
64 // If we are initializing an anonymous union field, drill down to the field.
65 IndirectFieldDecl *indirectField = memberInit->getIndirectMember();
66 for (const auto *nd : indirectField->chain()) {
67 auto *fd = cast<clang::FieldDecl>(nd);
68 lhs = cgf.emitLValueForFieldInitialization(lhs, fd, fd->getName());
69 }
70 } else {
71 lhs = cgf.emitLValueForFieldInitialization(lhs, field, field->getName());
72 }
73 }
74
75 static void emitMemberInitializer(CIRGenFunction &cgf,
76 const CXXRecordDecl *classDecl,
77 CXXCtorInitializer *memberInit,
78 const CXXConstructorDecl *constructor,
79 FunctionArgList &args) {
80 assert(memberInit->isAnyMemberInitializer() &&
81 "Must have member initializer!");
82 assert(memberInit->getInit() && "Must have initializer!");
83
84 assert(!cir::MissingFeatures::generateDebugInfo());
85
86 // non-static data member initializers
87 FieldDecl *field = memberInit->getAnyMember();
88 QualType fieldType = field->getType();
89
90 mlir::Value thisPtr = cgf.loadCXXThis();
91 CanQualType recordTy = cgf.getContext().getCanonicalTagType(classDecl);
92
93 // If a base constructor is being emitted, create an LValue that has the
94 // non-virtual alignment.
95 LValue lhs = (cgf.curGD.getCtorType() == Ctor_Base)
96 ? cgf.makeNaturalAlignPointeeAddrLValue(thisPtr, recordTy)
97 : cgf.makeNaturalAlignAddrLValue(thisPtr, recordTy);
98
99 emitLValueForAnyFieldInitialization(cgf, memberInit, lhs);
100
101 // Special case: If we are in a copy or move constructor, and we are copying
102 // an array off PODs or classes with trivial copy constructors, ignore the AST
103 // and perform the copy we know is equivalent.
104 // FIXME: This is hacky at best... if we had a bit more explicit information
105 // in the AST, we could generalize it more easily.
106 const ConstantArrayType *array =
107 cgf.getContext().getAsConstantArrayType(fieldType);
108 if (array && constructor->isDefaulted() &&
109 constructor->isCopyOrMoveConstructor()) {
110 QualType baseElementTy = cgf.getContext().getBaseElementType(array);
111 // NOTE(cir): CodeGen allows record types to be memcpy'd if applicable,
112 // whereas ClangIR wants to represent all object construction explicitly.
113 if (!baseElementTy->isRecordType()) {
114 unsigned srcArgIndex =
115 cgf.cgm.getCXXABI().getSrcArgforCopyCtor(constructor, args);
116 cir::LoadOp srcPtr = cgf.getBuilder().createLoad(
117 cgf.getLoc(memberInit->getSourceLocation()),
118 cgf.getAddrOfLocalVar(args[srcArgIndex]));
119 LValue thisRhslv = cgf.makeNaturalAlignAddrLValue(srcPtr, recordTy);
120 LValue src = cgf.emitLValueForFieldInitialization(thisRhslv, field,
121 field->getName());
122
123 // Copy the aggregate.
124 cgf.emitAggregateCopy(lhs, src, fieldType,
125 cgf.getOverlapForFieldInit(field),
126 lhs.isVolatileQualified());
127 // Ensure that we destroy the objects if an exception is thrown later in
128 // the constructor.
129 assert(!cgf.needsEHCleanup(fieldType.isDestructedType()) &&
130 "Arrays of non-record types shouldn't need EH cleanup");
131 return;
132 }
133 }
134
135 cgf.emitInitializerForField(field, lhs, memberInit->getInit());
136 }
137
138 static bool isInitializerOfDynamicClass(const CXXCtorInitializer *baseInit) {
139 const Type *baseType = baseInit->getBaseClass();
140 const auto *baseClassDecl = baseType->castAsCXXRecordDecl();
141 return baseClassDecl->isDynamicClass();
142 }
143
144 namespace {
145 /// Call the destructor for a direct base class.
146 struct CallBaseDtor final : EHScopeStack::Cleanup {
147 const CXXRecordDecl *baseClass;
148 bool baseIsVirtual;
149 CallBaseDtor(const CXXRecordDecl *base, bool baseIsVirtual)
150 : baseClass(base), baseIsVirtual(baseIsVirtual) {}
151
152 void emit(CIRGenFunction &cgf, Flags flags) override {
153 const CXXRecordDecl *derivedClass =
154 cast<CXXMethodDecl>(cgf.curFuncDecl)->getParent();
155
156 const CXXDestructorDecl *d = baseClass->getDestructor();
157 // We are already inside a destructor, so presumably the object being
158 // destroyed should have the expected type.
159 QualType thisTy = d->getFunctionObjectParameterType();
160 assert(cgf.currSrcLoc && "expected source location");
161 Address addr = cgf.getAddressOfDirectBaseInCompleteClass(
162 *cgf.currSrcLoc, cgf.loadCXXThisAddress(), derivedClass, baseClass,
163 baseIsVirtual);
164 cgf.emitCXXDestructorCall(d, Dtor_Base, baseIsVirtual,
165 /*delegating=*/false, addr, thisTy);
166 }
167 };
168
169 /// A visitor which checks whether an initializer uses 'this' in a
170 /// way which requires the vtable to be properly set.
171 struct DynamicThisUseChecker
172 : ConstEvaluatedExprVisitor<DynamicThisUseChecker> {
173 using super = ConstEvaluatedExprVisitor<DynamicThisUseChecker>;
174
175 bool usesThis = false;
176
177 DynamicThisUseChecker(const ASTContext &c) : super(c) {}
178
179 // Black-list all explicit and implicit references to 'this'.
180 //
181 // Do we need to worry about external references to 'this' derived
182 // from arbitrary code? If so, then anything which runs arbitrary
183 // external code might potentially access the vtable.
184 void VisitCXXThisExpr(const CXXThisExpr *e) { usesThis = true; }
185 };
186 } // end anonymous namespace
187
188 static bool baseInitializerUsesThis(ASTContext &c, const Expr *init) {
189 DynamicThisUseChecker checker(c);
190 checker.Visit(init);
191 return checker.usesThis;
192 }
193
194 /// Gets the address of a direct base class within a complete object.
195 /// This should only be used for (1) non-virtual bases or (2) virtual bases
196 /// when the type is known to be complete (e.g. in complete destructors).
197 ///
198 /// The object pointed to by 'thisAddr' is assumed to be non-null.
199 Address CIRGenFunction::getAddressOfDirectBaseInCompleteClass(
200 mlir::Location loc, Address thisAddr, const CXXRecordDecl *derived,
201 const CXXRecordDecl *base, bool baseIsVirtual) {
202 // 'thisAddr' must be a pointer (in some address space) to Derived.
203 assert(thisAddr.getElementType() == convertType(derived));
204
205 // Compute the offset of the virtual base.
206 CharUnits offset;
207 const ASTRecordLayout &layout = getContext().getASTRecordLayout(derived);
208 if (baseIsVirtual)
209 offset = layout.getVBaseClassOffset(base);
210 else
211 offset = layout.getBaseClassOffset(base);
212
213 return builder.createBaseClassAddr(loc, thisAddr, convertType(base),
214 offset.getQuantity(),
215 /*assumeNotNull=*/true);
216 }
217
218 void CIRGenFunction::emitBaseInitializer(mlir::Location loc,
219 const CXXRecordDecl *classDecl,
220 CXXCtorInitializer *baseInit) {
221 assert(curFuncDecl && "loading 'this' without a func declaration?");
222 assert(isa<CXXMethodDecl>(curFuncDecl));
223
224 assert(baseInit->isBaseInitializer() && "Must have base initializer!");
225
226 Address thisPtr = loadCXXThisAddress();
227
228 const Type *baseType = baseInit->getBaseClass();
229 const auto *baseClassDecl = baseType->castAsCXXRecordDecl();
230
231 bool isBaseVirtual = baseInit->isBaseVirtual();
232
233 // If the initializer for the base (other than the constructor
234 // itself) accesses 'this' in any way, we need to initialize the
235 // vtables.
236 if (baseInitializerUsesThis(getContext(), baseInit->getInit()))
237 initializeVTablePointers(loc, classDecl);
238
239 // We can pretend to be a complete class because it only matters for
240 // virtual bases, and we only do virtual bases for complete ctors.
241 Address v = getAddressOfDirectBaseInCompleteClass(
242 loc, thisPtr, classDecl, baseClassDecl, isBaseVirtual);
243 assert(!cir::MissingFeatures::aggValueSlotGC());
244 AggValueSlot aggSlot = AggValueSlot::forAddr(
245 v, Qualifiers(), AggValueSlot::IsDestructed, AggValueSlot::IsNotAliased,
246 getOverlapForBaseInit(classDecl, baseClassDecl, isBaseVirtual));
247
248 emitAggExpr(baseInit->getInit(), aggSlot);
249
250 assert(!cir::MissingFeatures::requiresCleanups());
251 }
252
253 /// This routine generates necessary code to initialize base classes and
254 /// non-static data members belonging to this constructor.
255 void CIRGenFunction::emitCtorPrologue(const CXXConstructorDecl *cd,
256 CXXCtorType ctorType,
257 FunctionArgList &args) {
258 if (cd->isDelegatingConstructor()) {
259 emitDelegatingCXXConstructorCall(cd, args);
260 return;
261 }
262
263 const CXXRecordDecl *classDecl = cd->getParent();
264
265 // Virtual base initializers aren't needed if:
266 // - This is a base ctor variant
267 // - There are no vbases
268 // - The class is abstract, so a complete object of it cannot be constructed
269 //
270 // The check for an abstract class is necessary because sema may not have
271 // marked virtual base destructors referenced.
272 bool constructVBases = ctorType != Ctor_Base &&
273 classDecl->getNumVBases() != 0 &&
274 !classDecl->isAbstract();
275 if (constructVBases &&
276 !cgm.getTarget().getCXXABI().hasConstructorVariants()) {
277 cgm.errorNYI(cd->getSourceRange(),
278 "emitCtorPrologue: virtual base without variants");
279 return;
280 }
281
282 // Create three separate ranges for the different types of initializers.
283 auto allInits = cd->inits();
284
285 // Find the boundaries between the three groups.
286 auto virtualBaseEnd = std::find_if(
287 allInits.begin(), allInits.end(), [](const CXXCtorInitializer *Init) {
288 return !(Init->isBaseInitializer() && Init->isBaseVirtual());
289 });
290
291 auto nonVirtualBaseEnd = std::find_if(virtualBaseEnd, allInits.end(),
292 [](const CXXCtorInitializer *Init) {
293 return !Init->isBaseInitializer();
294 });
295
296 // Create the three ranges.
297 auto virtualBaseInits = llvm::make_range(allInits.begin(), virtualBaseEnd);
298 auto nonVirtualBaseInits =
299 llvm::make_range(virtualBaseEnd, nonVirtualBaseEnd);
300 auto memberInits = llvm::make_range(nonVirtualBaseEnd, allInits.end());
301
302 const mlir::Value oldThisValue = cxxThisValue;
303
304 auto emitInitializer = [&](CXXCtorInitializer *baseInit) {
305 if (cgm.getCodeGenOpts().StrictVTablePointers &&
306 cgm.getCodeGenOpts().OptimizationLevel > 0 &&
307 isInitializerOfDynamicClass(baseInit)) {
308 // It's OK to continue after emitting the error here. The missing code
309 // just "launders" the 'this' pointer.
310 cgm.errorNYI(cd->getSourceRange(),
311 "emitCtorPrologue: strict vtable pointers for vbase");
312 }
313 emitBaseInitializer(getLoc(cd->getBeginLoc()), classDecl, baseInit);
314 };
315
316 // Process virtual base initializers.
317 for (CXXCtorInitializer *virtualBaseInit : virtualBaseInits) {
318 if (!constructVBases)
319 continue;
320 emitInitializer(virtualBaseInit);
321 }
322
323 assert(!cir::MissingFeatures::msabi());
324
325 // Then, non-virtual base initializers.
326 for (CXXCtorInitializer *nonVirtualBaseInit : nonVirtualBaseInits) {
327 assert(!nonVirtualBaseInit->isBaseVirtual());
328 emitInitializer(nonVirtualBaseInit);
329 }
330
331 cxxThisValue = oldThisValue;
332
333 initializeVTablePointers(getLoc(cd->getBeginLoc()), classDecl);
334
335 // Finally, initialize class members.
336 FieldConstructionScope fcs(*this, loadCXXThisAddress());
337 // Classic codegen uses a special class to attempt to replace member
338 // initializers with memcpy. We could possibly defer that to the
339 // lowering or optimization phases to keep the memory accesses more
340 // explicit. For now, we don't insert memcpy at all.
341 assert(!cir::MissingFeatures::ctorMemcpyizer());
342 for (CXXCtorInitializer *member : memberInits) {
343 assert(!member->isBaseInitializer());
344 assert(member->isAnyMemberInitializer() &&
345 "Delegating initializer on non-delegating constructor");
346 emitMemberInitializer(*this, cd->getParent(), member, cd, args);
347 }
348 }
349
350 static Address applyNonVirtualAndVirtualOffset(
351 mlir::Location loc, CIRGenFunction &cgf, Address addr,
352 CharUnits nonVirtualOffset, mlir::Value virtualOffset,
353 const CXXRecordDecl *derivedClass, const CXXRecordDecl *nearestVBase,
354 mlir::Type baseValueTy = {}, bool assumeNotNull = true) {
355 // Assert that we have something to do.
356 assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
357
358 // Compute the offset from the static and dynamic components.
359 mlir::Value baseOffset;
360 if (!nonVirtualOffset.isZero()) {
361 if (virtualOffset) {
362 cgf.cgm.errorNYI(
363 loc,
364 "applyNonVirtualAndVirtualOffset: virtual and non-virtual offset");
365 return Address::invalid();
366 } else {
367 assert(baseValueTy && "expected base type");
368 // If no virtualOffset is present this is the final stop.
369 return cgf.getBuilder().createBaseClassAddr(
370 loc, addr, baseValueTy, nonVirtualOffset.getQuantity(),
371 assumeNotNull);
372 }
373 } else {
374 baseOffset = virtualOffset;
375 }
376
377 // Apply the base offset. cir.ptr_stride adjusts by a number of elements,
378 // not bytes. So the pointer must be cast to a byte pointer and back.
379
380 mlir::Value ptr = addr.getPointer();
381 mlir::Type charPtrType = cgf.cgm.uInt8PtrTy;
382 mlir::Value charPtr = cgf.getBuilder().createBitcast(ptr, charPtrType);
383 mlir::Value adjusted = cir::PtrStrideOp::create(
384 cgf.getBuilder(), loc, charPtrType, charPtr, baseOffset);
385 ptr = cgf.getBuilder().createBitcast(adjusted, ptr.getType());
386
387 // If we have a virtual component, the alignment of the result will
388 // be relative only to the known alignment of that vbase.
389 CharUnits alignment;
390 if (virtualOffset) {
391 assert(nearestVBase && "virtual offset without vbase?");
392 alignment = cgf.cgm.getVBaseAlignment(addr.getAlignment(), derivedClass,
393 nearestVBase);
394 } else {
395 alignment = addr.getAlignment();
396 }
397 alignment = alignment.alignmentAtOffset(nonVirtualOffset);
398
399 return Address(ptr, alignment);
400 }
401
402 void CIRGenFunction::initializeVTablePointer(mlir::Location loc,
403 const VPtr &vptr) {
404 // Compute the address point.
405 mlir::Value vtableAddressPoint =
406 cgm.getCXXABI().getVTableAddressPointInStructor(
407 *this, vptr.vtableClass, vptr.base, vptr.nearestVBase);
408
409 if (!vtableAddressPoint)
410 return;
411
412 // Compute where to store the address point.
413 mlir::Value virtualOffset{};
414 CharUnits nonVirtualOffset = CharUnits::Zero();
415
416 mlir::Type baseValueTy;
417 if (cgm.getCXXABI().isVirtualOffsetNeededForVTableField(*this, vptr)) {
418 // We need to use the virtual base offset offset because the virtual base
419 // might have a different offset in the most derived class.
420 virtualOffset = cgm.getCXXABI().getVirtualBaseClassOffset(
421 loc, *this, loadCXXThisAddress(), vptr.vtableClass, vptr.nearestVBase);
422 nonVirtualOffset = vptr.offsetFromNearestVBase;
423 } else {
424 // We can just use the base offset in the complete class.
425 nonVirtualOffset = vptr.base.getBaseOffset();
426 baseValueTy =
427 convertType(getContext().getCanonicalTagType(vptr.base.getBase()));
428 }
429
430 // Apply the offsets.
431 Address classAddr = loadCXXThisAddress();
432 if (!nonVirtualOffset.isZero() || virtualOffset) {
433 classAddr = applyNonVirtualAndVirtualOffset(
434 loc, *this, classAddr, nonVirtualOffset, virtualOffset,
435 vptr.vtableClass, vptr.nearestVBase, baseValueTy);
436 }
437
438 // Finally, store the address point. Use the same CIR types as the field.
439 //
440 // vtable field is derived from `this` pointer, therefore they should be in
441 // the same addr space.
442 assert(!cir::MissingFeatures::addressSpace());
443 auto vtablePtr =
444 cir::VTableGetVPtrOp::create(builder, loc, classAddr.getPointer());
445 Address vtableField = Address(vtablePtr, classAddr.getAlignment());
446 builder.createStore(loc, vtableAddressPoint, vtableField);
447 assert(!cir::MissingFeatures::opTBAA());
448 assert(!cir::MissingFeatures::createInvariantGroup());
449 }
450
451 void CIRGenFunction::initializeVTablePointers(mlir::Location loc,
452 const CXXRecordDecl *rd) {
453 // Ignore classes without a vtable.
454 if (!rd->isDynamicClass())
455 return;
456
457 // Initialize the vtable pointers for this class and all of its bases.
458 if (cgm.getCXXABI().doStructorsInitializeVPtrs(rd))
459 for (const auto &vptr : getVTablePointers(rd))
460 initializeVTablePointer(loc, vptr);
461
462 if (rd->getNumVBases())
463 cgm.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, rd);
464 }
465
466 CIRGenFunction::VPtrsVector
467 CIRGenFunction::getVTablePointers(const CXXRecordDecl *vtableClass) {
468 CIRGenFunction::VPtrsVector vptrsResult;
469 VisitedVirtualBasesSetTy vbases;
470 getVTablePointers(BaseSubobject(vtableClass, CharUnits::Zero()),
471 /*NearestVBase=*/nullptr,
472 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
473 /*BaseIsNonVirtualPrimaryBase=*/false, vtableClass, vbases,
474 vptrsResult);
475 return vptrsResult;
476 }
477
478 void CIRGenFunction::getVTablePointers(BaseSubobject base,
479 const CXXRecordDecl *nearestVBase,
480 CharUnits offsetFromNearestVBase,
481 bool baseIsNonVirtualPrimaryBase,
482 const CXXRecordDecl *vtableClass,
483 VisitedVirtualBasesSetTy &vbases,
484 VPtrsVector &vptrs) {
485 // If this base is a non-virtual primary base the address point has already
486 // been set.
487 if (!baseIsNonVirtualPrimaryBase) {
488 // Initialize the vtable pointer for this base.
489 VPtr vptr = {base, nearestVBase, offsetFromNearestVBase, vtableClass};
490 vptrs.push_back(vptr);
491 }
492
493 const CXXRecordDecl *rd = base.getBase();
494
495 for (const auto &nextBase : rd->bases()) {
496 const auto *baseDecl =
497 cast<CXXRecordDecl>(nextBase.getType()->castAs<RecordType>()->getDecl())
498 ->getDefinitionOrSelf();
499
500 // Ignore classes without a vtable.
501 if (!baseDecl->isDynamicClass())
502 continue;
503
504 CharUnits baseOffset;
505 CharUnits baseOffsetFromNearestVBase;
506 bool baseDeclIsNonVirtualPrimaryBase;
507 const CXXRecordDecl *nextBaseDecl;
508
509 if (nextBase.isVirtual()) {
510 // Check if we've visited this virtual base before.
511 if (!vbases.insert(baseDecl).second)
512 continue;
513
514 const ASTRecordLayout &layout =
515 getContext().getASTRecordLayout(vtableClass);
516
517 nextBaseDecl = baseDecl;
518 baseOffset = layout.getVBaseClassOffset(baseDecl);
519 baseOffsetFromNearestVBase = CharUnits::Zero();
520 baseDeclIsNonVirtualPrimaryBase = false;
521 } else {
522 const ASTRecordLayout &layout = getContext().getASTRecordLayout(rd);
523
524 nextBaseDecl = nearestVBase;
525 baseOffset = base.getBaseOffset() + layout.getBaseClassOffset(baseDecl);
526 baseOffsetFromNearestVBase =
527 offsetFromNearestVBase + layout.getBaseClassOffset(baseDecl);
528 baseDeclIsNonVirtualPrimaryBase = layout.getPrimaryBase() == baseDecl;
529 }
530
531 getVTablePointers(BaseSubobject(baseDecl, baseOffset), nextBaseDecl,
532 baseOffsetFromNearestVBase,
533 baseDeclIsNonVirtualPrimaryBase, vtableClass, vbases,
534 vptrs);
535 }
536 }
537
538 Address CIRGenFunction::loadCXXThisAddress() {
539 assert(curFuncDecl && "loading 'this' without a func declaration?");
540 assert(isa<CXXMethodDecl>(curFuncDecl));
541
542 // Lazily compute CXXThisAlignment.
543 if (cxxThisAlignment.isZero()) {
544 // Just use the best known alignment for the parent.
545 // TODO: if we're currently emitting a complete-object ctor/dtor, we can
546 // always use the complete-object alignment.
547 auto rd = cast<CXXMethodDecl>(curFuncDecl)->getParent();
548 cxxThisAlignment = cgm.getClassPointerAlignment(rd);
549 }
550
551 return Address(loadCXXThis(), cxxThisAlignment);
552 }
553
554 void CIRGenFunction::emitInitializerForField(FieldDecl *field, LValue lhs,
555 Expr *init) {
556 QualType fieldType = field->getType();
557 switch (getEvaluationKind(fieldType)) {
558 case cir::TEK_Scalar:
559 if (lhs.isSimple()) {
560 emitExprAsInit(init, field, lhs, false);
561 } else {
562 RValue rhs = RValue::get(emitScalarExpr(init));
563 emitStoreThroughLValue(rhs, lhs);
564 }
565 break;
566 case cir::TEK_Complex:
567 emitComplexExprIntoLValue(init, lhs, /*isInit=*/true);
568 break;
569 case cir::TEK_Aggregate: {
570 assert(!cir::MissingFeatures::aggValueSlotGC());
571 assert(!cir::MissingFeatures::sanitizers());
572 AggValueSlot slot = AggValueSlot::forLValue(
573 lhs, AggValueSlot::IsDestructed, AggValueSlot::IsNotAliased,
574 getOverlapForFieldInit(field), AggValueSlot::IsNotZeroed);
575 emitAggExpr(init, slot);
576 break;
577 }
578 }
579
580 // Ensure that we destroy this object if an exception is thrown later in the
581 // constructor.
582 QualType::DestructionKind dtorKind = fieldType.isDestructedType();
583 (void)dtorKind;
584 assert(!cir::MissingFeatures::requiresCleanups());
585 }
586
587 Address CIRGenFunction::emitCXXMemberDataPointerAddress(
588 const Expr *e, Address base, mlir::Value memberPtr,
589 const MemberPointerType *memberPtrType, LValueBaseInfo *baseInfo) {
590 assert(!cir::MissingFeatures::cxxABI());
591
592 cir::GetRuntimeMemberOp op = builder.createGetIndirectMember(
593 getLoc(e->getSourceRange()), base.getPointer(), memberPtr);
594
595 QualType memberType = memberPtrType->getPointeeType();
596 assert(!cir::MissingFeatures::opTBAA());
597 CharUnits memberAlign = cgm.getNaturalTypeAlignment(memberType, baseInfo);
598 memberAlign = cgm.getDynamicOffsetAlignment(
599 base.getAlignment(), memberPtrType->getMostRecentCXXRecordDecl(),
600 memberAlign);
601
602 return Address(op, convertTypeForMem(memberPtrType->getPointeeType()),
603 memberAlign);
604 }
605
606 CharUnits
607 CIRGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign,
608 const CXXRecordDecl *baseDecl,
609 CharUnits expectedTargetAlign) {
610 // If the base is an incomplete type (which is, alas, possible with
611 // member pointers), be pessimistic.
612 if (!baseDecl->isCompleteDefinition())
613 return std::min(actualBaseAlign, expectedTargetAlign);
614
615 const ASTRecordLayout &baseLayout =
616 getASTContext().getASTRecordLayout(baseDecl);
617 CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment();
618
619 // If the class is properly aligned, assume the target offset is, too.
620 //
621 // This actually isn't necessarily the right thing to do --- if the
622 // class is a complete object, but it's only properly aligned for a
623 // base subobject, then the alignments of things relative to it are
624 // probably off as well. (Note that this requires the alignment of
625 // the target to be greater than the NV alignment of the derived
626 // class.)
627 //
628 // However, our approach to this kind of under-alignment can only
629 // ever be best effort; after all, we're never going to propagate
630 // alignments through variables or parameters. Note, in particular,
631 // that constructing a polymorphic type in an address that's less
632 // than pointer-aligned will generally trap in the constructor,
633 // unless we someday add some sort of attribute to change the
634 // assumed alignment of 'this'. So our goal here is pretty much
635 // just to allow the user to explicitly say that a pointer is
636 // under-aligned and then safely access its fields and vtables.
637 if (actualBaseAlign >= expectedBaseAlign)
638 return expectedTargetAlign;
639
640 // Otherwise, we might be offset by an arbitrary multiple of the
641 // actual alignment. The correct adjustment is to take the min of
642 // the two alignments.
643 return std::min(actualBaseAlign, expectedTargetAlign);
644 }
645
646 /// Return the best known alignment for a pointer to a virtual base,
647 /// given the alignment of a pointer to the derived class.
648 clang::CharUnits
649 CIRGenModule::getVBaseAlignment(CharUnits actualDerivedAlign,
650 const CXXRecordDecl *derivedClass,
651 const CXXRecordDecl *vbaseClass) {
652 // The basic idea here is that an underaligned derived pointer might
653 // indicate an underaligned base pointer.
654
655 assert(vbaseClass->isCompleteDefinition());
656 const ASTRecordLayout &baseLayout =
657 getASTContext().getASTRecordLayout(vbaseClass);
658 CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment();
659
660 return getDynamicOffsetAlignment(actualDerivedAlign, derivedClass,
661 expectedVBaseAlign);
662 }
663
664 /// Emit a loop to call a particular constructor for each of several members
665 /// of an array.
666 ///
667 /// \param ctor the constructor to call for each element
668 /// \param arrayType the type of the array to initialize
669 /// \param arrayBegin an arrayType*
670 /// \param zeroInitialize true if each element should be
671 /// zero-initialized before it is constructed
672 void CIRGenFunction::emitCXXAggrConstructorCall(
673 const CXXConstructorDecl *ctor, const clang::ArrayType *arrayType,
674 Address arrayBegin, const CXXConstructExpr *e, bool newPointerIsChecked,
675 bool zeroInitialize) {
676 QualType elementType;
677 mlir::Value numElements = emitArrayLength(arrayType, elementType, arrayBegin);
678 emitCXXAggrConstructorCall(ctor, numElements, arrayBegin, e,
679 newPointerIsChecked, zeroInitialize);
680 }
681
682 /// Emit a loop to call a particular constructor for each of several members
683 /// of an array.
684 ///
685 /// \param ctor the constructor to call for each element
686 /// \param numElements the number of elements in the array;
687 /// may be zero
688 /// \param arrayBase a T*, where T is the type constructed by ctor
689 /// \param zeroInitialize true if each element should be
690 /// zero-initialized before it is constructed
691 void CIRGenFunction::emitCXXAggrConstructorCall(
692 const CXXConstructorDecl *ctor, mlir::Value numElements, Address arrayBase,
693 const CXXConstructExpr *e, bool newPointerIsChecked, bool zeroInitialize) {
694 // It's legal for numElements to be zero. This can happen both
695 // dynamically, because x can be zero in 'new A[x]', and statically,
696 // because of GCC extensions that permit zero-length arrays. There
697 // are probably legitimate places where we could assume that this
698 // doesn't happen, but it's not clear that it's worth it.
699
700 auto arrayTy = mlir::cast<cir::ArrayType>(arrayBase.getElementType());
701 mlir::Type elementType = arrayTy.getElementType();
702
703 // This might be a multi-dimensional array. Find the innermost element type.
704 while (auto maybeArrayTy = mlir::dyn_cast<cir::ArrayType>(elementType))
705 elementType = maybeArrayTy.getElementType();
706 cir::PointerType ptrToElmType = builder.getPointerTo(elementType);
707
708 // Optimize for a constant count.
709 if (auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
710 if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>()) {
711 // Just skip out if the constant count is zero.
712 if (constIntAttr.getUInt() == 0)
713 return;
714
715 arrayTy = cir::ArrayType::get(elementType, constIntAttr.getUInt());
716 // Otherwise, emit the check.
717 }
718
719 if (constantCount.use_empty())
720 constantCount.erase();
721 } else {
722 // Otherwise, emit the check.
723 cgm.errorNYI(e->getSourceRange(), "dynamic-length array expression");
724 }
725
726 // Tradional LLVM codegen emits a loop here. CIR lowers to a loop as part of
727 // LoweringPrepare.
728
729 // The alignment of the base, adjusted by the size of a single element,
730 // provides a conservative estimate of the alignment of every element.
731 // (This assumes we never start tracking offsetted alignments.)
732 //
733 // Note that these are complete objects and so we don't need to
734 // use the non-virtual size or alignment.
735 CanQualType type = getContext().getCanonicalTagType(ctor->getParent());
736 CharUnits eltAlignment = arrayBase.getAlignment().alignmentOfArrayElement(
737 getContext().getTypeSizeInChars(type));
738
739 // Zero initialize the storage, if requested.
740 if (zeroInitialize)
741 emitNullInitialization(*currSrcLoc, arrayBase, type);
742
743 // C++ [class.temporary]p4:
744 // There are two contexts in which temporaries are destroyed at a different
745 // point than the end of the full-expression. The first context is when a
746 // default constructor is called to initialize an element of an array.
747 // If the constructor has one or more default arguments, the destruction of
748 // every temporary created in a default argument expression is sequenced
749 // before the construction of the next array element, if any.
750 {
751 RunCleanupsScope scope(*this);
752
753 // Evaluate the constructor and its arguments in a regular
754 // partial-destroy cleanup.
755 if (getLangOpts().Exceptions &&
756 !ctor->getParent()->hasTrivialDestructor()) {
757 cgm.errorNYI(e->getSourceRange(), "partial array cleanups");
758 }
759
760 // Emit the constructor call that will execute for every array element.
761 mlir::Value arrayOp =
762 builder.createPtrBitcast(arrayBase.getPointer(), arrayTy);
763 cir::ArrayCtor::create(
764 builder, *currSrcLoc, arrayOp,
765 [&](mlir::OpBuilder &b, mlir::Location loc) {
766 mlir::BlockArgument arg =
767 b.getInsertionBlock()->addArgument(ptrToElmType, loc);
768 Address curAddr = Address(arg, elementType, eltAlignment);
769 assert(!cir::MissingFeatures::sanitizers());
770 auto currAVS = AggValueSlot::forAddr(
771 curAddr, type.getQualifiers(), AggValueSlot::IsDestructed,
772 AggValueSlot::IsNotAliased, AggValueSlot::DoesNotOverlap,
773 AggValueSlot::IsNotZeroed);
774 emitCXXConstructorCall(ctor, Ctor_Complete,
775 /*ForVirtualBase=*/false,
776 /*Delegating=*/false, currAVS, e);
777 cir::YieldOp::create(builder, loc);
778 });
779 }
780 }
781
782 void CIRGenFunction::emitDelegateCXXConstructorCall(
783 const CXXConstructorDecl *ctor, CXXCtorType ctorType,
784 const FunctionArgList &args, SourceLocation loc) {
785 CallArgList delegateArgs;
786
787 FunctionArgList::const_iterator i = args.begin(), e = args.end();
788 assert(i != e && "no parameters to constructor");
789
790 // this
791 Address thisAddr = loadCXXThisAddress();
792 delegateArgs.add(RValue::get(thisAddr.getPointer()), (*i)->getType());
793 ++i;
794
795 // FIXME: The location of the VTT parameter in the parameter list is specific
796 // to the Itanium ABI and shouldn't be hardcoded here.
797 if (cgm.getCXXABI().needsVTTParameter(curGD)) {
798 cgm.errorNYI(loc, "emitDelegateCXXConstructorCall: VTT parameter");
799 return;
800 }
801
802 // Explicit arguments.
803 for (; i != e; ++i) {
804 const VarDecl *param = *i;
805 // FIXME: per-argument source location
806 emitDelegateCallArg(delegateArgs, param, loc);
807 }
808
809 assert(!cir::MissingFeatures::sanitizers());
810
811 emitCXXConstructorCall(ctor, ctorType, /*ForVirtualBase=*/false,
812 /*Delegating=*/true, thisAddr, delegateArgs, loc);
813 }
814
815 void CIRGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &args) {
816 const auto *assignOp = cast<CXXMethodDecl>(curGD.getDecl());
817 assert(assignOp->isCopyAssignmentOperator() ||
818 assignOp->isMoveAssignmentOperator());
819 const Stmt *rootS = assignOp->getBody();
820 assert(isa<CompoundStmt>(rootS) &&
821 "Body of an implicit assignment operator should be compound stmt.");
822 const auto *rootCS = cast<CompoundStmt>(rootS);
823
824 cgm.setCXXSpecialMemberAttr(cast<cir::FuncOp>(curFn), assignOp);
825
826 assert(!cir::MissingFeatures::incrementProfileCounter());
827 assert(!cir::MissingFeatures::runCleanupsScope());
828
829 // Classic codegen uses a special class to attempt to replace member
830 // initializers with memcpy. We could possibly defer that to the
831 // lowering or optimization phases to keep the memory accesses more
832 // explicit. For now, we don't insert memcpy at all, though in some
833 // cases the AST contains a call to memcpy.
834 assert(!cir::MissingFeatures::assignMemcpyizer());
835 for (Stmt *s : rootCS->body())
836 if (emitStmt(s, /*useCurrentScope=*/true).failed())
837 cgm.errorNYI(s->getSourceRange(),
838 std::string("emitImplicitAssignmentOperatorBody: ") +
839 s->getStmtClassName());
840 }
841
842 void CIRGenFunction::emitForwardingCallToLambda(
843 const CXXMethodDecl *callOperator, CallArgList &callArgs) {
844 // Get the address of the call operator.
845 const CIRGenFunctionInfo &calleeFnInfo =
846 cgm.getTypes().arrangeCXXMethodDeclaration(callOperator);
847 cir::FuncOp calleePtr = cgm.getAddrOfFunction(
848 GlobalDecl(callOperator), cgm.getTypes().getFunctionType(calleeFnInfo));
849
850 // Prepare the return slot.
851 const FunctionProtoType *fpt =
852 callOperator->getType()->castAs<FunctionProtoType>();
853 QualType resultType = fpt->getReturnType();
854 ReturnValueSlot returnSlot;
855
856 // We don't need to separately arrange the call arguments because
857 // the call can't be variadic anyway --- it's impossible to forward
858 // variadic arguments.
859
860 // Now emit our call.
861 CIRGenCallee callee =
862 CIRGenCallee::forDirect(calleePtr, GlobalDecl(callOperator));
863 RValue rv = emitCall(calleeFnInfo, callee, returnSlot, callArgs);
864
865 // If necessary, copy the returned value into the slot.
866 if (!resultType->isVoidType() && returnSlot.isNull()) {
867 if (getLangOpts().ObjCAutoRefCount && resultType->isObjCRetainableType())
868 cgm.errorNYI(callOperator->getSourceRange(),
869 "emitForwardingCallToLambda: ObjCAutoRefCount");
870 emitReturnOfRValue(*currSrcLoc, rv, resultType);
871 } else {
872 cgm.errorNYI(callOperator->getSourceRange(),
873 "emitForwardingCallToLambda: return slot is not null");
874 }
875 }
876
877 void CIRGenFunction::emitLambdaDelegatingInvokeBody(const CXXMethodDecl *md) {
878 const CXXRecordDecl *lambda = md->getParent();
879
880 // Start building arguments for forwarding call
881 CallArgList callArgs;
882
883 QualType lambdaType = getContext().getCanonicalTagType(lambda);
884 QualType thisType = getContext().getPointerType(lambdaType);
885 Address thisPtr =
886 createMemTemp(lambdaType, getLoc(md->getSourceRange()), "unused.capture");
887 callArgs.add(RValue::get(thisPtr.getPointer()), thisType);
888
889 // Add the rest of the parameters.
890 for (auto *param : md->parameters())
891 emitDelegateCallArg(callArgs, param, param->getBeginLoc());
892
893 const CXXMethodDecl *callOp = lambda->getLambdaCallOperator();
894 // For a generic lambda, find the corresponding call operator specialization
895 // to which the call to the static-invoker shall be forwarded.
896 if (lambda->isGenericLambda()) {
897 assert(md->isFunctionTemplateSpecialization());
898 const TemplateArgumentList *tal = md->getTemplateSpecializationArgs();
899 FunctionTemplateDecl *callOpTemplate =
900 callOp->getDescribedFunctionTemplate();
901 void *InsertPos = nullptr;
902 FunctionDecl *correspondingCallOpSpecialization =
903 callOpTemplate->findSpecialization(tal->asArray(), InsertPos);
904 assert(correspondingCallOpSpecialization);
905 callOp = cast<CXXMethodDecl>(correspondingCallOpSpecialization);
906 }
907 emitForwardingCallToLambda(callOp, callArgs);
908 }
909
910 void CIRGenFunction::emitLambdaStaticInvokeBody(const CXXMethodDecl *md) {
911 if (md->isVariadic()) {
912 // Codgen for LLVM doesn't emit code for this as well, it says:
913 // FIXME: Making this work correctly is nasty because it requires either
914 // cloning the body of the call operator or making the call operator
915 // forward.
916 cgm.errorNYI(md->getSourceRange(), "emitLambdaStaticInvokeBody: variadic");
917 }
918
919 emitLambdaDelegatingInvokeBody(md);
920 }
921
922 void CIRGenFunction::destroyCXXObject(CIRGenFunction &cgf, Address addr,
923 QualType type) {
924 const auto *record = type->castAsCXXRecordDecl();
925 const CXXDestructorDecl *dtor = record->getDestructor();
926 // TODO(cir): Unlike traditional codegen, CIRGen should actually emit trivial
927 // dtors which shall be removed on later CIR passes. However, only remove this
928 // assertion after we have a test case to exercise this path.
929 assert(!dtor->isTrivial());
930 cgf.emitCXXDestructorCall(dtor, Dtor_Complete, /*forVirtualBase*/ false,
931 /*delegating=*/false, addr, type);
932 }
933
934 namespace {
935 mlir::Value loadThisForDtorDelete(CIRGenFunction &cgf,
936 const CXXDestructorDecl *dd) {
937 if (Expr *thisArg = dd->getOperatorDeleteThisArg())
938 return cgf.emitScalarExpr(thisArg);
939 return cgf.loadCXXThis();
940 }
941
942 /// Call the operator delete associated with the current destructor.
943 struct CallDtorDelete final : EHScopeStack::Cleanup {
944 CallDtorDelete() {}
945
946 void emit(CIRGenFunction &cgf, Flags flags) override {
947 const CXXDestructorDecl *dtor = cast<CXXDestructorDecl>(cgf.curFuncDecl);
948 const CXXRecordDecl *classDecl = dtor->getParent();
949 cgf.emitDeleteCall(dtor->getOperatorDelete(),
950 loadThisForDtorDelete(cgf, dtor),
951 cgf.getContext().getCanonicalTagType(classDecl));
952 }
953 };
954
955 class DestroyField final : public EHScopeStack::Cleanup {
956 const FieldDecl *field;
957 CIRGenFunction::Destroyer *destroyer;
958
959 public:
960 DestroyField(const FieldDecl *field, CIRGenFunction::Destroyer *destroyer)
961 : field(field), destroyer(destroyer) {}
962
963 void emit(CIRGenFunction &cgf, Flags flags) override {
964 // Find the address of the field.
965 Address thisValue = cgf.loadCXXThisAddress();
966 CanQualType recordTy =
967 cgf.getContext().getCanonicalTagType(field->getParent());
968 LValue thisLV = cgf.makeAddrLValue(thisValue, recordTy);
969 LValue lv = cgf.emitLValueForField(thisLV, field);
970 assert(lv.isSimple());
971
972 assert(!cir::MissingFeatures::useEHCleanupForArray());
973 cgf.emitDestroy(lv.getAddress(), field->getType(), destroyer);
974 }
975 };
976 } // namespace
977
978 /// Emit all code that comes at the end of class's destructor. This is to call
979 /// destructors on members and base classes in reverse order of their
980 /// construction.
981 ///
982 /// For a deleting destructor, this also handles the case where a destroying
983 /// operator delete completely overrides the definition.
984 void CIRGenFunction::enterDtorCleanups(const CXXDestructorDecl *dd,
985 CXXDtorType dtorType) {
986 assert((!dd->isTrivial() || dd->hasAttr<DLLExportAttr>()) &&
987 "Should not emit dtor epilogue for non-exported trivial dtor!");
988
989 // The deleting-destructor phase just needs to call the appropriate
990 // operator delete that Sema picked up.
991 if (dtorType == Dtor_Deleting) {
992 assert(dd->getOperatorDelete() &&
993 "operator delete missing - EnterDtorCleanups");
994 if (cxxStructorImplicitParamValue) {
995 cgm.errorNYI(dd->getSourceRange(), "deleting destructor with vtt");
996 } else {
997 if (dd->getOperatorDelete()->isDestroyingOperatorDelete()) {
998 cgm.errorNYI(dd->getSourceRange(),
999 "deleting destructor with destroying operator delete");
1000 } else {
1001 ehStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1002 }
1003 }
1004 return;
1005 }
1006
1007 const CXXRecordDecl *classDecl = dd->getParent();
1008
1009 // Unions have no bases and do not call field destructors.
1010 if (classDecl->isUnion())
1011 return;
1012
1013 // The complete-destructor phase just destructs all the virtual bases.
1014 if (dtorType == Dtor_Complete) {
1015 assert(!cir::MissingFeatures::sanitizers());
1016
1017 // We push them in the forward order so that they'll be popped in
1018 // the reverse order.
1019 for (const CXXBaseSpecifier &base : classDecl->vbases()) {
1020 auto *baseClassDecl = base.getType()->castAsCXXRecordDecl();
1021
1022 if (baseClassDecl->hasTrivialDestructor()) {
1023 // Under SanitizeMemoryUseAfterDtor, poison the trivial base class
1024 // memory. For non-trival base classes the same is done in the class
1025 // destructor.
1026 assert(!cir::MissingFeatures::sanitizers());
1027 } else {
1028 ehStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, baseClassDecl,
1029 /*baseIsVirtual=*/true);
1030 }
1031 }
1032
1033 return;
1034 }
1035
1036 assert(dtorType == Dtor_Base);
1037 assert(!cir::MissingFeatures::sanitizers());
1038
1039 // Destroy non-virtual bases.
1040 for (const CXXBaseSpecifier &base : classDecl->bases()) {
1041 // Ignore virtual bases.
1042 if (base.isVirtual())
1043 continue;
1044
1045 CXXRecordDecl *baseClassDecl = base.getType()->getAsCXXRecordDecl();
1046
1047 if (baseClassDecl->hasTrivialDestructor())
1048 assert(!cir::MissingFeatures::sanitizers());
1049 else
1050 ehStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, baseClassDecl,
1051 /*baseIsVirtual=*/false);
1052 }
1053
1054 assert(!cir::MissingFeatures::sanitizers());
1055
1056 // Destroy direct fields.
1057 for (const FieldDecl *field : classDecl->fields()) {
1058 QualType type = field->getType();
1059 QualType::DestructionKind dtorKind = type.isDestructedType();
1060 if (!dtorKind)
1061 continue;
1062
1063 // Anonymous union members do not have their destructors called.
1064 const RecordType *rt = type->getAsUnionType();
1065 if (rt && rt->getDecl()->isAnonymousStructOrUnion())
1066 continue;
1067
1068 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1069 assert(!cir::MissingFeatures::useEHCleanupForArray());
1070 ehStack.pushCleanup<DestroyField>(cleanupKind, field,
1071 getDestroyer(dtorKind));
1072 }
1073 }
1074
1075 void CIRGenFunction::emitDelegatingCXXConstructorCall(
1076 const CXXConstructorDecl *ctor, const FunctionArgList &args) {
1077 assert(ctor->isDelegatingConstructor());
1078
1079 Address thisPtr = loadCXXThisAddress();
1080
1081 assert(!cir::MissingFeatures::objCGC());
1082 assert(!cir::MissingFeatures::sanitizers());
1083 AggValueSlot aggSlot = AggValueSlot::forAddr(
1084 thisPtr, Qualifiers(), AggValueSlot::IsDestructed,
1085 AggValueSlot::IsNotAliased, AggValueSlot::MayOverlap,
1086 AggValueSlot::IsNotZeroed);
1087
1088 emitAggExpr(ctor->init_begin()[0]->getInit(), aggSlot);
1089
1090 const CXXRecordDecl *classDecl = ctor->getParent();
1091 if (cgm.getLangOpts().Exceptions && !classDecl->hasTrivialDestructor()) {
1092 cgm.errorNYI(ctor->getSourceRange(),
1093 "emitDelegatingCXXConstructorCall: exception");
1094 return;
1095 }
1096 }
1097
1098 void CIRGenFunction::emitCXXDestructorCall(const CXXDestructorDecl *dd,
1099 CXXDtorType type,
1100 bool forVirtualBase, bool delegating,
1101 Address thisAddr, QualType thisTy) {
1102 cgm.getCXXABI().emitDestructorCall(*this, dd, type, forVirtualBase,
1103 delegating, thisAddr, thisTy);
1104 }
1105
1106 mlir::Value CIRGenFunction::getVTTParameter(GlobalDecl gd, bool forVirtualBase,
1107 bool delegating) {
1108 if (!cgm.getCXXABI().needsVTTParameter(gd))
1109 return nullptr;
1110
1111 const CXXRecordDecl *rd = cast<CXXMethodDecl>(curCodeDecl)->getParent();
1112 const CXXRecordDecl *base = cast<CXXMethodDecl>(gd.getDecl())->getParent();
1113
1114 uint64_t subVTTIndex;
1115
1116 if (delegating) {
1117 // If this is a delegating constructor call, just load the VTT.
1118 return loadCXXVTT();
1119 } else if (rd == base) {
1120 // If the record matches the base, this is the complete ctor/dtor
1121 // variant calling the base variant in a class with virtual bases.
1122 assert(!cgm.getCXXABI().needsVTTParameter(curGD) &&
1123 "doing no-op VTT offset in base dtor/ctor?");
1124 assert(!forVirtualBase && "Can't have same class as virtual base!");
1125 subVTTIndex = 0;
1126 } else {
1127 const ASTRecordLayout &layout = getContext().getASTRecordLayout(rd);
1128 CharUnits baseOffset = forVirtualBase ? layout.getVBaseClassOffset(base)
1129 : layout.getBaseClassOffset(base);
1130
1131 subVTTIndex =
1132 cgm.getVTables().getSubVTTIndex(rd, BaseSubobject(base, baseOffset));
1133 assert(subVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
1134 }
1135
1136 mlir::Location loc = cgm.getLoc(rd->getBeginLoc());
1137 if (cgm.getCXXABI().needsVTTParameter(curGD)) {
1138 // A VTT parameter was passed to the constructor, use it.
1139 mlir::Value vtt = loadCXXVTT();
1140 return builder.createVTTAddrPoint(loc, vtt.getType(), vtt, subVTTIndex);
1141 } else {
1142 // We're the complete constructor, so get the VTT by name.
1143 cir::GlobalOp vtt = cgm.getVTables().getAddrOfVTT(rd);
1144 return builder.createVTTAddrPoint(
1145 loc, builder.getPointerTo(cgm.voidPtrTy),
1146 mlir::FlatSymbolRefAttr::get(vtt.getSymNameAttr()), subVTTIndex);
1147 }
1148 }
1149
1150 Address CIRGenFunction::getAddressOfDerivedClass(
1151 mlir::Location loc, Address baseAddr, const CXXRecordDecl *derived,
1152 llvm::iterator_range<CastExpr::path_const_iterator> path,
1153 bool nullCheckValue) {
1154 assert(!path.empty() && "Base path should not be empty!");
1155
1156 QualType derivedTy = getContext().getCanonicalTagType(derived);
1157 mlir::Type derivedValueTy = convertType(derivedTy);
1158 CharUnits nonVirtualOffset =
1159 cgm.computeNonVirtualBaseClassOffset(derived, path);
1160
1161 // Note that in OG, no offset (nonVirtualOffset.getQuantity() == 0) means it
1162 // just gives the address back. In CIR a `cir.derived_class` is created and
1163 // made into a nop later on during lowering.
1164 return builder.createDerivedClassAddr(loc, baseAddr, derivedValueTy,
1165 nonVirtualOffset.getQuantity(),
1166 /*assumeNotNull=*/!nullCheckValue);
1167 }
1168
1169 Address CIRGenFunction::getAddressOfBaseClass(
1170 Address value, const CXXRecordDecl *derived,
1171 llvm::iterator_range<CastExpr::path_const_iterator> path,
1172 bool nullCheckValue, SourceLocation loc) {
1173 assert(!path.empty() && "Base path should not be empty!");
1174
1175 CastExpr::path_const_iterator start = path.begin();
1176 const CXXRecordDecl *vBase = nullptr;
1177
1178 if ((*path.begin())->isVirtual()) {
1179 vBase = (*start)->getType()->castAsCXXRecordDecl();
1180 ++start;
1181 }
1182
1183 // Compute the static offset of the ultimate destination within its
1184 // allocating subobject (the virtual base, if there is one, or else
1185 // the "complete" object that we see).
1186 CharUnits nonVirtualOffset = cgm.computeNonVirtualBaseClassOffset(
1187 vBase ? vBase : derived, {start, path.end()});
1188
1189 // If there's a virtual step, we can sometimes "devirtualize" it.
1190 // For now, that's limited to when the derived type is final.
1191 // TODO: "devirtualize" this for accesses to known-complete objects.
1192 if (vBase && derived->hasAttr<FinalAttr>()) {
1193 const ASTRecordLayout &layout = getContext().getASTRecordLayout(derived);
1194 CharUnits vBaseOffset = layout.getVBaseClassOffset(vBase);
1195 nonVirtualOffset += vBaseOffset;
1196 vBase = nullptr; // we no longer have a virtual step
1197 }
1198
1199 // Get the base pointer type.
1200 mlir::Type baseValueTy = convertType((path.end()[-1])->getType());
1201 assert(!cir::MissingFeatures::addressSpace());
1202
1203 // If there is no virtual base, use cir.base_class_addr. It takes care of
1204 // the adjustment and the null pointer check.
1205 if (nonVirtualOffset.isZero() && !vBase) {
1206 assert(!cir::MissingFeatures::sanitizers());
1207 return builder.createBaseClassAddr(getLoc(loc), value, baseValueTy, 0,
1208 /*assumeNotNull=*/true);
1209 }
1210
1211 assert(!cir::MissingFeatures::sanitizers());
1212
1213 // Compute the virtual offset.
1214 mlir::Value virtualOffset = nullptr;
1215 if (vBase) {
1216 virtualOffset = cgm.getCXXABI().getVirtualBaseClassOffset(
1217 getLoc(loc), *this, value, derived, vBase);
1218 }
1219
1220 // Apply both offsets.
1221 value = applyNonVirtualAndVirtualOffset(
1222 getLoc(loc), *this, value, nonVirtualOffset, virtualOffset, derived,
1223 vBase, baseValueTy, not nullCheckValue);
1224
1225 // Cast to the destination type.
1226 value = value.withElementType(builder, baseValueTy);
1227
1228 return value;
1229 }
1230
1231 // TODO(cir): this can be shared with LLVM codegen.
1232 bool CIRGenFunction::shouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *rd) {
1233 assert(!cir::MissingFeatures::hiddenVisibility());
1234 if (!cgm.getCodeGenOpts().WholeProgramVTables)
1235 return false;
1236
1237 if (cgm.getCodeGenOpts().VirtualFunctionElimination)
1238 return true;
1239
1240 assert(!cir::MissingFeatures::sanitizers());
1241
1242 return false;
1243 }
1244
1245 mlir::Value CIRGenFunction::getVTablePtr(mlir::Location loc, Address thisAddr,
1246 const CXXRecordDecl *rd) {
1247 auto vtablePtr =
1248 cir::VTableGetVPtrOp::create(builder, loc, thisAddr.getPointer());
1249 Address vtablePtrAddr = Address(vtablePtr, thisAddr.getAlignment());
1250
1251 auto vtable = builder.createLoad(loc, vtablePtrAddr);
1252 assert(!cir::MissingFeatures::opTBAA());
1253
1254 if (cgm.getCodeGenOpts().OptimizationLevel > 0 &&
1255 cgm.getCodeGenOpts().StrictVTablePointers) {
1256 assert(!cir::MissingFeatures::createInvariantGroup());
1257 }
1258
1259 return vtable;
1260 }
1261
1262 void CIRGenFunction::emitCXXConstructorCall(const clang::CXXConstructorDecl *d,
1263 clang::CXXCtorType type,
1264 bool forVirtualBase,
1265 bool delegating,
1266 AggValueSlot thisAVS,
1267 const clang::CXXConstructExpr *e) {
1268 CallArgList args;
1269 Address thisAddr = thisAVS.getAddress();
1270 QualType thisType = d->getThisType();
1271 mlir::Value thisPtr = thisAddr.getPointer();
1272
1273 assert(!cir::MissingFeatures::addressSpace());
1274
1275 args.add(RValue::get(thisPtr), thisType);
1276
1277 // In LLVM Codegen: If this is a trivial constructor, just emit what's needed.
1278 // If this is a union copy constructor, we must emit a memcpy, because the AST
1279 // does not model that copy.
1280 assert(!cir::MissingFeatures::isMemcpyEquivalentSpecialMember());
1281
1282 const FunctionProtoType *fpt = d->getType()->castAs<FunctionProtoType>();
1283
1284 assert(!cir::MissingFeatures::opCallArgEvaluationOrder());
1285
1286 emitCallArgs(args, fpt, e->arguments(), e->getConstructor(),
1287 /*ParamsToSkip=*/0);
1288
1289 assert(!cir::MissingFeatures::sanitizers());
1290 emitCXXConstructorCall(d, type, forVirtualBase, delegating, thisAddr, args,
1291 e->getExprLoc());
1292 }
1293
1294 void CIRGenFunction::emitCXXConstructorCall(
1295 const CXXConstructorDecl *d, CXXCtorType type, bool forVirtualBase,
1296 bool delegating, Address thisAddr, CallArgList &args, SourceLocation loc) {
1297
1298 const CXXRecordDecl *crd = d->getParent();
1299
1300 // If this is a call to a trivial default constructor:
1301 // In LLVM: do nothing.
1302 // In CIR: emit as a regular call, other later passes should lower the
1303 // ctor call into trivial initialization.
1304 assert(!cir::MissingFeatures::isTrivialCtorOrDtor());
1305
1306 assert(!cir::MissingFeatures::isMemcpyEquivalentSpecialMember());
1307
1308 bool passPrototypeArgs = true;
1309
1310 // Check whether we can actually emit the constructor before trying to do so.
1311 if (d->getInheritedConstructor()) {
1312 cgm.errorNYI(d->getSourceRange(),
1313 "emitCXXConstructorCall: inherited constructor");
1314 return;
1315 }
1316
1317 // Insert any ABI-specific implicit constructor arguments.
1318 CIRGenCXXABI::AddedStructorArgCounts extraArgs =
1319 cgm.getCXXABI().addImplicitConstructorArgs(*this, d, type, forVirtualBase,
1320 delegating, args);
1321
1322 // Emit the call.
1323 auto calleePtr = cgm.getAddrOfCXXStructor(GlobalDecl(d, type));
1324 const CIRGenFunctionInfo &info = cgm.getTypes().arrangeCXXConstructorCall(
1325 args, d, type, extraArgs.prefix, extraArgs.suffix, passPrototypeArgs);
1326 CIRGenCallee callee = CIRGenCallee::forDirect(calleePtr, GlobalDecl(d, type));
1327 cir::CIRCallOpInterface c;
1328 emitCall(info, callee, ReturnValueSlot(), args, &c, getLoc(loc));
1329
1330 if (cgm.getCodeGenOpts().OptimizationLevel != 0 && !crd->isDynamicClass() &&
1331 type != Ctor_Base && cgm.getCodeGenOpts().StrictVTablePointers)
1332 cgm.errorNYI(d->getSourceRange(), "vtable assumption loads");
1333 }