Skip to content

Commit 2be5aa7

Browse files
committed
visibility design and discussion
1 parent 946d64d commit 2be5aa7

5 files changed

Lines changed: 631 additions & 110 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# macOS
2+
.DS_Store
3+
4+
# vi temp files
5+
*.swp

Imports.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ translation_unit:
7070
| import_statement* global_directive* global_decl*
7171
7272
import_statement:
73-
| attribute* 'import' import_relative? (import_collection | import_path_or_item) ';'
73+
| attribute* 'public'? 'import' import_relative? (import_collection | import_path_or_item) ';'
7474
7575
import_relative:
7676
| 'package' '::' | 'super' '::' ('super' '::')*
@@ -85,8 +85,8 @@ import_collection:
8585
```
8686

8787
Where `translation_unit` and `ident` are defined in the WGSL grammar.
88-
`ident`s must not be current WGSL keywords. `ident`s also must not be
89-
current WESL keywords: `as`, `import`, `module`, `package`, `super`, or `self`.
88+
`ident`s must not be current WGSL keywords. `ident`s also must not be
89+
current WESL keywords: `as`, `import`, `module`, `package`, `private`, `public`, `self`, or `super`.
9090
Reserved words that are
9191
not current keywords are allowed,
9292
but not recommended.
@@ -98,6 +98,9 @@ An import collection imports multiple items, and allows for nested imports.
9898

9999
A wildcard import imports all top-level declarations from a module. Submodule names and submodule contents are not imported.
100100

101+
The optional `public` prefix also re-exports the imported names under the importing module's path; see
102+
[Visibility.md](Visibility.md).
103+
101104
WESL also extends WGSL's `global_directive` rule with a `module_declaration` form, used by `@wildcardable` (see [Wildcard imports](#wildcard-imports)) and reserved for future module-level metadata. `attribute` is the WGSL attribute rule.
102105

103106
```ebnf
@@ -125,22 +128,18 @@ import a::c::e as f;
125128
Then, one iterates over each segment from left to right, and looks it up one by one.
126129

127130
1. We start with the first segment.
128-
* `super` refers to the parent module. Can be repeated to go up multiple parent modules. Exiting the root is an error.
129131
* `package` refers to the top level module of the current package.
132+
* `super` refers to the parent module, so `super::sibling` reaches a module alongside the current one. Can be repeated to climb further; climbing out of the current package is an error.
130133
* `ident` must be a known package, usually found in the `wesl.toml` file. It refers to the top level module of that package.
131134
2. We take that as the "current module".
132135
3. We repeatedly look at the next segment.
133-
1. Item in current module: Take that item. We must be at the last segment, otherwise it's an error.
136+
1. Item in current module (declared or re-exported via `public import`): Take that item. We must be at the last segment, otherwise it's an error.
134137
2. Wildcard import: Take all items in the current module.
135-
3. (Else if re-exported or inline module in current module: We continue with that module.)
136-
4. Else go to `current module path/ident.wesl`
138+
3. Else go to `current module path/ident.wesl` (or `.wgsl` if `.wesl` is not found)
137139
* File found: We take that file as the current module.
138140
* File not found: We assume an empty module as the current module, and continue with that.
139-
* (Re-exporting changes the path.)
140-
* (Inline modules do not have a path.)
141141

142-
To get an absolute path to a module, one follows the algorithm above. In step 1, one takes the known absolute path of the `super` module, or the package.
143-
The absolute path of the `super` module is always known, since the first loaded WESL file must always be the root module, and children are only discovered from there.
142+
The steps above resolve a path; whether the referencing module may then use the result is governed by [visibility](Visibility.md).
144143

145144
Once the import has been resolved, the last segment, or its `as` alias, is brought into scope.
146145

@@ -174,10 +173,22 @@ The [`wesl.toml`](WeslToml.md) file provides linker configuration options affect
174173
* A file whitelist and/or blacklist.
175174

176175
## Filesystem Resolution
177-
To resolve a module on a filesystem, one follows the algorithm above.
178-
The root folder, or the root module, needs to be provided to the linker. This is currently a linker-specific API, and may change once we introduce a `wesl.toml`.
179176

180-
Linkers should fall back to `.wgsl` files when a `.wesl` file cannot be found.
177+
To resolve a module on a filesystem, one follows the [import resolution
178+
algorithm](#import-resolution-algorithm). It traces a path from a known starting
179+
directory (a package's root directory, or for `super` an enclosing directory of
180+
the current module) through any subdirectories to the module's `.wesl` or
181+
`.wgsl` file. The starting directory is always known: each module's path is
182+
fixed relative to its package's root directory, which the linker takes from a
183+
`wesl.toml` file or its own configuration.
184+
185+
### `package.wesl`
186+
187+
`package.wesl` is the file backing a package's top-level module, placed at the
188+
package's root directory. Items declared in or `public import`ed from
189+
`package.wesl` are reachable as `<package>::item`.
190+
191+
### Reserved file names
181192

182193
Due to filesystem limitations, it can happen that WESL idents are invalid file or folder names.
183194
Notable examples are `CON, PRN, AUX, NUL, COM1 - COM9, LPT1 - LPT9` on Windows, and Windows being case-insensitive.
@@ -323,7 +334,7 @@ version bumps but can break users who have local declarations or import other
323334
- **Document** additions clearly in changelogs so downstream users debugging
324335
unexpected name resolution can trace them.
325336

326-
**Compose with re-exports.** See [Re-exports](#re-exports) (TBD) to collect
337+
**Compose with re-exports.** See [Re-exports](Visibility.md#re-exports) to collect
327338
items from other modules into a single `@wildcardable` module for user
328339
convenience.
329340

@@ -487,11 +498,6 @@ Linkers may choose to do dead code elimination, but it is a non-observable imple
487498

488499
`const_assert` statements inside of functions need special treatment, see relevant section.
489500

490-
## Visibility
491-
Everything is public by default.
492-
493-
Future proposals will introduce visibility (privacy) for items and/or modules.
494-
495501
# Drawbacks
496502
Are there reasons as to why we should not do this?
497503

0 commit comments

Comments
 (0)