mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
change all expression and statement pointers to be const (#449)
* change all expression and statement pointers to be const * const in paren tests
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
Expression* ParenContents::AsExpression(int line_number) const {
|
||||
const Expression* ParenContents::AsExpression(int line_number) const {
|
||||
if (fields_.size() == 1 && fields_.front().name == "" &&
|
||||
has_trailing_comma_ == HasTrailingComma::No) {
|
||||
return fields_.front().expression;
|
||||
@@ -15,8 +15,9 @@ Expression* ParenContents::AsExpression(int line_number) const {
|
||||
}
|
||||
}
|
||||
|
||||
Expression* ParenContents::AsTuple(int line_number) const {
|
||||
auto vec = new std::vector<std::pair<std::string, Carbon::Expression*>>();
|
||||
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});
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ struct FieldInitializer {
|
||||
std::string name;
|
||||
|
||||
// The expression that initializes the field.
|
||||
Expression* expression;
|
||||
const Expression* expression;
|
||||
};
|
||||
|
||||
// Represents the syntactic contents of an expression delimited by
|
||||
@@ -45,11 +45,11 @@ class ParenContents {
|
||||
ParenContents& operator=(const ParenContents&) = default;
|
||||
|
||||
// Returns the paren expression, interpreted as a tuple.
|
||||
Expression* AsTuple(int line_number) const;
|
||||
const Expression* AsTuple(int line_number) const;
|
||||
|
||||
// Returns the paren expression, with no external constraints on what kind
|
||||
// of expression it represents.
|
||||
Expression* AsExpression(int line_number) const;
|
||||
const Expression* AsExpression(int line_number) const;
|
||||
|
||||
private:
|
||||
std::vector<FieldInitializer> fields_;
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace {
|
||||
|
||||
TEST(ParenContentsTest, EmptyAsExpression) {
|
||||
ParenContents contents;
|
||||
Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 1);
|
||||
ASSERT_EQ(expression->tag, ExpressionKind::Tuple);
|
||||
EXPECT_EQ(expression->u.tuple.fields->size(), 0);
|
||||
@@ -19,7 +19,7 @@ TEST(ParenContentsTest, EmptyAsExpression) {
|
||||
|
||||
TEST(ParenContentsTest, EmptyAsTuple) {
|
||||
ParenContents contents;
|
||||
Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag, ExpressionKind::Tuple);
|
||||
EXPECT_EQ(tuple->u.tuple.fields->size(), 0);
|
||||
@@ -35,7 +35,7 @@ TEST(ParenContentsTest, UnaryNoCommaAsExpression) {
|
||||
ParenContents contents({{.expression = MakeInt(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::No);
|
||||
|
||||
Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 2);
|
||||
ASSERT_EQ(expression->tag, ExpressionKind::Integer);
|
||||
}
|
||||
@@ -44,10 +44,10 @@ TEST(ParenContentsTest, UnaryNoCommaAsTuple) {
|
||||
ParenContents contents({{.expression = MakeInt(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::No);
|
||||
|
||||
Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
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, Expression*>> fields =
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*tuple->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
@@ -57,10 +57,10 @@ TEST(ParenContentsTest, UnaryWithCommaAsExpression) {
|
||||
ParenContents contents({{.expression = MakeInt(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
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, Expression*>> fields =
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*expression->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
@@ -70,10 +70,10 @@ TEST(ParenContentsTest, UnaryWithCommaAsTuple) {
|
||||
ParenContents contents({{.expression = MakeInt(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
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, Expression*>> fields =
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*tuple->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
@@ -84,10 +84,10 @@ TEST(ParenContentsTest, BinaryAsExpression) {
|
||||
{.expression = MakeInt(/*line_num=*/3, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
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, Expression*>> fields =
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*expression->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 2);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
@@ -99,10 +99,10 @@ TEST(ParenContentsTest, BinaryAsTuple) {
|
||||
{.expression = MakeInt(/*line_num=*/3, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
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, Expression*>> fields =
|
||||
std::vector<std::pair<std::string, const Expression*>> fields =
|
||||
*tuple->u.tuple.fields;
|
||||
ASSERT_EQ(fields.size(), 2);
|
||||
EXPECT_EQ(fields[0].second->tag, ExpressionKind::Integer);
|
||||
|
||||
@@ -96,24 +96,24 @@ void yy::parser::error(
|
||||
%type <Carbon::FunctionDefinition*> function_declaration
|
||||
%type <Carbon::FunctionDefinition*> function_definition
|
||||
%type <std::list<Carbon::Declaration>*> declaration_list
|
||||
%type <Carbon::Statement*> statement
|
||||
%type <Carbon::Statement*> optional_else
|
||||
%type <Carbon::Statement*> statement_list
|
||||
%type <Carbon::Expression*> expression
|
||||
%type <Carbon::Expression*> pattern
|
||||
%type <Carbon::Expression*> return_type
|
||||
%type <Carbon::Expression*> paren_expression
|
||||
%type <Carbon::Expression*> tuple
|
||||
%type <const Carbon::Statement*> statement
|
||||
%type <const Carbon::Statement*> optional_else
|
||||
%type <const Carbon::Statement*> statement_list
|
||||
%type <const Carbon::Expression*> expression
|
||||
%type <const Carbon::Expression*> pattern
|
||||
%type <const Carbon::Expression*> return_type
|
||||
%type <const Carbon::Expression*> paren_expression
|
||||
%type <const Carbon::Expression*> tuple
|
||||
%type <Carbon::Member*> variable_declaration
|
||||
%type <Carbon::Member*> member
|
||||
%type <std::list<Carbon::Member*>*> member_list
|
||||
%type <Carbon::FieldInitializer> field_initializer
|
||||
%type <Carbon::ParenContents> paren_contents
|
||||
%type <std::vector<Carbon::FieldInitializer>> paren_contents_without_trailing_comma
|
||||
%type <std::pair<std::string, Carbon::Expression*>*> alternative
|
||||
%type <std::list<std::pair<std::string, Carbon::Expression*>>*> alternative_list
|
||||
%type <std::pair<Carbon::Expression*, Carbon::Statement*>*> clause
|
||||
%type <std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>*> clause_list
|
||||
%type <std::pair<std::string, const Carbon::Expression*>*> alternative
|
||||
%type <std::list<std::pair<std::string, const Carbon::Expression*>>*> alternative_list
|
||||
%type <std::pair<const Carbon::Expression*, const Carbon::Statement*>*> clause
|
||||
%type <std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>*> clause_list
|
||||
%token END_OF_FILE 0
|
||||
%token AND
|
||||
%token OR
|
||||
@@ -266,18 +266,18 @@ paren_contents_without_trailing_comma:
|
||||
;
|
||||
clause:
|
||||
CASE pattern DBLARROW statement
|
||||
{ $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>($2, $4); }
|
||||
{ $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>($2, $4); }
|
||||
| DEFAULT DBLARROW statement
|
||||
{
|
||||
auto vp = Carbon::MakeVarPat(yylineno, "_",
|
||||
Carbon::MakeAutoType(yylineno));
|
||||
$$ = new std::pair<Carbon::Expression*, Carbon::Statement*>(vp, $3);
|
||||
$$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>(vp, $3);
|
||||
}
|
||||
;
|
||||
clause_list:
|
||||
// Empty
|
||||
{
|
||||
$$ = new std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>();
|
||||
$$ = new std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>();
|
||||
}
|
||||
| clause clause_list
|
||||
{ $$ = $2; $$->push_front(*$1); }
|
||||
@@ -326,7 +326,7 @@ return_type:
|
||||
{
|
||||
$$ = Carbon::MakeTuple(
|
||||
yylineno,
|
||||
new std::vector<std::pair<std::string, Carbon::Expression*>>());
|
||||
new std::vector<std::pair<std::string, const Carbon::Expression*>>());
|
||||
}
|
||||
| ARROW expression
|
||||
{ $$ = $2; }
|
||||
@@ -358,21 +358,21 @@ member_list:
|
||||
;
|
||||
alternative:
|
||||
identifier tuple
|
||||
{ $$ = new std::pair<std::string, Carbon::Expression*>($1, $2); }
|
||||
{ $$ = new std::pair<std::string, const Carbon::Expression*>($1, $2); }
|
||||
| identifier
|
||||
{
|
||||
$$ = new std::pair<std::string, Carbon::Expression*>(
|
||||
$$ = new std::pair<std::string, const Carbon::Expression*>(
|
||||
$1, Carbon::MakeTuple(
|
||||
yylineno,
|
||||
new std::vector<std::pair<std::string, Carbon::Expression*>>()));
|
||||
new std::vector<std::pair<std::string, const Carbon::Expression*>>()));
|
||||
}
|
||||
;
|
||||
alternative_list:
|
||||
// Empty
|
||||
{ $$ = new std::list<std::pair<std::string, Carbon::Expression*>>(); }
|
||||
{ $$ = new std::list<std::pair<std::string, const Carbon::Expression*>>(); }
|
||||
| alternative
|
||||
{
|
||||
$$ = new std::list<std::pair<std::string, Carbon::Expression*>>();
|
||||
$$ = new std::list<std::pair<std::string, const Carbon::Expression*>>();
|
||||
$$->push_front(*$1);
|
||||
}
|
||||
| alternative "," alternative_list
|
||||
|
||||
Reference in New Issue
Block a user