mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Remove global_arena (#814)
With this, only main.cpp instantiates an arena. Maybe we'll want to split that up more later (e.g., so that the runtime interpreter uses its own arena), but given the intent to have type-checking update the AST, I thought this was a reasonable approach for now in order to avoid ownership complexities. Fixes #769
This commit is contained in:
@@ -56,8 +56,8 @@ auto TupleValue::FindField(const std::string& name) const
|
||||
|
||||
namespace {
|
||||
|
||||
auto GetMember(Ptr<const Value> v, const std::string& f, SourceLocation loc)
|
||||
-> Ptr<const Value> {
|
||||
auto GetMember(Ptr<Arena> arena, Ptr<const Value> v, const std::string& f,
|
||||
SourceLocation loc) -> Ptr<const Value> {
|
||||
switch (v->Tag()) {
|
||||
case Value::Kind::StructValue: {
|
||||
std::optional<Ptr<const Value>> field =
|
||||
@@ -79,7 +79,7 @@ auto GetMember(Ptr<const Value> v, const std::string& f, SourceLocation loc)
|
||||
if (!FindInVarValues(f, choice.Alternatives())) {
|
||||
FATAL_RUNTIME_ERROR(loc) << "alternative " << f << " not in " << *v;
|
||||
}
|
||||
return global_arena->New<AlternativeConstructorValue>(f, choice.Name());
|
||||
return arena->New<AlternativeConstructorValue>(f, choice.Name());
|
||||
}
|
||||
default:
|
||||
FATAL() << "field access not allowed for value " << *v;
|
||||
@@ -88,18 +88,18 @@ auto GetMember(Ptr<const Value> v, const std::string& f, SourceLocation loc)
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Value::GetField(const FieldPath& path, SourceLocation loc) const
|
||||
-> Ptr<const Value> {
|
||||
auto Value::GetField(Ptr<Arena> arena, const FieldPath& path,
|
||||
SourceLocation loc) const -> Ptr<const Value> {
|
||||
Ptr<const Value> value(this);
|
||||
for (const std::string& field : path.components) {
|
||||
value = GetMember(value, field, loc);
|
||||
value = GetMember(arena, value, field, loc);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
auto SetFieldImpl(Ptr<const Value> value,
|
||||
auto SetFieldImpl(Ptr<Arena> arena, Ptr<const Value> value,
|
||||
std::vector<std::string>::const_iterator path_begin,
|
||||
std::vector<std::string>::const_iterator path_end,
|
||||
Ptr<const Value> field_value, SourceLocation loc)
|
||||
@@ -109,7 +109,7 @@ auto SetFieldImpl(Ptr<const Value> value,
|
||||
}
|
||||
switch (value->Tag()) {
|
||||
case Value::Kind::StructValue: {
|
||||
return SetFieldImpl(cast<StructValue>(*value).Inits(), path_begin,
|
||||
return SetFieldImpl(arena, cast<StructValue>(*value).Inits(), path_begin,
|
||||
path_end, field_value, loc);
|
||||
}
|
||||
case Value::Kind::TupleValue: {
|
||||
@@ -122,9 +122,9 @@ auto SetFieldImpl(Ptr<const Value> value,
|
||||
FATAL_RUNTIME_ERROR(loc)
|
||||
<< "field " << *path_begin << " not in " << *value;
|
||||
}
|
||||
it->value =
|
||||
SetFieldImpl(it->value, path_begin + 1, path_end, field_value, loc);
|
||||
return global_arena->New<TupleValue>(elements);
|
||||
it->value = SetFieldImpl(arena, it->value, path_begin + 1, path_end,
|
||||
field_value, loc);
|
||||
return arena->New<TupleValue>(elements);
|
||||
}
|
||||
default:
|
||||
FATAL() << "field access not allowed for value " << *value;
|
||||
@@ -133,9 +133,10 @@ auto SetFieldImpl(Ptr<const Value> value,
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Value::SetField(const FieldPath& path, Ptr<const Value> field_value,
|
||||
SourceLocation loc) const -> Ptr<const Value> {
|
||||
return SetFieldImpl(Ptr<const Value>(this), path.components.begin(),
|
||||
auto Value::SetField(Ptr<Arena> arena, const FieldPath& path,
|
||||
Ptr<const Value> field_value, SourceLocation loc) const
|
||||
-> Ptr<const Value> {
|
||||
return SetFieldImpl(arena, Ptr<const Value>(this), path.components.begin(),
|
||||
path.components.end(), field_value, loc);
|
||||
}
|
||||
|
||||
@@ -253,64 +254,64 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
}
|
||||
|
||||
auto CopyVal(Ptr<const Value> val, SourceLocation loc) -> Ptr<const Value> {
|
||||
auto CopyVal(Ptr<Arena> arena, Ptr<const Value> val, SourceLocation loc)
|
||||
-> Ptr<const Value> {
|
||||
switch (val->Tag()) {
|
||||
case Value::Kind::TupleValue: {
|
||||
std::vector<TupleElement> elements;
|
||||
for (const TupleElement& element : cast<TupleValue>(*val).Elements()) {
|
||||
elements.push_back(
|
||||
{.name = element.name, .value = CopyVal(element.value, loc)});
|
||||
elements.push_back({.name = element.name,
|
||||
.value = CopyVal(arena, element.value, loc)});
|
||||
}
|
||||
return global_arena->New<TupleValue>(std::move(elements));
|
||||
return arena->New<TupleValue>(std::move(elements));
|
||||
}
|
||||
case Value::Kind::AlternativeValue: {
|
||||
const auto& alt = cast<AlternativeValue>(*val);
|
||||
Ptr<const Value> arg = CopyVal(alt.Argument(), loc);
|
||||
return global_arena->New<AlternativeValue>(alt.AltName(),
|
||||
alt.ChoiceName(), arg);
|
||||
Ptr<const Value> arg = CopyVal(arena, alt.Argument(), loc);
|
||||
return arena->New<AlternativeValue>(alt.AltName(), alt.ChoiceName(), arg);
|
||||
}
|
||||
case Value::Kind::StructValue: {
|
||||
const auto& s = cast<StructValue>(*val);
|
||||
Ptr<const Value> inits = CopyVal(s.Inits(), loc);
|
||||
return global_arena->New<StructValue>(s.Type(), inits);
|
||||
Ptr<const Value> inits = CopyVal(arena, s.Inits(), loc);
|
||||
return arena->New<StructValue>(s.Type(), inits);
|
||||
}
|
||||
case Value::Kind::IntValue:
|
||||
return global_arena->New<IntValue>(cast<IntValue>(*val).Val());
|
||||
return arena->New<IntValue>(cast<IntValue>(*val).Val());
|
||||
case Value::Kind::BoolValue:
|
||||
return global_arena->New<BoolValue>(cast<BoolValue>(*val).Val());
|
||||
return arena->New<BoolValue>(cast<BoolValue>(*val).Val());
|
||||
case Value::Kind::FunctionValue: {
|
||||
const auto& fn_value = cast<FunctionValue>(*val);
|
||||
return global_arena->New<FunctionValue>(fn_value.Name(), fn_value.Param(),
|
||||
fn_value.Body());
|
||||
return arena->New<FunctionValue>(fn_value.Name(), fn_value.Param(),
|
||||
fn_value.Body());
|
||||
}
|
||||
case Value::Kind::PointerValue:
|
||||
return global_arena->New<PointerValue>(cast<PointerValue>(*val).Val());
|
||||
return arena->New<PointerValue>(cast<PointerValue>(*val).Val());
|
||||
case Value::Kind::ContinuationValue:
|
||||
// Copying a continuation is "shallow".
|
||||
return val;
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*val);
|
||||
return global_arena->New<FunctionType>(fn_type.Deduced(),
|
||||
CopyVal(fn_type.Param(), loc),
|
||||
CopyVal(fn_type.Ret(), loc));
|
||||
return arena->New<FunctionType>(fn_type.Deduced(),
|
||||
CopyVal(arena, fn_type.Param(), loc),
|
||||
CopyVal(arena, fn_type.Ret(), loc));
|
||||
}
|
||||
case Value::Kind::PointerType:
|
||||
return global_arena->New<PointerType>(
|
||||
CopyVal(cast<PointerType>(*val).Type(), loc));
|
||||
return arena->New<PointerType>(
|
||||
CopyVal(arena, cast<PointerType>(*val).Type(), loc));
|
||||
case Value::Kind::IntType:
|
||||
return global_arena->New<IntType>();
|
||||
return arena->New<IntType>();
|
||||
case Value::Kind::BoolType:
|
||||
return global_arena->New<BoolType>();
|
||||
return arena->New<BoolType>();
|
||||
case Value::Kind::TypeType:
|
||||
return global_arena->New<TypeType>();
|
||||
return arena->New<TypeType>();
|
||||
case Value::Kind::AutoType:
|
||||
return global_arena->New<AutoType>();
|
||||
return arena->New<AutoType>();
|
||||
case Value::Kind::ContinuationType:
|
||||
return global_arena->New<ContinuationType>();
|
||||
return arena->New<ContinuationType>();
|
||||
case Value::Kind::StringType:
|
||||
return global_arena->New<StringType>();
|
||||
return arena->New<StringType>();
|
||||
case Value::Kind::StringValue:
|
||||
return global_arena->New<StringValue>(cast<StringValue>(*val).Val());
|
||||
return arena->New<StringValue>(cast<StringValue>(*val).Val());
|
||||
case Value::Kind::VariableType:
|
||||
case Value::Kind::ClassType:
|
||||
case Value::Kind::ChoiceType:
|
||||
|
||||
Reference in New Issue
Block a user