<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright (C) 1997-2021 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.

package Automake::DisjConditions;

use 5.006;
use strict;
use warnings FATAL =&gt; 'all';

use Carp;
use Automake::Condition qw (TRUE FALSE);

=head1 NAME

Automake::DisjConditions - record a disjunction of Conditions

=head1 SYNOPSIS

  use Automake::Condition;
  use Automake::DisjConditions;

  # Create a Condition to represent "COND1 and not COND2".
  my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
  # Create a Condition to represent "not COND3".
  my $other = new Automake::Condition "COND3_FALSE";

  # Create a DisjConditions to represent
  #   "(COND1 and not COND2) or (not COND3)"
  my $set = new Automake::DisjConditions $cond, $other;

  # Return the list of Conditions involved in $set.
  my @conds = $set-&gt;conds;

  # Return one of the Condition involved in $set.
  my $cond = $set-&gt;one_cond;

  # Return true iff $set is always true (i.e. its subconditions
  # cover all cases).
  if ($set-&gt;true) { ... }

  # Return false iff $set is always false (i.e. is empty, or contains
  # only false conditions).
  if ($set-&gt;false) { ... }

  # Return a string representing the DisjConditions.
  #   "COND1_TRUE COND2_FALSE | COND3_FALSE"
  my $str = $set-&gt;string;

  # Return a human readable string representing the DisjConditions.
  #   "(COND1 and !COND2) or (!COND3)"
  my $str = $set-&gt;human;

  # Merge (OR) several DisjConditions.
  my $all = $set-&gt;merge($set2, $set3, ...)

  # Invert a DisjConditions, i.e., create a new DisjConditions
  # that complements $set.
  my $inv = $set-&gt;invert;

  # Multiply two DisjConditions.
  my $prod = $set1-&gt;multiply ($set2);

  # Return the subconditions of a DisjConditions with respect to
  # a Condition.  See the description for a real example.
  my $subconds = $set-&gt;sub_conditions ($cond);

  # Check whether a new definition in condition $cond would be
  # ambiguous w.r.t. existing definitions in $set.
  ($msg, $ambig_cond) = $set-&gt;ambiguous_p ($what, $cond);

=head1 DESCRIPTION

A C&lt;DisjConditions&gt; is a disjunction of C&lt;Condition&gt;s.  In Automake
they are used to represent the conditions into which Makefile
variables and Makefile rules are defined.

If the variable C&lt;VAR&gt; is defined as

  if COND1
    if COND2
      VAR = value1
    endif
  endif
  if !COND3
    if COND4
      VAR = value2
    endif
  endif

then it will be associated a C&lt;DisjConditions&gt; created with
the following statement.

  new Automake::DisjConditions
    (new Automake::Condition ("COND1_TRUE", "COND2_TRUE"),
     new Automake::Condition ("COND3_FALSE", "COND4_TRUE"));

As you can see, a C&lt;DisjConditions&gt; is made from a list of
C&lt;Condition&gt;s.  Since C&lt;DisjConditions&gt; is a disjunction, and
C&lt;Condition&gt; is a conjunction, the above can be read as
follows.

  (COND1 and COND2) or ((not COND3) and COND4)

That's indeed the condition in which C&lt;VAR&gt; has a value.

Like C&lt;Condition&gt; objects, a C&lt;DisjConditions&gt; object is unique
with respect to its conditions.  Two C&lt;DisjConditions&gt; objects created
for the same set of conditions will have the same address.  This makes
it easy to compare C&lt;DisjConditions&gt;s: just compare the references.

=head2 Methods

=over 4

=item C&lt;$set = new Automake::DisjConditions [@conds]&gt;

Create a C&lt;DisjConditions&gt; object from the list of C&lt;Condition&gt;
objects passed in arguments.

If the C&lt;@conds&gt; list is empty, the C&lt;DisjConditions&gt; is assumed to be
false.

As explained previously, the reference (object) returned is unique
with respect to C&lt;@conds&gt;.  For this purpose, duplicate elements are
ignored.

=cut

# Keys in this hash are DisjConditions strings. Values are the
# associated object DisjConditions.  This is used by 'new' to reuse
# DisjConditions objects with identical conditions.
our %_disjcondition_singletons;

sub new ($;@)
{
  my ($class, @conds) = @_;
  my @filtered_conds = ();
  for my $cond (@conds)
    {
      confess "'$cond' isn't a reference" unless ref $cond;
      confess "'$cond' isn't an Automake::Condition"
	unless $cond-&gt;isa ("Automake::Condition");

      # This is a disjunction of conditions, so we drop
      # false conditions.  We'll always treat an "empty"
      # DisjConditions as false for this reason.
      next if $cond-&gt;false;

      push @filtered_conds, $cond;
    }

  my $string;
  if (@filtered_conds)
    {
      @filtered_conds = sort { $a-&gt;string cmp $b-&gt;string } @filtered_conds;
      $string = join (' | ', map { $_-&gt;string } @filtered_conds);
    }
  else
    {
      $string = 'FALSE';
    }

  # Return any existing identical DisjConditions.
  my $me = $_disjcondition_singletons{$string};
  return $me if $me;

  # Else, create a new DisjConditions.

  # Store conditions as keys AND as values, because blessed
  # objects are converted to strings when used as keys (so
  # at least we still have the value when we need to call
  # a method).
  my %h = map {$_ =&gt; $_} @filtered_conds;

  my $self = {
    hash =&gt; \%h,
    string =&gt; $string,
    conds =&gt; \@filtered_conds,
  };
  bless $self, $class;

  $_disjcondition_singletons{$string} = $self;
  return $self;
}


=item C&lt;CLONE&gt;

Internal special subroutine to fix up the self hashes in
C&lt;%_disjcondition_singletons&gt; upon thread creation.  C&lt;CLONE&gt; is invoked
automatically with ithreads from Perl 5.7.2 or later, so if you use this
module with earlier versions of Perl, it is not thread-safe.

=cut

sub CLONE
{
  foreach my $self (values %_disjcondition_singletons)
    {
      my %h = map { $_ =&gt; $_ } @{$self-&gt;{'conds'}};
      $self-&gt;{'hash'} = \%h;
    }
}


=item C&lt;@conds = $set-E&lt;gt&gt;conds&gt;

Return the list of C&lt;Condition&gt; objects involved in C&lt;$set&gt;.

=cut

sub conds ($ )
{
  my ($self) = @_;
  return @{$self-&gt;{'conds'}};
}

=item C&lt;$cond = $set-E&lt;gt&gt;one_cond&gt;

Return one C&lt;Condition&gt; object involved in C&lt;$set&gt;.

=cut

sub one_cond ($)
{
  my ($self) = @_;
  return (%{$self-&gt;{'hash'}},)[1];
}

=item C&lt;$et = $set-E&lt;gt&gt;false&gt;

Return 1 iff the C&lt;DisjConditions&gt; object is always false (i.e., if it
is empty, or if it contains only false C&lt;Condition&gt;s). Return 0
otherwise.

=cut

sub false ($ )
{
  my ($self) = @_;
  return 0 == keys %{$self-&gt;{'hash'}};
}

=item C&lt;$et = $set-E&lt;gt&gt;true&gt;

Return 1 iff the C&lt;DisjConditions&gt; object is always true (i.e. covers all
conditions). Return 0 otherwise.

=cut

sub true ($ )
{
  my ($self) = @_;
  return $self-&gt;invert-&gt;false;
}

=item C&lt;$str = $set-E&lt;gt&gt;string&gt;

Build a string which denotes the C&lt;DisjConditions&gt;.

=cut

sub string ($ )
{
  my ($self) = @_;
  return $self-&gt;{'string'};
}

=item C&lt;$cond-E&lt;gt&gt;human&gt;

Build a human readable string which denotes the C&lt;DisjConditions&gt;.

=cut

sub human ($ )
{
  my ($self) = @_;

  return $self-&gt;{'human'} if defined $self-&gt;{'human'};

  my $res = '';
  if ($self-&gt;false)
    {
      $res = 'FALSE';
    }
  else
    {
      my @c = $self-&gt;conds;
      if (1 == @c)
	{
	  $res = $c[0]-&gt;human;
	}
      else
	{
	  $res = '(' . join (') or (', map { $_-&gt;human } $self-&gt;conds) . ')';
	}
    }
  $self-&gt;{'human'} = $res;
  return $res;
}


=item C&lt;$newcond = $cond-E&lt;gt&gt;merge (@otherconds)&gt;

Return a new C&lt;DisjConditions&gt; which is the disjunction of
C&lt;$cond&gt; and C&lt;@otherconds&gt;.  Items in C&lt;@otherconds&gt; can be
@C&lt;Condition&gt;s or C&lt;DisjConditions&gt;.

=cut

sub merge ($@)
{
  my ($self, @otherconds) = @_;
  new Automake::DisjConditions (
    map { $_-&gt;isa ("Automake::DisjConditions") ? $_-&gt;conds : $_ }
        ($self, @otherconds));
}


=item C&lt;$prod = $set1-E&lt;gt&gt;multiply ($set2)&gt;

Multiply two conditional sets.

  my $set1 = new Automake::DisjConditions
    (new Automake::Condition ("A_TRUE"),
     new Automake::Condition ("B_TRUE"));
  my $set2 = new Automake::DisjConditions
    (new Automake::Condition ("C_FALSE"),
     new Automake::Condition ("D_FALSE"));

C&lt;$set1-E&lt;gt&gt;multiply ($set2)&gt; will return

  new Automake::DisjConditions
    (new Automake::Condition ("A_TRUE", "C_FALSE"),
     new Automake::Condition ("B_TRUE", "C_FALSE"),;
     new Automake::Condition ("A_TRUE", "D_FALSE"),
     new Automake::Condition ("B_TRUE", "D_FALSE"));

The argument can also be a C&lt;Condition&gt;.

=cut

# Same as multiply() but take a list of Conditionals as second argument.
# We use this in invert().
sub _multiply ($@)
{
  my ($self, @set) = @_;
  my @res = map { $_-&gt;multiply (@set) } $self-&gt;conds;
  return new Automake::DisjConditions (Automake::Condition::reduce_or @res);
}

sub multiply ($$)
{
  my ($self, $set) = @_;
  return $self-&gt;_multiply ($set) if $set-&gt;isa('Automake::Condition');
  return $self-&gt;_multiply ($set-&gt;conds);
}

=item C&lt;$inv = $set-E&lt;gt&gt;invert&gt;

Invert a C&lt;DisjConditions&gt;.  Return a C&lt;DisjConditions&gt; which is true
when C&lt;$set&gt; is false, and vice-versa.

  my $set = new Automake::DisjConditions
    (new Automake::Condition ("A_TRUE", "B_TRUE"),
     new Automake::Condition ("A_FALSE", "B_FALSE"));

Calling C&lt;$set-E&lt;gt&gt;invert&gt; will return the following C&lt;DisjConditions&gt;.

  new Automake::DisjConditions
    (new Automake::Condition ("A_TRUE", "B_FALSE"),
     new Automake::Condition ("A_FALSE", "B_TRUE"));

We implement the inversion by a product-of-sums to sum-of-products
conversion using repeated multiplications.  Because of the way we
implement multiplication, the result of inversion is in canonical
prime implicant form.

=cut

sub invert($ )
{
  my ($self) = @_;

  return $self-&gt;{'invert'} if defined $self-&gt;{'invert'};

  # The invert of an empty DisjConditions is TRUE.
  my $res = new Automake::DisjConditions TRUE;

  #   !((a.b)+(c.d)+(e.f))
  # = (!a+!b).(!c+!d).(!e+!f)
  # We develop this into a sum of product iteratively, starting from TRUE:
  # 1) TRUE
  # 2) TRUE.!a + TRUE.!b
  # 3) TRUE.!a.!c + TRUE.!b.!c + TRUE.!a.!d + TRUE.!b.!d
  # 4) TRUE.!a.!c.!e + TRUE.!b.!c.!e + TRUE.!a.!d.!e + TRUE.!b.!d.!e
  #    + TRUE.!a.!c.!f + TRUE.!b.!c.!f + TRUE.!a.!d.!f + TRUE.!b.!d.!f
  foreach my $cond ($self-&gt;conds)
    {
      $res = $res-&gt;_multiply ($cond-&gt;not);
    }

  # Cache result.
  $self-&gt;{'invert'} = $res;
  # It's tempting to also set $res-&gt;{'invert'} to $self, but that
  # is a bad idea as $self hasn't been normalized in any way.
  # (Different inputs can produce the same inverted set.)
  return $res;
}

=item C&lt;$self-E&lt;gt&gt;simplify&gt;

Return a C&lt;Disjunction&gt; which is a simplified canonical form of C&lt;$self&gt;.
This canonical form contains only prime implicants, but it can contain
non-essential prime implicants.

=cut

sub simplify ($)
{
  my ($self) = @_;
  return $self-&gt;invert-&gt;invert;
}

=item C&lt;$self-E&lt;gt&gt;sub_conditions ($cond)&gt;

