Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : 2504 : 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 as_string () const = 0;
73 : :
74 : : virtual void accept_vis (HIRTraitItemVisitor &vis) = 0;
75 : :
76 : : virtual const std::string trait_identifier () const = 0;
77 : :
78 : 34069 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
79 : :
80 : : virtual location_t get_trait_locus () const = 0;
81 : :
82 : : virtual TraitItemKind get_item_kind () const = 0;
83 : :
84 : : virtual AST::AttrVec &get_outer_attrs () = 0;
85 : : virtual const AST::AttrVec &get_outer_attrs () const = 0;
86 : : };
87 : :
88 : 14365 : class ImplItem : public Node, public FullVisitable
89 : : {
90 : : public:
91 : : using FullVisitable::accept_vis;
92 : : enum ImplItemType
93 : : {
94 : : FUNCTION,
95 : : TYPE_ALIAS,
96 : : CONSTANT
97 : : };
98 : :
99 : : virtual ~ImplItem () {}
100 : :
101 : 0 : BaseKind get_hir_kind () override final { return IMPL; }
102 : :
103 : : // Unique pointer custom clone function
104 : 0 : std::unique_ptr<ImplItem> clone_inherent_impl_item () const
105 : : {
106 : 0 : return std::unique_ptr<ImplItem> (clone_inherent_impl_item_impl ());
107 : : }
108 : :
109 : : virtual std::string as_string () const = 0;
110 : :
111 : : virtual void accept_vis (HIRImplVisitor &vis) = 0;
112 : : virtual void accept_vis (HIRStmtVisitor &vis) = 0;
113 : :
114 : : virtual Analysis::NodeMapping get_impl_mappings () const = 0;
115 : :
116 : : virtual location_t get_locus () const = 0;
117 : :
118 : : virtual ImplItemType get_impl_item_type () const = 0;
119 : :
120 : : virtual std::string get_impl_item_name () const = 0;
121 : :
122 : : protected:
123 : : // Clone function implementation as pure virtual method
124 : : virtual ImplItem *clone_inherent_impl_item_impl () const = 0;
125 : : };
126 : :
127 : : // A crate HIR object - holds all the data for a single compilation unit
128 : : class Crate : public WithInnerAttrs
129 : : {
130 : : // dodgy spacing required here
131 : : /* TODO: is it better to have a vector of items here or a module (implicit
132 : : * top-level one)? */
133 : : std::vector<std::unique_ptr<Item>> items;
134 : :
135 : : Analysis::NodeMapping mappings;
136 : :
137 : : public:
138 : : // Constructor
139 : : Crate (std::vector<std::unique_ptr<Item>> items, AST::AttrVec inner_attrs,
140 : : Analysis::NodeMapping mappings);
141 : :
142 : : // Copy constructor with vector clone
143 : : Crate (Crate const &other);
144 : :
145 : 10 : ~Crate () = default;
146 : :
147 : : // Overloaded assignment operator with vector clone
148 : : Crate &operator= (Crate const &other);
149 : :
150 : : // Move constructors
151 : : Crate (Crate &&other) = default;
152 : : Crate &operator= (Crate &&other) = default;
153 : :
154 : : // Get crate representation as string (e.g. for debugging).
155 : : std::string as_string () const;
156 : :
157 : 101241 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
158 : 51856 : std::vector<std::unique_ptr<Item>> &get_items () { return items; }
159 : : };
160 : :
161 : : } // namespace HIR
162 : : } // namespace Rust
163 : :
164 : : #endif
|