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/depend/build.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/depend/build.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 733 - (view) (download)

1 : blume 286 (*
2 :     * Build the dependency graph for one group/library.
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 : blume 279 signature BUILDDEPEND = sig
9 : blume 283 type impexp = DependencyGraph.impexp
10 :    
11 :     val build :
12 :     { imports: impexp SymbolMap.map,
13 :     gimports: impexp SymbolMap.map,
14 :     smlfiles: SmlInfo.info list,
15 : blume 294 localdefs: SmlInfo.info SymbolMap.map,
16 : blume 444 subgroups: 'a,
17 : blume 642 sources: 'b,
18 : blume 301 reqpriv: GroupGraph.privileges }
19 : blume 283 * SymbolSet.set option (* filter *)
20 : blume 299 * GeneralParams.info
21 : blume 537 * DependencyGraph.farsbnode (* pervasive env *)
22 : blume 283 ->
23 :     impexp SymbolMap.map (* exports *)
24 : blume 301 * GroupGraph.privileges (* required privileges (aggregate) *)
25 : blume 733 * SymbolSet.set (* imported symbols *)
26 : blume 355
27 :     (* for the autoloader *)
28 :     type looker = Symbol.symbol -> DAEnv.env
29 :     val look : looker -> DAEnv.env -> looker
30 :     val processOneSkeleton : looker -> Skeleton.decl -> unit
31 : blume 279 end
32 : blume 277
33 : blume 279 structure BuildDepend :> BUILDDEPEND = struct
34 :    
35 : blume 278 structure S = Symbol
36 : blume 277 structure SS = SymbolSet
37 : blume 278 structure SM = SymbolMap
38 : blume 277 structure SK = Skeleton
39 :     structure DG = DependencyGraph
40 : blume 286 structure DE = DAEnv
41 : blume 279 structure EM = GenericVC.ErrorMsg
42 :     structure SP = GenericVC.SymPath
43 : blume 277
44 : blume 283 type impexp = DG.impexp
45 :    
46 : blume 355 type looker = Symbol.symbol -> DAEnv.env
47 :    
48 : blume 286 fun look otherwise DE.EMPTY s = otherwise s
49 :     | look otherwise (DE.BINDING (s', v)) s =
50 : blume 278 if S.eq (s, s') then v else otherwise s
51 : blume 286 | look otherwise (DE.LAYER (e, e')) s = look (look otherwise e') e s
52 : blume 308 | look otherwise (DE.FCTENV looker) s =
53 : blume 278 (case looker s of NONE => otherwise s | SOME v => v)
54 : blume 286 | look otherwise (DE.FILTER (ss, e)) s =
55 :     if SymbolSet.member (ss, s) then look otherwise e s else otherwise s
56 : blume 279
57 : blume 355 fun evalOneSkeleton lookimport = let
58 :     (* build the lookup function for DG.env *)
59 :     val lookup = look lookimport
60 :    
61 :     fun lookSymPath e (SP.SPATH []) = DE.EMPTY
62 :     | lookSymPath e (SP.SPATH (p as (h :: t))) = let
63 :     (* again, if we don't find it here we just ignore
64 :     * the problem and let the compiler catch it later *)
65 :     val lookup' = look (fn _ => DE.EMPTY)
66 :     fun loop (e, []) = e
67 :     | loop (e, h :: t) = loop (lookup' e h, t)
68 :     in
69 :     loop (lookup e h, t)
70 :     end
71 :    
72 :     (* "eval" -- compute the export environment of a skeleton *)
73 :     fun eval sk = let
74 :     fun evalDecl e (SK.Bind (name, def)) =
75 :     DE.BINDING (name, evalModExp e def)
76 :     | evalDecl e (SK.Local (d1, d2)) =
77 :     evalDecl (DE.LAYER (evalDecl e d1, e)) d2
78 :     | evalDecl e (SK.Seq l) = evalSeqDecl e l
79 :     | evalDecl e (SK.Par []) = DE.EMPTY
80 :     | evalDecl e (SK.Par (h :: t)) =
81 :     foldl (fn (x, r) => DE.LAYER (evalDecl e x, r))
82 :     (evalDecl e h) t
83 :     | evalDecl e (SK.Open s) = evalModExp e s
84 :     | evalDecl e (SK.Ref s) =
85 :     (SS.app (ignore o lookup e) s; DE.EMPTY)
86 :    
87 :     and evalSeqDecl e [] = DE.EMPTY
88 :     | evalSeqDecl e (h :: t) = let
89 :     fun one (d, e') =
90 :     DE.LAYER (evalDecl (DE.LAYER (e', e)) d, e')
91 :     in
92 :     foldl one (evalDecl e h) t
93 :     end
94 :    
95 :     and evalModExp e (SK.Var sp) = lookSymPath e sp
96 :     | evalModExp e (SK.Decl l) = evalSeqDecl e l
97 :     | evalModExp e (SK.Let (d, m)) =
98 :     evalModExp (DE.LAYER (evalSeqDecl e d, e)) m
99 :     | evalModExp e (SK.Ign1 (m1, m2)) =
100 :     (ignore (evalModExp e m1); evalModExp e m2)
101 :     in
102 :     evalDecl DE.EMPTY sk
103 :     end
104 :     in
105 :     eval
106 :     end
107 :    
108 :     fun processOneSkeleton lookimport sk =
109 :     ignore (evalOneSkeleton lookimport sk)
110 :    
111 : blume 279 (* get the description for a symbol *)
112 :     fun symDesc (s, r) =
113 : blume 294 S.nameSpaceToString (S.nameSpace s) :: " " :: S.name s :: r
114 : blume 279
115 : blume 537 fun build (coll, fopt, gp, perv_fsbnode) = let
116 : blume 642 val { imports, gimports, smlfiles, localdefs, subgroups,
117 :     sources, reqpriv } = coll
118 : blume 277
119 : blume 278 (* the "blackboard" where analysis results are announced *)
120 :     (* (also used for cycle detection) *)
121 : blume 305 val bb = ref SmlInfoMap.empty
122 :     fun lock i = bb := SmlInfoMap.insert (!bb, i, NONE)
123 :     fun release (i, r) = (bb := SmlInfoMap.insert (!bb, i, SOME r); r)
124 :     fun fetch i = SmlInfoMap.find (!bb, i)
125 : blume 277
126 : blume 733 (* We collect all imported symbols so that we can then narrow
127 :     * the list of libraries. *)
128 :     val gi_syms = ref SymbolSet.empty
129 :     fun add_gi_sym s = gi_syms := SymbolSet.add (!gi_syms, s)
130 :    
131 : blume 278 (* - get the result from the blackboard if it is there *)
132 :     (* - otherwise trigger analysis *)
133 :     (* - detect cycles using locking *)
134 :     (* - maintain root set *)
135 : blume 277 fun getResult (i, history) =
136 :     case fetch i of
137 : blume 286 NONE => (lock i; release (i, analyze (i, history)))
138 :     | SOME (SOME r) => r
139 : blume 278 | SOME NONE => let (* cycle found --> error message *)
140 : blume 277 val f = SmlInfo.sourcepath i
141 :     fun pphist pps = let
142 : blume 280 fun recur (_, []) = () (* shouldn't happen *)
143 :     | recur (n'', (s, i') :: r) = let
144 : blume 277 val f' = SmlInfo.sourcepath i'
145 : blume 666 val n' = SrcPath.descr f'
146 : blume 278 val _ =
147 : blume 279 if SmlInfo.eq (i, i') then ()
148 : blume 280 else recur (n', r)
149 : blume 277 val l =
150 :     n' :: " refers to " ::
151 : blume 280 symDesc (s, [" defined in ", n''])
152 : blume 277 in
153 :     app (PrettyPrint.add_string pps) l;
154 :     PrettyPrint.add_newline pps
155 :     end
156 :     in
157 : blume 280 PrettyPrint.add_newline pps;
158 : blume 666 recur (SrcPath.descr f, history)
159 : blume 277 end
160 :     in
161 : blume 297 SmlInfo.error gp i EM.COMPLAIN
162 : blume 295 "cyclic ML dependencies" pphist;
163 : blume 282 release (i, (DG.SNODE { smlinfo = i,
164 :     localimports = [],
165 :     globalimports = [] },
166 : blume 286 DE.EMPTY))
167 : blume 277 end
168 :    
169 : blume 279 (* do the actual analysis of an ML source and generate the
170 :     * corresponding node *)
171 : blume 278 and analyze (i, history) = let
172 : blume 279 val li = ref []
173 : blume 537 val gi = ref [perv_fsbnode]
174 : blume 279
175 :     (* register a local import *)
176 : blume 282 fun localImport n =
177 :     if List.exists (fn n' => DG.seq (n, n')) (!li) then ()
178 : blume 279 else li := n :: !li
179 :    
180 :     (* register a global import, maintain filter sets *)
181 : blume 733 fun globalImport (s, (f, n)) = let
182 : blume 282 fun sameN (_, n') = DG.sbeq (n, n')
183 :     in
184 : blume 733 add_gi_sym s;
185 : blume 282 case List.find sameN (!gi) of
186 :     NONE => gi := (f, n) :: !gi (* brand new *)
187 :     | SOME (NONE, n') => () (* no filter -> no change *)
188 :     | SOME (SOME f', n') => let
189 :     (* there is a filter...
190 :     * calculate "union", see if there is a change,
191 :     * and if so, replace the filter *)
192 :     fun replace filt =
193 : blume 587 gi := (filt, n) ::
194 :     List.filter (not o sameN) (!gi)
195 : blume 282 in
196 :     case f of
197 :     NONE => replace NONE
198 : blume 587 | SOME f => if SS.equal (f, f') then ()
199 :     else replace (SOME (SS.union (f, f')))
200 : blume 282 end
201 :     end
202 : blume 279
203 :     val f = SmlInfo.sourcepath i
204 :     fun isSelf i' = SmlInfo.eq (i, i')
205 :    
206 :     (* lookup function for things not defined in the same ML file.
207 :     * As a side effect, this function registers local and
208 :     * global imports. *)
209 :     fun lookimport s = let
210 : blume 404 fun dontcomplain s = DE.EMPTY
211 : blume 279 fun lookfar () =
212 : blume 283 case SM.find (imports, s) of
213 : blume 733 SOME (farnth, e, _) => (globalImport (s, farnth ());
214 : blume 652 look dontcomplain e s)
215 : blume 301 | NONE =>
216 :     (* We could complain here about an undefined
217 :     * name. However, since CM doesn't have the
218 :     * proper source locations available, it is
219 :     * better to handle this case silently and
220 :     * have the compiler catch the problem later. *)
221 :     DE.EMPTY
222 : blume 279 in
223 : blume 278 case SM.find (localdefs, s) of
224 : blume 279 SOME i' =>
225 :     if isSelf i' then lookfar ()
226 :     else let
227 :     val (n, e) = getResult (i', (s, i) :: history)
228 :     in
229 :     localImport n;
230 : blume 404 look dontcomplain e s
231 : blume 279 end
232 :     | NONE => lookfar ()
233 :     end
234 :    
235 : blume 355 val eval = evalOneSkeleton lookimport
236 : blume 279
237 : blume 301 val e = case SmlInfo.skeleton gp i of
238 :     SOME sk => eval sk
239 :     | NONE => DE.EMPTY
240 :    
241 : blume 282 val n = DG.SNODE { smlinfo = i,
242 : blume 371 localimports = !li,
243 :     globalimports = !gi }
244 : blume 278 in
245 : blume 279 (n, e)
246 : blume 278 end
247 :    
248 :     (* run the analysis on one ML file -- causing the blackboard
249 : blume 286 * to be updated accordingly *)
250 : blume 278 fun doSmlFile i = ignore (getResult (i, []))
251 : blume 279
252 : blume 652 (* run the analysis *)
253 :     val _ = app doSmlFile smlfiles
254 :    
255 :     (* Invert the "localdefs" map so that each smlinfo is mapped to the
256 :     * corresponding _set_ of symbols: *)
257 :     local
258 :     fun add (sy, i, m) =
259 :     case SmlInfoMap.find (m, i) of
260 :     NONE => SmlInfoMap.insert (m, i, SymbolSet.singleton sy)
261 :     | SOME ss => SmlInfoMap.insert (m, i, SymbolSet.add (ss, sy))
262 :     in
263 :     val ilocaldefs = SymbolMap.foldli add SmlInfoMap.empty localdefs
264 :     end
265 :    
266 :     fun addDummyFilt i = let
267 : blume 283 val (sn, e) = valOf (valOf (fetch i))
268 : blume 652 val sbn = DG.SB_SNODE sn
269 :     val fsbn = (NONE, sbn)
270 : blume 283 in
271 : blume 652 (* We also thunkify the fsbn so that the result is an impexp. *)
272 :     (fn () => fsbn, e, valOf (SmlInfoMap.find (ilocaldefs, i)))
273 : blume 283 end
274 :    
275 :     (* First we make a map of all locally defined symbols to
276 :     * the local "far sb node"
277 :     * but with only a dummy filter attached.
278 :     * This makes it consistent with the current state
279 :     * of "imports" and "gimports" where there can be filters, but
280 :     * where those filters are not yet strengthened according to fopt *)
281 : blume 652 val localmap = SM.map addDummyFilt localdefs
282 : blume 283
283 :     val exports =
284 :     case fopt of
285 : blume 733 NONE => let
286 : blume 283 (* There is no filter -- so we are in an ordinary
287 :     * group and should export all gimports as well as
288 :     * all local definitions.
289 :     * No filter strengthening is necessary. *)
290 : blume 733 fun add (s, b, m) =
291 :     if SM.inDomain (localmap, s) then m
292 :     else (add_gi_sym s; SM.insert (m, s, b))
293 :     in
294 :     SM.foldli add localmap gimports
295 :     end
296 : blume 283 | SOME ss => let
297 :     (* There is a filter.
298 :     * We export only the things in the filter.
299 :     * They can be taken from either localmap or else from
300 :     * imports. In either case, it is necessary to strengthen
301 :     * the filter attached to each node. *)
302 : blume 652 fun strengthen (nth, e, allsyms) = let
303 :     val (fopt', sbn) = nth ()
304 : blume 283 val new_fopt =
305 :     case fopt' of
306 :     NONE => fopt
307 :     | SOME ss' => SOME (SS.intersection (ss, ss'))
308 : blume 652 fun nth' () = (new_fopt, sbn)
309 : blume 283 in
310 : blume 652 (nth', DE.FILTER (ss, e), SS.intersection (allsyms,ss))
311 : blume 283 end
312 :     fun addNodeFor (s, m) =
313 : blume 733 case SM.find (localmap, s) of
314 : blume 283 SOME n => SM.insert (m, s, strengthen n)
315 : blume 357 | NONE =>
316 : blume 733 (case SM.find (imports, s) of
317 :     SOME n => (add_gi_sym s;
318 :     SM.insert (m, s, strengthen n))
319 :     | NONE =>
320 :     (* This should never happen since we
321 :     * checked beforehand during
322 :     * parsing/semantic analysis *)
323 :     EM.impossible "build: undefined export")
324 : blume 283 in
325 :     SS.foldl addNodeFor SM.empty ss
326 :     end
327 : blume 277 in
328 : blume 387 CheckSharing.check (exports, gp);
329 : blume 733 (exports, reqpriv, !gi_syms)
330 : blume 277 end
331 :     end

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