diff --git a/executable_semantics/ast/declaration.cpp b/executable_semantics/ast/declaration.cpp index 9a0de4d9750f..817cc447ee8b 100644 --- a/executable_semantics/ast/declaration.cpp +++ b/executable_semantics/ast/declaration.cpp @@ -8,7 +8,7 @@ namespace Carbon { -void FunctionDeclaration::Print() const { PrintFunDef(definition); } +void FunctionDeclaration::Print() const { definition.Print(); } void StructDeclaration::Print() const { std::cout << "struct " << *definition.name << " {" << std::endl; diff --git a/executable_semantics/ast/function_definition.cpp b/executable_semantics/ast/function_definition.cpp index 55bca9ba7a94..4599758bfccb 100644 --- a/executable_semantics/ast/function_definition.cpp +++ b/executable_semantics/ast/function_definition.cpp @@ -8,32 +8,18 @@ namespace Carbon { -auto MakeFunDef(int line_num, std::string name, const Expression* ret_type, - const Expression* param_pattern, const Statement* body) - -> FunctionDefinition { - FunctionDefinition f; - f.line_num = line_num; - f.name = std::move(name); - f.return_type = ret_type; - f.param_pattern = param_pattern; - f.body = body; - return f; -} - -void PrintFunDefDepth(const FunctionDefinition& f, int depth) { - std::cout << "fn " << f.name << " "; - PrintExp(f.param_pattern); +void FunctionDefinition::PrintDepth(int depth) const { + std::cout << "fn " << name << " "; + PrintExp(param_pattern); std::cout << " -> "; - PrintExp(f.return_type); - if (f.body) { + PrintExp(return_type); + if (body) { std::cout << " {" << std::endl; - PrintStatement(f.body, depth); + PrintStatement(body, depth); std::cout << std::endl << "}" << std::endl; } else { std::cout << ";" << std::endl; } } -void PrintFunDef(const FunctionDefinition& f) { PrintFunDefDepth(f, -1); } - } // namespace Carbon diff --git a/executable_semantics/ast/function_definition.h b/executable_semantics/ast/function_definition.h index 1ca710e81a39..b2dc5c45514f 100644 --- a/executable_semantics/ast/function_definition.h +++ b/executable_semantics/ast/function_definition.h @@ -11,6 +11,19 @@ namespace Carbon { struct FunctionDefinition { + FunctionDefinition() = default; + FunctionDefinition(int line_num, std::string name, + const Expression* param_pattern, + const Expression* return_type, const Statement* body) + : line_num(line_num), + name(std::move(name)), + param_pattern(param_pattern), + return_type(return_type), + body(body) {} + + void Print() const { PrintDepth(-1); } + void PrintDepth(int depth) const; + int line_num; std::string name; const Expression* param_pattern; @@ -18,12 +31,6 @@ struct FunctionDefinition { const Statement* body; }; -auto MakeFunDef(int line_num, std::string name, const Expression* ret_type, - const Expression* param, const Statement* body) - -> FunctionDefinition; -void PrintFunDef(const FunctionDefinition&); -void PrintFunDefDepth(const FunctionDefinition&, int); - } // namespace Carbon #endif // EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_ diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index d1d29b05ed18..d1563a9de430 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -661,9 +661,8 @@ auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types, Env values) auto res = TypeCheckStmt(f->body, param_res.types, values, return_type); bool void_return = TypeEqual(return_type, Value::MakeUnitTypeVal()); auto body = CheckOrEnsureReturn(res.stmt, void_return, f->line_num); - return new FunctionDefinition(MakeFunDef(f->line_num, f->name, - ReifyType(return_type, f->line_num), - f->param_pattern, body)); + return new FunctionDefinition(f->line_num, f->name, f->param_pattern, + ReifyType(return_type, f->line_num), body); } auto TypeOfFunDef(TypeEnv types, Env values, const FunctionDefinition* fun_def) diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index 733351f8a6e2..a8968f8941b5 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -370,17 +370,18 @@ return_type: ; function_definition: FN identifier tuple return_type "{" statement_list "}" - { $$ = MakeFunDef(yylineno, $2, $4, $3, $6); } + { $$ = Carbon::FunctionDefinition(yylineno, $2, $3, $4, $6); } | FN identifier tuple DBLARROW expression ";" { - $$ = Carbon::MakeFunDef(yylineno, $2, - Carbon::Expression::MakeAutoTypeLiteral(yylineno), - $3, Carbon::Statement::MakeReturn(yylineno, $5)); + $$ = Carbon::FunctionDefinition( + yylineno, $2, $3, + Carbon::Expression::MakeAutoTypeLiteral(yylineno), + Carbon::Statement::MakeReturn(yylineno, $5)); } ; function_declaration: FN identifier tuple return_type ";" - { $$ = MakeFunDef(yylineno, $2, $4, $3, 0); } + { $$ = Carbon::FunctionDefinition(yylineno, $2, $3, $4, 0); } ; variable_declaration: identifier ":" expression { $$ = MakeField(yylineno, $1, $3); }