Skip to content

Commit 49a3914

Browse files
docs: thin SPEC.adoc §8 to forward-reference codegen-environment.adoc (#110)
* docs: thin SPEC.adoc §8 to forward-reference codegen-environment.adoc PR #94 landed the full codegen environment reference as docs/specs/codegen-environment.adoc. §8 in SPEC.adoc is now redundant; replace its body with a one-paragraph forward-reference to avoid maintaining two copies. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * docs: thin SPEC.adoc §8 to forward-reference codegen-environment.adoc PR #94 landed the full codegen environment reference as docs/specs/codegen-environment.adoc. §8 in SPEC.adoc is now redundant; replace its body with a one-paragraph forward-reference to avoid maintaining two copies. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 154806a commit 49a3914

1 file changed

Lines changed: 3 additions & 141 deletions

File tree

docs/specs/SPEC.adoc

Lines changed: 3 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -652,147 +652,9 @@ Compiles to (ownership removed):
652652

653653
== 8. Top-Level Binding Environment
654654

655-
This section specifies how top-level declarations populate the binding
656-
environment that subsequent declarations and their bodies resolve against.
657-
It complements §3 (which gives the typing judgements) by fixing the
658-
*operational* rules every conforming code generator must obey, independent
659-
of any concrete target.
660-
661-
The WebAssembly realisation of these rules is documented at
662-
link:codegen-environment.adoc[`docs/specs/codegen-environment.adoc`].
663-
664-
=== 8.1 Top-Level Kinds
665-
666-
A program is an ordered sequence of top-level declarations. Each kind
667-
contributes to the binding environment as follows:
668-
669-
[cols="1,2,2", options="header"]
670-
|===
671-
| Declaration | Binds | Runtime artefact
672-
673-
| `fn f(…) { … }` (§2.4)
674-
| `f` as a function value
675-
| Yes — function in the target module
676-
677-
| `extern fn f(…) -> T;` (§2.10)
678-
| `f` as a function value
679-
| Yes — host-supplied import
680-
681-
| `const c: T = e;` (§2.9)
682-
| `c` as an immutable value of type `T`
683-
| Yes — same-type immutable cell
684-
685-
| `type T = …;` (§2.2)
686-
| `T` as a type
687-
| No — compile-time only
688-
689-
| `extern type T;` (§2.10)
690-
| `T` as an opaque type
691-
| No — compile-time only
692-
693-
| `effect E { … }` (§2.7)
694-
| `E` and its operations
695-
| No — handled by effect lowering (§7)
696-
697-
| `trait Tr { … }` (§2.8)
698-
| `Tr` as a trait
699-
| No — compile-time only
700-
701-
| `impl Tr for T { … }` (§2.8)
702-
| Trait dictionary for `(Tr, T)`
703-
| No — compile-time only
704-
|===
705-
706-
=== 8.2 Declaration Order and Visibility
707-
708-
Top-level declarations are processed in source order. The binding
709-
environment in scope when a declaration is processed contains exactly
710-
the names of declarations that *precede* it in source order, together
711-
with the names introduced by the module's `use` clauses (§8.4).
712-
713-
A reference to a name not yet bound at its use site is a static error:
714-
715-
[source,affinescript]
716-
----
717-
fn use_pi() -> Float { pi } // ERROR: `pi` not yet declared
718-
const pi: Float = 3.141592;
719-
----
720-
721-
Reordering resolves it:
722-
723-
[source,affinescript]
724-
----
725-
const pi: Float = 3.141592;
726-
fn use_pi() -> Float { pi } // OK: `pi` is in scope here
727-
----
728-
729-
The recommended source ordering — types, effects, constants, traits,
730-
impls, functions — is sufficient (though not necessary) to avoid every
731-
forward-reference error.
732-
733-
=== 8.3 Identifier Resolution
734-
735-
A bare identifier `x` in expression position resolves by the following
736-
lookup order, in the context where the expression appears:
737-
738-
. Local bindings introduced by enclosing `let`, lambda parameters, or
739-
function parameters.
740-
. Variant constructors of any in-scope `enum` type, resolved to their
741-
tag (§2.2).
742-
. Top-level bindings: `fn`, `extern fn`, and `const` names registered
743-
under §8.1.
744-
745-
A call expression `f(args)` resolves `f` against the same environment.
746-
A name bound to a `const` may appear in expression position only; a
747-
name bound to a `fn` or `extern fn` may appear in either expression or
748-
call position. The well-formedness of these positions is established by
749-
the type system (§3); a backend may rely on the typechecker having
750-
rejected ill-positioned references.
751-
752-
=== 8.4 Cross-Module Bindings
753-
754-
Imports (`use M::{…}`, `use M::*`) extend the binding environment of
755-
the importer with the public top-level names of the imported module,
756-
under the alias chosen by the import form (§2.1). The order in which
757-
import-introduced names enter the environment is *before* every
758-
local top-level declaration of the importer.
759-
760-
Items that flow across module boundaries:
761-
762-
* `fn` and `extern fn` — imported as references to the original definition.
763-
* `const` — its initialiser is compiled inline into the importer's
764-
module, so each importer materialises its own copy of the value.
765-
The denotation seen at use sites is the same as the source `const`
766-
in the defining module.
767-
768-
=== 8.5 Conformance Criteria
769-
770-
A conforming code generator MUST:
771-
772-
C1:: Process top-level declarations in source order.
773-
774-
C2:: Register a top-level name in its environment *before* generating
775-
the body of any later declaration that may reference it.
776-
777-
C3:: For each runtime-bearing declaration (`fn`, `extern fn`, `const`)
778-
emit an artefact whose denotation matches §2 and §3 — functions are
779-
first-class values, constants are immutable cells of their declared
780-
type.
781-
782-
C4:: For each compile-time declaration (`type`, `extern type`, `effect`,
783-
`trait`, `impl`) record sufficient information in the environment for
784-
subsequent declarations to typecheck and lower correctly, without
785-
necessarily emitting any target artefact.
786-
787-
C5:: Report a static error (rather than emit a malformed module) when
788-
an identifier escapes its lexical scope or names a binding not yet in
789-
the environment under C1–C2.
790-
791-
C6:: If a binding kind is unsupported by the target, raise
792-
`UnsupportedFeature` at the declaration site, never silently drop it.
793-
794-
Implementations MAY use any internal representation for the binding
795-
environment; C1–C6 fix what is observable, not how it is stored.
655+
See link:codegen-environment.adoc[`docs/specs/codegen-environment.adoc`] for the
656+
full codegen module environment reference, including the `func_indices`
657+
dual-use encoding, population order, and extern binding rules.
796658

797659
== Appendix: Grammar Reference
798660

0 commit comments

Comments
 (0)