Line data Source code
1 : // Copyright (C) 2025-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_DERIVE_CMP_COMMON_H
20 : #define RUST_DERIVE_CMP_COMMON_H
21 :
22 : #include "rust-ast.h"
23 : #include "rust-ast-builder.h"
24 : #include "rust-item.h"
25 : #include "rust-path.h"
26 :
27 : namespace Rust {
28 : namespace AST {
29 :
30 : /**
31 : * A pair of two expressions from each instance being compared. E.g. this
32 : * could be `self.0` and `other.0`, or `self.field` and `other.field`
33 : */
34 1185 : struct SelfOther
35 : {
36 : std::unique_ptr<Expr> self_expr;
37 : std::unique_ptr<Expr> other_expr;
38 :
39 : /* Create a <self.i> and an <other.i> expression */
40 : static SelfOther index (Builder builder, int idx);
41 : static std::vector<SelfOther> indexes (Builder builder,
42 : const std::vector<TupleField> &fields);
43 :
44 : /* Create a <self.field> and an <other.field> expression */
45 : static SelfOther field (Builder builder, const std::string &field_name);
46 : static std::vector<SelfOther> fields (Builder builder,
47 : const std::vector<StructField> &fields);
48 : };
49 :
50 : /**
51 : * Builder for common match cases used when comparing two enum instances. This
52 : * builder takes care of creating the unique patterns for the `self` instance
53 : * and `other` instance, as well as the entire `MatchCase` required for building
54 : * a proper comparision expression for an implementation of a comparision trait
55 : * for an enum type. The functions take a lambda to use when creating the
56 : * expression of the generated `MatchCase`.
57 : */
58 111 : class EnumMatchBuilder
59 : {
60 : public:
61 : /**
62 : * The type of functions to call when creating the resulting expression in the
63 : * generated `MatchCase`
64 : */
65 : using ExprFn
66 : = std::function<std::unique_ptr<Expr> (std::vector<SelfOther> &&)>;
67 :
68 111 : EnumMatchBuilder (const std::string &enum_path,
69 : const std::string &variant_path, ExprFn fn,
70 : Builder &builder)
71 111 : : enum_path (enum_path), variant_path (variant_path), fn (fn),
72 111 : builder (builder)
73 : {}
74 :
75 : /**
76 : * Generate a `MatchCase` for an enum tuple variant
77 : *
78 : * (&Enum::Tuple(self0, self1), &Enum::Tuple(other0, other1)) => <fn>
79 : */
80 : MatchCase tuple (EnumItem &variant);
81 :
82 : /**
83 : * Generate a `MatchCase` for an enum struct variant
84 : *
85 : * (&Enum::Struct { a: self_a }, &Enum::Struct { a: other_a }) => <fn>
86 : */
87 : MatchCase strukt (EnumItem &variant);
88 :
89 : private:
90 : const std::string &enum_path;
91 : const std::string &variant_path;
92 : ExprFn fn;
93 : Builder &builder;
94 : };
95 :
96 : } // namespace AST
97 : } // namespace Rust
98 :
99 : #endif // ! RUST_DERIVE_CMP_COMMON_H
|