1//===- SyntheticTypeNameBuilder.h -------------------------------*- C++ -*-===//
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#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_SYNTHETICTYPENAMEBUILDER_H
10#define LLVM_LIB_DWARFLINKER_PARALLEL_SYNTHETICTYPENAMEBUILDER_H
11
12#include "DWARFLinkerCompileUnit.h"
13#include "DWARFLinkerGlobalData.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/SmallVector.h"
16
17namespace llvm {
18class DWARFDebugInfoEntry;
19
20namespace dwarf_linker {
21namespace parallel {
22struct LinkContext;
23class CompileUnit;
24
25/// The helper class to build type name based on DIE properties.
26/// It builds synthetic name based on explicit attributes: DW_AT_name,
27/// DW_AT_linkage_name or based on implicit attributes(DW_AT_decl*).
28/// Names for specific DIEs(like subprograms, template classes...) include
29/// additional attributes: subprogram parameters, template parameters,
30/// array ranges. Examples of built name:
31///
32/// class A { } : {8}A
33///
34/// namspace llvm { class A { } } : {1}llvm{8}A
35///
36/// template <int> structure B { } : {F}B<{0}int>
37///
38/// void foo ( int p1, float p3 ) : {a}void foo({0}int, {0}int)
39///
40/// int *ptr; : {c}ptr {0}int
41///
42/// int var; : {d}var
43///
44/// These names is used to refer DIEs describing types.
45class SyntheticTypeNameBuilder {
46public:
47 SyntheticTypeNameBuilder(TypePool &TypePoolRef) : TypePoolRef(TypePoolRef) {}
48
49 /// Create synthetic name for the specified DIE \p InputUnitEntryPair
50 /// and assign created name to the DIE type info. \p ChildIndex is used
51 /// to create name for ordered DIEs(function arguments f.e.).
52 Error assignName(UnitEntryPairTy InputUnitEntryPair,
53 std::optional<std::pair<size_t, size_t>> ChildIndex);
54
55protected:
56 /// Add array type dimension.
57 void addArrayDimension(UnitEntryPairTy InputUnitEntryPair);
58
59 /// Add signature( entry type plus type of parameters plus type of template
60 /// parameters(if \p addTemplateParameters is true).
61 Error addSignature(UnitEntryPairTy InputUnitEntryPair,
62 bool addTemplateParameters);
63
64 /// Add specified \p FunctionParameters to the built name.
65 Error addParamNames(
66 CompileUnit &CU,
67 SmallVector<const DWARFDebugInfoEntry *, 20> &FunctionParameters);
68
69 /// Add specified \p TemplateParameters to the built name.
70 Error addTemplateParamNames(
71 CompileUnit &CU,
72 SmallVector<const DWARFDebugInfoEntry *, 10> &TemplateParameters);
73
74 /// Analyze \p InputUnitEntryPair's ODR attributes and put names
75 /// of the referenced type dies to the built name.
76 Error addReferencedODRDies(UnitEntryPairTy InputUnitEntryPair,
77 bool AssignNameToTypeDescriptor,
78 ArrayRef<dwarf::Attribute> ODRAttrs);
79
80 /// Resolve the DIE that \p Attr of \p InputUnitEntryPair refers to.
81 /// \returns std::nullopt if there is nothing to follow.
82 Expected<std::optional<UnitEntryPairTy>>
83 resolveReference(UnitEntryPairTy InputUnitEntryPair, dwarf::Attribute Attr);
84
85 /// Count a referenced DIE toward the recursion limit. Callers decrement
86 /// RecursionDepth once they are done with it.
87 Error incrementRecursionDepth();
88
89 /// Add names of parent dies to the built name.
90 Error addParentName(UnitEntryPairTy &InputUnitEntryPair);
91
92 /// \returns synthetic name of the specified \p DieEntry.
93 /// The name is constructed from the dwarf::DW_AT_decl_file
94 /// and dwarf::DW_AT_decl_line attributes.
95 void addDieNameFromDeclFileAndDeclLine(UnitEntryPairTy &InputUnitEntryPair,
96 bool &HasDeclFileName);
97
98 /// Add a fixed-size digest of the contents of the unnamed type \p
99 /// InputUnitEntryPair, so the names of the types referring to it do not grow
100 /// with its size or nesting depth.
101 Error addUnnamedTypeContents(UnitEntryPairTy InputUnitEntryPair);
102
103 /// Add the type \p InputUnitEntryPair refers to, without parent scopes. A
104 /// type nested in an unnamed type would need the name being built.
105 Error addUnqualifiedTypeName(UnitEntryPairTy InputUnitEntryPair);
106
107 /// Add type prefix to the built name.
108 void addTypePrefix(CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry);
109
110 /// Add type name to the built name.
111 Error addTypeName(UnitEntryPairTy InputUnitEntryPair, bool AddParentNames);
112
113 /// Analyze \p InputUnitEntryPair for the type name and possibly assign
114 /// built type name to the DIE's type info.
115 /// NOTE: while analyzing types we may create different kind of names
116 /// for the same type depending on whether the type is part of another type.
117 /// f.e. DW_TAG_formal_parameter would receive "{02}01" name when
118 /// examined alone. Or "{0}int" name when it is a part of a function name:
119 /// {a}void foo({0}int). The \p AssignNameToTypeDescriptor tells whether
120 /// the type name is part of another type name and then should not be assigned
121 /// to DIE type descriptor.
122 Error addDIETypeName(UnitEntryPairTy InputUnitEntryPair,
123 std::optional<std::pair<size_t, size_t>> ChildIndex,
124 bool AssignNameToTypeDescriptor);
125
126 /// Add ordered name to the built name.
127 void addOrderedName(std::pair<size_t, size_t> ChildIdx);
128
129 /// Add value name to the built name.
130 void addValueName(UnitEntryPairTy InputUnitEntryPair, dwarf::Attribute Attr);
131
132 /// Buffer keeping bult name.
133 TypeKey SyntheticName;
134
135 /// Digests of the contents of unnamed types, by DIE.
136 DenseMap<const DWARFDebugInfoEntry *, std::string> ContentsDigests;
137
138 /// Recursion counter
139 size_t RecursionDepth = 0;
140
141 /// Bounds RecursionDepth, which malformed input can make unbounded.
142 static constexpr size_t MaxRecursionDepth = 1000;
143
144 /// Type pool
145 TypePool &TypePoolRef;
146};
147
148/// This class helps to assign indexes for DIE children.
149/// Indexes are used to create type name for children which
150/// should be presented in the original order(function parameters,
151/// array dimensions, enumeration members, class/structure members).
152class OrderedChildrenIndexAssigner {
153public:
154 OrderedChildrenIndexAssigner(CompileUnit &CU,
155 const DWARFDebugInfoEntry *DieEntry);
156
157 /// Returns index of the specified child and width of hexadecimal
158 /// representation.
159 std::optional<std::pair<size_t, size_t>>
160 getChildIndex(CompileUnit &CU, const DWARFDebugInfoEntry *ChildDieEntry);
161
162protected:
163 using OrderedChildrenIndexesArrayTy = std::array<size_t, 8>;
164
165 std::optional<size_t> tagToArrayIndex(CompileUnit &CU,
166 const DWARFDebugInfoEntry *DieEntry);
167
168 bool NeedCountChildren = false;
169 OrderedChildrenIndexesArrayTy OrderedChildIdxs = {0};
170 OrderedChildrenIndexesArrayTy ChildIndexesWidth = {0};
171};
172
173} // end of namespace parallel
174} // end of namespace dwarf_linker
175} // end of namespace llvm
176
177#endif // LLVM_LIB_DWARFLINKER_PARALLEL_SYNTHETICTYPENAMEBUILDER_H
178