Skip to content

Commit

Permalink
Implement rename me -> self (#2444)
Browse files Browse the repository at this point in the history
Implements change proposed in #1382

Replaces #1624
  • Loading branch information
josh11b authored Dec 7, 2022
1 parent d1210c4 commit 9c8fd68
Show file tree
Hide file tree
Showing 287 changed files with 1,727 additions and 1,720 deletions.
4 changes: 2 additions & 2 deletions common/fuzzing/carbon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ message ReturnTerm {
message FunctionDeclaration {
optional string name = 1;
repeated GenericBinding deduced_parameters = 2;
optional Pattern me_pattern = 3;
optional Pattern self_pattern = 3;
optional TuplePattern param_pattern = 4;
optional ReturnTerm return_term = 5;
optional BlockStatement body = 6;
}

message DestructorDeclaration {
optional Pattern me_pattern = 1;
optional Pattern self_pattern = 1;
optional BlockStatement body = 2;
}

Expand Down
11 changes: 6 additions & 5 deletions common/fuzzing/proto_to_carbon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,10 @@ static auto DeclarationToCarbon(const Fuzzing::Declaration& declaration,
out << "destructor";
llvm::ListSeparator sep;
out << "[";
if (function.has_me_pattern()) {
if (function.has_self_pattern()) {
// This is a class method.
out << sep;
PatternToCarbon(function.me_pattern(), out);
PatternToCarbon(function.self_pattern(), out);
}
out << "]";

Expand All @@ -707,17 +707,18 @@ static auto DeclarationToCarbon(const Fuzzing::Declaration& declaration,
out << "fn ";
IdentifierToCarbon(function.name(), out);

if (!function.deduced_parameters().empty() || function.has_me_pattern()) {
if (!function.deduced_parameters().empty() ||
function.has_self_pattern()) {
out << "[";
llvm::ListSeparator sep;
for (const Fuzzing::GenericBinding& p : function.deduced_parameters()) {
out << sep;
GenericBindingToCarbon(p, out);
}
if (function.has_me_pattern()) {
if (function.has_self_pattern()) {
// This is a class method.
out << sep;
PatternToCarbon(function.me_pattern(), out);
PatternToCarbon(function.self_pattern(), out);
}
out << "]";
}
Expand Down
56 changes: 28 additions & 28 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1668,22 +1668,22 @@ Class type definitions can include methods:
```carbon
class Point {
// Method defined inline
fn Distance[me: Self](x2: i32, y2: i32) -> f32 {
var dx: i32 = x2 - me.x;
var dy: i32 = y2 - me.y;
fn Distance[self: Self](x2: i32, y2: i32) -> f32 {
var dx: i32 = x2 - self.x;
var dy: i32 = y2 - self.y;
return Math.Sqrt(dx * dx + dy * dy);
}
// Mutating method declaration
fn Offset[addr me: Self*](dx: i32, dy: i32);
fn Offset[addr self: Self*](dx: i32, dy: i32);
var x: i32;
var y: i32;
}
// Out-of-line definition of method declared inline
fn Point.Offset[addr me: Self*](dx: i32, dy: i32) {
me->x += dx;
me->y += dy;
fn Point.Offset[addr self: Self*](dx: i32, dy: i32) {
self->x += dx;
self->y += dy;
}
var origin: Point = {.x = 0, .y = 0};
Expand All @@ -1695,16 +1695,16 @@ Assert(origin.Distance(3, 4) == 0.0);
This defines a `Point` class type with two integer data members `x` and `y` and
two methods `Distance` and `Offset`:

- Methods are defined as class functions with a `me` parameter inside square
- Methods are defined as class functions with a `self` parameter inside square
brackets `[`...`]` before the regular explicit parameter list in parens
`(`...`)`.
- Methods are called using the member syntax, `origin.Distance(`...`)` and
`origin.Offset(`...`)`.
- `Distance` computes and returns the distance to another point, without
modifying the `Point`. This is signified using `[me: Self]` in the method
modifying the `Point`. This is signified using `[self: Self]` in the method
declaration.
- `origin.Offset(`...`)` does modify the value of `origin`. This is signified
using `[addr me: Self*]` in the method declaration. Since calling this
using `[addr self: Self*]` in the method declaration. Since calling this
method requires taking the address of `origin`, it may only be called on
[non-`const`](#const) [l-values](#value-categories-and-value-phases).
- Methods may be declared lexically inline like `Distance`, or lexically out
Expand Down Expand Up @@ -1852,21 +1852,21 @@ names resolvable by the compiler, and don't act like forward declarations.

A destructor for a class is custom code executed when the lifetime of a value of
that type ends. They are defined with the `destructor` keyword followed by
either `[me: Self]` or `[addr me: Self*]` (as is done with [methods](#methods))
and the block of code in the class definition, as in:
either `[self: Self]` or `[addr self: Self*]` (as is done with
[methods](#methods)) and the block of code in the class definition, as in:

```carbon
class MyClass {
destructor [me: Self] { ... }
destructor [self: Self] { ... }
}
```

or:

```carbon
class MyClass {
// Can modify `me` in the body.
destructor [addr me: Self*] { ... }
// Can modify `self` in the body.
destructor [addr self: Self*] { ... }
}
```

Expand Down Expand Up @@ -1903,7 +1903,7 @@ For every type `MyClass`, there is the type `const MyClass` such that:
- If member `x` of `MyClass` has type `T`, then member `x` of `const MyClass`
has type `const T`.
- The API of a `const MyClass` is a subset of `MyClass`, excluding all methods
taking `[addr me: Self*]`.
taking `[addr self: Self*]`.

Note that `const` binds more tightly than postfix-`*` for forming a pointer
type, so `const MyClass*` is equal to `(const MyClass)*`.
Expand Down Expand Up @@ -2662,7 +2662,7 @@ capabilities that may be assumed of types that satisfy that constraint.
interface Printable {
// Inside an interface definition `Self` means
// "the type implementing this interface".
fn Print[me: Self]();
fn Print[self: Self]();
}
```

Expand All @@ -2685,8 +2685,8 @@ class Circle {
var radius: f32;
impl as Printable {
fn Print[me: Self]() {
Carbon.Print("Circle with radius: {0}", me.radius);
fn Print[self: Self]() {
Carbon.Print("Circle with radius: {0}", self.radius);
}
}
}
Expand Down Expand Up @@ -2779,9 +2779,9 @@ associated type to represent the type of elements stored in the stack.
```
interface StackInterface {
let ElementType:! Movable;
fn Push[addr me: Self*](value: ElementType);
fn Pop[addr me: Self*]() -> ElementType;
fn IsEmpty[addr me: Self*]() -> bool;
fn Push[addr self: Self*](value: ElementType);
fn Pop[addr self: Self*]() -> ElementType;
fn IsEmpty[addr self: Self*]() -> bool;
}
```

Expand All @@ -2791,14 +2791,14 @@ values for the `ElementType` member of the interface using a `where` clause:
```carbon
class IntStack {
impl as StackInterface where .ElementType = i32 {
fn Push[addr me: Self*](value: i32);
fn Push[addr self: Self*](value: i32);
// ...
}
}
class FruitStack {
impl as StackInterface where .ElementType = Fruit {
fn Push[addr me: Self*](value: Fruit);
fn Push[addr self: Self*](value: Fruit);
// ...
}
}
Expand Down Expand Up @@ -2826,8 +2826,8 @@ type `T`:

```carbon
class Stack(T:! Type) {
fn Push[addr me: Self*](value: T);
fn Pop[addr me: Self*]() -> T;
fn Push[addr self: Self*](value: T);
fn Pop[addr self: Self*]() -> T;
var storage: Array(T);
}
Expand Down Expand Up @@ -3032,7 +3032,7 @@ types in the `impl` declaration, as in:
```carbon
external impl like T as AddWith(like U) where .Result = V {
// `Self` is `T` here
fn Op[me: Self](other: U) -> V { ... }
fn Op[self: Self](other: U) -> V { ... }
}
```

Expand All @@ -3041,7 +3041,7 @@ implementing the `Add` interface:

```carbon
external impl T as Add {
fn Op[me: Self](other: Self) -> Self { ... }
fn Op[self: Self](other: Self) -> Self { ... }
}
```

Expand Down
Loading

0 comments on commit 9c8fd68

Please sign in to comment.