mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 20:31:10 +01:00
Stop using std::pair in the implementation of tuples (#479)
This commit is contained in:
@@ -16,12 +16,7 @@ const Expression* ParenContents::AsExpression(int line_number) const {
|
||||
}
|
||||
|
||||
const Expression* ParenContents::AsTuple(int line_number) const {
|
||||
auto vec =
|
||||
new std::vector<std::pair<std::string, const Carbon::Expression*>>();
|
||||
for (const FieldInitializer& initializer : fields_) {
|
||||
vec->push_back({initializer.name, initializer.expression});
|
||||
}
|
||||
return MakeTuple(line_number, vec);
|
||||
return MakeTuple(line_number, new std::vector<FieldInitializer>(fields_));
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -11,16 +11,6 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// A FieldInitializer represents the initialization of a single tuple field.
|
||||
struct FieldInitializer {
|
||||
// The field name. An empty string indicates that this represents a
|
||||
// positional field.
|
||||
std::string name;
|
||||
|
||||
// The expression that initializes the field.
|
||||
const Expression* expression;
|
||||
};
|
||||
|
||||
// Represents the syntactic contents of an expression delimited by
|
||||
// parentheses. Such expressions can be interpreted as either tuples or
|
||||
// arbitrary expressions, depending on their context and the syntax of their
|
||||
|
||||
@@ -47,10 +47,9 @@ TEST(ParenContentsTest, UnaryNoCommaAsTuple) {
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag, ExpressionKind::Tuple);
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*tuple->u.tuple.fields;
|
||||
std::vector<FieldInitializer> fields = *tuple->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag, ExpressionKind::Integer);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, UnaryWithCommaAsExpression) {
|
||||
@@ -60,10 +59,9 @@ TEST(ParenContentsTest, UnaryWithCommaAsExpression) {
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 1);
|
||||
ASSERT_EQ(expression->tag, ExpressionKind::Tuple);
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*expression->u.tuple.fields;
|
||||
std::vector<FieldInitializer> fields = *expression->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag, ExpressionKind::Integer);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, UnaryWithCommaAsTuple) {
|
||||
@@ -73,10 +71,9 @@ TEST(ParenContentsTest, UnaryWithCommaAsTuple) {
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag, ExpressionKind::Tuple);
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*tuple->u.tuple.fields;
|
||||
std::vector<FieldInitializer> fields = *tuple->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag, ExpressionKind::Integer);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, BinaryAsExpression) {
|
||||
@@ -87,11 +84,10 @@ TEST(ParenContentsTest, BinaryAsExpression) {
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 1);
|
||||
ASSERT_EQ(expression->tag, ExpressionKind::Tuple);
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*expression->u.tuple.fields;
|
||||
std::vector<FieldInitializer> fields = *expression->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 2);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[1].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[1].expression->tag, ExpressionKind::Integer);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, BinaryAsTuple) {
|
||||
@@ -102,11 +98,10 @@ TEST(ParenContentsTest, BinaryAsTuple) {
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag, ExpressionKind::Tuple);
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*tuple->u.tuple.fields;
|
||||
std::vector<FieldInitializer> fields = *tuple->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 2);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[1].second->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag, ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[1].expression->tag, ExpressionKind::Integer);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -323,11 +323,7 @@ statement_list:
|
||||
;
|
||||
return_type:
|
||||
// Empty
|
||||
{
|
||||
$$ = Carbon::MakeTuple(
|
||||
yylineno,
|
||||
new std::vector<std::pair<std::string, const Carbon::Expression*>>());
|
||||
}
|
||||
{ $$ = Carbon::MakeUnit(yylineno); }
|
||||
| ARROW expression
|
||||
{ $$ = $2; }
|
||||
;
|
||||
@@ -362,9 +358,7 @@ alternative:
|
||||
| identifier
|
||||
{
|
||||
$$ = new std::pair<std::string, const Carbon::Expression*>(
|
||||
$1, Carbon::MakeTuple(
|
||||
yylineno,
|
||||
new std::vector<std::pair<std::string, const Carbon::Expression*>>()));
|
||||
$1, Carbon::MakeUnit(yylineno));
|
||||
}
|
||||
;
|
||||
alternative_list:
|
||||
|
||||
Reference in New Issue
Block a user