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