mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 08:51:05 +01:00
Be more explicit about AST types (#921)
This was born out of wanting FunctionDeclaration to explicitly have a Block for a body, and became a bit more of specifying types around. Note this forces exec_program to generate a Block for print()'s body, which is probably more correct as now we can expect a standard FunctionDeclaration AST structure, even for the built-ins. There is a syntactic change here: a continuation's body is now a Block, not just a Statement. I've added an example disallowed test case. I think this is more reasonable syntax. Other than that, note that optional_else now generates a valid Block. This has me thinking about whether we can eliminate Sequence, but that seemed well out of scope for this.
This commit is contained in:
@@ -90,7 +90,7 @@ class FunctionDeclaration : public Declaration {
|
||||
Nonnull<TuplePattern*> param_pattern,
|
||||
Nonnull<Pattern*> return_type,
|
||||
bool is_omitted_return_type,
|
||||
std::optional<Nonnull<Statement*>> body)
|
||||
std::optional<Nonnull<Block*>> body)
|
||||
: Declaration(Kind::FunctionDeclaration, source_loc),
|
||||
name_(std::move(name)),
|
||||
deduced_parameters_(std::move(deduced_params)),
|
||||
@@ -116,10 +116,8 @@ class FunctionDeclaration : public Declaration {
|
||||
auto is_omitted_return_type() const -> bool {
|
||||
return is_omitted_return_type_;
|
||||
}
|
||||
auto body() const -> std::optional<Nonnull<const Statement*>> {
|
||||
return body_;
|
||||
}
|
||||
auto body() -> std::optional<Nonnull<Statement*>> { return body_; }
|
||||
auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
|
||||
auto body() -> std::optional<Nonnull<Block*>> { return body_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
@@ -127,7 +125,7 @@ class FunctionDeclaration : public Declaration {
|
||||
Nonnull<TuplePattern*> param_pattern_;
|
||||
Nonnull<Pattern*> return_type_;
|
||||
bool is_omitted_return_type_;
|
||||
std::optional<Nonnull<Statement*>> body_;
|
||||
std::optional<Nonnull<Block*>> body_;
|
||||
};
|
||||
|
||||
class ClassDeclaration : public Declaration {
|
||||
|
||||
@@ -30,7 +30,7 @@ auto ExpressionFromParenContents(
|
||||
|
||||
auto TupleExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*> {
|
||||
return arena->New<TupleLiteral>(source_loc, paren_contents.elements);
|
||||
}
|
||||
|
||||
|
||||
@@ -79,19 +79,6 @@ class Expression {
|
||||
std::optional<Nonnull<const Value*>> static_type_;
|
||||
};
|
||||
|
||||
// Converts paren_contents to an Expression, interpreting the parentheses as
|
||||
// grouping if their contents permit that interpretation, or as forming a
|
||||
// tuple otherwise.
|
||||
auto ExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
|
||||
|
||||
// Converts paren_contents to an Expression, interpreting the parentheses as
|
||||
// forming a tuple.
|
||||
auto TupleExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
|
||||
|
||||
// A FieldInitializer represents the initialization of a single struct field.
|
||||
class FieldInitializer {
|
||||
public:
|
||||
@@ -450,6 +437,19 @@ class IntrinsicExpression : public Expression {
|
||||
Intrinsic intrinsic_;
|
||||
};
|
||||
|
||||
// Converts paren_contents to an Expression, interpreting the parentheses as
|
||||
// grouping if their contents permit that interpretation, or as forming a
|
||||
// tuple otherwise.
|
||||
auto ExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
|
||||
|
||||
// Converts paren_contents to an Expression, interpreting the parentheses as
|
||||
// forming a tuple.
|
||||
auto TupleExpressionFromParenContents(
|
||||
Nonnull<Arena*> arena, SourceLocation source_loc,
|
||||
const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|
||||
|
||||
@@ -62,10 +62,10 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
case Kind::If: {
|
||||
const auto& if_stmt = cast<If>(*this);
|
||||
out << "if (" << if_stmt.condition() << ")\n";
|
||||
if_stmt.then_statement().PrintDepth(depth - 1, out);
|
||||
if (if_stmt.else_statement()) {
|
||||
if_stmt.then_block().PrintDepth(depth - 1, out);
|
||||
if (if_stmt.else_block()) {
|
||||
out << "\nelse\n";
|
||||
(*if_stmt.else_statement())->PrintDepth(depth - 1, out);
|
||||
(*if_stmt.else_block())->PrintDepth(depth - 1, out);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -97,8 +97,8 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
if (depth < 0 || depth > 1) {
|
||||
out << "\n";
|
||||
}
|
||||
if (block.statement()) {
|
||||
(*block.statement())->PrintDepth(depth, out);
|
||||
if (block.sequence()) {
|
||||
(*block.sequence())->PrintDepth(depth, out);
|
||||
if (depth < 0 || depth > 1) {
|
||||
out << "\n";
|
||||
}
|
||||
|
||||
@@ -60,6 +60,48 @@ class Statement {
|
||||
SourceLocation source_loc_;
|
||||
};
|
||||
|
||||
class Sequence : public Statement {
|
||||
public:
|
||||
Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
|
||||
std::optional<Nonnull<Statement*>> next)
|
||||
: Statement(Kind::Sequence, source_loc),
|
||||
statement_(statement),
|
||||
next_(next) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->kind() == Kind::Sequence;
|
||||
}
|
||||
|
||||
auto statement() const -> const Statement& { return *statement_; }
|
||||
auto statement() -> Statement& { return *statement_; }
|
||||
auto next() const -> std::optional<Nonnull<const Statement*>> {
|
||||
return next_;
|
||||
}
|
||||
auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
|
||||
|
||||
private:
|
||||
Nonnull<Statement*> statement_;
|
||||
std::optional<Nonnull<Statement*>> next_;
|
||||
};
|
||||
|
||||
class Block : public Statement {
|
||||
public:
|
||||
Block(SourceLocation source_loc, std::optional<Nonnull<Sequence*>> sequence)
|
||||
: Statement(Kind::Block, source_loc), sequence_(sequence) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->kind() == Kind::Block;
|
||||
}
|
||||
|
||||
auto sequence() const -> std::optional<Nonnull<const Sequence*>> {
|
||||
return sequence_;
|
||||
}
|
||||
auto sequence() -> std::optional<Nonnull<Sequence*>> { return sequence_; }
|
||||
|
||||
private:
|
||||
std::optional<Nonnull<Sequence*>> sequence_;
|
||||
};
|
||||
|
||||
class ExpressionStatement : public Statement {
|
||||
public:
|
||||
ExpressionStatement(SourceLocation source_loc,
|
||||
@@ -123,12 +165,11 @@ class VariableDefinition : public Statement {
|
||||
class If : public Statement {
|
||||
public:
|
||||
If(SourceLocation source_loc, Nonnull<Expression*> condition,
|
||||
Nonnull<Statement*> then_statement,
|
||||
std::optional<Nonnull<Statement*>> else_statement)
|
||||
Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
|
||||
: Statement(Kind::If, source_loc),
|
||||
condition_(condition),
|
||||
then_statement_(then_statement),
|
||||
else_statement_(else_statement) {}
|
||||
then_block_(then_block),
|
||||
else_block_(else_block) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->kind() == Kind::If;
|
||||
@@ -136,19 +177,17 @@ class If : public Statement {
|
||||
|
||||
auto condition() const -> const Expression& { return *condition_; }
|
||||
auto condition() -> Expression& { return *condition_; }
|
||||
auto then_statement() const -> const Statement& { return *then_statement_; }
|
||||
auto then_statement() -> Statement& { return *then_statement_; }
|
||||
auto else_statement() const -> std::optional<Nonnull<const Statement*>> {
|
||||
return else_statement_;
|
||||
}
|
||||
auto else_statement() -> std::optional<Nonnull<Statement*>> {
|
||||
return else_statement_;
|
||||
auto then_block() const -> const Block& { return *then_block_; }
|
||||
auto then_block() -> Block& { return *then_block_; }
|
||||
auto else_block() const -> std::optional<Nonnull<const Block*>> {
|
||||
return else_block_;
|
||||
}
|
||||
auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
|
||||
|
||||
private:
|
||||
Nonnull<Expression*> condition_;
|
||||
Nonnull<Statement*> then_statement_;
|
||||
std::optional<Nonnull<Statement*>> else_statement_;
|
||||
Nonnull<Block*> then_block_;
|
||||
std::optional<Nonnull<Block*>> else_block_;
|
||||
};
|
||||
|
||||
class Return : public Statement {
|
||||
@@ -189,52 +228,10 @@ class Return : public Statement {
|
||||
std::optional<Nonnull<const FunctionDeclaration*>> function_;
|
||||
};
|
||||
|
||||
class Sequence : public Statement {
|
||||
public:
|
||||
Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
|
||||
std::optional<Nonnull<Statement*>> next)
|
||||
: Statement(Kind::Sequence, source_loc),
|
||||
statement_(statement),
|
||||
next_(next) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->kind() == Kind::Sequence;
|
||||
}
|
||||
|
||||
auto statement() const -> const Statement& { return *statement_; }
|
||||
auto statement() -> Statement& { return *statement_; }
|
||||
auto next() const -> std::optional<Nonnull<const Statement*>> {
|
||||
return next_;
|
||||
}
|
||||
auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
|
||||
|
||||
private:
|
||||
Nonnull<Statement*> statement_;
|
||||
std::optional<Nonnull<Statement*>> next_;
|
||||
};
|
||||
|
||||
class Block : public Statement {
|
||||
public:
|
||||
Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> statement)
|
||||
: Statement(Kind::Block, source_loc), statement_(statement) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->kind() == Kind::Block;
|
||||
}
|
||||
|
||||
auto statement() const -> std::optional<Nonnull<const Statement*>> {
|
||||
return statement_;
|
||||
}
|
||||
auto statement() -> std::optional<Nonnull<Statement*>> { return statement_; }
|
||||
|
||||
private:
|
||||
std::optional<Nonnull<Statement*>> statement_;
|
||||
};
|
||||
|
||||
class While : public Statement {
|
||||
public:
|
||||
While(SourceLocation source_loc, Nonnull<Expression*> condition,
|
||||
Nonnull<Statement*> body)
|
||||
Nonnull<Block*> body)
|
||||
: Statement(Kind::While, source_loc),
|
||||
condition_(condition),
|
||||
body_(body) {}
|
||||
@@ -245,12 +242,12 @@ class While : public Statement {
|
||||
|
||||
auto condition() const -> const Expression& { return *condition_; }
|
||||
auto condition() -> Expression& { return *condition_; }
|
||||
auto body() const -> const Statement& { return *body_; }
|
||||
auto body() -> Statement& { return *body_; }
|
||||
auto body() const -> const Block& { return *body_; }
|
||||
auto body() -> Block& { return *body_; }
|
||||
|
||||
private:
|
||||
Nonnull<Expression*> condition_;
|
||||
Nonnull<Statement*> body_;
|
||||
Nonnull<Block*> body_;
|
||||
};
|
||||
|
||||
class Break : public Statement {
|
||||
@@ -352,7 +349,7 @@ class Match : public Statement {
|
||||
class Continuation : public Statement {
|
||||
public:
|
||||
Continuation(SourceLocation source_loc, std::string continuation_variable,
|
||||
Nonnull<Statement*> body)
|
||||
Nonnull<Block*> body)
|
||||
: Statement(Kind::Continuation, source_loc),
|
||||
continuation_variable_(std::move(continuation_variable)),
|
||||
body_(body) {}
|
||||
@@ -364,12 +361,12 @@ class Continuation : public Statement {
|
||||
auto continuation_variable() const -> const std::string& {
|
||||
return continuation_variable_;
|
||||
}
|
||||
auto body() const -> const Statement& { return *body_; }
|
||||
auto body() -> Statement& { return *body_; }
|
||||
auto body() const -> const Block& { return *body_; }
|
||||
auto body() -> Block& { return *body_; }
|
||||
|
||||
private:
|
||||
std::string continuation_variable_;
|
||||
Nonnull<Statement*> body_;
|
||||
Nonnull<Block*> body_;
|
||||
};
|
||||
|
||||
// A run statement.
|
||||
|
||||
Reference in New Issue
Block a user