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 (ToDo: honor -ohi flag)
54 system("$Cat $new_hi 1>&2"); # if $Verbose;
56 &run_something("$Cmp -s $hifile_target $new_hi || ( $Rm $hifile_target && $Cp $new_hi $hifile_target )",
57 "Replace .$HiSuffix file, if changed");
62 local($ifile,$ofile) = @_;
64 open(OLDHIF, "< $ifile") || &tidy_up_and_die(1,"Can't open $ifile (read)\n");
65 open(NEWHIF, "> $ofile") || &tidy_up_and_die(1,"Can't open $ofile (write)\n");
67 # read up to _usages_ line
69 while ($_ ne '' && ! /^_usages_/) {
70 print NEWHIF $_ unless /^(_interface_ |\{-# GHC_PRAGMA)/;
74 # skip to next _<anything> line
76 while ($_ ne '' && ! /^_/) { $_ = <OLDHIF>; }
85 close(OLDHIF) || &tidy_up_and_die(1,"Failed reading from $ifile\n");
86 close(NEWHIF) || &tidy_up_and_die(1,"Failed writing to $ofile\n");
91 sub constructNewHiFile {
92 local($hsc_hi, # The iface info produced by hsc.
93 *hifile_target, # Pre-existing .hi filename (if it exists)
94 $new_hi, # Filename for new one
96 local($hiname,$hidir);
98 &readHiFile('new',$hsc_hi) unless $HiHasBeenRead{'new'} == 1;
99 if ($Specific_hi_file eq '') { # -ohi is used even if module name != stem of filename.
100 ($hiname = $hifile_target) =~ s/([^\/]*\/)*(.*)\.$HiSuffix/$2/;
101 if ($ModuleName{'new'} ne $hiname) {
102 ($hidir = $hifile_target) =~ s/([^\/]*\/)*.*\.$HiSuffix/$1/;
103 $hifile_target = $hidir . $ModuleName{'new'} . ".$HiSuffix";
106 &readHiFile('old',$hifile_target) unless $HiHasBeenRead{'old'} == 1;
108 open(NEWHI, "> $new_hi") || &tidy_up_and_die(1,"Can't open $new_hi (write)\n");
110 local(@decl_names) = (); # Entities in _declarations_ section of new module
111 foreach $v (sort (keys %Decl)) {
112 next unless $v =~ /^new:(.*$)/;
113 push(@decl_names,$1);
116 local($new_module_version) = &calcNewModuleVersion(@decl_names);
117 print NEWHI "_interface_ ", $ModuleName{'new'}, " $new_module_version $GhcVersionInfo\n";
119 if ( $Stuff{'new:instance_modules'} ) {
120 print NEWHI "_instance_modules_\n";
121 print NEWHI $Stuff{'new:instance_modules'};
124 print NEWHI "_usages_\n", $Stuff{'new:usages'} unless $Stuff{'new:usages'} eq '';
126 print NEWHI "_exports_\n";
127 print NEWHI $Stuff{'new:exports'};
129 if ( $Stuff{'new:fixities'} ) {
130 print NEWHI "_fixities_\n";
131 print NEWHI $Stuff{'new:fixities'};
134 if ( $Stuff{'new:instances'} ) {
135 print NEWHI "_instances_\n";
136 print NEWHI $Stuff{'new:instances'};
139 print NEWHI "_declarations_\n";
140 foreach $v (@decl_names) {
141 &printNewItemVersion(NEWHI, $v, $new_module_version, $show_hi_diffs); # Print new version number
142 print NEWHI $Decl{"new:$v"}; # Print the new decl itself
147 close(NEWHI) || &tidy_up_and_die(1,"Failed writing to $new_hi\n");
151 Read the .hi file made by the compiler, or the old one.
152 All the declarations in the file are stored in
156 where $mod is "new" or "old", depending on whether it's the new or old
157 .hi file that's being read.
161 for tycons T "type T" or "data T"
162 for classes C "class C"
167 local($mod, # module to read; can be special tag 'old'
168 # (old .hi file for module being compiled) or
169 # 'new' (new proto-.hi file for...)
170 $hifile) = @_; # actual file to read
172 # info about the old version of this module's interface
173 $HiExists{$mod} = -1; # 1 <=> definitely exists; 0 <=> doesn't
174 $HiHasBeenRead{$mod} = 0;
175 $ModuleVersion{$mod} = 0;
176 $Stuff{"$mod:instance_modules"} = '';
177 $Stuff{"$mod:usages"} = ''; # stuff glommed together
178 $Stuff{"$mod:exports"} = '';
179 $Stuff{"$mod:fixities"} = '';
180 $Stuff{"$mod:instances"} = '';
181 $Stuff{"$mod:declarations"} = '';
183 if (! -f $hifile) { # no pre-existing .hi file
188 open(HIFILE, "< $hifile") || &tidy_up_and_die(1,"Can't open $hifile (read)\n");
191 hi_line: while (<HIFILE>) {
192 next if /^ *$/; # blank line
193 next if /\{-# GHC_PRAGMA INTERFACE VERSION 20 #-\}/;
195 # avoid pre-1.3 interfaces
196 #print STDERR "now_in:$now_in:$_";
197 if ( /\{-# GHC_PRAGMA INTERFACE VERSION . #-\}/ ) {
202 if ( /^_interface_ ([A-Z]\S*) (\d+)/ && $mod ne 'new' ) {
203 $ModuleName{$mod} = $1; # used to decide name of interface file.
204 $ModuleVersion{$mod} = $2;
206 } elsif ( /^_interface_ ([A-Z]\S*) (\d+)/ && $mod eq 'new' ) { # special case: no version
207 $ModuleName{'new'} = $1;
209 } elsif ( /^_([a-z_]+)_$/ ) {
212 } elsif ( $now_in eq 'usages' && /^(\S+)\s+(!\s+)?(\d+)\s+::(.*)/ ) {
213 $Stuff{"$mod:usages"} .= $_; # save the whole thing
216 } elsif ( $now_in =~ /^(exports|instance_modules|instances|fixities)$/ ) {
217 $Stuff{"$mod:$1"} .= $_; # just save it up
219 } elsif ( /^--.*/ ) { # silently ignore comment lines.
221 } elsif ( $now_in eq 'declarations' ) { # relatively special treatment needed...
222 # We're in a declaration
224 # Strip off the initial version number, if any
225 if ( /^([0-9]+) (.*\n)/ ) {
226 # The "\n" is because we need to keep the newline at the end, so that
227 # it looks the same as if there's no version number and this if statement
230 # So there's an initial version number
235 if ( /^(\S+)\s+_:_\s+/ ) {
238 $Decl{"$mod:$current_name"} = $_;
239 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
241 } elsif ( /^type\s+(\S+)/ ) {
243 $current_name = "type $1";
244 $Decl{"$mod:$current_name"} = $_;
245 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
247 } elsif ( /^(newtype|data)\s+(.*\s+=>\s+)?(\S+)\s+/ ) {
249 $current_name = "data $3";
250 $Decl{"$mod:$current_name"} = $_;
251 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
253 } elsif ( /^class\s+(\{[^{}]*\}\s+=>\s+)?(\S+)\s+/ ) {
255 # must be wary of => bit matching after "where"...
256 # ..hence the [^{}] part
257 # NB: a class decl may not have a where part at all
258 $current_name = "class $2";
259 $Decl{"$mod:$current_name"} = $_;
260 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
262 } else { # Continuation line
263 $Decl{"$mod:$current_name"} .= $_
267 print STDERR "$Pgm:junk old iface line?:section:$now_in:$_";
271 # foreach $i ( sort (keys %Decl)) {
272 # print STDERR "$i: ",$Decl{$i}, "\n";
275 close(HIFILE) || &tidy_up_and_die(1,"Failed reading from $hifile\n");
276 $HiHasBeenRead{$mod} = 1;
281 sub calcNewModuleVersion {
282 local (@decl_names) = @_;
284 return(&mv_change(1,'no old .hi file')) if $HiExists{'old'} == 0;
285 # could use "time()" as initial version; if a module existed, then was deleted,
286 # then comes back, we don't want the resurrected one to have an
287 # lower version number than the original (in case there are any
288 # lingering references to the original in other .hi files).
290 local($unchanged_version) = $ModuleVersion{'old'}; # will return one of these two
291 local($changed_version) = $unchanged_version + 1;
293 # This statement is curious; it is subsumed by the foreach!
294 # return(&mv_change($changed_version,'usages changed')) if $Stuff{'old:usages'} ne $Stuff{'new:usages'};
296 foreach $t ( 'usages' , 'exports', 'instance_modules', 'instances', 'fixities' ) {
297 return(&mv_change($changed_version,"$t changed")) if $Stuff{"old:$t"} ne $Stuff{"new:$t"};
300 # Decl need separate treatment; they aren't in $Stuff
301 foreach $v (@decl_names) {
302 return(&mv_change($changed_version,"$v changed")) if $Decl{"old:$v"} ne $Decl{"new:$v"};
305 print STDERR "$Pgm: module version unchanged at $unchanged_version\n";
306 return($unchanged_version);
310 local($mv, $str) = @_;
312 print STDERR "$Pgm: module version changed to $mv; reason: $str\n";
316 sub printNewItemVersion {
317 local($hifile, $item, $mod_version, $show_hi_diffs) = @_;
318 local($idecl) = $Decl{"new:$item"};
321 if (! defined($Decl{"old:$item"})) { # Old decl doesn't exist
322 if ($show_hi_diffs) {print STDERR "new: $item\n";}
323 print $hifile "$mod_version "; # Use module version
325 } elsif (! defined($OldVersion{"$item"}) ) {
326 if ($show_hi_diffs) {print STDERR "$item: no old version?!\n";}
327 print $hifile "$mod_version "; # Use module version
329 } elsif ($idecl ne $Decl{"old:$item"}) { # Old decl differs from new decl
330 local($odecl) = $Decl{"old:$item"};
331 if ($show_hi_diffs) {print STDERR "changed: $item\nOld: $odecl", "New: $idecl";}
332 print $hifile "--old: ", $OldVersion{"$item"}, " $odecl"
333 if $Keep_HiDiffs; # show old in interface file
334 print $hifile "$mod_version "; # Use module version
336 } else { # Identical decls, so use old version number
337 #if ($show_hi_diffs) {print STDERR "$item: unchanged\n";}
338 print $hifile $OldVersion{"$item"}, " ";
346 local($hsc_hi, # The iface info produced by hsc.
347 $hifile_target) = @_; # Pre-existing .hi filename (if it exists)
352 # make "require"r happy...