mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:54:47 +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:
@@ -314,9 +314,9 @@ void InitGlobals(std::list<Declaration>* fs) {
|
||||
|
||||
auto ChoiceDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
auto alts = new VarValues();
|
||||
for (auto kv : alternatives) {
|
||||
auto t = InterpExp(Env(), kv.second);
|
||||
alts->push_back(make_pair(kv.first, t));
|
||||
for (const auto& [name, signature] : alternatives) {
|
||||
auto t = InterpExp(Env(), &signature);
|
||||
alts->push_back(make_pair(name, t));
|
||||
}
|
||||
auto ct = Value::MakeChoiceTypeVal(name, alts);
|
||||
auto a = state->heap.AllocateValue(ct);
|
||||
@@ -342,10 +342,10 @@ auto StructDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
}
|
||||
|
||||
auto FunctionDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
auto pt = InterpExp(globals, definition->param_pattern);
|
||||
auto f = Value::MakeFunVal(definition->name, pt, definition->body);
|
||||
auto pt = InterpExp(globals, &definition.param_pattern);
|
||||
auto f = Value::MakeFunVal(definition.name, pt, definition.body);
|
||||
Address a = state->heap.AllocateValue(f);
|
||||
globals.Set(definition->name, a);
|
||||
globals.Set(definition.name, a);
|
||||
}
|
||||
|
||||
// Adds an entry in `globals` mapping the variable's name to the
|
||||
|
||||
@@ -664,9 +664,9 @@ auto CheckOrEnsureReturn(const Statement* stmt, bool void_return, int line_num)
|
||||
|
||||
auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types, Env values)
|
||||
-> struct FunctionDefinition* {
|
||||
auto param_res = TypeCheckExp(f->param_pattern, types, values, nullptr,
|
||||
auto param_res = TypeCheckExp(&f->param_pattern, types, values, nullptr,
|
||||
TCContext::PatternContext);
|
||||
auto return_type = InterpExp(values, f->return_type);
|
||||
auto return_type = InterpExp(values, &f->return_type);
|
||||
if (f->name == "main") {
|
||||
ExpectType(f->line_num, "return type of `main`", Value::MakeIntTypeVal(),
|
||||
return_type);
|
||||
@@ -675,18 +675,19 @@ 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 MakeFunDef(f->line_num, f->name, ReifyType(return_type, f->line_num),
|
||||
f->param_pattern, body);
|
||||
return new FunctionDefinition(MakeFunDef(f->line_num, f->name,
|
||||
*ReifyType(return_type, f->line_num),
|
||||
f->param_pattern, body));
|
||||
}
|
||||
|
||||
auto TypeOfFunDef(TypeEnv types, Env values, const FunctionDefinition* fun_def)
|
||||
-> const Value* {
|
||||
auto param_res = TypeCheckExp(fun_def->param_pattern, types, values, nullptr,
|
||||
auto param_res = TypeCheckExp(&fun_def->param_pattern, types, values, nullptr,
|
||||
TCContext::PatternContext);
|
||||
auto ret = InterpExp(values, fun_def->return_type);
|
||||
auto ret = InterpExp(values, &fun_def->return_type);
|
||||
if (ret->tag == ValKind::AutoTV) {
|
||||
auto f = TypeCheckFunDef(fun_def, types, values);
|
||||
ret = InterpExp(values, f->return_type);
|
||||
ret = InterpExp(values, &f->return_type);
|
||||
}
|
||||
return Value::MakeFunTypeVal(param_res.type, ret);
|
||||
}
|
||||
@@ -705,7 +706,7 @@ auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/, Env ct_top)
|
||||
}
|
||||
|
||||
auto FunctionDeclaration::Name() const -> std::string {
|
||||
return definition->name;
|
||||
return definition.name;
|
||||
}
|
||||
|
||||
auto StructDeclaration::Name() const -> std::string { return *definition.name; }
|
||||
@@ -729,7 +730,7 @@ auto StructDeclaration::TypeChecked(TypeEnv types, Env values) const
|
||||
|
||||
auto FunctionDeclaration::TypeChecked(TypeEnv types, Env values) const
|
||||
-> Declaration {
|
||||
return FunctionDeclaration(TypeCheckFunDef(definition, types, values));
|
||||
return FunctionDeclaration(*TypeCheckFunDef(&definition, types, values));
|
||||
}
|
||||
|
||||
auto ChoiceDeclaration::TypeChecked(TypeEnv types, Env values) const
|
||||
@@ -770,7 +771,7 @@ auto TopLevel(std::list<Declaration>* fs) -> TypeCheckContext {
|
||||
}
|
||||
|
||||
auto FunctionDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
|
||||
auto t = TypeOfFunDef(tops.types, tops.values, definition);
|
||||
auto t = TypeOfFunDef(tops.types, tops.values, &definition);
|
||||
tops.types.Set(Name(), t);
|
||||
InitGlobals(tops.values);
|
||||
}
|
||||
@@ -790,9 +791,9 @@ auto StructDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
|
||||
|
||||
auto ChoiceDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
|
||||
auto alts = new VarValues();
|
||||
for (auto a : alternatives) {
|
||||
auto t = InterpExp(tops.values, a.second);
|
||||
alts->push_back(std::make_pair(a.first, t));
|
||||
for (const auto& [name, signature] : alternatives) {
|
||||
auto t = InterpExp(tops.values, &signature);
|
||||
alts->push_back(std::make_pair(name, t));
|
||||
}
|
||||
auto ct = Value::MakeChoiceTypeVal(name, alts);
|
||||
Address a = state->heap.AllocateValue(ct);
|
||||
|
||||
Reference in New Issue
Block a user