Handle arrow operator (#3768)

This change implements the check behavior for the arrow operator.

`ptr->Foo()` is rewritten as `(*ptr).Foo()` and `ptr->(X.y)` is
rewritten as `(*ptr).(X.y)`
This commit is contained in:
CJ Johnson
2024-03-18 17:40:00 +00:00
committed by GitHub
parent 4421a75c36
commit d0e8afc51b
17 changed files with 365 additions and 89 deletions
+97
View File
@@ -0,0 +1,97 @@
// 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
//
// AUTOUPDATE
class C {
fn Member[self: Self]();
var field: C*;
}
fn Foo(ptr: C*) {
(*ptr).Member();
ptr->Member();
(*ptr).field;
ptr->field;
ptr->field->field;
}
// CHECK:STDOUT: --- arrow.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %C: type = class_type @C [template]
// CHECK:STDOUT: %.1: type = ptr_type C [template]
// CHECK:STDOUT: %.2: type = unbound_element_type C, C* [template]
// CHECK:STDOUT: %.3: type = struct_type {.field: C*} [template]
// CHECK:STDOUT: %.4: type = ptr_type {.field: C*} [template]
// CHECK:STDOUT: %.5: type = tuple_type () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .C = %C.decl
// CHECK:STDOUT: .Foo = %Foo
// CHECK:STDOUT: }
// CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
// CHECK:STDOUT: %Foo: <function> = fn_decl @Foo [template] {
// CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl [template = constants.%C]
// CHECK:STDOUT: %.loc12: type = ptr_type C [template = constants.%.1]
// CHECK:STDOUT: %ptr.loc12_8.1: C* = param ptr
// CHECK:STDOUT: @Foo.%ptr: C* = bind_name ptr, %ptr.loc12_8.1
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: %Member: <function> = fn_decl @Member [template] {
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
// CHECK:STDOUT: %self.loc8_13.1: C = param self
// CHECK:STDOUT: %self.loc8_13.2: C = bind_name self, %self.loc8_13.1
// CHECK:STDOUT: }
// CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
// CHECK:STDOUT: %.loc9_15: type = ptr_type C [template = constants.%.1]
// CHECK:STDOUT: %.loc9_12: <unbound element of class C> = field_decl field, element0 [template]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%C
// CHECK:STDOUT: .Member = %Member
// CHECK:STDOUT: .field = %.loc9_12
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Member[@C.%self.loc8_13.2: C]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Foo(%ptr: C*) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %ptr.ref.loc13: C* = name_ref ptr, %ptr
// CHECK:STDOUT: %.loc13_4.1: ref C = deref %ptr.ref.loc13
// CHECK:STDOUT: %Member.ref.loc13: <function> = name_ref Member, @C.%Member [template = @C.%Member]
// CHECK:STDOUT: %.loc13_9: <bound method> = bound_method %.loc13_4.1, %Member.ref.loc13
// CHECK:STDOUT: %.loc13_4.2: C = bind_value %.loc13_4.1
// CHECK:STDOUT: %.loc13_16: init () = call %.loc13_9(%.loc13_4.2)
// CHECK:STDOUT: %ptr.ref.loc14: C* = name_ref ptr, %ptr
// CHECK:STDOUT: %.loc14_6.1: ref C = deref %ptr.ref.loc14
// CHECK:STDOUT: %Member.ref.loc14: <function> = name_ref Member, @C.%Member [template = @C.%Member]
// CHECK:STDOUT: %.loc14_6.2: <bound method> = bound_method %.loc14_6.1, %Member.ref.loc14
// CHECK:STDOUT: %.loc14_6.3: C = bind_value %.loc14_6.1
// CHECK:STDOUT: %.loc14_14: init () = call %.loc14_6.2(%.loc14_6.3)
// CHECK:STDOUT: %ptr.ref.loc16: C* = name_ref ptr, %ptr
// CHECK:STDOUT: %.loc16_4: ref C = deref %ptr.ref.loc16
// CHECK:STDOUT: %field.ref.loc16: <unbound element of class C> = name_ref field, @C.%.loc9_12 [template = @C.%.loc9_12]
// CHECK:STDOUT: %.loc16_9: ref C* = class_element_access %.loc16_4, element0
// CHECK:STDOUT: %ptr.ref.loc17: C* = name_ref ptr, %ptr
// CHECK:STDOUT: %.loc17_6.1: ref C = deref %ptr.ref.loc17
// CHECK:STDOUT: %field.ref.loc17: <unbound element of class C> = name_ref field, @C.%.loc9_12 [template = @C.%.loc9_12]
// CHECK:STDOUT: %.loc17_6.2: ref C* = class_element_access %.loc17_6.1, element0
// CHECK:STDOUT: %ptr.ref.loc19: C* = name_ref ptr, %ptr
// CHECK:STDOUT: %.loc19_6.1: ref C = deref %ptr.ref.loc19
// CHECK:STDOUT: %field.ref.loc19_6: <unbound element of class C> = name_ref field, @C.%.loc9_12 [template = @C.%.loc9_12]
// CHECK:STDOUT: %.loc19_6.2: ref C* = class_element_access %.loc19_6.1, element0
// CHECK:STDOUT: %.loc19_6.3: C* = bind_value %.loc19_6.2
// CHECK:STDOUT: %.loc19_13.1: ref C = deref %.loc19_6.3
// CHECK:STDOUT: %field.ref.loc19_13: <unbound element of class C> = name_ref field, @C.%.loc9_12 [template = @C.%.loc9_12]
// CHECK:STDOUT: %.loc19_13.2: ref C* = class_element_access %.loc19_13.1, element0
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
+8 -1
View File
@@ -8,13 +8,20 @@
// CHECK:STDERR: let n: i32 = *undeclared;
// CHECK:STDERR: ^~~~~~~~~~
let n: i32 = *undeclared;
// CHECK:STDERR: fail_deref_error.carbon:[[@LINE+3]]:15: ERROR: Name `undeclared` not found.
// CHECK:STDERR: let n2: i32 = undeclared->foo;
// CHECK:STDERR: ^~~~~~~~~~
let n2: i32 = undeclared->foo;
// CHECK:STDOUT: --- fail_deref_error.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {}
// CHECK:STDOUT: %undeclared.ref: <error> = name_ref undeclared, <error> [template = <error>]
// CHECK:STDOUT: %undeclared.ref.loc10: <error> = name_ref undeclared, <error> [template = <error>]
// CHECK:STDOUT: %.loc10: ref <error> = deref <error>
// CHECK:STDOUT: %n: i32 = bind_name n, <error>
// CHECK:STDOUT: %undeclared.ref.loc14: <error> = name_ref undeclared, <error> [template = <error>]
// CHECK:STDOUT: %.loc14: ref <error> = deref <error>
// CHECK:STDOUT: %n2: i32 = bind_name n2, <error>
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -9,6 +9,10 @@ fn A() {
// CHECK:STDERR: *A;
// CHECK:STDERR: ^
*A;
// CHECK:STDERR: fail_deref_function.carbon:[[@LINE+3]]:3: ERROR: Expression cannot be used as a value.
// CHECK:STDERR: A->foo;
// CHECK:STDERR: ^
A->foo;
}
// CHECK:STDOUT: --- fail_deref_function.carbon
@@ -22,8 +26,10 @@ fn A() {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @A() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %A.ref: <function> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %A.ref.loc11: <function> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %.loc11: ref <error> = deref <error>
// CHECK:STDOUT: %A.ref.loc15: <function> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %.loc15: ref <error> = deref <error>
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -11,6 +11,10 @@ fn F() {
// CHECK:STDERR: *A;
// CHECK:STDERR: ^
*A;
// CHECK:STDERR: fail_deref_namespace.carbon:[[@LINE+3]]:3: ERROR: Expression cannot be used as a value.
// CHECK:STDERR: A->foo;
// CHECK:STDERR: ^
A->foo;
}
// CHECK:STDOUT: --- fail_deref_namespace.carbon
@@ -26,8 +30,10 @@ fn F() {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %A.ref: <namespace> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %A.ref.loc13: <namespace> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %.loc13: ref <error> = deref <error>
// CHECK:STDOUT: %A.ref.loc17: <namespace> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %.loc17: ref <error> = deref <error>
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -9,14 +9,26 @@ fn Deref(n: i32) {
// CHECK:STDERR: *n;
// CHECK:STDERR: ^
*n;
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+3]]:4: ERROR: Cannot apply `->` operator to non-pointer type `i32`.
// CHECK:STDERR: n->foo;
// CHECK:STDERR: ^~
n->foo;
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+3]]:3: ERROR: Cannot dereference operand of non-pointer type `()`.
// CHECK:STDERR: *();
// CHECK:STDERR: ^
*();
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+3]]:5: ERROR: Cannot apply `->` operator to non-pointer type `()`.
// CHECK:STDERR: ()->foo;
// CHECK:STDERR: ^~
()->foo;
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+3]]:3: ERROR: Cannot dereference operand of non-pointer type `{}`.
// CHECK:STDERR: *{};
// CHECK:STDERR: ^
*{};
// CHECK:STDERR: fail_deref_not_pointer.carbon:[[@LINE+3]]:5: ERROR: Cannot apply `->` operator to non-pointer type `{}`.
// CHECK:STDERR: {}->foo;
// CHECK:STDERR: ^~
{}->foo;
}
// CHECK:STDOUT: --- fail_deref_not_pointer.carbon
@@ -40,16 +52,26 @@ fn Deref(n: i32) {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Deref(%n: i32) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
// CHECK:STDOUT: %.loc11: ref <error> = deref %n.ref
// CHECK:STDOUT: %.loc15_5.1: () = tuple_literal ()
// CHECK:STDOUT: %.loc15_5.2: () = tuple_value () [template = constants.%.2]
// CHECK:STDOUT: %.loc15_5.3: () = converted %.loc15_5.1, %.loc15_5.2 [template = constants.%.2]
// CHECK:STDOUT: %.loc15_3: ref <error> = deref %.loc15_5.3
// CHECK:STDOUT: %.loc19_5.1: {} = struct_literal ()
// CHECK:STDOUT: %.loc19_5.2: {} = struct_value () [template = constants.%.4]
// CHECK:STDOUT: %.loc19_5.3: {} = converted %.loc19_5.1, %.loc19_5.2 [template = constants.%.4]
// CHECK:STDOUT: %n.ref.loc11: i32 = name_ref n, %n
// CHECK:STDOUT: %.loc11: ref <error> = deref %n.ref.loc11
// CHECK:STDOUT: %n.ref.loc15: i32 = name_ref n, %n
// CHECK:STDOUT: %.loc15: ref <error> = deref %n.ref.loc15
// CHECK:STDOUT: %.loc19_5.1: () = tuple_literal ()
// CHECK:STDOUT: %.loc19_5.2: () = tuple_value () [template = constants.%.2]
// CHECK:STDOUT: %.loc19_5.3: () = converted %.loc19_5.1, %.loc19_5.2 [template = constants.%.2]
// CHECK:STDOUT: %.loc19_3: ref <error> = deref %.loc19_5.3
// CHECK:STDOUT: %.loc23_4.1: () = tuple_literal ()
// CHECK:STDOUT: %.loc23_4.2: () = tuple_value () [template = constants.%.2]
// CHECK:STDOUT: %.loc23_4.3: () = converted %.loc23_4.1, %.loc23_4.2 [template = constants.%.2]
// CHECK:STDOUT: %.loc23_5: ref <error> = deref %.loc23_4.3
// CHECK:STDOUT: %.loc27_5.1: {} = struct_literal ()
// CHECK:STDOUT: %.loc27_5.2: {} = struct_value () [template = constants.%.4]
// CHECK:STDOUT: %.loc27_5.3: {} = converted %.loc27_5.1, %.loc27_5.2 [template = constants.%.4]
// CHECK:STDOUT: %.loc27_3: ref <error> = deref %.loc27_5.3
// CHECK:STDOUT: %.loc31_4.1: {} = struct_literal ()
// CHECK:STDOUT: %.loc31_4.2: {} = struct_value () [template = constants.%.4]
// CHECK:STDOUT: %.loc31_4.3: {} = converted %.loc31_4.1, %.loc31_4.2 [template = constants.%.4]
// CHECK:STDOUT: %.loc31_5: ref <error> = deref %.loc31_4.3
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -11,15 +11,23 @@
// CHECK:STDERR: var p: *i32;
// CHECK:STDERR: ^
var p: *i32;
// CHECK:STDERR: fail_deref_type.carbon:[[@LINE+3]]:12: ERROR: Cannot apply `->` operator to non-pointer type `type`.
// CHECK:STDERR: var p2: i32->foo;
// CHECK:STDERR: ^~
var p2: i32->foo;
// CHECK:STDOUT: --- fail_deref_type.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .p = %p
// CHECK:STDOUT: .p2 = %p2
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc13: ref <error> = deref i32
// CHECK:STDOUT: %p.var: ref <error> = var p
// CHECK:STDOUT: %p: ref <error> = bind_name p, %p.var
// CHECK:STDOUT: %.loc17: ref <error> = deref i32
// CHECK:STDOUT: %p2.var: ref <error> = var p2
// CHECK:STDOUT: %p2: ref <error> = bind_name p2, %p2.var
// CHECK:STDOUT: }
// CHECK:STDOUT: