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

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