xamlx 0.3.0

Rust port of the backend-independent part of XamlX (XAML compiler library)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Port of `Transform/XamlTransformHelpers.cs`.

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use crate::ast::{
    IXamlAstValueNode, IXamlLineInfo, XamlAstClrProperty, XamlAstClrTypeReference,
    XamlAstContextLocalNode, XamlAstExtensions, XamlAstNeedsParentStackValueNode,
    XamlAstNewClrObjectNode, XamlAstNodeExtensions, XamlAstRuntimeCastNode, XamlAstTextNode,
    XamlLoadMethodDelegateNode, XamlMarkupExtensionNode, XamlStaticOrTargetedReturnMethodCallNode,
    XamlTypeExtensionNode,
};
use crate::exceptions::{XamlError, XamlResult};
use crate::type_system::{
    IXamlCustomAttribute, IXamlMethod, IXamlParameterInfo, IXamlProperty, IXamlType,
    TypeSystemHelpers, XamlTypeKey, XamlValue,
};

use super::transformers::TypeReferenceResolver;
use super::{AstTransformationContext, TransformerConfiguration};

#[derive(Default)]
struct AdderCache(RefCell<HashMap<XamlTypeKey, Vec<Rc<dyn IXamlMethod>>>>);

#[derive(Default)]
struct MarkupExtensionProvideValueCache {
    type_to_provide_value: RefCell<HashMap<XamlTypeKey, Option<Rc<dyn IXamlMethod>>>>,
}

pub struct XamlTransformHelpers;

impl XamlTransformHelpers {
    pub fn find_possible_adders(
        context: &AstTransformationContext,
        type_: &Rc<dyn IXamlType>,
    ) -> XamlResult<Vec<Rc<dyn IXamlMethod>>> {
        let find_possible_adders_impl = || -> XamlResult<Vec<Rc<dyn IXamlMethod>>> {
            let configuration = context.configuration();
            let known = configuration.well_known_types();

            // Attempt to cast IEnumerable and IEnumerable<T> to IList<T>
            let mut actual_type = type_.clone();
            if actual_type.equals(&*known.i_enumerable) {
                actual_type = known.i_list.clone();
            }
            if actual_type
                .generic_type_definition()
                .is_some_and(|d| d.equals(&*known.i_enumerable_of_t))
            {
                let argument = first_generic_argument(&actual_type)?;
                actual_type = known.i_list_of_t.make_generic_type(&[argument])?;
            }

            let mut inspect_types = vec![actual_type.clone()];
            inspect_types.extend(actual_type.get_all_interfaces());

            // If type supports IList<T> don't fall back to IList
            if inspect_types.iter().any(|t| {
                t.generic_type_definition()
                    .is_some_and(|d| d.equals(&*known.i_list_of_t))
            }) {
                inspect_types.retain(|t| !t.equals(&*known.i_list));
            }

            let mut rv: Vec<Rc<dyn IXamlMethod>> = Vec::new();
            for t in &inspect_types {
                for m in t.find_methods(|m| {
                    m.name() == "Add"
                        && m.is_public()
                        && !m.is_static()
                        && (m.parameters().len() == 1 || m.parameters().len() == 2)
                }) {
                    if rv.iter().any(|em| em.equals(&*m)) {
                        continue;
                    }
                    rv.push(m);
                }
            }

            // First use methods from the type itself, then from base types, then from interfaces
            let mut keyed = Vec::with_capacity(rv.len());
            for m in rv {
                let this = m.this_or_first_parameter()?;
                keyed.push((!this.equals(&*actual_type), this.is_interface(), m));
            }
            keyed.sort_by_key(|(not_actual, is_interface, _)| (*not_actual, *is_interface));
            let mut rv: Vec<Rc<dyn IXamlMethod>> = keyed.into_iter().map(|(_, _, m)| m).collect();

            if let Some(i_add_child_of_t) = &configuration.type_mappings.i_add_child_of_t {
                for t in inspect_types.iter().filter(|t| {
                    t.generic_type_definition()
                        .is_some_and(|d| d.equals(&**i_add_child_of_t))
                }) {
                    rv.push(t.get_method(|x| x.name() == "AddChild")?);
                }
            }

            if let Some(i_add_child) = &configuration.type_mappings.i_add_child {
                for t in inspect_types.iter().filter(|t| t.equals(&**i_add_child)) {
                    rv.push(t.get_method(|x| x.name() == "AddChild")?);
                }
            }

            Ok(rv)
        };

        let cache = context.get_or_create_item::<AdderCache>();
        let key = XamlTypeKey(type_.clone());
        if let Some(rvr) = cache.0.borrow().get(&key) {
            return Ok(rvr.clone());
        }
        let rv = find_possible_adders_impl()?;
        cache.0.borrow_mut().insert(key, rv.clone());
        Ok(rv)
    }