Return the subconditions of C&lt;$self&gt; that contains C&lt;$cond&gt;, with
C&lt;$cond&gt; stripped.  More formally, return C&lt;$res&gt; such that
C&lt;$res-E&lt;gt&gt;multiply ($cond) == $self-E&lt;gt&gt;multiply ($cond)&gt; and
C&lt;$res&gt; does not mention any of the variables in C&lt;$cond&gt;.

For instance, consider:

  my $a = new Automake::DisjConditions
    (new Automake::Condition ("A_TRUE", "B_TRUE"),
     new Automake::Condition ("A_TRUE", "C_FALSE"),
     new Automake::Condition ("A_TRUE", "B_FALSE", "C_TRUE"),
     new Automake::Condition ("A_FALSE"));
  my $b = new Automake::DisjConditions
    (new Automake::Condition ("A_TRUE", "B_FALSE"));

Calling C&lt;$a-E&lt;gt&gt;sub_conditions ($b)&gt; will return the following
C&lt;DisjConditions&gt;.

  new Automake::DisjConditions
    (new Automake::Condition ("C_FALSE"), # From A_TRUE C_FALSE
     new Automake::Condition ("C_TRUE")); # From A_TRUE B_FALSE C_TRUE"

=cut

sub sub_conditions ($$)
{
  my ($self, $subcond) = @_;

  # Make $subcond blindingly apparent in the DisjConditions.
  # For instance '$b-&gt;multiply($a-&gt;conds)' (from the POD example) is:
  # 	(new Automake::Condition ("FALSE"),
  # 	 new Automake::Condition ("A_TRUE", "B_FALSE", "C_FALSE"),
  # 	 new Automake::Condition ("A_TRUE", "B_FALSE", "C_TRUE"),
  # 	 new Automake::Condition ("FALSE"))
  my @prodconds = $subcond-&gt;multiply ($self-&gt;conds);

  # Now, strip $subcond from the remaining (i.e., non-false) Conditions.
  my @res = map { $_-&gt;false ? () : $_-&gt;strip ($subcond) } @prodconds;

  return new Automake::DisjConditions @res;
}

=item C&lt;($string, $ambig_cond) = $condset-E&lt;gt&gt;ambiguous_p ($what, $cond)&gt;

Check for an ambiguous condition.  Return an error message and the
other condition involved if we have an ambiguity.  Return an empty
string and FALSE otherwise.

C&lt;$what&gt; is the name of the thing being defined, to use in the error
message.  C&lt;$cond&gt; is the C&lt;Condition&gt; under which it is being
defined.  C&lt;$condset&gt; is the C&lt;DisjConditions&gt; under which it had
already been defined.

=cut

sub ambiguous_p ($$$)
{
  my ($self, $var, $cond) = @_;

  # Note that these rules don't consider the following
  # example as ambiguous.
  #
  #   if COND1
  #     FOO = foo
  #   endif
  #   if COND2
  #     FOO = bar
  #   endif
  #
  # It's up to the user to not define COND1 and COND2
  # simultaneously.

  return ("$var multiply defined in condition " . $cond-&gt;human, $cond)
    if exists $self-&gt;{'hash'}{$cond};

  foreach my $vcond ($self-&gt;conds)
    {
      return ("$var was already defined in condition " . $vcond-&gt;human
	      . ", which includes condition ". $cond-&gt;human, $vcond)
	if $vcond-&gt;true_when ($cond);

      return ("$var was already defined in condition " . $vcond-&gt;human
	      . ", which is included in condition " . $cond-&gt;human, $vcond)
	if $cond-&gt;true_when ($vcond);
    }
  return ('', FALSE);
}

=head1 SEE ALSO

L&lt;Automake::Condition&gt;.

=head1 HISTORY

C&lt;AM_CONDITIONAL&gt;s and supporting code were added to Automake 1.1o by
Ian Lance Taylor &lt;ian@cygnus.org&gt; in 1997.  Since then it has been
improved by Tom Tromey &lt;tromey@redhat.com&gt;, Richard Boulton
&lt;richard@tartarus.org&gt;, Raja R Harinath &lt;harinath@cs.umn.edu&gt;, Akim
Demaille &lt;akim@epita.fr&gt;, Pavel Roskin &lt;proski@gnu.org&gt;, and
Alexandre Duret-Lutz &lt;adl@gnu.org&gt;.

=cut

1;
</pre></body></html>