Automatic date update in version.in
[binutils-gdb.git] / gdb / dwarf2 / attribute.h
blob447d195eb9a4baf936a0e7bf8c2292b209637b87
1 /* DWARF attributes
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #ifndef GDB_DWARF2_ATTRIBUTE_H
28 #define GDB_DWARF2_ATTRIBUTE_H
30 #include "dwarf2.h"
31 #include "gdbtypes.h"
32 #include "gdbsupport/gdb_optional.h"
34 /* Blocks are a bunch of untyped bytes. */
35 struct dwarf_block
37 size_t size;
39 /* Valid only if SIZE is not zero. */
40 const gdb_byte *data;
43 /* Attributes have a name and a value. */
44 struct attribute
46 /* Read the given attribute value as an address, taking the
47 attribute's form into account. */
48 CORE_ADDR as_address () const;
50 /* If the attribute has a string form, return the string value;
51 otherwise return NULL. */
52 const char *as_string () const;
54 /* Return the block value. The attribute must have block form. */
55 dwarf_block *as_block () const
57 gdb_assert (form_is_block ());
58 return u.blk;
61 /* Return the signature. The attribute must have signature
62 form. */
63 ULONGEST as_signature () const
65 gdb_assert (form == DW_FORM_ref_sig8);
66 return u.signature;
69 /* Return the signed value. The attribute must have the appropriate
70 form. */
71 LONGEST as_signed () const
73 gdb_assert (form_is_signed ());
74 return u.snd;
77 /* Return the unsigned value, but only for attributes requiring
78 reprocessing. */
79 ULONGEST as_unsigned_reprocess () const
81 gdb_assert (form_requires_reprocessing ());
82 gdb_assert (requires_reprocessing);
83 return u.unsnd;
86 /* Return the unsigned value. Requires that the form be an unsigned
87 form, and that reprocessing not be needed. */
88 ULONGEST as_unsigned () const
90 gdb_assert (form_is_unsigned ());
91 gdb_assert (!requires_reprocessing);
92 return u.unsnd;
95 /* Return true if the value is nonnegative. Requires that that
96 reprocessing not be needed. */
97 bool is_nonnegative () const
99 if (form_is_unsigned ())
100 return true;
101 if (form_is_signed ())
102 return as_signed () >= 0;
103 return false;
106 /* Return the nonnegative value. Requires that that reprocessing not be
107 needed. */
108 ULONGEST as_nonnegative () const
110 if (form_is_unsigned ())
111 return as_unsigned ();
112 if (form_is_signed ())
113 return (ULONGEST)as_signed ();
114 gdb_assert (false);
117 /* Return non-zero if ATTR's value is a section offset --- classes
118 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
119 You may use the as_unsigned method to retrieve such offsets.
121 Section 7.5.4, "Attribute Encodings", explains that no attribute
122 may have a value that belongs to more than one of these classes; it
123 would be ambiguous if we did, because we use the same forms for all
124 of them. */
126 bool form_is_section_offset () const;
128 /* Return non-zero if ATTR's value falls in the 'constant' class, or
129 zero otherwise. When this function returns true, you can apply
130 the constant_value method to it.
132 However, note that for some attributes you must check
133 attr_form_is_section_offset before using this test. DW_FORM_data4
134 and DW_FORM_data8 are members of both the constant class, and of
135 the classes that contain offsets into other debug sections
136 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
137 that, if an attribute's can be either a constant or one of the
138 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
139 taken as section offsets, not constants.
141 DW_FORM_data16 is not considered as constant_value cannot handle
142 that. */
144 bool form_is_constant () const;
146 /* The address is always stored already as sect_offset; despite for
147 the forms besides DW_FORM_ref_addr it is stored as cu_offset in
148 the DWARF file. */
150 bool form_is_ref () const
152 return (form == DW_FORM_ref_addr
153 || form == DW_FORM_ref1
154 || form == DW_FORM_ref2
155 || form == DW_FORM_ref4
156 || form == DW_FORM_ref8
157 || form == DW_FORM_ref_udata
158 || form == DW_FORM_GNU_ref_alt);
161 /* Check if the attribute's form is a DW_FORM_block*
162 if so return true else false. */
164 bool form_is_block () const;
166 /* Check if the attribute's form is a string form. */
167 bool form_is_string () const;
169 /* Check if the attribute's form is an unsigned integer form. */
170 bool form_is_unsigned () const;
172 /* Check if the attribute's form is a signed integer form. */
173 bool form_is_signed () const;
175 /* Check if the attribute's form is a form that requires
176 "reprocessing". */
177 bool form_requires_reprocessing () const;
179 /* Return DIE offset of this attribute. Return 0 with complaint if
180 the attribute is not of the required kind. */
182 sect_offset get_ref_die_offset () const
184 if (form_is_ref ())
185 return (sect_offset) u.unsnd;
186 get_ref_die_offset_complaint ();
187 return {};
190 /* Return the constant value held by this attribute. Return
191 DEFAULT_VALUE if the value held by the attribute is not
192 constant. */
194 LONGEST constant_value (int default_value) const;
196 /* Return true if this attribute holds a canonical string. In some
197 cases, like C++ names, gdb will rewrite the name of a DIE to a
198 canonical form. This makes lookups robust when a name can be
199 spelled different ways (e.g., "signed" or "signed int"). This
200 flag indicates whether the value has been canonicalized. */
201 bool canonical_string_p () const
203 gdb_assert (form_is_string ());
204 return string_is_canonical;
207 /* Initialize this attribute to hold a non-canonical string
208 value. */
209 void set_string_noncanonical (const char *str)
211 gdb_assert (form_is_string ());
212 u.str = str;
213 string_is_canonical = 0;
214 requires_reprocessing = 0;
217 /* Set the canonical string value for this attribute. */
218 void set_string_canonical (const char *str)
220 gdb_assert (form_is_string ());
221 u.str = str;
222 string_is_canonical = 1;
225 /* Set the block value for this attribute. */
226 void set_block (dwarf_block *blk)
228 gdb_assert (form_is_block ());
229 u.blk = blk;
232 /* Set the signature value for this attribute. */
233 void set_signature (ULONGEST signature)
235 gdb_assert (form == DW_FORM_ref_sig8);
236 u.signature = signature;
239 /* Set this attribute to a signed integer. */
240 void set_signed (LONGEST snd)
242 gdb_assert (form == DW_FORM_sdata || form == DW_FORM_implicit_const);
243 u.snd = snd;
246 /* Set this attribute to an unsigned integer. */
247 void set_unsigned (ULONGEST unsnd)
249 gdb_assert (form_is_unsigned ());
250 u.unsnd = unsnd;
251 requires_reprocessing = 0;
254 /* Temporarily set this attribute to an unsigned integer. This is
255 used only for those forms that require reprocessing. */
256 void set_unsigned_reprocess (ULONGEST unsnd)
258 gdb_assert (form_requires_reprocessing ());
259 u.unsnd = unsnd;
260 requires_reprocessing = 1;
263 /* Set this attribute to an address. */
264 void set_address (CORE_ADDR addr)
266 gdb_assert (form == DW_FORM_addr
267 || ((form == DW_FORM_addrx
268 || form == DW_FORM_GNU_addr_index)
269 && requires_reprocessing));
270 u.addr = addr;
271 requires_reprocessing = 0;
274 /* True if this attribute requires reprocessing. */
275 bool requires_reprocessing_p () const
277 return requires_reprocessing;
280 /* Return the value as one of the recognized enum
281 dwarf_defaulted_attribute constants according to DWARF5 spec,
282 Table 7.24. If the value is incorrect, or if this attribute has
283 the wrong form, then a complaint is issued and DW_DEFAULTED_no is
284 returned. */
285 dwarf_defaulted_attribute defaulted () const;
287 /* Return the attribute's value as a dwarf_virtuality_attribute
288 constant according to DWARF spec. An unrecognized value will
289 issue a complaint and return DW_VIRTUALITY_none. */
290 dwarf_virtuality_attribute as_virtuality () const;
292 /* Return the attribute's value as a boolean. An unrecognized form
293 will issue a complaint and return false. */
294 bool as_boolean () const;
296 ENUM_BITFIELD(dwarf_attribute) name : 15;
298 /* A boolean that is used for forms that require reprocessing. A
299 form may require data not directly available in the attribute.
300 E.g., DW_FORM_strx requires the corresponding
301 DW_AT_str_offsets_base. In this case, the processing for the
302 attribute must be done in two passes. In the first past, this
303 flag is set and the value is an unsigned. In the second pass,
304 the unsigned value is turned into the correct value for the form,
305 and this flag is cleared. This flag is unused for other
306 forms. */
307 unsigned int requires_reprocessing : 1;
309 ENUM_BITFIELD(dwarf_form) form : 15;
311 /* Has u.str already been updated by dwarf2_canonicalize_name? This
312 field should be in u.str but it is kept here for better struct
313 attribute alignment. */
314 unsigned int string_is_canonical : 1;
316 union
318 const char *str;
319 struct dwarf_block *blk;
320 ULONGEST unsnd;
321 LONGEST snd;
322 CORE_ADDR addr;
323 ULONGEST signature;
327 private:
329 /* Used by get_ref_die_offset to issue a complaint. */
331 void get_ref_die_offset_complaint () const;
334 #endif /* GDB_DWARF2_ATTRIBUTE_H */