3fae1d10dd3ac050c3830c95ead4ef092c762422
[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 %HiSections = ();
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.
53     if ( $HiOnStdout ) {
54         if ( $HiWith ne '' ) {
55             # output some of the sections
56             local($hi_after)  = "$Tmp_prefix.hi-now";
57
58             foreach $hi ( split(' ',$HiWith) ) { 
59                 $HiSection{$hi} = 1; 
60             }
61             &hiSectionsOnly($new_hi, $hi_after);
62
63             system("$Cat $hi_after 1>&2 ; $Rm $hi_after; ");
64         } else {
65             system("$Cat $new_hi 1>&2");
66         }
67     } else {
68         &run_something("$Cmp -s $hifile_target $new_hi || ( $Rm $hifile_target && $Cp $new_hi $hifile_target )",
69            "Replace .$HiSuffix file, if changed");
70     }
71 }
72
73 sub deUsagifyHi {
74     local($ifile,$ofile) = @_;
75
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");
78
79     # read up to _usages_ line
80     $_ = <OLDHIF>;
81     while ($_ ne '') {
82         print NEWHIF $_ unless /^(__interface|import)/;
83         $_ = <OLDHIF>;
84     }
85
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");
88 }
89 \end{code}
90
91 \begin{code}
92 sub hiSectionsOnly {
93     local($ifile,$ofile) = @_;
94
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");
97
98     # read up to _usages_ line
99     $_ = <OLDHIF>;
100     while ($_ ne '' ) {
101         if ( /^__export/ && $HiSection {'exports'}           ||
102              /^import /  && $HiSection {'imports'}           ||
103              /^\d+ ([^ ]+ :: |type |data |class |newtype )/  && $HiSection {'declarations'} ||
104              /^instance / && $HiSection {'instances'} ) {
105              print NEWHIF $_;
106              $_ = <OLDHIF>;
107         } else {
108           $_ = <OLDHIF>;
109         }
110     }
111
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");
114 }
115 \end{code}
116
117 \begin{code}
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);
125     
126     &readHiFile('new',$hsc_hi)        unless $HiHasBeenRead{'new'} == 1;
127
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
130     # interface file.
131     $mod_name_dec = $ModuleName{'new'};
132     $mod_name_dec =~ s/zz/z/g;
133     $mod_name_dec =~ s/ZZ/Z/g;
134
135     if ($Specific_hi_file eq '') {  # -ohi is used even if  module name != stem of filename.
136         ($hiname = $hifile_target) = $1 if  $hifile_target =~ /\/?([^\/\.]+)\.$HiSuffix/;
137         if ( $mod_name_dec ne $hiname ) {
138           ($hidir  = $hifile_target) =~ s/(.*)$hiname\.$HiSuffix/$1/;
139           $hifile_target = $hidir . $mod_name_dec . ".$HiSuffix";
140         }
141     }
142     &readHiFile('old',$hifile_target) unless $HiHasBeenRead{'old'} == 1;
143
144     open(NEWHI, "> $new_hi") || &tidy_up_and_die(1,"Can't open $new_hi (write)\n");
145
146     local(@decl_names) = ();    # Declarations in new module
147     foreach $v (sort (keys %Decl)) {
148         next unless $v =~ /^new:(.*$)/;
149         push(@decl_names,$1);
150     }
151
152     local($new_module_version) = &calcNewModuleVersion(@decl_names);
153     print NEWHI "__interface ", $ModuleName{'new'}, " $new_module_version $ProjectVersionInt where\n";
154
155     print NEWHI $Stuff{'new:usages'} unless $Stuff{'new:usages'} eq '';
156     print NEWHI $Stuff{'new:instance_modules'} unless $Stuff{'new:instance_modules'} eq '';
157     print NEWHI $Stuff{'new:exports'};
158     print NEWHI $Stuff{'new:instances'} unless $Stuff{'new:instances'} eq '';
159
160     foreach $v (@decl_names) {
161         &printNewItemVersion(NEWHI, $v, $new_module_version, $show_hi_diffs);           # Print new version number
162         print NEWHI $Decl{"new:$v"};            # Print the new decl itself
163     }
164
165     close(NEWHI) || &tidy_up_and_die(1,"Failed writing to $new_hi\n");
166 }
167 \end{code}
168
169 Read the .hi file made by the compiler, or the old one.
170 All the declarations in the file are stored in
171
172         $Decl{"$mod:$v"}
173
174 where $mod is "new" or "old", depending on whether it's the new or old
175         .hi file that's being read.
176
177 and $v is
178         for values v    "v"
179         for tycons T    "type T" or "data T"
180         for classes C   "class C"
181
182
183 \begin{code}
184 sub readHiFile {
185     local($mod,             # module to read; can be special tag 'old'
186                             # (old .hi file for module being compiled) or
187                             # 'new' (new proto-.hi file for...)
188           $hifile) = @_;    # actual file to read
189
190     # info about the old version of this module's interface
191     $HiExists{$mod}      = -1; # 1 <=> definitely exists; 0 <=> doesn't
192     $HiHasBeenRead{$mod} = 0;
193     $ModuleVersion{$mod} = 0;
194     $Stuff{"$mod:instance_modules"} = '';
195     $Stuff{"$mod:usages"}           = ''; # stuff glommed together
196     $Stuff{"$mod:exports"}          = '';
197     $Stuff{"$mod:instances"}        = '';
198     $Stuff{"$mod:declarations"}     = '';
199
200     if (! -f $hifile) { # no pre-existing .hi file
201         $HiExists{$mod} = 0;
202         return();
203     }
204
205     open(HIFILE, "< $hifile") || &tidy_up_and_die(1,"Can't open $hifile (read)\n");
206     $HiExists{$mod} = 1;
207     hi_line: while (<HIFILE>) {
208         next if /^ *$/; # blank line
209
210         if ( /^__interface ([A-Z]\S*) (\d+)/ ) {
211             if ( $mod ne 'new' ) {
212                 $ModuleVersion{$mod} = $2;
213             }
214             $ModuleName{$mod}    = $1; # used to decide name of iface file.
215
216         } elsif ( /^import / ) {
217             $Stuff{"$mod:usages"} .= $_; # save the whole thing
218
219         } elsif ( /^__instimport/ ) {
220             $Stuff{"$mod:instance_modules"} .= $_;
221
222         } elsif ( /^__export/ ) {
223             $Stuff{"$mod:exports"} .= $_;
224
225         } elsif ( /^instance / ) {
226             $Stuff{"$mod:instances"} .= $_;
227
228         } elsif ( /^-[-]+ .*/ ) { # silently ignore comment lines.
229             ;
230         } else {  # We're in a declaration
231
232         # Strip off the initial version number, if any
233            if ( /^([0-9]+)\s+(.*\n)/ ) {
234
235                 # The "\n" is because we need to keep the newline at
236                 # the end, so that it looks the same as if there's no version 
237                 # number and this if statement doesn't fire.
238
239                 # So there's an initial version number
240                 $version = $1;
241                 $_ = $2;
242            }
243
244            if ( /^type\s+(\S+)/ ) {             
245                         # Type declaration      
246                 $current_name = "type $1";
247                 $Decl{"$mod:$current_name"} = $_;
248                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
249
250            } elsif ( /^(newtype|data)\s+(.*\s+=>\s+)?(\S+)\s+/ ) {
251                         # Data declaration      
252                 $current_name = "data $3";
253                 $Decl{"$mod:$current_name"} = $_;
254                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
255
256            } elsif ( /^class\s+(\{[^{}]*\}\s+=>\s+)?(\S+)\s+/ ) {
257                         # Class declaration     
258                 # must be wary of => bit matching after "where"...
259                 # ..hence the [^{}] part
260                 # NB: a class decl may not have a where part at all
261                 $current_name = "class $2";
262                 $Decl{"$mod:$current_name"} = $_;
263                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
264
265            } elsif ( /^infix(r|l)?\s+[0-9]\s+(\S+)/ ) {
266                     # fixity declaration
267                 $current_name = "fixity $2";
268                 $Decl{"$mod:$current_name"} = $_;
269                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
270
271            } elsif ( /^(\S+)\s+::\s+/ ) {
272                         # Value declaration
273                 $current_name = $1;
274                 $Decl{"$mod:$current_name"} = $_;
275                 if ($mod eq "old") { $OldVersion{$current_name} = $version; }
276
277             } else { # Continuation line
278                 print STDERR "$Pgm:junk old iface line?:$_";
279                 # $Decl{"$mod:$current_name"} .= $_
280             }
281
282        }
283     }
284
285     close(HIFILE) || &tidy_up_and_die(1,"Failed reading from $hifile\n");
286     $HiHasBeenRead{$mod} = 1;
287 }
288 \end{code}
289
290 \begin{code}
291 sub calcNewModuleVersion {
292     local (@decl_names) = @_;
293
294     return(&mv_change(1,'no old .hi file')) if $HiExists{'old'} == 0;
295         # could use "time()" as initial version; if a module existed, then was deleted,
296         # then comes back, we don't want the resurrected one to have an
297         # lower version number than the original (in case there are any
298         # lingering references to the original in other .hi files).
299
300     local($unchanged_version) = $ModuleVersion{'old'}; # will return one of these two
301     local($changed_version)   = $unchanged_version + 1;
302
303     foreach $t ( 'usages' , 'exports', 'instance_modules', 'instances', 'fixities' ) {
304         return(&mv_change($changed_version,"$t changed")) if $Stuff{"old:$t"} ne $Stuff{"new:$t"};
305     }
306
307 # Decl need separate treatment; they aren't in $Stuff
308     foreach $v (@decl_names) {
309         return(&mv_change($changed_version,"$v changed")) if $Decl{"old:$v"} ne $Decl{"new:$v"};
310     }
311     
312     print STDERR "$Pgm: module version unchanged at $unchanged_version\n";
313     return($unchanged_version);
314 }
315
316 sub mv_change {
317     local($mv, $str) = @_;
318
319     print STDERR "$Pgm: module version changed to $mv; reason: $str\n";
320     return($mv);
321 }
322
323 sub printNewItemVersion {
324     local($hifile, $item, $mod_version, $show_hi_diffs) = @_;
325     local($idecl) = $Decl{"new:$item"};
326     
327
328     if (! defined($Decl{"old:$item"})) {        # Old decl doesn't exist
329         if ($show_hi_diffs) {print STDERR "new: $item\n";}
330         print $hifile  "$mod_version ";         # Use module version
331
332     } elsif (! defined($OldVersion{"$item"}) ) {
333         if ($show_hi_diffs) {print STDERR "$item: no old version?!\n";}
334         print $hifile  "$mod_version ";                 # Use module version
335
336     } elsif ($idecl ne $Decl{"old:$item"})  {   # Old decl differs from new decl
337         local($odecl) = $Decl{"old:$item"};
338         if ($show_hi_diffs) {print STDERR "changed: $item\nOld: $odecl", "New: $idecl";}
339         print $hifile  "--old: ", $OldVersion{"$item"}, " $odecl" 
340                         if $Keep_HiDiffs;         # show old in interface file
341         print $hifile  "$mod_version ";           # Use module version
342
343     } else {                                    # Identical decls, so use old version number
344         #if ($show_hi_diffs) {print STDERR "$item: unchanged\n";}
345         print $hifile  $OldVersion{"$item"}, " ";
346     }
347     return;
348 }
349 \end{code}
350
351 \begin{code}
352 # make "require"r happy...
353 1;
354 \end{code}