Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values, variables, pointers, and references #2006

Merged
merged 100 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
18e79ba
Filling out template with PR 2006
chandlerc Aug 12, 2022
b8fbace
Migrate content over from #821 which can't be updated.
chandlerc Aug 12, 2022
34e5e7a
add codespell fix
chandlerc Aug 12, 2022
7d1f3be
begin reworking abstract
chandlerc Aug 12, 2022
2ac27c4
precommit
chandlerc Aug 12, 2022
3b8bf85
change name
chandlerc Aug 12, 2022
6ee7bbe
improve terminology
chandlerc Aug 13, 2022
649e660
precommit
chandlerc Aug 13, 2022
dbe628e
wip
chandlerc Oct 8, 2022
fbc05e9
add missing file
chandlerc Oct 15, 2022
8a3902d
fixups
chandlerc Oct 15, 2022
62499f9
Merge branch 'trunk' into pointers2
chandlerc Mar 30, 2023
b2460bd
trim down to just the value semantic bits and leave pattern matching …
chandlerc Mar 31, 2023
406df97
expand much of the values docs
chandlerc Mar 31, 2023
ae043e9
toc
chandlerc Mar 31, 2023
4612d7d
more content and skeleton
chandlerc Mar 31, 2023
ac6ab56
toc
chandlerc Mar 31, 2023
baf91f9
add more content and improve formatting
chandlerc Apr 1, 2023
352f606
finish fleshing out new version of docs
chandlerc Apr 1, 2023
06ef0e8
format, toc, and other cleanups
chandlerc Apr 1, 2023
0d606da
Major update to terminology and TOC started.
chandlerc Apr 8, 2023
ff9eeb7
TOC andother tweaks
chandlerc Apr 8, 2023
20e97c6
more toc
chandlerc Apr 8, 2023
883ef56
push more content through
chandlerc Apr 8, 2023
e44b8e8
main overhaul complete
chandlerc Apr 14, 2023
6b24020
toc
chandlerc Apr 14, 2023
4a51cc8
improve initial wording a bit
chandlerc Apr 15, 2023
9507d79
fill in initializing
chandlerc Apr 15, 2023
1eb3d15
fix fenced languages
chandlerc Apr 15, 2023
57307f6
start filling in all the gaps
chandlerc Apr 15, 2023
fd3e65f
many many fixes
chandlerc Apr 15, 2023
d0540dc
toc and links
chandlerc Apr 15, 2023
443f4d6
loooooooonger
chandlerc Apr 15, 2023
c32d958
looooooooooooooooooonger
chandlerc Apr 15, 2023
b082769
tweak the graph more
chandlerc Apr 15, 2023
3229c33
nm
chandlerc Apr 15, 2023
d8d3d29
fix a typo and add a reference
chandlerc Apr 15, 2023
af6c7f6
Merge branch 'trunk' into pointers2
chandlerc Apr 19, 2023
fad8304
Update docs/design/values.md
chandlerc Apr 19, 2023
358b04a
address review comments
chandlerc Apr 19, 2023
8b72b14
Apply suggestions from code review
chandlerc Apr 20, 2023
8f6542f
replicate some context
chandlerc Apr 20, 2023
bd13418
remove an incorrect bit
chandlerc Apr 20, 2023
e58bd26
format
chandlerc Apr 20, 2023
d208544
start building up a useful proposal, including the template
chandlerc Apr 20, 2023
8b49cbd
toc
chandlerc Apr 20, 2023
ef140ca
fix link
chandlerc Apr 20, 2023
3d37ec1
format
chandlerc Apr 20, 2023
7a58838
complete the proposal update
chandlerc Apr 20, 2023
c530308
toc
chandlerc Apr 20, 2023
fc442eb
Apply suggestions from code review
chandlerc Jun 21, 2023
fd3ab3d
improvements from review
chandlerc Jun 22, 2023
e861ae0
start trying a simplified model
chandlerc Jun 23, 2023
1d968d1
get the order right
chandlerc Jun 23, 2023
8a77b18
Apply suggestions from code review
chandlerc Jun 24, 2023
7eb061c
Update docs/design/expressions/README.md
chandlerc Jun 24, 2023
23ea26c
more updates from review
chandlerc Jun 24, 2023
6c1aed0
lots of work on alternatives
chandlerc Jun 24, 2023
3f3df4a
TOC updates, link updates, and more links
chandlerc Jun 24, 2023
67e1206
remove unlinkable headings
chandlerc Jun 24, 2023
c3f87c1
fill in some todos
chandlerc Jun 24, 2023
06f9fb2
clean up a bunch of interactions
chandlerc Jun 26, 2023
60cbf93
lots more fixes
chandlerc Jun 26, 2023
46da744
still more fixes
chandlerc Jun 26, 2023
1991d37
Apply suggestions from code review
chandlerc Jul 10, 2023
bc5aac3
add file that i forgot in previous commits
chandlerc Jul 10, 2023
81e5dec
address a couple of review comments
chandlerc Jul 10, 2023
43e2e35
more updates from review
chandlerc Jul 11, 2023
68dbdc1
add more content
chandlerc Jul 11, 2023
85a8bd5
tidy
chandlerc Jul 11, 2023
ebea40b
fix a link
chandlerc Jul 11, 2023
7f6f9b6
Update proposals/p2006.md
chandlerc Jul 12, 2023
31508f4
fix formmatting
chandlerc Jul 11, 2023
0e32816
fix formmatting
chandlerc Jul 11, 2023
1852b79
undo a formatting hack that seems unnecesasry now
chandlerc Jul 12, 2023
9676357
lots of updates from review
chandlerc Jul 13, 2023
2cabca0
fixes
chandlerc Jul 13, 2023
580ec34
Update docs/design/values.md
chandlerc Jul 13, 2023
153809c
Update docs/design/values.md
chandlerc Jul 13, 2023
a27c60a
Update proposals/p2006.md
chandlerc Jul 13, 2023
18bb868
formatting
chandlerc Jul 13, 2023
1781950
adjust from review
chandlerc Jul 14, 2023
3464499
Merge branch 'trunk' into pointers2
chandlerc Jul 14, 2023
aaedc23
Update proposals/p2006.md
chandlerc Jul 15, 2023
441928d
Merge branch 'trunk' into pointers2
chandlerc Jul 15, 2023
408af96
remove a stray edit
chandlerc Jul 15, 2023
5fb5b4a
remove another stray edit
chandlerc Jul 15, 2023
0dba116
Apply suggestions from code review
chandlerc Aug 4, 2023
3cc6ab6
format
chandlerc Aug 4, 2023
63b4b05
replace "read" with "bind" in another place
chandlerc Aug 4, 2023
e23c819
restructure
chandlerc Aug 4, 2023
8f0340b
toc
chandlerc Aug 4, 2023
ee3c494
updates based on review
chandlerc Aug 4, 2023
2e0e0e0
format
chandlerc Aug 4, 2023
1c5b066
review feedback
chandlerc Aug 5, 2023
4a68fec
toc
chandlerc Aug 5, 2023
da6fe5d
Merge branch 'trunk' into pointers2
chandlerc Aug 5, 2023
d4fbee7
fix link
chandlerc Aug 5, 2023
2e1e8b3
Update docs/design/expressions/type_operators.md
chandlerc Aug 7, 2023
1615171
format
chandlerc Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ Some values, such as `()` and `{}`, may even be used as types, but only act like
types when they are in a type position, like after a `:` in a variable
declaration or the return type after a `->` in a function declaration. Any
expression in a type position must be
[a constants or symbolic value](#value-phases) so the compiler can resolve
[a constant or symbolic value](#value-phases) so the compiler can resolve
whether the value can be used as a type. This also puts limits on how much
operators can do different things for types. This is good for consistency, but
is a significant restriction on Carbon's design.
Expand Down Expand Up @@ -655,12 +655,13 @@ address of an object in memory in Carbon.

### Expression categories

Carbon expressions produce values or reference objects. Every expression has a
A Carbon expression produces a value, references an object, or initializes an
object. Every expression has a
[category](<https://en.wikipedia.org/wiki/Value_(computer_science)#lrvalue>),
similar to [C++](https://en.cppreference.com/w/cpp/language/value_category):

- [_Value expressions_](values.md#value-expressions) produce abstract,
read-only _values_ that cannot be modified or have its address taken.
read-only _values_ that cannot be modified or have their address taken.
- [_Reference expressions_](values.md#reference-expressions) refer to
_objects_ with _storage_ where a value may be read or written and the
object's address can be taken.
Expand All @@ -673,7 +674,7 @@ similar to [C++](https://en.cppreference.com/w/cpp/language/value_category):
Expressions in one category can be converted to any other category when needed.
The primitive conversion steps used are:

- A _read_ converts a reference expression into a value expression.
- _Value binding_ converts a reference expression into a value expression.
- _Direct initialization_ converts a value expression into an initializing
expression.
- _Copy initialization_ converts a reference expression into an initializing
Expand Down Expand Up @@ -798,11 +799,11 @@ not support
[pointer arithmetic](<https://en.wikipedia.org/wiki/Pointer_(computer_programming)>);
the only pointer [operations](#expressions) are:

- Dereference: given a pointer `p`, `*p` gives the value `p` points to as an
- Dereference: given a pointer `p`, `*p` gives the value `p` points to as a
[reference expression](#expression-categories). `p->m` is syntactic sugar
for `(*p).m`.
- Address-of: given an [reference expression](#expression-categories) `x`,
`&x` returns a pointer to `x`.
- Address-of: given a [reference expression](#expression-categories) `x`, `&x`
returns a pointer to `x`.

There are no [null pointers](https://en.wikipedia.org/wiki/Null_pointer) in
Carbon. To represent a pointer that may not refer to a valid object, use the
Expand Down Expand Up @@ -987,7 +988,7 @@ binding any name to it.
Binding patterns default to _`let` bindings_. The `var` keyword is used to make
it a _`var` binding_.

- The result of a `let` binding is the name is bound to a
- A `let` binding binds a name to a value, so the name can be used as a
[value expression](#expression-categories). This means the value cannot be
modified, and its address generally cannot be taken.
- A `var` binding creates an object with dedicated storage, and so the name
Expand Down
12 changes: 8 additions & 4 deletions docs/design/expressions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ graph BT
constType["const T"]
click pointer-type "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/type_operators.md"

pointerType["T*"]
pointerType>"T*"]
click pointer-type "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/type_operators.md"

pointer["*x<br>
%% FIXME: Need to switch unary operators from a left/right associativity to
%% a "repeated" marker, as we only have one direction for associativity and
%% that is wrong in this specific case.
pointer>"*x<br>
&x<br>"]
click pointer "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/pointer.md"

Expand Down Expand Up @@ -142,6 +145,7 @@ graph BT

constType --> top
pointerType --> constType
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
as --> pointerType

memberAccess --> top
pointer --> memberAccess
Expand All @@ -150,9 +154,9 @@ graph BT
unary --> negation & complement
%% Use a longer arrow here to put `not` next to `and` and `or`.
not -------> memberAccess
multiplication & modulo & as & bitwise_and & bitwise_or & bitwise_xor & shift --> unary
as & multiplication & modulo & bitwise_and & bitwise_or & bitwise_xor & shift --> unary
addition --> multiplication
comparison --> modulo & addition & as & bitwise_and & bitwise_or & bitwise_xor & shift
comparison --> as & addition & modulo & bitwise_and & bitwise_or & bitwise_xor & shift
logicalOperand --> comparison & not
and & or --> logicalOperand
logicalExpression --> and & or
Expand Down
4 changes: 2 additions & 2 deletions docs/design/expressions/indexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

Carbon supports indexing using the conventional `a[i]` subscript syntax. When
`a` is a
[_durable reference expression_](/docs/design/values.md#durable-reference-expressions),
[durable reference expression](/docs/design/values.md#durable-reference-expressions),
the result of subscripting is also a durable reference expression, but when `a`
is a [_value expression_](/docs/design/values.md#value-expressions), the result
is a [value expression](/docs/design/values.md#value-expressions), the result
can be a durable reference expression or a value expression, depending on which
interface the type implements:

Expand Down
20 changes: 14 additions & 6 deletions docs/design/expressions/type_operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ The syntax of these operators tries to mimic the most common appearance of

### Precedence

Because these are type operators, they don't have any precedence relationship
with non-type operators. `const` binds more tightly than `*` and can appear
unparenthesized in an operand, despite being both a unary type and having
whitespace separating it. This allows the syntax of a pointer to a `const i32`
to be `const i32*`, which is intended to be familiar to C++ developers. Forming
a `const` pointer type requires parentheses: `const (i32*)`.
Because these are type operators, they don't have many precedence relationship
with non-type operators.

- `const` binds more tightly than `*` and can appear unparenthesized in an
operand, despite being both a unary type and having whitespace separating
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
it.
- This allows the syntax of a pointer to a `const i32` to be `const i32*`,
which is intended to be familiar to C++ developers.
- Forming a `const` pointer type requires parentheses: `const (i32*)`.
- All type operators bind more tightly than `as` so they can be used in its
type operand.
- This also allows a desirable transitive precedence with `if`:
`if condition then T* else U*`.

## Alternatives considered
geoffromer marked this conversation as resolved.
Show resolved Hide resolved

- [Alternative pointer syntaxes](/proposals/p2006.md#alternative-pointer-syntaxes)
- [Alternative syntaxes for locals](/proposals/p2006.md#alternative-syntaxes-for-locals)
- [Make `const` a postfix rather than prefix operator](/proposals/p2006.md#make-const-a-postfix-rather-than-prefix-operator)

## References

Expand Down
2 changes: 1 addition & 1 deletion docs/design/generics/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ _Binding patterns_ associate a name with a type and a value. This is used to
declare function parameters, in `let` and `var` declarations, as well as to
declare [generic parameters](#generic-means-compile-time-parameterized). There
are three kinds of binding patterns, corresponding to
[the three value phases](/docs/design/README.md#value-categories-and-value-phases):
[the three value phases](/docs/design/README.md#value-phases):

- A _runtime binding pattern_ binds to a dynamic value at runtime, and is
written using a `:`, as in `x: i32`.
Expand Down
Loading
Loading