///|
/// A value that a converter, setter or markup extension of the built-in types cannot accept.
#derive(Eq, Debug)
pub(all) suberror ValueError {
  ValueError(String)
}

///|
fn xaml_type(name : String) -> @nodes.XamlTypeName {
  @nodes.XamlTypeName::new(@nodes.xaml_language_namespace, name)
}

///|
/// `x:Object`, the type of every value. Its converter passes values through.
pub let object_type : @nodes.XamlTypeName = xaml_type("Object")

///|
/// `x:String`, whose values are `String`.
pub let string_type : @nodes.XamlTypeName = xaml_type("String")

///|
/// `x:Int32`, whose values are `Int`.
pub let int32_type : @nodes.XamlTypeName = xaml_type("Int32")

///|
/// `x:Int64`, whose values are `Int64`.
pub let int64_type : @nodes.XamlTypeName = xaml_type("Int64")

///|
/// `x:Double`, whose values are `Double`.
pub let double_type : @nodes.XamlTypeName = xaml_type("Double")

///|
/// `x:Boolean`, whose values are `Bool`.
pub let boolean_type : @nodes.XamlTypeName = xaml_type("Boolean")

///|
/// `urn:moonrockz:xaml`, the namespace of the built-in types that are not XAML language types:
/// `TypeValue`, `Array`, `ObjectList` and `NameScope` (design D8, D12, D15).
pub let moonrockz_namespace : String = "urn:moonrockz:xaml"

///|
fn moonrockz_type(name : String) -> @nodes.XamlTypeName {
  @nodes.XamlTypeName::new(moonrockz_namespace, name)
}

///|
/// `TypeValue` in `urn:moonrockz:xaml`, the type of resolved type values, which `type_value`
/// creates. MoonBit has no `System.Type`, so this type stands for it, outside the XAML language
/// namespaces (design D8, deliberate difference 12).
pub let type_value_type : @nodes.XamlTypeName = moonrockz_type("TypeValue")

///|
/// `Array` in `urn:moonrockz:xaml`, the type of the `XamlArray` values that `x:Array` gives.
pub let array_value_type : @nodes.XamlTypeName = moonrockz_type("Array")

///|
/// `ObjectList` in `urn:moonrockz:xaml`, a collection of `x:Object` items: the `Items` of
/// `x:Array`, as System.Xaml's `ArrayExtension.Items` is an `IList`.
pub let object_list_type : @nodes.XamlTypeName = moonrockz_type("ObjectList")

///|
/// The markup extension `{x:Null}`, also named `x:Null`.
pub let null_extension_type : @nodes.XamlTypeName = xaml_type("NullExtension")

///|
/// The markup extension `{x:Type TypeName}`, also named `x:Type`.
pub let type_extension_type : @nodes.XamlTypeName = xaml_type("TypeExtension")

///|
/// The markup extension `{x:Static prefix:Type.Member}`, also named `x:Static`.
pub let static_extension_type : @nodes.XamlTypeName = xaml_type(
  "StaticExtension",
)

///|
/// The markup extension `x:Array`, also named `x:ArrayExtension`.
pub let array_extension_type : @nodes.XamlTypeName = xaml_type("ArrayExtension")

///|
/// The markup extension `{x:Reference name}`, also named `x:ReferenceExtension`. Its identity is
/// `x:Reference`, because System.Xaml's type is `System.Windows.Markup.Reference`.
pub let reference_type : @nodes.XamlTypeName = xaml_type("Reference")

///|
/// `x:Member`, the abstract type of the members that `x:Members` defines.
pub let member_definition_type : @nodes.XamlTypeName = xaml_type("Member")

///|
/// `x:Property`, a property that `x:Members` defines; it derives from `x:Member`.
pub let property_definition_type : @nodes.XamlTypeName = xaml_type("Property")

///|
/// The directive names of the XAML language namespace. `_Items`, `_PositionalParameters` and
/// `_Initialization` carry collection items, positional markup extension parameters and the
/// initialization text of an object, as in System.Xaml.
pub let directive_key : String = "Key"

