Files
carbon-lang/toolchain/sem_ir/builtin_function_kind.def
T
Jon Ross-PerkinsandDana Jansens 5e3bb523f8 Add builtin functions for destroy, with special requirements in facet types (#6035)
This is in support of a goal of changing the blanket `destroy` impl to
use (roughly):

```
private fn CanAggregateDestroy() -> type = "type.can_aggregate_destroy";

// Handles aggregate type destruction.
impl forall [AggregateDestroyT:! CanAggregateDestroy()] AggregateDestroyT as Destroy {
  fn Op[addr self: Self*]() = "type.aggregate_destroy";
}
```

That isn't done here because there's still other issues that migrating
raises. What this *does* do is add the builtin functions, and in
particular, support to `FacetTypeInfo` to make `CanAggregateDestroy`
work.

The "special requirement" approach in `FacetTypeInfo` allows us to
support restricting a blanket impl under the current approach of impls.
Maybe we'll find a cleaner approach that can work in the future, but
this fits into the current model by propagating similar to other
requirements. I'm using an enum mask because we have a number of similar
things to add (e.g. copy, move) but I'm not sure we need a full vector.

A few alternatives considered were:

- Supporting syntax more like `where .Self impls
TypeCanAggregateDestroy(.Self, SupportedInterface,
UnsupportedInterface)`. I think it'd be a little cleaner, but requires
better compile-time evaluation in order to assess the type of the call.
Right now it's expected to be a `FacetType` too early to make this work,
and I was concerned about pouring too much more time down this route.
- Providing an actual interface, in particular doing name lookup back
into `Core.` for an interface. This would've added name lookup overhead,
and the question of whether an `impl` exists.
- Generating an interface. This avoids the name lookup, but would still
raise the question of whether an `impl` should also be generated. Work
I've previously done generating interfaces for class destruction also
feels complex to both write and understand (an unfortunate issue).
- Still modeling as an `ImplsConstraint`, for example by defining a
special `InterfaceId::CanAggregateDestroy = -2` similar to what we do on
other ids. I was hesitant because of how this expands the number of
modes of `InterfaceId`, and things for consuming code to watch out for,
for what feels like a relatively niche set of use-cases that are only
interface-like.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
2025-09-15 17:03:43 +00:00

134 lines
5.0 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// This is an X-macro header. It does not use `#include` guards, and instead is
// designed to be `#include`ed after the x-macro is defined in order for its
// inclusion to expand to the desired output. Macro definitions are cleaned up
// at the end of this file.
//
// Supported x-macro is:
// - CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name)
// Defines a builtin function type.
#if !defined(CARBON_SEM_IR_BUILTIN_FUNCTION_KIND)
#error \
"Must define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND x-macro to use this file."
#define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name)
#endif
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(None)
// A no-op function definition; calls should be elided when lowering.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(NoOp)
// A primitive copy for types whose value and initializing representation are
// the same.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(PrimitiveCopy)
// Temporary builtins for primitive IO.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(PrintChar)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(PrintInt)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(ReadChar)
// Type factories.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(CharLiteralMakeType)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntLiteralMakeType)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatLiteralMakeType)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntMakeTypeSigned)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntMakeTypeUnsigned)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatMakeType)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(BoolMakeType)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(MaybeUnformedMakeType)
// Character conversion.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(CharConvertChecked)
// Integer conversion.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntConvert)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntConvertChecked)
// Integer arithmetic.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSNegate)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSAdd)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSSub)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSMul)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSDiv)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSMod)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUNegate)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUAdd)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUSub)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUMul)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUDiv)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUMod)
// Integer bitwise.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntComplement)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntAnd)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntOr)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntXor)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntLeftShift)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntRightShift)
// Integer comparison.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntEq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntNeq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntLess)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntLessEq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntGreater)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntGreaterEq)
// Integer compound assignment.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSAddAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSSubAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSMulAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSDivAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntSModAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUAddAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUSubAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUMulAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUDivAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntUModAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntAndAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntOrAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntXorAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntLeftShiftAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(IntRightShiftAssign)
// Float arithmetic.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatNegate)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatAdd)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatSub)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatMul)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatDiv)
// Float compound assignment.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatAddAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatSubAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatMulAssign)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatDivAssign)
// Float conversion.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatConvertChecked)
// Float comparison.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatEq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatNeq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatLess)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatLessEq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatGreater)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatGreaterEq)
// Bool comparison.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(BoolEq)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(BoolNeq)
// Facet type combination.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(TypeAnd)
// Primitive type destruction.
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(TypeAggregateDestroy)
CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(TypeCanAggregateDestroy)
#undef CARBON_SEM_IR_BUILTIN_FUNCTION_KIND