1 //===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This provides an abstract class for C++ code generation. Concrete subclasses 10 // of this implement code generation for specific C++ ABIs. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CGCXXABI.h" 15 #include "CGCleanup.h" 16 #include "clang/AST/Attr.h" 17 18 using namespace clang; 19 using namespace CodeGen; 20 21 CGCXXABI::~CGCXXABI() { } 22 23 void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S) { 24 DiagnosticsEngine &Diags = CGF.CGM.getDiags(); 25 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 26 "cannot yet compile %0 in this ABI"); 27 Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()), 28 DiagID) 29 << S; 30 } 31 32 llvm::Constant *CGCXXABI::GetBogusMemberPointer(QualType T) { 33 return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T)); 34 } 35 36 llvm::Type * 37 CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 38 return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 39 } 40 41 CGCallee CGCXXABI::EmitLoadOfMemberFunctionPointer( 42 CodeGenFunction &CGF, const Expr *E, Address This, 43 llvm::Value *&ThisPtrForCall, 44 llvm::Value *MemPtr, const MemberPointerType *MPT) { 45 ErrorUnsupportedABI(CGF, "calls through member pointers"); 46 47 ThisPtrForCall = This.getPointer(); 48 const auto *FPT = MPT->getPointeeType()->castAs<FunctionProtoType>(); 49 const auto *RD = 50 cast<CXXRecordDecl>(MPT->getClass()->castAs<RecordType>()->getDecl()); 51 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType( 52 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr)); 53 llvm::Constant *FnPtr = llvm::Constant::getNullValue(FTy->getPointerTo()); 54 return CGCallee::forDirect(FnPtr, FPT); 55 } 56 57 llvm::Value * 58 CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, 59 Address Base, llvm::Value *MemPtr, 60 const MemberPointerType *MPT) { 61 ErrorUnsupportedABI(CGF, "loads of member pointers"); 62 llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType()) 63 ->getPointerTo(Base.getAddressSpace()); 64 return llvm::Constant::getNullValue(Ty); 65 } 66 67 llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 68 const CastExpr *E, 69 llvm::Value *Src) { 70 ErrorUnsupportedABI(CGF, "member function pointer conversions"); 71 return GetBogusMemberPointer(E->getType()); 72 } 73 74 llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E, 75 llvm::Constant *Src) { 76 return GetBogusMemberPointer(E->getType()); 77 } 78 79 llvm::Value * 80 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 81 llvm::Value *L, 82 llvm::Value *R, 83 const MemberPointerType *MPT, 84 bool Inequality) { 85 ErrorUnsupportedABI(CGF, "member function pointer comparison"); 86 return CGF.Builder.getFalse(); 87 } 88 89 llvm::Value * 90 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 91 llvm::Value *MemPtr, 92 const MemberPointerType *MPT) { 93 ErrorUnsupportedABI(CGF, "member function pointer null testing"); 94 return CGF.Builder.getFalse(); 95 } 96 97 llvm::Constant * 98 CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 99 return GetBogusMemberPointer(QualType(MPT, 0)); 100 } 101 102 llvm::Constant *CGCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) { 103 return GetBogusMemberPointer(CGM.getContext().getMemberPointerType( 104 MD->getType(), MD->getParent()->getTypeForDecl())); 105 } 106 107 llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 108 CharUnits offset) { 109 return GetBogusMemberPointer(QualType(MPT, 0)); 110 } 111 112 llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) { 113 return GetBogusMemberPointer(MPT); 114 } 115 116 bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 117 // Fake answer. 118 return true; 119 } 120 121 void CGCXXABI::buildThisParam(CodeGenFunction &CGF, FunctionArgList ¶ms) { 122 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 123 124 // FIXME: I'm not entirely sure I like using a fake decl just for code 125 // generation. Maybe we can come up with a better way? 126 auto *ThisDecl = ImplicitParamDecl::Create( 127 CGM.getContext(), nullptr, MD->getLocation(), 128 &CGM.getContext().Idents.get("this"), MD->getThisType(), 129 ImplicitParamDecl::CXXThis); 130 params.push_back(ThisDecl); 131 CGF.CXXABIThisDecl = ThisDecl; 132 133 // Compute the presumed alignment of 'this', which basically comes 134 // down to whether we know it's a complete object or not. 135 auto &Layout = CGF.getContext().getASTRecordLayout(MD->getParent()); 136 if (MD->getParent()->getNumVBases() == 0 || // avoid vcall in common case 137 MD->getParent()->isEffectivelyFinal() || 138 isThisCompleteObject(CGF.CurGD)) { 139 CGF.CXXABIThisAlignment = Layout.getAlignment(); 140 } else { 141 CGF.CXXABIThisAlignment = Layout.getNonVirtualAlignment(); 142 } 143 } 144 145 llvm::Value *CGCXXABI::loadIncomingCXXThis(CodeGenFunction &CGF) { 146 return CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)), 147 "this"); 148 } 149 150 void CGCXXABI::setCXXABIThisValue(CodeGenFunction &CGF, llvm::Value *ThisPtr) { 151 /// Initialize the 'this' slot. 152 assert(getThisDecl(CGF) && "no 'this' variable for function"); 153 CGF.CXXABIThisValue = ThisPtr; 154 } 155 156 bool CGCXXABI::mayNeedDestruction(const VarDecl *VD) const { 157 if (VD->needsDestruction(getContext())) 158 return true; 159 160 // If the variable has an incomplete class type (or array thereof), it 161 // might need destruction. 162 const Type *T = VD->getType()->getBaseElementTypeUnsafe(); 163 if (T->getAs<RecordType>() && T->isIncompleteType()) 164 return true; 165 166 return false; 167 } 168 169 bool CGCXXABI::isEmittedWithConstantInitializer( 170 const VarDecl *VD, bool InspectInitForWeakDef) const { 171 VD = VD->getMostRecentDecl(); 172 if (VD->hasAttr<ConstInitAttr>()) 173 return true; 174 175 // All later checks examine the initializer specified on the variable. If 176 // the variable is weak, such examination would not be correct. 177 if (!InspectInitForWeakDef && (VD->isWeak() || VD->hasAttr<SelectAnyAttr>())) 178 return false; 179 180 const VarDecl *InitDecl = VD->getInitializingDeclaration(); 181 if (!InitDecl) 182 return false; 183 184 // If there's no initializer to run, this is constant initialization. 185 if (!InitDecl->hasInit()) 186 return true; 187 188 // If we have the only definition, we don't need a thread wrapper if we 189 // will emit the value as a constant. 190 if (isUniqueGVALinkage(getContext().GetGVALinkageForVariable(VD))) 191 return !mayNeedDestruction(VD) && InitDecl->evaluateValue(); 192 193 // Otherwise, we need a thread wrapper unless we know that every 194 // translation unit will emit the value as a constant. We rely on the 195 // variable being constant-initialized in every translation unit if it's 196 // constant-initialized in any translation unit, which isn't actually 197 // guaranteed by the standard but is necessary for sanity. 198 return InitDecl->hasConstantInitialization(); 199 } 200 201 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 202 RValue RV, QualType ResultType) { 203 assert(!CGF.hasAggregateEvaluationKind(ResultType) && 204 "cannot handle aggregates"); 205 CGF.EmitReturnOfRValue(RV, ResultType); 206 } 207 208 CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) { 209 if (!requiresArrayCookie(expr)) 210 return CharUnits::Zero(); 211 return getArrayCookieSizeImpl(expr->getAllocatedType()); 212 } 213 214 CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) { 215 // BOGUS 216 return CharUnits::Zero(); 217 } 218 219 Address CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 220 Address NewPtr, 221 llvm::Value *NumElements, 222 const CXXNewExpr *expr, 223 QualType ElementType) { 224 // Should never be called. 225 ErrorUnsupportedABI(CGF, "array cookie initialization"); 226 return Address::invalid(); 227 } 228 229 bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, 230 QualType elementType) { 231 // If the class's usual deallocation function takes two arguments, 232 // it needs a cookie. 233 if (expr->doesUsualArrayDeleteWantSize()) 234 return true; 235 236 return elementType.isDestructedType(); 237 } 238 239 bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { 240 // If the class's usual deallocation function takes two arguments, 241 // it needs a cookie. 242 if (expr->doesUsualArrayDeleteWantSize()) 243 return true; 244 245 return expr->getAllocatedType().isDestructedType(); 246 } 247 248 void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, Address ptr, 249 const CXXDeleteExpr *expr, QualType eltTy, 250 llvm::Value *&numElements, 251 llvm::Value *&allocPtr, CharUnits &cookieSize) { 252 // Derive a char* in the same address space as the pointer. 253 ptr = CGF.Builder.CreateElementBitCast(ptr, CGF.Int8Ty); 254 255 // If we don't need an array cookie, bail out early. 256 if (!requiresArrayCookie(expr, eltTy)) { 257 allocPtr = ptr.getPointer(); 258 numElements = nullptr; 259 cookieSize = CharUnits::Zero(); 260 return; 261 } 262 263 cookieSize = getArrayCookieSizeImpl(eltTy); 264 Address allocAddr = 265 CGF.Builder.CreateConstInBoundsByteGEP(ptr, -cookieSize); 266 allocPtr = allocAddr.getPointer(); 267 numElements = readArrayCookieImpl(CGF, allocAddr, cookieSize); 268 } 269 270 llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 271 Address ptr, 272 CharUnits cookieSize) { 273 ErrorUnsupportedABI(CGF, "reading a new[] cookie"); 274 return llvm::ConstantInt::get(CGF.SizeTy, 0); 275 } 276 277 /// Returns the adjustment, in bytes, required for the given 278 /// member-pointer operation. Returns null if no adjustment is 279 /// required. 280 llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) { 281 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 282 E->getCastKind() == CK_BaseToDerivedMemberPointer); 283 284 QualType derivedType; 285 if (E->getCastKind() == CK_DerivedToBaseMemberPointer) 286 derivedType = E->getSubExpr()->getType(); 287 else 288 derivedType = E->getType(); 289 290 const CXXRecordDecl *derivedClass = 291 derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl(); 292 293 return CGM.GetNonVirtualBaseClassOffset(derivedClass, 294 E->path_begin(), 295 E->path_end()); 296 } 297 298 llvm::BasicBlock * 299 CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 300 const CXXRecordDecl *RD) { 301 if (CGM.getTarget().getCXXABI().hasConstructorVariants()) 302 llvm_unreachable("shouldn't be called in this ABI"); 303 304 ErrorUnsupportedABI(CGF, "complete object detection in ctor"); 305 return nullptr; 306 } 307 308 void CGCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue *GV, 309 const CXXDestructorDecl *Dtor, 310 CXXDtorType DT) const { 311 // Assume the base C++ ABI has no special rules for destructor variants. 312 CGM.setDLLImportDLLExport(GV, Dtor); 313 } 314 315 llvm::GlobalValue::LinkageTypes CGCXXABI::getCXXDestructorLinkage( 316 GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const { 317 // Delegate back to CGM by default. 318 return CGM.getLLVMLinkageForDeclarator(Dtor, Linkage, 319 /*IsConstantVariable=*/false); 320 } 321 322 bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD) { 323 return false; 324 } 325 326 llvm::CallInst * 327 CGCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF, 328 llvm::Value *Exn) { 329 // Just call std::terminate and ignore the violating exception. 330 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn()); 331 } 332 333 CatchTypeInfo CGCXXABI::getCatchAllTypeInfo() { 334 return CatchTypeInfo{nullptr, 0}; 335 } 336 337 std::vector<CharUnits> CGCXXABI::getVBPtrOffsets(const CXXRecordDecl *RD) { 338 return std::vector<CharUnits>(); 339 } 340 341 CGCXXABI::AddedStructorArgCounts CGCXXABI::addImplicitConstructorArgs( 342 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, 343 bool ForVirtualBase, bool Delegating, CallArgList &Args) { 344 AddedStructorArgs AddedArgs = 345 getImplicitConstructorArgs(CGF, D, Type, ForVirtualBase, Delegating); 346 for (size_t i = 0; i < AddedArgs.Prefix.size(); ++i) { 347 Args.insert(Args.begin() + 1 + i, 348 CallArg(RValue::get(AddedArgs.Prefix[i].Value), 349 AddedArgs.Prefix[i].Type)); 350 } 351 for (const auto &arg : AddedArgs.Suffix) { 352 Args.add(RValue::get(arg.Value), arg.Type); 353 } 354 return AddedStructorArgCounts(AddedArgs.Prefix.size(), 355 AddedArgs.Suffix.size()); 356 } 357