mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 06:20:11 +01:00
Make the AST mutable (#849)
The code is pretty intertwined: having the AST be truly mutable means (to me) changing parser.ypp to return non-const values, but then the way things are passed around between objects should be non-const (particularly an issue with lists), which then creates issues with construction of lists in the TypeChecker, which then TypeChecker needs to mostly be non-const. Due to the difficulties in breaking this apart, whereas I'd previously considering refactoring accessor naming in the same PR, I've largely avoided doing so. The intent is then that this PR focuses mainly on const -> non-const AST behavior. call_main moves out of interpreter.cpp so that interpreter.cpp can receive a fully const AST.
This commit is contained in:
@@ -55,7 +55,7 @@ void Pattern::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
|
||||
TuplePattern::TuplePattern(Nonnull<Arena*> arena,
|
||||
Nonnull<const Expression*> tuple_literal)
|
||||
Nonnull<Expression*> tuple_literal)
|
||||
: Pattern(Kind::TuplePattern, tuple_literal->SourceLoc()) {
|
||||
const auto& tuple = cast<TupleLiteral>(*tuple_literal);
|
||||
for (const FieldInitializer& init : tuple.Fields()) {
|
||||
@@ -66,9 +66,8 @@ TuplePattern::TuplePattern(Nonnull<Arena*> arena,
|
||||
|
||||
auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> Nonnull<const Pattern*> {
|
||||
std::optional<Nonnull<const Pattern*>> single_term =
|
||||
paren_contents.SingleTerm();
|
||||
-> Nonnull<Pattern*> {
|
||||
std::optional<Nonnull<Pattern*>> single_term = paren_contents.SingleTerm();
|
||||
if (single_term.has_value()) {
|
||||
return *single_term;
|
||||
} else {
|
||||
@@ -78,7 +77,7 @@ auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation loc,
|
||||
|
||||
auto TuplePatternFromParenContents(Nonnull<Arena*> arena, SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> Nonnull<const TuplePattern*> {
|
||||
-> Nonnull<TuplePattern*> {
|
||||
return arena->New<TuplePattern>(
|
||||
loc, paren_contents.TupleElements<TuplePattern::Field>(loc));
|
||||
}
|
||||
@@ -86,8 +85,8 @@ auto TuplePatternFromParenContents(Nonnull<Arena*> arena, SourceLocation loc,
|
||||
// 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(
|
||||
Nonnull<const Expression*> alternative) {
|
||||
static auto RequireFieldAccess(Nonnull<Expression*> alternative)
|
||||
-> FieldAccessExpression& {
|
||||
if (alternative->Tag() != Expression::Kind::FieldAccessExpression) {
|
||||
FATAL_PROGRAM_ERROR(alternative->SourceLoc())
|
||||
<< "Alternative pattern must have the form of a field access.";
|
||||
@@ -96,8 +95,8 @@ static const FieldAccessExpression& RequireFieldAccess(
|
||||
}
|
||||
|
||||
AlternativePattern::AlternativePattern(SourceLocation loc,
|
||||
Nonnull<const Expression*> alternative,
|
||||
Nonnull<const TuplePattern*> arguments)
|
||||
Nonnull<Expression*> alternative,
|
||||
Nonnull<TuplePattern*> arguments)
|
||||
: Pattern(Kind::AlternativePattern, loc),
|
||||
choice_type(RequireFieldAccess(alternative).Aggregate()),
|
||||
alternative_name(RequireFieldAccess(alternative).Field()),
|
||||
|
||||
Reference in New Issue
Block a user