///|
pub let directive_name : String = "Name"

///|
pub let directive_uid : String = "Uid"

///|
pub let directive_items : String = "_Items"

///|
pub let directive_positional_parameters : String = "_PositionalParameters"

///|
pub let directive_initialization : String = "_Initialization"

///|
/// Content that no member of its object takes, in the order of the document.
pub let directive_unknown_content : String = "_UnknownContent"

///|
pub let directive_arguments : String = "Arguments"

///|
let type_key : @nodes.ObjectKey[@nodes.XamlTypeName] = @nodes.ObjectKey::new(
  equal=(a, b) => a == b,
  hash=name => Hash::hash(name),
)

///|
/// A type value: a handle of type `TypeValue` that holds a type name, as `{x:Type Name}` gives.
pub fn type_value(name : @nodes.XamlTypeName) -> @nodes.XamlValue {
  Object(type_key.wrap(type_value_type, name))
}

///|
/// The type name in a value that `type_value` created.
pub fn as_type_name(value : @nodes.XamlValue) -> @nodes.XamlTypeName? {
  match value {
    Object(handle) => type_key.unwrap(handle)
    _ => None
  }
}

///|
let array_key : @nodes.ObjectKey[XamlArray] = @nodes.ObjectKey::new(
  equal=(a, b) => a == b,
  hash=array => Hash::hash(array),
)

///|
/// The value of an `x:Array`: a handle of type `Array` in `urn:moonrockz:xaml`.
pub fn array_value(array : XamlArray) -> @nodes.XamlValue {
  Object(array_key.wrap(array_value_type, array))
}

///|
/// The array in a value that `array_value` created.
pub fn as_array(value : @nodes.XamlValue) -> XamlArray? {
  match value {
    Object(handle) => array_key.unwrap(handle)
    _ => None
  }
}

///|
/// The members of an `x:TypeExtension`: a supplied type value, or the text of a type name. Setting one
/// clears the other.
priv struct TypeExtensionState {
  mut type_name : String?
  mut type_value : @nodes.XamlTypeName?
}

///|
let type_extension_key : @nodes.ObjectKey[TypeExtensionState] = @nodes.ObjectKey::new()

///|
let null_extension_key : @nodes.ObjectKey[Unit] = @nodes.ObjectKey::new()

///|
/// The members of an `x:StaticExtension`.
priv struct StaticExtensionState {
  mut member_text : String?
  mut member_type : @nodes.XamlTypeName?
}

///|
let static_extension_key : @nodes.ObjectKey[StaticExtensionState] = @nodes.ObjectKey::new()

///|
/// The members of an `x:ArrayExtension`: the item type and the items, which `Items` gives as an
/// `ObjectList`.
priv struct ArrayExtensionState {
  mut item_type : @nodes.XamlTypeName?
  items : Array[@nodes.XamlValue]
}

///|
let array_extension_key : @nodes.ObjectKey[ArrayExtensionState] = @nodes.ObjectKey::new()

///|
let object_list_key : @nodes.ObjectKey[Array[@nodes.XamlValue]] = @nodes.ObjectKey::new()

///|
/// The member of an `x:Reference`.
priv struct ReferenceState {
  mut name : String?
}

///|
let reference_key : @nodes.ObjectKey[ReferenceState] = @nodes.ObjectKey::new()

///|
fn new_type_extension(
  type_name : String?,
  type_value : @nodes.XamlTypeName?,
) -> @nodes.XamlValue {
  Object(
    type_extension_key.wrap(type_extension_type, { type_name, type_value, }),
  )
}

///|
/// The state behind a built-in handle, or a `ValueError` that names its type.
fn[T] state_of(
  key : @nodes.ObjectKey[T],
  target : @nodes.XamlObject,
  what : String,
) -> T raise ValueError {
  guard key.unwrap(target) is Some(state) else {
    raise ValueError("not an \{what} object")
  }
  state
}

