mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Initial support for statically-sized arrays (#1158)
This commit is contained in:
@@ -55,3 +55,4 @@ abstract class Expression : AstNode;
|
||||
class IntrinsicExpression : Expression;
|
||||
class IfExpression : Expression;
|
||||
class UnimplementedExpression : Expression;
|
||||
class ArrayTypeLiteral : Expression;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user