Initial support for statically-sized arrays (#1158)

This commit is contained in:
Geoff Romer
2022-03-31 10:55:34 -07:00
committed by GitHub
parent 0b8a96af6d
commit 8f9638d759
16 changed files with 302 additions and 22 deletions
+1
View File
@@ -55,3 +55,4 @@ abstract class Expression : AstNode;
class IntrinsicExpression : Expression;
class IfExpression : Expression;
class UnimplementedExpression : Expression;
class ArrayTypeLiteral : Expression;
+7
View File
@@ -176,6 +176,12 @@ void Expression::Print(llvm::raw_ostream& out) const {
out << ")";
break;
}
case ExpressionKind::ArrayTypeLiteral: {
const auto& array_literal = cast<ArrayTypeLiteral>(*this);
out << "[" << array_literal.element_type_expression() << "; "
<< array_literal.size_expression() << "]";
break;
}
case ExpressionKind::IdentifierExpression:
case ExpressionKind::IntLiteral:
case ExpressionKind::BoolLiteral:
@@ -232,6 +238,7 @@ void Expression::PrintID(llvm::raw_ostream& out) const {
case ExpressionKind::IntrinsicExpression:
case ExpressionKind::UnimplementedExpression:
case ExpressionKind::FunctionTypeLiteral:
case ExpressionKind::ArrayTypeLiteral:
out << "...";
break;
}
+33
View File
@@ -572,6 +572,39 @@ class UnimplementedExpression : public Expression {
std::vector<Nonnull<AstNode*>> children_;
};
// A literal representing a statically-sized array type.
class ArrayTypeLiteral : public Expression {
public:
// Constructs an array type literal which uses the given expressions to
// represent the element type and size.
ArrayTypeLiteral(SourceLocation source_loc,
Nonnull<Expression*> element_type_expression,
Nonnull<Expression*> size_expression)
: Expression(AstNodeKind::ArrayTypeLiteral, source_loc),
element_type_expression_(element_type_expression),
size_expression_(size_expression) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromArrayTypeLiteral(node->kind());
}
auto element_type_expression() const -> const Expression& {
return *element_type_expression_;
}
auto element_type_expression() -> Expression& {
return *element_type_expression_;
}
auto size_expression() const -> const Expression& {
return *size_expression_;
}
auto size_expression() -> Expression& { return *size_expression_; }
private:
Nonnull<Expression*> element_type_expression_;
Nonnull<Expression*> size_expression_;
};
// Converts paren_contents to an Expression, interpreting the parentheses as
// grouping if their contents permit that interpretation, or as forming a
// tuple otherwise.