function udtSpec = udtCreateUDTSpecStruct(udtDTRow, udtString)
%udtCreateUDTSpecStruct creates a Unified Data Type (UDT) specification structure
%   udtCreateUDTSpecStruct(udtDTRow, udtString) returns the UDT spec structure
%   corresponding to the provided udtDTRow struct argument and udtString argument
%   (Unified Data Type string setting, e.g. 'fixdt(1,16,15)', 'Inherit: Same as input').

% Copyright 2008-2015 The MathWorks, Inc.

% Required udtSpec fields
% -----------------------
udtSpec.dtPrompt = strcat(udtDTRow.name, ':'); % e.g. 'Accumulator:', 'Output:'
try
  udtSpec.dtVal  = udtString;
catch
  udtSpec.dtVal  = '';
end
prmPrefixStr     = udtGetPrmPrefixFromFixptDTRowStruct(udtDTRow);
udtSpec.dtName   = strcat(prmPrefixStr, 'DataTypeStr'); % UDT mask param name
udtSpec.dtTag    = udtSpec.dtName; % must match var name (DDG assumption)

% Builtin type support (optional)
% -------------------------------
if isfield(udtDTRow,'builtinTypes') && ~isempty(udtDTRow.builtinTypes)
    udtSpec.dtaItems.builtinTypes = udtDTRow.builtinTypes;
end

% Scaling support
% ---------------
udtSpec.dtaItems.scalingModes = {};
if udtDTRow.binaryPointScaling
    udtSpec.dtaItems.scalingModes{end+1} = 'UDTBinaryPointMode';
end
if udtDTRow.bestPrecisionMode
    udtSpec.dtaItems.scalingModes{end+1} = 'UDTBestPrecisionMode';
end

% Signedness support
% ------------------
udtSpec.dtaItems.signModes = {};
if udtDTRow.autoSignedness
    % IMPLICIT (AUTO) SIGNEDNESS SETTING
    udtSpec.dtaItems.signModes{end+1} = 'UDTInheritSign';
end
if udtDTRow.signedSignedness
    % EXPLICIT SIGNED SETTING
    udtSpec.dtaItems.signModes{end+1} = 'UDTSignedSign';
end
if udtDTRow.unsignedSignedness
    % EXPLICIT SIGNED SETTING
    udtSpec.dtaItems.signModes{end+1} = 'UDTUnsignedSign';
end
if isempty(udtSpec.dtaItems.signModes)
    % NOTE specified -> default to SIGNED
    udtSpec.dtaItems.signModes = {'UDTSignedSign'};
end

% Inheritance rule support (optional)
% -----------------------------------
udtSpec.dtaItems.inheritRules = {};

% Possible NON-STANDARD 'Inherit:' rules
if isfield(udtDTRow, 'customInhRuleStrs')
    customInheritRuleCellArray = udtDTRow.customInhRuleStrs;
    if ~isempty(customInheritRuleCellArray)
        for inhIdx = 1:length(customInheritRuleCellArray)
            udtSpec.dtaItems.inheritRules{end+1} = ...
                sprintf('Inherit: %s', customInheritRuleCellArray{inhIdx});
        end
    end
end

% Possible STANDARD 'Inherit:' rules
if udtDTRow.inheritInternalRule
    udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Inherit via internal rule';
end

if udtDTRow.inheritSameWLAsInput
    udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Same word length as input';
end

if udtDTRow.inheritInput
    % ONE inport
    udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Same as input';
else
    % Possibly MULTIPLE inports
    if udtDTRow.inheritFirstInput
        udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Same as first input';
    end

    if udtDTRow.inheritSecondInput
        udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Same as second input';
    end
end

if udtDTRow.inheritProdOutput
    udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Same as product output';
end

if udtDTRow.inheritAccumulator
    udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Same as accumulator';
end

if isfield(udtDTRow, 'inheritBackProp') && udtDTRow.inheritBackProp
    udtSpec.dtaItems.inheritRules{end+1} = 'Inherit: Inherit via back propagation';
end

% Design Min/Max support (optional)
% ---------------------------------
if udtDTRow.hasDesignMin
    udtSpec.dtaItems.scalingMinTag = {strcat(prmPrefixStr,'Min')};
else
    udtSpec.dtaItems.scalingMinTag = {};
end

if udtDTRow.hasDesignMax
    udtSpec.dtaItems.scalingMaxTag = {strcat(prmPrefixStr,'Max')};
else
    udtSpec.dtaItems.scalingMaxTag = {};
end

% Optional value param associations (for 'Calculate best precision' button)
% -------------------------------------------------------------------------
if isempty(udtDTRow.ValBestPrecFLMaskPrm)
    udtSpec.dtaItems.scalingValueTags = {};
elseif iscell(udtDTRow.ValBestPrecFLMaskPrm)
    % Multiple (cell array of) Mask Parameter strings specified
    udtSpec.dtaItems.scalingValueTags = udtDTRow.ValBestPrecFLMaskPrm;
else
    % Only one Mask Parameter string specified as value param (or EMPTY)
    udtSpec.dtaItems.scalingValueTags = {udtDTRow.ValBestPrecFLMaskPrm};
end
