/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of xAPI4Unity. */ #if UNITY_EDITOR using System.Collections.Generic; namespace xAPI4Unity.Editor.Parser.Code.SyntaxTree { /// /// A constructor in the syntax tree /// internal sealed class Constructor { // Backing field for the constructor body statements. private MethodBody _body; // Arguments to be forwarded to the base(...) constructor invocation. private readonly List _baseArguments = new List(); // Access and other modifiers applied to this constructor. private readonly List _modifiers = new List(); // Formal parameters declared by this constructor. private readonly List _parameters = new List(); /// /// Type of the constructor /// public string Type { get; private set; } /// /// Body of the constructor /// public ref readonly MethodBody Body => ref _body; /// /// Arguments for the constructor's base constructor /// public IReadOnlyList BaseArguments => _baseArguments.AsReadOnly(); /// /// Access modifiers of the constructor /// public IReadOnlyList Modifiers => _modifiers.AsReadOnly(); /// /// Parameters of the constructor /// public IReadOnlyList Parameters => _parameters.AsReadOnly(); /// /// Initializes a constructor with no type or body. /// public Constructor() { } /// /// Initializes a constructor with a type (class name) and optional body. /// /// The declaring type name (constructor identifier). /// Optional method body. public Constructor(string type, MethodBody body = default) { Type = type; this._body = body; } /// /// Sets the constructor type (declaring class name). /// /// Type name to assign. /// The same Constructor instance for fluent chaining. public Constructor WithType(string type) { this.Type = type; return this; } /// /// Sets the constructor body. /// /// Method body to assign. /// The same Constructor instance for fluent chaining. public Constructor WithBody(MethodBody body) { this._body = body; return this; } /// /// Adds a single base(...) argument. /// /// Argument to forward to base constructor. /// The same Constructor instance for fluent chaining. public Constructor AddBaseArgument(Argument arg) { _baseArguments.Add(arg); return this; } /// /// Adds multiple base(...) arguments. /// /// Arguments to forward to base constructor. /// The same Constructor instance for fluent chaining. public Constructor AddBaseArguments(params Argument[] args) { _baseArguments.AddRange(args); return this; } /// /// Adds a sequence of base(...) arguments. /// /// Arguments to forward to base constructor. /// The same Constructor instance for fluent chaining. public Constructor AddBaseArguments(IEnumerable args) { _baseArguments.AddRange(args); return this; } /// /// Adds a single modifier to this constructor. /// /// Modifier to add. /// The same Constructor instance for fluent chaining. public Constructor AddModifier(Modifier mod) { _modifiers.Add(mod); return this; } /// /// Adds multiple modifiers to this constructor. /// /// Modifiers to add. /// The same Constructor instance for fluent chaining. public Constructor AddModifiers(params Modifier[] mods) { _modifiers.AddRange(mods); return this; } /// /// Adds a sequence of modifiers to this constructor. /// /// Modifiers to add. /// The same Constructor instance for fluent chaining. public Constructor AddModifiers(IEnumerable mods) { _modifiers.AddRange(mods); return this; } /// /// Adds a single parameter to the constructor signature. /// /// Parameter to add. /// The same Constructor instance for fluent chaining. public Constructor AddParameter(Parameter param) { _parameters.Add(param); return this; } /// /// Adds multiple parameters to the constructor signature. /// /// Parameters to add. /// The same Constructor instance for fluent chaining. public Constructor AddParameters(params Parameter[] parameters) { this._parameters.AddRange(parameters); return this; } /// /// Adds a sequence of parameters to the constructor signature. /// /// Parameters to add. /// The same Constructor instance for fluent chaining. public Constructor AddParameters(IEnumerable parameters) { this._parameters.AddRange(parameters); return this; } } } #endif