Promote FunctionType to a standard instruction. (#3931)

This removes the builtin FunctionType, replacing it with a FunctionType
instruction. The constant for a FunctionDecl is now a StructValue with
type of FunctionType.

Note this means a function declaration produces _both_ a type, and a
value of the type. This has some consequences in terms of circularity,
and makes the importing of function declarations a little more complex.

It'll get particularly peculiar for imports because of the behavior of
the reference, but that's a known issue due to other things such as
`alias`. The impact will hopefully be contained to
ResolvePrevInstForMerge (and ImportRefs).

To note a small formatting change in diagnostics:

```
-  // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable.
+  // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated F in Interface>` is not callable.

-  // CHECK:STDERR: fail_todo_facet_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable.
+  // CHECK:STDERR: fail_todo_facet_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated F in Interface>` is not callable.
```

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Ross-Perkins
2024-05-03 15:18:04 +00:00
committed by GitHub
co-authored by Richard Smith
parent e6061f6910
commit 76ed3c73cb
405 changed files with 9799 additions and 6579 deletions
@@ -13,15 +13,18 @@ fn F(N:! i32, a: [i32; N]*);
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %N: i32 = bind_symbolic_name N 0 [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct: F = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct] {
// CHECK:STDOUT: %N.loc10_6.1: i32 = param N
// CHECK:STDOUT: @F.%N: i32 = bind_symbolic_name N 0, %N.loc10_6.1 [symbolic = constants.%N]
// CHECK:STDOUT: %N.ref: i32 = name_ref N, @F.%N [symbolic = constants.%N]
@@ -17,14 +17,17 @@ fn F(T:! type, U:! type) {
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic]
// CHECK:STDOUT: %.1: type = ptr_type T [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct: F = struct_value () [template]
// CHECK:STDOUT: %.2: type = ptr_type T [symbolic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct] {
// CHECK:STDOUT: %T.loc7_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc7_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: %U.loc7_16.1: type = param U
@@ -35,7 +38,7 @@ fn F(T:! type, U:! type) {
// CHECK:STDOUT: fn @F(%T: type, %U: type) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %T.ref: type = name_ref T, %T [symbolic = constants.%T]
// CHECK:STDOUT: %.loc8: type = ptr_type T [symbolic = constants.%.1]
// CHECK:STDOUT: %.loc8: type = ptr_type T [symbolic = constants.%.2]
// CHECK:STDOUT: %p.var: ref T* = var p
// CHECK:STDOUT: %p: ref T* = bind_name p, %p.var
// CHECK:STDOUT: %U.ref: type = name_ref U, %U [symbolic = constants.%U]
@@ -13,14 +13,17 @@ fn F(T:! type) {
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %.1: type = ptr_type T [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct: F = struct_value () [template]
// CHECK:STDOUT: %.2: type = ptr_type T [symbolic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct] {
// CHECK:STDOUT: %T.loc7_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc7_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: }
@@ -29,7 +32,7 @@ fn F(T:! type) {
// CHECK:STDOUT: fn @F(%T: type) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %T.ref.loc8: type = name_ref T, %T [symbolic = constants.%T]
// CHECK:STDOUT: %.loc8: type = ptr_type T [symbolic = constants.%.1]
// CHECK:STDOUT: %.loc8: type = ptr_type T [symbolic = constants.%.2]
// CHECK:STDOUT: %p.var: ref T* = var p
// CHECK:STDOUT: %p: ref T* = bind_name p, %p.var
// CHECK:STDOUT: %T.ref.loc9: type = name_ref T, %T [symbolic = constants.%T]
@@ -13,13 +13,16 @@ fn F(T:! type, n: T) -> T {
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct: F = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct] {
// CHECK:STDOUT: %T.loc7_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc7_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: %T.ref.loc7_19: type = name_ref T, @F.%T [symbolic = constants.%T]
+40 -22
View File
@@ -102,22 +102,25 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %.1: type = ptr_type T [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.2: type = tuple_type () [template]
// CHECK:STDOUT: %struct: F = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .F = %F.loc4
// CHECK:STDOUT: .F = %F.decl.loc4
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %F.loc4: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl.loc4: F = fn_decl @F [template = constants.%struct] {
// CHECK:STDOUT: %T.loc4_6.1: type = param T
// CHECK:STDOUT: %T.loc4_6.2: type = bind_symbolic_name T 0, %T.loc4_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: %T.ref.loc4: type = name_ref T, %T.loc4_6.2 [symbolic = constants.%T]
// CHECK:STDOUT: %.loc4: type = ptr_type T [symbolic = constants.%.1]
// CHECK:STDOUT: %return.var.loc4: ref T* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %F.loc6: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl.loc6: F = fn_decl @F [template = constants.%struct] {
// CHECK:STDOUT: %T.loc6_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc6_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: %T.ref.loc6: type = name_ref T, @F.%T [symbolic = constants.%T]
@@ -128,7 +131,7 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%T: type) -> T* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %F.ref: <function> = name_ref F, file.%F.loc4 [template = file.%F.loc4]
// CHECK:STDOUT: %F.ref: F = name_ref F, file.%F.decl.loc4 [template = constants.%struct]
// CHECK:STDOUT: %T.ref: type = name_ref T, %T [symbolic = constants.%T]
// CHECK:STDOUT: %F.call: init T* = call %F.ref(%T.ref)
// CHECK:STDOUT: %.loc7_14.1: T* = value_of_initializer %F.call
@@ -142,16 +145,21 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic]
// CHECK:STDOUT: %.1: type = ptr_type T [symbolic]
// CHECK:STDOUT: %.2: type = ptr_type U [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.2: type = tuple_type () [template]
// CHECK:STDOUT: %struct.1: F = struct_value () [template]
// CHECK:STDOUT: %.3: type = ptr_type U [symbolic]
// CHECK:STDOUT: %.4: type = fn_type @.1 [template]
// CHECK:STDOUT: %struct.2: <invalid> = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct.1] {
// CHECK:STDOUT: %T.loc4_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc4_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: %U.loc4_16.1: type = param U
@@ -160,13 +168,13 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: %.loc4: type = ptr_type T [symbolic = constants.%.1]
// CHECK:STDOUT: @F.%return: ref T* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc13_32: <function> = fn_decl @.1 [template] {
// CHECK:STDOUT: %.decl: <invalid> = fn_decl @.1 [template = constants.%struct.2] {
// CHECK:STDOUT: %T.loc13_6.1: type = param T
// CHECK:STDOUT: @.1.%T: type = bind_symbolic_name T 0, %T.loc13_6.1 [symbolic = constants.%T]
// CHECK:STDOUT: %U.loc13_16.1: type = param U
// CHECK:STDOUT: @.1.%U: type = bind_symbolic_name U 1, %U.loc13_16.1 [symbolic = constants.%U]
// CHECK:STDOUT: %U.ref: type = name_ref U, @.1.%U [symbolic = constants.%U]
// CHECK:STDOUT: %.loc13_30: type = ptr_type U [symbolic = constants.%.2]
// CHECK:STDOUT: %.loc13: type = ptr_type U [symbolic = constants.%.3]
// CHECK:STDOUT: @.1.%return: ref U* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
@@ -175,7 +183,7 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @.1(%T: type, %U: type) -> U* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %F.ref: <function> = name_ref F, file.%F [template = file.%F]
// CHECK:STDOUT: %F.ref: F = name_ref F, file.%F.decl [template = constants.%struct.1]
// CHECK:STDOUT: %T.ref: type = name_ref T, %T [symbolic = constants.%T]
// CHECK:STDOUT: %F.call: init T* = call %F.ref(<invalid>) [template = <error>]
// CHECK:STDOUT: return <error>
@@ -187,18 +195,23 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: %T.1: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic]
// CHECK:STDOUT: %.1: type = ptr_type T [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.2: type = tuple_type () [template]
// CHECK:STDOUT: %struct.1: F = struct_value () [template]
// CHECK:STDOUT: %U.2: type = bind_symbolic_name U 0 [symbolic]
// CHECK:STDOUT: %T.2: type = bind_symbolic_name T 1 [symbolic]
// CHECK:STDOUT: %.2: type = ptr_type T [symbolic]
// CHECK:STDOUT: %.3: type = ptr_type T [symbolic]
// CHECK:STDOUT: %.4: type = fn_type @.1 [template]
// CHECK:STDOUT: %struct.2: <invalid> = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct.1] {
// CHECK:STDOUT: %T.loc4_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc4_6.1 [symbolic = constants.%T.1]
// CHECK:STDOUT: %U.loc4_16.1: type = param U
@@ -207,13 +220,13 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: %.loc4: type = ptr_type T [symbolic = constants.%.1]
// CHECK:STDOUT: @F.%return: ref T* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc13_32: <function> = fn_decl @.1 [template] {
// CHECK:STDOUT: %.decl: <invalid> = fn_decl @.1 [template = constants.%struct.2] {
// CHECK:STDOUT: %U.loc13_6.1: type = param U
// CHECK:STDOUT: @.1.%U: type = bind_symbolic_name U 0, %U.loc13_6.1 [symbolic = constants.%U.2]
// CHECK:STDOUT: %T.loc13_16.1: type = param T
// CHECK:STDOUT: @.1.%T: type = bind_symbolic_name T 1, %T.loc13_16.1 [symbolic = constants.%T.2]
// CHECK:STDOUT: %T.ref.loc13: type = name_ref T, @.1.%T [symbolic = constants.%T.2]
// CHECK:STDOUT: %.loc13_30: type = ptr_type T [symbolic = constants.%.2]
// CHECK:STDOUT: %.loc13: type = ptr_type T [symbolic = constants.%.3]
// CHECK:STDOUT: @.1.%return: ref T* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
@@ -222,7 +235,7 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @.1(%U: type, %T: type) -> T* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %F.ref: <function> = name_ref F, file.%F [template = file.%F]
// CHECK:STDOUT: %F.ref: F = name_ref F, file.%F.decl [template = constants.%struct.1]
// CHECK:STDOUT: %T.ref: type = name_ref T, %T [symbolic = constants.%T.2]
// CHECK:STDOUT: %F.call: init T* = call %F.ref(<invalid>) [template = <error>]
// CHECK:STDOUT: return <error>
@@ -234,18 +247,23 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: %T.1: type = bind_symbolic_name T 0 [symbolic]
// CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic]
// CHECK:STDOUT: %.1: type = ptr_type T [symbolic]
// CHECK:STDOUT: %F: type = fn_type @F [template]
// CHECK:STDOUT: %.2: type = tuple_type () [template]
// CHECK:STDOUT: %struct.1: F = struct_value () [template]
// CHECK:STDOUT: %U.2: type = bind_symbolic_name U 0 [symbolic]
// CHECK:STDOUT: %T.2: type = bind_symbolic_name T 1 [symbolic]
// CHECK:STDOUT: %.2: type = ptr_type U [symbolic]
// CHECK:STDOUT: %.3: type = ptr_type U [symbolic]
// CHECK:STDOUT: %.4: type = fn_type @.1 [template]
// CHECK:STDOUT: %struct.2: <invalid> = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct.1] {
// CHECK:STDOUT: %T.loc4_6.1: type = param T
// CHECK:STDOUT: @F.%T: type = bind_symbolic_name T 0, %T.loc4_6.1 [symbolic = constants.%T.1]
// CHECK:STDOUT: %U.loc4_16.1: type = param U
@@ -254,13 +272,13 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT: %.loc4: type = ptr_type T [symbolic = constants.%.1]
// CHECK:STDOUT: @F.%return: ref T* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc13_32: <function> = fn_decl @.1 [template] {
// CHECK:STDOUT: %.decl: <invalid> = fn_decl @.1 [template = constants.%struct.2] {
// CHECK:STDOUT: %U.loc13_6.1: type = param U
// CHECK:STDOUT: @.1.%U: type = bind_symbolic_name U 0, %U.loc13_6.1 [symbolic = constants.%U.2]
// CHECK:STDOUT: %T.loc13_16.1: type = param T
// CHECK:STDOUT: @.1.%T: type = bind_symbolic_name T 1, %T.loc13_16.1 [symbolic = constants.%T.2]
// CHECK:STDOUT: %U.ref: type = name_ref U, @.1.%U [symbolic = constants.%U.2]
// CHECK:STDOUT: %.loc13_30: type = ptr_type U [symbolic = constants.%.2]
// CHECK:STDOUT: %.loc13: type = ptr_type U [symbolic = constants.%.3]
// CHECK:STDOUT: @.1.%return: ref U* = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
@@ -269,7 +287,7 @@ fn F(U:! type, T:! type) -> U* {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @.1(%U: type, %T: type) -> U* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %F.ref: <function> = name_ref F, file.%F [template = file.%F]
// CHECK:STDOUT: %F.ref: F = name_ref F, file.%F.decl [template = constants.%struct.1]
// CHECK:STDOUT: %T.ref: type = name_ref T, %T [symbolic = constants.%T.2]
// CHECK:STDOUT: %F.call: init T* = call %F.ref(<invalid>) [template = <error>]
// CHECK:STDOUT: return <error>