blob: 1aa7685b6293d443b0d32b27b1669871313ed87f [file] [log] [blame]
Devang Patel57c5a202010-11-04 15:01:38 +00001//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
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 file implements the DIBuilder.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth12664a02014-03-06 00:22:06 +000014#include "llvm/IR/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000017#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Module.h"
Eric Christopher34164192012-04-03 00:43:49 +000020#include "llvm/Support/Debug.h"
Devang Patel57c5a202010-11-04 15:01:38 +000021#include "llvm/Support/Dwarf.h"
22
23using namespace llvm;
24using namespace llvm::dwarf;
25
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000026namespace {
27class HeaderBuilder {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000028 /// \brief Whether there are any fields yet.
29 ///
30 /// Note that this is not equivalent to \c Chars.empty(), since \a concat()
31 /// may have been called already with an empty string.
32 bool IsEmpty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000033 SmallVector<char, 256> Chars;
34
35public:
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000036 HeaderBuilder() : IsEmpty(true) {}
37 HeaderBuilder(const HeaderBuilder &X) : IsEmpty(X.IsEmpty), Chars(X.Chars) {}
38 HeaderBuilder(HeaderBuilder &&X)
39 : IsEmpty(X.IsEmpty), Chars(std::move(X.Chars)) {}
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000040
41 template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000042 if (IsEmpty)
43 IsEmpty = false;
44 else
45 Chars.push_back(0);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000046 Twine(X).toVector(Chars);
47 return *this;
48 }
49
50 MDString *get(LLVMContext &Context) const {
51 return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
52 }
53
54 static HeaderBuilder get(unsigned Tag) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000055 return HeaderBuilder().concat("0x" + Twine::utohexstr(Tag));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000056 }
57};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000058}
Devang Patel63f83cd2010-12-07 23:58:00 +000059
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000060DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
Adrian Prantl18c073a2015-07-02 22:32:52 +000061 : M(m), VMContext(M.getContext()), CUNode(nullptr),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000062 DeclareFn(nullptr), ValueFn(nullptr),
63 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
64
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000066 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000067 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000068 if (N->isResolved())
69 return;
70
71 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
72 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000073}
Devang Patel57c5a202010-11-04 15:01:38 +000074
Devang Patel2b8acaf2011-08-15 23:00:00 +000075void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000076 if (!CUNode) {
77 assert(!AllowUnresolvedNodes &&
78 "creating type nodes without a CU is not supported");
79 return;
Devang Patel59e27c52011-08-19 23:28:12 +000080 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000081
Adrian Prantl0e224a62015-07-06 16:22:12 +000082 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
83
84 SmallVector<Metadata *, 16> RetainValues;
85 // Declarations and definitions of the same type may be retained. Some
86 // clients RAUW these pairs, leaving duplicates in the retained types
87 // list. Use a set to remove the duplicates while we transform the
88 // TrackingVHs back into Values.
89 SmallPtrSet<Metadata *, 16> RetainSet;
90 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
91 if (RetainSet.insert(AllRetainTypes[I]).second)
92 RetainValues.push_back(AllRetainTypes[I]);
93 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
94
95 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
96 CUNode->replaceSubprograms(SPs.get());
97 for (auto *SP : SPs) {
98 if (MDTuple *Temp = SP->getVariables().get()) {
99 const auto &PV = PreservedVariables.lookup(SP);
100 SmallVector<Metadata *, 4> Variables(PV.begin(), PV.end());
101 DINodeArray AV = getOrCreateArray(Variables);
102 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
103 }
104 }
105
106 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
107
108 CUNode->replaceImportedEntities(MDTuple::get(
109 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
110 AllImportedModules.end())));
111
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000112 // Now that all temp nodes have been replaced or deleted, resolve remaining
113 // cycles.
114 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000115 if (N && !N->isResolved())
116 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000117 UnresolvedNodes.clear();
118
119 // Can't handle unresolved nodes anymore.
120 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000121}
122
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000123/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000124static DIScope *getNonCompileUnitScope(DIScope *N) {
125 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000126 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000127 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000128}
129
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000130DICompileUnit *DIBuilder::createCompileUnit(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000131 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
132 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
Adrian Prantl1f599f92015-05-21 20:37:30 +0000133 DebugEmissionKind Kind, uint64_t DWOId, bool EmitDebugInfo) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000134
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000135 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000136 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
137 "Invalid Language tag");
138 assert(!Filename.empty() &&
139 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000140
Adrian Prantl18c073a2015-07-02 22:32:52 +0000141 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
142 CUNode = DICompileUnit::getDistinct(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000143 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
Adrian Prantl18c073a2015-07-02 22:32:52 +0000144 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr,
145 nullptr, nullptr, nullptr, nullptr, DWOId);
Devang Patel09fa69e2011-05-03 16:18:28 +0000146
147 // Create a named metadata so that it is easier to find cu in a module.
Diego Novillo56653fd2014-06-24 17:02:03 +0000148 // Note that we only generate this when the caller wants to actually
149 // emit debug information. When we are only interested in tracking
150 // source line locations throughout the backend, we prevent codegen from
151 // emitting debug info in the final output by not generating llvm.dbg.cu.
152 if (EmitDebugInfo) {
153 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
154 NMD->addOperand(CUNode);
155 }
Eric Christopher03b3e112013-07-19 00:51:47 +0000156
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000157 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000158 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000159}
160
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000161static DIImportedEntity *
162createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000163 Metadata *NS, unsigned Line, StringRef Name,
164 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000165 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000166 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000167 return M;
168}
169
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000170DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
171 DINamespace *NS,
172 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000173 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
174 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000175}
176
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000177DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
178 DIImportedEntity *NS,
179 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000180 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
181 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000182}
183
Adrian Prantlab1243f2015-06-29 23:03:47 +0000184DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
185 unsigned Line) {
186 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
187 Context, M, Line, StringRef(), AllImportedModules);
188}
189
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
191 DINode *Decl,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000192 unsigned Line,
193 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000194 // Make sure to use the unique identifier based metadata reference for
195 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000196 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000197 Context, DINodeRef::get(Decl), Line, Name,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000198 AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000199}
200
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000201DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory) {
202 return DIFile::get(VMContext, Filename, Directory);
Devang Patel57c5a202010-11-04 15:01:38 +0000203}
204
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000205DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000206 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000207 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000208}
209
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000210DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000211 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000212 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000213}
214
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000215DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000216 return createUnspecifiedType("decltype(nullptr)");
217}
218
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000220 uint64_t AlignInBits,
221 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000222 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000223 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000224 AlignInBits, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000225}
226
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000227DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
228 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
229 DITypeRef::get(FromTy), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000230}
231
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000232DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000233 uint64_t SizeInBits,
234 uint64_t AlignInBits,
235 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000236 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000237 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
238 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000239 SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000240}
241
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000242DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
243 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000244 uint64_t SizeInBits,
245 uint64_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000246 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
247 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
248 SizeInBits, AlignInBits, 0, 0,
249 DITypeRef::get(Base));
David Blaikie5d3249b2013-01-07 05:51:15 +0000250}
251
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000252DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000253 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000254 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
255 DITypeRef::get(RTy), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000256}
257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
259 DIFile *File, unsigned LineNo,
260 DIScope *Context) {
261 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000262 LineNo,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000263 DIScopeRef::get(getNonCompileUnitScope(Context)),
264 DITypeRef::get(Ty), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000265}
266
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000267DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000268 assert(Ty && "Invalid type!");
269 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000270 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0,
271 DITypeRef::get(Ty), DITypeRef::get(FriendTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000272 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000273}
274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000275DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000276 uint64_t BaseOffset,
277 unsigned Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000278 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000279 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
280 0, DITypeRef::get(Ty), DITypeRef::get(BaseTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000281 BaseOffset, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000282}
283
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000284DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
285 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000286 uint64_t SizeInBits,
287 uint64_t AlignInBits,
288 uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000289 unsigned Flags, DIType *Ty) {
290 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000291 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000292 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000293 SizeInBits, AlignInBits, OffsetInBits, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000294}
295
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000296static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000297 if (C)
298 return ConstantAsMetadata::get(C);
299 return nullptr;
300}
301
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000302DIDerivedType *DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name,
303 DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000304 unsigned LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000305 DIType *Ty, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000306 llvm::Constant *Val) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000307 Flags |= DINode::FlagStaticMember;
308 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000309 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000310 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty), 0, 0,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000311 0, Flags, getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000312}
313
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000314DIDerivedType *DIBuilder::createObjCIVar(StringRef Name, DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000315 unsigned LineNumber,
316 uint64_t SizeInBits,
317 uint64_t AlignInBits,
318 uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000319 DIType *Ty, MDNode *PropertyNode) {
320 return DIDerivedType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000321 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000322 DIScopeRef::get(getNonCompileUnitScope(File)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000323 SizeInBits, AlignInBits, OffsetInBits, Flags, PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000324}
325
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000326DIObjCProperty *
327DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000328 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000329 unsigned PropertyAttributes, DIType *Ty) {
330 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Adrian Prantl8ff53b32015-06-15 23:18:03 +0000331 SetterName, PropertyAttributes,
332 DITypeRef::get(Ty));
Devang Patelcc481592012-02-04 00:59:25 +0000333}
334
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000335DITemplateTypeParameter *
336DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
337 DIType *Ty) {
338 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
339 return DITemplateTypeParameter::get(VMContext, Name, DITypeRef::get(Ty));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000340}
341
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000342static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000343createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000344 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000345 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000346 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
347 return DITemplateValueParameter::get(VMContext, Tag, Name, DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000348 MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000349}
350
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000351DITemplateValueParameter *
352DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
353 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000354 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000355 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000356 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000357}
358
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000359DITemplateValueParameter *
360DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
361 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000362 return createTemplateValueParameterHelper(
363 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000364 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000365}
366
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000367DITemplateValueParameter *
368DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
369 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000370 return createTemplateValueParameterHelper(
371 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000372 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000373}
374
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000375DICompositeType *DIBuilder::createClassType(
376 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000377 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000378 unsigned Flags, DIType *DerivedFrom, DINodeArray Elements,
379 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
380 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000381 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000382
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000383 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000384 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000385 DIScopeRef::get(getNonCompileUnitScope(Context)),
386 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, OffsetInBits, Flags,
387 Elements, 0, DITypeRef::get(VTableHolder),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000388 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000389 if (!UniqueIdentifier.empty())
390 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000391 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000392 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000393}
394
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000395DICompositeType *DIBuilder::createStructType(
396 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000397 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000398 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
399 DIType *VTableHolder, StringRef UniqueIdentifier) {
400 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000401 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000402 DIScopeRef::get(getNonCompileUnitScope(Context)),
403 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, 0, Flags, Elements,
404 RunTimeLang, DITypeRef::get(VTableHolder), nullptr, UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000405 if (!UniqueIdentifier.empty())
406 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000407 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000408 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000409}
410
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000411DICompositeType *DIBuilder::createUnionType(
412 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smithaa861aa2015-04-21 20:07:38 +0000413 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000414 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
415 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000416 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000417 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000418 AlignInBits, 0, Flags, Elements, RunTimeLang, nullptr, nullptr,
419 UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000420 if (!UniqueIdentifier.empty())
421 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000422 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000423 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000424}
425
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000426DISubroutineType *DIBuilder::createSubroutineType(DIFile *File,
427 DITypeRefArray ParameterTypes,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000428 unsigned Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000429 return DISubroutineType::get(VMContext, Flags, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000430}
431
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000432DICompositeType *DIBuilder::createEnumerationType(
433 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
434 uint64_t SizeInBits, uint64_t AlignInBits, DINodeArray Elements,
435 DIType *UnderlyingType, StringRef UniqueIdentifier) {
436 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000437 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000438 DIScopeRef::get(getNonCompileUnitScope(Scope)),
439 DITypeRef::get(UnderlyingType), SizeInBits, AlignInBits, 0, 0, Elements,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000440 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000441 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000442 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000443 retainType(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000444 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000445 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000446}
447
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000448DICompositeType *DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
449 DIType *Ty,
450 DINodeArray Subscripts) {
451 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
452 nullptr, 0, nullptr, DITypeRef::get(Ty), Size,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000453 AlignInBits, 0, 0, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000454 trackIfUnresolved(R);
455 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000456}
457
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000458DICompositeType *DIBuilder::createVectorType(uint64_t Size,
459 uint64_t AlignInBits, DIType *Ty,
460 DINodeArray Subscripts) {
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000461 auto *R =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000462 DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0,
463 nullptr, DITypeRef::get(Ty), Size, AlignInBits, 0,
464 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000465 trackIfUnresolved(R);
466 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000467}
Devang Patel746660f2010-12-07 23:25:47 +0000468
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000469static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000470 unsigned FlagsToSet) {
471 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000472 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
473 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000474}
475
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000476DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000477 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000478 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000479 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000480 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000481}
Devang Patel746660f2010-12-07 23:25:47 +0000482
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000483DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000484 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000485 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000486 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487 unsigned Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000488 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000489}
490
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000491void DIBuilder::retainType(DIType *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000492 assert(T && "Expected non-null type");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000493 AllRetainTypes.emplace_back(T);
494}
Devang Patel89ea4f22010-12-08 01:50:15 +0000495
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000496DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000497
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000498DICompositeType *
499DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
500 DIFile *F, unsigned Line, unsigned RuntimeLang,
Eric Christopher98f9c232013-10-15 23:31:31 +0000501 uint64_t SizeInBits, uint64_t AlignInBits,
502 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000503 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
504 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000505 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000506 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000507 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
508 AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang, nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000509 nullptr, UniqueIdentifier);
David Blaikied3f094a2014-05-06 03:41:57 +0000510 if (!UniqueIdentifier.empty())
511 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000512 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000513 return RetTy;
514}
515
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000516DICompositeType *DIBuilder::createReplaceableCompositeType(
517 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
David Blaikied3f094a2014-05-06 03:41:57 +0000518 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000519 unsigned Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000520 auto *RetTy = DICompositeType::getTemporary(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000521 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000522 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000523 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000524 nullptr, nullptr, UniqueIdentifier)
525 .release();
Manman Ren0b410402013-08-29 23:17:54 +0000526 if (!UniqueIdentifier.empty())
527 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000528 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000529 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000530}
531
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000532DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000533 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000534}
535
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000536DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000537 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000538 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
539 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000540 Elts.push_back(DITypeRef::get(cast<DIType>(Elements[i])));
Manman Ren1a125c92014-07-28 19:33:20 +0000541 else
542 Elts.push_back(Elements[i]);
543 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000544 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000545}
546
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000547DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
548 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000549}
550
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000551static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000552#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000553 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000554 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000555 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000556 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000557#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000558}
559
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000560DIGlobalVariable *DIBuilder::createGlobalVariable(
561 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
562 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000563 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000564 checkGlobalVariableScope(Context);
565
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000566 auto *N = DIGlobalVariable::get(VMContext, cast_or_null<DIScope>(Context),
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000567 Name, LinkageName, F, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000568 DITypeRef::get(Ty), isLocalToUnit, true, Val,
569 cast_or_null<DIDerivedType>(Decl));
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000570 AllGVs.push_back(N);
571 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000572}
573
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000574DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
575 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
576 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000577 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000578 checkGlobalVariableScope(Context);
579
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000580 return DIGlobalVariable::getTemporary(
581 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
582 LineNumber, DITypeRef::get(Ty), isLocalToUnit, false, Val,
583 cast_or_null<DIDerivedType>(Decl))
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000584 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000585}
586
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000587DILocalVariable *DIBuilder::createLocalVariable(
588 unsigned Tag, DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo,
589 DIType *Ty, bool AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000590 // FIXME: Why getNonCompileUnitScope()?
591 // FIXME: Why is "!Context" okay here?
592 // FIXME: WHy doesn't this check for a subprogram or lexical block (AFAICT
593 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000594 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000595
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000596 auto *Node = DILocalVariable::get(
597 VMContext, Tag, cast_or_null<DILocalScope>(Context), Name, File, LineNo,
598 DITypeRef::get(Ty), ArgNo, Flags);
Devang Patel63f83cd2010-12-07 23:58:00 +0000599 if (AlwaysPreserve) {
600 // The optimizer may remove local variable. If there is an interest
601 // to preserve variable info in such situation then stash it in a
602 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000603 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000604 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000605 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000606 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000607 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000608}
609
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000610DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
611 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000612}
613
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000614DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000615 // TODO: Remove the callers of this signed version and delete.
616 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
617 return createExpression(Addr);
618}
619
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000620DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
621 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000622 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000623 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000624}
625
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000626DISubprogram *DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
627 StringRef LinkageName, DIFile *File,
628 unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith848af382015-04-20 18:20:03 +0000629 bool isLocalToUnit, bool isDefinition,
630 unsigned ScopeLine, unsigned Flags,
631 bool isOptimized, Function *Fn,
632 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000633 // dragonegg does not generate identifier for types, so using an empty map
634 // to resolve the context should be fine.
635 DITypeIdentifierMap EmptyMap;
636 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
637 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
638 Flags, isOptimized, Fn, TParams, Decl);
639}
640
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000641DISubprogram *DIBuilder::createFunction(DIScope *Context, StringRef Name,
642 StringRef LinkageName, DIFile *File,
643 unsigned LineNo, DISubroutineType *Ty,
644 bool isLocalToUnit, bool isDefinition,
645 unsigned ScopeLine, unsigned Flags,
646 bool isOptimized, Function *Fn,
647 MDNode *TParams, MDNode *Decl) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000648 assert(Ty->getTag() == dwarf::DW_TAG_subroutine_type &&
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000649 "function types should be subroutines");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000650 auto *Node = DISubprogram::get(
651 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
652 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
653 nullptr, 0, 0, Flags, isOptimized, Fn, cast_or_null<MDTuple>(TParams),
654 cast_or_null<DISubprogram>(Decl),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000655 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000656
657 if (isDefinition)
658 AllSubprograms.push_back(Node);
659 trackIfUnresolved(Node);
660 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000661}
662
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000663DISubprogram *DIBuilder::createTempFunctionFwdDecl(
664 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
665 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
666 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
667 Function *Fn, MDNode *TParams, MDNode *Decl) {
668 return DISubprogram::getTemporary(
669 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
670 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition,
671 ScopeLine, nullptr, 0, 0, Flags, isOptimized, Fn,
672 cast_or_null<MDTuple>(TParams), cast_or_null<DISubprogram>(Decl),
673 nullptr)
674 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000675}
676
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000677DISubprogram *
678DIBuilder::createMethod(DIScope *Context, StringRef Name, StringRef LinkageName,
679 DIFile *F, unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000680 bool isLocalToUnit, bool isDefinition, unsigned VK,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000681 unsigned VIndex, DIType *VTableHolder, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000682 bool isOptimized, Function *Fn, MDNode *TParam) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000683 assert(Ty->getTag() == dwarf::DW_TAG_subroutine_type &&
David Blaikie5174c842013-05-22 23:22:18 +0000684 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +0000685 assert(getNonCompileUnitScope(Context) &&
686 "Methods should have both a Context and a context that isn't "
687 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000688 // FIXME: Do we want to use different scope/lines?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000689 auto *SP = DISubprogram::get(
690 VMContext, DIScopeRef::get(cast<DIScope>(Context)), Name, LinkageName, F,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000691 LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000692 DITypeRef::get(VTableHolder), VK, VIndex, Flags, isOptimized, Fn,
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000693 cast_or_null<MDTuple>(TParam), nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000694
David Blaikie595eb442013-02-18 07:10:22 +0000695 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000696 AllSubprograms.push_back(SP);
697 trackIfUnresolved(SP);
698 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000699}
700
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000701DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
702 DIFile *File, unsigned LineNo) {
703 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000704 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000705}
706
Adrian Prantlab1243f2015-06-29 23:03:47 +0000707DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
708 StringRef ConfigurationMacros,
709 StringRef IncludePath,
710 StringRef ISysRoot) {
711 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
712 ConfigurationMacros, IncludePath, ISysRoot);
713}
714
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000715DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
716 DIFile *File,
717 unsigned Discriminator) {
718 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000719}
720
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000721DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
722 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000723 // Make these distinct, to avoid merging two lexical blocks on the same
724 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000725 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000726 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000727}
728
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000729static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
730 assert(V && "no value passed to dbg intrinsic");
731 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
732}
733
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000734static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
735 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000736 return I;
737}
738
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000739Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
740 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000741 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000742 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000743 assert(DL && "Expected debug loc");
744 assert(DL->getScope()->getSubprogram() ==
745 VarInfo->getScope()->getSubprogram() &&
746 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000747 if (!DeclareFn)
748 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
749
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000750 trackIfUnresolved(VarInfo);
751 trackIfUnresolved(Expr);
752 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
753 MetadataAsValue::get(VMContext, VarInfo),
754 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000755 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000756}
757
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000758Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
759 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000760 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000761 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000762 assert(DL && "Expected debug loc");
763 assert(DL->getScope()->getSubprogram() ==
764 VarInfo->getScope()->getSubprogram() &&
765 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000766 if (!DeclareFn)
767 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
768
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000769 trackIfUnresolved(VarInfo);
770 trackIfUnresolved(Expr);
771 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
772 MetadataAsValue::get(VMContext, VarInfo),
773 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000774
775 // If this block already has a terminator then insert this intrinsic
776 // before the terminator.
777 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000778 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
779 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000780}
781
Devang Patel9b412732011-02-22 18:56:12 +0000782Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000783 DILocalVariable *VarInfo,
784 DIExpression *Expr,
785 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000786 Instruction *InsertBefore) {
787 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000788 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000789 assert(DL && "Expected debug loc");
790 assert(DL->getScope()->getSubprogram() ==
791 VarInfo->getScope()->getSubprogram() &&
792 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000793 if (!ValueFn)
794 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
795
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000796 trackIfUnresolved(VarInfo);
797 trackIfUnresolved(Expr);
798 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
799 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
800 MetadataAsValue::get(VMContext, VarInfo),
801 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000802 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000803}
804
Devang Patel9b412732011-02-22 18:56:12 +0000805Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000806 DILocalVariable *VarInfo,
807 DIExpression *Expr,
808 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000809 BasicBlock *InsertAtEnd) {
810 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000811 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000812 assert(DL && "Expected debug loc");
813 assert(DL->getScope()->getSubprogram() ==
814 VarInfo->getScope()->getSubprogram() &&
815 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000816 if (!ValueFn)
817 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
818
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000819 trackIfUnresolved(VarInfo);
820 trackIfUnresolved(Expr);
821 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
822 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
823 MetadataAsValue::get(VMContext, VarInfo),
824 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000825
826 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000827}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000828
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000829void DIBuilder::replaceVTableHolder(DICompositeType *&T,
830 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000831 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000832 TypedTrackingMDRef<DICompositeType> N(T);
833 N->replaceVTableHolder(DITypeRef::get(VTableHolder));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000834 T = N.get();
835 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000836
837 // If this didn't create a self-reference, just return.
838 if (T != VTableHolder)
839 return;
840
Adrian Prantl18a25b02015-02-11 17:45:10 +0000841 // Look for unresolved operands. T will drop RAUW support, orphaning any
842 // cycles underneath it.
843 if (T->isResolved())
844 for (const MDOperand &O : T->operands())
845 if (auto *N = dyn_cast_or_null<MDNode>(O))
846 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000847}
848
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000849void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
850 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000851 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000852 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000853 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000854 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000855 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000856 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000857 T = N.get();
858 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000859
860 // If T isn't resolved, there's no problem.
861 if (!T->isResolved())
862 return;
863
864 // If "T" is resolved, it may be due to a self-reference cycle. Track the
865 // arrays explicitly if they're unresolved, or else the cycles will be
866 // orphaned.
867 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000868 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000869 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000870 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000871}