sub ProcessConditionsGeneric()

in meta/parse.pl [1855:1995]


sub ProcessConditionsGeneric
{
    my ($attr, $conditions, $enumtype, $name) = @_;

    return "NULL" if not defined $conditions;

    my @conditions = @{ $conditions };

    my $ctype = shift @conditions;

    if (not $ctype =~ /^SAI_ATTR_CONDITION_TYPE_(AND|OR|MIXED)$/)
    {
        LogError "unsupported condition type $ctype on $attr";
        return "";
    }

    my $count = 0;

    my @values = ();

    for my $cond (@conditions)
    {
        if ($ctype eq "SAI_ATTR_CONDITION_TYPE_MIXED")
        {
            if ($cond =~ /^SAI_ATTR_CONDITION_TYPE_(AND|OR)$/)
            {
                WriteSource "const sai_attr_condition_t sai_metadata_${name}_${attr}_$count = {";
                WriteSource ".attrid = SAI_INVALID_ATTRIBUTE_ID,";
                WriteSource ".condition = { 0 },";
                WriteSource ".op = SAI_CONDITION_OPERATOR_EQ,";
                WriteSource ".type = $cond";
                WriteSource "};";

                $count++;
                next;
            }
        }

        if (not $cond =~ /^(SAI_\w+) == (true|false|SAI_\w+|$NUMBER_REGEX)$/)
        {
            LogError "invalid condition '$cond' on $attr";
            return "";
        }

        my $attrid = $1;
        my $val = $2;

        my $main_attr = $1 if $attr =~ /^SAI_(\w+?)_ATTR_/;
        my $cond_attr = $1 if $attrid =~ /^SAI_(\w+?)_ATTR_/;

        if ($main_attr ne $cond_attr)
        {
            LogError "$name attribute $attr has condition from different object $attrid";
            return "";
        }

        WriteSource "const sai_attr_condition_t sai_metadata_${name}_${attr}_$count = {";

        my $attrType = lc("$1t") if $attrid =~ /^(SAI_\w+_ATTR_)/;
        my $enumTypeName = $METADATA{$attrType}{$attrid}{type};

        if (not defined $enumTypeName)
        {
            LogError("failed to find attribute ${attrType}::${attrid} when processing $attrid");
            next;
        }

        if ($val eq "true" or $val eq "false")
        {
            WriteSource ".attrid = $attrid,";
            WriteSource ".condition = { .booldata = $val },";
            WriteSource ".op = SAI_CONDITION_OPERATOR_EQ,";
            WriteSource ".type = SAI_ATTR_CONDITION_TYPE_NONE";
        }
        elsif ($val =~ /^SAI_/)
        {
            WriteSource ".attrid = $attrid,";
            WriteSource ".condition = { .s32 = $val },";
            WriteSource ".op = SAI_CONDITION_OPERATOR_EQ,";
            WriteSource ".type = SAI_ATTR_CONDITION_TYPE_NONE";

            my $attrType = lc("$1t") if $attrid =~ /^(SAI_\w+_ATTR_)/;

            my $enumTypeName = $METADATA{$attrType}{$attrid}{type};

            if (not defined $enumTypeName)
            {
                LogError("failed to find attribute ${attrType}::${attrid} when processing $attrid");
                next;
            }

            if (defined $SAI_ENUMS{$enumTypeName})
            {
                # this condition is enum condition, check if condition value
                # belongs to that enum

                my @values = @{ $SAI_ENUMS{$enumTypeName}{values} };

                if (not grep( /^$val$/, @values))
                {
                    LogError "condition value '$val' in '$cond' on $attr is not present in $enumTypeName";
                }
            }
        }
        elsif ($val =~ /^$NUMBER_REGEX$/ and $enumTypeName =~ /^sai_u?int(\d+)_t$/)
        {
            my $n = $1;
            my $item = ($enumTypeName =~ /uint/) ? "u$n" : "s$n";

            WriteSource ".attrid = $attrid,";
            WriteSource ".condition = { .$item = $val },";
            WriteSource ".op = SAI_CONDITION_OPERATOR_EQ,";
            WriteSource ".type = SAI_ATTR_CONDITION_TYPE_NONE";
        }
        else
        {
            LogError "unknown $name value: $val on $attr $enumtype";
            return "";
        }

        WriteSource "};";

        $count++;
    }

    WriteSource "const sai_attr_condition_t* const sai_metadata_${name}s_${attr}\[\] = {";

    $count = 0;

    for my $cond (@conditions)
    {
        WriteSource "&sai_metadata_${name}_${attr}_$count,";

        $count++;
    }

    WriteSource "NULL";
    WriteSource "};";

    return "sai_metadata_${name}s_${attr}";
}