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

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