aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-ast-resolve-pattern.h
blob: 876de1607831b08e893545a1c6b70ec85cca7d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2020-2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#ifndef RUST_AST_RESOLVE_PATTERN_H
#define RUST_AST_RESOLVE_PATTERN_H

#include "rust-ast-resolve-base.h"
#include "rust-ast-full.h"

namespace Rust {
namespace Resolver {

// Specifies whether the set of already bound patterns are related by 'Or' or
// 'Product'. Used to check for multiple bindings to the same identifier.
enum PatternBoundCtx
{
  // A product pattern context (e.g. struct and tuple patterns)
  Product,
  // An or-pattern context (e.g. p_0 | p_1 | ...)
  Or,
};

struct PatternBinding
{
  PatternBoundCtx ctx;
  std::set<Identifier> idents;

  PatternBinding (PatternBoundCtx ctx, std::set<Identifier> idents)
    : ctx (ctx), idents (idents)
  {}
};

// Info that gets stored in the map. Helps us detect if two bindings to the same
// identifier have different mutability or ref states.
class BindingTypeInfo
{
  Mutability mut;
  bool is_ref;
  location_t locus;

public:
  BindingTypeInfo (Mutability mut, bool is_ref, location_t locus)
    : mut (mut), is_ref (is_ref), locus (locus)
  {}

  BindingTypeInfo (BindingTypeInfo const &other)
    : mut (other.mut), is_ref (other.is_ref), locus (other.get_locus ())
  {}

  BindingTypeInfo (){};

  location_t get_locus () const { return locus; }
  Mutability get_mut () const { return mut; }
  bool get_is_ref () const { return is_ref; }

  BindingTypeInfo operator= (BindingTypeInfo const &other)
  {
    mut = other.mut;
    is_ref = other.is_ref;
    locus = other.get_locus ();

    return *this;
  }

  bool operator== (BindingTypeInfo const &other)
  {
    return mut == other.mut && is_ref == other.is_ref;
  }

  bool operator!= (BindingTypeInfo const &other)
  {
    return !BindingTypeInfo::operator== (other);
  }
};

typedef std::map<Identifier, BindingTypeInfo> BindingMap;

class PatternDeclaration : public ResolverBase
{
  using Rust::Resolver::ResolverBase::visit;

public:
  static void go (AST::Pattern &pattern, Rib::ItemType type);
  static void go (AST::Pattern &pattern, Rib::ItemType type,
		  std::vector<PatternBinding> &bindings);

  void visit (AST::IdentifierPattern &pattern) override;
  void visit (AST::GroupedPattern &pattern) override;
  void visit (AST::ReferencePattern &pattern) override;
  void visit (AST::PathInExpression &pattern) override;
  void visit (AST::StructPattern &pattern) override;
  void visit (AST::TupleStructPattern &pattern) override;
  void visit (AST::TuplePattern &pattern) override;
  void visit (AST::RangePattern &pattern) override;
  void visit (AST::AltPattern &pattern) override;
  void visit (AST::SlicePattern &pattern) override;

  void add_new_binding (Identifier ident, NodeId node_id, BindingTypeInfo info);
  void check_bindings_consistency (std::vector<BindingMap> &binding_maps);

private:
  PatternDeclaration (std::vector<PatternBinding> &bindings_with_ctx,
		      Rib::ItemType type)
    : ResolverBase (), bindings_with_ctx (bindings_with_ctx), type (type)
  {}

  // To avoid having a separate visitor for consistency checks, we store
  // bindings in two forms:

  // 1) Bindings as a vector of context-related sets.
  // Used for checking multiple bindings to the same identifier (i.e. E0415,
  // E0416).
  std::vector<PatternBinding> &bindings_with_ctx;

  // 2) Bindings as a map between identifiers and binding info.
  // Used for checking consistency between alt patterns (i.e. E0408, E0409).
  BindingMap binding_info_map;

  // we need to insert the missing and inconsistent bindings (found in
  // check_bindings_consistency) into maps to avoid duplication of error
  // messages.
  BindingMap inconsistent_bindings;
  BindingMap missing_bindings;

  Rib::ItemType type;
};

} // namespace Resolver
} // namespace Rust

#endif // RUST_AST_RESOLVE_PATTERN_H