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

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