Branch data Line data Source code
1 : : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
5 : : // GCC is free software; you can redistribute it and/or modify it under
6 : : // the terms of the GNU General Public License as published by the Free
7 : : // Software Foundation; either version 3, or (at your option) any later
8 : : // version.
9 : :
10 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : : // for more details.
14 : :
15 : : // You should have received a copy of the GNU General Public License
16 : : // along with GCC; see the file COPYING3. If not see
17 : : // <http://www.gnu.org/licenses/>.
18 : :
19 : : #ifndef RUST_HIR_BASE_H
20 : : #define RUST_HIR_BASE_H
21 : :
22 : : #include "rust-system.h"
23 : : #include "rust-ast.h"
24 : : #include "rust-hir-visitable.h"
25 : : #include "rust-hir-attrs.h"
26 : :
27 : : #include "rust-token.h"
28 : :
29 : : #include "rust-location.h"
30 : :
31 : : #include "rust-hir-map.h"
32 : : #include "rust-diagnostics.h"
33 : : #include "rust-hir-bound.h"
34 : :
35 : : namespace Rust {
36 : :
37 : : typedef int TupleIndex;
38 : :
39 : : namespace HIR {
40 : :
41 : : // Item used in trait declarations - abstract base class
42 : 0 : class TraitItem : public Node, public FullVisitable
43 : : {
44 : : public:
45 : : using FullVisitable::accept_vis;
46 : : enum TraitItemKind
47 : : {
48 : : FUNC,
49 : : CONST,
50 : : TYPE
51 : : };
52 : :
53 : 0 : BaseKind get_hir_kind () override final { return TRAIT_ITEM; }
54 : :
55 : : protected:
56 : : // Constructor
57 : 3319 : TraitItem (Analysis::NodeMapping mappings) : mappings (mappings) {}
58 : :
59 : : // Clone function implementation as pure virtual method
60 : : virtual TraitItem *clone_trait_item_impl () const = 0;
61 : :
62 : : Analysis::NodeMapping mappings;
63 : :
64 : : public:
65 : : virtual ~TraitItem () {}
66 : :
67 : 0 : std::unique_ptr<TraitItem> clone_trait_item () const
68 : : {
69 : 0 : return std::unique_ptr<TraitItem> (clone_trait_item_impl ());
70 : : }
71 : :
72 : : virtual std::string to_string () const = 0;
73 : :
74 : : std::string to_debug_string () const
75 : : {
76 : : return to_string () + mappings.as_string ();
77 : : }
78 : :
79 : : virtual void accept_vis (HIRTraitItemVisitor &vis) = 0;
80 : :
81 : : virtual const std::string trait_identifier () const = 0;
82 : :
83 : 45032 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
84 : :
85 : : virtual location_t get_trait_locus () const = 0;
86 : :
87 : : virtual TraitItemKind get_item_kind () const = 0;
88 : :
89 : : virtual AST::AttrVec &get_outer_attrs () = 0;
90 : : virtual const AST::AttrVec &get_outer_attrs () const = 0;
91 : : };
92 : :
93 : 14914 : class ImplItem : public Node, public FullVisitable
94 : : {
95 : : public:
96 : : using FullVisitable::accept_vis;
97 : : enum ImplItemType
98 : : {
99 : : FUNCTION,
100 : : TYPE_ALIAS,
101 : : CONSTANT
102 : : };
103 : :
104 : : virtual ~ImplItem () {}
105 : :
106 : 0 : BaseKind get_hir_kind () override final { return IMPL; }
107 : :
108 : : // Unique pointer custom clone function
109 : 0 : std::unique_ptr<ImplItem> clone_inherent_impl_item () const
110 : : {
111 : 0 : return std::unique_ptr<ImplItem> (clone_inherent_impl_item_impl ());
112 : : }
113 : :
114 : : virtual std::string to_string () const = 0;
115 : :
116 : : std::string to_debug_string () const
117 : : {
118 : : return to_string () + get_impl_mappings ().as_string ();
119 : : }
120 : :
121 : : virtual void accept_vis (HIRImplVisitor &vis) = 0;
122 : : virtual void accept_vis (HIRStmtVisitor &vis) = 0;
123 : :
124 : : virtual Analysis::NodeMapping get_impl_mappings () const = 0;
125 : :
126 : : virtual location_t get_locus () const = 0;
127 : :
128 : : virtual ImplItemType get_impl_item_type () const = 0;
129 : :
130 : : virtual std::string get_impl_item_name () const = 0;
131 : :
132 : : protected:
133 : : // Clone function implementation as pure virtual method
134 : : virtual ImplItem *clone_inherent_impl_item_impl () const = 0;
135 : : };
136 : :
137 : : // A crate HIR object - holds all the data for a single compilation unit
138 : : class Crate : public WithInnerAttrs
139 : : {
140 : : // dodgy spacing required here
141 : : /* TODO: is it better to have a vector of items here or a module (implicit
142 : : * top-level one)? */
143 : : std::vector<std::unique_ptr<Item>> items;
144 : :
145 : : Analysis::NodeMapping mappings;
146 : :
147 : : public:
148 : : // Constructor
149 : : Crate (std::vector<std::unique_ptr<Item>> items, AST::AttrVec inner_attrs,
150 : : Analysis::NodeMapping mappings);
151 : :
152 : : // Copy constructor with vector clone
153 : : Crate (Crate const &other);
154 : :
155 : 10 : ~Crate () = default;
156 : :
157 : : // Overloaded assignment operator with vector clone
158 : : Crate &operator= (Crate const &other);
159 : :
160 : : // Move constructors
161 : : Crate (Crate &&other) = default;
162 : : Crate &operator= (Crate &&other) = default;
163 : :
164 : : // Get crate representation as string (e.g. for debugging).
165 : : std::string to_string () const;
166 : :
167 : : std::string to_debug_string () const;
168 : :
169 : 119826 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
170 : 53102 : std::vector<std::unique_ptr<Item>> &get_items () { return items; }
171 : : };
172 : :
173 : : } // namespace HIR
174 : : } // namespace Rust
175 : :
176 : : #endif
|