Skip to content

Commit

Permalink
Member access expressions (#989)
Browse files Browse the repository at this point in the history
Support for member access expressions with syntax `container.member`, covering cases such as:

* `object.field`
* `object.method(args)`
* `package.namespace.class.member`
* `object.(interface.member)`
* `object.(class.member)`

... and so on. Includes the rule for template name lookup as decided in #949.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
  • Loading branch information
zygoloid and josh11b committed Mar 2, 2022
1 parent 7cce1bd commit 18b423d
Show file tree
Hide file tree
Showing 3 changed files with 1,084 additions and 1 deletion.
86 changes: 85 additions & 1 deletion docs/design/expressions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

- [Overview](#overview)
- [Precedence](#precedence)
- [Names](#names)
- [Unqualified names](#unqualified-names)
- [Qualified names and member access](#qualified-names-and-member-access)
- [Operators](#operators)
- [Conversions and casts](#conversions-and-casts)
- [`if` expressions](#if-expressions)
Expand Down Expand Up @@ -54,6 +57,13 @@ graph BT
braces["{...}"]
click braces "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#literals"
unqualifiedName["x"]
click unqualifiedName "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/README.md#unqualified-names"
memberAccess>"x.y<br>
x.(...)"]
click memberAccess "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/member_access.md"
as["x as T"]
click as "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/implicit_conversions.md"
Expand All @@ -79,7 +89,8 @@ graph BT
expressionEnd["x;"]
as & not --> parens & braces
memberAccess --> parens & braces & unqualifiedName
as & not --> memberAccess
comparison --> as
and & or --> comparison & not
if & expressionEnd --> and & or
Expand Down Expand Up @@ -113,6 +124,79 @@ The diagram's attributes are:
- For example, `+` and `-` are left-associative and in the same precedence
group, so `a + b + c - d` is treated as `((a + b) + c) - d`.

## Names

### Unqualified names

An _unqualified name_ is a [word](../lexical_conventions/words.md) that is not a
keyword and is not preceded by a period (`.`).

**TODO:** Name lookup rules for unqualified names.

### Qualified names and member access

A _qualified name_ is a word that is prefixed by a period. Qualified names
appear in the following contexts:

- [Designators](/docs/design/classes.md#literals): `.` _word_
- [Direct member access expressions](member_access.md): _expression_ `.`
_word_

```
var x: auto = {.hello = 1, .world = 2};
^^^^^ ^^^^^ qualified name
^^^^^^ ^^^^^^ designator
x.hello = x.world;
^^^^^ ^^^^^ qualified name
^^^^^^^ ^^^^^^^ member access expression
```

Qualified names refer to members of an entity determined by the context in which
the expression appears. For a member access, the entity is named by the
expression preceding the period. In a struct literal, the entity is the struct
type. For example:

```
package Foo api;
namespace N;
fn N.F() {}
fn G() {
// Same as `(Foo.N).F()`.
// `Foo.N` names namespace `N` in package `Foo`.
// `(Foo.N).F` names function `F` in namespace `N`.
Foo.N.F();
}
// `.n` refers to the member `n` of `{.n: i32}`.
fn H(a: {.n: i32}) -> i32 {
// `a.n` is resolved to the member `{.n: i32}.n`,
// and names the corresponding subobject of `a`.
return a.n;
}
fn J() {
// `.n` refers to the member `n of `{.n: i32}`.
H({.n = 5 as i32});
}
```

Member access expressions associate left-to-right. If the member name is more
complex than a single _word_, an indirect member access expression can be used,
with parentheses around the member name:

- _expression_ `.` `(` _member-access-expression_ `)`

```
interface I { fn F[me: Self](); }
class X {}
external impl X as I { fn F[me: Self]() {} }
// `x.I.F()` would mean `(x.I).F()`.
fn Q(x: X) { x.(I.F)(); }
```

## Operators

Most expressions are modeled as operators:
Expand Down
Loading

0 comments on commit 18b423d

Please sign in to comment.