clang API Documentation
00001 //===------- MicrosoftCXXABI.cpp - AST support for the Microsoft C++ ABI --===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This provides C++ AST support targeting the Microsoft Visual C++ 00011 // ABI. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "CXXABI.h" 00016 #include "clang/AST/ASTContext.h" 00017 #include "clang/AST/Attr.h" 00018 #include "clang/AST/DeclCXX.h" 00019 #include "clang/AST/MangleNumberingContext.h" 00020 #include "clang/AST/RecordLayout.h" 00021 #include "clang/AST/Type.h" 00022 #include "clang/Basic/TargetInfo.h" 00023 00024 using namespace clang; 00025 00026 namespace { 00027 00028 /// \brief Numbers things which need to correspond across multiple TUs. 00029 /// Typically these are things like static locals, lambdas, or blocks. 00030 class MicrosoftNumberingContext : public MangleNumberingContext { 00031 llvm::DenseMap<const Type *, unsigned> ManglingNumbers; 00032 unsigned LambdaManglingNumber; 00033 unsigned StaticLocalNumber; 00034 00035 public: 00036 MicrosoftNumberingContext() 00037 : MangleNumberingContext(), LambdaManglingNumber(0), 00038 StaticLocalNumber(0) {} 00039 00040 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override { 00041 return ++LambdaManglingNumber; 00042 } 00043 00044 unsigned getManglingNumber(const BlockDecl *BD) override { 00045 const Type *Ty = nullptr; 00046 return ++ManglingNumbers[Ty]; 00047 } 00048 00049 unsigned getStaticLocalNumber(const VarDecl *VD) override { 00050 return ++StaticLocalNumber; 00051 } 00052 00053 unsigned getManglingNumber(const VarDecl *VD, 00054 unsigned MSLocalManglingNumber) override { 00055 return MSLocalManglingNumber; 00056 } 00057 00058 unsigned getManglingNumber(const TagDecl *TD, 00059 unsigned MSLocalManglingNumber) override { 00060 return MSLocalManglingNumber; 00061 } 00062 }; 00063 00064 class MicrosoftCXXABI : public CXXABI { 00065 ASTContext &Context; 00066 public: 00067 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { } 00068 00069 std::pair<uint64_t, unsigned> 00070 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override; 00071 00072 CallingConv getDefaultMethodCallConv(bool isVariadic) const override { 00073 if (!isVariadic && 00074 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) 00075 return CC_X86ThisCall; 00076 return CC_C; 00077 } 00078 00079 bool isNearlyEmpty(const CXXRecordDecl *RD) const override { 00080 // FIXME: Audit the corners 00081 if (!RD->isDynamicClass()) 00082 return false; 00083 00084 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 00085 00086 // In the Microsoft ABI, classes can have one or two vtable pointers. 00087 CharUnits PointerSize = 00088 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 00089 return Layout.getNonVirtualSize() == PointerSize || 00090 Layout.getNonVirtualSize() == PointerSize * 2; 00091 } 00092 00093 MangleNumberingContext *createMangleNumberingContext() const override { 00094 return new MicrosoftNumberingContext(); 00095 } 00096 }; 00097 } 00098 00099 // getNumBases() seems to only give us the number of direct bases, and not the 00100 // total. This function tells us if we inherit from anybody that uses MI, or if 00101 // we have a non-primary base class, which uses the multiple inheritance model. 00102 static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) { 00103 while (RD->getNumBases() > 0) { 00104 if (RD->getNumBases() > 1) 00105 return true; 00106 assert(RD->getNumBases() == 1); 00107 const CXXRecordDecl *Base = 00108 RD->bases_begin()->getType()->getAsCXXRecordDecl(); 00109 if (RD->isPolymorphic() && !Base->isPolymorphic()) 00110 return true; 00111 RD = Base; 00112 } 00113 return false; 00114 } 00115 00116 MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const { 00117 if (!hasDefinition() || isParsingBaseSpecifiers()) 00118 return MSInheritanceAttr::Keyword_unspecified_inheritance; 00119 if (getNumVBases() > 0) 00120 return MSInheritanceAttr::Keyword_virtual_inheritance; 00121 if (usesMultipleInheritanceModel(this)) 00122 return MSInheritanceAttr::Keyword_multiple_inheritance; 00123 return MSInheritanceAttr::Keyword_single_inheritance; 00124 } 00125 00126 MSInheritanceAttr::Spelling 00127 CXXRecordDecl::getMSInheritanceModel() const { 00128 MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>(); 00129 assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!"); 00130 return IA->getSemanticSpelling(); 00131 } 00132 00133 MSVtorDispAttr::Mode CXXRecordDecl::getMSVtorDispMode() const { 00134 if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>()) 00135 return VDA->getVtorDispMode(); 00136 return MSVtorDispAttr::Mode(getASTContext().getLangOpts().VtorDispMode); 00137 } 00138 00139 // Returns the number of pointer and integer slots used to represent a member 00140 // pointer in the MS C++ ABI. 00141 // 00142 // Member function pointers have the following general form; however, fields 00143 // are dropped as permitted (under the MSVC interpretation) by the inheritance 00144 // model of the actual class. 00145 // 00146 // struct { 00147 // // A pointer to the member function to call. If the member function is 00148 // // virtual, this will be a thunk that forwards to the appropriate vftable 00149 // // slot. 00150 // void *FunctionPointerOrVirtualThunk; 00151 // 00152 // // An offset to add to the address of the vbtable pointer after (possibly) 00153 // // selecting the virtual base but before resolving and calling the function. 00154 // // Only needed if the class has any virtual bases or bases at a non-zero 00155 // // offset. 00156 // int NonVirtualBaseAdjustment; 00157 // 00158 // // The offset of the vb-table pointer within the object. Only needed for 00159 // // incomplete types. 00160 // int VBPtrOffset; 00161 // 00162 // // An offset within the vb-table that selects the virtual base containing 00163 // // the member. Loading from this offset produces a new offset that is 00164 // // added to the address of the vb-table pointer to produce the base. 00165 // int VirtualBaseAdjustmentOffset; 00166 // }; 00167 static std::pair<unsigned, unsigned> 00168 getMSMemberPointerSlots(const MemberPointerType *MPT) { 00169 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 00170 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 00171 unsigned Ptrs = 0; 00172 unsigned Ints = 0; 00173 if (MPT->isMemberFunctionPointer()) 00174 Ptrs = 1; 00175 else 00176 Ints = 1; 00177 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), 00178 Inheritance)) 00179 Ints++; 00180 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 00181 Ints++; 00182 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 00183 Ints++; 00184 return std::make_pair(Ptrs, Ints); 00185 } 00186 00187 std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign( 00188 const MemberPointerType *MPT) const { 00189 const TargetInfo &Target = Context.getTargetInfo(); 00190 assert(Target.getTriple().getArch() == llvm::Triple::x86 || 00191 Target.getTriple().getArch() == llvm::Triple::x86_64); 00192 unsigned Ptrs, Ints; 00193 std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT); 00194 // The nominal struct is laid out with pointers followed by ints and aligned 00195 // to a pointer width if any are present and an int width otherwise. 00196 unsigned PtrSize = Target.getPointerWidth(0); 00197 unsigned IntSize = Target.getIntWidth(); 00198 uint64_t Width = Ptrs * PtrSize + Ints * IntSize; 00199 unsigned Align; 00200 00201 // When MSVC does x86_32 record layout, it aligns aggregate member pointers to 00202 // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for 00203 // function memptrs. 00204 if (Ptrs + Ints > 1 && Target.getTriple().getArch() == llvm::Triple::x86) 00205 Align = 8 * 8; 00206 else if (Ptrs) 00207 Align = Target.getPointerAlign(0); 00208 else 00209 Align = Target.getIntAlign(); 00210 00211 if (Target.getTriple().getArch() == llvm::Triple::x86_64) 00212 Width = llvm::RoundUpToAlignment(Width, Align); 00213 return std::make_pair(Width, Align); 00214 } 00215 00216 CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) { 00217 return new MicrosoftCXXABI(Ctx); 00218 } 00219