123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132moduleStringMap=Map.Make(String)typet={mutableexisting_names:intStringMap.t;reserved:intStringMap.t;(* The reserved words seeded at creation, kept separately so [add'] can
tell a reserved-word collision from a collision with another name. *)mutablelocations:Wax_utils.Ast.locationStringMap.t;(* The source location each name was claimed from (when one was given), so
[add'] can point a conflict at the previous occurrence. *)}letbuildl=List.fold_left(funms->StringMap.adds1m)StringMap.emptyl(* Only the Wax keywords are reserved. Intrinsics need no reservation: the
free-function ones are spelled as [v128::]/[i64::] paths and every other is a
receiver-based method, so no bare entity name can ever shadow one. *)(* The Wax bare-word keywords. The single source shared by [from_wasm]'s
reserved-name set (below), the editor's keyword completion
([Wax_format_js]), and the keyword-consistency test, which checks this list
against the lexer (the source of truth) and the other keyword lists. *)letreserved_words=["as";"become";"br";"br_if";"br_on_cast";"br_on_cast_fail";"br_on_non_null";"br_on_null";"br_table";"catch";"const";"cont";"data";"dispatch";"do";"elem";"else";"fn";"describes";"descriptor";"inf";"if";"import";"is";"let";"loop";"match";"memory";"mut";"nan";"nop";"null";"pagesize";"on";"open";"rec";"return";"shared";"suspend";"table";"tag";"throw";"throw_ref";"try";"try_legacy";"type";"unreachable";"while";]letreserved=buildreserved_words(* A [$type] name may also not collide with a Wax built-in type name — the
abstract heap types, the value types, the [atomic] intrinsic namespace —
which a [type] declaration may not take (the typer's
[Wax_lang.Typing.reserved_type_names], the single source). *)letreserved_heap_types=StringMap.union(fun__->assertfalse)reserved(buildWax_lang.Typing.reserved_type_names)letrecadd_indexednsxi=lety=Printf.sprintf"%s_%d"xiinifStringMap.memyns.existing_namesthenadd_indexednsx(i+1)else(ns.existing_names<-ns.existing_names|>StringMap.addy1|>StringMap.addxi;y)typeoutcome=|Available|Renamedof{reserved:bool;previous:Wax_utils.Ast.locationoption}letrecord_locationns?locname=matchlocwith|Someloc->ns.locations<-ns.locations|>StringMap.addnameloc|None->()letadd'?locnsx=matchStringMap.find_optxns.existing_nameswith|Somei->lety=add_indexednsx(i+1)inletprevious=StringMap.find_optxns.locationsinrecord_locationns?locy;(y,Renamed{reserved=StringMap.memxns.reserved;previous})|None->ns.existing_names<-ns.existing_names|>StringMap.addx1;record_locationns?locx;(x,Available)letaddnsx=fst(add'nsx)letis_reservednsx=StringMap.memxns.reservedletreservensx=ifnot(StringMap.memxns.existing_names)thenns.existing_names<-ns.existing_names|>StringMap.addx1letdup{existing_names;reserved;locations}={existing_names;reserved;locations}letmake?(kind=`Regular)()=letreserved=matchkindwith|`Regular->reserved|`Label->StringMap.empty|`Type->reserved_heap_typesin{existing_names=reserved;reserved;locations=StringMap.empty}