1 %************************************************************************
3 \section[Driver-iface-thing]{Interface-file handling}
5 %************************************************************************
9 %Decl = (); # details about individual definitions
10 %Stuff = (); # where we glom things together
11 %HiExists = ('old',-1, 'new',-1); # 1 <=> definitely exists; 0 <=> doesn't
12 %HiHasBeenRead = ('old', 0, 'new', 0);
13 %ModuleVersion = ('old', 0, 'new', 0);
17 sub postprocessHiFile {
18 local($hsc_hi, # The iface info produced by hsc.
19 $hifile_target, # The name both of the .hi file we
20 # already have and which we *might*
22 $going_interactive) = @_;
24 local($new_hi) = "$Tmp_prefix.hi-new";
25 local($show_hi_diffs) = $HiDiff_flag && ! $HiOnStdout && ! $going_interactive && -f $hifile_target;
27 print STDERR "*** New hi file follows...\n" if $Verbose;
28 system("$Cat $hsc_hi 1>&2") if $Verbose;
30 &constructNewHiFile($hsc_hi, *hifile_target, $new_hi, $show_hi_diffs);
32 # run diff if they asked for it
34 if ( $HiDiff_flag eq 'usages' ) {
35 # lots of near-useless info; but if you want it...
36 &run_something("$Cmp -s $hifile_target $new_hi || $Diff $hifile_target $new_hi 1>&2 || exit 0",
37 "Diff'ing old and new .$HiSuffix files"); # NB: to stderr
39 # strip out usages, *then* run diff
40 local($hi_before) = "$Tmp_prefix.hi-before";
41 local($hi_after) = "$Tmp_prefix.hi-now";
43 &deUsagifyHi($hifile_target, $hi_before);
44 &deUsagifyHi($new_hi, $hi_after);
46 &run_something("$Cmp -s $hi_before $hi_after || $Diff $hi_before $hi_after 1>&2 || exit 0",
47 "Diff'ing old and new .$HiSuffix files"); # NB: to stderr
51 # if we produced an interface file "no matter what",
52 # print what we got on stderr.
54 if ( $HiWith ne '' ) {
55 # output some of the sections
56 local($hi_after) = "$Tmp_prefix.hi-now";
58 foreach $hi ( split(' ',$HiWith) ) {
61 &hiSectionsOnly($new_hi, $hi_after);
63 system("$Cat $hi_after 1>&2 ; $Rm $hi_after; ");
65 system("$Cat $new_hi 1>&2");
68 &run_something("$Cmp -s $hifile_target $new_hi || ( $Rm $hifile_target && $Cp $new_hi $hifile_target )",
69 "Replace .$HiSuffix file, if changed");
74 local($ifile,$ofile) = @_;
76 open(OLDHIF, "< $ifile") || &tidy_up_and_die(1,"Can't open $ifile (read)\n");
77 open(NEWHIF, "> $ofile") || &tidy_up_and_die(1,"Can't open $ofile (write)\n");
79 # read up to _usages_ line
82 print NEWHIF $_ unless /^(__interface|import)/;
86 close(OLDHIF) || &tidy_up_and_die(1,"Failed reading from $ifile\n");
87 close(NEWHIF) || &tidy_up_and_die(1,"Failed writing to $ofile\n");
93 local($ifile,$ofile) = @_;
95 open(OLDHIF, "< $ifile") || &tidy_up_and_die(1,"Can't open $ifile (read)\n");
96 open(NEWHIF, "> $ofile") || &tidy_up_and_die(1,"Can't open $ofile (write)\n");
98 # read up to _usages_ line
101 if ( /^__export/ && $HiSection {'exports'} ||
102 /^import / && $HiSection {'imports'} ||
103 /^\d+ ([^ ]+ :: |type |data |class |newtype )/ && $HiSection {'declarations'} ||
104 /^instance / && $HiSection {'instances'} ) {
112 close(OLDHIF) || &tidy_up_and_die(1,"Failed reading from $ifile\n");
113 close(NEWHIF) || &tidy_up_and_die(1,"Failed writing to $ofile\n");
118 sub constructNewHiFile {
119 local($hsc_hi, # The iface info produced by hsc.
120 *hifile_target, # Pre-existing .hi filename (if it exists)
121 $new_hi, # Filename for new one
122 $show_hi_diffs) = @_;
123 local($hiname,$hidir);
124 local($mod_name_dec);
126 &readHiFile('new',$hsc_hi) unless $HiHasBeenRead{'new'} == 1;
128 # Sigh, we need decode the module name found in the interface file
129 # since that's the (base)name we want to use when outputting the
131 $mod_name_dec = $ModuleName{'new'};
132 $mod_name_dec =~ s/zz/z/g;
133 $mod_name_dec =~ s/ZZ/Z/g;
134 $mod_name_dec =~ s/zu/_/g;
136 if ($Specific_hi_file eq '') { # -ohi is used even if module name != stem of filename.
137 ($hiname = $hifile_target) = $1 if $hifile_target =~ /\/?([^\/]+)\.$HiSuffix$/;
138 if ( $mod_name_dec ne $hiname ) {
140 # strip off basename only if we've got a dirname.
141 ($hidir = $hifile_target) =~ s/(.*\/)[^\/]*$/$1/
142 if ( $hifile_target =~ /\/$hiname\.$HiSuffix/ );
143 $hifile_target = $hidir . $mod_name_dec . ".$HiSuffix";
146 &readHiFile('old',$hifile_target) unless $HiHasBeenRead{'old'} == 1;
148 open(NEWHI, "> $new_hi") || &tidy_up_and_die(1,"Can't open $new_hi (write)\n");
150 local(@decl_names) = (); # Declarations in new module
151 foreach $v (sort (keys %Decl)) {
152 next unless $v =~ /^new:(.*$)/;
153 push(@decl_names,$1);
156 local($new_module_version) = &calcNewModuleVersion(@decl_names);
157 print NEWHI "__interface ", $PackageName{'new'}, $ModuleName{'new'}, " $new_module_version $Orphan{'new'} $ProjectVersionInt where\n";
158 print NEWHI $Stuff{'new:exports'};
159 print NEWHI $Stuff{'new:usages'} unless $Stuff{'new:usages'} eq '';
160 print NEWHI $Stuff{'new:instances'} unless $Stuff{'new:instances'} eq '';
162 foreach $v (@decl_names) {
163 &printNewItemVersion(NEWHI, $v, $new_module_version, $show_hi_diffs); # Print new version number
164 print NEWHI $Decl{"new:$v"}; # Print the new decl itself
166 print NEWHI $Stuff{'new:rules'} unless $Stuff{'new:rules'} eq '';
167 print NEWHI $Stuff{'new:deprecations'} unless $Stuff{'new:deprecations'} eq '';
169 close(NEWHI) || &tidy_up_and_die(1,"Failed writing to $new_hi\n");
173 Read the .hi file made by the compiler, or the old one.
174 All the declarations in the file are stored in
178 where $mod is "new" or "old", depending on whether it's the new or old
179 .hi file that's being read.
183 for tycons T "type T" or "data T"
184 for classes C "class C"
189 local($mod, # module to read; can be special tag 'old'
190 # (old .hi file for module being compiled) or
191 # 'new' (new proto-.hi file for...)
192 $hifile) = @_; # actual file to read
194 # info about the old version of this module's interface
195 $HiExists{$mod} = -1; # 1 <=> definitely exists; 0 <=> doesn't
196 $HiHasBeenRead{$mod} = 0;
197 $ModuleVersion{$mod} = 0;
198 $Stuff{"$mod:usages"} = ''; # stuff glommed together
199 $Stuff{"$mod:exports"} = '';
200 $Stuff{"$mod:instances"} = '';
201 $Stuff{"$mod:declarations"} = '';
202 $Stuff{"$mod:rules"} = '';
203 $Stuff{"$mod:deprecations"} = '';
205 if (! -f $hifile) { # no pre-existing .hi file
210 open(HIFILE, "< $hifile") || &tidy_up_and_die(1,"Can't open $hifile (read)\n");
212 hi_line: while (<HIFILE>) {
213 next if /^ *$/; # blank line
215 if ( /^__interface ("[A-Za-z]*"\s*)([A-Z]\S*)\s+(\d+)?\s*(\!)?/ ) {
216 if ( $mod ne 'new' ) {
217 # Reading old .hi file
218 $ModuleVersion{$mod} = $3;
221 $PackageName{$mod} = $1;
222 $ModuleName{$mod} = $2; # used to decide name of iface file.
224 # optional "!" indicates that the
225 # module contains orphan rules or instance decls
227 } elsif ( /^import / ) {
228 $Stuff{"$mod:usages"} .= $_; # save the whole thing
230 } elsif ( /^__export/ ) {
231 $Stuff{"$mod:exports"} .= $_;
233 } elsif ( /^instance / ) {
234 $Stuff{"$mod:instances"} .= $_;
236 } elsif ( /^{-## __R / ) {
237 $Stuff{"$mod:rules"} .= $_;
239 } elsif ( /^{-## __D / ) {
240 $Stuff{"$mod:deprecations"} .= $_;
242 } elsif ( /^-[-]+ .*/ ) { # silently ignore comment lines.
244 } else { # We're in a declaration
246 # Strip off the initial version number, if any
247 if ( /^([0-9]+)\s+(.*\n)/ ) {
249 # The "\n" is because we need to keep the newline at
250 # the end, so that it looks the same as if there's no version
251 # number and this if statement doesn't fire.
253 # So there's an initial version number
258 if ( /^type\s+(\S+)/ ) {
260 $current_name = "type $1";
261 $Decl{"$mod:$current_name"} = $_;
262 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
264 } elsif ( /^(newtype|data)\s+({.*}\s+=>\s+)?(\S+)\s+/ ) {
266 # The (...)? parts skips over the context of a data decl
267 # to find the name of the type constructor. The curly
268 # brackets are part of the iface file syntax for contexts
269 $current_name = "data $3";
270 $Decl{"$mod:$current_name"} = $_;
271 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
273 } elsif ( /^class\s+(\{[^{}]*\}\s+=>\s+)?(\S+)\s+/ ) {
275 # must be wary of => bit matching after "where"...
276 # ..hence the [^{}] part
277 # NB: a class decl may not have a where part at all
278 $current_name = "class $2";
279 $Decl{"$mod:$current_name"} = $_;
280 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
282 } elsif ( /^infix(r|l)?\s+[0-9]\s+(\S+)/ ) {
284 $current_name = "fixity $2";
285 $Decl{"$mod:$current_name"} = $_;
286 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
288 } elsif ( /^(\S+)\s+::\s+/ ) {
291 $Decl{"$mod:$current_name"} = $_;
292 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
294 } else { # Continuation line
295 # print STDERR "$Pgm:junk old iface line?:$_";
296 $Decl{"$mod:$current_name"} .= $_
302 close(HIFILE) || &tidy_up_and_die(1,"Failed reading from $hifile\n");
303 $HiHasBeenRead{$mod} = 1;
308 sub calcNewModuleVersion {
309 local (@decl_names) = @_;
311 return(&mv_change(1,'no old .hi file')) if $HiExists{'old'} == 0;
312 # could use "time()" as initial version; if a module existed, then was deleted,
313 # then comes back, we don't want the resurrected one to have an
314 # lower version number than the original (in case there are any
315 # lingering references to the original in other .hi files).
317 local($unchanged_version) = $ModuleVersion{'old'}; # will return one of these two
318 local($changed_version) = $unchanged_version + 1;
320 if ($Orphan{'old'} ne $Orphan{'new'}) {
321 return(&mv_change($changed_version, "orphan-hood changed"));
324 foreach $t ( 'usages' , 'exports', 'instances', 'fixities', 'rules', 'deprecations' ) {
325 return(&mv_change($changed_version,"$t changed")) if $Stuff{"old:$t"} ne $Stuff{"new:$t"};
328 # Decl need separate treatment; they aren't in $Stuff
329 foreach $v (@decl_names) {
330 return(&mv_change($changed_version,"$v changed")) if $Decl{"old:$v"} ne $Decl{"new:$v"};
333 print STDERR "$Pgm: module version unchanged at $unchanged_version\n"
335 return($unchanged_version);
339 local($mv, $str) = @_;
341 print STDERR "$Pgm: module version changed to $mv; reason: $str\n"
346 sub printNewItemVersion {
347 local($hifile, $item, $mod_version, $show_hi_diffs) = @_;
348 local($idecl) = $Decl{"new:$item"};
351 if (! defined($Decl{"old:$item"})) { # Old decl doesn't exist
352 if ($show_hi_diffs) {print STDERR "new: $item\n";}
353 print $hifile "$mod_version "; # Use module version
355 } elsif (! defined($OldVersion{"$item"}) ) {
356 if ($show_hi_diffs) {print STDERR "$item: no old version?!\n";}
357 print $hifile "$mod_version "; # Use module version
359 } elsif ($idecl ne $Decl{"old:$item"}) { # Old decl differs from new decl
360 local($odecl) = $Decl{"old:$item"};
361 if ($show_hi_diffs) {print STDERR "changed: $item\nOld: $odecl", "New: $idecl";}
362 print $hifile "--old: ", $OldVersion{"$item"}, " $odecl"
363 if $Keep_HiDiffs; # show old in interface file
364 print $hifile "$mod_version "; # Use module version
366 } else { # Identical decls, so use old version number
367 #if ($show_hi_diffs) {print STDERR "$item: unchanged\n";}
368 print $hifile $OldVersion{"$item"}, " ";
375 # make "require"r happy...