///|
/// The text of a string member value, or a `ValueError` that names the member.
fn text_of(value : @nodes.XamlValue, what : String) -> String raise ValueError {
  guard value is String(text) else {
    raise ValueError("\{what} must be a string")
  }
  text
}

///|
/// The value of a string member, null when it is not set.
fn text_or_null(text : String?) -> @nodes.XamlValue {
  text.map(text => @nodes.XamlValue::String(text)).unwrap_or(Null)
}

///|
/// The value of a type value member, null when it is not set.
fn type_or_null(name : @nodes.XamlTypeName?) -> @nodes.XamlValue {
  name.map(type_value).unwrap_or(Null)
}

///|
/// The instance descriptor of a markup extension with one positional parameter. An extension without `argument` is
/// constructed without it.
fn positional_descriptor(
  argument : @nodes.XamlValue,
  complete~ : Bool,
) -> InstanceDescriptor {
  let arguments : ReadOnlyArray[@nodes.XamlValue] = if argument is Null {
    []
  } else {
    [argument]
  }
  { factory_method: None, arguments, complete, }
}

///|
/// The type name in a type value member, or a `ValueError` that names the member.
fn type_name_of(
  value : @nodes.XamlValue,
  what : String,
) -> @nodes.XamlTypeName raise ValueError {
  guard as_type_name(value) is Some(name) else {
    raise ValueError("\{what} must be a type value")
  }
  name
}

///|
/// `x:Static`: the value of `Member` (`prefix:Type.Member`, or an unqualified member of
/// `MemberType`), fields before properties through the base chain, enumeration values included, as
/// System.Xaml `StaticExtension.ProvideValue` (design D13).
fn provide_static(
  target : @nodes.XamlObject,
  context : ServiceContext,
) -> @nodes.XamlValue raise {
  let state = state_of(static_extension_key, target, "x:Static")
  guard state.member_text is Some(text) && !text.is_empty() else {
    raise ValueError("x:Static needs Member")
  }
  let (owner, member_name, shown) = match state.member_type {
    Some(owner) => (owner, text, "\{owner}.\{text}")
    None => {
      guard text.find(".") is Some(dot) && dot > 0 && dot < text.length() - 1 else {
        raise ValueError(
          "x:Static needs Member as Type.Member, or MemberType: \{text}",
        )
      }
      let type_text = text[:dot].to_owned()
      guard context.resolve_type is Some(resolve_type) else {
        raise ValueError(
          "x:Static needs a type resolver to resolve \{type_text}",
        )
      }
      guard resolve_type(type_text) is Some(owner) else {
        raise ValueError("x:Static cannot resolve the type \{type_text}")
      }
      (owner, text[dot + 1:].to_owned(), text)
    }
  }
  guard context.schema_context.get_type(owner) is Some(t) else {
    raise ValueError("x:Static cannot resolve the type \{owner}")
  }
  guard t.static_member(member_name) is Some(found) else {
    raise ValueError("x:Static finds no static member \{shown}")
  }
  found.value()
}

///|
/// `x:Array`: an `XamlArray` with the item type in `Type` and the items in document order, each an
/// instance of the item type or null when the type takes null (design D13, F7).
fn provide_array(
  target : @nodes.XamlObject,
  context : ServiceContext,
) -> @nodes.XamlValue raise {
  let state = state_of(array_extension_key, target, "x:Array")
  guard state.item_type is Some(item_type) else {
    raise ValueError("x:Array needs Type")
  }
  for item in state.items {
    if !context.schema_context.accepts(item_type, item) {
      let shown = match (item, value_type_name(item)) {
        (Null, _) => "null"
        (_, Some(t)) => "an item of type \{t}"
        (_, None) => "a list"
      }
      raise ValueError("x:Array of \{item_type} cannot hold \{shown}")
    }
  }
  array_value(XamlArray::new(item_type, state.items[:]))
}

///|
/// `x:Reference`: the named object when the name resolver gives it, else a fix-up token for the name that
/// may be assigned directly, as System.Windows.Markup `Reference` (design D15).
fn provide_reference(
  target : @nodes.XamlObject,
  context : ServiceContext,
) -> @nodes.XamlValue raise {
  let state = state_of(reference_key, target, "x:Reference")
  guard state.name is Some(name) && !name.is_empty() else {
    raise ValueError("x:Reference needs a nonempty Name")
  }
  guard context.name_resolver is Some(resolver) else {
    raise ValueError("x:Reference needs a name resolver to resolve \{name}")
  }
  if resolver.resolve(name) is Some(found) {
    return found.value
  }
  guard resolver.fixup_token([name], can_assign_directly=true) is Some(token) else {
    raise ValueError("x:Reference cannot resolve the name \{name}")
  }
  Object(token)
}

///|
/// The built-in markup extensions, with System.Xaml's members, content properties, constructors and
/// constructor-argument mappings. Each answers to its short and its suffixed name (F15).
fn builtin_extensions() -> Array[XamlTypeDefinition] {
  let ns = @nodes.xaml_language_namespace
  let null_extension = XamlTypeDefinition::new(ns, "NullExtension")
    .object_key(null_extension_key)
    .add_constructor([], (_, _) => {
      Object(null_extension_key.wrap(null_extension_type, ()))
    })
    .markup_extension((_, _) => Null)
  let type_extension = XamlTypeDefinition::new(ns, "TypeExtension")
    .object_key(type_extension_key)
    .add_member(
      "TypeName",
      value_type=string_type,
      default_value=Null,
      getter=target => {
        text_or_null(state_of(type_extension_key, target, "x:Type").type_name)
      },
      setter=(target, value) => {
        let state = state_of(type_extension_key, target, "x:Type")
        state.type_name = Some(text_of(value, "x:Type TypeName"))
        state.type_value = None
      },
    )
    .add_member(
      "Type",
      value_type=type_value_type,
      constructor_argument="type",
      default_value=Null,
      getter=target => {
        type_or_null(state_of(type_extension_key, target, "x:Type").type_value)
      },
      setter=(target, value) => {
        let state = state_of(type_extension_key, target, "x:Type")
        state.type_value = Some(type_name_of(value, "x:Type Type"))
        state.type_name = None
      },
    )
    .add_constructor([], (_, _) => new_type_extension(None, None))
    // As System.Xaml hard-codes `TypeExtension` to its `Type` constructor, the one-argument
    // positional signature takes a type value, and the string constructor is not positional.
    .add_non_positional_constructor(
      [XamlParameter::new("typeName", string_type)],
      (_, arguments) => {
        new_type_extension(Some(text_of(arguments[0], "x:Type typeName")), None)
      },
    )
    .add_constructor([XamlParameter::new("type", type_value_type)], (
      _,
      arguments,
    ) => {
      new_type_extension(None, Some(type_name_of(arguments[0], "x:Type type")))
    })
    .instance_descriptor(target => {
      let state = state_of(type_extension_key, target, "x:Type")
      positional_descriptor(type_or_null(state.type_value), complete=true)
    })
    .markup_extension((target, context) => {
      let state = state_of(type_extension_key, target, "x:Type")
      if state.type_value is Some(name) {
        return type_value(name)
      }
      guard state.type_name is Some(text) else {
        raise ValueError("x:Type needs Type or TypeName")
      }
      guard context.resolve_type is Some(resolve_type) else {
        raise ValueError("x:Type needs a type resolver to resolve \{text}")
      }
      guard resolve_type(text) is Some(name) else {
        raise ValueError("x:Type cannot resolve the type \{text}")
      }
      type_value(name)
    })
  let new_static = (member_text : String?) => {
    @nodes.XamlValue::Object(
      static_extension_key.wrap(static_extension_type, {
        member_text,
        member_type: None,
      }),
    )
  }
  let static_extension = XamlTypeDefinition::new(ns, "StaticExtension")
    .object_key(static_extension_key)
    .add_member(
      "Member",
      value_type=string_type,
      constructor_argument="member",
      default_value=Null,
      getter=target => {
        text_or_null(
          state_of(static_extension_key, target, "x:Static").member_text,
        )
      },
      setter=(target, value) => {
        state_of(static_extension_key, target, "x:Static").member_text = Some(
          text_of(value, "x:Static Member"),
        )
      },
    )
    .add_member(
      "MemberType",
      value_type=type_value_type,
      default_value=Null,
      getter=target => {
        type_or_null(
          state_of(static_extension_key, target, "x:Static").member_type,
        )
      },
      setter=(target, value) => {
        state_of(static_extension_key, target, "x:Static").member_type = Some(
          type_name_of(value, "x:Static MemberType"),
        )
      },
    )
    .add_constructor([], (_, _) => new_static(None))
    .add_constructor([XamlParameter::new("member", string_type)], (_, arguments) => {
      new_static(Some(text_of(arguments[0], "x:Static member")))
    })
    .instance_descriptor(target => {
      let state = state_of(static_extension_key, target, "x:Static")
      positional_descriptor(
        text_or_null(state.member_text),
        complete=state.member_type is None,
      )
    })
    .markup_extension(provide_static)
  let new_array = (item_type : @nodes.XamlTypeName?) => {
    @nodes.XamlValue::Object(
      array_extension_key.wrap(array_extension_type, { item_type, items: [], }),
    )
  }
  let array_extension = XamlTypeDefinition::new(
      ns,
      "ArrayExtension",
      content_property="Items",
    )
    .object_key(array_extension_key)
    .add_member(
      "Type",
      value_type=type_value_type,
      // System.Xaml names the argument `type` and the parameter `arrayType`.
      constructor_argument="type",
      setter=(target, value) => {
        state_of(array_extension_key, target, "x:Array").item_type = Some(
          type_name_of(value, "x:Array Type"),
        )
      },
      getter=target => {
        type_or_null(state_of(array_extension_key, target, "x:Array").item_type)
      },
    )
    .add_member("Items", value_type=object_list_type, getter=target => {
      Object(
        object_list_key.wrap(
          object_list_type,
          state_of(array_extension_key, target, "x:Array").items,
        ),
      )
    })
    .add_constructor([], (_, _) => new_array(None))
    .add_constructor([XamlParameter::new("arrayType", type_value_type)], (
      _,
      arguments,
    ) => new_array(Some(type_name_of(arguments[0], "x:Array arrayType"))))
    .markup_extension(provide_array)
  let new_reference = (name : String?) => {
    @nodes.XamlValue::Object(reference_key.wrap(reference_type, { name, }))
  }
  let reference = XamlTypeDefinition::new(
      ns,
      "Reference",
      content_property="Name",
    )
    .object_key(reference_key)
    .add_member(
      "Name",
      value_type=string_type,
      constructor_argument="name",
      default_value=Null,
      getter=target => {
        text_or_null(state_of(reference_key, target, "x:Reference").name)
      },
      setter=(target, value) => {
        state_of(reference_key, target, "x:Reference").name = Some(
          text_of(value, "x:Reference Name"),
        )
      },
    )
    .add_constructor([], (_, _) => new_reference(None))
    .add_constructor([XamlParameter::new("name", string_type)], (_, arguments) => {
      new_reference(Some(text_of(arguments[0], "x:Reference name")))
    })
    .markup_extension(provide_reference)
  null_extension.short_name = Some("Null")
  type_extension.short_name = Some("Type")
  static_extension.short_name = Some("Static")
  array_extension.short_name = Some("Array")
  reference.short_name = Some("ReferenceExtension")
  [null_extension, type_extension, static_extension, array_extension, reference]
}

///|
/// The members of an `x:Property`.
priv struct PropertyDefinitionState {
  mut name : @nodes.XamlValue
  mut type_value : @nodes.XamlValue
  mut modifier : @nodes.XamlValue
  attributes : Array[@nodes.XamlValue]
}

///|
let property_definition_key : @nodes.ObjectKey[PropertyDefinitionState] = @nodes.ObjectKey::new()

///|
/// The text of `Type` names a type that need not be registered (System.Xaml `XamlTypeTypeConverter`).
let property_type_converter : TypeConverter = {
  ..type_value_converter,
  from: (context, value) => {
    guard value is String(text) && context is Some(context) else {
      return type_value_converter.convert_from(context?, value)
    }
    if context.resolve_type is Some(resolve) && resolve(text) is Some(name) {
      return type_value(name)
    }
    type_value(
      @nodes.XamlTypeName::parse(text, prefix => {
        context.resolve_prefix.bind(resolve => resolve(prefix))
      }),
    )
  },
}

///|
/// `x:Property` with the members of System.Windows.Markup `PropertyDefinition`.
fn property_definition() -> XamlTypeDefinition {
  let of = target => state_of(property_definition_key, target, "x:Property")
  XamlTypeDefinition::new(
    @nodes.xaml_language_namespace,
    "Property",
    base_type=member_definition_type,
  )
  .object_key(property_definition_key)
  .add_member(
    "Name",
    value_type=string_type,
    getter=target => of(target).name,
    setter=(target, value) => of(target).name = value,
  )
  .add_member(
    "Type",
    value_type=type_value_type,
    type_converter=property_type_converter,
    getter=target => of(target).type_value,
    setter=(target, value) => of(target).type_value = value,
  )
  .add_member(
    "Modifier",
    value_type=string_type,
    default_value=Null,
    getter=target => of(target).modifier,
    setter=(target, value) => of(target).modifier = value,
  )
  .add_member("Attributes", value_type=object_list_type, getter=target => {
    Object(object_list_key.wrap(object_list_type, of(target).attributes))
  })
  .add_constructor([], (_, _) => {
    Object(
      property_definition_key.wrap(property_definition_type, {
        name: Null,
        type_value: Null,
        modifier: Null,
        attributes: [],
      }),
    )
  })
}

