Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/builtin_function_kind.cpp (C++)

Code before:
  }
};

// Constraint that allows an arbitrary type.
struct AnyType {
  static auto Check(const File& /*sem_ir*/, ValidateState& /*state*/,
                    TypeId /*type_id*/) -> bool {
    return true;
  }
};
// Constraint that checks if a type is Core.String.
struct CoreStringType {
  static auto Check(const File& sem_ir, ValidateState& /*state*/,
                    TypeId type_id) -> bool {
    auto type_inst_id = sem_ir.types().GetInstId(type_id);
    auto class_type = sem_ir.insts().TryGetAs<ClassType>(type_inst_id);
    if (!class_type) {
      // Not a string.
      return false;
    }

    const auto& class_info = sem_ir.classes().Get(class_type->class_id);

    return sem_ir.names().GetFormatted(class_info.name_id).str() == "String";
  }
};

// Constraint that requires the type to be the type type.
using Type = BuiltinType<TypeType::TypeInstId>;

// Constraint that a type supports a primitive copy. This happens if its
// initializing representation is a copy of its value representation.
struct PrimitiveCopyable {
  static auto Check(const File& sem_ir, ValidateState& /*state*/,
                    TypeId type_id) -> bool {
    return InitRepr::ForType(sem_ir, type_id).IsCopyOfObjectRepr() &&
           ValueRepr::ForType(sem_ir, type_id)
               .IsCopyOfObjectRepr(sem_ir, type_id);
  }
};

// Checks that the specified type matches the given type constraint.
template <typename TypeConstraint>
auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool {
  while (type_id.has_value()) {
    // Allow a type that satisfies the constraint.
    if (TypeConstraint::Check(sem_ir, state, type_id)) {
      return true;
    }

    // Also allow a class type that adapts a matching type.

Review comment (suggestion):
```suggestion

// Constraint that checks if a type is Core.String.
```

Code after:
  static auto CheckType(const File& sem_ir, ValidateState& /*state*/,
                        TypeId type_id) -> bool {
    return sem_ir.types().Is<FloatType>(type_id);
  }
};

// Constraint that requires the type to be a float type: either a sized float
// type or a literal.
struct AnyFloat {
  static auto CheckType(const File& sem_ir, ValidateState& state,
                        TypeId type_id) -> bool {
    return AnySizedFloat::CheckType(sem_ir, state, type_id) ||
           BuiltinType<FloatLiteralType::TypeInstId>::CheckType(sem_ir, state,
                                                                type_id);
  }
};

// Constraint that allows an arbitrary type.
struct AnyType {
  static auto CheckType(const File& /*sem_ir*/, ValidateState& /*state*/,
                        TypeId /*type_id*/) -> bool {
    return true;
  }
};

// Constraint that checks if a type is Core.String.
struct CoreStringType {
  static auto CheckType(const File& sem_ir, ValidateState& /*state*/,
                        TypeId type_id) -> bool {
    auto type_inst_id = sem_ir.types().GetInstId(type_id);
    auto class_type = sem_ir.insts().TryGetAs<ClassType>(type_inst_id);
    if (!class_type) {
      return false;
    }

    const auto& class_info = sem_ir.classes().Get(class_type->class_id);
    return sem_ir.names().GetFormatted(class_info.name_id).str() == "String";
  }
};

// Constraint that checks if a type is Core.Char.
struct CoreCharType {
  static auto CheckType(const File& sem_ir, ValidateState& /*state*/,
                        TypeId type_id) -> bool {
    auto type_inst_id = sem_ir.types().GetInstId(type_id);
    auto class_type = sem_ir.insts().TryGetAs<ClassType>(type_inst_id);
    if (!class_type) {
      return false;
    }

    const auto& class_info = sem_ir.classes().Get(class_type->class_id);