mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Migrate declaration parsing to value semantics (#614)
* Use Expression by value in FunctionDefinition * Migrate choice declarations to value semantics * Migrate Declaration parsing to value semantics
This commit is contained in:
@@ -92,10 +92,10 @@ void yy::parser::error(
|
||||
%token <int> integer_literal
|
||||
%token <char*> identifier
|
||||
%type <char*> designator
|
||||
%type <Carbon::Declaration*> declaration
|
||||
%type <Carbon::FunctionDefinition*> function_declaration
|
||||
%type <Carbon::FunctionDefinition*> function_definition
|
||||
%type <std::list<Carbon::Declaration>*> declaration_list
|
||||
%type <Carbon::Declaration> declaration
|
||||
%type <Carbon::FunctionDefinition> function_declaration
|
||||
%type <Carbon::FunctionDefinition> function_definition
|
||||
%type <std::list<Carbon::Declaration>> declaration_list
|
||||
%type <const Carbon::Statement*> statement
|
||||
%type <const Carbon::Statement*> optional_else
|
||||
%type <const Carbon::Statement*> statement_list
|
||||
@@ -110,8 +110,8 @@ void yy::parser::error(
|
||||
%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, const Carbon::Expression*>*> alternative
|
||||
%type <std::list<std::pair<std::string, const Carbon::Expression*>>*> alternative_list
|
||||
%type <std::pair<std::string, Carbon::Expression>> alternative
|
||||
%type <std::list<std::pair<std::string, 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
|
||||
@@ -356,16 +356,16 @@ return_type:
|
||||
;
|
||||
function_definition:
|
||||
FN identifier tuple return_type "{" statement_list "}"
|
||||
{ $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
|
||||
{ $$ = MakeFunDef(yylineno, $2, *$4, *$3, $6); }
|
||||
| FN identifier tuple DBLARROW expression ";"
|
||||
{
|
||||
$$ = Carbon::MakeFunDef(yylineno, $2, Carbon::Expression::MakeAutoType(yylineno), $3,
|
||||
$$ = Carbon::MakeFunDef(yylineno, $2, *Carbon::Expression::MakeAutoType(yylineno), *$3,
|
||||
Carbon::Statement::MakeReturn(yylineno, *$5));
|
||||
}
|
||||
;
|
||||
function_declaration:
|
||||
FN identifier tuple return_type ";"
|
||||
{ $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
|
||||
{ $$ = MakeFunDef(yylineno, $2, *$4, *$3, 0); }
|
||||
;
|
||||
variable_declaration: identifier ":" expression
|
||||
{ $$ = MakeField(yylineno, $1, $3); }
|
||||
@@ -381,52 +381,52 @@ member_list:
|
||||
;
|
||||
alternative:
|
||||
identifier tuple
|
||||
{ $$ = new std::pair<std::string, const Carbon::Expression*>($1, $2); }
|
||||
{ $$ = std::pair<std::string, Carbon::Expression>($1, *$2); }
|
||||
| identifier
|
||||
{
|
||||
$$ = new std::pair<std::string, const Carbon::Expression*>(
|
||||
$1, Carbon::Expression::MakeTuple(yylineno, {}));
|
||||
$$ = std::pair<std::string, Carbon::Expression>(
|
||||
$1, *Carbon::Expression::MakeTuple(yylineno, {}));
|
||||
}
|
||||
;
|
||||
alternative_list:
|
||||
// Empty
|
||||
{ $$ = new std::list<std::pair<std::string, const Carbon::Expression*>>(); }
|
||||
{ $$ = std::list<std::pair<std::string, Carbon::Expression>>(); }
|
||||
| alternative
|
||||
{
|
||||
$$ = new std::list<std::pair<std::string, const Carbon::Expression*>>();
|
||||
$$->push_front(*$1);
|
||||
$$ = std::list<std::pair<std::string, Carbon::Expression>>();
|
||||
$$.push_front($1);
|
||||
}
|
||||
| alternative "," alternative_list
|
||||
{ $$ = $3; $$->push_front(*$1); }
|
||||
{ $$ = std::move($3); $$.push_front($1); }
|
||||
;
|
||||
declaration:
|
||||
function_definition
|
||||
{ $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
|
||||
{ $$ = Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
|
||||
| function_declaration
|
||||
{ $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
|
||||
{ $$ = Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
|
||||
| STRUCT identifier "{" member_list "}"
|
||||
{
|
||||
$$ = new Carbon::Declaration(
|
||||
$$ = Carbon::Declaration(
|
||||
Carbon::StructDeclaration{yylineno, $2, $4});
|
||||
}
|
||||
| CHOICE identifier "{" alternative_list "}"
|
||||
{
|
||||
$$ = new Carbon::Declaration(
|
||||
Carbon::ChoiceDeclaration{yylineno, $2, std::list(*$4)});
|
||||
$$ = Carbon::Declaration(
|
||||
Carbon::ChoiceDeclaration{yylineno, $2, $4});
|
||||
}
|
||||
| VAR variable_declaration "=" expression ";"
|
||||
{
|
||||
$$ = new Carbon::Declaration(
|
||||
$$ = Carbon::Declaration(
|
||||
Carbon::VariableDeclaration(yylineno, *$2->u.field.name, $2->u.field.type, $4));
|
||||
}
|
||||
;
|
||||
declaration_list:
|
||||
// Empty
|
||||
{ $$ = new std::list<Carbon::Declaration>(); }
|
||||
{ $$ = std::list<Carbon::Declaration>(); }
|
||||
| declaration declaration_list
|
||||
{
|
||||
$$ = $2;
|
||||
$$->push_front(*$1);
|
||||
$$.push_front($1);
|
||||
}
|
||||
;
|
||||
%%
|
||||
|
||||
Reference in New Issue
Block a user