///|
fn object_list_of(target : @nodes.XamlObject) -> Array[@nodes.XamlValue] raise {
  state_of(object_list_key, target, "ObjectList")
}

///|
/// The key of the objects that `x:Object` constructs, which have an identity and nothing else.
let plain_object_key : @nodes.ObjectKey[Ref[Unit]] = @nodes.ObjectKey::new()

///|
fn builtin_types(
  dependency_types : DependencyTypes,
) -> Array[XamlTypeDefinition] {
  let ns = @nodes.xaml_language_namespace
  // The primitive value types construct their zero value, as .NET value types have a
  // zero-parameter constructor, so `` needs no initialization text (2F22).
  let value_type = (name : String, zero : () -> @nodes.XamlValue raise) => {
    XamlTypeDefinition::new(ns, name).add_constructor([], (_, _) => zero())
  }
  let types = [
    // `x:Object` registers no object key, because every type would inherit it.
    XamlTypeDefinition::new(ns, "Object").add_constructor([], (_, _) => {
      Object(plain_object_key.wrap(object_type, Ref(())))
    }),
    XamlTypeDefinition::new(ns, "String"),
    value_type("Int32", () => Int(0)),
    value_type("Int64", () => Int64(0)),
    value_type("Double", () => Double(0.0)),
    value_type("Boolean", () => Bool(false)),
    value_type("Byte", () => Byte(0)),
    value_type("Int16", () => Int16(0)),
    value_type("Single", () => Single(0.0)),
    value_type("Char", () => Char(0)),
    value_type("Decimal", () => decimal_value(XamlDecimal::new(0N, 0))),
    value_type("TimeSpan", () => time_span_value(XamlTimeSpan::new(0))),
    XamlTypeDefinition::new(ns, "Uri"),
    XamlTypeDefinition::new(ns, "Member")
    .abstract_type()
    .add_member("Name", value_type=string_type),
    property_definition(),
    XamlTypeDefinition::new(moonrockz_namespace, "TypeValue"),
    XamlTypeDefinition::new(moonrockz_namespace, "Array"),
    XamlTypeDefinition::new(moonrockz_namespace, "ObjectList")
    .object_key(object_list_key)
    .add_constructor([], (_, _) => {
      Object(object_list_key.wrap(object_list_type, []))
    })
    .collection(
      (target, item) => object_list_of(target).push(item),
      item_type=object_type,
    )
    .item_enumerator(target => ReadOnlyArray::from_array(object_list_of(target))),
    name_scope_definition(),
    element_collection_definition(dependency_types),
  ]
  types.append(builtin_extensions())
  // The primitives' built-in converters, and the built-in value serializer of `x:String`.
  let converters = primitive_converters()
  let any_values = primitive_any_values()
  types.map(definition => {
    guard converters.get(definition.name.name) is Some(converter) else {
      return definition
    }
    let typed = {
      ..definition,
      type_converter: Some(_ => converter),
      any_value_converter: any_values.get(definition.name.name),
    }
    if definition.name == string_type {
      typed.exact_serializer = Some(string_serializer)
    }
    typed
  })
}

