1 /* Implementation of class record_layout.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
27 #include "basic-block.h"
29 #include "diagnostic-core.h"
30 #include "diagnostic.h"
31 #include "tree-diagnostic.h"
32 #include "analyzer/analyzer.h"
33 #include "analyzer/record-layout.h"
39 /* class record_layout. */
41 record_layout::record_layout (tree record_type
)
43 gcc_assert (TREE_CODE (record_type
) == RECORD_TYPE
);
45 for (tree iter
= TYPE_FIELDS (record_type
); iter
!= NULL_TREE
;
46 iter
= DECL_CHAIN (iter
))
48 if (TREE_CODE (iter
) == FIELD_DECL
)
50 int iter_field_offset
= int_bit_position (iter
);
51 bit_size_t size_in_bits
;
52 if (!int_size_in_bits (TREE_TYPE (iter
), &size_in_bits
))
55 maybe_pad_to (iter_field_offset
);
58 m_items
.safe_push (item (bit_range (iter_field_offset
,
64 /* Add any trailing padding. */
65 bit_size_t size_in_bits
;
66 if (int_size_in_bits (record_type
, &size_in_bits
))
67 maybe_pad_to (size_in_bits
);
71 record_layout::dump_to_pp (pretty_printer
*pp
) const
75 FOR_EACH_VEC_ELT (m_items
, i
, it
)
83 record_layout::dump () const
86 pp_format_decoder (&pp
) = default_tree_printer
;
87 pp
.buffer
->stream
= stderr
;
92 const record_layout::item
*
93 record_layout::get_item_at (bit_offset_t offset
) const
97 FOR_EACH_VEC_ELT (m_items
, i
, it
)
98 if (it
->contains_p (offset
))
103 /* Subroutine of ctor. Add padding item to NEXT_OFFSET if necessary. */
106 record_layout::maybe_pad_to (bit_offset_t next_offset
)
108 if (m_items
.length () > 0)
110 const item
&last_item
= m_items
[m_items
.length () - 1];
111 bit_offset_t offset_after_last_item
112 = last_item
.get_next_bit_offset ();
113 if (next_offset
> offset_after_last_item
)
115 bit_size_t padding_size
116 = next_offset
- offset_after_last_item
;
117 m_items
.safe_push (item (bit_range (offset_after_last_item
,
119 last_item
.m_field
, true));
126 #endif /* #if ENABLE_ANALYZER */