forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.cpp
197 lines (182 loc) · 6.38 KB
/
pattern.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/ast/pattern.h"
#include <string>
#include "common/ostream.h"
#include "explorer/ast/expression.h"
#include "explorer/ast/impl_binding.h"
#include "explorer/ast/value.h"
#include "explorer/base/arena.h"
#include "explorer/base/error_builders.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Casting.h"
namespace Carbon {
using llvm::cast;
Pattern::~Pattern() = default;
void Pattern::Print(llvm::raw_ostream& out) const {
switch (kind()) {
case PatternKind::AutoPattern:
out << "auto";
break;
case PatternKind::BindingPattern: {
const auto& binding = cast<BindingPattern>(*this);
out << binding.name() << ": " << binding.type();
break;
}
case PatternKind::GenericBinding: {
const auto& binding = cast<GenericBinding>(*this);
switch (binding.binding_kind()) {
case GenericBinding::BindingKind::Checked:
break;
case GenericBinding::BindingKind::Template:
out << "template ";
break;
}
out << binding.name() << ":! " << binding.type();
if (auto value = binding.constant_value()) {
out << " = " << **value;
}
break;
}
case PatternKind::TuplePattern: {
const auto& tuple = cast<TuplePattern>(*this);
out << "(";
llvm::ListSeparator sep;
for (Nonnull<const Pattern*> field : tuple.fields()) {
out << sep << *field;
}
out << ")";
break;
}
case PatternKind::AlternativePattern: {
const auto& alternative = cast<AlternativePattern>(*this);
out << alternative.choice_type() << "." << alternative.alternative_name()
<< alternative.arguments();
break;
}
case PatternKind::ExpressionPattern:
out << cast<ExpressionPattern>(*this).expression();
break;
case PatternKind::VarPattern:
out << "var" << cast<VarPattern>(*this).pattern();
break;
case PatternKind::AddrPattern:
out << "addr" << cast<AddrPattern>(*this).binding();
break;
}
}
void Pattern::PrintID(llvm::raw_ostream& out) const {
switch (kind()) {
case PatternKind::AutoPattern:
out << "auto";
break;
case PatternKind::BindingPattern: {
const auto& binding = cast<BindingPattern>(*this);
out << binding.name();
break;
}
case PatternKind::GenericBinding: {
const auto& binding = cast<GenericBinding>(*this);
out << binding.name();
break;
}
case PatternKind::TuplePattern: {
out << "(...)";
break;
}
case PatternKind::AlternativePattern: {
const auto& alternative = cast<AlternativePattern>(*this);
out << alternative.choice_type() << "." << alternative.alternative_name()
<< "(...)";
break;
}
case PatternKind::VarPattern:
out << "var ...";
break;
case PatternKind::AddrPattern:
out << "addr ...";
break;
case PatternKind::ExpressionPattern:
out << "...";
break;
}
}
auto VisitNestedPatterns(const Pattern& pattern,
llvm::function_ref<bool(const Pattern&)> visitor)
-> bool {
if (!visitor(pattern)) {
return false;
}
switch (pattern.kind()) {
case PatternKind::TuplePattern:
for (const Pattern* field : cast<TuplePattern>(pattern).fields()) {
if (!VisitNestedPatterns(*field, visitor)) {
return false;
}
}
return true;
case PatternKind::AlternativePattern:
return VisitNestedPatterns(cast<AlternativePattern>(pattern).arguments(),
visitor);
case PatternKind::VarPattern:
return VisitNestedPatterns(cast<VarPattern>(pattern).pattern(), visitor);
case PatternKind::AddrPattern:
return VisitNestedPatterns(cast<AddrPattern>(pattern).binding(), visitor);
case PatternKind::BindingPattern:
case PatternKind::AutoPattern:
case PatternKind::ExpressionPattern:
case PatternKind::GenericBinding:
return true;
}
}
auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
const ParenContents<Pattern>& paren_contents)
-> Nonnull<Pattern*> {
std::optional<Nonnull<Pattern*>> single_term = paren_contents.SingleTerm();
if (single_term.has_value()) {
return *single_term;
} else {
return TuplePatternFromParenContents(arena, source_loc, paren_contents);
}
}
auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
SourceLocation source_loc,
const ParenContents<Pattern>& paren_contents)
-> Nonnull<TuplePattern*> {
return arena->New<TuplePattern>(source_loc, paren_contents.elements);
}
// Used by AlternativePattern for constructor initialization. Produces a helpful
// error for incorrect expressions, rather than letting a default cast error
// apply.
auto AlternativePattern::RequireSimpleMemberAccess(
Nonnull<Expression*> alternative)
-> ErrorOr<Nonnull<SimpleMemberAccessExpression*>> {
if (alternative->kind() != ExpressionKind::SimpleMemberAccessExpression) {
return ProgramError(alternative->source_loc())
<< "Alternative pattern must have the form of a field access.";
}
return &cast<SimpleMemberAccessExpression>(*alternative);
}
auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
const ParenContents<Expression>& contents)
-> ParenContents<Pattern> {
ParenContents<Pattern> result = {
.elements = {}, .has_trailing_comma = contents.has_trailing_comma};
for (const auto& element : contents.elements) {
result.elements.push_back(arena->New<ExpressionPattern>(element));
}
return result;
}
GenericBinding::GenericBinding(CloneContext& context,
const GenericBinding& other)
: Pattern(context, other),
name_(other.name_),
type_(context.Clone(other.type_)),
binding_kind_(other.binding_kind_),
template_value_(context.Clone(other.template_value_)),
symbolic_identity_(context.Clone(other.symbolic_identity_)),
impl_binding_(context.Clone(other.impl_binding_)),
original_(context.Remap(other.original_)),
named_as_type_via_dot_self_(other.named_as_type_via_dot_self_) {}
} // namespace Carbon