@geastack/compiler 1.0.30 → 1.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ir/dynamic-protocol.d.ts +20 -0
- package/dist/ir/dynamic-protocol.js +88 -0
- package/dist/targets/cpp/emit-narrowing.js +17 -0
- package/dist/targets/cpp/emit-native-field-view.js +23 -13
- package/dist/targets/cpp/records.d.ts +2 -0
- package/dist/targets/cpp/records.js +6 -3
- package/dist/targets/cpp/translation-unit.js +38 -1
- package/package.json +1 -1
- package/src/targets/cpp/runtime/gea_document_view.h +988 -0
- package/src/targets/cpp/runtime/gea_runtime.h +367 -218
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DeclarationId, StructuralTypeId } from '../identity/ids.js';
|
|
2
|
+
import type { ClassLayout } from '../projection/classes.js';
|
|
3
|
+
import { type Representation } from '../representation/model.js';
|
|
4
|
+
import type { ReflectionExposure } from './reflection-demand.js';
|
|
5
|
+
/**
|
|
6
|
+
* The program carriers a box holds with no dynamic protocol at all: no
|
|
7
|
+
* document table and no array table. The runtime builds both for every boxable
|
|
8
|
+
* type unless told otherwise (`gea::detail::dynamicProtocolOmitted`), so this
|
|
9
|
+
* is the one place that says which types nothing reads dynamically. The
|
|
10
|
+
* reflection census is the evidence: a carrier is omitted only when it has a
|
|
11
|
+
* row and the row is below `full`. No row, an incomplete census, or any
|
|
12
|
+
* sharing carrier at `full` keeps the protocol.
|
|
13
|
+
*/
|
|
14
|
+
export interface DynamicProtocolOmissions {
|
|
15
|
+
readonly classes: ReadonlySet<DeclarationId>;
|
|
16
|
+
readonly records: ReadonlySet<StructuralTypeId>;
|
|
17
|
+
/** Every array carrier of an omitted element identity. */
|
|
18
|
+
readonly arrays: readonly Representation[];
|
|
19
|
+
}
|
|
20
|
+
export declare const dynamicProtocolOmissionsOf: (classes: ReadonlyMap<DeclarationId, ClassLayout>, reflection: ReflectionExposure | undefined, representations: readonly Representation[]) => DynamicProtocolOmissions | null;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { abiKey, representationKey, walkRepresentation } from '../representation/model.js';
|
|
2
|
+
const programRecordShapeOf = (representation) => {
|
|
3
|
+
if (representation.kind === 'record' || representation.kind === 'record-with-index')
|
|
4
|
+
return representation.shapeId;
|
|
5
|
+
if (representation.kind === 'native-record-ref' && representation.native === null && representation.recursive === undefined)
|
|
6
|
+
return representation.shapeId;
|
|
7
|
+
return null;
|
|
8
|
+
};
|
|
9
|
+
// One physical runtime type per identity: the carriers it groups are every
|
|
10
|
+
// carrier that can name the same `ArrayObject<E>`, so one of them read
|
|
11
|
+
// dynamically keeps the protocol for all of them. A plain `function` element
|
|
12
|
+
// takes the callable identity without ever being eligible -- it shares the
|
|
13
|
+
// spelling of the callable families and the census does not row it -- and an
|
|
14
|
+
// element with no identity here (a string, a number, a union, `any`) is an
|
|
15
|
+
// array the runtime also builds for itself, out of the census's sight.
|
|
16
|
+
const arrayElementIdentityOf = (element) => {
|
|
17
|
+
switch (element.kind) {
|
|
18
|
+
case 'class-ref':
|
|
19
|
+
return { identity: JSON.stringify(['class', element.declaration]), eligible: true };
|
|
20
|
+
case 'record':
|
|
21
|
+
case 'record-with-index':
|
|
22
|
+
case 'native-record-ref': {
|
|
23
|
+
const shape = programRecordShapeOf(element);
|
|
24
|
+
return shape === null ? null : { identity: JSON.stringify(['record', shape]), eligible: true };
|
|
25
|
+
}
|
|
26
|
+
case 'function':
|
|
27
|
+
return { identity: JSON.stringify(['call', abiKey(element.abi)]), eligible: false };
|
|
28
|
+
case 'function-family':
|
|
29
|
+
case 'function-value-family':
|
|
30
|
+
return { identity: JSON.stringify(['call', abiKey(element.abi)]), eligible: true };
|
|
31
|
+
case 'function-value-dispatch':
|
|
32
|
+
return element.recursive === undefined ? { identity: JSON.stringify(['call', abiKey(element.abi)]), eligible: true } : null;
|
|
33
|
+
case 'function-and-constructor':
|
|
34
|
+
return { identity: JSON.stringify(['call-construct', abiKey(element.call), abiKey(element.construct)]), eligible: true };
|
|
35
|
+
default:
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
export const dynamicProtocolOmissionsOf = (classes, reflection, representations) => {
|
|
40
|
+
if (reflection === undefined || !reflection.complete)
|
|
41
|
+
return null;
|
|
42
|
+
const unread = (representation) => {
|
|
43
|
+
const demand = reflection.byRepresentation.get(representationKey(representation));
|
|
44
|
+
return demand !== undefined && demand.level !== 'full';
|
|
45
|
+
};
|
|
46
|
+
// A derived class reaches its dynamic protocol through its bases, so every
|
|
47
|
+
// base of a class kept below keeps its own.
|
|
48
|
+
const keptClasses = new Set();
|
|
49
|
+
for (const declaration of classes.keys()) {
|
|
50
|
+
const demand = reflection.classes.get(declaration);
|
|
51
|
+
if (demand !== undefined && demand.level !== 'full')
|
|
52
|
+
continue;
|
|
53
|
+
for (let base = declaration; base !== null && !keptClasses.has(base); base = classes.get(base)?.base ?? null)
|
|
54
|
+
keptClasses.add(base);
|
|
55
|
+
}
|
|
56
|
+
const recordRead = new Set();
|
|
57
|
+
const recordSeen = new Set();
|
|
58
|
+
const arrays = new Map();
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
for (const root of representations)
|
|
61
|
+
for (const representation of walkRepresentation(root, seen)) {
|
|
62
|
+
const shape = programRecordShapeOf(representation);
|
|
63
|
+
if (shape !== null) {
|
|
64
|
+
recordSeen.add(shape);
|
|
65
|
+
if (!unread(representation))
|
|
66
|
+
recordRead.add(shape);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (representation.kind !== 'array-object')
|
|
70
|
+
continue;
|
|
71
|
+
const element = representation.recursive === undefined ? arrayElementIdentityOf(representation.element) : null;
|
|
72
|
+
if (element === null)
|
|
73
|
+
continue;
|
|
74
|
+
const group = arrays.get(element.identity) ?? { members: [], omitted: true };
|
|
75
|
+
group.members.push(representation);
|
|
76
|
+
if (!element.eligible || !unread(representation))
|
|
77
|
+
group.omitted = false;
|
|
78
|
+
arrays.set(element.identity, group);
|
|
79
|
+
}
|
|
80
|
+
for (const [shape, demand] of reflection.records)
|
|
81
|
+
if (demand.level === 'full')
|
|
82
|
+
recordRead.add(shape);
|
|
83
|
+
return {
|
|
84
|
+
classes: new Set([...classes.keys()].filter((declaration) => !keptClasses.has(declaration))),
|
|
85
|
+
records: new Set([...recordSeen].filter((shape) => !recordRead.has(shape))),
|
|
86
|
+
arrays: [...arrays.values()].flatMap((group) => (group.omitted ? group.members : []))
|
|
87
|
+
};
|
|
88
|
+
};
|
|
@@ -4931,7 +4931,24 @@ export const recipeText = (ctx, node, text) => {
|
|
|
4931
4931
|
throw createCppEmitBlockedError(`conversion:${node.id}`, 'the conversion dispatcher requires the exact certified recipe');
|
|
4932
4932
|
return evaluatedOnceText(text, (operand) => renderedRecipeText(ctx, node, operand));
|
|
4933
4933
|
};
|
|
4934
|
+
/**
|
|
4935
|
+
* A union read out of a box depends on its plan alone, so a unit defines it
|
|
4936
|
+
* once and every site calls it by name (`unitFunctionName`). Spelled inline it
|
|
4937
|
+
* was re-emitted at every read: one `Headers`-or-record union in hono tests
|
|
4938
|
+
* some seventy required keys, ~46 KB of machine code per copy.
|
|
4939
|
+
*/
|
|
4934
4940
|
const dynamicWrapperText = (ctx, plan, node, text) => {
|
|
4941
|
+
if (plan.kind !== 'union')
|
|
4942
|
+
return dynamicWrapperTextAt(ctx, plan, node, text);
|
|
4943
|
+
const formal = 'gea_dynamic_source';
|
|
4944
|
+
const body = dynamicWrapperTextAt(ctx, plan, node, formal);
|
|
4945
|
+
if (body === null)
|
|
4946
|
+
return null;
|
|
4947
|
+
const signature = (name) => `${cppTypeOf(plan.target)} ${name}(const gea::Value& ${formal})`;
|
|
4948
|
+
const named = unitFunctionName('gea_dynamic_union', signature, `return ${body};`);
|
|
4949
|
+
return named === null ? dynamicWrapperTextAt(ctx, plan, node, text) : `${named}(${text})`;
|
|
4950
|
+
};
|
|
4951
|
+
const dynamicWrapperTextAt = (ctx, plan, node, text) => {
|
|
4935
4952
|
if (!dynamicWrapperPlanMatches(plan, node.source, node.target, ctx.conversions.nodeById))
|
|
4936
4953
|
return null;
|
|
4937
4954
|
const targetType = cppTypeOf(plan.target);
|
|
@@ -3,7 +3,7 @@ import { createCppEmitBlockedError, operandText } from './emit-context.js';
|
|
|
3
3
|
import { recipeText } from './emit-narrowing.js';
|
|
4
4
|
import { nativeFieldPolicyType, staticKeyOrderText } from './records.js';
|
|
5
5
|
import { tracksKeyOrder } from './key-order-tracking.js';
|
|
6
|
-
import { cppTypeOf, cppStringLiteral, cppRecordFieldKeyIsSymbol, fieldPropertyKeyText } from './types.js';
|
|
6
|
+
import { cppTypeOf, cppStringLiteral, cppRecordFieldKeyIsSymbol, fieldPropertyKeyText, unitFunctionName } from './types.js';
|
|
7
7
|
import { propertyKeyText } from './emit-dynamic-properties.js';
|
|
8
8
|
import { nativeDataPropertyText } from './emit-buffers.js';
|
|
9
9
|
import { nativeDataPropertyOf } from '../../representation/native-data-properties.js';
|
|
@@ -63,15 +63,7 @@ export const nativeFieldViewOperandContext = (ctx, operation) => {
|
|
|
63
63
|
};
|
|
64
64
|
export const nativeFieldViewReadForText = (ctx, receiver, target, receipt, ordinaryReadOf, keyOperand) => {
|
|
65
65
|
const result = cppTypeOf(target);
|
|
66
|
-
const
|
|
67
|
-
const from = cppTypeOf(source);
|
|
68
|
-
const converted = renderedLeaf(ctx, conversion, `(*static_cast<const ${from}*>(gea_native))`);
|
|
69
|
-
return `if (${routeTokenOf(source)}) { static_cast<std::optional<${result}>*>(gea_out)->emplace(${converted}); return true; }`;
|
|
70
|
-
});
|
|
71
|
-
const accepts = receipt.sources.map(({ source }) => `(${routeTokenOf(source)})`).join(' || ') || 'false';
|
|
72
|
-
const adapter = '[&](void* gea_out, const void* gea_type, const void* gea_policy, const void* gea_native) -> bool { ' +
|
|
73
|
-
`${arms.join(' ')} return false; }`;
|
|
74
|
-
const accepted = `+[](const void* gea_type, const void* gea_policy) -> bool { return ${accepts}; }`;
|
|
66
|
+
const { adapter, accepted } = fieldViewRoutesText(ctx, receipt, result);
|
|
75
67
|
const dynamicRead = (key) => (carrier, text) => {
|
|
76
68
|
const route = receipt.sources.find((entry) => representationKey(entry.source) === representationKey(carrier));
|
|
77
69
|
return route === undefined
|
|
@@ -224,12 +216,30 @@ export const nativeFieldViewWriteText = (ctx, operation) => {
|
|
|
224
216
|
*/
|
|
225
217
|
export const nativeHeldFieldViewReadText = (ctx, receipt, target, receiver, plain) => {
|
|
226
218
|
const result = cppTypeOf(target);
|
|
219
|
+
const { adapter, accepted } = fieldViewRoutesText(ctx, receipt, result);
|
|
220
|
+
return `gea::record::readLiveField<${result}>(${receiver}, ${viewKeyText(receipt.key)}, ${adapter}, ${accepted}, [&]() -> ${result} { return ${plain}; })`;
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* The adapter a live view's certified routes answer through, and the policy
|
|
224
|
+
* check naming them. Both depend on the receipt's routes and result carrier
|
|
225
|
+
* alone, so a unit defines each once (`unitFunctionName`): spelled inline they
|
|
226
|
+
* were re-emitted for every read, every receiver arm and both the live and the
|
|
227
|
+
* plain path of each.
|
|
228
|
+
*/
|
|
229
|
+
const fieldViewRoutesText = (ctx, receipt, result) => {
|
|
227
230
|
const arms = receipt.sources.map(({ source, conversion }) => {
|
|
228
231
|
const converted = renderedLeaf(ctx, conversion, `(*static_cast<const ${cppTypeOf(source)}*>(gea_native))`);
|
|
229
232
|
return `if (${routeTokenOf(source)}) { static_cast<std::optional<${result}>*>(gea_out)->emplace(${converted}); return true; }`;
|
|
230
233
|
});
|
|
231
234
|
const accepts = receipt.sources.map(({ source }) => `(${routeTokenOf(source)})`).join(' || ') || 'false';
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
+
const adapterParameters = '(void* gea_out, const void* gea_type, const void* gea_policy, const void* gea_native)';
|
|
236
|
+
const adapterBody = `${arms.join(' ')} return false;`;
|
|
237
|
+
const acceptedParameters = '(const void* gea_type, const void* gea_policy)';
|
|
238
|
+
const acceptedBody = `return ${accepts};`;
|
|
239
|
+
return {
|
|
240
|
+
adapter: unitFunctionName('gea_field_view_adapter', (name) => `bool ${name}${adapterParameters}`, adapterBody) ??
|
|
241
|
+
`[&]${adapterParameters} -> bool { ${adapterBody} }`,
|
|
242
|
+
accepted: unitFunctionName('gea_field_view_accepts', (name) => `bool ${name}${acceptedParameters}`, acceptedBody) ??
|
|
243
|
+
`+[]${acceptedParameters} -> bool { ${acceptedBody} }`
|
|
244
|
+
};
|
|
235
245
|
};
|
|
@@ -485,6 +485,8 @@ conversionSite?: ConversionSite) => {
|
|
|
485
485
|
* static in rather than a member the struct does not have.
|
|
486
486
|
*/
|
|
487
487
|
readonly staticMethodStateClasses: ReadonlySet<DeclarationId>;
|
|
488
|
+
/** Every program struct this pass declares. */
|
|
489
|
+
readonly structNames: ReadonlySet<string>;
|
|
488
490
|
/** Authenticated program-class ancestry; the translation unit owns namespace placement. */
|
|
489
491
|
/** `nativeBase` marks a runtime-owned base spelled fully qualified, outside the program's namespace. */
|
|
490
492
|
readonly runtimeClassBases: readonly {
|
|
@@ -3518,7 +3518,8 @@ conversionSite) => {
|
|
|
3518
3518
|
})),
|
|
3519
3519
|
revisionFields: new Map(),
|
|
3520
3520
|
celledFields: new Map(),
|
|
3521
|
-
staticMethodStateClasses: new Set()
|
|
3521
|
+
staticMethodStateClasses: new Set(),
|
|
3522
|
+
structNames: new Set()
|
|
3522
3523
|
};
|
|
3523
3524
|
}
|
|
3524
3525
|
const structNames = [...requiredStructs].sort();
|
|
@@ -3686,7 +3687,8 @@ conversionSite) => {
|
|
|
3686
3687
|
refused,
|
|
3687
3688
|
revisionFields: new Map(),
|
|
3688
3689
|
celledFields: new Map(),
|
|
3689
|
-
staticMethodStateClasses: new Set()
|
|
3690
|
+
staticMethodStateClasses: new Set(),
|
|
3691
|
+
structNames: new Set()
|
|
3690
3692
|
};
|
|
3691
3693
|
// `Value` can only project a genuinely dynamic class back to a nominal
|
|
3692
3694
|
// carrier when the allocation itself states an authenticated ancestry. The
|
|
@@ -3745,7 +3747,8 @@ conversionSite) => {
|
|
|
3745
3747
|
refused: [],
|
|
3746
3748
|
revisionFields,
|
|
3747
3749
|
celledFields,
|
|
3748
|
-
staticMethodStateClasses
|
|
3750
|
+
staticMethodStateClasses,
|
|
3751
|
+
structNames: requiredStructs
|
|
3749
3752
|
};
|
|
3750
3753
|
};
|
|
3751
3754
|
/**
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { asyncResultConventionsOf } from '../../ir/async-result-conventions.js';
|
|
2
2
|
import { prototypePropertyDeclarationsOf } from '../../ir/program-conversions.js';
|
|
3
|
+
import { dynamicProtocolOmissionsOf } from '../../ir/dynamic-protocol.js';
|
|
3
4
|
import { createNativeDebugSource } from './native-debug-source.js';
|
|
4
5
|
import { publishRealmStorage } from './realm-storage.js';
|
|
5
6
|
import { nativeSelectionHelpers } from './native-selection-helpers.js';
|
|
@@ -2141,16 +2142,52 @@ const renderTranslationUnitSession = (input) => {
|
|
|
2141
2142
|
// conversion builds and every body signature a record view's bound method
|
|
2142
2143
|
// or getter calls. They are `inline` where several units include the header
|
|
2143
2144
|
// that holds them.
|
|
2145
|
+
// The published omissions (`ir/dynamic-protocol.ts`), spelled as the
|
|
2146
|
+
// runtime's `gea::detail::dynamicProtocolOmitted` specializations. Only a
|
|
2147
|
+
// type whose program structs this unit declares can be named, and several
|
|
2148
|
+
// carriers can share one spelling, so the list is the set of distinct ones.
|
|
2149
|
+
const dynamicProtocolOmissions = dynamicProtocolOmissionsOf(input.classes, input.reflection, emissionRepresentations);
|
|
2150
|
+
const dynamicProtocolOmittedTypes = () => {
|
|
2151
|
+
if (dynamicProtocolOmissions === null)
|
|
2152
|
+
return [];
|
|
2153
|
+
const declared = (representation) => [...walkRepresentation(representation)].every((inner) => inner.kind === 'class-ref'
|
|
2154
|
+
? structs.structNames.has(cppClassName(inner.declaration))
|
|
2155
|
+
: inner.kind === 'record' || inner.kind === 'record-with-index' || (inner.kind === 'native-record-ref' && inner.native === null)
|
|
2156
|
+
? structs.structNames.has(cppRecordStructName(inner.shapeId))
|
|
2157
|
+
: true);
|
|
2158
|
+
const types = new Set();
|
|
2159
|
+
for (const declaration of dynamicProtocolOmissions.classes)
|
|
2160
|
+
if (structs.structNames.has(cppClassName(declaration)))
|
|
2161
|
+
types.add(cppClassName(declaration));
|
|
2162
|
+
for (const shape of dynamicProtocolOmissions.records)
|
|
2163
|
+
if (structs.structNames.has(cppRecordStructName(shape)))
|
|
2164
|
+
types.add(cppRecordStructName(shape));
|
|
2165
|
+
for (const array of dynamicProtocolOmissions.arrays)
|
|
2166
|
+
if (declared(array))
|
|
2167
|
+
types.add(cppTypeOf(array, 'owned'));
|
|
2168
|
+
return [...types].sort();
|
|
2169
|
+
};
|
|
2144
2170
|
const unionAliasMarker = '// gea-union-aliases';
|
|
2145
2171
|
const unitFunctionMarker = '// gea-unit-functions';
|
|
2146
2172
|
const unitFunctionLinkage = perFile ? 'inline ' : '[[maybe_unused]] static ';
|
|
2147
2173
|
const spliceUnionAliases = (source) => {
|
|
2148
2174
|
const { aliases, functions } = endUnionAliasing();
|
|
2149
2175
|
const forwards = [...recursiveContainers, ...structs.declarations, ...jsonStructs.declarations].filter((declaration) => declaration.startsWith('struct ') && declaration.endsWith(';') && !declaration.includes('{'));
|
|
2176
|
+
// Spelled after the alias session ends, so each is its full type.
|
|
2177
|
+
const omitted = dynamicProtocolOmittedTypes();
|
|
2150
2178
|
const block = [
|
|
2151
2179
|
...forwards,
|
|
2152
2180
|
...aliases.map(({ name, spelling }) => `using ${name} = ${spelling};`),
|
|
2153
|
-
...functions.map(({ signature }) => `${unitFunctionLinkage}${signature};`)
|
|
2181
|
+
...functions.map(({ signature }) => `${unitFunctionLinkage}${signature};`),
|
|
2182
|
+
...(omitted.length === 0
|
|
2183
|
+
? []
|
|
2184
|
+
: [
|
|
2185
|
+
...(isolate ? [namespaceClose] : []),
|
|
2186
|
+
'namespace gea::detail {',
|
|
2187
|
+
...omitted.map((type) => `template <> inline constexpr bool dynamicProtocolOmitted<${type}> = true;`),
|
|
2188
|
+
'} // namespace gea::detail',
|
|
2189
|
+
...(isolate ? [namespaceOpen] : [])
|
|
2190
|
+
])
|
|
2154
2191
|
].join('\n');
|
|
2155
2192
|
const definitions = functions.map(({ signature, body }) => `${unitFunctionLinkage}${signature} { ${body} }`).join('\n');
|
|
2156
2193
|
return spliceRendered(spliceRendered(source, unionAliasMarker, block), unitFunctionMarker, definitions);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geastack/compiler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.32",
|
|
4
4
|
"description": "The geastack TypeScript-to-C++ compiler: one semantic spine, no source-shaped authority, no boxing of statically typed values.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|