silo:std.reflection

Source src/silo:std.reflection

1##! Compile-time reflection types.
2##!
3##! `:type-info(T)` pushes a `TypeInfo` cell describing the structure
4##! of `T`. These types live during type checking and bytecode
5##! generation and are produced by the compiler for each reflected
6##! type; erasure and compile-time folding are a separate concern.
7
8# si[impl reflection.typeinfo]
9
10:use
11  :open core AnyInt Str Bool Option Vec
12:end
13
14# si[impl reflection.typeinfo.int]
15## Describes an `Int` type. `min` and `max` bound the range when known;
16## `None` on either side indicates an unbounded direction.
17:record(pub) IntTypeInfo
18  .min (Option AnyInt)
19  .max (Option AnyInt)
20:end
21
22# si[impl reflection.typeinfo.float]
23## Describes a `Float` type by its bit width (16, 32, or 64).
24:record(pub) FloatTypeInfo
25  .bits AnyInt
26:end
27
28# si[impl reflection.typeinfo.record]
29## Describes a record type: its declared name, type parameter names, and
30## the list of field descriptors.
31:record(pub) RecordTypeInfo
32  .name Str
33  .params (Vec Str)
34  .fields (Vec FieldInfo)
35:end
36
37## Describes a single field of a record: its declared name and the
38## `TypeInfo` of its type.
39:record(pub) FieldInfo
40  .name Str
41  .type TypeInfo
42:end
43
44# si[impl reflection.typeinfo.union]
45## Describes a union type: its declared name, type parameter names, and
46## the list of variant descriptors.
47:record(pub) UnionTypeInfo
48  .name Str
49  .params (Vec Str)
50  .variants (Vec VariantInfo)
51:end
52
53## Describes a single variant of a union: its declared name and the list
54## of payload type descriptors.
55:record(pub) VariantInfo
56  .name Str
57  .payload (Vec TypeInfo)
58:end
59
60# si[impl reflection.typeinfo.enum]
61## Describes an enum type: its declared name and the list of variant
62## descriptors (each carrying its integer tag value).
63:record(pub) EnumTypeInfo
64  .name Str
65  .variants (Vec EnumVariantTypeInfo)
66:end
67
68## Describes a single enum variant: its declared name and integer value.
69:record(pub) EnumVariantTypeInfo
70  .name Str
71  .value AnyInt
72:end
73
74# si[impl reflection.typeinfo.newtype]
75## Describes a newtype: its declared name, type parameter names, and the
76## `TypeInfo` of the underlying backing type.
77:record(pub) NewtypeTypeInfo
78  .name Str
79  .params (Vec Str)
80  .backing TypeInfo
81:end
82
83# si[impl reflection.typeinfo.alias]
84## Describes a type alias: its declared name, type parameter names, and
85## the `TypeInfo` of its target.
86:record(pub) AliasTypeInfo
87  .name Str
88  .params (Vec Str)
89  .target TypeInfo
90:end
91
92# si[impl reflection.typeinfo.quotation]
93## Describes a quotation type by its input row, output row, and declared
94## effect row (as string names).
95:record(pub) QuotationTypeInfo
96  .inputs (Vec TypeInfo)
97  .outputs (Vec TypeInfo)
98  .effects (Vec Str)
99:end
100
101# si[impl reflection.typeinfo.vec]
102# NOTE: the spec names this field `.len`, but that collides with Vec's `.len`
103# method in the current field-dispatch scheme. Using `.length` here until the
104# dispatcher can distinguish record fields from impl methods by scrutinee type
105# in the single-candidate case.
106## Describes a `Vec` type: the element type and, when statically known, its
107## length. `None` indicates a length-existential vector.
108:record(pub) VecTypeInfo
109  .elem TypeInfo
110  .length (Option AnyInt)
111:end
112
113# si[impl reflection.typeinfo.existential]
114## Describes an existential type by the list of trait names it witnesses.
115:record(pub) ExistentialTypeInfo
116  .traits (Vec Str)
117:end
118
119# si[impl reflection.typeinfo]
120## Top-level reflected type descriptor. Variants cover every structured
121## type form Silo exposes to reflection: Int, Float, Str, Bool, records,
122## unions, enums, newtypes, aliases, quotations, vectors, and existentials.
123@lang(type-info)
124:union(pub) TypeInfo
125  | IntInfo IntTypeInfo
126  | FloatInfo FloatTypeInfo
127  | StrInfo
128  | BoolInfo
129  | RecordInfo RecordTypeInfo
130  | UnionInfo UnionTypeInfo
131  | EnumInfo EnumTypeInfo
132  | NewtypeInfo NewtypeTypeInfo
133  | AliasInfo AliasTypeInfo
134  | QuotationInfo QuotationTypeInfo
135  | VecInfo VecTypeInfo
136  | ExistentialInfo ExistentialTypeInfo
137:end