Debian Codegraph @

gcc/rust/resolve/rust-ast-resolve-pattern.h

raw
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_AST_RESOLVE_PATTERN_H
20 #define RUST_AST_RESOLVE_PATTERN_H
21
22 #include "rust-ast-resolve-base.h"
23 #include "rust-ast-full.h"
24
25 namespace Rust {
26 namespace Resolver {
27
28 // Specifies whether the set of already bound patterns are related by 'Or' or
29 // 'Product'. Used to check for multiple bindings to the same identifier.
30 enum PatternBoundCtx
31 {
32 // A product pattern context (e.g. struct and tuple patterns)
33 Product,
34 // An or-pattern context (e.g. p_0 | p_1 | ...)
35 Or,
36 };
37
38 struct PatternBinding
39 {
40 PatternBoundCtx ctx;
41 std::set<Identifier> idents;
42
43 PatternBinding (PatternBoundCtx ctx, std::set<Identifier> idents)
44 : ctx (ctx), idents (idents)
45 {}
46 };
47
48 // Info that gets stored in the map. Helps us detect if two bindings to the same
49 // identifier have different mutability or ref states.
50 class BindingTypeInfo
51 {
52 Mutability mut;
53 bool is_ref;
54 location_t locus;
55
56 public:
57 BindingTypeInfo (Mutability mut, bool is_ref, location_t locus)
58 : mut (mut), is_ref (is_ref), locus (locus)
59 {}
60
61 BindingTypeInfo (BindingTypeInfo const &other)
62 : mut (other.mut), is_ref (other.is_ref), locus (other.get_locus ())
63 {}
64
65 BindingTypeInfo (){};
66
67 location_t get_locus () const { return locus; }
68 Mutability get_mut () const { return mut; }
69 bool get_is_ref () const { return is_ref; }
70
71 BindingTypeInfo operator= (BindingTypeInfo const &other)
72 {
73 mut = other.mut;
74 is_ref = other.is_ref;
75 locus = other.get_locus ();
76
77 return *this;
78 }
79
80 bool operator== (BindingTypeInfo const &other)
81 {
82 return mut == other.mut && is_ref == other.is_ref;
83 }
84
85 bool operator!= (BindingTypeInfo const &other)
86 {
87 return !BindingTypeInfo::operator== (other);
88 }
89 };
90
91 typedef std::map<Identifier, BindingTypeInfo> BindingMap;
92
93 class PatternDeclaration : public ResolverBase
94 {
95 using Rust::Resolver::ResolverBase::visit;
96
97 public:
98 static void go (AST::Pattern &pattern, Rib::ItemType type);
99 static void go (AST::Pattern &pattern, Rib::ItemType type,
100 std::vector<PatternBinding> &bindings);
101
102 void visit (AST::IdentifierPattern &pattern) override;
103 void visit (AST::GroupedPattern &pattern) override;
104 void visit (AST::ReferencePattern &pattern) override;
105 void visit (AST::PathInExpression &pattern) override;
106 void visit (AST::StructPattern &pattern) override;
107 void visit (AST::TupleStructPattern &pattern) override;
108 void visit (AST::TuplePattern &pattern) override;
109 void visit (AST::RangePattern &pattern) override;
110 void visit (AST::AltPattern &pattern) override;
111 void visit (AST::SlicePattern &pattern) override;
112
113 void add_new_binding (Identifier ident, NodeId node_id, BindingTypeInfo info);
114 void check_bindings_consistency (std::vector<BindingMap> &binding_maps);
115
116 private:
117 PatternDeclaration (std::vector<PatternBinding> &bindings_with_ctx,
118 Rib::ItemType type)
119 : ResolverBase (), bindings_with_ctx (bindings_with_ctx), type (type)
120 {}
121
122 // To avoid having a separate visitor for consistency checks, we store
123 // bindings in two forms:
124
125 // 1) Bindings as a vector of context-related sets.
126 // Used for checking multiple bindings to the same identifier (i.e. E0415,
127 // E0416).
128 std::vector<PatternBinding> &bindings_with_ctx;
129
130 // 2) Bindings as a map between identifiers and binding info.
131 // Used for checking consistency between alt patterns (i.e. E0408, E0409).
132 BindingMap binding_info_map;
133
134 // we need to insert the missing and inconsistent bindings (found in
135 // check_bindings_consistency) into maps to avoid duplication of error
136 // messages.
137 BindingMap inconsistent_bindings;
138 BindingMap missing_bindings;
139
140 Rib::ItemType type;
141 };
142
143 } // namespace Resolver
144 } // namespace Rust
145
146 #endif // RUST_AST_RESOLVE_PATTERN_H