2
0

Item.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright (C) 2003-2017 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2, or (at your option)
  5. # any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Automake::Item;
  13. use 5.006;
  14. use strict;
  15. use Carp;
  16. use Automake::ChannelDefs;
  17. use Automake::DisjConditions;
  18. =head1 NAME
  19. Automake::Item - base class for Automake::Variable and Automake::Rule
  20. =head1 DESCRIPTION
  21. =head2 Methods
  22. =over 4
  23. =item C<new Automake::Item $name>
  24. Create and return an empty Item called C<$name>.
  25. =cut
  26. sub new ($$)
  27. {
  28. my ($class, $name) = @_;
  29. my $self = {
  30. name => $name,
  31. defs => {},
  32. conds => {},
  33. };
  34. bless $self, $class;
  35. return $self;
  36. }
  37. =item C<$item-E<gt>name>
  38. Return the name of C<$item>.
  39. =cut
  40. sub name ($)
  41. {
  42. my ($self) = @_;
  43. return $self->{'name'};
  44. }
  45. =item C<$item-E<gt>def ($cond)>
  46. Return the definition for this item in condition C<$cond>, if it
  47. exists. Return 0 otherwise.
  48. =cut
  49. sub def ($$)
  50. {
  51. # This method is called very often, so keep it small and fast. We
  52. # don't mind the extra undefined items introduced by lookup failure;
  53. # avoiding this with 'exists' means doing two hash lookup on
  54. # success, and proved worse on benchmark.
  55. my $def = $_[0]->{'defs'}{$_[1]};
  56. return defined $def && $def;
  57. }
  58. =item C<$item-E<gt>rdef ($cond)>
  59. Return the definition for this item in condition C<$cond>. Abort with
  60. an internal error if the item was not defined under this condition.
  61. The I<r> in front of C<def> stands for I<required>. One
  62. should call C<rdef> to assert the conditional definition's existence.
  63. =cut
  64. sub rdef ($$)
  65. {
  66. my ($self, $cond) = @_;
  67. my $d = $self->def ($cond);
  68. prog_error ("undefined condition '" . $cond->human . "' for '"
  69. . $self->name . "'\n" . $self->dump)
  70. unless $d;
  71. return $d;
  72. }
  73. =item C<$item-E<gt>set ($cond, $def)>
  74. Add a new definition to an existing item.
  75. =cut
  76. sub set ($$$)
  77. {
  78. my ($self, $cond, $def) = @_;
  79. $self->{'defs'}{$cond} = $def;
  80. $self->{'conds'}{$cond} = $cond;
  81. }
  82. =item C<$var-E<gt>conditions>
  83. Return an L<Automake::DisjConditions> describing the conditions that
  84. that an item is defined in.
  85. These are all the conditions for which is would be safe to call
  86. C<rdef>.
  87. =cut
  88. sub conditions ($)
  89. {
  90. my ($self) = @_;
  91. prog_error ("self is not a reference")
  92. unless ref $self;
  93. return new Automake::DisjConditions (values %{$self->{'conds'}});
  94. }
  95. =item C<@missing_conds = $var-E<gt>not_always_defined_in_cond ($cond)>
  96. Check whether C<$var> is always defined for condition C<$cond>.
  97. Return a list of conditions where the definition is missing.
  98. For instance, given
  99. if COND1
  100. if COND2
  101. A = foo
  102. D = d1
  103. else
  104. A = bar
  105. D = d2
  106. endif
  107. else
  108. D = d3
  109. endif
  110. if COND3
  111. A = baz
  112. B = mumble
  113. endif
  114. C = mumble
  115. we should have (we display result as conditional strings in this
  116. illustration, but we really return DisjConditions objects):
  117. var ('A')->not_always_defined_in_cond ('COND1_TRUE COND2_TRUE')
  118. => ()
  119. var ('A')->not_always_defined_in_cond ('COND1_TRUE')
  120. => ()
  121. var ('A')->not_always_defined_in_cond ('TRUE')
  122. => ("COND1_FALSE COND3_FALSE")
  123. var ('B')->not_always_defined_in_cond ('COND1_TRUE')
  124. => ("COND1_TRUE COND3_FALSE")
  125. var ('C')->not_always_defined_in_cond ('COND1_TRUE')
  126. => ()
  127. var ('D')->not_always_defined_in_cond ('TRUE')
  128. => ()
  129. var ('Z')->not_always_defined_in_cond ('TRUE')
  130. => ("TRUE")
  131. =cut
  132. sub not_always_defined_in_cond ($$)
  133. {
  134. my ($self, $cond) = @_;
  135. # Compute the subconditions where $var isn't defined.
  136. return
  137. $self->conditions
  138. ->sub_conditions ($cond)
  139. ->invert
  140. ->multiply ($cond);
  141. }
  142. 1;
  143. ### Setup "GNU" style for perl-mode and cperl-mode.
  144. ## Local Variables:
  145. ## perl-indent-level: 2
  146. ## perl-continued-statement-offset: 2
  147. ## perl-continued-brace-offset: 0
  148. ## perl-brace-offset: 0
  149. ## perl-brace-imaginary-offset: 0
  150. ## perl-label-offset: -2
  151. ## cperl-indent-level: 2
  152. ## cperl-brace-offset: 0
  153. ## cperl-continued-brace-offset: 0
  154. ## cperl-label-offset: -2
  155. ## cperl-extra-newline-before-brace: t
  156. ## cperl-merge-trailing-else: nil
  157. ## cperl-continued-statement-offset: 2
  158. ## End: