[project @ 1997-11-25 13:55:30 by sof]
[ghc-hetmet.git] / ghc / driver / ghc-iface.lprl
1 %************************************************************************
2 %*                                                                      *
3 \section[Driver-iface-thing]{Interface-file handling}
4 %*                                                                      *
5 %************************************************************************
6
7 \begin{code}
8 %OldVersion = ();
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);
14
15
16
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*
21                                 # replace.
22           $going_interactive) = @_;
23
24     local($new_hi) = "$Tmp_prefix.hi-new";
25     local($show_hi_diffs) = $HiDiff_flag && ! $HiOnStdout && ! $going_interactive && -f $hifile_target;
26
27     print STDERR "*** New hi file follows...\n" if $Verbose;
28     system("$Cat $hsc_hi 1>&2") if $Verbose;
29
30     &constructNewHiFile($hsc_hi, *hifile_target, $new_hi, $show_hi_diffs);
31
32     # run diff if they asked for it
33     if ($show_hi_diffs) {
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
38         } else {
39             # strip out usages, *then* run diff
40             local($hi_before) = "$Tmp_prefix.hi-before";
41             local($hi_after)  = "$Tmp_prefix.hi-now";
42
43             &deUsagifyHi($hifile_target, $hi_before);
44             &deUsagifyHi($new_hi,        $hi_after);
45
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
48         }
49     }
50
51     # if we produced an interface file "no matter what",
52     # print what we got on stderr (ToDo: honor -ohi flag)
53     if ( $HiOnStdout ) {
54         system("$Cat $new_hi 1>&2"); # if $Verbose;
55     } else {
56         &run_something("$Cmp -s $hifile_target $new_hi || ( $Rm $hifile_target && $Cp $new_hi $hifile_target )",
57            "Replace .$HiSuffix file, if changed");
58     }
59 }
60
61 sub deUsagifyHi {
62     local($ifile,$ofile) = @_;
63
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");
66
67     # read up to _usages_ line
68     $_ = <OLDHIF>;
69     while ($_ ne '' && ! /^_usages_/) {
70         print NEWHIF $_ unless /^(_interface_ |\{-# GHC_PRAGMA)/;
71         $_ = <OLDHIF>;
72     }
73     if ( $_ ne '' ) {
74         # skip to next _<anything> line
75         $_ = <OLDHIF>;
76         while ($_ ne '' && ! /^_/) { $_ = <OLDHIF>; }
77
78         # print the rest
79         while ($_ ne '') {
80             print NEWHIF $_;
81             $_ = <OLDHIF>;
82         }
83     }
84
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");
87 }
88 \end{code}
89
90 \begin{code}
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
95           $show_hi_diffs) = @_;
96     local($hiname,$hidir);
97
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";
104         }
105     }
106     &readHiFile('old',$hifile_target) unless $HiHasBeenRead{'old'} == 1;
107
108     open(NEWHI, "> $new_hi") || &tidy_up_and_die(1,"Can't open $new_hi (write)\n");
109
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);
114     }
115
116     local($new_module_version) = &calcNewModuleVersion(@decl_names);
117     print NEWHI "_interface_ ", $ModuleName{'new'}, " $new_module_version\n";
118
119     if ( $Stuff{'new:instance_modules'} ) {
120         print NEWHI "_instance_modules_\n";
121         print NEWHI $Stuff{'new:instance_modules'};
122     }
123
124     print NEWHI "_usages_\n", $Stuff{'new:usages'} unless $Stuff{'new:usages'} eq '';
125
126     print NEWHI "_exports_\n";
127     print NEWHI $Stuff{'new:exports'};
128
129     if ( $Stuff{'new:fixities'} ) {
130         print NEWHI "_fixities_\n";
131         print NEWHI $Stuff{'new:fixities'};
132     }
133
134     if ( $Stuff{'new:instances'} ) {
135         print NEWHI "_instances_\n";
136         print NEWHI $Stuff{'new:instances'};
137     }
138
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
143     }
144
145     
146
147     close(NEWHI) || &tidy_up_and_die(1,"Failed writing to $new_hi\n");
148 }
149 \end{code}
150
151 \begin{code}
152 sub readHiFile {
153     local($mod,             # module to read; can be special tag 'old'
154                             # (old .hi file for module being compiled) or
155                             # 'new' (new proto-.hi file for...)
156           $hifile) = @_;    # actual file to read
157
158     # info about the old version of this module's interface
159     $HiExists{$mod}      = -1; # 1 <=> definitely exists; 0 <=> doesn't
160     $HiHasBeenRead{$mod} = 0;
161     $ModuleVersion{$mod} = 0;
162     $Stuff{"$mod:instance_modules"} = '';
163     $Stuff{"$mod:usages"}           = ''; # stuff glommed together
164     $Stuff{"$mod:exports"}          = '';
165     $Stuff{"$mod:fixities"}         = '';
166     $Stuff{"$mod:instances"}        = '';
167     $Stuff{"$mod:declarations"}     = '';
168
169     if (! -f $hifile) { # no pre-existing .hi file
170         $HiExists{$mod} = 0;
171         return();
172     }
173
174     open(HIFILE, "< $hifile") || &tidy_up_and_die(1,"Can't open $hifile (read)\n");
175     $HiExists{$mod} = 1;
176     local($now_in) = '';
177     hi_line: while (<HIFILE>) {
178         next if /^ *$/; # blank line
179         next if /\{-# GHC_PRAGMA INTERFACE VERSION 20 #-\}/;
180
181         # avoid pre-1.3 interfaces
182         #print STDERR "now_in:$now_in:$_";
183         if ( /\{-# GHC_PRAGMA INTERFACE VERSION . #-\}/ ) {
184             $HiExists{$mod} = 0;
185             last hi_line;
186         }
187
188         if ( /^_interface_ ([A-Z]\S*) (\d+)/ ) {
189             $ModuleName{$mod}    = $1; # used to decide name of interface file.
190             $ModuleVersion{$mod} = $2;
191
192         } elsif ( /^_interface_ ([A-Z]\S*)/ && $mod eq 'new' ) { # special case: no version
193             $ModuleName{'new'} = $1;
194
195         } elsif ( /^_([a-z_]+)_$/ ) {
196             $now_in = $1;
197
198         } elsif ( $now_in eq 'usages' && /^(\S+)\s+(!\s+)?(\d+)\s+::(.*)/ ) {
199             $Stuff{"$mod:usages"} .= $_; # save the whole thing
200
201
202         } elsif ( $now_in =~ /^(exports|instance_modules|instances|fixities)$/ ) {
203             $Stuff{"$mod:$1"} .= $_; # just save it up
204
205         } elsif ( /^--.*/ ) { # silently ignore comment lines.
206             ;
207         } elsif ( $now_in eq 'declarations' ) { # relatively special treatment needed...
208         # We're in a declaration
209
210         # Strip off the initial version number, if any
211            if ( /^([0-9]+) (.*\n)/ ) {
212                 # The "\n" is because we need to keep the newline at the end, so that
213                 # it looks the same as if there's no version number and this if statement
214                 # doesn't fire.
215
216                 # So there's an initial version number
217                 $version = $1;
218                 $_ = $2;
219            }
220         
221             if ( /^(\S+)\s+_:_\s+/ ) {
222                 $current_name = $1;
223                 $Decl{"$mod:$current_name"} = $_;
224                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
225
226             } elsif ( /^type\s+(\S+)/ ) {
227                 $current_name = $1;
228                 $Decl{"$mod:$current_name"} = $_;
229                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
230
231             } elsif ( /^(newtype|data)\s+(.*\s+=>\s+)?(\S+)\s+/ ) {
232                 $current_name = $3;
233                 $Decl{"$mod:$current_name"} = $_;
234                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
235
236             } elsif ( /^class\s+(\{[^{}]*\}\s+=>\s+)?(\S+)\s+/ ) {
237                 # must be wary of => bit matching after "where"...
238                 # ..hence the [^{}] part
239                 # NB: a class decl may not have a where part at all
240                 $current_name = $2;
241                 $Decl{"$mod:$current_name"} = $_;
242                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
243
244             } else { # Continuation line
245                 $Decl{"$mod:$current_name"} .= $_
246             }
247
248         } else {
249             print STDERR "$Pgm:junk old iface line?:section:$now_in:$_";
250         }
251     }
252
253 #   foreach $i ( sort (keys %Decl)) {
254 #       print STDERR "$i: ",$Decl{$i}, "\n";
255 #   }
256
257     close(HIFILE) || &tidy_up_and_die(1,"Failed reading from $hifile\n");
258     $HiHasBeenRead{$mod} = 1;
259 }
260 \end{code}
261
262 \begin{code}
263 sub calcNewModuleVersion {
264     local (@decl_names) = @_;
265
266     return(&mv_change(1,'no old .hi file')) if $HiExists{'old'} == 0;
267         # could use "time()" as initial version; if a module existed, then was deleted,
268         # then comes back, we don't want the resurrected one to have an
269         # lower version number than the original (in case there are any
270         # lingering references to the original in other .hi files).
271
272     local($unchanged_version) = $ModuleVersion{'old'}; # will return one of these two
273     local($changed_version)   = $unchanged_version + 1;
274
275 # This statement is curious; it is subsumed by the foreach!
276 #    return(&mv_change($changed_version,'usages changed')) if $Stuff{'old:usages'} ne $Stuff{'new:usages'};
277
278     foreach $t ( 'usages' , 'exports', 'instance_modules', 'instances', 'fixities' ) {
279         return(&mv_change($changed_version,"$t changed")) if $Stuff{"old:$t"} ne $Stuff{"new:$t"};
280     }
281
282 # Decl need separate treatment; they aren't in $Stuff
283     foreach $v (@decl_names) {
284         return(&mv_change($changed_version,"$v changed")) if $Decl{"old:$v"} ne $Decl{"new:$v"};
285     }
286     
287     print STDERR "Module version unchanged at $unchanged_version\n";
288     return($unchanged_version);
289 }
290
291 sub mv_change {
292     local($mv, $str) = @_;
293
294     print STDERR "$Pgm: module version changed to $mv; reason: $str\n";
295     return($mv);
296 }
297
298 sub printNewItemVersion {
299     local($hifile, $item, $mod_version, $show_hi_diffs) = @_;
300     local($idecl) = $Decl{"new:$item"};
301     
302
303     if (! defined($Decl{"old:$item"})) {        # Old decl doesn't exist
304         if ($show_hi_diffs) {print STDERR "new: $item\n";}
305         print $hifile  "$mod_version ";         # Use module version
306
307     } elsif (! defined($OldVersion{"$item"}) ) {
308         if ($show_hi_diffs) {print STDERR "$item: no old version?!\n";}
309         print $hifile  "$mod_version ";                 # Use module version
310
311     } elsif ($idecl ne $Decl{"old:$item"})  {   # Old decl differs from new decl
312         local($odecl) = $Decl{"old:$item"};
313         if ($show_hi_diffs) {print STDERR "changed: $item\nOld: $odecl", "New: $idecl";}
314         print $hifile  "--old: ", $OldVersion{"$item"}, " $odecl" 
315                         if $Keep_HiDiffs;         # show old in interface file
316         print $hifile  "$mod_version ";           # Use module version
317
318     } else {                                    # Identical decls, so use old version number
319         #if ($show_hi_diffs) {print STDERR "$item: unchanged\n";}
320         print $hifile  $OldVersion{"$item"}, " ";
321     }
322     return;
323 }
324 \end{code}
325
326 \begin{code}
327 sub findHiChanges {
328     local($hsc_hi,              # The iface info produced by hsc.
329           $hifile_target) = @_; # Pre-existing .hi filename (if it exists)
330 }
331 \end{code}
332
333 \begin{code}
334 # make "require"r happy...
335 1;
336 \end{code}