mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 22:02:45 +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:
@@ -11,6 +11,7 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -57,57 +58,58 @@ class Statement {
|
||||
|
||||
class ExpressionStatement : public Statement {
|
||||
public:
|
||||
ExpressionStatement(SourceLocation loc, const Expression* exp)
|
||||
ExpressionStatement(SourceLocation loc, Ptr<const Expression> exp)
|
||||
: Statement(Kind::ExpressionStatement, loc), exp(exp) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::ExpressionStatement;
|
||||
}
|
||||
|
||||
auto Exp() const -> const Expression* { return exp; }
|
||||
auto Exp() const -> Ptr<const Expression> { return exp; }
|
||||
|
||||
private:
|
||||
const Expression* exp;
|
||||
Ptr<const Expression> exp;
|
||||
};
|
||||
|
||||
class Assign : public Statement {
|
||||
public:
|
||||
Assign(SourceLocation loc, const Expression* lhs, const Expression* rhs)
|
||||
Assign(SourceLocation loc, Ptr<const Expression> lhs,
|
||||
Ptr<const Expression> rhs)
|
||||
: Statement(Kind::Assign, loc), lhs(lhs), rhs(rhs) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Assign;
|
||||
}
|
||||
|
||||
auto Lhs() const -> const Expression* { return lhs; }
|
||||
auto Rhs() const -> const Expression* { return rhs; }
|
||||
auto Lhs() const -> Ptr<const Expression> { return lhs; }
|
||||
auto Rhs() const -> Ptr<const Expression> { return rhs; }
|
||||
|
||||
private:
|
||||
const Expression* lhs;
|
||||
const Expression* rhs;
|
||||
Ptr<const Expression> lhs;
|
||||
Ptr<const Expression> rhs;
|
||||
};
|
||||
|
||||
class VariableDefinition : public Statement {
|
||||
public:
|
||||
VariableDefinition(SourceLocation loc, const Pattern* pat,
|
||||
const Expression* init)
|
||||
VariableDefinition(SourceLocation loc, Ptr<const Pattern> pat,
|
||||
Ptr<const Expression> init)
|
||||
: Statement(Kind::VariableDefinition, loc), pat(pat), init(init) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::VariableDefinition;
|
||||
}
|
||||
|
||||
auto Pat() const -> const Pattern* { return pat; }
|
||||
auto Init() const -> const Expression* { return init; }
|
||||
auto Pat() const -> Ptr<const Pattern> { return pat; }
|
||||
auto Init() const -> Ptr<const Expression> { return init; }
|
||||
|
||||
private:
|
||||
const Pattern* pat;
|
||||
const Expression* init;
|
||||
Ptr<const Pattern> pat;
|
||||
Ptr<const Expression> init;
|
||||
};
|
||||
|
||||
class If : public Statement {
|
||||
public:
|
||||
If(SourceLocation loc, const Expression* cond, const Statement* then_stmt,
|
||||
If(SourceLocation loc, Ptr<const Expression> cond, const Statement* then_stmt,
|
||||
const Statement* else_stmt)
|
||||
: Statement(Kind::If, loc),
|
||||
cond(cond),
|
||||
@@ -118,29 +120,34 @@ class If : public Statement {
|
||||
return stmt->Tag() == Kind::If;
|
||||
}
|
||||
|
||||
auto Cond() const -> const Expression* { return cond; }
|
||||
auto Cond() const -> Ptr<const Expression> { return cond; }
|
||||
auto ThenStmt() const -> const Statement* { return then_stmt; }
|
||||
auto ElseStmt() const -> const Statement* { return else_stmt; }
|
||||
|
||||
private:
|
||||
const Expression* cond;
|
||||
Ptr<const Expression> cond;
|
||||
const Statement* then_stmt;
|
||||
const Statement* else_stmt;
|
||||
};
|
||||
|
||||
class Return : public Statement {
|
||||
public:
|
||||
Return(SourceLocation loc, const Expression* exp, bool is_omitted_exp);
|
||||
explicit Return(SourceLocation loc)
|
||||
: Return(loc, global_arena->New<TupleLiteral>(loc), true) {}
|
||||
Return(SourceLocation loc, Ptr<const Expression> exp, bool is_omitted_exp)
|
||||
: Statement(Kind::Return, loc),
|
||||
exp(exp),
|
||||
is_omitted_exp(is_omitted_exp) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Return;
|
||||
}
|
||||
|
||||
auto Exp() const -> const Expression* { return exp; }
|
||||
auto Exp() const -> Ptr<const Expression> { return exp; }
|
||||
auto IsOmittedExp() const -> bool { return is_omitted_exp; }
|
||||
|
||||
private:
|
||||
const Expression* exp;
|
||||
Ptr<const Expression> exp;
|
||||
bool is_omitted_exp;
|
||||
};
|
||||
|
||||
@@ -178,18 +185,18 @@ class Block : public Statement {
|
||||
|
||||
class While : public Statement {
|
||||
public:
|
||||
While(SourceLocation loc, const Expression* cond, const Statement* body)
|
||||
While(SourceLocation loc, Ptr<const Expression> cond, const Statement* body)
|
||||
: Statement(Kind::While, loc), cond(cond), body(body) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::While;
|
||||
}
|
||||
|
||||
auto Cond() const -> const Expression* { return cond; }
|
||||
auto Cond() const -> Ptr<const Expression> { return cond; }
|
||||
auto Body() const -> const Statement* { return body; }
|
||||
|
||||
private:
|
||||
const Expression* cond;
|
||||
Ptr<const Expression> cond;
|
||||
const Statement* body;
|
||||
};
|
||||
|
||||
@@ -213,23 +220,23 @@ class Continue : public Statement {
|
||||
|
||||
class Match : public Statement {
|
||||
public:
|
||||
Match(SourceLocation loc, const Expression* exp,
|
||||
std::list<std::pair<const Pattern*, const Statement*>>* clauses)
|
||||
Match(SourceLocation loc, Ptr<const Expression> exp,
|
||||
std::list<std::pair<Ptr<const Pattern>, const Statement*>>* clauses)
|
||||
: Statement(Kind::Match, loc), exp(exp), clauses(clauses) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Match;
|
||||
}
|
||||
|
||||
auto Exp() const -> const Expression* { return exp; }
|
||||
auto Exp() const -> Ptr<const Expression> { return exp; }
|
||||
auto Clauses() const
|
||||
-> const std::list<std::pair<const Pattern*, const Statement*>>* {
|
||||
-> const std::list<std::pair<Ptr<const Pattern>, const Statement*>>* {
|
||||
return clauses;
|
||||
}
|
||||
|
||||
private:
|
||||
const Expression* exp;
|
||||
std::list<std::pair<const Pattern*, const Statement*>>* clauses;
|
||||
Ptr<const Expression> exp;
|
||||
std::list<std::pair<Ptr<const Pattern>, const Statement*>>* clauses;
|
||||
};
|
||||
|
||||
// A continuation statement.
|
||||
@@ -264,17 +271,17 @@ class Continuation : public Statement {
|
||||
// __run <argument>;
|
||||
class Run : public Statement {
|
||||
public:
|
||||
Run(SourceLocation loc, const Expression* argument)
|
||||
Run(SourceLocation loc, Ptr<const Expression> argument)
|
||||
: Statement(Kind::Run, loc), argument(argument) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Run;
|
||||
}
|
||||
|
||||
auto Argument() const -> const Expression* { return argument; }
|
||||
auto Argument() const -> Ptr<const Expression> { return argument; }
|
||||
|
||||
private:
|
||||
const Expression* argument;
|
||||
Ptr<const Expression> argument;
|
||||
};
|
||||
|
||||
// An await statement.
|
||||
|
||||
Reference in New Issue
Block a user