| 1 | <!--===- docs/Aliasing.md |
| 2 | |
| 3 | Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | See https://llvm.org/LICENSE.txt for license information. |
| 5 | SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | |
| 7 | --> |
| 8 | |
| 9 | # Bijective Internal Name Uniquing |
| 10 | |
| 11 | ```{contents} |
| 12 | --- |
| 13 | local: |
| 14 | --- |
| 15 | ``` |
| 16 | |
| 17 | FIR has a flat namespace. No two objects may have the same name at the module |
| 18 | level. (These would be functions, globals, etc.) This necessitates some sort |
| 19 | of encoding scheme to unique symbols from the front-end into FIR. |
| 20 | |
| 21 | Another requirement is to be able to reverse these unique names and recover |
| 22 | the associated symbol in the symbol table. |
| 23 | |
| 24 | Fortran is case insensitive, which allows the compiler to convert the user's |
| 25 | identifiers to all lower case. Such a universal conversion implies that all |
| 26 | upper case letters are available for use in uniquing. |
| 27 | |
| 28 | ## Prefix `_Q` |
| 29 | |
| 30 | All uniqued names have the prefix sequence `_Q` to indicate the name has been |
| 31 | uniqued. (Q is chosen because it is a [low frequency letter](http://pi.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html) |
| 32 | in English.) |
| 33 | |
| 34 | ## Scope Building |
| 35 | |
| 36 | Symbols are scoped by any module, submodule, procedure, and block that |
| 37 | contains that symbol. After the `_Q` sigil, names are constructed from |
| 38 | outermost to innermost scope as |
| 39 | |
| 40 | * Module name prefixed with `M` |
| 41 | * Submodule name/s prefixed with `S` |
| 42 | * Procedure name/s prefixed with `F` |
| 43 | * Innermost block index prefixed with `B` |
| 44 | |
| 45 | Given: |
| 46 | ``` |
| 47 | submodule (mod:s1mod) s2mod |
| 48 | ... |
| 49 | subroutine sub |
| 50 | ... |
| 51 | contains |
| 52 | function fun |
| 53 | ``` |
| 54 | |
| 55 | The uniqued name of `fun` becomes: |
| 56 | ``` |
| 57 | _QMmodSs1modSs2modFsubPfun |
| 58 | ``` |
| 59 | |
| 60 | ## Prefix tag summary |
| 61 | |
| 62 | | Tag | Description |
| 63 | | ----| --------------------------------------------------------- | |
| 64 | | B | Block ("name" is a compiler generated integer index) |
| 65 | | C | Common block |
| 66 | | D | Dispatch table (compiler internal) |
| 67 | | E | variable Entity |
| 68 | | EC | Constant Entity |
| 69 | | F | procedure/Function (as a prefix) |
| 70 | | K | Kind |
| 71 | | KN | Negative Kind |
| 72 | | M | Module |
| 73 | | N | Namelist group |
| 74 | | P | Procedure/function (as itself) |
| 75 | | Q | uniQue mangled name tag |
| 76 | | S | Submodule |
| 77 | | T | derived Type |
| 78 | | Y | tYpe descriptor (compiler internal) |
| 79 | | YI | tYpe descriptor for an Intrinsic type (compiler internal) |
| 80 | |
| 81 | ## Common blocks |
| 82 | |
| 83 | * A common block name will be prefixed with `C` |
| 84 | |
| 85 | Given: |
| 86 | ``` |
| 87 | common /work/ i, j |
| 88 | ``` |
| 89 | |
| 90 | The uniqued name of `work` becomes: |
| 91 | ``` |
| 92 | _QCwork |
| 93 | ``` |
| 94 | |
| 95 | Given: |
| 96 | ``` |
| 97 | common i, j |
| 98 | ``` |
| 99 | |
| 100 | The uniqued name in case of `blank common block` becomes: |
| 101 | ``` |
| 102 | _QC |
| 103 | ``` |
| 104 | |
| 105 | ## Module scope global data |
| 106 | |
| 107 | * A global data entity is prefixed with `E` |
| 108 | * A global entity that is constant (parameter) will be prefixed with `EC` |
| 109 | |
| 110 | Given: |
| 111 | ``` |
| 112 | module mod |
| 113 | integer :: intvar |
| 114 | real, parameter :: pi = 3.14 |
| 115 | end module |
| 116 | ``` |
| 117 | |
| 118 | The uniqued name of `intvar` becomes: |
| 119 | ``` |
| 120 | _QMmodEintvar |
| 121 | ``` |
| 122 | |
| 123 | The uniqued name of `pi` becomes: |
| 124 | ``` |
| 125 | _QMmodECpi |
| 126 | ``` |
| 127 | |
| 128 | ## Procedures |
| 129 | |
| 130 | * A procedure/subprogram as itself is prefixed with `P` |
| 131 | * A procedure/subprogram as an ancestor name is prefixed with `F` |
| 132 | |
| 133 | Procedures are the only names that are themselves uniqued, as well as |
| 134 | appearing as a prefix component of other uniqued names. |
| 135 | |
| 136 | Given: |
| 137 | ``` |
| 138 | subroutine sub |
| 139 | real, save :: x(1000) |
| 140 | ... |
| 141 | ``` |
| 142 | The uniqued name of `sub` becomes: |
| 143 | ``` |
| 144 | _QPsub |
| 145 | ``` |
| 146 | The uniqued name of `x` becomes: |
| 147 | ``` |
| 148 | _QFsubEx |
| 149 | ``` |
| 150 | |
| 151 | ## Blocks |
| 152 | |
| 153 | * A block is prefixed with `B`; the block "name" is a compiler generated |
| 154 | index |
| 155 | |
| 156 | Each block has a per-procedure preorder index. The prefix for the immediately |
| 157 | containing block construct is unique within the procedure. |
| 158 | |
| 159 | Given: |
| 160 | ``` |
| 161 | subroutine sub |
| 162 | block |
| 163 | block |
| 164 | real, save :: x(1000) |
| 165 | ... |
| 166 | end block |
| 167 | ... |
| 168 | end block |
| 169 | ``` |
| 170 | The uniqued name of `x` becomes: |
| 171 | ``` |
| 172 | _QFsubB2Ex |
| 173 | ``` |
| 174 | |
| 175 | ## Namelist groups |
| 176 | |
| 177 | * A namelist group is prefixed with `N` |
| 178 | |
| 179 | Given: |
| 180 | ``` |
| 181 | subroutine sub |
| 182 | real, save :: x(1000) |
| 183 | namelist /temps/ x |
| 184 | ... |
| 185 | ``` |
| 186 | The uniqued name of `temps` becomes: |
| 187 | ``` |
| 188 | _QFsubNtemps |
| 189 | ``` |
| 190 | |
| 191 | ## Derived types |
| 192 | |
| 193 | * A derived type is prefixed with `T` |
| 194 | * If a derived type has KIND parameters, they are listed in a consistent |
| 195 | canonical order where each takes the form `Ki` and where _i_ is the |
| 196 | compile-time constant value. (All type parameters are integer.) If _i_ |
| 197 | is a negative value, the prefix `KN` will be used and _i_ will reflect |
| 198 | the magnitude of the value. |
| 199 | |
| 200 | Given: |
| 201 | ``` |
| 202 | module mymodule |
| 203 | type mytype |
| 204 | integer :: member |
| 205 | end type |
| 206 | ... |
| 207 | ``` |
| 208 | The uniqued name of `mytype` becomes: |
| 209 | ``` |
| 210 | _QMmymoduleTmytype |
| 211 | ``` |
| 212 | |
| 213 | Given: |
| 214 | ``` |
| 215 | type yourtype(k1,k2) |
| 216 | integer, kind :: k1, k2 |
| 217 | real :: mem1 |
| 218 | complex :: mem2 |
| 219 | end type |
| 220 | ``` |
| 221 | |
| 222 | The uniqued name of `yourtype` where `k1=4` and `k2=-6` (at compile-time): |
| 223 | ``` |
| 224 | _QTyourtypeK4KN6 |
| 225 | ``` |
| 226 | |
| 227 | * A derived type dispatch table is prefixed with `D`. The dispatch table |
| 228 | for `type t` would be `_QDTt` |
| 229 | * A type descriptor instance is prefixed with `C`. Intrinsic types can |
| 230 | be encoded with their names and kinds. The type descriptor for the |
| 231 | type `yourtype` above would be `_QCTyourtypeK4KN6`. The type |
| 232 | descriptor for `REAL(4)` would be `_QCrealK4`. |
| 233 | |
| 234 | ## Compiler internal names |
| 235 | |
| 236 | Compiler generated names do not have to be mapped back to Fortran. This |
| 237 | includes names prefixed with `_QQ`, tag `D` for a type bound procedure |
| 238 | dispatch table, and tags `Y` and `YI` for runtime type descriptors. |
| 239 | Combinations of internal names are separated with the `X` tag. |
| 240 | |
| 241 | Given: |
| 242 | ``` |
| 243 | _QQcl, 9a37c0 |
| 244 | ``` |
| 245 | |
| 246 | The uniqued name of `_QQcl` and `9a37c0`: |
| 247 | ``` |
| 248 | _QQclX9a37c0 |
| 249 | ``` |