Files
carbon-lang/toolchain/sem_ir/cpp_initializer_list.h
T
Richard Smith c0b24047dd Interop support for initialization via std::initializer_list. (#6672)
Add a new builtin function `cpp.std.initializer_list.make` that takes an
array and returns a `std::initializer_list`, initialized to refer to
that array. When C++ initialization wants to perform a
`std::initializer_list`-from-array construction, synthesize a
declaration of a matching builtin function and use that to perform the
initialization.

Ideally we would specify this conversion as an impl of `ImplicitAs` in
the prelude instead of hardcoding it in the interop layer, but
unfortunately that's not currently possible, for various reasons -- we
can't make the conversion form-generic, we can't deduce the array length
from the initializer, and we can't deduce against the arguments of
imported C++ class templates yet -- so for now synthesizing a builtin
function on demand is the best we can do.

Assisted-by: Gemini 3 Pro via Antigravity
2026-01-30 22:24:18 +00:00

39 lines
1.3 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
#ifndef CARBON_TOOLCHAIN_SEM_IR_CPP_INITIALIZER_LIST_H_
#define CARBON_TOOLCHAIN_SEM_IR_CPP_INITIALIZER_LIST_H_
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
class File;
// The layout of `std::initializer_list` that we are dealing with.
struct StdInitializerListLayout {
enum Kind : int8_t {
// Not a recognized layout.
None,
// `struct { T* begin; T* end; }`
PointerPointer,
// `struct { T* begin; size_t size; }`
PointerInt,
};
Kind kind = Kind::None;
// If the kind is PointerInt, the type of the size.
TypeId size_type_id = TypeId::None;
};
// Returns the kind of `std::initializer_list` that `type_id` represents, or
// `None` if it is not a `std::initializer_list`. This does not verify that
// `type_id` is actually a type named `std::initializer_list`, only that it has
// a recognized set of fields that allows us to treat it as one.
auto GetStdInitializerListLayout(const File& sem_ir, TypeId type_id)
-> StdInitializerListLayout;
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_CPP_INITIALIZER_LIST_H_