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 514 - (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 274
20 : blume 279 val eq : info * info -> bool (* compares sourcepaths *)
21 :     val compare : info * info -> order (* compares sourcepaths *)
22 :    
23 : blume 487 (* The idea behind "newGeneration" is the following:
24 :     * Before parsing .cm files (on behalf of CM.make/recomp or CMB.make etc.)
25 :     * we start a new generation. While parsing, when we encounter a new
26 :     * SML source we re-use existing information and bump its generation
27 :     * number to "now". After we are done with one group we can safely
28 :     * evict all info records for files in this group if their generation
29 :     * is not "now".
30 :     * Moreover, if we encounter an entry that has a different owner group,
31 :     * we can either signal an error (if the generation is "now" which means
32 :     * that the file was found in another group during the same parse) or
33 :     * issue a "switched groups" warning (if the generation is older than
34 :     * now which means that the file used to be in another group). *)
35 :     val newGeneration : unit -> unit
36 :    
37 : blume 299 val info : GeneralParams.info ->
38 : blume 354 { sourcepath: SrcPath.t,
39 :     group: SrcPath.t * region,
40 : blume 387 sh_spec: Sharing.request,
41 : blume 326 split: bool }
42 : blume 270 -> info
43 :    
44 : blume 354 val sourcepath : info -> SrcPath.t
45 :     val skelname : info -> string
46 :     val binname : info -> string
47 : blume 299 val error : GeneralParams.info -> info -> complainer
48 : blume 277
49 : blume 299 val parsetree : GeneralParams.info -> info -> (ast * source) option
50 : blume 301 val exports : GeneralParams.info -> info -> SymbolSet.set option
51 :     val skeleton : GeneralParams.info -> info -> Skeleton.decl option
52 : blume 387 val sh_spec : info -> Sharing.request
53 :     val set_sh_mode : info * Sharing.mode -> unit
54 :     val sh_mode : info -> Sharing.mode
55 : blume 326 val split : info -> bool
56 : blume 301 val lastseen : info -> TStamp.t
57 : blume 279
58 : blume 301 (* forget a parse tree that we are done with *)
59 :     val forgetParsetree : info -> unit
60 :    
61 : blume 487 (* Evict all elements that belong to a given group but which
62 :     * are not of the current generation. "cleanGroup" should be
63 : blume 514 * called right after finishing to parse the group file.
64 :     * If the boolean flag ("nowStable") is set to true, then all
65 :     * members of the group are dismissed regardless of their
66 :     * generation. This is used to get rid of the information for
67 :     * members of now-stable libraries. *)
68 :     val cleanGroup : bool -> SrcPath.t -> unit
69 : blume 301
70 : blume 487 (* Delete all known info. *)
71 :     val reset : unit -> unit
72 :    
73 : blume 280 (* different ways of describing an sml file using group and source *)
74 :     val spec : info -> string (* sspec *)
75 :     val fullSpec : info -> string (* gspec(sspec) *)
76 : blume 354 val descr : info -> string (* sname *)
77 :     val fullDescr : info -> string (* gname(sspec) *)
78 : blume 305
79 : blume 306 val errorLocation : GeneralParams.info -> info -> string
80 : blume 270 end
81 :    
82 :     structure SmlInfo :> SMLINFO = struct
83 :    
84 : blume 275 structure Source = GenericVC.Source
85 :     structure SF = GenericVC.SmlFile
86 :     structure EM = GenericVC.ErrorMsg
87 : blume 286 structure FNP = FilenamePolicy
88 : blume 275
89 :     type source = Source.inputSource
90 : blume 297 type ast = GenericVC.Ast.dec
91 :     type region = GenericVC.SourceMap.region
92 : blume 274
93 : blume 295 type complainer = EM.complainer
94 : blume 274
95 : blume 487 type generation = unit ref
96 :    
97 : blume 389 (* sh_mode is an elaboration of sh_spec; it must be persistent
98 :     * and gets properly re-computed when there is a new sh_spec *)
99 : blume 297 datatype persinfo =
100 : blume 354 PERS of { group: SrcPath.t * region,
101 : blume 487 generation: generation ref,
102 : blume 297 lastseen: TStamp.t ref,
103 : blume 299 parsetree: (ast * source) option ref,
104 : blume 389 skeleton: Skeleton.decl option ref,
105 :     sh_mode: Sharing.mode ref }
106 : blume 297
107 : blume 275 datatype info =
108 : blume 354 INFO of { sourcepath: SrcPath.t,
109 : blume 361 mkSkelname: unit -> string,
110 :     mkBinname: unit -> string,
111 : blume 297 persinfo: persinfo,
112 : blume 387 sh_spec: Sharing.request,
113 : blume 326 split: bool }
114 : blume 270
115 : blume 305 type ord_key = info
116 :    
117 : blume 487 local
118 :     val generation = ref (ref ())
119 :     in
120 :     fun now () = !generation
121 :     fun newGeneration () = generation := ref ()
122 :     end
123 :    
124 : blume 277 fun sourcepath (INFO { sourcepath = sp, ... }) = sp
125 : blume 361 fun skelname (INFO { mkSkelname = msn, ... }) = msn ()
126 :     fun binname (INFO { mkBinname = mbn, ... }) = mbn ()
127 : blume 387 fun sh_spec (INFO { sh_spec = s, ... }) = s
128 : blume 389 fun sh_mode (INFO { persinfo = PERS { sh_mode = ref m, ... }, ... }) = m
129 :     fun set_sh_mode (INFO { persinfo = PERS { sh_mode, ... }, ... }, m) =
130 :     sh_mode := m
131 : blume 326 fun split (INFO { split = s, ... }) = s
132 : blume 270
133 : blume 299 fun gerror (gp: GeneralParams.info) = GroupReg.error (#groupreg gp)
134 : blume 297
135 :     fun error gp (INFO { persinfo = PERS { group, ... }, ... }) =
136 :     gerror gp group
137 :    
138 : blume 279 fun compare (INFO { sourcepath = p, ... }, INFO { sourcepath = p', ... }) =
139 : blume 354 SrcPath.compare (p, p')
140 : blume 279 fun eq (i, i') = compare (i, i') = EQUAL
141 :    
142 : blume 301 fun lastseen (INFO { persinfo = PERS { lastseen, ... }, ... }) =
143 :     !lastseen
144 :    
145 : blume 354 val knownInfo = ref (SrcPathMap.empty: persinfo SrcPathMap.map)
146 : blume 277
147 : blume 365 fun countParseTrees () = let
148 :     fun one (PERS { parsetree = ref (SOME _), ... }, i) = i + 1
149 :     | one (_, i) = i
150 :     in
151 :     SrcPathMap.foldl one 0 (!knownInfo)
152 :     end
153 :    
154 : blume 301 fun forgetParsetree (INFO { persinfo = PERS { parsetree, ... }, ... }) =
155 :     parsetree := NONE
156 :    
157 : blume 514 fun cleanGroup nowStable g = let
158 : blume 487 val n = now ()
159 :     fun isCurrent (PERS { generation = ref gen, group = (g', _), ... }) =
160 : blume 514 ((not nowStable) andalso gen = n)
161 :     orelse SrcPath.compare (g, g') <> EQUAL
162 : blume 301 in
163 : blume 487 knownInfo := SrcPathMap.filter isCurrent (!knownInfo)
164 : blume 301 end
165 :    
166 : blume 487 fun reset () = knownInfo := SrcPathMap.empty
167 :    
168 : blume 330 (* check timestamp and throw away any invalid cache *)
169 :     fun validate (sourcepath, PERS pir) = let
170 :     (* don't use "..." pattern to have the compiler catch later
171 :     * additions to the type! *)
172 : blume 487 val { group, lastseen, parsetree, skeleton, sh_mode, generation } = pir
173 : blume 330 val ts = !lastseen
174 : blume 354 val nts = SrcPath.tstamp sourcepath
175 : blume 330 in
176 : blume 345 if TStamp.needsUpdate { source = nts, target = ts } then
177 : blume 330 (lastseen := nts;
178 : blume 487 generation := now ();
179 : blume 330 parsetree := NONE;
180 :     skeleton := NONE)
181 :     else ()
182 :     end
183 :    
184 : blume 299 fun info (gp: GeneralParams.info) arg = let
185 : blume 387 val { sourcepath, group = gr as (group, region), sh_spec, split } = arg
186 : blume 310 val policy = #fnpolicy (#param gp)
187 : blume 361 fun mkSkelname () = FNP.mkSkelName policy sourcepath
188 :     fun mkBinname () = FNP.mkBinName policy sourcepath
189 : blume 297 val groupreg = #groupreg gp
190 :     fun newpersinfo () = let
191 : blume 354 val ts = SrcPath.tstamp sourcepath
192 : blume 330 val pi = PERS { group = gr, lastseen = ref ts,
193 : blume 389 parsetree = ref NONE, skeleton = ref NONE,
194 : blume 487 sh_mode = ref (Sharing.SHARE false),
195 :     generation = ref (now ()) }
196 : blume 281 in
197 : blume 354 knownInfo := SrcPathMap.insert (!knownInfo, sourcepath, pi);
198 : blume 297 pi
199 : blume 281 end
200 : blume 297 fun persinfo () =
201 : blume 354 case SrcPathMap.find (!knownInfo, sourcepath) of
202 : blume 297 NONE => newpersinfo ()
203 : blume 487 | SOME (pi as PERS { group = gr' as (g, r), generation, ... }) =>
204 : blume 354 if SrcPath.compare (group, g) <> EQUAL then let
205 :     val n = SrcPath.descr sourcepath
206 : blume 322 in
207 : blume 487 if !generation = now () then
208 : blume 322 (gerror gp gr EM.COMPLAIN
209 :     (concat ["ML source file ", n,
210 :     " appears in more than one group"])
211 :     EM.nullErrorBody;
212 :     gerror gp gr' EM.COMPLAIN
213 :     (concat ["(previous occurence of ", n, ")"])
214 :     EM.nullErrorBody)
215 :     else
216 :     gerror gp gr EM.WARN
217 :     (concat ["ML source file ", n,
218 :     " has switched groups"])
219 :     EM.nullErrorBody;
220 :     newpersinfo ()
221 :     end
222 : blume 330 else (validate (sourcepath, pi); pi)
223 : blume 281 in
224 : blume 297 INFO { sourcepath = sourcepath,
225 : blume 361 mkSkelname = mkSkelname,
226 :     mkBinname = mkBinname,
227 : blume 297 persinfo = persinfo (),
228 : blume 387 sh_spec = sh_spec,
229 : blume 326 split = split }
230 : blume 281 end
231 : blume 277
232 : blume 275 (* the following functions are only concerned with getting the data,
233 :     * not with checking time stamps *)
234 : blume 297 fun getParseTree gp (i as INFO ir, quiet, noerrors) = let
235 :     val { sourcepath, persinfo = PERS { parsetree, ... }, ... } = ir
236 : blume 281 val err = if noerrors then (fn m => ())
237 : blume 297 else (fn m => error gp i EM.COMPLAIN m EM.nullErrorBody)
238 : blume 274 in
239 : blume 275 case !parsetree of
240 :     SOME pt => SOME pt
241 :     | NONE => let
242 : blume 345 fun work stream = let
243 :     val _ = if noerrors orelse quiet then ()
244 : blume 354 else Say.vsay ["[parsing ",
245 :     SrcPath.descr sourcepath, "]\n"]
246 : blume 345 val source =
247 : blume 492 Source.newSource (SrcPath.osstring' sourcepath,
248 : blume 354 1, stream, false, #errcons gp)
249 : blume 275 in
250 : blume 345 (SF.parse source, source)
251 :     end
252 : blume 364 fun openIt () = TextIO.openIn (SrcPath.osstring sourcepath)
253 : blume 345 val pto =
254 :     SOME (SafeIO.perform { openIt = openIt,
255 :     closeIt = TextIO.closeIn,
256 :     work = work,
257 : blume 459 cleanup = fn _ => () })
258 : blume 365 (* Counting the trees explicitly may be a bit slow,
259 :     * but maintaining an accurate count is difficult, so
260 :     * this method should be robust. (I don't think that
261 :     * the overhead of counting will make a noticeable
262 :     * difference.) *)
263 :     val ntrees = countParseTrees ()
264 : blume 433 val treelimit = #get StdConfig.parse_caching ()
265 : blume 275 in
266 : blume 365 if ntrees < treelimit then
267 :     parsetree := pto
268 :     else ();
269 : blume 275 pto
270 : blume 345 end handle exn as IO.Io _ => (err (General.exnMessage exn); NONE)
271 :     | SF.Compile msg => (err msg; NONE)
272 : blume 274 end
273 :    
274 : blume 297 fun getSkeleton gp (i as INFO ir, noerrors) = let
275 : blume 361 val { sourcepath, mkSkelname, persinfo = PERS pir, ... } = ir
276 : blume 297 val { skeleton, lastseen, ... } = pir
277 : blume 275 in
278 :     case !skeleton of
279 : blume 301 SOME sk => SOME sk
280 : blume 361 | NONE => let
281 :     val skelname = mkSkelname ()
282 :     in
283 :     case SkelIO.read (skelname, !lastseen) of
284 :     SOME sk => (skeleton := SOME sk; SOME sk)
285 :     | NONE =>
286 :     (case getParseTree gp (i, false, noerrors) of
287 :     SOME (tree, source) => let
288 :     fun err sv region s =
289 :     EM.error source region sv s
290 :     EM.nullErrorBody
291 :     val { skeleton = sk, complain } =
292 :     SkelCvt.convert { tree = tree,
293 :     err = err }
294 :     in
295 :     if noerrors then () else complain ();
296 : blume 310 if EM.anyErrors (EM.errors source) then
297 : blume 361 if noerrors then ()
298 :     else error gp i EM.COMPLAIN
299 : blume 310 "error(s) in ML source file"
300 :     EM.nullErrorBody
301 : blume 354 else (SkelIO.write (skelname, sk, !lastseen);
302 : blume 310 skeleton := SOME sk);
303 :     SOME sk
304 : blume 361 end
305 :     | NONE => NONE)
306 :     end
307 : blume 275 end
308 :    
309 : blume 330 fun skeleton0 noerrors gp i = getSkeleton gp (i, noerrors)
310 : blume 281
311 :     (* we only complain at the time of getting the exports *)
312 : blume 301 fun exports gp i = Option.map SkelExports.exports (skeleton0 false gp i)
313 : blume 281 val skeleton = skeleton0 true
314 : blume 275
315 : blume 330 fun parsetree gp i = getParseTree gp (i, true, true)
316 : blume 282
317 : blume 354 fun spec (INFO { sourcepath, ... }) = SrcPath.specOf sourcepath
318 : blume 297 fun fullSpec (INFO { sourcepath, persinfo = PERS { group, ... }, ... }) =
319 : blume 354 concat [SrcPath.specOf (#1 group), "(", SrcPath.specOf sourcepath, ")"]
320 :     fun descr (INFO { sourcepath, ... }) = SrcPath.descr sourcepath
321 :     fun fullDescr (INFO { sourcepath, persinfo = PERS { group, ... }, ... }) =
322 :     concat [SrcPath.descr (#1 group), "(", SrcPath.specOf sourcepath, ")"]
323 : blume 305
324 : blume 306 fun errorLocation (gp: GeneralParams.info) (INFO i) = let
325 :     val { persinfo = PERS { group = (group, reg), ... }, ... } = i
326 : blume 305 in
327 : blume 306 EM.matchErrorString (GroupReg.lookup (#groupreg gp) group) reg
328 : blume 305 end
329 : blume 270 end

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