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

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