瀏覽代碼

Add `Core.Form` to prelude (#6745)

Unfortunately, currently it has to be a function rather than a constant.
Geoff Romer 2 月之前
父節點
當前提交
6a3529f4b5

+ 8 - 0
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";

+ 4 - 0
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) {

+ 1 - 0
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}

+ 57 - 0
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:

+ 1 - 0
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;
 

+ 4 - 0
toolchain/sem_ir/builtin_function_kind.cpp

@@ -454,6 +454,10 @@ constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
 constexpr BuiltinInfo MaybeUnformedMakeType = {
     "maybe_unformed.make_type", ValidateSignature<auto(Type)->Type>};
 
+// Returns the `Form` type.
+constexpr BuiltinInfo FormMakeType = {"form.make_type",
+                                      ValidateSignature<auto()->Type>};
+
 // Converts between char types, with a diagnostic if the value doesn't fit.
 constexpr BuiltinInfo CharConvertChecked = {
     "char.convert_checked",

+ 1 - 0
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)

+ 2 - 0
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;

+ 9 - 0
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";