Add C API bindings for DIBuilder 'Type' APIs
[lldb.git] / llvm / bindings / go / llvm / DIBuilderBindings.cpp
1 //===- DIBuilderBindings.cpp - Bindings for DIBuilder ---------------------===//
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 defines C bindings for the DIBuilder class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DIBuilderBindings.h"
15 #include "IRBindings.h"
16 #include "llvm/IR/DIBuilder.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Module.h"
19
20 using namespace llvm;
21
22 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
23                                                 LLVMMetadataRef Scope,
24                                                 LLVMMetadataRef File,
25                                                 unsigned Line,
26                                                 unsigned Column) {
27   DIBuilder *D = unwrap(Dref);
28   auto *LB = D->createLexicalBlock(unwrap<DILocalScope>(Scope),
29                                    unwrap<DIFile>(File), Line, Column);
30   return wrap(LB);
31 }
32
33 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
34                                                     LLVMMetadataRef Scope,
35                                                     LLVMMetadataRef File,
36                                                     unsigned Discriminator) {
37   DIBuilder *D = unwrap(Dref);
38   return wrap(D->createLexicalBlockFile(unwrap<DILocalScope>(Scope),
39                                         unwrap<DIFile>(File), Discriminator));
40 }
41
42 LLVMMetadataRef LLVMDIBuilderCreateFunction(
43     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
44     const char *LinkageName, LLVMMetadataRef File, unsigned Line,
45     LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
46     unsigned ScopeLine, unsigned Flags, int IsOptimized) {
47   DIBuilder *D = unwrap(Dref);
48   return wrap(D->createFunction(
49       unwrap<DIScope>(Scope), Name, LinkageName,
50       File ? unwrap<DIFile>(File) : nullptr, Line,
51       unwrap<DISubroutineType>(CompositeType), IsLocalToUnit, IsDefinition,
52       ScopeLine, static_cast<DINode::DIFlags>(Flags), IsOptimized));
53 }
54
55 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
56     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
57     LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
58     unsigned Flags, uint32_t AlignInBits) {
59   DIBuilder *D = unwrap(Dref);
60   return wrap(
61       D->createAutoVariable(unwrap<DIScope>(Scope), Name, unwrap<DIFile>(File),
62                             Line, unwrap<DIType>(Ty), AlwaysPreserve,
63                             static_cast<DINode::DIFlags>(Flags), AlignInBits));
64 }
65
66 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
67     LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
68     unsigned ArgNo, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
69     int AlwaysPreserve, unsigned Flags) {
70   DIBuilder *D = unwrap(Dref);
71   return wrap(D->createParameterVariable(
72       unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), Line,
73       unwrap<DIType>(Ty), AlwaysPreserve, static_cast<DINode::DIFlags>(Flags)));
74 }
75
76 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
77                                            LLVMMetadataRef Ty, const char *Name,
78                                            LLVMMetadataRef File, unsigned Line,
79                                            LLVMMetadataRef Context) {
80   DIBuilder *D = unwrap(Dref);
81   return wrap(D->createTypedef(unwrap<DIType>(Ty), Name,
82                                File ? unwrap<DIFile>(File) : nullptr, Line,
83                                Context ? unwrap<DIScope>(Context) : nullptr));
84 }
85
86 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
87                                                  int64_t Lo, int64_t Count) {
88   DIBuilder *D = unwrap(Dref);
89   return wrap(D->getOrCreateSubrange(Lo, Count));
90 }
91
92 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
93                                               LLVMMetadataRef *Data,
94                                               size_t Length) {
95   DIBuilder *D = unwrap(Dref);
96   Metadata **DataValue = unwrap(Data);
97   ArrayRef<Metadata *> Elements(DataValue, Length);
98   DINodeArray A = D->getOrCreateArray(Elements);
99   return wrap(A.get());
100 }
101
102 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
103                                                   LLVMMetadataRef *Data,
104                                                   size_t Length) {
105   DIBuilder *D = unwrap(Dref);
106   Metadata **DataValue = unwrap(Data);
107   ArrayRef<Metadata *> Elements(DataValue, Length);
108   DITypeRefArray A = D->getOrCreateTypeArray(Elements);
109   return wrap(A.get());
110 }
111
112 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
113                                               int64_t *Addr, size_t Length) {
114   DIBuilder *D = unwrap(Dref);
115   return wrap(D->createExpression(ArrayRef<int64_t>(Addr, Length)));
116 }
117
118 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
119                                              LLVMValueRef Storage,
120                                              LLVMMetadataRef VarInfo,
121                                              LLVMMetadataRef Expr,
122                                              LLVMBasicBlockRef Block) {
123   // Fail immediately here until the llgo folks update their bindings.  The
124   // called function is going to assert out anyway.
125   llvm_unreachable("DIBuilder API change requires a DebugLoc");
126
127   DIBuilder *D = unwrap(Dref);
128   Instruction *Instr = D->insertDeclare(
129       unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
130       unwrap<DIExpression>(Expr), /* DebugLoc */ nullptr, unwrap(Block));
131   return wrap(Instr);
132 }
133
134 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
135                                            LLVMValueRef Val,
136                                            LLVMMetadataRef VarInfo,
137                                            LLVMMetadataRef Expr,
138                                            LLVMBasicBlockRef Block) {
139   // Fail immediately here until the llgo folks update their bindings.  The
140   // called function is going to assert out anyway.
141   llvm_unreachable("DIBuilder API change requires a DebugLoc");
142
143   DIBuilder *D = unwrap(Dref);
144   Instruction *Instr = D->insertDbgValueIntrinsic(
145       unwrap(Val), unwrap<DILocalVariable>(VarInfo), unwrap<DIExpression>(Expr),
146       /* DebugLoc */ nullptr, unwrap(Block));
147   return wrap(Instr);
148 }