|
| 1 | +# Associated constants |
| 2 | + |
| 3 | +<!-- |
| 4 | +Part of the Carbon Language project, under the Apache License v2.0 with LLVM |
| 5 | +Exceptions. See /LICENSE for license information. |
| 6 | +SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +--> |
| 8 | + |
| 9 | +<!-- toc --> |
| 10 | + |
| 11 | +## Table of contents |
| 12 | + |
| 13 | +- [Overview](#overview) |
| 14 | +- [Declaration checking](#declaration-checking) |
| 15 | +- [Specifying rewrite constraints](#specifying-rewrite-constraints) |
| 16 | +- [Definition of associated constant values](#definition-of-associated-constant-values) |
| 17 | +- [Use of associated constants](#use-of-associated-constants) |
| 18 | + - [Simple member access](#simple-member-access) |
| 19 | + - [Compound member access](#compound-member-access) |
| 20 | + - [Forming the constant value](#forming-the-constant-value) |
| 21 | + |
| 22 | +<!-- tocstop --> |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +_Note:_ This document only describes non-function associated constants. |
| 27 | + |
| 28 | +An associated constant is declared within an interface scope with the syntax: |
| 29 | + |
| 30 | +```carbon |
| 31 | +[MODIFIERS] let NAME:! TYPE [= INITIALIZER] ; |
| 32 | +``` |
| 33 | + |
| 34 | +Associated constants introduce a slot in the witness table for an interface that |
| 35 | +contains a value of type `TYPE`. |
| 36 | + |
| 37 | +Associated constants are always generic entities, because they're always |
| 38 | +parameterized at least by the `Self` type of the interface, as well as any other |
| 39 | +enclosing generic parameters. Note that the interface itself is _not_ |
| 40 | +parameterized by its `Self`. |
| 41 | + |
| 42 | +Associated constant entities are held in the `associated_constants` value store |
| 43 | +as objects of type `AssociatedConstant`. Each declaration of an associated |
| 44 | +constant is modeled by an `AssociatedConstantDecl` instruction. Each such |
| 45 | +instruction is then wrapped in an `AssociatedEntity` instruction which |
| 46 | +represents the slot within an interface witness where the constant's value can |
| 47 | +be found. |
| 48 | + |
| 49 | +## Declaration checking |
| 50 | + |
| 51 | +Because associated constants share the syntax of `let` declarations, a lot of |
| 52 | +the checking logic is also shared. This logic is in |
| 53 | +[handle_let_and_var.cpp](/toolchain/check/handle_let_and_var.cpp). Associated |
| 54 | +constant declaration handling proceeds as follows: |
| 55 | + |
| 56 | +1. ```carbon |
| 57 | + let NAME:! TYPE [= INITIALIZER] ; |
| 58 | + ^ |
| 59 | + ``` |
| 60 | +
|
| 61 | + `StartAssociatedConstant` is called at the start of an interface-scope `let` |
| 62 | + declaration. This: |
| 63 | +
|
| 64 | + - Starts a generic declaration region. |
| 65 | + - Pushes an instruction block to hold instructions within the declaration |
| 66 | + of the constant. These form the body of the generic. |
| 67 | +
|
| 68 | +2. ```carbon |
| 69 | + let NAME:! TYPE [= INITIALIZER] ; |
| 70 | + ~~~~^~~~~~~ |
| 71 | + ``` |
| 72 | +
|
| 73 | + Process the symbolic binding pattern. This is done in |
| 74 | + [handle_binding_pattern.cpp](/toolchain/check/handle_binding_pattern.cpp), |
| 75 | + which detects that we are at interface scope, and creates an |
| 76 | + `AssociatedConstantDecl` and corresponding `AssociatedConstant` entity. This |
| 77 | + binding is then produced as the instruction associated with the binding |
| 78 | + pattern. |
| 79 | +
|
| 80 | + _Note:_ This is somewhat unusual: usually, a pattern instruction would be |
| 81 | + associated with a pattern parse node. |
| 82 | +
|
| 83 | +3. ```carbon |
| 84 | + let NAME:! TYPE ; |
| 85 | + ^ |
| 86 | + let NAME:! TYPE = INITIALIZER ; |
| 87 | + ^ |
| 88 | + ``` |
| 89 | +
|
| 90 | + When we reach the end of the pattern in an interface-scope `let` binding, |
| 91 | + either because we reached the `=` or because we reached the `;` and there |
| 92 | + was no initializer, `EndAssociatedConstantDeclRegion` is called. This: |
| 93 | +
|
| 94 | + - Ends the generic declaration region. |
| 95 | + - Builds an `AssociatedEntity` object, reserving a slot in the interface's |
| 96 | + witness table for the constant. |
| 97 | + - Adds the associated constant to name lookup. |
| 98 | +
|
| 99 | + _Note:_ The pattern might not be valid for an associated constant. In this |
| 100 | + case, we won't have built an `AssociatedConstantDecl` in the previous step. |
| 101 | + When this happens, we instead just discard the generic declaration region |
| 102 | + and continue. The invalid pattern will be diagnosed later. |
| 103 | +
|
| 104 | +4. ```carbon |
| 105 | + let NAME:! TYPE = INITIALIZER ; |
| 106 | + ^ |
| 107 | + ``` |
| 108 | +
|
| 109 | + If there is an initializer, we start the generic definition region. |
| 110 | +
|
| 111 | +5. ```carbon |
| 112 | + let NAME:! TYPE [= INITIALIZER] ; |
| 113 | + ^ |
| 114 | + ``` |
| 115 | +
|
| 116 | + At the end of the declaration, `FinishAssociatedConstant` is called to |
| 117 | + finalize the declaration. This: |
| 118 | +
|
| 119 | + - Diagnoses if the pattern handling didn't create an |
| 120 | + `AssociatedConstantDecl`. |
| 121 | + - Finishes handling the initializer, if it's present: |
| 122 | + - Converts the initializer to the type of the constant. |
| 123 | + - Ends the generic definition region. |
| 124 | + - Pops the inst block created by `StartAssociatedConstant` and attaches it |
| 125 | + to the `AssociatedConstantDecl`. |
| 126 | + - Adds the `AssociatedConstantDecl` to the enclosing inst block. |
| 127 | +
|
| 128 | +## Specifying rewrite constraints |
| 129 | +
|
| 130 | +TODO: Fill this out. In particular, note that we do not convert the rewrite to |
| 131 | +the type of the associated constant as part of forming a `where` expression if |
| 132 | +the constant's type is symbolic, and instead defer that until the facet type is |
| 133 | +resolved. |
| 134 | +
|
| 135 | +## Definition of associated constant values |
| 136 | +
|
| 137 | +Associated constant values are stored into witness tables as part of impl |
| 138 | +processing in [impl.cpp](/toolchain/check/impl.cpp). |
| 139 | +
|
| 140 | +TODO: Fill this out once the new model is implemented. |
| 141 | +
|
| 142 | +## Use of associated constants |
| 143 | +
|
| 144 | +The work to handle uses of associated constants starts in |
| 145 | +[member_access.cpp](/toolchain/check/member_access.cpp). |
| 146 | +
|
| 147 | +When an `AssociatedEntity` is the member in a member access, impl lookup is |
| 148 | +performed to find the corresponding impl witness. The self type in impl lookup |
| 149 | +depends on how the member name was found. |
| 150 | +
|
| 151 | +### Simple member access |
| 152 | +
|
| 153 | +In `LookupMemberNameInScope`, if lookup for `y` in `x.y` finds an associated |
| 154 | +constant from interface `I`, then a witness is determined as follows: |
| 155 | +
|
| 156 | +- If the lookup scope is the type `T` of `x`, then: |
| 157 | + - If `T` is a non-type facet, the witness for that facet is used. TODO: |
| 158 | + That facet might not contain a witness for `I`. In that case we will |
| 159 | + need to perform impl lookup for `T as I` instead. |
| 160 | + - Otherwise, impl lookup for `T as I` is performed to find the witness. |
| 161 | +- If the lookup scope is `x` itself, then: |
| 162 | + - If `x` is a facet type or a namespace, impl lookup is not performed, and |
| 163 | + the result is simply `y`. This happens for cases such as |
| 164 | + `Interface.AssocConst`. |
| 165 | + - Otherwise, `x` must be a type other than a facet type, and impl lookup |
| 166 | + for `x as I` is performed to find the witness. |
| 167 | +
|
| 168 | +### Compound member access |
| 169 | +
|
| 170 | +In `PerformCompoundMemberAccess` for `x.(y)`, if `y` is an associated constant |
| 171 | +then impl lookup is performed for `T as I`, where `T` is the type of `x` and `I` |
| 172 | +is the interface in which `y` is declared to find the witness containing the |
| 173 | +constant value. |
| 174 | +
|
| 175 | +### Forming the constant value |
| 176 | +
|
| 177 | +Once the witness is determined, `AccessMemberOfImplWitness` is called to find |
| 178 | +the value of the associated constant in the witness. In the case where an impl |
| 179 | +lookup is needed, `PerformImplLookup` calls `AccessMemberOfImplWitness`, |
| 180 | +otherwise it's called directly. |
| 181 | +
|
| 182 | +`AccessMemberOfImplWitness` uses `GetTypeForSpecificAssociatedEntity` to form |
| 183 | +the type of the constant. This substitutes both the generic arguments (if any) |
| 184 | +for the interface and the `Self` type into the type of the associated constant. |
| 185 | +Then, an `ImplWitnessAccess` instruction is created to extract the relevant slot |
| 186 | +from the witness. Constant evaluation of this instruction reads the associated |
| 187 | +constant from the witness table. |
0 commit comments