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

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