mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 09:30:11 +01:00
Convert Pattern and Expression to Ptr (#787)
Sorry about the big change, this is hard to split. ParenContents is used by both, templated, and expects the same pointer type. While I could duplicate ParenContents with some ExpressionParenContents or PatternParenContents, that seems a little kludgy versus a single large change handling both. The worst of it is that Expression is already pretty sweeping, Pattern is really just incrementally adding. That said, I believe this includes a couple fixes I found with incorrect use of dyn_cast in typecheck.cpp (checked nullptr at the wrong step in 2 code locations). There's also a missing `*` in member.cpp this caught. I adjust passing of expressions for Return due to nullness (I felt adding another constructor was the best solution). I add a `.Release()` to BisonWrap due to things like `$3.first` needing some way to work through BIsonWrap. I felt this was better than `operator->`, but feel free to comment if you prefer the other path (`.Release()` conveniently lets me do pair unwrapping, so it felt a better solution). I do add a TODO to think about better Ptr-to-Ptr cast<> support too, though, as that doesn't work cleanly with LLVM's infra. But so far it seems to only come up in one spot, so I'm not prioritizing it.
This commit is contained in:
@@ -54,19 +54,19 @@ void Pattern::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
}
|
||||
|
||||
TuplePattern::TuplePattern(const Expression* tuple_literal)
|
||||
TuplePattern::TuplePattern(Ptr<const Expression> tuple_literal)
|
||||
: Pattern(Kind::TuplePattern, tuple_literal->SourceLoc()) {
|
||||
const auto& tuple = cast<TupleLiteral>(*tuple_literal);
|
||||
for (const FieldInitializer& init : tuple.Fields()) {
|
||||
fields.push_back(Field(
|
||||
init.name, global_arena->RawNew<ExpressionPattern>(init.expression)));
|
||||
init.name, global_arena->New<ExpressionPattern>(init.expression)));
|
||||
}
|
||||
}
|
||||
|
||||
auto PatternFromParenContents(SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const Pattern* {
|
||||
std::optional<const Pattern*> single_term = paren_contents.SingleTerm();
|
||||
-> Ptr<const Pattern> {
|
||||
std::optional<Ptr<const Pattern>> single_term = paren_contents.SingleTerm();
|
||||
if (single_term.has_value()) {
|
||||
return *single_term;
|
||||
} else {
|
||||
@@ -76,24 +76,31 @@ auto PatternFromParenContents(SourceLocation loc,
|
||||
|
||||
auto TuplePatternFromParenContents(SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const TuplePattern* {
|
||||
return global_arena->RawNew<TuplePattern>(
|
||||
-> Ptr<const TuplePattern> {
|
||||
return global_arena->New<TuplePattern>(
|
||||
loc, paren_contents.TupleElements<TuplePattern::Field>(loc));
|
||||
}
|
||||
|
||||
AlternativePattern::AlternativePattern(SourceLocation loc,
|
||||
const Expression* alternative,
|
||||
const TuplePattern* arguments)
|
||||
: Pattern(Kind::AlternativePattern, loc), arguments(arguments) {
|
||||
// Used by AlternativePattern for constructor initialization. Produces a helpful
|
||||
// error for incorrect expressions, rather than letting a default cast error
|
||||
// apply.
|
||||
static const FieldAccessExpression& RequireFieldAccess(
|
||||
Ptr<const Expression> alternative) {
|
||||
if (alternative->Tag() != Expression::Kind::FieldAccessExpression) {
|
||||
FATAL_PROGRAM_ERROR(alternative->SourceLoc())
|
||||
<< "Alternative pattern must have the form of a field access.";
|
||||
}
|
||||
const auto& field_access = cast<FieldAccessExpression>(*alternative);
|
||||
choice_type = field_access.Aggregate();
|
||||
alternative_name = field_access.Field();
|
||||
return cast<FieldAccessExpression>(*alternative);
|
||||
}
|
||||
|
||||
AlternativePattern::AlternativePattern(SourceLocation loc,
|
||||
Ptr<const Expression> alternative,
|
||||
Ptr<const TuplePattern> arguments)
|
||||
: Pattern(Kind::AlternativePattern, loc),
|
||||
choice_type(RequireFieldAccess(alternative).Aggregate()),
|
||||
alternative_name(RequireFieldAccess(alternative).Field()),
|
||||
arguments(arguments) {}
|
||||
|
||||
auto ParenExpressionToParenPattern(const ParenContents<Expression>& contents)
|
||||
-> ParenContents<Pattern> {
|
||||
ParenContents<Pattern> result = {
|
||||
@@ -101,7 +108,7 @@ auto ParenExpressionToParenPattern(const ParenContents<Expression>& contents)
|
||||
for (const auto& element : contents.elements) {
|
||||
result.elements.push_back(
|
||||
{.name = element.name,
|
||||
.term = global_arena->RawNew<ExpressionPattern>(element.term)});
|
||||
.term = global_arena->New<ExpressionPattern>(element.term)});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user