]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h
Merge llvm, clang, lld and lldb release_40 branch r292009. Also update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / DebugInfo / DWARF / DWARFAbbreviationDeclaration.h
1 //===-- DWARFAbbreviationDeclaration.h --------------------------*- C++ -*-===//
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 #ifndef LLVM_LIB_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
11 #define LLVM_LIB_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
12
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/Support/DataExtractor.h"
16 #include "llvm/Support/Dwarf.h"
17 namespace llvm {
18
19 class DWARFUnit;
20 class DWARFFormValue;
21 class raw_ostream;
22
23 class DWARFAbbreviationDeclaration {
24 public:
25   struct AttributeSpec {
26     AttributeSpec(dwarf::Attribute A, dwarf::Form F, Optional<int64_t> V)
27         : Attr(A), Form(F), ByteSizeOrValue(V) {}
28     dwarf::Attribute Attr;
29     dwarf::Form Form;
30     /// The following field is used for ByteSize for non-implicit_const
31     /// attributes and as value for implicit_const ones, indicated by
32     /// Form == DW_FORM_implicit_const.
33     /// The following cases are distinguished:
34     /// * Form != DW_FORM_implicit_const and ByteSizeOrValue has a value:
35     ///     ByteSizeOrValue contains the fixed size in bytes
36     ///     for the Form in this object.
37     /// * Form != DW_FORM_implicit_const and ByteSizeOrValue is None:
38     ///     byte size of Form either varies according to the DWARFUnit
39     ///     that it is contained in or the value size varies and must be
40     ///     decoded from the debug information in order to determine its size.
41     /// * Form == DW_FORM_implicit_const:
42     ///     ByteSizeOrValue contains value for the implicit_const attribute.
43     Optional<int64_t> ByteSizeOrValue;
44     bool isImplicitConst() const {
45       return Form == dwarf::DW_FORM_implicit_const;
46     }
47     /// Get the fixed byte size of this Form if possible. This function might
48     /// use the DWARFUnit to calculate the size of the Form, like for
49     /// DW_AT_address and DW_AT_ref_addr, so this isn't just an accessor for
50     /// the ByteSize member.
51     Optional<int64_t> getByteSize(const DWARFUnit &U) const;
52   };
53   typedef SmallVector<AttributeSpec, 8> AttributeSpecVector;
54
55   DWARFAbbreviationDeclaration();
56
57   uint32_t getCode() const { return Code; }
58   dwarf::Tag getTag() const { return Tag; }
59   bool hasChildren() const { return HasChildren; }
60
61   typedef iterator_range<AttributeSpecVector::const_iterator>
62   attr_iterator_range;
63
64   attr_iterator_range attributes() const {
65     return attr_iterator_range(AttributeSpecs.begin(), AttributeSpecs.end());
66   }
67
68   dwarf::Form getFormByIndex(uint32_t idx) const {
69     if (idx < AttributeSpecs.size())
70       return AttributeSpecs[idx].Form;
71     return dwarf::Form(0);
72   }
73
74   /// Get the index of the specified attribute.
75   ///
76   /// Searches the this abbreviation declaration for the index of the specified
77   /// attribute.
78   ///
79   /// \param attr DWARF attribute to search for.
80   /// \returns Optional index of the attribute if found, None otherwise.
81   Optional<uint32_t> findAttributeIndex(dwarf::Attribute attr) const;
82
83   /// Extract a DWARF form value from a DIE specified by DIE offset.
84   ///
85   /// Extract an attribute value for a DWARFUnit given the DIE offset and the
86   /// attribute.
87   ///
88   /// \param DIEOffset the DIE offset that points to the ULEB128 abbreviation
89   /// code in the .debug_info data.
90   /// \param Attr DWARF attribute to search for.
91   /// \param U the DWARFUnit the contains the DIE.
92   /// \returns Optional DWARF form value if the attribute was extracted.
93   Optional<DWARFFormValue> getAttributeValue(const uint32_t DIEOffset,
94                                              const dwarf::Attribute Attr,
95                                              const DWARFUnit &U) const;
96
97   bool extract(DataExtractor Data, uint32_t* OffsetPtr);
98   void dump(raw_ostream &OS) const;
99
100   // Return an optional byte size of all attribute data in this abbreviation
101   // if a constant byte size can be calculated given a DWARFUnit. This allows
102   // DWARF parsing to be faster as many DWARF DIEs have a fixed byte size.
103   Optional<size_t> getFixedAttributesByteSize(const DWARFUnit &U) const;
104
105 private:
106   void clear();
107
108   /// A helper structure that can quickly determine the size in bytes of an
109   /// abbreviation declaration.
110   struct FixedSizeInfo {
111     /// The fixed byte size for fixed size forms.
112     uint16_t NumBytes;
113     /// Number of DW_FORM_address forms in this abbrevation declaration.
114     uint8_t NumAddrs;
115     /// Number of DW_FORM_ref_addr forms in this abbrevation declaration.
116     uint8_t NumRefAddrs;
117     /// Number of 4 byte in DWARF32 and 8 byte in DWARF64 forms.
118     uint8_t NumDwarfOffsets;
119     /// Constructor
120     FixedSizeInfo()
121         : NumBytes(0), NumAddrs(0), NumRefAddrs(0), NumDwarfOffsets(0) {}
122     /// Calculate the fixed size in bytes given a DWARFUnit.
123     ///
124     /// \param U the DWARFUnit to use when determing the byte size.
125     /// \returns the size in bytes for all attribute data in this abbreviation.
126     /// The returned size does not include bytes for the  ULEB128 abbreviation
127     /// code
128     size_t getByteSize(const DWARFUnit &U) const;
129   };
130
131   uint32_t Code;
132   dwarf::Tag Tag;
133   uint8_t CodeByteSize;
134   bool HasChildren;
135   AttributeSpecVector AttributeSpecs;
136   /// If this abbreviation has a fixed byte size then FixedAttributeSize member
137   /// variable below will have a value.
138   Optional<FixedSizeInfo> FixedAttributeSize;
139 };
140
141 }
142
143 #endif