    pub fn get_markup_extension_provide_value_alternatives(
        context: &AstTransformationContext,
        type_: &dyn IXamlType,
    ) -> XamlResult<Vec<Rc<dyn IXamlMethod>>> {
        let sp = context.configuration().type_mappings.service_provider()?;
        Ok(type_.find_methods(|m| {
            (m.name() == "ProvideValue" || m.name() == "ProvideTypedValue")
                && m.is_public()
                && !m.is_static()
                && {
                    let parameters = m.parameters();
                    parameters.is_empty() || (parameters.len() == 1 && parameters[0].equals(&*sp))
                }
        }))
    }

    /// Returns the markup extension node wrapping `node` when its type provides a suitable
    /// `ProvideValue`/`ProvideTypedValue` method.
    pub fn try_convert_markup_extension(
        context: &AstTransformationContext,
        node: &Rc<dyn IXamlAstValueNode>,
    ) -> XamlResult<Option<Rc<XamlMarkupExtensionNode>>> {
        let cache = context.get_or_create_item::<MarkupExtensionProvideValueCache>();
        let node_type = node.type_().get_clr_type()?;
        let key = XamlTypeKey(node_type.clone());

        let cached = cache.type_to_provide_value.borrow().get(&key).cloned();
        let provide_value = match cached {
            Some(provide_value) => provide_value,
            None => {
                let candidates =
                    Self::get_markup_extension_provide_value_alternatives(context, &*node_type)?;
                let so = context.configuration().well_known_types().object.clone();
                let sp = context.configuration().type_mappings.service_provider()?;

                // Try non-object variant first and variants without IServiceProvider argument first
                let provide_value = candidates
                    .iter()
                    .find(|m| m.parameters().is_empty() && !m.return_type().equals(&*so))
                    .or_else(|| candidates.iter().find(|m| m.parameters().is_empty()))
                    .or_else(|| {
                        candidates.iter().find(|m| {
                            let p = m.parameters();
                            p.len() == 1 && p[0].equals(&*sp) && !m.return_type().equals(&*so)
                        })
                    })
                    .or_else(|| {
                        candidates.iter().find(|m| {
                            let p = m.parameters();
                            p.len() == 1 && p[0].equals(&*sp)
                        })
                    })
                    .cloned();
                cache
                    .type_to_provide_value
                    .borrow_mut()
                    .insert(key, provide_value.clone());
                provide_value
            }
        };

        let Some(provide_value) = provide_value else {
            if node.type_().is_markup_extension() {
                context.report_transform_error(
                    &format!(
                        "{} was resolved as markup extension, but doesn't have a matching ProvideValue/ProvideTypedValue method",
                        node_type.get_fqn()
                    ),
                    Some(&**node),
                    (),
                )?;
            }
            return Ok(None);
        };

        Ok(Some(XamlMarkupExtensionNode::new(
            &**node,
            provide_value,
            node.clone(),
        )))
    }

    /// `TryGetCorrectlyTypedValue(context, node, IXamlType xamlType, out rv)`.
    pub fn try_get_correctly_typed_value(
        context: &AstTransformationContext,
        node: &Rc<dyn IXamlAstValueNode>,
        xaml_type: &Rc<dyn IXamlType>,
    ) -> XamlResult<Option<Rc<dyn IXamlAstValueNode>>> {
        Self::try_get_correctly_typed_value_with_attributes(context, node, None, xaml_type)
    }

    /// `TryGetCorrectlyTypedValue(context, node, IXamlProperty property, out rv)`.
    pub fn try_get_correctly_typed_value_for_property(
        context: &AstTransformationContext,
        node: &Rc<dyn IXamlAstValueNode>,
        property: &dyn IXamlProperty,
    ) -> XamlResult<Option<Rc<dyn IXamlAstValueNode>>> {
        Self::try_get_correctly_typed_value_with_attributes(
            context,
            node,
            Some(&property.custom_attributes()),
            &property.property_type(),
        )
    }

    /// `TryGetCorrectlyTypedValue(context, node, IXamlParameterInfo parameterInfo, out rv)`.
    pub fn try_get_correctly_typed_value_for_parameter(
        context: &AstTransformationContext,
        node: &Rc<dyn IXamlAstValueNode>,
        parameter_info: &dyn IXamlParameterInfo,
    ) -> XamlResult<Option<Rc<dyn IXamlAstValueNode>>> {
        Self::try_get_correctly_typed_value_with_attributes(
            context,
            node,
            Some(&parameter_info.custom_attributes()),
            &parameter_info.parameter_type(),
        )
    }

