diff --git a/core/prelude/types/form.carbon b/core/prelude/types/form.carbon new file mode 100644 index 000000000000..7cbb9a0946ff --- /dev/null +++ b/core/prelude/types/form.carbon @@ -0,0 +1,8 @@ +// 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 + +package Core library "prelude/types/form"; + +// TODO: this should be a concrete constant, not a function. +fn Form() -> type = "bool.make_type"; diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index eb610a9b061f..c875f0e91668 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -1933,6 +1933,10 @@ static auto MakeConstantForBuiltinCall(EvalContext& eval_context, phase); } + case SemIR::BuiltinFunctionKind::FormMakeType: { + return context.constant_values().Get(SemIR::FormType::TypeInstId); + } + // Character conversions. case SemIR::BuiltinFunctionKind::CharConvertChecked: { if (phase != Phase::Concrete) { diff --git a/toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon b/toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon index aaddc6048820..fc9db378b7d0 100644 --- a/toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon +++ b/toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon @@ -35,6 +35,7 @@ fn Foo[T:! type](p: T*) -> (T*, ()) { // CHECK:STDOUT: import_ir60000003: {decl_id: inst60000010, is_export: false} // CHECK:STDOUT: import_ir60000004: {decl_id: inst60000010, is_export: false} // CHECK:STDOUT: import_ir60000005: {decl_id: inst60000010, is_export: false} +// CHECK:STDOUT: import_ir60000006: {decl_id: inst60000010, is_export: false} // CHECK:STDOUT: import_ir_insts: // CHECK:STDOUT: import_ir_inst0: {ir_id: import_ir60000004, inst_id: inst48000010} // CHECK:STDOUT: import_ir_inst1: {ir_id: import_ir60000004, inst_id: inst48000010} diff --git a/toolchain/check/testdata/builtins/form/make_type.carbon b/toolchain/check/testdata/builtins/form/make_type.carbon new file mode 100644 index 000000000000..ac785eb4c1be --- /dev/null +++ b/toolchain/check/testdata/builtins/form/make_type.carbon @@ -0,0 +1,57 @@ +// 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 +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/form/make_type.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/form/make_type.carbon + +// --- types.carbon + +library "[[@TEST_NAME]]"; + +fn Form() -> type = "form.make_type"; + +// --- use_types.carbon + +library "[[@TEST_NAME]]"; + +import library "types"; + +// TODO: test more more realistic usages once they're supported. + +//@dump-sem-ir-begin +var f: Form(); +//@dump-sem-ir-end + +// CHECK:STDOUT: --- use_types.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %Form.type: type = fn_type @Form [concrete] +// CHECK:STDOUT: %Form: %Form.type = struct_value () [concrete] +// CHECK:STDOUT: %pattern_type.13f: type = pattern_type Core.Form [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Main.Form: %Form.type = import_ref Main//types, Form, loaded [concrete = constants.%Form] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: name_binding_decl { +// CHECK:STDOUT: %f.patt: %pattern_type.13f = ref_binding_pattern f [concrete] +// CHECK:STDOUT: %f.var_patt: %pattern_type.13f = var_pattern %f.patt [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: %f.var: ref Core.Form = var %f.var_patt [concrete] +// CHECK:STDOUT: %.loc9_13.1: type = splice_block %.loc9_13.3 [concrete = Core.Form] { +// CHECK:STDOUT: %Form.ref: %Form.type = name_ref Form, imports.%Main.Form [concrete = constants.%Form] +// CHECK:STDOUT: %Form.call: init type = call %Form.ref() [concrete = Core.Form] +// CHECK:STDOUT: %.loc9_13.2: type = value_of_initializer %Form.call [concrete = Core.Form] +// CHECK:STDOUT: %.loc9_13.3: type = converted %Form.call, %.loc9_13.2 [concrete = Core.Form] +// CHECK:STDOUT: } +// CHECK:STDOUT: %f: ref Core.Form = ref_binding f, %f.var [concrete = %f.var] +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/lower/handle_call.cpp b/toolchain/lower/handle_call.cpp index 0a2911729117..5a27f3bb9e92 100644 --- a/toolchain/lower/handle_call.cpp +++ b/toolchain/lower/handle_call.cpp @@ -413,6 +413,7 @@ static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id, case SemIR::BuiltinFunctionKind::IntMakeTypeSigned: case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned: case SemIR::BuiltinFunctionKind::MaybeUnformedMakeType: + case SemIR::BuiltinFunctionKind::FormMakeType: context.SetLocal(inst_id, context.GetTypeAsValue()); return; diff --git a/toolchain/sem_ir/builtin_function_kind.cpp b/toolchain/sem_ir/builtin_function_kind.cpp index 9235811ce22d..91702a443c0b 100644 --- a/toolchain/sem_ir/builtin_function_kind.cpp +++ b/toolchain/sem_ir/builtin_function_kind.cpp @@ -454,6 +454,10 @@ constexpr BuiltinInfo BoolMakeType = {"bool.make_type", constexpr BuiltinInfo MaybeUnformedMakeType = { "maybe_unformed.make_type", ValidateSignatureType>}; +// Returns the `Form` type. +constexpr BuiltinInfo FormMakeType = {"form.make_type", + ValidateSignatureType>}; + // Converts between char types, with a diagnostic if the value doesn't fit. constexpr BuiltinInfo CharConvertChecked = { "char.convert_checked", diff --git a/toolchain/sem_ir/builtin_function_kind.def b/toolchain/sem_ir/builtin_function_kind.def index 6b7d1be490ed..c0e86781f092 100644 --- a/toolchain/sem_ir/builtin_function_kind.def +++ b/toolchain/sem_ir/builtin_function_kind.def @@ -41,6 +41,7 @@ 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) +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FormMakeType) // Character conversion. CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(CharConvertChecked) diff --git a/toolchain/testing/testdata/min_prelude/parts/as.carbon b/toolchain/testing/testdata/min_prelude/parts/as.carbon index 824146f42eff..5a05ffd4b7ee 100644 --- a/toolchain/testing/testdata/min_prelude/parts/as.carbon +++ b/toolchain/testing/testdata/min_prelude/parts/as.carbon @@ -3,12 +3,14 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/destroy.carbon +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/form.carbon // --- min_prelude/parts/as.carbon package Core library "prelude/parts/as"; export import library "prelude/parts/destroy"; +export import library "prelude/parts/form"; interface UnsafeAs(Dest:! type) { fn Convert[self: Self]() -> Dest; diff --git a/toolchain/testing/testdata/min_prelude/parts/form.carbon b/toolchain/testing/testdata/min_prelude/parts/form.carbon new file mode 100644 index 000000000000..580670b112af --- /dev/null +++ b/toolchain/testing/testdata/min_prelude/parts/form.carbon @@ -0,0 +1,9 @@ +// 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 + +// --- min_prelude/parts/form.carbon + +package Core library "prelude/parts/form"; + +fn Form() -> type = "form.make_type";