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 879 - (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 879 type complainer = ErrorMsg.complainer
16 :     type ast = Ast.dec
17 :     type region = SourceMap.region
18 :     type source = Source.inputSource
19 :     type splitrequest = 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 879 extra_compenv: Environment.staticEnv option }
26 : blume 537
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 879 structure Source = Source
97 :     structure SF = SmlFile
98 :     structure EM = ErrorMsg
99 : blume 286 structure FNP = FilenamePolicy
100 : blume 275
101 :     type source = Source.inputSource
102 : blume 879 type ast = Ast.dec
103 :     type region = SourceMap.region
104 :     type splitrequest = Control.LambdaSplitting.localsetting
105 : blume 274
106 : blume 295 type complainer = EM.complainer
107 : blume 274
108 : blume 879 type attribs = { split: splitrequest,
109 :     is_rts: bool,
110 :     explicit_core_sym: Symbol.symbol option,
111 :     extra_compenv: Environment.staticEnv option }
112 : blume 537
113 : blume 879 type info_args = { sourcepath: SrcPath.file,
114 :     group: SrcPath.file * region,
115 :     sh_spec: Sharing.request,
116 :     setup: string option * string option }
117 : blume 818
118 : blume 487 type generation = unit ref
119 :    
120 : blume 389 (* sh_mode is an elaboration of sh_spec; it must be persistent
121 :     * and gets properly re-computed when there is a new sh_spec *)
122 : blume 297 datatype persinfo =
123 : blume 666 PERS of { group: SrcPath.file * region,
124 : blume 487 generation: generation ref,
125 : blume 297 lastseen: TStamp.t ref,
126 : blume 299 parsetree: (ast * source) option ref,
127 : blume 389 skeleton: Skeleton.decl option ref,
128 :     sh_mode: Sharing.mode ref }
129 : blume 297
130 : blume 275 datatype info =
131 : blume 666 INFO of { sourcepath: SrcPath.file,
132 : blume 361 mkSkelname: unit -> string,
133 :     mkBinname: unit -> string,
134 : blume 297 persinfo: persinfo,
135 : blume 387 sh_spec: Sharing.request,
136 : blume 677 attribs: attribs,
137 :     setup: string option * string option }
138 : blume 270
139 : blume 305 type ord_key = info
140 :    
141 : blume 487 local
142 :     val generation = ref (ref ())
143 :     in
144 :     fun now () = !generation
145 :     fun newGeneration () = generation := ref ()
146 :     end
147 :    
148 : blume 277 fun sourcepath (INFO { sourcepath = sp, ... }) = sp
149 : blume 361 fun skelname (INFO { mkSkelname = msn, ... }) = msn ()
150 :     fun binname (INFO { mkBinname = mbn, ... }) = mbn ()
151 : blume 387 fun sh_spec (INFO { sh_spec = s, ... }) = s
152 : blume 389 fun sh_mode (INFO { persinfo = PERS { sh_mode = ref m, ... }, ... }) = m
153 :     fun set_sh_mode (INFO { persinfo = PERS { sh_mode, ... }, ... }, m) =
154 :     sh_mode := m
155 : blume 537 fun attribs (INFO { attribs = a, ... }) = a
156 : blume 677 fun setup (INFO { setup = s, ... }) = s
157 : blume 270
158 : blume 299 fun gerror (gp: GeneralParams.info) = GroupReg.error (#groupreg gp)
159 : blume 297
160 :     fun error gp (INFO { persinfo = PERS { group, ... }, ... }) =
161 :     gerror gp group
162 :    
163 : blume 632 fun group (INFO { persinfo = PERS { group = (g, _), ... }, ... }) = g
164 :    
165 : blume 279 fun compare (INFO { sourcepath = p, ... }, INFO { sourcepath = p', ... }) =
166 : blume 354 SrcPath.compare (p, p')
167 : blume 279 fun eq (i, i') = compare (i, i') = EQUAL
168 :    
169 : blume 301 fun lastseen (INFO { persinfo = PERS { lastseen, ... }, ... }) =
170 :     !lastseen
171 :    
172 : blume 354 val knownInfo = ref (SrcPathMap.empty: persinfo SrcPathMap.map)
173 : blume 277
174 : blume 537 fun isKnown (INFO { sourcepath, ... }) =
175 :     isSome (SrcPathMap.find (!knownInfo, sourcepath))
176 :    
177 : blume 365 fun countParseTrees () = let
178 :     fun one (PERS { parsetree = ref (SOME _), ... }, i) = i + 1
179 :     | one (_, i) = i
180 :     in
181 :     SrcPathMap.foldl one 0 (!knownInfo)
182 :     end
183 :    
184 : blume 301 fun forgetParsetree (INFO { persinfo = PERS { parsetree, ... }, ... }) =
185 :     parsetree := NONE
186 :    
187 : blume 514 fun cleanGroup nowStable g = let
188 : blume 487 val n = now ()
189 :     fun isCurrent (PERS { generation = ref gen, group = (g', _), ... }) =
190 : blume 514 ((not nowStable) andalso gen = n)
191 :     orelse SrcPath.compare (g, g') <> EQUAL
192 : blume 301 in
193 : blume 487 knownInfo := SrcPathMap.filter isCurrent (!knownInfo)
194 : blume 301 end
195 :    
196 : blume 487 fun reset () = knownInfo := SrcPathMap.empty
197 :    
198 : blume 330 (* check timestamp and throw away any invalid cache *)
199 :     fun validate (sourcepath, PERS pir) = let
200 :     (* don't use "..." pattern to have the compiler catch later
201 :     * additions to the type! *)
202 : blume 487 val { group, lastseen, parsetree, skeleton, sh_mode, generation } = pir
203 : blume 330 val ts = !lastseen
204 : blume 354 val nts = SrcPath.tstamp sourcepath
205 : blume 330 in
206 : blume 345 if TStamp.needsUpdate { source = nts, target = ts } then
207 : blume 330 (lastseen := nts;
208 : blume 487 generation := now ();
209 : blume 330 parsetree := NONE;
210 :     skeleton := NONE)
211 :     else ()
212 :     end
213 :    
214 : blume 537 fun info' attribs (gp: GeneralParams.info) arg = let
215 : blume 677 val { sourcepath, group = gr as (group, region), sh_spec, setup } = arg
216 : blume 310 val policy = #fnpolicy (#param gp)
217 : blume 361 fun mkSkelname () = FNP.mkSkelName policy sourcepath
218 :     fun mkBinname () = FNP.mkBinName policy sourcepath
219 : blume 297 val groupreg = #groupreg gp
220 :     fun newpersinfo () = let
221 : blume 354 val ts = SrcPath.tstamp sourcepath
222 : blume 330 val pi = PERS { group = gr, lastseen = ref ts,
223 : blume 389 parsetree = ref NONE, skeleton = ref NONE,
224 : blume 487 sh_mode = ref (Sharing.SHARE false),
225 :     generation = ref (now ()) }
226 : blume 281 in
227 : blume 354 knownInfo := SrcPathMap.insert (!knownInfo, sourcepath, pi);
228 : blume 297 pi
229 : blume 281 end
230 : blume 297 fun persinfo () =
231 : blume 354 case SrcPathMap.find (!knownInfo, sourcepath) of
232 : blume 297 NONE => newpersinfo ()
233 : blume 487 | SOME (pi as PERS { group = gr' as (g, r), generation, ... }) =>
234 : blume 354 if SrcPath.compare (group, g) <> EQUAL then let
235 :     val n = SrcPath.descr sourcepath
236 : blume 322 in
237 : blume 487 if !generation = now () then
238 : blume 322 (gerror gp gr EM.COMPLAIN
239 :     (concat ["ML source file ", n,
240 :     " appears in more than one group"])
241 :     EM.nullErrorBody;
242 :     gerror gp gr' EM.COMPLAIN
243 :     (concat ["(previous occurence of ", n, ")"])
244 :     EM.nullErrorBody)
245 :     else
246 :     gerror gp gr EM.WARN
247 :     (concat ["ML source file ", n,
248 :     " has switched groups"])
249 :     EM.nullErrorBody;
250 :     newpersinfo ()
251 :     end
252 : blume 330 else (validate (sourcepath, pi); pi)
253 : blume 281 in
254 : blume 297 INFO { sourcepath = sourcepath,
255 : blume 361 mkSkelname = mkSkelname,
256 :     mkBinname = mkBinname,
257 : blume 297 persinfo = persinfo (),
258 : blume 387 sh_spec = sh_spec,
259 : blume 677 attribs = attribs,
260 :     setup = setup }
261 : blume 281 end
262 : blume 277
263 : blume 818 fun info split = info' { split = split, extra_compenv = NONE,
264 :     is_rts = false, explicit_core_sym = NONE }
265 : blume 537
266 : blume 275 (* the following functions are only concerned with getting the data,
267 :     * not with checking time stamps *)
268 : blume 297 fun getParseTree gp (i as INFO ir, quiet, noerrors) = let
269 :     val { sourcepath, persinfo = PERS { parsetree, ... }, ... } = ir
270 : blume 281 val err = if noerrors then (fn m => ())
271 : blume 297 else (fn m => error gp i EM.COMPLAIN m EM.nullErrorBody)
272 : blume 274 in
273 : blume 275 case !parsetree of
274 :     SOME pt => SOME pt
275 :     | NONE => let
276 : blume 345 fun work stream = let
277 :     val _ = if noerrors orelse quiet then ()
278 : blume 354 else Say.vsay ["[parsing ",
279 :     SrcPath.descr sourcepath, "]\n"]
280 : blume 710 (* The logic is a bit tricky here:
281 :     * If "noerrors" is set we want to suppress error
282 :     * messages from the parser. This is done using
283 :     * a dummy error consumer that does nothing. However,
284 :     * if we do that we get a "source" object that has
285 :     * a dummy error consumer hard-wired into it. As a
286 :     * result we also don't see error messages from the
287 :     * elaborator in this case -- bad. So we make
288 :     * TWO "source" objects that share the same input
289 :     * stream but used different error consumers. *)
290 :     val (source, parse_source) = let
291 :     val normal_ec = #errcons gp
292 :     val source =
293 :     Source.newSource (SrcPath.osstring' sourcepath,
294 :     1, stream, false, normal_ec)
295 :     in
296 :     if noerrors then let
297 :     val dummy_ec = { consumer = fn (x: string) => (),
298 :     linewidth = #linewidth normal_ec,
299 :     flush = fn () => () }
300 :     val parse_source =
301 : blume 711 (* clone of "source", mute error consumer *)
302 :     { sourceMap = #sourceMap source,
303 :     fileOpened = #fileOpened source,
304 :     interactive = #interactive source,
305 :     sourceStream = #sourceStream source,
306 :     anyErrors = #anyErrors source,
307 :     errConsumer = dummy_ec }
308 : blume 710 in
309 :     (source, parse_source)
310 :     end
311 :     else (source, source)
312 :     end
313 : blume 275 in
314 : blume 710 (SF.parse parse_source, source)
315 : blume 345 end
316 : blume 364 fun openIt () = TextIO.openIn (SrcPath.osstring sourcepath)
317 : blume 345 val pto =
318 :     SOME (SafeIO.perform { openIt = openIt,
319 :     closeIt = TextIO.closeIn,
320 :     work = work,
321 : blume 459 cleanup = fn _ => () })
322 : blume 365 (* Counting the trees explicitly may be a bit slow,
323 :     * but maintaining an accurate count is difficult, so
324 :     * this method should be robust. (I don't think that
325 :     * the overhead of counting will make a noticeable
326 :     * difference.) *)
327 :     val ntrees = countParseTrees ()
328 : blume 433 val treelimit = #get StdConfig.parse_caching ()
329 : blume 275 in
330 : blume 365 if ntrees < treelimit then
331 :     parsetree := pto
332 :     else ();
333 : blume 275 pto
334 : blume 345 end handle exn as IO.Io _ => (err (General.exnMessage exn); NONE)
335 : blume 879 | CompileExn.Compile msg => (err msg; NONE)
336 : blume 274 end
337 :    
338 : blume 297 fun getSkeleton gp (i as INFO ir, noerrors) = let
339 : blume 361 val { sourcepath, mkSkelname, persinfo = PERS pir, ... } = ir
340 : blume 297 val { skeleton, lastseen, ... } = pir
341 : blume 275 in
342 :     case !skeleton of
343 : blume 301 SOME sk => SOME sk
344 : blume 361 | NONE => let
345 :     val skelname = mkSkelname ()
346 :     in
347 :     case SkelIO.read (skelname, !lastseen) of
348 :     SOME sk => (skeleton := SOME sk; SOME sk)
349 :     | NONE =>
350 :     (case getParseTree gp (i, false, noerrors) of
351 :     SOME (tree, source) => let
352 :     fun err sv region s =
353 :     EM.error source region sv s
354 :     EM.nullErrorBody
355 :     val { skeleton = sk, complain } =
356 :     SkelCvt.convert { tree = tree,
357 :     err = err }
358 :     in
359 :     if noerrors then () else complain ();
360 : blume 310 if EM.anyErrors (EM.errors source) then
361 : blume 361 if noerrors then ()
362 :     else error gp i EM.COMPLAIN
363 : blume 310 "error(s) in ML source file"
364 :     EM.nullErrorBody
365 : blume 354 else (SkelIO.write (skelname, sk, !lastseen);
366 : blume 310 skeleton := SOME sk);
367 :     SOME sk
368 : blume 361 end
369 :     | NONE => NONE)
370 :     end
371 : blume 275 end
372 :    
373 : blume 330 fun skeleton0 noerrors gp i = getSkeleton gp (i, noerrors)
374 : blume 281
375 :     (* we only complain at the time of getting the exports *)
376 : blume 301 fun exports gp i = Option.map SkelExports.exports (skeleton0 false gp i)
377 : blume 281 val skeleton = skeleton0 true
378 : blume 275
379 : blume 330 fun parsetree gp i = getParseTree gp (i, true, true)
380 : blume 282
381 : blume 354 fun descr (INFO { sourcepath, ... }) = SrcPath.descr sourcepath
382 : blume 305
383 : blume 306 fun errorLocation (gp: GeneralParams.info) (INFO i) = let
384 :     val { persinfo = PERS { group = (group, reg), ... }, ... } = i
385 : blume 305 in
386 : blume 306 EM.matchErrorString (GroupReg.lookup (#groupreg gp) group) reg
387 : blume 305 end
388 : blume 270 end

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