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";
26 # print STDERR `$Cat $hsc_hi`;
28 &constructNewHiFile($hsc_hi, $hifile_target, $new_hi);
30 # run diff if they asked for it
31 if ($HiDiff_flag && ! $HiOnStdout && ! $going_interactive && -f $hifile_target) {
32 if ( $HiDiff_flag eq 'usages' ) {
33 # lots of near-useless info; but if you want it...
34 &run_something("$Cmp -s $hifile_target $new_hi || $Diff $hifile_target $new_hi 1>&2 || exit 0",
35 "Diff'ing old and new .$HiSuffix files"); # NB: to stderr
37 # strip out usages, *then* run diff
38 local($hi_before) = "$Tmp_prefix.hi-before";
39 local($hi_after) = "$Tmp_prefix.hi-now";
41 &deUsagifyHi($hifile_target, $hi_before);
42 &deUsagifyHi($new_hi, $hi_after);
44 &run_something("$Cmp -s $hi_before $hi_after || $Diff $hi_before $hi_after 1>&2 || exit 0",
45 "Diff'ing old and new .$HiSuffix files"); # NB: to stderr
49 # if we produced an interface file "no matter what",
50 # print what we got on stderr (ToDo: honor -ohi flag)
52 print STDERR `$Cat $new_hi`;
54 &run_something("$Cmp -s $hifile_target $new_hi || ( $Rm $hifile_target && $Cp $new_hi $hifile_target )",
55 "Replace .$HiSuffix file, if changed");
60 local($ifile,$ofile) = @_;
62 open(OLDHIF, "< $ifile") || &tidy_up_and_die(1,"Can't open $ifile (read)\n");
63 open(NEWHIF, "> $ofile") || &tidy_up_and_die(1,"Can't open $ofile (write)\n");
65 # read up to _usages_ line
67 while ($_ ne '' && ! /^_usages_/) {
68 print NEWHIF $_ unless /^(_interface_ |\{-# GHC_PRAGMA)/;
72 # skip to next _<anything> line
74 while ($_ ne '' && ! /^_/) { $_ = <OLDHIF>; }
83 close(OLDHIF) || &tidy_up_and_die(1,"Failed reading from $ifile\n");
84 close(NEWHIF) || &tidy_up_and_die(1,"Failed writing to $ofile\n");
89 sub constructNewHiFile {
90 local($hsc_hi, # The iface info produced by hsc.
91 $hifile_target, # Pre-existing .hi filename (if it exists)
92 $new_hi) = @_; # Filename for new one
94 &readHiFile('old',$hifile_target) unless $HiHasBeenRead{'old'} == 1;
95 &readHiFile('new',$hsc_hi) unless $HiHasBeenRead{'new'} == 1;
97 open(NEWHI, "> $new_hi") || &tidy_up_and_die(1,"Can't open $new_hi (write)\n");
99 local(@decl_names) = (); # Entities in _declarations_ section of new module
100 foreach $v (sort (keys %Decl)) {
101 next unless $v =~ /^new:(.*$)/;
102 push(@decl_names,$1);
105 local($new_module_version) = &calcNewModuleVersion(@decl_names);
106 print NEWHI "_interface_ ", $ModuleName{'new'}, " $new_module_version\n";
108 if ( $Stuff{'new:instance_modules'} ) {
109 print NEWHI "_instance_modules_\n";
110 print NEWHI $Stuff{'new:instance_modules'};
113 print NEWHI "_usages_\n", $Stuff{'new:usages'} unless $Stuff{'new:usages'} eq '';
115 print NEWHI "_exports_\n";
116 print NEWHI $Stuff{'new:exports'};
118 if ( $Stuff{'new:fixities'} ) {
119 print NEWHI "_fixities_\n";
120 print NEWHI $Stuff{'new:fixities'};
123 if ( $Stuff{'new:instances'} ) {
124 print NEWHI "_instances_\n";
125 print NEWHI $Stuff{'new:instances'};
128 print NEWHI "_declarations_\n";
129 foreach $v (@decl_names) {
130 &printNewItemVersion(NEWHI, $v, $new_module_version); # Print new version number
131 print NEWHI $Decl{"new:$v"}; # Print the new decl itself
136 close(NEWHI) || &tidy_up_and_die(1,"Failed writing to $new_hi\n");
142 local($mod, # module to read; can be special tag 'old'
143 # (old .hi file for module being compiled) or
144 # 'new' (new proto-.hi file for...)
145 $hifile) = @_; # actual file to read
147 # info about the old version of this module's interface
148 $HiExists{$mod} = -1; # 1 <=> definitely exists; 0 <=> doesn't
149 $HiHasBeenRead{$mod} = 0;
150 $ModuleVersion{$mod} = 0;
151 $Stuff{"$mod:instance_modules"} = '';
152 $Stuff{"$mod:usages"} = ''; # stuff glommed together
153 $Stuff{"$mod:exports"} = '';
154 $Stuff{"$mod:fixities"} = '';
155 $Stuff{"$mod:instances"} = '';
156 $Stuff{"$mod:declarations"} = '';
158 if (! -f $hifile) { # no pre-existing .hi file
163 open(HIFILE, "< $hifile") || &tidy_up_and_die(1,"Can't open $hifile (read)\n");
166 hi_line: while (<HIFILE>) {
167 next if /^ *$/; # blank line
168 next if /\{-# GHC_PRAGMA INTERFACE VERSION 20 #-\}/;
170 # avoid pre-1.3 interfaces
171 #print STDERR "now_in:$now_in:$_";
172 if ( /\{-# GHC_PRAGMA INTERFACE VERSION . #-\}/ ) {
177 if ( /^_interface_ ([A-Z]\S*) (\d+)/ ) {
178 $ModuleName{$mod} = $1; # not sure this is used much...
179 $ModuleVersion{$mod} = $2;
181 } elsif ( /^_interface_ ([A-Z]\S*)/ && $mod eq 'new' ) { # special case: no version
182 $ModuleName{'new'} = $1;
184 } elsif ( /^_([a-z_]+)_$/ ) {
187 } elsif ( $now_in eq 'usages' && /^(\S+)\s+(\d+)\s+:: (.*)/ ) {
188 $Stuff{"$mod:usages"} .= $_; # save the whole thing
191 } elsif ( $now_in =~ /^(exports|instance_modules|instances|fixities)$/ ) {
192 $Stuff{"$mod:$1"} .= $_; # just save it up
194 } elsif ( $now_in eq 'declarations' ) { # relatively special treatment needed...
195 # We're in a declaration
197 # Strip off the initial version number, if any
198 if ( /^([0-9]+) (.*\n)/ ) {
199 # The "\n" is because we need to keep the newline at the end, so that
200 # it looks the same as if there's no version number and this if statement
203 # So there's an initial version number
208 if ( /^(\S+)\s+::\s+/ ) {
210 $Decl{"$mod:$current_name"} = $_;
211 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
213 } elsif ( /^type\s+(\S+)/ ) {
215 $Decl{"$mod:$current_name"} = $_;
216 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
218 } elsif ( /^(newtype|data)\s+(.*\s+=>\s+)?(\S+)\s+/ ) {
220 $Decl{"$mod:$current_name"} = $_;
221 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
223 } elsif ( /class\s+(.*\s+=>\s+)?(\S+)\s+.*where\s+\{.*\};/ ) {
224 # must be wary of => bit matching after "where"...
226 $Decl{"$mod:$current_name"} = $_;
227 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
229 } elsif ( /class\s+(.*\s+=>\s+)?(\S+)\s+/ ) {
231 $Decl{"$mod:$current_name"} = $_;
232 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
234 } else { # Continuation line
235 $Decl{"$mod:$current_name"} .= $_
239 print STDERR "$Pgm:junk old iface line?:section:$now_in:$_";
243 # foreach $i ( sort (keys %Decl)) {
244 # print STDERR "$i: ",$Decl{$i}, "\n";
247 close(HIFILE) || &tidy_up_and_die(1,"Failed reading from $hifile\n");
248 $HiHasBeenRead{$mod} = 1;
253 sub calcNewModuleVersion {
254 local (@decl_names) = @_;
256 return(&mv_change(1,'no old .hi file')) if $HiExists{'old'} == 0;
257 # could use "time()" as initial version; if a module existed, then was deleted,
258 # then comes back, we don't want the resurrected one to have an
259 # lower version number than the original (in case there are any
260 # lingering references to the original in other .hi files).
262 local($unchanged_version) = $ModuleVersion{'old'}; # will return one of these two
263 local($changed_version) = $unchanged_version + 1;
265 # This statement is curious; it is subsumed by the foreach!
266 # return(&mv_change($changed_version,'usages changed')) if $Stuff{'old:usages'} ne $Stuff{'new:usages'};
268 foreach $t ( 'usages' , 'exports', 'instance_modules', 'instances', 'fixities' ) {
269 return(&mv_change($changed_version,"$t changed")) if $Stuff{"old:$t"} ne $Stuff{"new:$t"};
272 # Decl need separate treatment; they aren't in $Stuff
273 foreach $v (@decl_names) {
274 return(&mv_change($changed_version,"$v changed")) if $Decl{"old:$v"} ne $Decl{"new:$v"};
277 print STDERR "Module version unchanged at $unchanged_version\n";
278 return($unchanged_version);
282 local($mv, $str) = @_;
284 print STDERR "$Pgm: module version changed to $mv; reason: $str\n";
288 sub printNewItemVersion {
289 local($hifile, $item, $mod_version) = @_;
290 local($idecl) = $Decl{"new:$item"};
292 if (! defined($Decl{"old:$item"})) { # Old decl doesn't exist
293 print STDERR "new: $item\n";
294 print $hifile "$mod_version "; # Use module version
296 } elsif ($idecl ne $Decl{"old:$item"}) { # Old decl differs from new decl
297 local($odecl) = $Decl{"old:$item"};
298 # print STDERR "changed: $item\nOld: $odecl\nNew: $idecl\n";
299 print $hifile "$mod_version "; # Use module version
301 } elsif (! defined($OldVersion{"$item"}) ) {
302 print STDERR "$item: no old version?!\n";
303 print $hifile "$mod_version "; # Use module version
305 } else { # Identical decls, so use old version number
306 print STDERR "$item: unchanged\n";
307 print $hifile $OldVersion{"$item"}, " ";
315 local($hsc_hi, # The iface info produced by hsc.
316 $hifile_target) = @_; # Pre-existing .hi filename (if it exists)
321 # make "require"r happy...