    /// `TryGetCorrectlyTypedValue(context, node, customAttributes, IXamlType type, out rv)`.
    pub fn try_get_correctly_typed_value_with_attributes(
        context: &AstTransformationContext,
        node: &Rc<dyn IXamlAstValueNode>,
        custom_attributes: Option<&[Rc<dyn IXamlCustomAttribute>]>,
        type_: &Rc<dyn IXamlType>,
    ) -> XamlResult<Option<Rc<dyn IXamlAstValueNode>>> {
        if type_.is_assignable_from(&*node.type_().get_clr_type()?) {
            return Ok(Some(node.clone()));
        }

        Self::try_convert_value(context, node, custom_attributes, type_, None)
    }

    pub fn try_get_type_converter_from_custom_attribute(
        cfg: &TransformerConfiguration,
        attribute: Option<&Rc<dyn IXamlCustomAttribute>>,
    ) -> Option<Rc<dyn IXamlType>> {
        let attribute = attribute?;
        match attribute.parameters().into_iter().next() {
            Some(XamlValue::Type(t)) => Some(t),
            Some(XamlValue::String(sarg)) => cfg.type_system.find_type(&sarg),
            _ => None,
        }
    }

    pub fn get_common_base_class(types: &[Rc<dyn IXamlType>]) -> XamlResult<Rc<dyn IXamlType>> {
        let Some(first) = types.first() else {
            return Err(XamlError::argument("Input types array must not be empty"));
        };

        let mut ret = first.clone();

        for t in &types[1..] {
            if t.is_assignable_from(&*ret) {
                ret = t.clone();
            } else {
                // This will always terminate when ret == typeof(object)
                while !ret.is_assignable_from(&**t) {
                    ret = ret.base_type().ok_or_else(|| {
                        XamlError::internal(
                            "NullReferenceException",
                            "Unable to find a common base class",
                        )
                    })?;
                }
            }
        }

        Ok(ret)
    }

    fn create_invariant_culture(
        cfg: &TransformerConfiguration,
        line_info: &dyn IXamlLineInfo,
    ) -> XamlResult<Rc<dyn IXamlAstValueNode>> {
        let method = cfg
            .well_known_types()
            .culture_info
            .methods()
            .into_iter()
            .find(|x| x.is_public() && x.is_static() && x.name() == "get_InvariantCulture")
            .ok_or_else(|| XamlError::invalid_operation("Sequence contains no matching element"))?;
        Ok(XamlStaticOrTargetedReturnMethodCallNode::new(
            line_info, method, None,
        ))
    }

    pub fn try_convert_value(
        context: &AstTransformationContext,
        node: &Rc<dyn IXamlAstValueNode>,
        custom_attributes: Option<&[Rc<dyn IXamlCustomAttribute>]>,
        type_: &Rc<dyn IXamlType>,
        property_context: Option<&Rc<XamlAstClrProperty>>,
    ) -> XamlResult<Option<Rc<dyn IXamlAstValueNode>>> {
        let cfg = context.configuration().clone();
        let known = cfg.well_known_types();
        let mut type_ = type_.clone();
        // Since we are doing a conversion anyway, it makes sense to check for the underlying nullable type
        if type_
            .generic_type_definition()
            .is_some_and(|d| d.equals(&*known.nullable_t))
        {
            type_ = first_generic_argument(&type_)?;
        }

        let node_type = node.type_().get_clr_type()?;

        // Try with property-defined converter first
        if let Some(property_context) = property_context {
            let property_converter_type = property_context
                .type_converters
                .borrow()
                .get(&XamlTypeKey(type_.clone()))
                .cloned();
            if let Some(property_converter_type) = property_converter_type {
                return Ok(Some(Self::convert_with_converter(
                    node,
                    &property_converter_type,
                    &cfg,
                    &type_,
                )?));
            }
        }

        // Ask the hosting platform to apply its custom conversions
        if let Some(converter) = &cfg.custom_value_converter {
            let property_attributes;
            let attrs: Option<&[Rc<dyn IXamlCustomAttribute>]> = match custom_attributes {
                Some(a) if !a.is_empty() => Some(a),
                _ => match property_context {
                    Some(p) => {
                        property_attributes = p.custom_attributes();
                        Some(&property_attributes)
                    }
                    None => None,
                },
            };
            if let Some(rv) = converter(context, node, attrs, &type_)? {
                return Ok(Some(rv));
            }
        }

        // Implicit type converters
        if !node_type.equals(&*known.string) {
            return Ok(None);
        }

        if let Some(tn) = node.cast::<XamlAstTextNode>() {
            let text = tn.text();
            if type_.is_enum() {
                if let Some(enum_constant_node) =
                    TypeSystemHelpers::try_get_enum_value_node(&type_, &text, &*tn, false)?
                {
                    return Ok(Some(enum_constant_node));
                }
            }

            // Well known types
            if let Some(constant_node) =
                TypeSystemHelpers::parse_constant_if_type_allows(&text, &type_, &*tn)?
            {
                return Ok(Some(constant_node));
            }

            if type_.is("System", "Type") {
                let resolved_type = TypeReferenceResolver::resolve_type_by_xml_name(
                    context, &text, false, &*tn, true,
                )?;
                return Ok(Some(XamlTypeExtensionNode::new(
                    &*tn,
                    resolved_type,
                    type_.clone(),
                )));
            }

            if known.delegate.is_assignable_from(&*type_) {
                let invoke = type_.get_method(|m| m.name() == "Invoke")?;
                let root_object = context.root_object()?;
                let root_type = root_object.type_().get_clr_type()?;
                let handler = root_type.find_method_by_name(
                    &text,
                    &*invoke.return_type(),
                    false,
                    &invoke.parameters(),
                );
                if let Some(handler) = handler {
                    return Ok(Some(XamlLoadMethodDelegateNode::new(
                        &*tn,
                        root_object,
                        type_.clone(),
                        handler,
                    )));
                }
            }
        }

        let candidates: Vec<Rc<dyn IXamlMethod>> = type_
            .methods()
            .into_iter()
            .filter(|m| {
                m.name() == "Parse" && m.return_type().equals(&*type_) && {
                    let p = m.parameters();
                    !p.is_empty() && p[0].equals(&*known.string)
                }
            })
            .collect();

        // Types with parse method
        let parser = candidates
            .iter()
            .find(|m| {
                let p = m.parameters();
                p.len() == 2
                    && (p[1].equals(&*known.culture_info) || p[1].equals(&*known.i_format_provider))
            })
            .or_else(|| candidates.iter().find(|m| m.parameters().len() == 1))
            .cloned();

        if let Some(parser) = parser {
            let mut args: Vec<Rc<dyn IXamlAstValueNode>> = vec![node.clone()];
            if parser.parameters().len() == 2 {
                args.push(Self::create_invariant_culture(&cfg, &**node)?);
            }

            return Ok(Some(XamlStaticOrTargetedReturnMethodCallNode::new(
                &**node,
                parser,
                Some(args),
            )));
        }

        if cfg.type_mappings.type_descriptor_context.is_some() {
            let type_converter_attribute = cfg
                .get_custom_attributes_for_type(
                    &*type_,
                    &cfg.type_mappings.type_converter_attributes,
                )
                .into_iter()
                .next();
            if let Some(type_converter_attribute) = type_converter_attribute {
                let converter_type = Self::try_get_type_converter_from_custom_attribute(
                    &cfg,
                    Some(&type_converter_attribute),
                );
                if let Some(converter_type) = converter_type {
                    return Ok(Some(Self::convert_with_converter(
                        node,
                        &converter_type,
                        &cfg,
                        &type_,
                    )?));
                }
            }
        }

        Ok(None)
    }

    fn convert_with_converter(
        node: &Rc<dyn IXamlAstValueNode>,
        converter_type: &Rc<dyn IXamlType>,
        cfg: &TransformerConfiguration,
        type_: &Rc<dyn IXamlType>,
    ) -> XamlResult<Rc<dyn IXamlAstValueNode>> {
        let known = cfg.well_known_types();
        let type_descriptor_context = cfg
            .type_mappings
            .type_descriptor_context
            .clone()
            .ok_or_else(|| {
                XamlError::internal(
                    "NullReferenceException",
                    "XamlLanguageTypeMappings.TypeDescriptorContext is not set",
                )
            })?;
        let li: &dyn IXamlLineInfo = &**node;
        let converter_method = converter_type.get_method_by_name(
            "ConvertFrom",
            &*known.object,
            false,
            &[
                type_descriptor_context.clone(),
                known.culture_info.clone(),
                known.object.clone(),
            ],
        )?;
        let call_arguments: Vec<Rc<dyn IXamlAstValueNode>> = vec![
            XamlAstNewClrObjectNode::new(
                li,
                XamlAstClrTypeReference::new(li, converter_type.clone(), false),
                converter_type.get_constructor(None)?,
                Vec::new(),
            ),
            XamlAstContextLocalNode::new(li, type_descriptor_context),
            Self::create_invariant_culture(cfg, li)?,
            node.clone(),
        ];
        Ok(XamlAstNeedsParentStackValueNode::new(
            li,
            XamlAstRuntimeCastNode::new(
                li,
                XamlStaticOrTargetedReturnMethodCallNode::new(
                    li,
                    converter_method,
                    Some(call_arguments),
                ),
                XamlAstClrTypeReference::new(li, type_.clone(), false),
            ),
        ))
    }
}

fn first_generic_argument(type_: &Rc<dyn IXamlType>) -> XamlResult<Rc<dyn IXamlType>> {
    type_.generic_arguments().into_iter().next().ok_or_else(|| {
        XamlError::internal(
            "ArgumentOutOfRangeException",
            format!("{} doesn't have generic arguments", type_.get_fqn()),
        )
    })
}