Implement support for named constraints (#2359)

Basic support for named constraints as described in [the generics design](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#named-constraints). Support is provided for `extends` and `impl` declarations in `constraint`, but not yet for member aliases.

The missing prelude named constraints `Add`, `Mul`, `Ordered`, etc. are also added.
This commit is contained in:
Richard Smith
2022-11-04 16:23:02 -07:00
committed by GitHub
parent 8c354ca232
commit 9e8cacae00
31 changed files with 687 additions and 200 deletions
+10 -3
View File
@@ -15,8 +15,9 @@ Declaration::~Declaration() = default;
void Declaration::Print(llvm::raw_ostream& out) const {
switch (kind()) {
case DeclarationKind::InterfaceDeclaration: {
const auto& iface_decl = cast<InterfaceDeclaration>(*this);
case DeclarationKind::InterfaceDeclaration:
case DeclarationKind::ConstraintDeclaration: {
const auto& iface_decl = cast<ConstraintTypeDeclaration>(*this);
PrintID(out);
out << " {\n";
for (Nonnull<Declaration*> m : iface_decl.members()) {
@@ -120,6 +121,11 @@ void Declaration::PrintID(llvm::raw_ostream& out) const {
out << "interface " << iface_decl.name();
break;
}
case DeclarationKind::ConstraintDeclaration: {
const auto& constraint_decl = cast<ConstraintDeclaration>(*this);
out << "constraint " << constraint_decl.name();
break;
}
case DeclarationKind::ImplDeclaration: {
const auto& impl_decl = cast<ImplDeclaration>(*this);
switch (impl_decl.kind()) {
@@ -217,7 +223,8 @@ auto GetName(const Declaration& declaration)
case DeclarationKind::ChoiceDeclaration:
return cast<ChoiceDeclaration>(declaration).name();
case DeclarationKind::InterfaceDeclaration:
return cast<InterfaceDeclaration>(declaration).name();
case DeclarationKind::ConstraintDeclaration:
return cast<ConstraintTypeDeclaration>(declaration).name();
case DeclarationKind::VariableDeclaration:
return cast<VariableDeclaration>(declaration).binding().name();
case DeclarationKind::AssociatedConstantDeclaration: