mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-16 14:58:05 +08:00
Let record nodes take element-consuming lazy inputs and convert map_points
This commit is contained in:
@@ -96,18 +96,12 @@ pub(crate) enum EvalStep<'a> {
|
||||
/// or return-tuple writes. Reads on lazy inputs belong to the record lowering
|
||||
/// of the flip class instead.
|
||||
pub(crate) fn has_record_io(parsed: &ParsedNodeFn) -> bool {
|
||||
let value_reads = parsed
|
||||
.fields
|
||||
.iter()
|
||||
.any(|field| !field.attribute_reads.is_empty() && matches!(field.ty, ParsedFieldType::Regular(_)));
|
||||
let value_reads = parsed.fields.iter().any(|field| !field.attribute_reads.is_empty() && matches!(field.ty, ParsedFieldType::Regular(_)));
|
||||
value_reads || record_writes(&slot_value_type(&parsed.output_type)).is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn has_lazy_reads(parsed: &ParsedNodeFn) -> bool {
|
||||
parsed
|
||||
.fields
|
||||
.iter()
|
||||
.any(|field| !field.attribute_reads.is_empty() && matches!(field.ty, ParsedFieldType::Node(_)))
|
||||
parsed.fields.iter().any(|field| !field.attribute_reads.is_empty() && matches!(field.ty, ParsedFieldType::Node(_)))
|
||||
}
|
||||
|
||||
/// The value inputs of a routing node (every regular field that is neither a
|
||||
@@ -266,10 +260,7 @@ pub(crate) fn record_shape(parsed: &ParsedNodeFn) -> Option<RecordShape> {
|
||||
_ => return None,
|
||||
};
|
||||
let writes = record_writes(&value);
|
||||
let has_reads = parsed
|
||||
.fields
|
||||
.iter()
|
||||
.any(|field| !field.attribute_reads.is_empty() && matches!(field.ty, ParsedFieldType::Regular(_)));
|
||||
let has_reads = parsed.fields.iter().any(|field| !field.attribute_reads.is_empty() && matches!(field.ty, ParsedFieldType::Regular(_)));
|
||||
if !has_reads && writes.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -283,13 +274,17 @@ pub(crate) fn record_shape(parsed: &ParsedNodeFn) -> Option<RecordShape> {
|
||||
// A first-field lazy carrier: the kernel evaluates the derived content
|
||||
// itself and returns its opaque row token beside the write set.
|
||||
let lazy_carrier = matches!(&carrier_field.ty, ParsedFieldType::Node(_));
|
||||
if parsed.fields.iter().skip(lazy_carrier as usize).any(|field| matches!(field.ty, ParsedFieldType::Node(_))) {
|
||||
// Lazy secondaries are consumed as plain elements; raw record edges and
|
||||
// ranked outputs have no element binding here.
|
||||
let unsupported_lazy_secondary = |field: &ParsedField| match &field.ty {
|
||||
ParsedFieldType::Node(NodeParsedField { output_type, .. }) => is_record_value(output_type) || crate::codegen::ir::strip_ilist(output_type).1 > 0 || !field.attribute_reads.is_empty(),
|
||||
ParsedFieldType::Regular(_) => false,
|
||||
};
|
||||
if parsed.fields.iter().skip(lazy_carrier as usize).any(|field| unsupported_lazy_secondary(field)) {
|
||||
return None;
|
||||
}
|
||||
let reads_well_placed = parsed.fields.iter().enumerate().all(|(index, field)| {
|
||||
field.attribute_reads.is_empty()
|
||||
|| (lazy_carrier && index == 0)
|
||||
|| (!field.is_data_field && matches!(&field.ty, ParsedFieldType::Regular(RegularParsedField { lend: None, .. })))
|
||||
field.attribute_reads.is_empty() || (lazy_carrier && index == 0) || (!field.is_data_field && matches!(&field.ty, ParsedFieldType::Regular(RegularParsedField { lend: None, .. })))
|
||||
});
|
||||
if !reads_well_placed {
|
||||
return None;
|
||||
|
||||
@@ -227,6 +227,13 @@ fn single_row_entries(parsed: &ParsedNodeFn, struct_name: &Ident, regular_fields
|
||||
};
|
||||
}
|
||||
match &field.ty {
|
||||
// An element-consuming lazy secondary of a record node rides a
|
||||
// record edge with a layout slot, like a reading secondary.
|
||||
ParsedFieldType::Node(NodeParsedField { output_type, .. })
|
||||
if matches!(ir::node_kind(&node), ir::NodeKind::RecordIo) && matches!(ir::lazy_binding(&node, index), ir::LazyBinding::Element) =>
|
||||
{
|
||||
SlotKind::Value(output_type.clone())
|
||||
}
|
||||
ParsedFieldType::Node(NodeParsedField { output_type, .. }) => SlotKind::Lazy(output_type.clone()),
|
||||
ParsedFieldType::Regular(RegularParsedField { ty, .. }) => match ir::value_binding(&node, index) {
|
||||
ir::ValueBinding::Materialized => SlotKind::Ranked(ty.clone()),
|
||||
@@ -261,9 +268,7 @@ fn single_row_entries(parsed: &ParsedNodeFn, struct_name: &Ident, regular_fields
|
||||
.collect();
|
||||
let ranked_source = |generic: &Ident| {
|
||||
regular_fields.iter().position(|field| match &field.ty {
|
||||
ParsedFieldType::Regular(RegularParsedField { ty, list_levels, implementations, .. }) => {
|
||||
*list_levels > 0 && !implementations.is_empty() && generic_extractable(ty, generic)
|
||||
}
|
||||
ParsedFieldType::Regular(RegularParsedField { ty, list_levels, implementations, .. }) => *list_levels > 0 && !implementations.is_empty() && generic_extractable(ty, generic),
|
||||
_ => false,
|
||||
})
|
||||
};
|
||||
@@ -297,166 +302,171 @@ fn single_row_entries(parsed: &ParsedNodeFn, struct_name: &Ident, regular_fields
|
||||
let arity = regular_fields.len();
|
||||
let names: Vec<&Ident> = regular_fields.iter().map(|field| &field.pat_ident.ident).collect();
|
||||
|
||||
let entries: Vec<TokenStream2> = row_assignments.iter().filter_map(|assignments| {
|
||||
// A row whose assignments did not all solve cannot instantiate the struct.
|
||||
if assignments.len() != carried.len() {
|
||||
return None;
|
||||
}
|
||||
let slots: Vec<SlotKind> = slots
|
||||
let entries: Vec<TokenStream2> = row_assignments
|
||||
.iter()
|
||||
.map(|slot| match slot {
|
||||
SlotKind::BaseGeneric(name) => SlotKind::BaseGeneric(name.clone()),
|
||||
SlotKind::BaseConcrete(ty) => SlotKind::BaseConcrete(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Value(ty) => SlotKind::Value(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Extracted(ty) => SlotKind::Extracted(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Ranked(ty) => SlotKind::Ranked(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Plain(ty) => SlotKind::Plain(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Lazy(ty) => SlotKind::Lazy(substitute_ident_types(ty, assignments)),
|
||||
})
|
||||
.collect();
|
||||
.filter_map(|assignments| {
|
||||
// A row whose assignments did not all solve cannot instantiate the struct.
|
||||
if assignments.len() != carried.len() {
|
||||
return None;
|
||||
}
|
||||
let slots: Vec<SlotKind> = slots
|
||||
.iter()
|
||||
.map(|slot| match slot {
|
||||
SlotKind::BaseGeneric(name) => SlotKind::BaseGeneric(name.clone()),
|
||||
SlotKind::BaseConcrete(ty) => SlotKind::BaseConcrete(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Value(ty) => SlotKind::Value(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Extracted(ty) => SlotKind::Extracted(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Ranked(ty) => SlotKind::Ranked(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Plain(ty) => SlotKind::Plain(substitute_ident_types(ty, assignments)),
|
||||
SlotKind::Lazy(ty) => SlotKind::Lazy(substitute_ident_types(ty, assignments)),
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Every non-base value/plain/lazy input must be concrete.
|
||||
let values_concrete = regular_fields.iter().zip(&slots).all(|(field, slot)| match slot {
|
||||
SlotKind::BaseGeneric(_) | SlotKind::BaseConcrete(_) => true,
|
||||
SlotKind::Value(ty) | SlotKind::Extracted(ty) | SlotKind::Ranked(ty) | SlotKind::Plain(ty) | SlotKind::Lazy(ty) => {
|
||||
!contains_open_generic(parsed, ty) && (lend(field) || !type_disqualifies(ty))
|
||||
}
|
||||
});
|
||||
if !values_concrete {
|
||||
return None;
|
||||
}
|
||||
|
||||
let input_types = slots.iter().map(|slot| match slot {
|
||||
SlotKind::BaseGeneric(name) => quote!(gcore::registry::generic_record_edge_type(#name)),
|
||||
SlotKind::BaseConcrete(ty) | SlotKind::Value(ty) | SlotKind::Extracted(ty) | SlotKind::Ranked(ty) => quote!(gcore::registry::record_edge_type::<#ty>()),
|
||||
SlotKind::Plain(ty) | SlotKind::Lazy(ty) => quote!(gcore::registry::edge_type::<#ty>()),
|
||||
});
|
||||
|
||||
let downcasts = names.iter().zip(&slots).enumerate().map(|(index, (name, slot))| {
|
||||
let handle = format_ident!("__handle_{index}");
|
||||
let layout = format_ident!("__layout_{index}");
|
||||
let ty = format_ident!("__ty_{index}");
|
||||
match slot {
|
||||
SlotKind::BaseGeneric(_) | SlotKind::BaseConcrete(_) => quote! {
|
||||
let #handle = inputs.next().unwrap();
|
||||
let #ty = #handle.ty().clone();
|
||||
let #layout = #handle.layout().clone();
|
||||
let #name = #handle.downcast_erased::<gcore::registry::ErasedRecordNode>(#ty.clone())?;
|
||||
},
|
||||
SlotKind::Value(value_ty) => quote! {
|
||||
let #handle = inputs.next().unwrap();
|
||||
let #layout = #handle.layout().clone();
|
||||
let #name = #handle.downcast_record::<#value_ty>()?;
|
||||
},
|
||||
SlotKind::Extracted(value_ty) => quote! {
|
||||
let #handle = inputs.next().unwrap();
|
||||
let #layout = #handle.layout().clone();
|
||||
let #name = gcore::record::RecordExtract::<#value_ty, _>::new(#handle.downcast_record::<#value_ty>()?, &#layout);
|
||||
},
|
||||
SlotKind::Ranked(value_ty) => quote! {
|
||||
let #name = inputs.next().unwrap().downcast_record::<#value_ty>()?;
|
||||
},
|
||||
SlotKind::Plain(value_ty) | SlotKind::Lazy(value_ty) => quote!(let #name = inputs.next().unwrap().downcast::<#value_ty>()?;),
|
||||
}
|
||||
});
|
||||
|
||||
let base_indices: Vec<usize> = slots.iter().enumerate().filter(|(_, slot)| slot.is_base()).map(|(index, _)| index).collect();
|
||||
let value_indices: Vec<usize> = slots.iter().enumerate().filter(|(_, slot)| matches!(slot, SlotKind::Value(_))).map(|(index, _)| index).collect();
|
||||
let value_layout_args: Vec<TokenStream2> = value_indices
|
||||
.iter()
|
||||
.map(|index| {
|
||||
let layout = format_ident!("__layout_{index}");
|
||||
quote!(&#layout,)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let carried_meta = || {
|
||||
let meta = ir::layout_meta_tokens(&node, quote!(gcore::record::ElementSpec::Carried), &core_types);
|
||||
quote!(Some(#meta))
|
||||
};
|
||||
|
||||
// The output wire and node wrap follow the output element: a concrete (or
|
||||
// row-assigned) element is a typed record; a generic or opaque element is
|
||||
// an erased record carrying the first base slot's runtime type.
|
||||
let output_element = match &node.output.shape.element {
|
||||
ir::Element::Concrete(element) => Some(substitute_ident_types(element, assignments)),
|
||||
ir::Element::Generic(ident) => assignments.iter().find(|(generic, _)| generic == ident).map(|(_, ty)| ty.clone()),
|
||||
ir::Element::Opaque => None,
|
||||
};
|
||||
let (io_output, wrap) = match &output_element {
|
||||
Some(element) => (
|
||||
quote!(gcore::registry::record_type::<#element>()),
|
||||
quote!(Ok(gcore::registry::EdgeHandle::new_record::<#element>(::std::sync::Arc::new(__node)))),
|
||||
),
|
||||
None => {
|
||||
let name = match &node.output.shape.element {
|
||||
ir::Element::Generic(ident) => ident.to_string(),
|
||||
_ => "T".to_string(),
|
||||
};
|
||||
let base_ty = format_ident!("__ty_{}", base_indices[0]);
|
||||
(
|
||||
quote!(gcore::Type::Record(Box::new(gcore::Type::Generic(::std::borrow::Cow::Borrowed(#name))))),
|
||||
quote!(Ok(gcore::registry::EdgeHandle::new_erased(::std::sync::Arc::new(__node) as ::std::sync::Arc<gcore::registry::ErasedRecordNode>, #base_ty))),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let (prelude, new_layout_args, layout_meta) = match ir::node_kind(&node) {
|
||||
ir::NodeKind::RecordIo => {
|
||||
let carrier_arg = (node.inputs.first().is_some_and(|input| input.subject) && ir::materialized_levels(&node, 0) == 0).then(|| quote!(&__layout_0,));
|
||||
let layout_meta_fn = format_ident!("{}_layout_meta", fn_name);
|
||||
(quote!(), quote!(#carrier_arg #(#value_layout_args)*), quote!(Some(self::#layout_meta_fn())))
|
||||
}
|
||||
ir::NodeKind::Routing => {
|
||||
let source_layouts = base_indices.iter().map(|index| format_ident!("__layout_{index}"));
|
||||
let source_wraps = base_indices.iter().map(|index| {
|
||||
let name = names[*index];
|
||||
let layout = format_ident!("__layout_{index}");
|
||||
quote!(let #name = gcore::record::RecordSource::new(#name, &#layout, &__union);)
|
||||
});
|
||||
let prelude = quote! {
|
||||
let __union = gcore::record::Layout::union(&[#(&#source_layouts),*]);
|
||||
#(#source_wraps)*
|
||||
};
|
||||
(prelude, quote!(&__union, #(#value_layout_args)*), carried_meta())
|
||||
}
|
||||
ir::NodeKind::Opaque => {
|
||||
let record_layout = format_ident!("__layout_{}", base_indices[0]);
|
||||
(quote!(), quote!(&#record_layout), carried_meta())
|
||||
}
|
||||
ir::NodeKind::Flip => unreachable!("flip has its own multi-row emitter"),
|
||||
};
|
||||
|
||||
// A carried generic instantiates through the struct's trailing phantom
|
||||
// parameters, so the constructor names the row's types after one inferred
|
||||
// slot per input field.
|
||||
let turbofish = (!carried.is_empty()).then(|| {
|
||||
let underscores = (0..arity).map(|_| quote!(_));
|
||||
let carried_types = carried.iter().filter_map(|(generic, _)| assignments.iter().find(|(ident, _)| ident == generic).map(|(_, ty)| quote!(#ty)));
|
||||
quote!(::<#(#underscores,)* #(#carried_types,)*>)
|
||||
});
|
||||
|
||||
Some(quote! {
|
||||
gcore::registry::RegistryEntry {
|
||||
layout_meta: #layout_meta,
|
||||
io: gcore::registry::NodeIOTypes::new(
|
||||
gcore::concrete!(gcore::context::ContextImpl<'static>),
|
||||
#io_output,
|
||||
vec![#(#input_types),*],
|
||||
),
|
||||
constructor: |inputs| {
|
||||
if inputs.len() != #arity {
|
||||
return Err(gcore::registry::ConstructionError::Arity { expected: #arity, got: inputs.len() });
|
||||
// Every non-base value/plain/lazy input must be concrete.
|
||||
let values_concrete = regular_fields.iter().zip(&slots).all(|(field, slot)| match slot {
|
||||
SlotKind::BaseGeneric(_) | SlotKind::BaseConcrete(_) => true,
|
||||
SlotKind::Value(ty) | SlotKind::Extracted(ty) | SlotKind::Ranked(ty) | SlotKind::Plain(ty) | SlotKind::Lazy(ty) => {
|
||||
!contains_open_generic(parsed, ty) && (lend(field) || !type_disqualifies(ty))
|
||||
}
|
||||
let mut inputs = inputs.into_iter();
|
||||
#(#downcasts)*
|
||||
#prelude
|
||||
let __node = #struct_name #turbofish::new(#(#names,)* #new_layout_args);
|
||||
#wrap
|
||||
},
|
||||
}
|
||||
})
|
||||
}).collect();
|
||||
});
|
||||
if !values_concrete {
|
||||
return None;
|
||||
}
|
||||
|
||||
let input_types = slots.iter().map(|slot| match slot {
|
||||
SlotKind::BaseGeneric(name) => quote!(gcore::registry::generic_record_edge_type(#name)),
|
||||
SlotKind::BaseConcrete(ty) | SlotKind::Value(ty) | SlotKind::Extracted(ty) | SlotKind::Ranked(ty) => quote!(gcore::registry::record_edge_type::<#ty>()),
|
||||
SlotKind::Plain(ty) | SlotKind::Lazy(ty) => quote!(gcore::registry::edge_type::<#ty>()),
|
||||
});
|
||||
|
||||
let downcasts = names.iter().zip(&slots).enumerate().map(|(index, (name, slot))| {
|
||||
let handle = format_ident!("__handle_{index}");
|
||||
let layout = format_ident!("__layout_{index}");
|
||||
let ty = format_ident!("__ty_{index}");
|
||||
match slot {
|
||||
SlotKind::BaseGeneric(_) | SlotKind::BaseConcrete(_) => quote! {
|
||||
let #handle = inputs.next().unwrap();
|
||||
let #ty = #handle.ty().clone();
|
||||
let #layout = #handle.layout().clone();
|
||||
let #name = #handle.downcast_erased::<gcore::registry::ErasedRecordNode>(#ty.clone())?;
|
||||
},
|
||||
SlotKind::Value(value_ty) => quote! {
|
||||
let #handle = inputs.next().unwrap();
|
||||
let #layout = #handle.layout().clone();
|
||||
let #name = #handle.downcast_record::<#value_ty>()?;
|
||||
},
|
||||
SlotKind::Extracted(value_ty) => quote! {
|
||||
let #handle = inputs.next().unwrap();
|
||||
let #layout = #handle.layout().clone();
|
||||
let #name = gcore::record::RecordExtract::<#value_ty, _>::new(#handle.downcast_record::<#value_ty>()?, &#layout);
|
||||
},
|
||||
SlotKind::Ranked(value_ty) => quote! {
|
||||
let #name = inputs.next().unwrap().downcast_record::<#value_ty>()?;
|
||||
},
|
||||
SlotKind::Plain(value_ty) | SlotKind::Lazy(value_ty) => quote!(let #name = inputs.next().unwrap().downcast::<#value_ty>()?;),
|
||||
}
|
||||
});
|
||||
|
||||
let base_indices: Vec<usize> = slots.iter().enumerate().filter(|(_, slot)| slot.is_base()).map(|(index, _)| index).collect();
|
||||
let value_indices: Vec<usize> = slots.iter().enumerate().filter(|(_, slot)| matches!(slot, SlotKind::Value(_))).map(|(index, _)| index).collect();
|
||||
let value_layout_args: Vec<TokenStream2> = value_indices
|
||||
.iter()
|
||||
.map(|index| {
|
||||
let layout = format_ident!("__layout_{index}");
|
||||
quote!(&#layout,)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let carried_meta = || {
|
||||
let meta = ir::layout_meta_tokens(&node, quote!(gcore::record::ElementSpec::Carried), &core_types);
|
||||
quote!(Some(#meta))
|
||||
};
|
||||
|
||||
// The output wire and node wrap follow the output element: a concrete (or
|
||||
// row-assigned) element is a typed record; a generic or opaque element is
|
||||
// an erased record carrying the first base slot's runtime type.
|
||||
let output_element = match &node.output.shape.element {
|
||||
ir::Element::Concrete(element) => Some(substitute_ident_types(element, assignments)),
|
||||
ir::Element::Generic(ident) => assignments.iter().find(|(generic, _)| generic == ident).map(|(_, ty)| ty.clone()),
|
||||
ir::Element::Opaque => None,
|
||||
};
|
||||
let (io_output, wrap) = match &output_element {
|
||||
Some(element) => (
|
||||
quote!(gcore::registry::record_type::<#element>()),
|
||||
quote!(Ok(gcore::registry::EdgeHandle::new_record::<#element>(::std::sync::Arc::new(__node)))),
|
||||
),
|
||||
None => {
|
||||
let name = match &node.output.shape.element {
|
||||
ir::Element::Generic(ident) => ident.to_string(),
|
||||
_ => "T".to_string(),
|
||||
};
|
||||
let base_ty = format_ident!("__ty_{}", base_indices[0]);
|
||||
(
|
||||
quote!(gcore::Type::Record(Box::new(gcore::Type::Generic(::std::borrow::Cow::Borrowed(#name))))),
|
||||
quote!(Ok(gcore::registry::EdgeHandle::new_erased(::std::sync::Arc::new(__node) as ::std::sync::Arc<gcore::registry::ErasedRecordNode>, #base_ty))),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let (prelude, new_layout_args, layout_meta) = match ir::node_kind(&node) {
|
||||
ir::NodeKind::RecordIo => {
|
||||
let carrier_arg = (node.inputs.first().is_some_and(|input| input.subject) && ir::materialized_levels(&node, 0) == 0).then(|| quote!(&__layout_0,));
|
||||
let layout_meta_fn = format_ident!("{}_layout_meta", fn_name);
|
||||
(quote!(), quote!(#carrier_arg #(#value_layout_args)*), quote!(Some(self::#layout_meta_fn())))
|
||||
}
|
||||
ir::NodeKind::Routing => {
|
||||
let source_layouts = base_indices.iter().map(|index| format_ident!("__layout_{index}"));
|
||||
let source_wraps = base_indices.iter().map(|index| {
|
||||
let name = names[*index];
|
||||
let layout = format_ident!("__layout_{index}");
|
||||
quote!(let #name = gcore::record::RecordSource::new(#name, &#layout, &__union);)
|
||||
});
|
||||
let prelude = quote! {
|
||||
let __union = gcore::record::Layout::union(&[#(&#source_layouts),*]);
|
||||
#(#source_wraps)*
|
||||
};
|
||||
(prelude, quote!(&__union, #(#value_layout_args)*), carried_meta())
|
||||
}
|
||||
ir::NodeKind::Opaque => {
|
||||
let record_layout = format_ident!("__layout_{}", base_indices[0]);
|
||||
(quote!(), quote!(&#record_layout), carried_meta())
|
||||
}
|
||||
ir::NodeKind::Flip => unreachable!("flip has its own multi-row emitter"),
|
||||
};
|
||||
|
||||
// A carried generic instantiates through the struct's trailing phantom
|
||||
// parameters, so the constructor names the row's types after one inferred
|
||||
// slot per input field.
|
||||
let turbofish = (!carried.is_empty()).then(|| {
|
||||
let underscores = (0..arity).map(|_| quote!(_));
|
||||
let carried_types = carried
|
||||
.iter()
|
||||
.filter_map(|(generic, _)| assignments.iter().find(|(ident, _)| ident == generic).map(|(_, ty)| quote!(#ty)));
|
||||
quote!(::<#(#underscores,)* #(#carried_types,)*>)
|
||||
});
|
||||
|
||||
Some(quote! {
|
||||
gcore::registry::RegistryEntry {
|
||||
layout_meta: #layout_meta,
|
||||
io: gcore::registry::NodeIOTypes::new(
|
||||
gcore::concrete!(gcore::context::ContextImpl<'static>),
|
||||
#io_output,
|
||||
vec![#(#input_types),*],
|
||||
),
|
||||
constructor: |inputs| {
|
||||
if inputs.len() != #arity {
|
||||
return Err(gcore::registry::ConstructionError::Arity { expected: #arity, got: inputs.len() });
|
||||
}
|
||||
let mut inputs = inputs.into_iter();
|
||||
#(#downcasts)*
|
||||
#prelude
|
||||
let __node = #struct_name #turbofish::new(#(#names,)* #new_layout_args);
|
||||
#wrap
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
if entries.is_empty() {
|
||||
return quote!();
|
||||
|
||||
@@ -109,7 +109,12 @@ fn monomorphizations(parsed: &ParsedNodeFn, fields: &[&ParsedField], generics: &
|
||||
};
|
||||
let positions: Option<Vec<(Ident, usize)>> = generics
|
||||
.iter()
|
||||
.map(|generic| fields.iter().position(|&field| generic_extractable(field_element_type(field), generic)).map(|index| (generic.clone(), index)))
|
||||
.map(|generic| {
|
||||
fields
|
||||
.iter()
|
||||
.position(|&field| generic_extractable(field_element_type(field), generic))
|
||||
.map(|index| (generic.clone(), index))
|
||||
})
|
||||
.collect();
|
||||
let Some(positions) = positions else {
|
||||
return Vec::new();
|
||||
@@ -145,7 +150,13 @@ fn item_shape(element: &Type, depth: u8, reads: &[AttributeRead], generics: &[Id
|
||||
ItemShape {
|
||||
element: element_of(element, generics),
|
||||
depth,
|
||||
attrs: reads.iter().map(|read| LevelAttr { marker: read.marker.clone(), level: 0 }).collect(),
|
||||
attrs: reads
|
||||
.iter()
|
||||
.map(|read| LevelAttr {
|
||||
marker: read.marker.clone(),
|
||||
level: 0,
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,6 +341,20 @@ impl ValueBinding {
|
||||
}
|
||||
}
|
||||
|
||||
/// A record node's lazy inputs consumed as plain elements: their record edges
|
||||
/// need a layout slot at wiring, like the reading secondaries.
|
||||
pub(crate) fn element_lazy_indices(regular_fields: &[&ParsedField], node: &Node) -> Vec<usize> {
|
||||
if !matches!(node_kind(node), NodeKind::RecordIo) {
|
||||
return Vec::new();
|
||||
}
|
||||
regular_fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(index, field)| matches!(field.ty, ParsedFieldType::Node(_)) && matches!(lazy_binding(node, *index), LazyBinding::Element))
|
||||
.map(|(index, _)| index)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum NodeKind {
|
||||
Flip,
|
||||
@@ -356,7 +381,10 @@ fn is_routing(node: &Node) -> bool {
|
||||
let Element::Generic(output) = &node.output.shape.element else { return false };
|
||||
node.monomorphizations.is_empty()
|
||||
&& node.generics.iter().any(|generic| &generic.ident == output && generic.bounds.is_empty())
|
||||
&& node.inputs.iter().any(|input| input.subject && matches!(&input.shape.element, Element::Generic(generic) if generic == output))
|
||||
&& node
|
||||
.inputs
|
||||
.iter()
|
||||
.any(|input| input.subject && matches!(&input.shape.element, Element::Generic(generic) if generic == output))
|
||||
}
|
||||
|
||||
fn has_attr_io(node: &Node) -> bool {
|
||||
@@ -401,7 +429,7 @@ pub(crate) fn lazy_binding(node: &Node, index: usize) -> LazyBinding {
|
||||
LazyBinding::DeriveRouting
|
||||
} else if node.derives && matches!(kind, NodeKind::RecordIo) && input.subject {
|
||||
LazyBinding::DeriveCarrier
|
||||
} else if matches!(kind, NodeKind::Flip) {
|
||||
} else if matches!(kind, NodeKind::Flip) || (matches!(kind, NodeKind::RecordIo) && !input.subject) {
|
||||
LazyBinding::Element
|
||||
} else if matches!(input.shape.element, Element::Opaque) {
|
||||
LazyBinding::OpaqueRecord
|
||||
@@ -577,7 +605,9 @@ mod tests {
|
||||
delta: 0,
|
||||
}
|
||||
} else if kinds.opaque {
|
||||
let record = fields.iter().position(|field| matches!(&field.ty, ParsedFieldType::Node(NodeParsedField { output_type, .. }) if is_record_value(output_type)));
|
||||
let record = fields
|
||||
.iter()
|
||||
.position(|field| matches!(&field.ty, ParsedFieldType::Node(NodeParsedField { output_type, .. }) if is_record_value(output_type)));
|
||||
Facts {
|
||||
sources: record.into_iter().collect(),
|
||||
carried: true,
|
||||
@@ -588,7 +618,12 @@ mod tests {
|
||||
} else if kinds.routing {
|
||||
let generic = routing_generic(parsed).expect("routing has a generic");
|
||||
Facts {
|
||||
sources: fields.iter().enumerate().filter(|(_, field)| bare_ident(&source_ty(field)) == Some(&generic)).map(|(index, _)| index).collect(),
|
||||
sources: fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, field)| bare_ident(&source_ty(field)) == Some(&generic))
|
||||
.map(|(index, _)| index)
|
||||
.collect(),
|
||||
carried: true,
|
||||
writes: vec![],
|
||||
removes: vec![],
|
||||
@@ -617,7 +652,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn bridge_flip_concrete() {
|
||||
assert_bridge(quote!(category("")), quote!(fn negate(_: impl Ctx, x: f64) -> f64 { -x }));
|
||||
assert_bridge(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn negate(_: impl Ctx, x: f64) -> f64 {
|
||||
-x
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -632,17 +674,38 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn bridge_record_write() {
|
||||
assert_bridge(quote!(category("")), quote!(fn set_opacity(_: impl Ctx, val: f64) -> (f64, Attr<Opacity>) { (val, Attr(1.)) }));
|
||||
assert_bridge(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn set_opacity(_: impl Ctx, val: f64) -> (f64, Attr<Opacity>) {
|
||||
(val, Attr(1.))
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bridge_record_remove() {
|
||||
assert_bridge(quote!(category("")), quote!(fn strip(_: impl Ctx, val: f64) -> (f64, RemoveAttr<Opacity>) { (val, RemoveAttr) }));
|
||||
assert_bridge(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn strip(_: impl Ctx, val: f64) -> (f64, RemoveAttr<Opacity>) {
|
||||
(val, RemoveAttr)
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bridge_record_fresh() {
|
||||
assert_bridge(quote!(category("")), quote!(fn make(_: impl Ctx, _: (), fill: f64) -> (f64, Attr<Opacity>) { (fill, Attr(1.)) }));
|
||||
assert_bridge(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn make(_: impl Ctx, _: (), fill: f64) -> (f64, Attr<Opacity>) {
|
||||
(fill, Attr(1.))
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -724,7 +787,7 @@ mod tests {
|
||||
match &field.ty {
|
||||
ParsedFieldType::Regular(_) => match value_binding(node, index) {
|
||||
ValueBinding::Carrier => "carrier",
|
||||
ValueBinding::Materialized => "materialized",
|
||||
ValueBinding::Materialized => "materialized",
|
||||
ValueBinding::Lend => "lend",
|
||||
ValueBinding::ReadingSecondary => "reading",
|
||||
ValueBinding::RecordElement => "record",
|
||||
@@ -767,57 +830,109 @@ mod tests {
|
||||
assert_eq!(actual_kind, expected_kind, "node_kind of {}", parsed.fn_name);
|
||||
let fields: Vec<&ParsedField> = parsed.fields.iter().filter(|field| !field.is_data_field).collect();
|
||||
for (index, field) in fields.iter().enumerate() {
|
||||
assert_eq!(
|
||||
ir_label(&node, index, field, raw),
|
||||
reference_label(&parsed, raw, index, field),
|
||||
"field {index} of {}",
|
||||
parsed.fn_name
|
||||
);
|
||||
assert_eq!(ir_label(&node, index, field, raw), reference_label(&parsed, raw, index, field), "field {index} of {}", parsed.fn_name);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_flip() {
|
||||
assert_bindings(quote!(category("")), quote!(fn negate(_: impl Ctx, x: f64) -> f64 { -x }));
|
||||
assert_bindings(quote!(category("")), quote!(fn add2(_: impl Ctx, a: f64, b: f64) -> f64 { a + b }));
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn negate(_: impl Ctx, x: f64) -> f64 {
|
||||
-x
|
||||
}
|
||||
),
|
||||
);
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn add2(_: impl Ctx, a: f64, b: f64) -> f64 {
|
||||
a + b
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_lend() {
|
||||
assert_bindings(quote!(category("")), quote!(fn borrow(_: impl Ctx, prim: f64, other: &f64) -> f64 { prim + *other }));
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn borrow(_: impl Ctx, prim: f64, other: &f64) -> f64 {
|
||||
prim + *other
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_reading_secondary() {
|
||||
assert_bindings(quote!(category("")), quote!(fn read_op(_: impl Ctx, carrier: f64, (other, op): (f64, Attr<Opacity>)) -> f64 { carrier + other }));
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn read_op(_: impl Ctx, carrier: f64, (other, op): (f64, Attr<Opacity>)) -> f64 {
|
||||
carrier + other
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_flip_lazy() {
|
||||
assert_bindings(quote!(category("")), quote!(fn apply(_: impl Ctx, inner: impl Node<(), Output = f64>) -> f64 { inner.eval(()) }));
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn apply(_: impl Ctx, inner: impl Node<(), Output = f64>) -> f64 {
|
||||
inner.eval(())
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_flip_lazy_reads() {
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(fn apply_reads(_: impl Ctx, carrier: f64, inner: impl Node<(), Output = (f64, Attr<Opacity>)>) -> f64 { carrier + inner.eval(()).0 }),
|
||||
quote!(
|
||||
fn apply_reads(_: impl Ctx, carrier: f64, inner: impl Node<(), Output = (f64, Attr<Opacity>)>) -> f64 {
|
||||
carrier + inner.eval(()).0
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_flip_raw() {
|
||||
assert_bindings(quote!(category("")), quote!(fn poll_apply(_: impl Ctx, inner: impl Node<(), Output = f64>) -> GPoll<f64> { inner.eval(()) }));
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn poll_apply(_: impl Ctx, inner: impl Node<(), Output = f64>) -> GPoll<f64> {
|
||||
inner.eval(())
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_skip_impl_generic() {
|
||||
// A bounded generic forwarded whole (passthrough) flips, not routes.
|
||||
assert_bindings(quote!(category(""), skip_impl), quote!(fn passthrough<T: Send>(_: impl Ctx, content: T) -> T { content }));
|
||||
assert_bindings(
|
||||
quote!(category(""), skip_impl),
|
||||
quote!(
|
||||
fn passthrough<T: Send>(_: impl Ctx, content: T) -> T {
|
||||
content
|
||||
}
|
||||
),
|
||||
);
|
||||
// A generic transformed into a different output type flips.
|
||||
assert_bindings(
|
||||
quote!(category(""), skip_impl),
|
||||
quote!(fn into_ty<T: Send + Into<O>, O: Send>(_: impl Ctx, value: T, #[data] _out: PhantomData<O>) -> O { value.into() }),
|
||||
quote!(
|
||||
fn into_ty<T: Send + Into<O>, O: Send>(_: impl Ctx, value: T, #[data] _out: PhantomData<O>) -> O {
|
||||
value.into()
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -825,20 +940,35 @@ mod tests {
|
||||
fn bindings_routing() {
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(fn switch<T>(_: impl Ctx, condition: bool, off: impl Node<(), Output = T>, on: impl Node<(), Output = T>) -> T { if condition { on.eval(()) } else { off.eval(()) } }),
|
||||
quote!(
|
||||
fn switch<T>(_: impl Ctx, condition: bool, off: impl Node<(), Output = T>, on: impl Node<(), Output = T>) -> T {
|
||||
if condition { on.eval(()) } else { off.eval(()) }
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_derive_routing() {
|
||||
assert_bindings(quote!(category("")), quote!(fn ctx_mod<T>(_: impl Ctx + DeriveCtx, inner: impl Node<(), Output = T>) -> T { inner.eval(()) }));
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn ctx_mod<T>(_: impl Ctx + DeriveCtx, inner: impl Node<(), Output = T>) -> T {
|
||||
inner.eval(())
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bindings_opaque() {
|
||||
assert_bindings(
|
||||
quote!(category("")),
|
||||
quote!(fn memo<'e>(_: impl Ctx, #[data] cache: Store, content: impl Node<Context<'_>, Output = RecordValue<'e>>) -> GPoll<RecordValue<'e>> { content.eval(()) }),
|
||||
quote!(
|
||||
fn memo<'e>(_: impl Ctx, #[data] cache: Store, content: impl Node<Context<'_>, Output = RecordValue<'e>>) -> GPoll<RecordValue<'e>> {
|
||||
content.eval(())
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -846,7 +976,11 @@ mod tests {
|
||||
fn creator_ilist_return_pushes_a_level() {
|
||||
let mut parsed = parse_node_fn(
|
||||
quote!(category(""), extent(repeat_extent)),
|
||||
quote!(fn repeat<T>(_: impl Ctx, (element, transform): (T, Attr<Transform>), count: u32) -> IList<(T, Attr<Transform>)> { emit(element, Attr(count as f64)) }),
|
||||
quote!(
|
||||
fn repeat<T>(_: impl Ctx, (element, transform): (T, Attr<Transform>), count: u32) -> IList<(T, Attr<Transform>)> {
|
||||
emit(element, Attr(count as f64))
|
||||
}
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
parsed.replace_impl_trait_in_input();
|
||||
@@ -862,7 +996,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn reducer_ilist_input_collapses_a_level() {
|
||||
let mut parsed = parse_node_fn(quote!(category("")), quote!(fn sum(_: impl Ctx, items: IList<f64>) -> f64 { items.into_iter().sum() })).unwrap();
|
||||
let mut parsed = parse_node_fn(
|
||||
quote!(category("")),
|
||||
quote!(
|
||||
fn sum(_: impl Ctx, items: IList<f64>) -> f64 {
|
||||
items.into_iter().sum()
|
||||
}
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
parsed.replace_impl_trait_in_input();
|
||||
let node = build(&parsed);
|
||||
// The `IList` input is a depth-1 subject; the scalar output collapses it.
|
||||
|
||||
@@ -91,7 +91,6 @@ pub(crate) fn generate_phantom_data<'a>(fn_generics: impl Iterator<Item = &'a cr
|
||||
(fn_generic_params, phantom_data_declerations)
|
||||
}
|
||||
|
||||
|
||||
/// Get only the necessary generics.
|
||||
struct FilterUsedGenerics {
|
||||
all: Vec<crate::GenericParam>,
|
||||
|
||||
Reference in New Issue
Block a user