Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/smlfile/smlinfo.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/smlfile/smlinfo.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 818 - (view) (download)

1 : blume 275 (*
2 : blume 282 * Bundling information pertaining to one SML source file.
3 :     * - only includes information that does not require running
4 :     * the machine-dependent part of the compiler
5 : blume 275 *
6 :     * (C) 1999 Lucent Technologies, Bell Laboratories
7 :     *
8 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
9 :     *)
10 : blume 270 signature SMLINFO = sig
11 :    
12 :     type info
13 : blume 305 type ord_key = info
14 : blume 270
15 : blume 295 type complainer = GenericVC.ErrorMsg.complainer
16 : blume 297 type ast = GenericVC.Ast.dec
17 :     type region = GenericVC.SourceMap.region
18 : blume 299 type source = GenericVC.Source.inputSource
19 : blume 818 type splitrequest = GenericVC.Control.LambdaSplitting.localsetting
20 : blume 274
21 : blume 537 type attribs =
22 : blume 818 { split: splitrequest,
23 : blume 592 is_rts: bool,
24 :     explicit_core_sym: Symbol.symbol option,
25 : blume 537 extra_compenv: GenericVC.Environment.staticEnv option }
26 :    
27 : blume 818 type info_args =
28 :     { sourcepath: SrcPath.file,
29 :     group: SrcPath.file * region,
30 :     sh_spec: Sharing.request,
31 :     setup: string option * string option }
32 :    
33 : blume 279 val eq : info * info -> bool (* compares sourcepaths *)
34 :     val compare : info * info -> order (* compares sourcepaths *)
35 :    
36 : blume 487 (* The idea behind "newGeneration" is the following:
37 :     * Before parsing .cm files (on behalf of CM.make/recomp or CMB.make etc.)
38 :     * we start a new generation. While parsing, when we encounter a new
39 :     * SML source we re-use existing information and bump its generation
40 :     * number to "now". After we are done with one group we can safely
41 :     * evict all info records for files in this group if their generation
42 :     * is not "now".
43 :     * Moreover, if we encounter an entry that has a different owner group,
44 :     * we can either signal an error (if the generation is "now" which means
45 :     * that the file was found in another group during the same parse) or
46 :     * issue a "switched groups" warning (if the generation is older than
47 :     * now which means that the file used to be in another group). *)
48 :     val newGeneration : unit -> unit
49 :    
50 : blume 818 val info : splitrequest -> GeneralParams.info -> info_args -> info
51 : blume 270
52 : blume 818 val info' : attribs -> GeneralParams.info -> info_args -> info
53 : blume 537
54 : blume 666 val sourcepath : info -> SrcPath.file
55 : blume 354 val skelname : info -> string
56 :     val binname : info -> string
57 : blume 666 val group : info -> SrcPath.file
58 : blume 299 val error : GeneralParams.info -> info -> complainer
59 : blume 277
60 : blume 299 val parsetree : GeneralParams.info -> info -> (ast * source) option
61 : blume 301 val exports : GeneralParams.info -> info -> SymbolSet.set option
62 :     val skeleton : GeneralParams.info -> info -> Skeleton.decl option
63 : blume 387 val sh_spec : info -> Sharing.request
64 :     val set_sh_mode : info * Sharing.mode -> unit
65 :     val sh_mode : info -> Sharing.mode
66 : blume 537 val attribs : info -> attribs
67 : blume 301 val lastseen : info -> TStamp.t
68 : blume 677 val setup : info -> string option * string option
69 : blume 279
70 : blume 301 (* forget a parse tree that we are done with *)
71 :     val forgetParsetree : info -> unit
72 :    
73 : blume 487 (* Evict all elements that belong to a given group but which
74 :     * are not of the current generation. "cleanGroup" should be
75 : blume 514 * called right after finishing to parse the group file.
76 :     * If the boolean flag ("nowStable") is set to true, then all
77 :     * members of the group are dismissed regardless of their
78 :     * generation. This is used to get rid of the information for
79 :     * members of now-stable libraries. *)
80 : blume 666 val cleanGroup : bool -> SrcPath.file -> unit
81 : blume 301
82 : blume 537 (* See if a given piece of info is (still) known here: *)
83 :     val isKnown : info -> bool
84 :    
85 : blume 487 (* Delete all known info. *)
86 :     val reset : unit -> unit
87 :    
88 : blume 280 (* different ways of describing an sml file using group and source *)
89 : blume 354 val descr : info -> string (* sname *)
90 : blume 305
91 : blume 306 val errorLocation : GeneralParams.info -> info -> string
92 : blume 270 end
93 :    
94 :     structure SmlInfo :> SMLINFO = struct
95 :    
96 : blume 275 structure Source = GenericVC.Source
97 :     structure SF = GenericVC.SmlFile
98 :     structure EM = GenericVC.ErrorMsg
99 : blume 286 structure FNP = FilenamePolicy
100 : blume 275
101 :     type source = Source.inputSource
102 : blume 297 type ast = GenericVC.Ast.dec
103 :     type region = GenericVC.SourceMap.region
104 : blume 818 type splitrequest = GenericVC.Control.LambdaSplitting.localsetting
105 : blume 274
106 : blume 295 type complainer = EM.complainer
107 : blume 274
108 : blume 537 type attribs =
109 : blume 818 { split: splitrequest,
110 : blume 592 is_rts: bool,
111 :     explicit_core_sym: Symbol.symbol option,
112 : blume 537 extra_compenv: GenericVC.Environment.staticEnv option }
113 :    
114 : blume 818 type info_args =
115 :     { sourcepath: SrcPath.file,
116 :     group: SrcPath.file * region,
117 :     sh_spec: Sharing.request,
118 :     setup: string option * string option }
119 :    
120 : blume 487 type generation = unit ref
121 :    
122 : blume 389 (* sh_mode is an elaboration of sh_spec; it must be persistent
123 :     * and gets properly re-computed when there is a new sh_spec *)
124 : blume 297 datatype persinfo =
125 : blume 666 PERS of { group: SrcPath.file * region,
126 : blume 487 generation: generation ref,
127 : blume 297 lastseen: TStamp.t ref,
128 : blume 299 parsetree: (ast * source) option ref,
129 : blume 389 skeleton: Skeleton.decl option ref,
130 :     sh_mode: Sharing.mode ref }
131 : blume 297
132 : blume 275 datatype info =
133 : blume 666 INFO of { sourcepath: SrcPath.file,
134 : blume 361 mkSkelname: unit -> string,
135 :     mkBinname: unit -> string,
136 : blume 297 persinfo: persinfo,
137 : blume 387 sh_spec: Sharing.request,
138 : blume 677 attribs: attribs,
139 :     setup: string option * string option }
140 : blume 270
141 : blume 305 type ord_key = info
142 :    
143 : blume 487 local
144 :     val generation = ref (ref ())
145 :     in
146 :     fun now () = !generation
147 :     fun newGeneration () = generation := ref ()
148 :     end
149 :    
150 : blume 277 fun sourcepath (INFO { sourcepath = sp, ... }) = sp
151 : blume 361 fun skelname (INFO { mkSkelname = msn, ... }) = msn ()
152 :     fun binname (INFO { mkBinname = mbn, ... }) = mbn ()
153 : blume 387 fun sh_spec (INFO { sh_spec = s, ... }) = s
154 : blume 389 fun sh_mode (INFO { persinfo = PERS { sh_mode = ref m, ... }, ... }) = m
155 :     fun set_sh_mode (INFO { persinfo = PERS { sh_mode, ... }, ... }, m) =
156 :     sh_mode := m
157 : blume 537 fun attribs (INFO { attribs = a, ... }) = a
158 : blume 677 fun setup (INFO { setup = s, ... }) = s
159 : blume 270
160 : blume 299 fun gerror (gp: GeneralParams.info) = GroupReg.error (#groupreg gp)
161 : blume 297
162 :     fun error gp (INFO { persinfo = PERS { group, ... }, ... }) =
163 :     gerror gp group
164 :    
165 : blume 632 fun group (INFO { persinfo = PERS { group = (g, _), ... }, ... }) = g
166 :    
167 : blume 279 fun compare (INFO { sourcepath = p, ... }, INFO { sourcepath = p', ... }) =
168 : blume 354 SrcPath.compare (p, p')
169 : blume 279 fun eq (i, i') = compare (i, i') = EQUAL
170 :    
171 : blume 301 fun lastseen (INFO { persinfo = PERS { lastseen, ... }, ... }) =
172 :     !lastseen
173 :    
174 : blume 354 val knownInfo = ref (SrcPathMap.empty: persinfo SrcPathMap.map)
175 : blume 277
176 : blume 537 fun isKnown (INFO { sourcepath, ... }) =
177 :     isSome (SrcPathMap.find (!knownInfo, sourcepath))
178 :    
179 : blume 365 fun countParseTrees () = let
180 :     fun one (PERS { parsetree = ref (SOME _), ... }, i) = i + 1
181 :     | one (_, i) = i
182 :     in
183 :     SrcPathMap.foldl one 0 (!knownInfo)
184 :     end
185 :    
186 : blume 301 fun forgetParsetree (INFO { persinfo = PERS { parsetree, ... }, ... }) =
187 :     parsetree := NONE
188 :    
189 : blume 514 fun cleanGroup nowStable g = let
190 : blume 487 val n = now ()
191 :     fun isCurrent (PERS { generation = ref gen, group = (g', _), ... }) =
192 : blume 514 ((not nowStable) andalso gen = n)
193 :     orelse SrcPath.compare (g, g') <> EQUAL
194 : blume 301 in
195 : blume 487 knownInfo := SrcPathMap.filter isCurrent (!knownInfo)
196 : blume 301 end
197 :    
198 : blume 487 fun reset () = knownInfo := SrcPathMap.empty
199 :    
200 : blume 330 (* check timestamp and throw away any invalid cache *)
201 :     fun validate (sourcepath, PERS pir) = let
202 :     (* don't use "..." pattern to have the compiler catch later
203 :     * additions to the type! *)
204 : blume 487 val { group, lastseen, parsetree, skeleton, sh_mode, generation } = pir
205 : blume 330 val ts = !lastseen
206 : blume 354 val nts = SrcPath.tstamp sourcepath
207 : blume 330 in
208 : blume 345 if TStamp.needsUpdate { source = nts, target = ts } then
209 : blume 330 (lastseen := nts;
210 : blume 487 generation := now ();
211 : blume 330 parsetree := NONE;
212 :     skeleton := NONE)
213 :     else ()
214 :     end
215 :    
216 : blume 537 fun info' attribs (gp: GeneralParams.info) arg = let
217 : blume 677 val { sourcepath, group = gr as (group, region), sh_spec, setup } = arg
218 : blume 310 val policy = #fnpolicy (#param gp)
219 : blume 361 fun mkSkelname () = FNP.mkSkelName policy sourcepath
220 :     fun mkBinname () = FNP.mkBinName policy sourcepath
221 : blume 297 val groupreg = #groupreg gp
222 :     fun newpersinfo () = let
223 : blume 354 val ts = SrcPath.tstamp sourcepath
224 : blume 330 val pi = PERS { group = gr, lastseen = ref ts,
225 : blume 389 parsetree = ref NONE, skeleton = ref NONE,
226 : blume 487 sh_mode = ref (Sharing.SHARE false),
227 :     generation = ref (now ()) }
228 : blume 281 in
229 : blume 354 knownInfo := SrcPathMap.insert (!knownInfo, sourcepath, pi);
230 : blume 297 pi
231 : blume 281 end
232 : blume 297 fun persinfo () =
233 : blume 354 case SrcPathMap.find (!knownInfo, sourcepath) of
234 : blume 297 NONE => newpersinfo ()
235 : blume 487 | SOME (pi as PERS { group = gr' as (g, r), generation, ... }) =>
236 : blume 354 if SrcPath.compare (group, g) <> EQUAL then let
237 :     val n = SrcPath.descr sourcepath
238 : blume 322 in
239 : blume 487 if !generation = now () then
240 : blume 322 (gerror gp gr EM.COMPLAIN
241 :     (concat ["ML source file ", n,
242 :     " appears in more than one group"])
243 :     EM.nullErrorBody;
244 :     gerror gp gr' EM.COMPLAIN
245 :     (concat ["(previous occurence of ", n, ")"])
246 :     EM.nullErrorBody)
247 :     else
248 :     gerror gp gr EM.WARN
249 :     (concat ["ML source file ", n,
250 :     " has switched groups"])
251 :     EM.nullErrorBody;
252 :     newpersinfo ()
253 :     end
254 : blume 330 else (validate (sourcepath, pi); pi)
255 : blume 281 in
256 : blume 297 INFO { sourcepath = sourcepath,
257 : blume 361 mkSkelname = mkSkelname,
258 :     mkBinname = mkBinname,
259 : blume 297 persinfo = persinfo (),
260 : blume 387 sh_spec = sh_spec,
261 : blume 677 attribs = attribs,
262 :     setup = setup }
263 : blume 281 end
264 : blume 277
265 : blume 818 fun info split = info' { split = split, extra_compenv = NONE,
266 :     is_rts = false, explicit_core_sym = NONE }
267 : blume 537
268 : blume 275 (* the following functions are only concerned with getting the data,
269 :     * not with checking time stamps *)
270 : blume 297 fun getParseTree gp (i as INFO ir, quiet, noerrors) = let
271 :     val { sourcepath, persinfo = PERS { parsetree, ... }, ... } = ir
272 : blume 281 val err = if noerrors then (fn m => ())
273 : blume 297 else (fn m => error gp i EM.COMPLAIN m EM.nullErrorBody)
274 : blume 274 in
275 : blume 275 case !parsetree of
276 :     SOME pt => SOME pt
277 :     | NONE => let
278 : blume 345 fun work stream = let
279 :     val _ = if noerrors orelse quiet then ()
280 : blume 354 else Say.vsay ["[parsing ",
281 :     SrcPath.descr sourcepath, "]\n"]
282 : blume 710 (* The logic is a bit tricky here:
283 :     * If "noerrors" is set we want to suppress error
284 :     * messages from the parser. This is done using
285 :     * a dummy error consumer that does nothing. However,
286 :     * if we do that we get a "source" object that has
287 :     * a dummy error consumer hard-wired into it. As a
288 :     * result we also don't see error messages from the
289 :     * elaborator in this case -- bad. So we make
290 :     * TWO "source" objects that share the same input
291 :     * stream but used different error consumers. *)
292 :     val (source, parse_source) = let
293 :     val normal_ec = #errcons gp
294 :     val source =
295 :     Source.newSource (SrcPath.osstring' sourcepath,
296 :     1, stream, false, normal_ec)
297 :     in
298 :     if noerrors then let
299 :     val dummy_ec = { consumer = fn (x: string) => (),
300 :     linewidth = #linewidth normal_ec,
301 :     flush = fn () => () }
302 :     val parse_source =
303 : blume 711 (* clone of "source", mute error consumer *)
304 :     { sourceMap = #sourceMap source,
305 :     fileOpened = #fileOpened source,
306 :     interactive = #interactive source,
307 :     sourceStream = #sourceStream source,
308 :     anyErrors = #anyErrors source,
309 :     errConsumer = dummy_ec }
310 : blume 710 in
311 :     (source, parse_source)
312 :     end
313 :     else (source, source)
314 :     end
315 : blume 275 in
316 : blume 710 (SF.parse parse_source, source)
317 : blume 345 end
318 : blume 364 fun openIt () = TextIO.openIn (SrcPath.osstring sourcepath)
319 : blume 345 val pto =
320 :     SOME (SafeIO.perform { openIt = openIt,
321 :     closeIt = TextIO.closeIn,
322 :     work = work,
323 : blume 459 cleanup = fn _ => () })
324 : blume 365 (* Counting the trees explicitly may be a bit slow,
325 :     * but maintaining an accurate count is difficult, so
326 :     * this method should be robust. (I don't think that
327 :     * the overhead of counting will make a noticeable
328 :     * difference.) *)
329 :     val ntrees = countParseTrees ()
330 : blume 433 val treelimit = #get StdConfig.parse_caching ()
331 : blume 275 in
332 : blume 365 if ntrees < treelimit then
333 :     parsetree := pto
334 :     else ();
335 : blume 275 pto
336 : blume 345 end handle exn as IO.Io _ => (err (General.exnMessage exn); NONE)
337 :     | SF.Compile msg => (err msg; NONE)
338 : blume 274 end
339 :    
340 : blume 297 fun getSkeleton gp (i as INFO ir, noerrors) = let
341 : blume 361 val { sourcepath, mkSkelname, persinfo = PERS pir, ... } = ir
342 : blume 297 val { skeleton, lastseen, ... } = pir
343 : blume 275 in
344 :     case !skeleton of
345 : blume 301 SOME sk => SOME sk
346 : blume 361 | NONE => let
347 :     val skelname = mkSkelname ()
348 :     in
349 :     case SkelIO.read (skelname, !lastseen) of
350 :     SOME sk => (skeleton := SOME sk; SOME sk)
351 :     | NONE =>
352 :     (case getParseTree gp (i, false, noerrors) of
353 :     SOME (tree, source) => let
354 :     fun err sv region s =
355 :     EM.error source region sv s
356 :     EM.nullErrorBody
357 :     val { skeleton = sk, complain } =
358 :     SkelCvt.convert { tree = tree,
359 :     err = err }
360 :     in
361 :     if noerrors then () else complain ();
362 : blume 310 if EM.anyErrors (EM.errors source) then
363 : blume 361 if noerrors then ()
364 :     else error gp i EM.COMPLAIN
365 : blume 310 "error(s) in ML source file"
366 :     EM.nullErrorBody
367 : blume 354 else (SkelIO.write (skelname, sk, !lastseen);
368 : blume 310 skeleton := SOME sk);
369 :     SOME sk
370 : blume 361 end
371 :     | NONE => NONE)
372 :     end
373 : blume 275 end
374 :    
375 : blume 330 fun skeleton0 noerrors gp i = getSkeleton gp (i, noerrors)
376 : blume 281
377 :     (* we only complain at the time of getting the exports *)
378 : blume 301 fun exports gp i = Option.map SkelExports.exports (skeleton0 false gp i)
379 : blume 281 val skeleton = skeleton0 true
380 : blume 275
381 : blume 330 fun parsetree gp i = getParseTree gp (i, true, true)
382 : blume 282
383 : blume 354 fun descr (INFO { sourcepath, ... }) = SrcPath.descr sourcepath
384 : blume 305
385 : blume 306 fun errorLocation (gp: GeneralParams.info) (INFO i) = let
386 :     val { persinfo = PERS { group = (group, reg), ... }, ... } = i
387 : blume 305 in
388 : blume 306 EM.matchErrorString (GroupReg.lookup (#groupreg gp) group) reg
389 : blume 305 end
390 : blume 270 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0