1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "CGBlocks.h" |
14 | #include "CGCXXABI.h" |
15 | #include "CGDebugInfo.h" |
16 | #include "CGRecordLayout.h" |
17 | #include "CodeGenFunction.h" |
18 | #include "TargetInfo.h" |
19 | #include "clang/AST/CXXInheritance.h" |
20 | #include "clang/AST/DeclTemplate.h" |
21 | #include "clang/AST/EvaluatedExprVisitor.h" |
22 | #include "clang/AST/RecordLayout.h" |
23 | #include "clang/AST/StmtCXX.h" |
24 | #include "clang/Basic/CodeGenOptions.h" |
25 | #include "clang/Basic/TargetBuiltins.h" |
26 | #include "clang/CodeGen/CGFunctionInfo.h" |
27 | #include "llvm/IR/Intrinsics.h" |
28 | #include "llvm/IR/Metadata.h" |
29 | #include "llvm/Transforms/Utils/SanitizerStats.h" |
30 | |
31 | using namespace clang; |
32 | using namespace CodeGen; |
33 | |
34 | |
35 | |
36 | CharUnits CodeGenModule::getClassPointerAlignment(const CXXRecordDecl *RD) { |
37 | if (!RD->isCompleteDefinition()) |
38 | return CharUnits::One(); |
39 | |
40 | auto &layout = getContext().getASTRecordLayout(RD); |
41 | |
42 | |
43 | |
44 | if (RD->hasAttr<FinalAttr>()) { |
45 | return layout.getAlignment(); |
46 | |
47 | |
48 | } else { |
49 | return layout.getNonVirtualAlignment(); |
50 | } |
51 | } |
52 | |
53 | |
54 | |
55 | CharUnits CodeGenModule::getVBaseAlignment(CharUnits actualDerivedAlign, |
56 | const CXXRecordDecl *derivedClass, |
57 | const CXXRecordDecl *vbaseClass) { |
58 | |
59 | |
60 | |
61 | isCompleteDefinition()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 61, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(vbaseClass->isCompleteDefinition()); |
62 | auto &baseLayout = getContext().getASTRecordLayout(vbaseClass); |
63 | CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment(); |
64 | |
65 | return getDynamicOffsetAlignment(actualDerivedAlign, derivedClass, |
66 | expectedVBaseAlign); |
67 | } |
68 | |
69 | CharUnits |
70 | CodeGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign, |
71 | const CXXRecordDecl *baseDecl, |
72 | CharUnits expectedTargetAlign) { |
73 | |
74 | |
75 | if (!baseDecl->isCompleteDefinition()) |
76 | return std::min(actualBaseAlign, expectedTargetAlign); |
77 | |
78 | auto &baseLayout = getContext().getASTRecordLayout(baseDecl); |
79 | CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment(); |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | if (actualBaseAlign >= expectedBaseAlign) { |
100 | return expectedTargetAlign; |
101 | } |
102 | |
103 | |
104 | |
105 | |
106 | return std::min(actualBaseAlign, expectedTargetAlign); |
107 | } |
108 | |
109 | Address CodeGenFunction::LoadCXXThisAddress() { |
110 | (0) . __assert_fail ("CurFuncDecl && \"loading 'this' without a func declaration?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 110, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurFuncDecl && "loading 'this' without a func declaration?"); |
111 | (CurFuncDecl)", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 111, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<CXXMethodDecl>(CurFuncDecl)); |
112 | |
113 | |
114 | if (CXXThisAlignment.isZero()) { |
115 | |
116 | |
117 | |
118 | auto RD = cast<CXXMethodDecl>(CurFuncDecl)->getParent(); |
119 | CXXThisAlignment = CGM.getClassPointerAlignment(RD); |
120 | } |
121 | |
122 | return Address(LoadCXXThis(), CXXThisAlignment); |
123 | } |
124 | |
125 | |
126 | |
127 | |
128 | Address |
129 | CodeGenFunction::EmitCXXMemberDataPointerAddress(const Expr *E, Address base, |
130 | llvm::Value *memberPtr, |
131 | const MemberPointerType *memberPtrType, |
132 | LValueBaseInfo *BaseInfo, |
133 | TBAAAccessInfo *TBAAInfo) { |
134 | |
135 | llvm::Value *ptr = |
136 | CGM.getCXXABI().EmitMemberDataPointerAddress(*this, E, base, |
137 | memberPtr, memberPtrType); |
138 | |
139 | QualType memberType = memberPtrType->getPointeeType(); |
140 | CharUnits memberAlign = getNaturalTypeAlignment(memberType, BaseInfo, |
141 | TBAAInfo); |
142 | memberAlign = |
143 | CGM.getDynamicOffsetAlignment(base.getAlignment(), |
144 | memberPtrType->getClass()->getAsCXXRecordDecl(), |
145 | memberAlign); |
146 | return Address(ptr, memberAlign); |
147 | } |
148 | |
149 | CharUnits CodeGenModule::computeNonVirtualBaseClassOffset( |
150 | const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, |
151 | CastExpr::path_const_iterator End) { |
152 | CharUnits Offset = CharUnits::Zero(); |
153 | |
154 | const ASTContext &Context = getContext(); |
155 | const CXXRecordDecl *RD = DerivedClass; |
156 | |
157 | for (CastExpr::path_const_iterator I = Start; I != End; ++I) { |
158 | const CXXBaseSpecifier *Base = *I; |
159 | (0) . __assert_fail ("!Base->isVirtual() && \"Should not see virtual bases here!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 159, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Base->isVirtual() && "Should not see virtual bases here!"); |
160 | |
161 | |
162 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
163 | |
164 | const CXXRecordDecl *BaseDecl = |
165 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
166 | |
167 | |
168 | Offset += Layout.getBaseClassOffset(BaseDecl); |
169 | |
170 | RD = BaseDecl; |
171 | } |
172 | |
173 | return Offset; |
174 | } |
175 | |
176 | llvm::Constant * |
177 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, |
178 | CastExpr::path_const_iterator PathBegin, |
179 | CastExpr::path_const_iterator PathEnd) { |
180 | (0) . __assert_fail ("PathBegin != PathEnd && \"Base path should not be empty!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 180, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PathBegin != PathEnd && "Base path should not be empty!"); |
181 | |
182 | CharUnits Offset = |
183 | computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd); |
184 | if (Offset.isZero()) |
185 | return nullptr; |
186 | |
187 | llvm::Type *PtrDiffTy = |
188 | Types.ConvertType(getContext().getPointerDiffType()); |
189 | |
190 | return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity()); |
191 | } |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | Address |
199 | CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(Address This, |
200 | const CXXRecordDecl *Derived, |
201 | const CXXRecordDecl *Base, |
202 | bool BaseIsVirtual) { |
203 | |
204 | assert(This.getElementType() == ConvertType(Derived)); |
205 | |
206 | |
207 | CharUnits Offset; |
208 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
209 | if (BaseIsVirtual) |
210 | Offset = Layout.getVBaseClassOffset(Base); |
211 | else |
212 | Offset = Layout.getBaseClassOffset(Base); |
213 | |
214 | |
215 | |
216 | Address V = This; |
217 | if (!Offset.isZero()) { |
218 | V = Builder.CreateElementBitCast(V, Int8Ty); |
219 | V = Builder.CreateConstInBoundsByteGEP(V, Offset); |
220 | } |
221 | V = Builder.CreateElementBitCast(V, ConvertType(Base)); |
222 | |
223 | return V; |
224 | } |
225 | |
226 | static Address |
227 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, Address addr, |
228 | CharUnits nonVirtualOffset, |
229 | llvm::Value *virtualOffset, |
230 | const CXXRecordDecl *derivedClass, |
231 | const CXXRecordDecl *nearestVBase) { |
232 | |
233 | assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr); |
234 | |
235 | |
236 | llvm::Value *baseOffset; |
237 | if (!nonVirtualOffset.isZero()) { |
238 | baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy, |
239 | nonVirtualOffset.getQuantity()); |
240 | if (virtualOffset) { |
241 | baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset); |
242 | } |
243 | } else { |
244 | baseOffset = virtualOffset; |
245 | } |
246 | |
247 | |
248 | llvm::Value *ptr = addr.getPointer(); |
249 | ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); |
250 | ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr"); |
251 | |
252 | |
253 | |
254 | CharUnits alignment; |
255 | if (virtualOffset) { |
256 | (0) . __assert_fail ("nearestVBase && \"virtual offset without vbase?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 256, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(nearestVBase && "virtual offset without vbase?"); |
257 | alignment = CGF.CGM.getVBaseAlignment(addr.getAlignment(), |
258 | derivedClass, nearestVBase); |
259 | } else { |
260 | alignment = addr.getAlignment(); |
261 | } |
262 | alignment = alignment.alignmentAtOffset(nonVirtualOffset); |
263 | |
264 | return Address(ptr, alignment); |
265 | } |
266 | |
267 | Address CodeGenFunction::GetAddressOfBaseClass( |
268 | Address Value, const CXXRecordDecl *Derived, |
269 | CastExpr::path_const_iterator PathBegin, |
270 | CastExpr::path_const_iterator PathEnd, bool NullCheckValue, |
271 | SourceLocation Loc) { |
272 | (0) . __assert_fail ("PathBegin != PathEnd && \"Base path should not be empty!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 272, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PathBegin != PathEnd && "Base path should not be empty!"); |
273 | |
274 | CastExpr::path_const_iterator Start = PathBegin; |
275 | const CXXRecordDecl *VBase = nullptr; |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | if ((*Start)->isVirtual()) { |
282 | VBase = |
283 | cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl()); |
284 | ++Start; |
285 | } |
286 | |
287 | |
288 | |
289 | |
290 | CharUnits NonVirtualOffset = CGM.computeNonVirtualBaseClassOffset( |
291 | VBase ? VBase : Derived, Start, PathEnd); |
292 | |
293 | |
294 | |
295 | |
296 | if (VBase && Derived->hasAttr<FinalAttr>()) { |
297 | const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived); |
298 | CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase); |
299 | NonVirtualOffset += vBaseOffset; |
300 | VBase = nullptr; |
301 | } |
302 | |
303 | |
304 | llvm::Type *BasePtrTy = |
305 | ConvertType((PathEnd[-1])->getType()) |
306 | ->getPointerTo(Value.getType()->getPointerAddressSpace()); |
307 | |
308 | QualType DerivedTy = getContext().getRecordType(Derived); |
309 | CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived); |
310 | |
311 | |
312 | |
313 | if (NonVirtualOffset.isZero() && !VBase) { |
314 | if (sanitizePerformTypeCheck()) { |
315 | SanitizerSet SkippedChecks; |
316 | SkippedChecks.set(SanitizerKind::Null, !NullCheckValue); |
317 | EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(), |
318 | DerivedTy, DerivedAlign, SkippedChecks); |
319 | } |
320 | return Builder.CreateBitCast(Value, BasePtrTy); |
321 | } |
322 | |
323 | llvm::BasicBlock *origBB = nullptr; |
324 | llvm::BasicBlock *endBB = nullptr; |
325 | |
326 | |
327 | |
328 | if (NullCheckValue) { |
329 | origBB = Builder.GetInsertBlock(); |
330 | llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull"); |
331 | endBB = createBasicBlock("cast.end"); |
332 | |
333 | llvm::Value *isNull = Builder.CreateIsNull(Value.getPointer()); |
334 | Builder.CreateCondBr(isNull, endBB, notNullBB); |
335 | EmitBlock(notNullBB); |
336 | } |
337 | |
338 | if (sanitizePerformTypeCheck()) { |
339 | SanitizerSet SkippedChecks; |
340 | SkippedChecks.set(SanitizerKind::Null, true); |
341 | EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc, |
342 | Value.getPointer(), DerivedTy, DerivedAlign, SkippedChecks); |
343 | } |
344 | |
345 | |
346 | llvm::Value *VirtualOffset = nullptr; |
347 | if (VBase) { |
348 | VirtualOffset = |
349 | CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase); |
350 | } |
351 | |
352 | |
353 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset, |
354 | VirtualOffset, Derived, VBase); |
355 | |
356 | |
357 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
358 | |
359 | |
360 | if (NullCheckValue) { |
361 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
362 | Builder.CreateBr(endBB); |
363 | EmitBlock(endBB); |
364 | |
365 | llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result"); |
366 | PHI->addIncoming(Value.getPointer(), notNullBB); |
367 | PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB); |
368 | Value = Address(PHI, Value.getAlignment()); |
369 | } |
370 | |
371 | return Value; |
372 | } |
373 | |
374 | Address |
375 | CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr, |
376 | const CXXRecordDecl *Derived, |
377 | CastExpr::path_const_iterator PathBegin, |
378 | CastExpr::path_const_iterator PathEnd, |
379 | bool NullCheckValue) { |
380 | (0) . __assert_fail ("PathBegin != PathEnd && \"Base path should not be empty!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 380, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PathBegin != PathEnd && "Base path should not be empty!"); |
381 | |
382 | QualType DerivedTy = |
383 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
384 | llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
385 | |
386 | llvm::Value *NonVirtualOffset = |
387 | CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); |
388 | |
389 | if (!NonVirtualOffset) { |
390 | |
391 | return Builder.CreateBitCast(BaseAddr, DerivedPtrTy); |
392 | } |
393 | |
394 | llvm::BasicBlock *CastNull = nullptr; |
395 | llvm::BasicBlock *CastNotNull = nullptr; |
396 | llvm::BasicBlock *CastEnd = nullptr; |
397 | |
398 | if (NullCheckValue) { |
399 | CastNull = createBasicBlock("cast.null"); |
400 | CastNotNull = createBasicBlock("cast.notnull"); |
401 | CastEnd = createBasicBlock("cast.end"); |
402 | |
403 | llvm::Value *IsNull = Builder.CreateIsNull(BaseAddr.getPointer()); |
404 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
405 | EmitBlock(CastNotNull); |
406 | } |
407 | |
408 | |
409 | llvm::Value *Value = Builder.CreateBitCast(BaseAddr.getPointer(), Int8PtrTy); |
410 | Value = Builder.CreateInBoundsGEP(Value, Builder.CreateNeg(NonVirtualOffset), |
411 | "sub.ptr"); |
412 | |
413 | |
414 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
415 | |
416 | |
417 | if (NullCheckValue) { |
418 | Builder.CreateBr(CastEnd); |
419 | EmitBlock(CastNull); |
420 | Builder.CreateBr(CastEnd); |
421 | EmitBlock(CastEnd); |
422 | |
423 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
424 | PHI->addIncoming(Value, CastNotNull); |
425 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
426 | Value = PHI; |
427 | } |
428 | |
429 | return Address(Value, CGM.getClassPointerAlignment(Derived)); |
430 | } |
431 | |
432 | llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD, |
433 | bool ForVirtualBase, |
434 | bool Delegating) { |
435 | if (!CGM.getCXXABI().NeedsVTTParameter(GD)) { |
436 | |
437 | return nullptr; |
438 | } |
439 | |
440 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent(); |
441 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
442 | |
443 | llvm::Value *VTT; |
444 | |
445 | uint64_t SubVTTIndex; |
446 | |
447 | if (Delegating) { |
448 | |
449 | return LoadCXXVTT(); |
450 | } else if (RD == Base) { |
451 | |
452 | |
453 | (0) . __assert_fail ("!CGM.getCXXABI().NeedsVTTParameter(CurGD) && \"doing no-op VTT offset in base dtor/ctor?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) && |
454 | (0) . __assert_fail ("!CGM.getCXXABI().NeedsVTTParameter(CurGD) && \"doing no-op VTT offset in base dtor/ctor?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "doing no-op VTT offset in base dtor/ctor?"); |
455 | (0) . __assert_fail ("!ForVirtualBase && \"Can't have same class as virtual base!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 455, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
456 | SubVTTIndex = 0; |
457 | } else { |
458 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
459 | CharUnits BaseOffset = ForVirtualBase ? |
460 | Layout.getVBaseClassOffset(Base) : |
461 | Layout.getBaseClassOffset(Base); |
462 | |
463 | SubVTTIndex = |
464 | CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
465 | (0) . __assert_fail ("SubVTTIndex != 0 && \"Sub-VTT index must be greater than zero!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
466 | } |
467 | |
468 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
469 | |
470 | VTT = LoadCXXVTT(); |
471 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
472 | } else { |
473 | |
474 | VTT = CGM.getVTables().GetAddrOfVTT(RD); |
475 | VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
476 | } |
477 | |
478 | return VTT; |
479 | } |
480 | |
481 | namespace { |
482 | |
483 | struct CallBaseDtor final : EHScopeStack::Cleanup { |
484 | const CXXRecordDecl *BaseClass; |
485 | bool BaseIsVirtual; |
486 | CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) |
487 | : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} |
488 | |
489 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
490 | const CXXRecordDecl *DerivedClass = |
491 | cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent(); |
492 | |
493 | const CXXDestructorDecl *D = BaseClass->getDestructor(); |
494 | Address Addr = |
495 | CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThisAddress(), |
496 | DerivedClass, BaseClass, |
497 | BaseIsVirtual); |
498 | CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, |
499 | , Addr); |
500 | } |
501 | }; |
502 | |
503 | |
504 | |
505 | struct DynamicThisUseChecker : ConstEvaluatedExprVisitor<DynamicThisUseChecker> { |
506 | typedef ConstEvaluatedExprVisitor<DynamicThisUseChecker> super; |
507 | |
508 | bool UsesThis; |
509 | |
510 | DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {} |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; } |
518 | }; |
519 | } |
520 | |
521 | static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { |
522 | DynamicThisUseChecker Checker(C); |
523 | Checker.Visit(Init); |
524 | return Checker.UsesThis; |
525 | } |
526 | |
527 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
528 | const CXXRecordDecl *ClassDecl, |
529 | CXXCtorInitializer *BaseInit) { |
530 | (0) . __assert_fail ("BaseInit->isBaseInitializer() && \"Must have base initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 531, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BaseInit->isBaseInitializer() && |
531 | (0) . __assert_fail ("BaseInit->isBaseInitializer() && \"Must have base initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 531, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Must have base initializer!"); |
532 | |
533 | Address ThisPtr = CGF.LoadCXXThisAddress(); |
534 | |
535 | const Type *BaseType = BaseInit->getBaseClass(); |
536 | CXXRecordDecl *BaseClassDecl = |
537 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
538 | |
539 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
540 | |
541 | |
542 | |
543 | |
544 | if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) |
545 | CGF.InitializeVTablePointers(ClassDecl); |
546 | |
547 | |
548 | |
549 | Address V = |
550 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
551 | BaseClassDecl, |
552 | isBaseVirtual); |
553 | AggValueSlot AggSlot = |
554 | AggValueSlot::forAddr( |
555 | V, Qualifiers(), |
556 | AggValueSlot::IsDestructed, |
557 | AggValueSlot::DoesNotNeedGCBarriers, |
558 | AggValueSlot::IsNotAliased, |
559 | CGF.overlapForBaseInit(ClassDecl, BaseClassDecl, isBaseVirtual)); |
560 | |
561 | CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); |
562 | |
563 | if (CGF.CGM.getLangOpts().Exceptions && |
564 | !BaseClassDecl->hasTrivialDestructor()) |
565 | CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl, |
566 | isBaseVirtual); |
567 | } |
568 | |
569 | static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D) { |
570 | auto *CD = dyn_cast<CXXConstructorDecl>(D); |
571 | if (!(CD && CD->isCopyOrMoveConstructor()) && |
572 | !D->isCopyAssignmentOperator() && !D->isMoveAssignmentOperator()) |
573 | return false; |
574 | |
575 | |
576 | if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding()) |
577 | return true; |
578 | |
579 | |
580 | if (D->getParent()->isUnion() && D->isDefaulted()) |
581 | return true; |
582 | |
583 | return false; |
584 | } |
585 | |
586 | static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF, |
587 | CXXCtorInitializer *MemberInit, |
588 | LValue &LHS) { |
589 | FieldDecl *Field = MemberInit->getAnyMember(); |
590 | if (MemberInit->isIndirectMemberInitializer()) { |
591 | |
592 | IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember(); |
593 | for (const auto *I : IndirectField->chain()) |
594 | LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I)); |
595 | } else { |
596 | LHS = CGF.EmitLValueForFieldInitialization(LHS, Field); |
597 | } |
598 | } |
599 | |
600 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
601 | const CXXRecordDecl *ClassDecl, |
602 | CXXCtorInitializer *MemberInit, |
603 | const CXXConstructorDecl *Constructor, |
604 | FunctionArgList &Args) { |
605 | ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation()); |
606 | (0) . __assert_fail ("MemberInit->isAnyMemberInitializer() && \"Must have member initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 607, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MemberInit->isAnyMemberInitializer() && |
607 | (0) . __assert_fail ("MemberInit->isAnyMemberInitializer() && \"Must have member initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 607, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Must have member initializer!"); |
608 | (0) . __assert_fail ("MemberInit->getInit() && \"Must have initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 608, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MemberInit->getInit() && "Must have initializer!"); |
609 | |
610 | |
611 | FieldDecl *Field = MemberInit->getAnyMember(); |
612 | QualType FieldType = Field->getType(); |
613 | |
614 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
615 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
616 | LValue LHS; |
617 | |
618 | |
619 | |
620 | if (CGF.CurGD.getCtorType() == Ctor_Base) |
621 | LHS = CGF.MakeNaturalAlignPointeeAddrLValue(ThisPtr, RecordTy); |
622 | else |
623 | LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
624 | |
625 | EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS); |
626 | |
627 | |
628 | |
629 | |
630 | |
631 | |
632 | const ConstantArrayType *Array |
633 | = CGF.getContext().getAsConstantArrayType(FieldType); |
634 | if (Array && Constructor->isDefaulted() && |
635 | Constructor->isCopyOrMoveConstructor()) { |
636 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
637 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
638 | if (BaseElementTy.isPODType(CGF.getContext()) || |
639 | (CE && isMemcpyEquivalentSpecialMember(CE->getConstructor()))) { |
640 | unsigned SrcArgIndex = |
641 | CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args); |
642 | llvm::Value *SrcPtr |
643 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); |
644 | LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
645 | LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field); |
646 | |
647 | |
648 | CGF.EmitAggregateCopy(LHS, Src, FieldType, CGF.overlapForFieldInit(Field), |
649 | LHS.isVolatileQualified()); |
650 | |
651 | |
652 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
653 | if (CGF.needsEHCleanup(dtorKind)) |
654 | CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
655 | return; |
656 | } |
657 | } |
658 | |
659 | CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit()); |
660 | } |
661 | |
662 | void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, LValue LHS, |
663 | Expr *Init) { |
664 | QualType FieldType = Field->getType(); |
665 | switch (getEvaluationKind(FieldType)) { |
666 | case TEK_Scalar: |
667 | if (LHS.isSimple()) { |
668 | EmitExprAsInit(Init, Field, LHS, false); |
669 | } else { |
670 | RValue RHS = RValue::get(EmitScalarExpr(Init)); |
671 | EmitStoreThroughLValue(RHS, LHS); |
672 | } |
673 | break; |
674 | case TEK_Complex: |
675 | EmitComplexExprIntoLValue(Init, LHS, true); |
676 | break; |
677 | case TEK_Aggregate: { |
678 | AggValueSlot Slot = |
679 | AggValueSlot::forLValue( |
680 | LHS, |
681 | AggValueSlot::IsDestructed, |
682 | AggValueSlot::DoesNotNeedGCBarriers, |
683 | AggValueSlot::IsNotAliased, |
684 | overlapForFieldInit(Field), |
685 | AggValueSlot::IsNotZeroed, |
686 | |
687 | AggValueSlot::IsSanitizerChecked); |
688 | EmitAggExpr(Init, Slot); |
689 | break; |
690 | } |
691 | } |
692 | |
693 | |
694 | |
695 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
696 | if (needsEHCleanup(dtorKind)) |
697 | pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
698 | } |
699 | |
700 | |
701 | |
702 | |
703 | |
704 | bool CodeGenFunction::IsConstructorDelegationValid( |
705 | const CXXConstructorDecl *Ctor) { |
706 | |
707 | |
708 | |
709 | |
710 | |
711 | |
712 | |
713 | |
714 | |
715 | |
716 | |
717 | |
718 | |
719 | if (Ctor->getParent()->getNumVBases()) { |
720 | |
721 | |
722 | |
723 | |
724 | |
725 | |
726 | |
727 | |
728 | |
729 | |
730 | |
731 | |
732 | |
733 | |
734 | return false; |
735 | } |
736 | |
737 | |
738 | |
739 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
740 | return false; |
741 | |
742 | |
743 | if (Ctor->isDelegatingConstructor()) |
744 | return false; |
745 | |
746 | return true; |
747 | } |
748 | |
749 | |
750 | |
751 | |
752 | void CodeGenFunction::EmitAsanPrologueOrEpilogue(bool Prologue) { |
753 | ASTContext &Context = getContext(); |
754 | const CXXRecordDecl *ClassDecl = |
755 | Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent() |
756 | : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent(); |
757 | if (!ClassDecl->mayInsertExtraPadding()) return; |
758 | |
759 | struct SizeAndOffset { |
760 | uint64_t Size; |
761 | uint64_t Offset; |
762 | }; |
763 | |
764 | unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits(); |
765 | const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl); |
766 | |
767 | |
768 | SmallVector<SizeAndOffset, 16> SSV(Info.getFieldCount()); |
769 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) |
770 | SSV[i].Offset = |
771 | Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity(); |
772 | |
773 | size_t NumFields = 0; |
774 | for (const auto *Field : ClassDecl->fields()) { |
775 | const FieldDecl *D = Field; |
776 | std::pair<CharUnits, CharUnits> FieldInfo = |
777 | Context.getTypeInfoInChars(D->getType()); |
778 | CharUnits FieldSize = FieldInfo.first; |
779 | assert(NumFields < SSV.size()); |
780 | SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity(); |
781 | NumFields++; |
782 | } |
783 | assert(NumFields == SSV.size()); |
784 | if (SSV.size() <= 1) return; |
785 | |
786 | |
787 | |
788 | llvm::Type *Args[2] = {IntPtrTy, IntPtrTy}; |
789 | llvm::FunctionType *FTy = |
790 | llvm::FunctionType::get(CGM.VoidTy, Args, false); |
791 | llvm::FunctionCallee F = CGM.CreateRuntimeFunction( |
792 | FTy, Prologue ? "__asan_poison_intra_object_redzone" |
793 | : "__asan_unpoison_intra_object_redzone"); |
794 | |
795 | llvm::Value *ThisPtr = LoadCXXThis(); |
796 | ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy); |
797 | uint64_t TypeSize = Info.getNonVirtualSize().getQuantity(); |
798 | |
799 | |
800 | for (size_t i = 0; i < SSV.size(); i++) { |
801 | uint64_t AsanAlignment = 8; |
802 | uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset; |
803 | uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size; |
804 | uint64_t EndOffset = SSV[i].Offset + SSV[i].Size; |
805 | if (PoisonSize < AsanAlignment || !SSV[i].Size || |
806 | (NextField % AsanAlignment) != 0) |
807 | continue; |
808 | Builder.CreateCall( |
809 | F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)), |
810 | Builder.getIntN(PtrSize, PoisonSize)}); |
811 | } |
812 | } |
813 | |
814 | |
815 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
816 | EmitAsanPrologueOrEpilogue(true); |
817 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
818 | CXXCtorType CtorType = CurGD.getCtorType(); |
819 | |
820 | (0) . __assert_fail ("(CGM.getTarget().getCXXABI().hasConstructorVariants() || CtorType == Ctor_Complete) && \"can only generate complete ctor for this ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((CGM.getTarget().getCXXABI().hasConstructorVariants() || |
821 | (0) . __assert_fail ("(CGM.getTarget().getCXXABI().hasConstructorVariants() || CtorType == Ctor_Complete) && \"can only generate complete ctor for this ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CtorType == Ctor_Complete) && |
822 | (0) . __assert_fail ("(CGM.getTarget().getCXXABI().hasConstructorVariants() || CtorType == Ctor_Complete) && \"can only generate complete ctor for this ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "can only generate complete ctor for this ABI"); |
823 | |
824 | |
825 | |
826 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) && |
827 | CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
828 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getEndLoc()); |
829 | return; |
830 | } |
831 | |
832 | const FunctionDecl *Definition = nullptr; |
833 | Stmt *Body = Ctor->getBody(Definition); |
834 | (0) . __assert_fail ("Definition == Ctor && \"emitting wrong constructor body\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 834, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Definition == Ctor && "emitting wrong constructor body"); |
835 | |
836 | |
837 | |
838 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
839 | if (IsTryBody) |
840 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
841 | |
842 | incrementProfileCounter(Body); |
843 | |
844 | RunCleanupsScope RunCleanups(*this); |
845 | |
846 | |
847 | |
848 | |
849 | |
850 | |
851 | EmitCtorPrologue(Ctor, CtorType, Args); |
852 | |
853 | |
854 | if (IsTryBody) |
855 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
856 | else if (Body) |
857 | EmitStmt(Body); |
858 | |
859 | |
860 | |
861 | |
862 | |
863 | RunCleanups.ForceCleanup(); |
864 | |
865 | if (IsTryBody) |
866 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
867 | } |
868 | |
869 | namespace { |
870 | |
871 | |
872 | |
873 | |
874 | class CopyingValueRepresentation { |
875 | public: |
876 | explicit CopyingValueRepresentation(CodeGenFunction &CGF) |
877 | : CGF(CGF), OldSanOpts(CGF.SanOpts) { |
878 | CGF.SanOpts.set(SanitizerKind::Bool, false); |
879 | CGF.SanOpts.set(SanitizerKind::Enum, false); |
880 | } |
881 | ~CopyingValueRepresentation() { |
882 | CGF.SanOpts = OldSanOpts; |
883 | } |
884 | private: |
885 | CodeGenFunction &CGF; |
886 | SanitizerSet OldSanOpts; |
887 | }; |
888 | } |
889 | |
890 | namespace { |
891 | class FieldMemcpyizer { |
892 | public: |
893 | FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, |
894 | const VarDecl *SrcRec) |
895 | : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec), |
896 | RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)), |
897 | FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0), |
898 | LastFieldOffset(0), LastAddedFieldIndex(0) {} |
899 | |
900 | bool isMemcpyableField(FieldDecl *F) const { |
901 | |
902 | if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding) |
903 | return false; |
904 | Qualifiers Qual = F->getType().getQualifiers(); |
905 | if (Qual.hasVolatile() || Qual.hasObjCLifetime()) |
906 | return false; |
907 | return true; |
908 | } |
909 | |
910 | void addMemcpyableField(FieldDecl *F) { |
911 | if (!FirstField) |
912 | addInitialField(F); |
913 | else |
914 | addNextField(F); |
915 | } |
916 | |
917 | CharUnits getMemcpySize(uint64_t FirstByteOffset) const { |
918 | ASTContext &Ctx = CGF.getContext(); |
919 | unsigned LastFieldSize = |
920 | LastField->isBitField() |
921 | ? LastField->getBitWidthValue(Ctx) |
922 | : Ctx.toBits( |
923 | Ctx.getTypeInfoDataSizeInChars(LastField->getType()).first); |
924 | uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize - |
925 | FirstByteOffset + Ctx.getCharWidth() - 1; |
926 | CharUnits MemcpySize = Ctx.toCharUnitsFromBits(MemcpySizeBits); |
927 | return MemcpySize; |
928 | } |
929 | |
930 | void emitMemcpy() { |
931 | |
932 | |
933 | if (!FirstField) { |
934 | return; |
935 | } |
936 | |
937 | uint64_t FirstByteOffset; |
938 | if (FirstField->isBitField()) { |
939 | const CGRecordLayout &RL = |
940 | CGF.getTypes().getCGRecordLayout(FirstField->getParent()); |
941 | const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField); |
942 | |
943 | |
944 | FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset); |
945 | } else { |
946 | FirstByteOffset = FirstFieldOffset; |
947 | } |
948 | |
949 | CharUnits MemcpySize = getMemcpySize(FirstByteOffset); |
950 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
951 | Address ThisPtr = CGF.LoadCXXThisAddress(); |
952 | LValue DestLV = CGF.MakeAddrLValue(ThisPtr, RecordTy); |
953 | LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField); |
954 | llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec)); |
955 | LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
956 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField); |
957 | |
958 | emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddress() : Dest.getAddress(), |
959 | Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(), |
960 | MemcpySize); |
961 | reset(); |
962 | } |
963 | |
964 | void reset() { |
965 | FirstField = nullptr; |
966 | } |
967 | |
968 | protected: |
969 | CodeGenFunction &CGF; |
970 | const CXXRecordDecl *ClassDecl; |
971 | |
972 | private: |
973 | void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) { |
974 | llvm::PointerType *DPT = DestPtr.getType(); |
975 | llvm::Type *DBP = |
976 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace()); |
977 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP); |
978 | |
979 | llvm::PointerType *SPT = SrcPtr.getType(); |
980 | llvm::Type *SBP = |
981 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace()); |
982 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP); |
983 | |
984 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity()); |
985 | } |
986 | |
987 | void addInitialField(FieldDecl *F) { |
988 | FirstField = F; |
989 | LastField = F; |
990 | FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
991 | LastFieldOffset = FirstFieldOffset; |
992 | LastAddedFieldIndex = F->getFieldIndex(); |
993 | } |
994 | |
995 | void addNextField(FieldDecl *F) { |
996 | |
997 | |
998 | |
999 | |
1000 | (0) . __assert_fail ("F->getFieldIndex() >= LastAddedFieldIndex + 1 && \"Cannot aggregate fields out of order.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1001, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 && |
1001 | (0) . __assert_fail ("F->getFieldIndex() >= LastAddedFieldIndex + 1 && \"Cannot aggregate fields out of order.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1001, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Cannot aggregate fields out of order."); |
1002 | LastAddedFieldIndex = F->getFieldIndex(); |
1003 | |
1004 | |
1005 | |
1006 | |
1007 | uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
1008 | if (FOffset < FirstFieldOffset) { |
1009 | FirstField = F; |
1010 | FirstFieldOffset = FOffset; |
1011 | } else if (FOffset > LastFieldOffset) { |
1012 | LastField = F; |
1013 | LastFieldOffset = FOffset; |
1014 | } |
1015 | } |
1016 | |
1017 | const VarDecl *SrcRec; |
1018 | const ASTRecordLayout &RecLayout; |
1019 | FieldDecl *FirstField; |
1020 | FieldDecl *LastField; |
1021 | uint64_t FirstFieldOffset, LastFieldOffset; |
1022 | unsigned LastAddedFieldIndex; |
1023 | }; |
1024 | |
1025 | class ConstructorMemcpyizer : public FieldMemcpyizer { |
1026 | private: |
1027 | |
1028 | |
1029 | static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF, |
1030 | const CXXConstructorDecl *CD, |
1031 | FunctionArgList &Args) { |
1032 | if (CD->isCopyOrMoveConstructor() && CD->isDefaulted()) |
1033 | return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)]; |
1034 | return nullptr; |
1035 | } |
1036 | |
1037 | |
1038 | |
1039 | bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const { |
1040 | if (!MemcpyableCtor) |
1041 | return false; |
1042 | FieldDecl *Field = MemberInit->getMember(); |
1043 | (0) . __assert_fail ("Field && \"No field for member init.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1043, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Field && "No field for member init."); |
1044 | QualType FieldType = Field->getType(); |
1045 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
1046 | |
1047 | |
1048 | if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) && |
1049 | !(FieldType.isTriviallyCopyableType(CGF.getContext()) || |
1050 | FieldType->isReferenceType())) |
1051 | return false; |
1052 | |
1053 | |
1054 | if (!isMemcpyableField(Field)) |
1055 | return false; |
1056 | |
1057 | |
1058 | return true; |
1059 | } |
1060 | |
1061 | public: |
1062 | ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD, |
1063 | FunctionArgList &Args) |
1064 | : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)), |
1065 | ConstructorDecl(CD), |
1066 | MemcpyableCtor(CD->isDefaulted() && |
1067 | CD->isCopyOrMoveConstructor() && |
1068 | CGF.getLangOpts().getGC() == LangOptions::NonGC), |
1069 | Args(Args) { } |
1070 | |
1071 | void addMemberInitializer(CXXCtorInitializer *MemberInit) { |
1072 | if (isMemberInitMemcpyable(MemberInit)) { |
1073 | AggregatedInits.push_back(MemberInit); |
1074 | addMemcpyableField(MemberInit->getMember()); |
1075 | } else { |
1076 | emitAggregatedInits(); |
1077 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit, |
1078 | ConstructorDecl, Args); |
1079 | } |
1080 | } |
1081 | |
1082 | void emitAggregatedInits() { |
1083 | if (AggregatedInits.size() <= 1) { |
1084 | |
1085 | |
1086 | if (!AggregatedInits.empty()) { |
1087 | CopyingValueRepresentation CVR(CGF); |
1088 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), |
1089 | AggregatedInits[0], ConstructorDecl, Args); |
1090 | AggregatedInits.clear(); |
1091 | } |
1092 | reset(); |
1093 | return; |
1094 | } |
1095 | |
1096 | pushEHDestructors(); |
1097 | emitMemcpy(); |
1098 | AggregatedInits.clear(); |
1099 | } |
1100 | |
1101 | void pushEHDestructors() { |
1102 | Address ThisPtr = CGF.LoadCXXThisAddress(); |
1103 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
1104 | LValue LHS = CGF.MakeAddrLValue(ThisPtr, RecordTy); |
1105 | |
1106 | for (unsigned i = 0; i < AggregatedInits.size(); ++i) { |
1107 | CXXCtorInitializer *MemberInit = AggregatedInits[i]; |
1108 | QualType FieldType = MemberInit->getAnyMember()->getType(); |
1109 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
1110 | if (!CGF.needsEHCleanup(dtorKind)) |
1111 | continue; |
1112 | LValue FieldLHS = LHS; |
1113 | EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS); |
1114 | CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(), FieldType); |
1115 | } |
1116 | } |
1117 | |
1118 | void finish() { |
1119 | emitAggregatedInits(); |
1120 | } |
1121 | |
1122 | private: |
1123 | const CXXConstructorDecl *ConstructorDecl; |
1124 | bool MemcpyableCtor; |
1125 | FunctionArgList &Args; |
1126 | SmallVector<CXXCtorInitializer*, 16> AggregatedInits; |
1127 | }; |
1128 | |
1129 | class AssignmentMemcpyizer : public FieldMemcpyizer { |
1130 | private: |
1131 | |
1132 | |
1133 | FieldDecl *getMemcpyableField(Stmt *S) { |
1134 | if (!AssignmentsMemcpyable) |
1135 | return nullptr; |
1136 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) { |
1137 | |
1138 | if (BO->getOpcode() != BO_Assign) |
1139 | return nullptr; |
1140 | MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS()); |
1141 | if (!ME) |
1142 | return nullptr; |
1143 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
1144 | if (!Field || !isMemcpyableField(Field)) |
1145 | return nullptr; |
1146 | Stmt *RHS = BO->getRHS(); |
1147 | if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS)) |
1148 | RHS = EC->getSubExpr(); |
1149 | if (!RHS) |
1150 | return nullptr; |
1151 | if (MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS)) { |
1152 | if (ME2->getMemberDecl() == Field) |
1153 | return Field; |
1154 | } |
1155 | return nullptr; |
1156 | } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) { |
1157 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl()); |
1158 | if (!(MD && isMemcpyEquivalentSpecialMember(MD))) |
1159 | return nullptr; |
1160 | MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument()); |
1161 | if (!IOA) |
1162 | return nullptr; |
1163 | FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl()); |
1164 | if (!Field || !isMemcpyableField(Field)) |
1165 | return nullptr; |
1166 | MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0)); |
1167 | if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl())) |
1168 | return nullptr; |
1169 | return Field; |
1170 | } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
1171 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
1172 | if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy) |
1173 | return nullptr; |
1174 | Expr *DstPtr = CE->getArg(0); |
1175 | if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr)) |
1176 | DstPtr = DC->getSubExpr(); |
1177 | UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr); |
1178 | if (!DUO || DUO->getOpcode() != UO_AddrOf) |
1179 | return nullptr; |
1180 | MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr()); |
1181 | if (!ME) |
1182 | return nullptr; |
1183 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
1184 | if (!Field || !isMemcpyableField(Field)) |
1185 | return nullptr; |
1186 | Expr *SrcPtr = CE->getArg(1); |
1187 | if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr)) |
1188 | SrcPtr = SC->getSubExpr(); |
1189 | UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr); |
1190 | if (!SUO || SUO->getOpcode() != UO_AddrOf) |
1191 | return nullptr; |
1192 | MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr()); |
1193 | if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl())) |
1194 | return nullptr; |
1195 | return Field; |
1196 | } |
1197 | |
1198 | return nullptr; |
1199 | } |
1200 | |
1201 | bool AssignmentsMemcpyable; |
1202 | SmallVector<Stmt*, 16> AggregatedStmts; |
1203 | |
1204 | public: |
1205 | AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD, |
1206 | FunctionArgList &Args) |
1207 | : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]), |
1208 | AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) { |
1209 | assert(Args.size() == 2); |
1210 | } |
1211 | |
1212 | void emitAssignment(Stmt *S) { |
1213 | FieldDecl *F = getMemcpyableField(S); |
1214 | if (F) { |
1215 | addMemcpyableField(F); |
1216 | AggregatedStmts.push_back(S); |
1217 | } else { |
1218 | emitAggregatedStmts(); |
1219 | CGF.EmitStmt(S); |
1220 | } |
1221 | } |
1222 | |
1223 | void emitAggregatedStmts() { |
1224 | if (AggregatedStmts.size() <= 1) { |
1225 | if (!AggregatedStmts.empty()) { |
1226 | CopyingValueRepresentation CVR(CGF); |
1227 | CGF.EmitStmt(AggregatedStmts[0]); |
1228 | } |
1229 | reset(); |
1230 | } |
1231 | |
1232 | emitMemcpy(); |
1233 | AggregatedStmts.clear(); |
1234 | } |
1235 | |
1236 | void finish() { |
1237 | emitAggregatedStmts(); |
1238 | } |
1239 | }; |
1240 | } |
1241 | |
1242 | static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) { |
1243 | const Type *BaseType = BaseInit->getBaseClass(); |
1244 | const auto *BaseClassDecl = |
1245 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
1246 | return BaseClassDecl->isDynamicClass(); |
1247 | } |
1248 | |
1249 | |
1250 | |
1251 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
1252 | CXXCtorType CtorType, |
1253 | FunctionArgList &Args) { |
1254 | if (CD->isDelegatingConstructor()) |
1255 | return EmitDelegatingCXXConstructorCall(CD, Args); |
1256 | |
1257 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
1258 | |
1259 | CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
1260 | E = CD->init_end(); |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | |
1269 | bool ConstructVBases = CtorType != Ctor_Base && |
1270 | ClassDecl->getNumVBases() != 0 && |
1271 | !ClassDecl->isAbstract(); |
1272 | |
1273 | |
1274 | |
1275 | |
1276 | llvm::BasicBlock *BaseCtorContinueBB = nullptr; |
1277 | if (ConstructVBases && |
1278 | !CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
1279 | BaseCtorContinueBB = |
1280 | CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl); |
1281 | assert(BaseCtorContinueBB); |
1282 | } |
1283 | |
1284 | llvm::Value *const OldThis = CXXThisValue; |
1285 | for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) { |
1286 | if (!ConstructVBases) |
1287 | continue; |
1288 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
1289 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
1290 | isInitializerOfDynamicClass(*B)) |
1291 | CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis()); |
1292 | EmitBaseInitializer(*this, ClassDecl, *B); |
1293 | } |
1294 | |
1295 | if (BaseCtorContinueBB) { |
1296 | |
1297 | Builder.CreateBr(BaseCtorContinueBB); |
1298 | EmitBlock(BaseCtorContinueBB); |
1299 | } |
1300 | |
1301 | |
1302 | for (; B != E && (*B)->isBaseInitializer(); B++) { |
1303 | isBaseVirtual()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1303, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!(*B)->isBaseVirtual()); |
1304 | |
1305 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
1306 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
1307 | isInitializerOfDynamicClass(*B)) |
1308 | CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis()); |
1309 | EmitBaseInitializer(*this, ClassDecl, *B); |
1310 | } |
1311 | |
1312 | CXXThisValue = OldThis; |
1313 | |
1314 | InitializeVTablePointers(ClassDecl); |
1315 | |
1316 | |
1317 | FieldConstructionScope FCS(*this, LoadCXXThisAddress()); |
1318 | ConstructorMemcpyizer CM(*this, CD, Args); |
1319 | for (; B != E; B++) { |
1320 | CXXCtorInitializer *Member = (*B); |
1321 | isBaseInitializer()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1321, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Member->isBaseInitializer()); |
1322 | (0) . __assert_fail ("Member->isAnyMemberInitializer() && \"Delegating initializer on non-delegating constructor\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1323, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Member->isAnyMemberInitializer() && |
1323 | (0) . __assert_fail ("Member->isAnyMemberInitializer() && \"Delegating initializer on non-delegating constructor\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1323, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Delegating initializer on non-delegating constructor"); |
1324 | CM.addMemberInitializer(Member); |
1325 | } |
1326 | CM.finish(); |
1327 | } |
1328 | |
1329 | static bool |
1330 | FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); |
1331 | |
1332 | static bool |
1333 | HasTrivialDestructorBody(ASTContext &Context, |
1334 | const CXXRecordDecl *BaseClassDecl, |
1335 | const CXXRecordDecl *MostDerivedClassDecl) |
1336 | { |
1337 | |
1338 | if (BaseClassDecl->hasTrivialDestructor()) |
1339 | return true; |
1340 | |
1341 | if (!BaseClassDecl->getDestructor()->hasTrivialBody()) |
1342 | return false; |
1343 | |
1344 | |
1345 | for (const auto *Field : BaseClassDecl->fields()) |
1346 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
1347 | return false; |
1348 | |
1349 | |
1350 | for (const auto &I : BaseClassDecl->bases()) { |
1351 | if (I.isVirtual()) |
1352 | continue; |
1353 | |
1354 | const CXXRecordDecl *NonVirtualBase = |
1355 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
1356 | if (!HasTrivialDestructorBody(Context, NonVirtualBase, |
1357 | MostDerivedClassDecl)) |
1358 | return false; |
1359 | } |
1360 | |
1361 | if (BaseClassDecl == MostDerivedClassDecl) { |
1362 | |
1363 | for (const auto &I : BaseClassDecl->vbases()) { |
1364 | const CXXRecordDecl *VirtualBase = |
1365 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
1366 | if (!HasTrivialDestructorBody(Context, VirtualBase, |
1367 | MostDerivedClassDecl)) |
1368 | return false; |
1369 | } |
1370 | } |
1371 | |
1372 | return true; |
1373 | } |
1374 | |
1375 | static bool |
1376 | FieldHasTrivialDestructorBody(ASTContext &Context, |
1377 | const FieldDecl *Field) |
1378 | { |
1379 | QualType FieldBaseElementType = Context.getBaseElementType(Field->getType()); |
1380 | |
1381 | const RecordType *RT = FieldBaseElementType->getAs<RecordType>(); |
1382 | if (!RT) |
1383 | return true; |
1384 | |
1385 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
1386 | |
1387 | |
1388 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
1389 | return false; |
1390 | |
1391 | return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl); |
1392 | } |
1393 | |
1394 | |
1395 | |
1396 | static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF, |
1397 | const CXXDestructorDecl *Dtor) { |
1398 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
1399 | if (!ClassDecl->isDynamicClass()) |
1400 | return true; |
1401 | |
1402 | if (!Dtor->hasTrivialBody()) |
1403 | return false; |
1404 | |
1405 | |
1406 | for (const auto *Field : ClassDecl->fields()) |
1407 | if (!FieldHasTrivialDestructorBody(CGF.getContext(), Field)) |
1408 | return false; |
1409 | |
1410 | return true; |
1411 | } |
1412 | |
1413 | |
1414 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
1415 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
1416 | CXXDtorType DtorType = CurGD.getDtorType(); |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | |
1423 | if (DtorType != Dtor_Base && Dtor->getParent()->isAbstract()) { |
1424 | llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); |
1425 | TrapCall->setDoesNotReturn(); |
1426 | TrapCall->setDoesNotThrow(); |
1427 | Builder.CreateUnreachable(); |
1428 | Builder.ClearInsertionPoint(); |
1429 | return; |
1430 | } |
1431 | |
1432 | Stmt *Body = Dtor->getBody(); |
1433 | if (Body) |
1434 | incrementProfileCounter(Body); |
1435 | |
1436 | |
1437 | |
1438 | |
1439 | |
1440 | if (DtorType == Dtor_Deleting) { |
1441 | RunCleanupsScope DtorEpilogue(*this); |
1442 | EnterDtorCleanups(Dtor, Dtor_Deleting); |
1443 | if (HaveInsertPoint()) |
1444 | EmitCXXDestructorCall(Dtor, Dtor_Complete, , |
1445 | , LoadCXXThisAddress()); |
1446 | return; |
1447 | } |
1448 | |
1449 | |
1450 | |
1451 | bool isTryBody = (Body && isa<CXXTryStmt>(Body)); |
1452 | if (isTryBody) |
1453 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
1454 | EmitAsanPrologueOrEpilogue(false); |
1455 | |
1456 | |
1457 | RunCleanupsScope DtorEpilogue(*this); |
1458 | |
1459 | |
1460 | |
1461 | |
1462 | |
1463 | |
1464 | switch (DtorType) { |
1465 | case Dtor_Comdat: llvm_unreachable("not expecting a COMDAT"); |
1466 | case Dtor_Deleting: llvm_unreachable("already handled deleting case"); |
1467 | |
1468 | case Dtor_Complete: |
1469 | (0) . __assert_fail ("(Body || getTarget().getCXXABI().isMicrosoft()) && \"can't emit a dtor without a body for non-Microsoft ABIs\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1470, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Body || getTarget().getCXXABI().isMicrosoft()) && |
1470 | (0) . __assert_fail ("(Body || getTarget().getCXXABI().isMicrosoft()) && \"can't emit a dtor without a body for non-Microsoft ABIs\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1470, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "can't emit a dtor without a body for non-Microsoft ABIs"); |
1471 | |
1472 | |
1473 | EnterDtorCleanups(Dtor, Dtor_Complete); |
1474 | |
1475 | if (!isTryBody) { |
1476 | EmitCXXDestructorCall(Dtor, Dtor_Base, , |
1477 | , LoadCXXThisAddress()); |
1478 | break; |
1479 | } |
1480 | |
1481 | |
1482 | LLVM_FALLTHROUGH; |
1483 | |
1484 | case Dtor_Base: |
1485 | assert(Body); |
1486 | |
1487 | |
1488 | EnterDtorCleanups(Dtor, Dtor_Base); |
1489 | |
1490 | |
1491 | if (!CanSkipVTablePointerInitialization(*this, Dtor)) { |
1492 | |
1493 | |
1494 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
1495 | CGM.getCodeGenOpts().OptimizationLevel > 0) |
1496 | CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis()); |
1497 | InitializeVTablePointers(Dtor->getParent()); |
1498 | } |
1499 | |
1500 | if (isTryBody) |
1501 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
1502 | else if (Body) |
1503 | EmitStmt(Body); |
1504 | else { |
1505 | (0) . __assert_fail ("Dtor->isImplicit() && \"bodyless dtor not implicit\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1505, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
1506 | |
1507 | } |
1508 | |
1509 | |
1510 | if (getLangOpts().AppleKext) |
1511 | CurFn->addFnAttr(llvm::Attribute::AlwaysInline); |
1512 | |
1513 | break; |
1514 | } |
1515 | |
1516 | |
1517 | DtorEpilogue.ForceCleanup(); |
1518 | |
1519 | |
1520 | if (isTryBody) |
1521 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
1522 | } |
1523 | |
1524 | void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) { |
1525 | const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl()); |
1526 | const Stmt *RootS = AssignOp->getBody(); |
1527 | (0) . __assert_fail ("isa(RootS) && \"Body of an implicit assignment operator should be compound stmt.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1528, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<CompoundStmt>(RootS) && |
1528 | (0) . __assert_fail ("isa(RootS) && \"Body of an implicit assignment operator should be compound stmt.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1528, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Body of an implicit assignment operator should be compound stmt."); |
1529 | const CompoundStmt *RootCS = cast<CompoundStmt>(RootS); |
1530 | |
1531 | LexicalScope Scope(*this, RootCS->getSourceRange()); |
1532 | |
1533 | incrementProfileCounter(RootCS); |
1534 | AssignmentMemcpyizer AM(*this, AssignOp, Args); |
1535 | for (auto *I : RootCS->body()) |
1536 | AM.emitAssignment(I); |
1537 | AM.finish(); |
1538 | } |
1539 | |
1540 | namespace { |
1541 | llvm::Value *LoadThisForDtorDelete(CodeGenFunction &CGF, |
1542 | const CXXDestructorDecl *DD) { |
1543 | if (Expr *ThisArg = DD->getOperatorDeleteThisArg()) |
1544 | return CGF.EmitScalarExpr(ThisArg); |
1545 | return CGF.LoadCXXThis(); |
1546 | } |
1547 | |
1548 | |
1549 | struct CallDtorDelete final : EHScopeStack::Cleanup { |
1550 | CallDtorDelete() {} |
1551 | |
1552 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1553 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
1554 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
1555 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), |
1556 | LoadThisForDtorDelete(CGF, Dtor), |
1557 | CGF.getContext().getTagDeclType(ClassDecl)); |
1558 | } |
1559 | }; |
1560 | |
1561 | void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF, |
1562 | llvm::Value *ShouldDeleteCondition, |
1563 | bool ReturnAfterDelete) { |
1564 | llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete"); |
1565 | llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue"); |
1566 | llvm::Value *ShouldCallDelete |
1567 | = CGF.Builder.CreateIsNull(ShouldDeleteCondition); |
1568 | CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB); |
1569 | |
1570 | CGF.EmitBlock(callDeleteBB); |
1571 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
1572 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
1573 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), |
1574 | LoadThisForDtorDelete(CGF, Dtor), |
1575 | CGF.getContext().getTagDeclType(ClassDecl)); |
1576 | (0) . __assert_fail ("Dtor->getOperatorDelete()->isDestroyingOperatorDelete() == ReturnAfterDelete && \"unexpected value for ReturnAfterDelete\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1578, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Dtor->getOperatorDelete()->isDestroyingOperatorDelete() == |
1577 | (0) . __assert_fail ("Dtor->getOperatorDelete()->isDestroyingOperatorDelete() == ReturnAfterDelete && \"unexpected value for ReturnAfterDelete\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1578, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ReturnAfterDelete && |
1578 | (0) . __assert_fail ("Dtor->getOperatorDelete()->isDestroyingOperatorDelete() == ReturnAfterDelete && \"unexpected value for ReturnAfterDelete\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1578, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "unexpected value for ReturnAfterDelete"); |
1579 | if (ReturnAfterDelete) |
1580 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
1581 | else |
1582 | CGF.Builder.CreateBr(continueBB); |
1583 | |
1584 | CGF.EmitBlock(continueBB); |
1585 | } |
1586 | |
1587 | struct CallDtorDeleteConditional final : EHScopeStack::Cleanup { |
1588 | llvm::Value *ShouldDeleteCondition; |
1589 | |
1590 | public: |
1591 | CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition) |
1592 | : ShouldDeleteCondition(ShouldDeleteCondition) { |
1593 | assert(ShouldDeleteCondition != nullptr); |
1594 | } |
1595 | |
1596 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1597 | EmitConditionalDtorDeleteCall(CGF, ShouldDeleteCondition, |
1598 | ); |
1599 | } |
1600 | }; |
1601 | |
1602 | class DestroyField final : public EHScopeStack::Cleanup { |
1603 | const FieldDecl *field; |
1604 | CodeGenFunction::Destroyer *destroyer; |
1605 | bool useEHCleanupForArray; |
1606 | |
1607 | public: |
1608 | DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer, |
1609 | bool useEHCleanupForArray) |
1610 | : field(field), destroyer(destroyer), |
1611 | useEHCleanupForArray(useEHCleanupForArray) {} |
1612 | |
1613 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1614 | |
1615 | Address thisValue = CGF.LoadCXXThisAddress(); |
1616 | QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent()); |
1617 | LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy); |
1618 | LValue LV = CGF.EmitLValueForField(ThisLV, field); |
1619 | assert(LV.isSimple()); |
1620 | |
1621 | CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer, |
1622 | flags.isForNormalCleanup() && useEHCleanupForArray); |
1623 | } |
1624 | }; |
1625 | |
1626 | static void EmitSanitizerDtorCallback(CodeGenFunction &CGF, llvm::Value *Ptr, |
1627 | CharUnits::QuantityType PoisonSize) { |
1628 | CodeGenFunction::SanitizerScope SanScope(&CGF); |
1629 | |
1630 | |
1631 | llvm::Value *Args[] = {CGF.Builder.CreateBitCast(Ptr, CGF.VoidPtrTy), |
1632 | llvm::ConstantInt::get(CGF.SizeTy, PoisonSize)}; |
1633 | |
1634 | llvm::Type *ArgTypes[] = {CGF.VoidPtrTy, CGF.SizeTy}; |
1635 | |
1636 | llvm::FunctionType *FnType = |
1637 | llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false); |
1638 | llvm::FunctionCallee Fn = |
1639 | CGF.CGM.CreateRuntimeFunction(FnType, "__sanitizer_dtor_callback"); |
1640 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
1641 | } |
1642 | |
1643 | class SanitizeDtorMembers final : public EHScopeStack::Cleanup { |
1644 | const CXXDestructorDecl *Dtor; |
1645 | |
1646 | public: |
1647 | SanitizeDtorMembers(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {} |
1648 | |
1649 | |
1650 | |
1651 | |
1652 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1653 | const ASTRecordLayout &Layout = |
1654 | CGF.getContext().getASTRecordLayout(Dtor->getParent()); |
1655 | |
1656 | |
1657 | if (Layout.getFieldCount() == 0) |
1658 | return; |
1659 | |
1660 | |
1661 | CGF.CurFn->addFnAttr("disable-tail-calls", "true"); |
1662 | |
1663 | |
1664 | |
1665 | ASTContext &Context = CGF.getContext(); |
1666 | unsigned fieldIndex = 0; |
1667 | int startIndex = -1; |
1668 | |
1669 | for (const FieldDecl *Field : Dtor->getParent()->fields()) { |
1670 | |
1671 | if (FieldHasTrivialDestructorBody(Context, Field)) { |
1672 | |
1673 | if (startIndex < 0) |
1674 | startIndex = fieldIndex; |
1675 | |
1676 | |
1677 | |
1678 | if (fieldIndex == Layout.getFieldCount() - 1) { |
1679 | PoisonMembers(CGF, startIndex, Layout.getFieldCount()); |
1680 | } |
1681 | } else if (startIndex >= 0) { |
1682 | |
1683 | PoisonMembers(CGF, startIndex, fieldIndex); |
1684 | |
1685 | startIndex = -1; |
1686 | } |
1687 | fieldIndex += 1; |
1688 | } |
1689 | } |
1690 | |
1691 | private: |
1692 | |
1693 | |
1694 | |
1695 | |
1696 | void PoisonMembers(CodeGenFunction &CGF, unsigned layoutStartOffset, |
1697 | unsigned layoutEndOffset) { |
1698 | ASTContext &Context = CGF.getContext(); |
1699 | const ASTRecordLayout &Layout = |
1700 | Context.getASTRecordLayout(Dtor->getParent()); |
1701 | |
1702 | llvm::ConstantInt *OffsetSizePtr = llvm::ConstantInt::get( |
1703 | CGF.SizeTy, |
1704 | Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset)) |
1705 | .getQuantity()); |
1706 | |
1707 | llvm::Value *OffsetPtr = CGF.Builder.CreateGEP( |
1708 | CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy), |
1709 | OffsetSizePtr); |
1710 | |
1711 | CharUnits::QuantityType PoisonSize; |
1712 | if (layoutEndOffset >= Layout.getFieldCount()) { |
1713 | PoisonSize = Layout.getNonVirtualSize().getQuantity() - |
1714 | Context.toCharUnitsFromBits( |
1715 | Layout.getFieldOffset(layoutStartOffset)) |
1716 | .getQuantity(); |
1717 | } else { |
1718 | PoisonSize = Context.toCharUnitsFromBits( |
1719 | Layout.getFieldOffset(layoutEndOffset) - |
1720 | Layout.getFieldOffset(layoutStartOffset)) |
1721 | .getQuantity(); |
1722 | } |
1723 | |
1724 | if (PoisonSize == 0) |
1725 | return; |
1726 | |
1727 | EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize); |
1728 | } |
1729 | }; |
1730 | |
1731 | class SanitizeDtorVTable final : public EHScopeStack::Cleanup { |
1732 | const CXXDestructorDecl *Dtor; |
1733 | |
1734 | public: |
1735 | SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {} |
1736 | |
1737 | |
1738 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1739 | getParent()->isDynamicClass()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1739, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Dtor->getParent()->isDynamicClass()); |
1740 | (void)Dtor; |
1741 | ASTContext &Context = CGF.getContext(); |
1742 | |
1743 | llvm::Value *VTablePtr = CGF.LoadCXXThis(); |
1744 | |
1745 | CharUnits::QuantityType PoisonSize = |
1746 | Context.toCharUnitsFromBits(CGF.PointerWidthInBits).getQuantity(); |
1747 | |
1748 | |
1749 | EmitSanitizerDtorCallback(CGF, VTablePtr, PoisonSize); |
1750 | } |
1751 | }; |
1752 | } |
1753 | |
1754 | |
1755 | |
1756 | |
1757 | |
1758 | |
1759 | |
1760 | void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, |
1761 | CXXDtorType DtorType) { |
1762 | (0) . __assert_fail ("(!DD->isTrivial() || DD->hasAttr()) && \"Should not emit dtor epilogue for non-exported trivial dtor!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1763, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) && |
1763 | (0) . __assert_fail ("(!DD->isTrivial() || DD->hasAttr()) && \"Should not emit dtor epilogue for non-exported trivial dtor!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1763, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should not emit dtor epilogue for non-exported trivial dtor!"); |
1764 | |
1765 | |
1766 | |
1767 | if (DtorType == Dtor_Deleting) { |
1768 | (0) . __assert_fail ("DD->getOperatorDelete() && \"operator delete missing - EnterDtorCleanups\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1769, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DD->getOperatorDelete() && |
1769 | (0) . __assert_fail ("DD->getOperatorDelete() && \"operator delete missing - EnterDtorCleanups\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 1769, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "operator delete missing - EnterDtorCleanups"); |
1770 | if (CXXStructorImplicitParamValue) { |
1771 | |
1772 | |
1773 | if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) |
1774 | EmitConditionalDtorDeleteCall(*this, CXXStructorImplicitParamValue, |
1775 | ); |
1776 | else |
1777 | EHStack.pushCleanup<CallDtorDeleteConditional>( |
1778 | NormalAndEHCleanup, CXXStructorImplicitParamValue); |
1779 | } else { |
1780 | if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) { |
1781 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
1782 | EmitDeleteCall(DD->getOperatorDelete(), |
1783 | LoadThisForDtorDelete(*this, DD), |
1784 | getContext().getTagDeclType(ClassDecl)); |
1785 | EmitBranchThroughCleanup(ReturnBlock); |
1786 | } else { |
1787 | EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup); |
1788 | } |
1789 | } |
1790 | return; |
1791 | } |
1792 | |
1793 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
1794 | |
1795 | |
1796 | if (ClassDecl->isUnion()) |
1797 | return; |
1798 | |
1799 | |
1800 | if (DtorType == Dtor_Complete) { |
1801 | |
1802 | |
1803 | if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
1804 | SanOpts.has(SanitizerKind::Memory) && ClassDecl->getNumVBases() && |
1805 | ClassDecl->isPolymorphic()) |
1806 | EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD); |
1807 | |
1808 | |
1809 | |
1810 | for (const auto &Base : ClassDecl->vbases()) { |
1811 | CXXRecordDecl *BaseClassDecl |
1812 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
1813 | |
1814 | |
1815 | if (BaseClassDecl->hasTrivialDestructor()) |
1816 | continue; |
1817 | |
1818 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
1819 | BaseClassDecl, |
1820 | true); |
1821 | } |
1822 | |
1823 | return; |
1824 | } |
1825 | |
1826 | assert(DtorType == Dtor_Base); |
1827 | |
1828 | |
1829 | if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
1830 | SanOpts.has(SanitizerKind::Memory) && !ClassDecl->getNumVBases() && |
1831 | ClassDecl->isPolymorphic()) |
1832 | EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD); |
1833 | |
1834 | |
1835 | for (const auto &Base : ClassDecl->bases()) { |
1836 | |
1837 | if (Base.isVirtual()) |
1838 | continue; |
1839 | |
1840 | CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); |
1841 | |
1842 | |
1843 | if (BaseClassDecl->hasTrivialDestructor()) |
1844 | continue; |
1845 | |
1846 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
1847 | BaseClassDecl, |
1848 | false); |
1849 | } |
1850 | |
1851 | |
1852 | |
1853 | if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
1854 | SanOpts.has(SanitizerKind::Memory)) |
1855 | EHStack.pushCleanup<SanitizeDtorMembers>(NormalAndEHCleanup, DD); |
1856 | |
1857 | |
1858 | for (const auto *Field : ClassDecl->fields()) { |
1859 | QualType type = Field->getType(); |
1860 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
1861 | if (!dtorKind) continue; |
1862 | |
1863 | |
1864 | const RecordType *RT = type->getAsUnionType(); |
1865 | if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue; |
1866 | |
1867 | CleanupKind cleanupKind = getCleanupKind(dtorKind); |
1868 | EHStack.pushCleanup<DestroyField>(cleanupKind, Field, |
1869 | getDestroyer(dtorKind), |
1870 | cleanupKind & EHCleanup); |
1871 | } |
1872 | } |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | |
1878 | |
1879 | |
1880 | |
1881 | |
1882 | void CodeGenFunction::EmitCXXAggrConstructorCall( |
1883 | const CXXConstructorDecl *ctor, const ArrayType *arrayType, |
1884 | Address arrayBegin, const CXXConstructExpr *E, bool NewPointerIsChecked, |
1885 | bool zeroInitialize) { |
1886 | QualType elementType; |
1887 | llvm::Value *numElements = |
1888 | emitArrayLength(arrayType, elementType, arrayBegin); |
1889 | |
1890 | EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, |
1891 | NewPointerIsChecked, zeroInitialize); |
1892 | } |
1893 | |
1894 | |
1895 | |
1896 | |
1897 | |
1898 | |
1899 | |
1900 | |
1901 | |
1902 | |
1903 | void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor, |
1904 | llvm::Value *numElements, |
1905 | Address arrayBase, |
1906 | const CXXConstructExpr *E, |
1907 | bool NewPointerIsChecked, |
1908 | bool zeroInitialize) { |
1909 | |
1910 | |
1911 | |
1912 | |
1913 | |
1914 | llvm::BranchInst *zeroCheckBranch = nullptr; |
1915 | |
1916 | |
1917 | llvm::ConstantInt *constantCount |
1918 | = dyn_cast<llvm::ConstantInt>(numElements); |
1919 | if (constantCount) { |
1920 | |
1921 | if (constantCount->isZero()) return; |
1922 | |
1923 | |
1924 | } else { |
1925 | llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop"); |
1926 | llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty"); |
1927 | zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB); |
1928 | EmitBlock(loopBB); |
1929 | } |
1930 | |
1931 | |
1932 | llvm::Value *arrayBegin = arrayBase.getPointer(); |
1933 | llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements, |
1934 | "arrayctor.end"); |
1935 | |
1936 | |
1937 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
1938 | llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop"); |
1939 | EmitBlock(loopBB); |
1940 | llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2, |
1941 | "arrayctor.cur"); |
1942 | cur->addIncoming(arrayBegin, entryBB); |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | |
1951 | |
1952 | QualType type = getContext().getTypeDeclType(ctor->getParent()); |
1953 | CharUnits eltAlignment = |
1954 | arrayBase.getAlignment() |
1955 | .alignmentOfArrayElement(getContext().getTypeSizeInChars(type)); |
1956 | Address curAddr = Address(cur, eltAlignment); |
1957 | |
1958 | |
1959 | if (zeroInitialize) |
1960 | EmitNullInitialization(curAddr, type); |
1961 | |
1962 | |
1963 | |
1964 | |
1965 | |
1966 | |
1967 | |
1968 | |
1969 | |
1970 | { |
1971 | RunCleanupsScope Scope(*this); |
1972 | |
1973 | |
1974 | |
1975 | if (getLangOpts().Exceptions && |
1976 | !ctor->getParent()->hasTrivialDestructor()) { |
1977 | Destroyer *destroyer = destroyCXXObject; |
1978 | pushRegularPartialArrayCleanup(arrayBegin, cur, type, eltAlignment, |
1979 | *destroyer); |
1980 | } |
1981 | |
1982 | EmitCXXConstructorCall(ctor, Ctor_Complete, , |
1983 | , curAddr, E, |
1984 | AggValueSlot::DoesNotOverlap, NewPointerIsChecked); |
1985 | } |
1986 | |
1987 | |
1988 | llvm::Value *next = |
1989 | Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1), |
1990 | "arrayctor.next"); |
1991 | cur->addIncoming(next, Builder.GetInsertBlock()); |
1992 | |
1993 | |
1994 | llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done"); |
1995 | llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont"); |
1996 | Builder.CreateCondBr(done, contBB, loopBB); |
1997 | |
1998 | |
1999 | if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB); |
2000 | |
2001 | EmitBlock(contBB); |
2002 | } |
2003 | |
2004 | void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, |
2005 | Address addr, |
2006 | QualType type) { |
2007 | const RecordType *rtype = type->castAs<RecordType>(); |
2008 | const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl()); |
2009 | const CXXDestructorDecl *dtor = record->getDestructor(); |
2010 | isTrivial()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2010, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!dtor->isTrivial()); |
2011 | CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, false, |
2012 | , addr); |
2013 | } |
2014 | |
2015 | void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
2016 | CXXCtorType Type, |
2017 | bool ForVirtualBase, |
2018 | bool Delegating, Address This, |
2019 | const CXXConstructExpr *E, |
2020 | AggValueSlot::Overlap_t Overlap, |
2021 | bool NewPointerIsChecked) { |
2022 | CallArgList Args; |
2023 | |
2024 | LangAS SlotAS = E->getType().getAddressSpace(); |
2025 | QualType ThisType = D->getThisType(); |
2026 | LangAS ThisAS = ThisType.getTypePtr()->getPointeeType().getAddressSpace(); |
2027 | llvm::Value *ThisPtr = This.getPointer(); |
2028 | if (SlotAS != ThisAS) { |
2029 | unsigned TargetThisAS = getContext().getTargetAddressSpace(ThisAS); |
2030 | llvm::Type *NewType = |
2031 | ThisPtr->getType()->getPointerElementType()->getPointerTo(TargetThisAS); |
2032 | ThisPtr = getTargetHooks().performAddrSpaceCast(*this, This.getPointer(), |
2033 | ThisAS, SlotAS, NewType); |
2034 | } |
2035 | |
2036 | Args.add(RValue::get(ThisPtr), D->getThisType()); |
2037 | |
2038 | |
2039 | |
2040 | |
2041 | if (isMemcpyEquivalentSpecialMember(D)) { |
2042 | (0) . __assert_fail ("E->getNumArgs() == 1 && \"unexpected argcount for trivial ctor\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2042, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); |
2043 | |
2044 | const Expr *Arg = E->getArg(0); |
2045 | LValue Src = EmitLValue(Arg); |
2046 | QualType DestTy = getContext().getTypeDeclType(D->getParent()); |
2047 | LValue Dest = MakeAddrLValue(This, DestTy); |
2048 | EmitAggregateCopyCtor(Dest, Src, Overlap); |
2049 | return; |
2050 | } |
2051 | |
2052 | |
2053 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
2054 | EvaluationOrder Order = E->isListInitialization() |
2055 | ? EvaluationOrder::ForceLeftToRight |
2056 | : EvaluationOrder::Default; |
2057 | EmitCallArgs(Args, FPT, E->arguments(), E->getConstructor(), |
2058 | 0, Order); |
2059 | |
2060 | EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args, |
2061 | Overlap, E->getExprLoc(), NewPointerIsChecked); |
2062 | } |
2063 | |
2064 | static bool canEmitDelegateCallArgs(CodeGenFunction &CGF, |
2065 | const CXXConstructorDecl *Ctor, |
2066 | CXXCtorType Type, CallArgList &Args) { |
2067 | |
2068 | if (Ctor->isVariadic()) |
2069 | return false; |
2070 | |
2071 | if (CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
2072 | |
2073 | for (auto *P : Ctor->parameters()) |
2074 | if (P->getType().isDestructedType()) |
2075 | return false; |
2076 | |
2077 | |
2078 | const CGFunctionInfo &Info = |
2079 | CGF.CGM.getTypes().arrangeCXXConstructorCall(Args, Ctor, Type, 0, 0); |
2080 | if (Info.usesInAlloca()) |
2081 | return false; |
2082 | } |
2083 | |
2084 | |
2085 | return true; |
2086 | } |
2087 | |
2088 | void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
2089 | CXXCtorType Type, |
2090 | bool ForVirtualBase, |
2091 | bool Delegating, |
2092 | Address This, |
2093 | CallArgList &Args, |
2094 | AggValueSlot::Overlap_t Overlap, |
2095 | SourceLocation Loc, |
2096 | bool NewPointerIsChecked) { |
2097 | const CXXRecordDecl *ClassDecl = D->getParent(); |
2098 | |
2099 | if (!NewPointerIsChecked) |
2100 | EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, Loc, This.getPointer(), |
2101 | getContext().getRecordType(ClassDecl), CharUnits::Zero()); |
2102 | |
2103 | if (D->isTrivial() && D->isDefaultConstructor()) { |
2104 | (0) . __assert_fail ("Args.size() == 1 && \"trivial default ctor with args\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2104, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() == 1 && "trivial default ctor with args"); |
2105 | return; |
2106 | } |
2107 | |
2108 | |
2109 | |
2110 | |
2111 | if (isMemcpyEquivalentSpecialMember(D)) { |
2112 | (0) . __assert_fail ("Args.size() == 2 && \"unexpected argcount for trivial ctor\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2112, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() == 2 && "unexpected argcount for trivial ctor"); |
2113 | |
2114 | QualType SrcTy = D->getParamDecl(0)->getType().getNonReferenceType(); |
2115 | Address Src(Args[1].getRValue(*this).getScalarVal(), |
2116 | getNaturalTypeAlignment(SrcTy)); |
2117 | LValue SrcLVal = MakeAddrLValue(Src, SrcTy); |
2118 | QualType DestTy = getContext().getTypeDeclType(ClassDecl); |
2119 | LValue DestLVal = MakeAddrLValue(This, DestTy); |
2120 | EmitAggregateCopyCtor(DestLVal, SrcLVal, Overlap); |
2121 | return; |
2122 | } |
2123 | |
2124 | bool PassPrototypeArgs = true; |
2125 | |
2126 | if (auto Inherited = D->getInheritedConstructor()) { |
2127 | PassPrototypeArgs = getTypes().inheritingCtorHasParams(Inherited, Type); |
2128 | if (PassPrototypeArgs && !canEmitDelegateCallArgs(*this, D, Type, Args)) { |
2129 | EmitInlinedInheritingCXXConstructorCall(D, Type, ForVirtualBase, |
2130 | Delegating, Args); |
2131 | return; |
2132 | } |
2133 | } |
2134 | |
2135 | |
2136 | CGCXXABI::AddedStructorArgs = |
2137 | CGM.getCXXABI().addImplicitConstructorArgs(*this, D, Type, ForVirtualBase, |
2138 | Delegating, Args); |
2139 | |
2140 | |
2141 | llvm::Constant *CalleePtr = CGM.getAddrOfCXXStructor(GlobalDecl(D, Type)); |
2142 | const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall( |
2143 | Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs); |
2144 | CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(D, Type)); |
2145 | EmitCall(Info, Callee, ReturnValueSlot(), Args); |
2146 | |
2147 | |
2148 | |
2149 | |
2150 | |
2151 | |
2152 | |
2153 | |
2154 | |
2155 | |
2156 | |
2157 | |
2158 | if (CGM.getCodeGenOpts().OptimizationLevel > 0 && |
2159 | ClassDecl->isDynamicClass() && Type != Ctor_Base && |
2160 | CGM.getCXXABI().canSpeculativelyEmitVTable(ClassDecl) && |
2161 | CGM.getCodeGenOpts().StrictVTablePointers) |
2162 | EmitVTableAssumptionLoads(ClassDecl, This); |
2163 | } |
2164 | |
2165 | void CodeGenFunction::EmitInheritedCXXConstructorCall( |
2166 | const CXXConstructorDecl *D, bool ForVirtualBase, Address This, |
2167 | bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E) { |
2168 | CallArgList Args; |
2169 | CallArg ThisArg(RValue::get(This.getPointer()), D->getThisType()); |
2170 | |
2171 | |
2172 | if (InheritedFromVBase && |
2173 | CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
2174 | |
2175 | |
2176 | |
2177 | |
2178 | Args.push_back(ThisArg); |
2179 | } else if (!CXXInheritedCtorInitExprArgs.empty()) { |
2180 | |
2181 | (0) . __assert_fail ("CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() && \"wrong number of parameters for inherited constructor call\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2182, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() && |
2182 | (0) . __assert_fail ("CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() && \"wrong number of parameters for inherited constructor call\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2182, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "wrong number of parameters for inherited constructor call"); |
2183 | Args = CXXInheritedCtorInitExprArgs; |
2184 | Args[0] = ThisArg; |
2185 | } else { |
2186 | |
2187 | Args.push_back(ThisArg); |
2188 | const auto *OuterCtor = cast<CXXConstructorDecl>(CurCodeDecl); |
2189 | getNumParams() == D->getNumParams()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2189, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OuterCtor->getNumParams() == D->getNumParams()); |
2190 | (0) . __assert_fail ("!OuterCtor->isVariadic() && \"should have been inlined\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2190, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!OuterCtor->isVariadic() && "should have been inlined"); |
2191 | |
2192 | for (const auto *Param : OuterCtor->parameters()) { |
2193 | getParamDecl(Param->getFunctionScopeIndex())->getType(), Param->getType())", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2195, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getContext().hasSameUnqualifiedType( |
2194 | getParamDecl(Param->getFunctionScopeIndex())->getType(), Param->getType())", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2195, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> OuterCtor->getParamDecl(Param->getFunctionScopeIndex())->getType(), |
2195 | getParamDecl(Param->getFunctionScopeIndex())->getType(), Param->getType())", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2195, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Param->getType())); |
2196 | EmitDelegateCallArg(Args, Param, E->getLocation()); |
2197 | |
2198 | |
2199 | if (Param->hasAttr<PassObjectSizeAttr>()) { |
2200 | auto *POSParam = SizeArguments[Param]; |
2201 | (0) . __assert_fail ("POSParam && \"missing pass_object_size value for forwarding\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2201, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(POSParam && "missing pass_object_size value for forwarding"); |
2202 | EmitDelegateCallArg(Args, POSParam, E->getLocation()); |
2203 | } |
2204 | } |
2205 | } |
2206 | |
2207 | EmitCXXConstructorCall(D, Ctor_Base, ForVirtualBase, , |
2208 | This, Args, AggValueSlot::MayOverlap, |
2209 | E->getLocation(), ); |
2210 | } |
2211 | |
2212 | void CodeGenFunction::EmitInlinedInheritingCXXConstructorCall( |
2213 | const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase, |
2214 | bool Delegating, CallArgList &Args) { |
2215 | GlobalDecl GD(Ctor, CtorType); |
2216 | InlinedInheritingConstructorScope Scope(*this, GD); |
2217 | ApplyInlineDebugLocation DebugScope(*this, GD); |
2218 | RunCleanupsScope RunCleanups(*this); |
2219 | |
2220 | |
2221 | CXXInheritedCtorInitExprArgs = Args; |
2222 | |
2223 | FunctionArgList Params; |
2224 | QualType RetType = BuildFunctionArgList(CurGD, Params); |
2225 | FnRetTy = RetType; |
2226 | |
2227 | |
2228 | CGM.getCXXABI().addImplicitConstructorArgs(*this, Ctor, CtorType, |
2229 | ForVirtualBase, Delegating, Args); |
2230 | |
2231 | |
2232 | (0) . __assert_fail ("Args.size() >= Params.size() && \"too few arguments for call\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2232, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() >= Params.size() && "too few arguments for call"); |
2233 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
2234 | if (I < Params.size() && isa<ImplicitParamDecl>(Params[I])) { |
2235 | const RValue &RV = Args[I].getRValue(*this); |
2236 | (0) . __assert_fail ("!RV.isComplex() && \"complex indirect params not supported\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2236, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!RV.isComplex() && "complex indirect params not supported"); |
2237 | ParamValue Val = RV.isScalar() |
2238 | ? ParamValue::forDirect(RV.getScalarVal()) |
2239 | : ParamValue::forIndirect(RV.getAggregateAddress()); |
2240 | EmitParmDecl(*Params[I], Val, I + 1); |
2241 | } |
2242 | } |
2243 | |
2244 | |
2245 | |
2246 | |
2247 | if (!RetType->isVoidType()) |
2248 | ReturnValue = CreateIRTemp(RetType, "retval.inhctor"); |
2249 | |
2250 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
2251 | CXXThisValue = CXXABIThisValue; |
2252 | |
2253 | |
2254 | EmitCtorPrologue(Ctor, CtorType, Params); |
2255 | } |
2256 | |
2257 | void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &Vptr, Address This) { |
2258 | llvm::Value *VTableGlobal = |
2259 | CGM.getCXXABI().getVTableAddressPoint(Vptr.Base, Vptr.VTableClass); |
2260 | if (!VTableGlobal) |
2261 | return; |
2262 | |
2263 | |
2264 | CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset(); |
2265 | |
2266 | if (!NonVirtualOffset.isZero()) |
2267 | This = |
2268 | ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr, |
2269 | Vptr.VTableClass, Vptr.NearestVBase); |
2270 | |
2271 | llvm::Value *VPtrValue = |
2272 | GetVTablePtr(This, VTableGlobal->getType(), Vptr.VTableClass); |
2273 | llvm::Value *Cmp = |
2274 | Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables"); |
2275 | Builder.CreateAssumption(Cmp); |
2276 | } |
2277 | |
2278 | void CodeGenFunction::EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, |
2279 | Address This) { |
2280 | if (CGM.getCXXABI().doStructorsInitializeVPtrs(ClassDecl)) |
2281 | for (const VPtr &Vptr : getVTablePointers(ClassDecl)) |
2282 | EmitVTableAssumptionLoad(Vptr, This); |
2283 | } |
2284 | |
2285 | void |
2286 | CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
2287 | Address This, Address Src, |
2288 | const CXXConstructExpr *E) { |
2289 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
2290 | |
2291 | CallArgList Args; |
2292 | |
2293 | |
2294 | Args.add(RValue::get(This.getPointer()), D->getThisType()); |
2295 | |
2296 | |
2297 | QualType QT = *(FPT->param_type_begin()); |
2298 | llvm::Type *t = CGM.getTypes().ConvertType(QT); |
2299 | Src = Builder.CreateBitCast(Src, t); |
2300 | Args.add(RValue::get(Src.getPointer()), QT); |
2301 | |
2302 | |
2303 | EmitCallArgs(Args, FPT, drop_begin(E->arguments(), 1), E->getConstructor(), |
2304 | 1); |
2305 | |
2306 | EmitCXXConstructorCall(D, Ctor_Complete, , |
2307 | , This, Args, |
2308 | AggValueSlot::MayOverlap, E->getExprLoc(), |
2309 | ); |
2310 | } |
2311 | |
2312 | void |
2313 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
2314 | CXXCtorType CtorType, |
2315 | const FunctionArgList &Args, |
2316 | SourceLocation Loc) { |
2317 | CallArgList DelegateArgs; |
2318 | |
2319 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
2320 | (0) . __assert_fail ("I != E && \"no parameters to constructor\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2320, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I != E && "no parameters to constructor"); |
2321 | |
2322 | |
2323 | Address This = LoadCXXThisAddress(); |
2324 | DelegateArgs.add(RValue::get(This.getPointer()), (*I)->getType()); |
2325 | ++I; |
2326 | |
2327 | |
2328 | |
2329 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
2330 | (0) . __assert_fail ("I != E && \"cannot skip vtt parameter, already done with args\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2330, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I != E && "cannot skip vtt parameter, already done with args"); |
2331 | (0) . __assert_fail ("(*I)->getType()->isPointerType() && \"skipping parameter not of vtt type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2332, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((*I)->getType()->isPointerType() && |
2332 | (0) . __assert_fail ("(*I)->getType()->isPointerType() && \"skipping parameter not of vtt type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2332, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "skipping parameter not of vtt type"); |
2333 | ++I; |
2334 | } |
2335 | |
2336 | |
2337 | for (; I != E; ++I) { |
2338 | const VarDecl *param = *I; |
2339 | |
2340 | EmitDelegateCallArg(DelegateArgs, param, Loc); |
2341 | } |
2342 | |
2343 | EmitCXXConstructorCall(Ctor, CtorType, , |
2344 | , This, DelegateArgs, |
2345 | AggValueSlot::MayOverlap, Loc, |
2346 | ); |
2347 | } |
2348 | |
2349 | namespace { |
2350 | struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup { |
2351 | const CXXDestructorDecl *Dtor; |
2352 | Address Addr; |
2353 | CXXDtorType Type; |
2354 | |
2355 | CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr, |
2356 | CXXDtorType Type) |
2357 | : Dtor(D), Addr(Addr), Type(Type) {} |
2358 | |
2359 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
2360 | CGF.EmitCXXDestructorCall(Dtor, Type, , |
2361 | , Addr); |
2362 | } |
2363 | }; |
2364 | } |
2365 | |
2366 | void |
2367 | CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
2368 | const FunctionArgList &Args) { |
2369 | isDelegatingConstructor()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2369, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Ctor->isDelegatingConstructor()); |
2370 | |
2371 | Address ThisPtr = LoadCXXThisAddress(); |
2372 | |
2373 | AggValueSlot AggSlot = |
2374 | AggValueSlot::forAddr(ThisPtr, Qualifiers(), |
2375 | AggValueSlot::IsDestructed, |
2376 | AggValueSlot::DoesNotNeedGCBarriers, |
2377 | AggValueSlot::IsNotAliased, |
2378 | AggValueSlot::MayOverlap, |
2379 | AggValueSlot::IsNotZeroed, |
2380 | |
2381 | AggValueSlot::IsSanitizerChecked); |
2382 | |
2383 | EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot); |
2384 | |
2385 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
2386 | if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) { |
2387 | CXXDtorType Type = |
2388 | CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base; |
2389 | |
2390 | EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup, |
2391 | ClassDecl->getDestructor(), |
2392 | ThisPtr, Type); |
2393 | } |
2394 | } |
2395 | |
2396 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
2397 | CXXDtorType Type, |
2398 | bool ForVirtualBase, |
2399 | bool Delegating, |
2400 | Address This) { |
2401 | CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase, |
2402 | Delegating, This); |
2403 | } |
2404 | |
2405 | namespace { |
2406 | struct CallLocalDtor final : EHScopeStack::Cleanup { |
2407 | const CXXDestructorDecl *Dtor; |
2408 | Address Addr; |
2409 | |
2410 | CallLocalDtor(const CXXDestructorDecl *D, Address Addr) |
2411 | : Dtor(D), Addr(Addr) {} |
2412 | |
2413 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
2414 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
2415 | , |
2416 | , Addr); |
2417 | } |
2418 | }; |
2419 | } |
2420 | |
2421 | void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, |
2422 | Address Addr) { |
2423 | EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr); |
2424 | } |
2425 | |
2426 | void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) { |
2427 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
2428 | if (!ClassDecl) return; |
2429 | if (ClassDecl->hasTrivialDestructor()) return; |
2430 | |
2431 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
2432 | (0) . __assert_fail ("D && D->isUsed() && \"destructor not marked as used!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2432, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D && D->isUsed() && "destructor not marked as used!"); |
2433 | PushDestructorCleanup(D, Addr); |
2434 | } |
2435 | |
2436 | void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) { |
2437 | |
2438 | llvm::Value *VTableAddressPoint = |
2439 | CGM.getCXXABI().getVTableAddressPointInStructor( |
2440 | *this, Vptr.VTableClass, Vptr.Base, Vptr.NearestVBase); |
2441 | |
2442 | if (!VTableAddressPoint) |
2443 | return; |
2444 | |
2445 | |
2446 | llvm::Value *VirtualOffset = nullptr; |
2447 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
2448 | |
2449 | if (CGM.getCXXABI().isVirtualOffsetNeededForVTableField(*this, Vptr)) { |
2450 | |
2451 | |
2452 | |
2453 | VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset( |
2454 | *this, LoadCXXThisAddress(), Vptr.VTableClass, Vptr.NearestVBase); |
2455 | NonVirtualOffset = Vptr.OffsetFromNearestVBase; |
2456 | } else { |
2457 | |
2458 | NonVirtualOffset = Vptr.Base.getBaseOffset(); |
2459 | } |
2460 | |
2461 | |
2462 | Address VTableField = LoadCXXThisAddress(); |
2463 | |
2464 | if (!NonVirtualOffset.isZero() || VirtualOffset) |
2465 | VTableField = ApplyNonVirtualAndVirtualOffset( |
2466 | *this, VTableField, NonVirtualOffset, VirtualOffset, Vptr.VTableClass, |
2467 | Vptr.NearestVBase); |
2468 | |
2469 | |
2470 | |
2471 | llvm::Type *VTablePtrTy = |
2472 | llvm::FunctionType::get(CGM.Int32Ty, ) |
2473 | ->getPointerTo() |
2474 | ->getPointerTo(); |
2475 | VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo()); |
2476 | VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy); |
2477 | |
2478 | llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); |
2479 | TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTablePtrTy); |
2480 | CGM.DecorateInstructionWithTBAA(Store, TBAAInfo); |
2481 | if (CGM.getCodeGenOpts().OptimizationLevel > 0 && |
2482 | CGM.getCodeGenOpts().StrictVTablePointers) |
2483 | CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass); |
2484 | } |
2485 | |
2486 | CodeGenFunction::VPtrsVector |
2487 | CodeGenFunction::getVTablePointers(const CXXRecordDecl *VTableClass) { |
2488 | CodeGenFunction::VPtrsVector VPtrsResult; |
2489 | VisitedVirtualBasesSetTy VBases; |
2490 | getVTablePointers(BaseSubobject(VTableClass, CharUnits::Zero()), |
2491 | , |
2492 | CharUnits::Zero(), |
2493 | , VTableClass, VBases, |
2494 | VPtrsResult); |
2495 | return VPtrsResult; |
2496 | } |
2497 | |
2498 | void CodeGenFunction::getVTablePointers(BaseSubobject Base, |
2499 | const CXXRecordDecl *NearestVBase, |
2500 | CharUnits OffsetFromNearestVBase, |
2501 | bool BaseIsNonVirtualPrimaryBase, |
2502 | const CXXRecordDecl *VTableClass, |
2503 | VisitedVirtualBasesSetTy &VBases, |
2504 | VPtrsVector &Vptrs) { |
2505 | |
2506 | |
2507 | if (!BaseIsNonVirtualPrimaryBase) { |
2508 | |
2509 | VPtr Vptr = {Base, NearestVBase, OffsetFromNearestVBase, VTableClass}; |
2510 | Vptrs.push_back(Vptr); |
2511 | } |
2512 | |
2513 | const CXXRecordDecl *RD = Base.getBase(); |
2514 | |
2515 | |
2516 | for (const auto &I : RD->bases()) { |
2517 | CXXRecordDecl *BaseDecl |
2518 | = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
2519 | |
2520 | |
2521 | if (!BaseDecl->isDynamicClass()) |
2522 | continue; |
2523 | |
2524 | CharUnits BaseOffset; |
2525 | CharUnits BaseOffsetFromNearestVBase; |
2526 | bool BaseDeclIsNonVirtualPrimaryBase; |
2527 | |
2528 | if (I.isVirtual()) { |
2529 | |
2530 | if (!VBases.insert(BaseDecl).second) |
2531 | continue; |
2532 | |
2533 | const ASTRecordLayout &Layout = |
2534 | getContext().getASTRecordLayout(VTableClass); |
2535 | |
2536 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
2537 | BaseOffsetFromNearestVBase = CharUnits::Zero(); |
2538 | BaseDeclIsNonVirtualPrimaryBase = false; |
2539 | } else { |
2540 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
2541 | |
2542 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
2543 | BaseOffsetFromNearestVBase = |
2544 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
2545 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
2546 | } |
2547 | |
2548 | getVTablePointers( |
2549 | BaseSubobject(BaseDecl, BaseOffset), |
2550 | I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase, |
2551 | BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs); |
2552 | } |
2553 | } |
2554 | |
2555 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
2556 | |
2557 | if (!RD->isDynamicClass()) |
2558 | return; |
2559 | |
2560 | |
2561 | if (CGM.getCXXABI().doStructorsInitializeVPtrs(RD)) |
2562 | for (const VPtr &Vptr : getVTablePointers(RD)) |
2563 | InitializeVTablePointer(Vptr); |
2564 | |
2565 | if (RD->getNumVBases()) |
2566 | CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD); |
2567 | } |
2568 | |
2569 | llvm::Value *CodeGenFunction::GetVTablePtr(Address This, |
2570 | llvm::Type *VTableTy, |
2571 | const CXXRecordDecl *RD) { |
2572 | Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy); |
2573 | llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable"); |
2574 | TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTableTy); |
2575 | CGM.DecorateInstructionWithTBAA(VTable, TBAAInfo); |
2576 | |
2577 | if (CGM.getCodeGenOpts().OptimizationLevel > 0 && |
2578 | CGM.getCodeGenOpts().StrictVTablePointers) |
2579 | CGM.DecorateInstructionWithInvariantGroup(VTable, RD); |
2580 | |
2581 | return VTable; |
2582 | } |
2583 | |
2584 | |
2585 | |
2586 | |
2587 | |
2588 | |
2589 | |
2590 | |
2591 | |
2592 | |
2593 | static const CXXRecordDecl * |
2594 | LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD) { |
2595 | if (!RD->field_empty()) |
2596 | return RD; |
2597 | |
2598 | if (RD->getNumVBases() != 0) |
2599 | return RD; |
2600 | |
2601 | if (RD->getNumBases() != 1) |
2602 | return RD; |
2603 | |
2604 | for (const CXXMethodDecl *MD : RD->methods()) { |
2605 | if (MD->isVirtual()) { |
2606 | |
2607 | |
2608 | |
2609 | if (isa<CXXDestructorDecl>(MD) && MD->isImplicit()) |
2610 | continue; |
2611 | return RD; |
2612 | } |
2613 | } |
2614 | |
2615 | return LeastDerivedClassWithSameLayout( |
2616 | RD->bases_begin()->getType()->getAsCXXRecordDecl()); |
2617 | } |
2618 | |
2619 | void CodeGenFunction::EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, |
2620 | llvm::Value *VTable, |
2621 | SourceLocation Loc) { |
2622 | if (SanOpts.has(SanitizerKind::CFIVCall)) |
2623 | EmitVTablePtrCheckForCall(RD, VTable, CodeGenFunction::CFITCK_VCall, Loc); |
2624 | else if (CGM.getCodeGenOpts().WholeProgramVTables && |
2625 | CGM.HasHiddenLTOVisibility(RD)) { |
2626 | llvm::Metadata *MD = |
2627 | CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
2628 | llvm::Value *TypeId = |
2629 | llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD); |
2630 | |
2631 | llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
2632 | llvm::Value *TypeTest = |
2633 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test), |
2634 | {CastedVTable, TypeId}); |
2635 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::assume), TypeTest); |
2636 | } |
2637 | } |
2638 | |
2639 | void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, |
2640 | llvm::Value *VTable, |
2641 | CFITypeCheckKind TCK, |
2642 | SourceLocation Loc) { |
2643 | if (!SanOpts.has(SanitizerKind::CFICastStrict)) |
2644 | RD = LeastDerivedClassWithSameLayout(RD); |
2645 | |
2646 | EmitVTablePtrCheck(RD, VTable, TCK, Loc); |
2647 | } |
2648 | |
2649 | void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T, |
2650 | llvm::Value *Derived, |
2651 | bool MayBeNull, |
2652 | CFITypeCheckKind TCK, |
2653 | SourceLocation Loc) { |
2654 | if (!getLangOpts().CPlusPlus) |
2655 | return; |
2656 | |
2657 | auto *ClassTy = T->getAs<RecordType>(); |
2658 | if (!ClassTy) |
2659 | return; |
2660 | |
2661 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassTy->getDecl()); |
2662 | |
2663 | if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass()) |
2664 | return; |
2665 | |
2666 | if (!SanOpts.has(SanitizerKind::CFICastStrict)) |
2667 | ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl); |
2668 | |
2669 | llvm::BasicBlock *ContBlock = nullptr; |
2670 | |
2671 | if (MayBeNull) { |
2672 | llvm::Value *DerivedNotNull = |
2673 | Builder.CreateIsNotNull(Derived, "cast.nonnull"); |
2674 | |
2675 | llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check"); |
2676 | ContBlock = createBasicBlock("cast.cont"); |
2677 | |
2678 | Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock); |
2679 | |
2680 | EmitBlock(CheckBlock); |
2681 | } |
2682 | |
2683 | llvm::Value *VTable; |
2684 | std::tie(VTable, ClassDecl) = CGM.getCXXABI().LoadVTablePtr( |
2685 | *this, Address(Derived, getPointerAlign()), ClassDecl); |
2686 | |
2687 | EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc); |
2688 | |
2689 | if (MayBeNull) { |
2690 | Builder.CreateBr(ContBlock); |
2691 | EmitBlock(ContBlock); |
2692 | } |
2693 | } |
2694 | |
2695 | void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD, |
2696 | llvm::Value *VTable, |
2697 | CFITypeCheckKind TCK, |
2698 | SourceLocation Loc) { |
2699 | if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso && |
2700 | !CGM.HasHiddenLTOVisibility(RD)) |
2701 | return; |
2702 | |
2703 | SanitizerMask M; |
2704 | llvm::SanitizerStatKind SSK; |
2705 | switch (TCK) { |
2706 | case CFITCK_VCall: |
2707 | M = SanitizerKind::CFIVCall; |
2708 | SSK = llvm::SanStat_CFI_VCall; |
2709 | break; |
2710 | case CFITCK_NVCall: |
2711 | M = SanitizerKind::CFINVCall; |
2712 | SSK = llvm::SanStat_CFI_NVCall; |
2713 | break; |
2714 | case CFITCK_DerivedCast: |
2715 | M = SanitizerKind::CFIDerivedCast; |
2716 | SSK = llvm::SanStat_CFI_DerivedCast; |
2717 | break; |
2718 | case CFITCK_UnrelatedCast: |
2719 | M = SanitizerKind::CFIUnrelatedCast; |
2720 | SSK = llvm::SanStat_CFI_UnrelatedCast; |
2721 | break; |
2722 | case CFITCK_ICall: |
2723 | case CFITCK_NVMFCall: |
2724 | case CFITCK_VMFCall: |
2725 | llvm_unreachable("unexpected sanitizer kind"); |
2726 | } |
2727 | |
2728 | std::string TypeName = RD->getQualifiedNameAsString(); |
2729 | if (getContext().getSanitizerBlacklist().isBlacklistedType(M, TypeName)) |
2730 | return; |
2731 | |
2732 | SanitizerScope SanScope(this); |
2733 | EmitSanitizerStatReport(SSK); |
2734 | |
2735 | llvm::Metadata *MD = |
2736 | CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
2737 | llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); |
2738 | |
2739 | llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
2740 | llvm::Value *TypeTest = Builder.CreateCall( |
2741 | CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, TypeId}); |
2742 | |
2743 | llvm::Constant *StaticData[] = { |
2744 | llvm::ConstantInt::get(Int8Ty, TCK), |
2745 | EmitCheckSourceLocation(Loc), |
2746 | EmitCheckTypeDescriptor(QualType(RD->getTypeForDecl(), 0)), |
2747 | }; |
2748 | |
2749 | auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD); |
2750 | if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) { |
2751 | EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, CastedVTable, StaticData); |
2752 | return; |
2753 | } |
2754 | |
2755 | if (CGM.getCodeGenOpts().SanitizeTrap.has(M)) { |
2756 | EmitTrapCheck(TypeTest); |
2757 | return; |
2758 | } |
2759 | |
2760 | llvm::Value *AllVtables = llvm::MetadataAsValue::get( |
2761 | CGM.getLLVMContext(), |
2762 | llvm::MDString::get(CGM.getLLVMContext(), "all-vtables")); |
2763 | llvm::Value *ValidVtable = Builder.CreateCall( |
2764 | CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, AllVtables}); |
2765 | EmitCheck(std::make_pair(TypeTest, M), SanitizerHandler::CFICheckFail, |
2766 | StaticData, {CastedVTable, ValidVtable}); |
2767 | } |
2768 | |
2769 | bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) { |
2770 | if (!CGM.getCodeGenOpts().WholeProgramVTables || |
2771 | !SanOpts.has(SanitizerKind::CFIVCall) || |
2772 | !CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIVCall) || |
2773 | !CGM.HasHiddenLTOVisibility(RD)) |
2774 | return false; |
2775 | |
2776 | std::string TypeName = RD->getQualifiedNameAsString(); |
2777 | return !getContext().getSanitizerBlacklist().isBlacklistedType( |
2778 | SanitizerKind::CFIVCall, TypeName); |
2779 | } |
2780 | |
2781 | llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad( |
2782 | const CXXRecordDecl *RD, llvm::Value *VTable, uint64_t VTableByteOffset) { |
2783 | SanitizerScope SanScope(this); |
2784 | |
2785 | EmitSanitizerStatReport(llvm::SanStat_CFI_VCall); |
2786 | |
2787 | llvm::Metadata *MD = |
2788 | CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
2789 | llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD); |
2790 | |
2791 | llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
2792 | llvm::Value *CheckedLoad = Builder.CreateCall( |
2793 | CGM.getIntrinsic(llvm::Intrinsic::type_checked_load), |
2794 | {CastedVTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset), |
2795 | TypeId}); |
2796 | llvm::Value *CheckResult = Builder.CreateExtractValue(CheckedLoad, 1); |
2797 | |
2798 | EmitCheck(std::make_pair(CheckResult, SanitizerKind::CFIVCall), |
2799 | SanitizerHandler::CFICheckFail, nullptr, nullptr); |
2800 | |
2801 | return Builder.CreateBitCast( |
2802 | Builder.CreateExtractValue(CheckedLoad, 0), |
2803 | cast<llvm::PointerType>(VTable->getType())->getElementType()); |
2804 | } |
2805 | |
2806 | void CodeGenFunction::EmitForwardingCallToLambda( |
2807 | const CXXMethodDecl *callOperator, |
2808 | CallArgList &callArgs) { |
2809 | |
2810 | const CGFunctionInfo &calleeFnInfo = |
2811 | CGM.getTypes().arrangeCXXMethodDeclaration(callOperator); |
2812 | llvm::Constant *calleePtr = |
2813 | CGM.GetAddrOfFunction(GlobalDecl(callOperator), |
2814 | CGM.getTypes().GetFunctionType(calleeFnInfo)); |
2815 | |
2816 | |
2817 | const FunctionProtoType *FPT = |
2818 | callOperator->getType()->castAs<FunctionProtoType>(); |
2819 | QualType resultType = FPT->getReturnType(); |
2820 | ReturnValueSlot returnSlot; |
2821 | if (!resultType->isVoidType() && |
2822 | calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && |
2823 | !hasScalarEvaluationKind(calleeFnInfo.getReturnType())) |
2824 | returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified()); |
2825 | |
2826 | |
2827 | |
2828 | |
2829 | |
2830 | |
2831 | auto callee = CGCallee::forDirect(calleePtr, GlobalDecl(callOperator)); |
2832 | RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, callArgs); |
2833 | |
2834 | |
2835 | if (!resultType->isVoidType() && returnSlot.isNull()) { |
2836 | if (getLangOpts().ObjCAutoRefCount && resultType->isObjCRetainableType()) { |
2837 | RV = RValue::get(EmitARCRetainAutoreleasedReturnValue(RV.getScalarVal())); |
2838 | } |
2839 | EmitReturnOfRValue(RV, resultType); |
2840 | } else |
2841 | EmitBranchThroughCleanup(ReturnBlock); |
2842 | } |
2843 | |
2844 | void CodeGenFunction::EmitLambdaBlockInvokeBody() { |
2845 | const BlockDecl *BD = BlockInfo->getBlockDecl(); |
2846 | const VarDecl *variable = BD->capture_begin()->getVariable(); |
2847 | const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl(); |
2848 | const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
2849 | |
2850 | if (CallOp->isVariadic()) { |
2851 | |
2852 | |
2853 | |
2854 | CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function"); |
2855 | return; |
2856 | } |
2857 | |
2858 | |
2859 | CallArgList CallArgs; |
2860 | |
2861 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
2862 | Address ThisPtr = GetAddrOfBlockDecl(variable); |
2863 | CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType); |
2864 | |
2865 | |
2866 | for (auto param : BD->parameters()) |
2867 | EmitDelegateCallArg(CallArgs, param, param->getBeginLoc()); |
2868 | |
2869 | (0) . __assert_fail ("!Lambda->isGenericLambda() && \"generic lambda interconversion to block not implemented\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2870, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Lambda->isGenericLambda() && |
2870 | (0) . __assert_fail ("!Lambda->isGenericLambda() && \"generic lambda interconversion to block not implemented\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2870, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "generic lambda interconversion to block not implemented"); |
2871 | EmitForwardingCallToLambda(CallOp, CallArgs); |
2872 | } |
2873 | |
2874 | void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) { |
2875 | const CXXRecordDecl *Lambda = MD->getParent(); |
2876 | |
2877 | |
2878 | CallArgList CallArgs; |
2879 | |
2880 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
2881 | llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType)); |
2882 | CallArgs.add(RValue::get(ThisPtr), ThisType); |
2883 | |
2884 | |
2885 | for (auto Param : MD->parameters()) |
2886 | EmitDelegateCallArg(CallArgs, Param, Param->getBeginLoc()); |
2887 | |
2888 | const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
2889 | |
2890 | |
2891 | if (Lambda->isGenericLambda()) { |
2892 | isFunctionTemplateSpecialization()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGClass.cpp", 2892, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD->isFunctionTemplateSpecialization()); |
2893 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
2894 | FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate(); |
2895 | void *InsertPos = nullptr; |
2896 | FunctionDecl *CorrespondingCallOpSpecialization = |
2897 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
2898 | assert(CorrespondingCallOpSpecialization); |
2899 | CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
2900 | } |
2901 | EmitForwardingCallToLambda(CallOp, CallArgs); |
2902 | } |
2903 | |
2904 | void CodeGenFunction::EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD) { |
2905 | if (MD->isVariadic()) { |
2906 | |
2907 | |
2908 | CGM.ErrorUnsupported(MD, "lambda conversion to variadic function"); |
2909 | return; |
2910 | } |
2911 | |
2912 | EmitLambdaDelegatingInvokeBody(MD); |
2913 | } |
2914 | |