Add typechecking of type declarations (#984)

Also explicitly model the types of expressions whose values are class or choice types, and make those be the static_type of the type declaration. This ensures that the static_type of a NamedEntity consistently corresponds to the static type of an IdentifierExpression using that name.
This commit is contained in:
Geoff Romer
2021-12-13 16:19:52 -08:00
committed by GitHub
parent 90c58dae2b
commit fd45b089e9
5 changed files with 123 additions and 45 deletions
+40
View File
@@ -58,6 +58,8 @@ class Value {
ContinuationValue, // A first-class continuation value.
StringType,
StringValue,
TypeOfClassType,
TypeOfChoiceType,
};
Value(const Value&) = delete;
@@ -560,6 +562,44 @@ class StringValue : public Value {
std::string value_;
};
// The type of an expression whose value is a class type. Currently there is no
// way to explicitly name such a type in Carbon code, but we are tentatively
// using `typeof(ClassName)` as the debug-printing format, in anticipation of
// something like that becoming valid Carbon syntax.
class TypeOfClassType : public Value {
public:
explicit TypeOfClassType(Nonnull<const NominalClassType*> class_type)
: Value(Kind::TypeOfClassType), class_type_(class_type) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::TypeOfClassType;
}
auto class_type() const -> const NominalClassType& { return *class_type_; }
private:
Nonnull<const NominalClassType*> class_type_;
};
// The type of an expression whose value is a choice type. Currently there is no
// way to explicitly name such a type in Carbon code, but we are tentatively
// using `typeof(ChoiceName)` as the debug-printing format, in anticipation of
// something like that becoming valid Carbon syntax.
class TypeOfChoiceType : public Value {
public:
explicit TypeOfChoiceType(Nonnull<const ChoiceType*> choice_type)
: Value(Kind::TypeOfChoiceType), choice_type_(choice_type) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::TypeOfChoiceType;
}
auto choice_type() const -> const ChoiceType& { return *choice_type_; }
private:
Nonnull<const ChoiceType*> choice_type_;
};
auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool;
auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
SourceLocation source_loc) -> bool;