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 295 - (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 295 SmlInfo.error i EM.COMPLAIN
91 :     "cyclic ML dependencies" pphist;
92 : blume 282 release (i, (DG.SNODE { smlinfo = i,
93 :     localimports = [],
94 :     globalimports = [] },
95 : blume 286 DE.EMPTY))
96 : blume 277 end
97 :    
98 : blume 279 (* do the actual analysis of an ML source and generate the
99 :     * corresponding node *)
100 : blume 278 and analyze (i, history) = let
101 : blume 279 val li = ref []
102 :     val gi = ref []
103 :    
104 :     (* register a local import *)
105 : blume 282 fun localImport n =
106 :     if List.exists (fn n' => DG.seq (n, n')) (!li) then ()
107 : blume 279 else li := n :: !li
108 :    
109 :     (* register a global import, maintain filter sets *)
110 : blume 282 fun globalImport (f, n) = let
111 :     fun sameN (_, n') = DG.sbeq (n, n')
112 :     in
113 :     case List.find sameN (!gi) of
114 :     NONE => gi := (f, n) :: !gi (* brand new *)
115 :     | SOME (NONE, n') => () (* no filter -> no change *)
116 :     | SOME (SOME f', n') => let
117 :     (* there is a filter...
118 :     * calculate "union", see if there is a change,
119 :     * and if so, replace the filter *)
120 :     fun replace filt =
121 :     gi := (filt, n) :: List.filter (not o sameN) (!gi)
122 :     in
123 :     case f of
124 :     NONE => replace NONE
125 :     | SOME f =>
126 :     if SS.equal (f, f') then ()
127 :     else replace (SOME (SS.union (f, f')))
128 :     end
129 :     end
130 : blume 279
131 :     val f = SmlInfo.sourcepath i
132 :     fun isSelf i' = SmlInfo.eq (i, i')
133 :    
134 : blume 281 exception Lookup
135 :    
136 : blume 279 (* lookup function for things not defined in the same ML file.
137 :     * As a side effect, this function registers local and
138 :     * global imports. *)
139 :     fun lookimport s = let
140 :     fun lookfar () =
141 : blume 283 case SM.find (imports, s) of
142 : blume 279 SOME (farn, e) => (globalImport farn; e)
143 : blume 295 | NONE => (SmlInfo.error i EM.COMPLAIN
144 : blume 280 (concat (AbsPath.spec f ::
145 : blume 279 ": reference to unknown " ::
146 :     symDesc (s, [])))
147 :     EM.nullErrorBody;
148 : blume 281 raise Lookup)
149 : blume 279 in
150 : blume 278 case SM.find (localdefs, s) of
151 : blume 279 SOME i' =>
152 :     if isSelf i' then lookfar ()
153 :     else let
154 :     val (n, e) = getResult (i', (s, i) :: history)
155 :     in
156 :     localImport n;
157 :     e
158 :     end
159 :     | NONE => lookfar ()
160 :     end
161 :    
162 :     (* build the lookup function for DG.env *)
163 : blume 281 val lookup_exn = look lookimport
164 : blume 279
165 : blume 286 fun lookSymPath e (SP.SPATH []) = DE.EMPTY
166 : blume 279 | lookSymPath e (SP.SPATH (p as (h :: t))) = let
167 :     fun dotPath [] = []
168 :     | dotPath [s] = [S.name s]
169 :     | dotPath (h :: t) = S.name h :: "." :: dotPath t
170 :     fun complain s =
171 : blume 295 (SmlInfo.error i EM.COMPLAIN
172 : blume 281 (concat
173 :     (AbsPath.spec f ::
174 :     ": undefined " ::
175 :     symDesc (s, " in path " :: dotPath p)))
176 :     EM.nullErrorBody;
177 :     raise Lookup)
178 :     val lookup_exn' = look complain
179 : blume 279 fun loop (e, []) = e
180 : blume 281 | loop (e, h :: t) = loop (lookup_exn' e h, t)
181 : blume 279 in
182 : blume 286 loop (lookup_exn e h, t) handle Lookup => DE.EMPTY
183 : blume 279 end
184 :    
185 : blume 286 fun lookup e s = lookup_exn e s handle Lookup => DE.EMPTY
186 : blume 281
187 : blume 279 (* "eval" -- compute the export environment of a skeleton *)
188 :     fun eval sk = let
189 : blume 286 fun evalDecl e (SK.Bind (name, def)) =
190 :     DE.BINDING (name, evalModExp e def)
191 :     | evalDecl e (SK.Local (d1, d2)) =
192 :     evalDecl (DE.LAYER (evalDecl e d1, e)) d2
193 : blume 293 | evalDecl e (SK.Seq l) = evalSeqDecl e l
194 :     | evalDecl e (SK.Par []) = DE.EMPTY
195 :     | evalDecl e (SK.Par (h :: t)) =
196 :     foldl (fn (x, r) => DE.LAYER (evalDecl e x, r))
197 :     (evalDecl e h) t
198 : blume 286 | evalDecl e (SK.Open s) = evalModExp e s
199 :     | evalDecl e (SK.Ref s) =
200 :     (SS.app (ignore o lookup e) s; DE.EMPTY)
201 : blume 278
202 : blume 293 and evalSeqDecl e [] = DE.EMPTY
203 : blume 294 | evalSeqDecl e (h :: t) = let
204 :     fun one (d, e') =
205 :     DE.LAYER (evalDecl (DE.LAYER (e', e)) d, e')
206 :     in
207 :     foldl one (evalDecl e h) t
208 :     end
209 : blume 293
210 : blume 286 and evalModExp e (SK.Var sp) = lookSymPath e sp
211 : blume 293 | evalModExp e (SK.Decl l) = evalSeqDecl e l
212 : blume 286 | evalModExp e (SK.Let (d, m)) =
213 : blume 293 evalModExp (DE.LAYER (evalSeqDecl e d, e)) m
214 : blume 291 | evalModExp e (SK.Ign1 (m1, m2)) =
215 : blume 286 (ignore (evalModExp e m1); evalModExp e m2)
216 : blume 279 in
217 : blume 286 evalDecl DE.EMPTY sk
218 : blume 279 end
219 :    
220 :     val e = eval (SmlInfo.skeleton i)
221 : blume 282 val n = DG.SNODE { smlinfo = i,
222 :     localimports = !li,
223 :     globalimports = !gi }
224 : blume 278 in
225 : blume 279 (n, e)
226 : blume 278 end
227 :    
228 :     (* run the analysis on one ML file -- causing the blackboard
229 : blume 286 * to be updated accordingly *)
230 : blume 278 fun doSmlFile i = ignore (getResult (i, []))
231 : blume 279
232 : blume 283 (* converting smlinfos to sbnodes * env *)
233 :     fun i2sbn i = let
234 :     val (sn, e) = valOf (valOf (fetch i))
235 :     in
236 :     (DG.SB_SNODE sn, e)
237 :     end
238 :    
239 :     (* run the analysis *)
240 :     val _ = app doSmlFile smlfiles
241 :    
242 :     fun addDummyFilt (sbn, e) = ((NONE, sbn), e)
243 :    
244 :     (* First we make a map of all locally defined symbols to
245 :     * the local "far sb node"
246 :     * but with only a dummy filter attached.
247 :     * This makes it consistent with the current state
248 :     * of "imports" and "gimports" where there can be filters, but
249 :     * where those filters are not yet strengthened according to fopt *)
250 :     val localmap = SM.map (addDummyFilt o i2sbn) localdefs
251 :    
252 :     val exports =
253 :     case fopt of
254 :     NONE =>
255 :     (* There is no filter -- so we are in an ordinary
256 :     * group and should export all gimports as well as
257 :     * all local definitions.
258 :     * No filter strengthening is necessary. *)
259 :     SM.unionWith #1 (localmap, gimports)
260 :     | SOME ss => let
261 :     (* There is a filter.
262 :     * We export only the things in the filter.
263 :     * They can be taken from either localmap or else from
264 :     * imports. In either case, it is necessary to strengthen
265 :     * the filter attached to each node. *)
266 :     fun strengthen ((fopt', sbn), e) = let
267 :     val new_fopt =
268 :     case fopt' of
269 :     NONE => fopt
270 :     | SOME ss' => SOME (SS.intersection (ss, ss'))
271 :     in
272 : blume 286 ((new_fopt, sbn), DE.FILTER (ss, e))
273 : blume 283 end
274 :     val availablemap = SM.unionWith #1 (localmap, imports)
275 :     fun addNodeFor (s, m) =
276 :     case SM.find (availablemap, s) of
277 :     SOME n => SM.insert (m, s, strengthen n)
278 :     | NONE => (error
279 :     (concat ("exported " ::
280 :     symDesc (s, [" not defined"])));
281 :     m)
282 :     in
283 :     SS.foldl addNodeFor SM.empty ss
284 :     end
285 : blume 277 in
286 : blume 283 exports
287 : blume 277 end
288 :     end

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