///|
/// Allowed as an attribute only.
let attribute_only : AllowedLocations = { attribute: true, element: false, }

///|
/// Allowed as a property element only.
let element_only : AllowedLocations = { attribute: false, element: true, }

///|
/// The directives of System.Xaml `XamlLanguage.AllDirectives`, with their value types, collection
/// item types and allowed locations as `XamlLanguage` declares them, except `x:Code`, which is
/// allowed in both forms (Owner, 2026-12-01), and `x:DataType` and `x:CompileBindings`, which
/// `XamlLanguage` lacks (design D8).
fn builtin_directives() -> Array[XamlMember] {
  let ns = @nodes.xaml_language_namespace
  let text = (namespace_uri : String, name : String) => {
    XamlMember::directive(
      namespace_uri,
      name,
      value_type=string_type,
      allowed_locations=attribute_only,
    )
  }
  [
    XamlMember::directive(ns, directive_arguments, item_type=object_type),
    XamlMember::directive(
      ns,
      "AsyncRecords",
      value_type=int32_type,
      allowed_locations=attribute_only,
    ),
    text(ns, "Class"),
    XamlMember::directive(
      ns,
      "ClassAttributes",
      item_type=object_type,
      allowed_locations=element_only,
    ),
    text(ns, "ClassModifier"),
    XamlMember::directive(ns, "Code", value_type=string_type),
    XamlMember::directive(ns, "ConnectionId", value_type=int32_type),
    XamlMember::directive(ns, "FactoryMethod", value_type=string_type),
    text(ns, "FieldModifier"),
    XamlMember::directive(ns, directive_initialization, value_type=object_type),
    XamlMember::directive(ns, directive_items, item_type=object_type),
    XamlMember::directive(ns, directive_key, value_type=object_type),
    XamlMember::directive(
      ns,
      "Members",
      item_type=member_definition_type,
      allowed_locations=element_only,
    ),
    text(ns, directive_name),
    XamlMember::directive(
      ns,
      directive_positional_parameters,
      item_type=object_type,
    ),
    text(ns, "Shared"),
    text(ns, "Subclass"),
    text(ns, "SynchronousMode"),
    text(ns, "TypeArguments"),
    text(ns, directive_uid),
    XamlMember::directive(
      ns,
      directive_unknown_content,
      value_type=object_type,
      allowed_locations=element_only,
    ),
    text(ns, "DataType"),
    XamlMember::directive(
      ns,
      "CompileBindings",
      value_type=boolean_type,
      allowed_locations=attribute_only,
    ),
    text(xml_namespace, "base"),
    text(xml_namespace, "lang"),
    text(xml_namespace, "space"),
  ]
}