clang  6.0.0svn
ItaniumCXXABI.cpp
Go to the documentation of this file.
1 //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides C++ AST support targeting the Itanium C++ ABI, which is
11 // documented at:
12 // http://www.codesourcery.com/public/cxx-abi/abi.html
13 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
14 //
15 // It also supports the closely-related ARM C++ ABI, documented at:
16 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "CXXABI.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "clang/AST/Type.h"
26 #include "clang/Basic/TargetInfo.h"
27 
28 using namespace clang;
29 
30 namespace {
31 
32 /// According to Itanium C++ ABI 5.1.2:
33 /// the name of an anonymous union is considered to be
34 /// the name of the first named data member found by a pre-order,
35 /// depth-first, declaration-order walk of the data members of
36 /// the anonymous union.
37 /// If there is no such data member (i.e., if all of the data members
38 /// in the union are unnamed), then there is no way for a program to
39 /// refer to the anonymous union, and there is therefore no need to mangle its name.
40 ///
41 /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
42 static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
43  const RecordType *RT = VD.getType()->getAs<RecordType>();
44  assert(RT && "type of VarDecl is expected to be RecordType.");
45  assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
46  if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
47  return FD->getIdentifier();
48  }
49 
50  return nullptr;
51 }
52 
53 /// \brief Keeps track of the mangled names of lambda expressions and block
54 /// literals within a particular context.
55 class ItaniumNumberingContext : public MangleNumberingContext {
56  llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
57  llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
58  llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
59 
60 public:
61  unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
62  const FunctionProtoType *Proto =
63  CallOperator->getType()->getAs<FunctionProtoType>();
64  ASTContext &Context = CallOperator->getASTContext();
65 
67  EPI.Variadic = Proto->isVariadic();
68  QualType Key =
69  Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI);
70  Key = Context.getCanonicalType(Key);
71  return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
72  }
73 
74  unsigned getManglingNumber(const BlockDecl *BD) override {
75  const Type *Ty = nullptr;
76  return ++ManglingNumbers[Ty];
77  }
78 
79  unsigned getStaticLocalNumber(const VarDecl *VD) override {
80  return 0;
81  }
82 
83  /// Variable decls are numbered by identifier.
84  unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
85  const IdentifierInfo *Identifier = VD->getIdentifier();
86  if (!Identifier) {
87  // VarDecl without an identifier represents an anonymous union declaration.
88  Identifier = findAnonymousUnionVarDeclName(*VD);
89  }
90  return ++VarManglingNumbers[Identifier];
91  }
92 
93  unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
94  return ++TagManglingNumbers[TD->getIdentifier()];
95  }
96 };
97 
98 class ItaniumCXXABI : public CXXABI {
99 protected:
100  ASTContext &Context;
101 public:
102  ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
103 
104  std::pair<uint64_t, unsigned>
105  getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
106  const TargetInfo &Target = Context.getTargetInfo();
107  TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
108  uint64_t Width = Target.getTypeWidth(PtrDiff);
109  unsigned Align = Target.getTypeAlign(PtrDiff);
110  if (MPT->isMemberFunctionPointer())
111  Width = 2 * Width;
112  return std::make_pair(Width, Align);
113  }
114 
115  CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
116  const llvm::Triple &T = Context.getTargetInfo().getTriple();
117  if (!isVariadic && T.isWindowsGNUEnvironment() &&
118  T.getArch() == llvm::Triple::x86)
119  return CC_X86ThisCall;
120  return CC_C;
121  }
122 
123  // We cheat and just check that the class has a vtable pointer, and that it's
124  // only big enough to have a vtable pointer and nothing more (or less).
125  bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
126 
127  // Check that the class has a vtable pointer.
128  if (!RD->isDynamicClass())
129  return false;
130 
131  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
132  CharUnits PointerSize =
133  Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
134  return Layout.getNonVirtualSize() == PointerSize;
135  }
136 
137  const CXXConstructorDecl *
138  getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
139  return nullptr;
140  }
141 
142  void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
143  CXXConstructorDecl *CD) override {}
144 
145  void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
146  TypedefNameDecl *DD) override {}
147 
148  TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
149  return nullptr;
150  }
151 
152  void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
153  DeclaratorDecl *DD) override {}
154 
155  DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
156  return nullptr;
157  }
158 
159  std::unique_ptr<MangleNumberingContext>
160  createMangleNumberingContext() const override {
161  return llvm::make_unique<ItaniumNumberingContext>();
162  }
163 };
164 }
165 
167  return new ItaniumCXXABI(Ctx);
168 }
Defines the clang::ASTContext interface.
unsigned getTypeWidth(IntType T) const
Return the width (in bits) of the specified integer type enum.
Definition: TargetInfo.cpp:180
A (possibly-)qualified type.
Definition: Type.h:614
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:782
C Language Family Type Representation.
bool isVariadic() const
Definition: Type.h:3497
The base class of the type hierarchy.
Definition: Type.h:1300
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:645
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2344
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:771
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6099
Extra information about a function prototype.
Definition: Type.h:3289
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:232
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:311
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
ArrayRef< QualType > getParamTypes() const
Definition: Type.h:3398
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
Definition: Decl.h:2382
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
CXXABI * CreateItaniumCXXABI(ASTContext &Ctx)
Creates an instance of a C++ ABI class.
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3170
bool isDynamicClass() const
Definition: DeclCXX.h:714
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:649
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:34
Exposes information about the current target.
Definition: TargetInfo.h:55
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3587
const FieldDecl * findFirstNamedDataMember() const
Finds the first data member which has a name.
Definition: Decl.cpp:3996
const FunctionProtoType * T
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6162
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:30
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:233
unsigned getTypeAlign(IntType T) const
Return the alignment (in bits) of the specified integer type enum.
Definition: TargetInfo.cpp:251
RecordDecl * getDecl() const
Definition: Type.h:3848
IntType getPtrDiffType(unsigned AddrSpace) const
Definition: TargetInfo.h:248
QualType getCanonicalType() const
Definition: Type.h:5582
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2846
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:346
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1918
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2712
IntType
===-— Target Data Type Query Methods ----------------------------—===//
Definition: TargetInfo.h:128
Dataflow Directional Tag Classes.
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2440
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3839
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Represents a C++ struct/union/class.
Definition: DeclCXX.h:266
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition: Type.h:2463
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:189
Defines the clang::TargetInfo interface.
bool isUnion() const
Definition: Decl.h:3058
QualType getType() const
Definition: Decl.h:602