mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
@@ -133,12 +133,11 @@ void InitEnv(const Declaration& d, Env* env) {
|
||||
break;
|
||||
}
|
||||
|
||||
case Declaration::Kind::StructDeclaration: {
|
||||
const StructDefinition& struct_def =
|
||||
cast<StructDeclaration>(d).Definition();
|
||||
case Declaration::Kind::ClassDeclaration: {
|
||||
const ClassDefinition& class_def = cast<ClassDeclaration>(d).Definition();
|
||||
VarValues fields;
|
||||
VarValues methods;
|
||||
for (const Member* m : struct_def.members) {
|
||||
for (const Member* m : class_def.members) {
|
||||
switch (m->Tag()) {
|
||||
case Member::Kind::FieldMember: {
|
||||
const BindingPattern* binding = cast<FieldMember>(*m).Binding();
|
||||
@@ -150,10 +149,10 @@ void InitEnv(const Declaration& d, Env* env) {
|
||||
}
|
||||
}
|
||||
}
|
||||
auto st = global_arena->RawNew<StructType>(
|
||||
struct_def.name, std::move(fields), std::move(methods));
|
||||
auto st = global_arena->RawNew<ClassType>(
|
||||
class_def.name, std::move(fields), std::move(methods));
|
||||
auto a = state->heap.AllocateValue(st);
|
||||
env->Set(struct_def.name, a);
|
||||
env->Set(class_def.name, a);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -216,7 +215,7 @@ void CallFunction(int line_num, std::vector<const Value*> operas,
|
||||
state->stack.Push(frame);
|
||||
break;
|
||||
}
|
||||
case Value::Kind::StructType: {
|
||||
case Value::Kind::ClassType: {
|
||||
const Value* arg = CopyVal(operas[1], line_num);
|
||||
const Value* sv = global_arena->RawNew<StructValue>(operas[0], arg);
|
||||
Ptr<Frame> frame = state->stack.Top();
|
||||
|
||||
@@ -76,9 +76,9 @@ static auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
}
|
||||
return global_arena->RawNew<TupleLiteral>(0, args);
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ClassType:
|
||||
return global_arena->RawNew<IdentifierExpression>(
|
||||
0, cast<StructType>(*t).Name());
|
||||
0, cast<ClassType>(*t).Name());
|
||||
case Value::Kind::ChoiceType:
|
||||
return global_arena->RawNew<IdentifierExpression>(
|
||||
0, cast<ChoiceType>(*t).Name());
|
||||
@@ -174,7 +174,7 @@ static auto ArgumentDeduction(int line_num, TypeEnv deduced, const Value* param,
|
||||
}
|
||||
// For the following cases, we check for type equality.
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ClassType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::IntType:
|
||||
case Value::Kind::BoolType:
|
||||
@@ -231,7 +231,7 @@ static auto Substitute(TypeEnv dict, const Value* type) -> const Value* {
|
||||
case Value::Kind::IntType:
|
||||
case Value::Kind::BoolType:
|
||||
case Value::Kind::TypeType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ClassType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::StringType:
|
||||
@@ -316,10 +316,10 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
auto res = TypeCheckExp(access.Aggregate(), types, values);
|
||||
auto t = res.type;
|
||||
switch (t->Tag()) {
|
||||
case Value::Kind::StructType: {
|
||||
const auto& t_struct = cast<StructType>(*t);
|
||||
case Value::Kind::ClassType: {
|
||||
const auto& t_class = cast<ClassType>(*t);
|
||||
// Search for a field
|
||||
for (auto& field : t_struct.Fields()) {
|
||||
for (auto& field : t_class.Fields()) {
|
||||
if (access.Field() == field.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->RawNew<FieldAccessExpression>(
|
||||
@@ -328,7 +328,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
}
|
||||
}
|
||||
// Search for a method
|
||||
for (auto& method : t_struct.Methods()) {
|
||||
for (auto& method : t_class.Methods()) {
|
||||
if (access.Field() == method.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->RawNew<FieldAccessExpression>(
|
||||
@@ -337,7 +337,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "struct " << t_struct.Name() << " does not have a field named "
|
||||
<< "class " << t_class.Name() << " does not have a field named "
|
||||
<< access.Field();
|
||||
}
|
||||
case Value::Kind::TupleValue: {
|
||||
@@ -942,8 +942,8 @@ static auto TypeOfFunDef(TypeEnv types, Env values,
|
||||
param_res.type, ret);
|
||||
}
|
||||
|
||||
static auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/,
|
||||
Env ct_top) -> const Value* {
|
||||
static auto TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/,
|
||||
Env ct_top) -> const Value* {
|
||||
VarValues fields;
|
||||
VarValues methods;
|
||||
for (const Member* m : sd->members) {
|
||||
@@ -966,16 +966,16 @@ static auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/,
|
||||
}
|
||||
}
|
||||
}
|
||||
return global_arena->RawNew<StructType>(sd->name, std::move(fields),
|
||||
std::move(methods));
|
||||
return global_arena->RawNew<ClassType>(sd->name, std::move(fields),
|
||||
std::move(methods));
|
||||
}
|
||||
|
||||
static auto GetName(const Declaration& d) -> const std::string& {
|
||||
switch (d.Tag()) {
|
||||
case Declaration::Kind::FunctionDeclaration:
|
||||
return cast<FunctionDeclaration>(d).Definition().name;
|
||||
case Declaration::Kind::StructDeclaration:
|
||||
return cast<StructDeclaration>(d).Definition().name;
|
||||
case Declaration::Kind::ClassDeclaration:
|
||||
return cast<ClassDeclaration>(d).Definition().name;
|
||||
case Declaration::Kind::ChoiceDeclaration:
|
||||
return cast<ChoiceDeclaration>(d).Name();
|
||||
case Declaration::Kind::VariableDeclaration: {
|
||||
@@ -996,11 +996,11 @@ auto MakeTypeChecked(const Ptr<const Declaration> d, const TypeEnv& types,
|
||||
return global_arena->New<FunctionDeclaration>(TypeCheckFunDef(
|
||||
&cast<FunctionDeclaration>(*d).Definition(), types, values));
|
||||
|
||||
case Declaration::Kind::StructDeclaration: {
|
||||
const StructDefinition& struct_def =
|
||||
cast<StructDeclaration>(*d).Definition();
|
||||
case Declaration::Kind::ClassDeclaration: {
|
||||
const ClassDefinition& class_def =
|
||||
cast<ClassDeclaration>(*d).Definition();
|
||||
std::list<Member*> fields;
|
||||
for (Member* m : struct_def.members) {
|
||||
for (Member* m : class_def.members) {
|
||||
switch (m->Tag()) {
|
||||
case Member::Kind::FieldMember:
|
||||
// TODO: Interpret the type expression and store the result.
|
||||
@@ -1008,8 +1008,8 @@ auto MakeTypeChecked(const Ptr<const Declaration> d, const TypeEnv& types,
|
||||
break;
|
||||
}
|
||||
}
|
||||
return global_arena->New<StructDeclaration>(
|
||||
struct_def.line_num, struct_def.name, std::move(fields));
|
||||
return global_arena->New<ClassDeclaration>(
|
||||
class_def.line_num, class_def.name, std::move(fields));
|
||||
}
|
||||
|
||||
case Declaration::Kind::ChoiceDeclaration:
|
||||
@@ -1049,21 +1049,20 @@ static void TopLevel(const Declaration& d, TypeCheckContext* tops) {
|
||||
break;
|
||||
}
|
||||
|
||||
case Declaration::Kind::StructDeclaration: {
|
||||
const StructDefinition& struct_def =
|
||||
cast<StructDeclaration>(d).Definition();
|
||||
auto st = TypeOfStructDef(&struct_def, tops->types, tops->values);
|
||||
case Declaration::Kind::ClassDeclaration: {
|
||||
const ClassDefinition& class_def = cast<ClassDeclaration>(d).Definition();
|
||||
auto st = TypeOfClassDef(&class_def, tops->types, tops->values);
|
||||
Address a = state->heap.AllocateValue(st);
|
||||
tops->values.Set(struct_def.name, a); // Is this obsolete?
|
||||
tops->values.Set(class_def.name, a); // Is this obsolete?
|
||||
std::vector<TupleElement> field_types;
|
||||
for (const auto& [field_name, field_value] :
|
||||
cast<StructType>(*st).Fields()) {
|
||||
cast<ClassType>(*st).Fields()) {
|
||||
field_types.push_back({.name = field_name, .value = field_value});
|
||||
}
|
||||
auto fun_ty = global_arena->RawNew<FunctionType>(
|
||||
std::vector<GenericBinding>(),
|
||||
global_arena->RawNew<TupleValue>(std::move(field_types)), st);
|
||||
tops->types.Set(struct_def.name, fun_ty);
|
||||
tops->types.Set(class_def.name, fun_ty);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -163,7 +163,7 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
case Value::Kind::StructValue: {
|
||||
const auto& s = cast<StructValue>(*this);
|
||||
out << cast<StructType>(*s.Type()).Name() << *s.Inits();
|
||||
out << cast<ClassType>(*s.Type()).Name() << *s.Inits();
|
||||
break;
|
||||
}
|
||||
case Value::Kind::TupleValue: {
|
||||
@@ -223,8 +223,8 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
out << *fn_type.Param() << " -> " << *fn_type.Ret();
|
||||
break;
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
out << "struct " << cast<StructType>(*this).Name();
|
||||
case Value::Kind::ClassType:
|
||||
out << "struct " << cast<ClassType>(*this).Name();
|
||||
break;
|
||||
case Value::Kind::ChoiceType:
|
||||
out << "choice " << cast<ChoiceType>(*this).Name();
|
||||
@@ -307,7 +307,7 @@ auto CopyVal(const Value* val, int line_num) -> const Value* {
|
||||
case Value::Kind::StringValue:
|
||||
return global_arena->RawNew<StringValue>(cast<StringValue>(*val).Val());
|
||||
case Value::Kind::VariableType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ClassType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::BindingPlaceholderValue:
|
||||
case Value::Kind::AlternativeConstructorValue:
|
||||
@@ -330,8 +330,8 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
|
||||
return TypeEqual(fn1.Param(), fn2.Param()) &&
|
||||
TypeEqual(fn1.Ret(), fn2.Ret());
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
return cast<StructType>(*t1).Name() == cast<StructType>(*t2).Name();
|
||||
case Value::Kind::ClassType:
|
||||
return cast<ClassType>(*t1).Name() == cast<ClassType>(*t2).Name();
|
||||
case Value::Kind::ChoiceType:
|
||||
return cast<ChoiceType>(*t1).Name() == cast<ChoiceType>(*t2).Name();
|
||||
case Value::Kind::TupleValue: {
|
||||
@@ -412,7 +412,7 @@ auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
|
||||
case Value::Kind::FunctionType:
|
||||
case Value::Kind::PointerType:
|
||||
case Value::Kind::AutoType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ClassType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::VariableType:
|
||||
|
||||
@@ -46,7 +46,7 @@ class Value {
|
||||
FunctionType,
|
||||
PointerType,
|
||||
AutoType,
|
||||
StructType,
|
||||
ClassType,
|
||||
ChoiceType,
|
||||
ContinuationType, // The type of a continuation.
|
||||
VariableType, // e.g., generic type parameters.
|
||||
@@ -361,16 +361,16 @@ class AutoType : public Value {
|
||||
};
|
||||
|
||||
// A struct type.
|
||||
class StructType : public Value {
|
||||
class ClassType : public Value {
|
||||
public:
|
||||
StructType(std::string name, VarValues fields, VarValues methods)
|
||||
: Value(Kind::StructType),
|
||||
ClassType(std::string name, VarValues fields, VarValues methods)
|
||||
: Value(Kind::ClassType),
|
||||
name(std::move(name)),
|
||||
fields(std::move(fields)),
|
||||
methods(std::move(methods)) {}
|
||||
|
||||
static auto classof(const Value* value) -> bool {
|
||||
return value->Tag() == Kind::StructType;
|
||||
return value->Tag() == Kind::ClassType;
|
||||
}
|
||||
|
||||
auto Name() const -> const std::string& { return name; }
|
||||
|
||||
Reference in New Issue
Block a user