mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:31:04 +01:00
Unify function declarations and definitions. (#896)
This commit is contained in:
@@ -24,8 +24,8 @@ cc_library(
|
||||
":field_path",
|
||||
":stack",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/ast:declaration",
|
||||
"//executable_semantics/ast:expression",
|
||||
"//executable_semantics/ast:function_definition",
|
||||
"//executable_semantics/ast:statement",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:error",
|
||||
@@ -97,7 +97,6 @@ cc_library(
|
||||
"//common:ostream",
|
||||
"//executable_semantics/ast:declaration",
|
||||
"//executable_semantics/ast:expression",
|
||||
"//executable_semantics/ast:function_definition",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:tracing_flag",
|
||||
"@llvm-project//llvm:Support",
|
||||
@@ -118,8 +117,8 @@ cc_library(
|
||||
":dictionary",
|
||||
":interpreter",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/ast:declaration",
|
||||
"//executable_semantics/ast:expression",
|
||||
"//executable_semantics/ast:function_definition",
|
||||
"//executable_semantics/ast:statement",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:tracing_flag",
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
|
||||
@@ -26,11 +26,11 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
|
||||
source_loc,
|
||||
arena->New<IntrinsicExpression>(IntrinsicExpression::Intrinsic::Print),
|
||||
false);
|
||||
auto print = arena->New<FunctionDeclaration>(arena->New<FunctionDefinition>(
|
||||
auto print = arena->New<FunctionDeclaration>(
|
||||
source_loc, "Print", std::vector<GenericBinding>(),
|
||||
arena->New<TuplePattern>(source_loc, print_params),
|
||||
arena->New<ExpressionPattern>(arena->New<TupleLiteral>(source_loc)),
|
||||
/*is_omitted_return_type=*/false, print_return));
|
||||
/*is_omitted_return_type=*/false, print_return);
|
||||
declarations->insert(declarations->begin(), print);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
#include <vector>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
@@ -109,8 +109,7 @@ auto Interpreter::EvalPrim(Operator op,
|
||||
void Interpreter::InitEnv(const Declaration& d, Env* env) {
|
||||
switch (d.kind()) {
|
||||
case Declaration::Kind::FunctionDeclaration: {
|
||||
const FunctionDefinition& func_def =
|
||||
cast<FunctionDeclaration>(d).definition();
|
||||
const auto& func_def = cast<FunctionDeclaration>(d);
|
||||
Env new_env = *env;
|
||||
// Bring the deduced parameters into scope.
|
||||
for (const auto& deduced : func_def.deduced_parameters()) {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
@@ -50,7 +50,7 @@ static void SetStaticType(Nonnull<Pattern*> pattern,
|
||||
|
||||
// Sets the static type of `definition`. Can be called multiple times on
|
||||
// the same node, so long as the types are the same on each call.
|
||||
static void SetStaticType(Nonnull<FunctionDefinition*> definition,
|
||||
static void SetStaticType(Nonnull<FunctionDeclaration*> definition,
|
||||
Nonnull<const Value*> type) {
|
||||
if (definition->has_static_type()) {
|
||||
CHECK(TypeEqual(&definition->static_type(), type));
|
||||
@@ -1027,7 +1027,7 @@ void TypeChecker::ExpectReturnOnAllPaths(
|
||||
// a function.
|
||||
// TODO: Add checking to function definitions to ensure that
|
||||
// all deduced type parameters will be deduced.
|
||||
auto TypeChecker::TypeCheckFunDef(FunctionDefinition* f, TypeEnv types,
|
||||
auto TypeChecker::TypeCheckFunDef(FunctionDeclaration* f, TypeEnv types,
|
||||
Env values) -> TCResult {
|
||||
// Bring the deduced parameters into scope
|
||||
for (const auto& deduced : f->deduced_parameters()) {
|
||||
@@ -1068,7 +1068,7 @@ auto TypeChecker::TypeCheckFunDef(FunctionDefinition* f, TypeEnv types,
|
||||
}
|
||||
|
||||
auto TypeChecker::TypeOfFunDef(TypeEnv types, Env values,
|
||||
FunctionDefinition* fun_def)
|
||||
FunctionDeclaration* fun_def)
|
||||
-> Nonnull<const Value*> {
|
||||
// Bring the deduced parameters into scope
|
||||
for (const auto& deduced : fun_def->deduced_parameters()) {
|
||||
@@ -1120,7 +1120,7 @@ auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/,
|
||||
static auto GetName(const Declaration& d) -> const std::string& {
|
||||
switch (d.kind()) {
|
||||
case Declaration::Kind::FunctionDeclaration:
|
||||
return cast<FunctionDeclaration>(d).definition().name();
|
||||
return cast<FunctionDeclaration>(d).name();
|
||||
case Declaration::Kind::ClassDeclaration:
|
||||
return cast<ClassDeclaration>(d).definition().name();
|
||||
case Declaration::Kind::ChoiceDeclaration:
|
||||
@@ -1140,8 +1140,7 @@ void TypeChecker::TypeCheck(Nonnull<Declaration*> d, const TypeEnv& types,
|
||||
const Env& values) {
|
||||
switch (d->kind()) {
|
||||
case Declaration::Kind::FunctionDeclaration:
|
||||
TypeCheckFunDef(&cast<FunctionDeclaration>(*d).definition(), types,
|
||||
values);
|
||||
TypeCheckFunDef(&cast<FunctionDeclaration>(*d), types, values);
|
||||
return;
|
||||
case Declaration::Kind::ClassDeclaration:
|
||||
// TODO
|
||||
@@ -1176,7 +1175,7 @@ void TypeChecker::TypeCheck(Nonnull<Declaration*> d, const TypeEnv& types,
|
||||
void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
|
||||
switch (d->kind()) {
|
||||
case Declaration::Kind::FunctionDeclaration: {
|
||||
FunctionDefinition& func_def = cast<FunctionDeclaration>(*d).definition();
|
||||
FunctionDeclaration& func_def = cast<FunctionDeclaration>(*d);
|
||||
auto t = TypeOfFunDef(tops->types, tops->values, &func_def);
|
||||
tops->types.Set(func_def.name(), t);
|
||||
interpreter.InitEnv(*d, &tops->values);
|
||||
|
||||
@@ -109,7 +109,7 @@ class TypeChecker {
|
||||
Nonnull<ReturnTypeContext*> return_type_context)
|
||||
-> TCResult;
|
||||
|
||||
auto TypeCheckFunDef(FunctionDefinition* f, TypeEnv types, Env values)
|
||||
auto TypeCheckFunDef(FunctionDeclaration* f, TypeEnv types, Env values)
|
||||
-> TCResult;
|
||||
|
||||
auto TypeCheckCase(Nonnull<const Value*> expected, Nonnull<Pattern*> pat,
|
||||
@@ -117,7 +117,7 @@ class TypeChecker {
|
||||
Nonnull<ReturnTypeContext*> return_type_context)
|
||||
-> Match::Clause;
|
||||
|
||||
auto TypeOfFunDef(TypeEnv types, Env values, FunctionDefinition* fun_def)
|
||||
auto TypeOfFunDef(TypeEnv types, Env values, FunctionDeclaration* fun_def)
|
||||
-> Nonnull<const Value*>;
|
||||
auto TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/, Env ct_top)
|
||||
-> Nonnull<const Value*>;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
#include "executable_semantics/interpreter/address.h"
|
||||
|
||||
Reference in New Issue
Block a user