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/smlfile/skel-cvt.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/smlfile/skel-cvt.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 293 - (view) (download)

1 : blume 275 (*
2 :     * Convert ASTs to CM's trimmed version thereof.
3 : blume 293 * Very heavily revised.
4 : blume 275 *
5 : blume 286 * Copyright (c) 1999 by Lucent Technologies, Bell Laboratories
6 : blume 275 * Copyright (c) 1995 by AT&T Bell Laboratories
7 :     * Copyright (c) 1993 by Carnegie Mellon University,
8 :     * School of Computer Science
9 :     * contact: Gene Rollins (rollins+@cs.cmu.edu)
10 :     *
11 :     * contact: Matthias Blume (blume@cs.princeton.edu)
12 :     *)
13 :     signature SKELCVT = sig
14 :     val convert : { tree: GenericVC.Ast.dec,
15 :     err: GenericVC.ErrorMsg.severity ->
16 :     GenericVC.Ast.region -> string -> unit }
17 : blume 281 -> { skeleton : Skeleton.decl, complain : unit -> unit }
18 : blume 275 end
19 :    
20 :     structure SkelCvt :> SKELCVT = struct
21 :    
22 :     open GenericVC.Ast Skeleton
23 :    
24 : blume 278 structure S = Symbol
25 : blume 275 structure SP = GenericVC.SymPath
26 :     structure SS = SymbolSet
27 :     structure EM = GenericVC.ErrorMsg
28 :    
29 : blume 278 type symbol = Symbol.symbol
30 : blume 275 type path = symbol list
31 :    
32 : blume 293 (* function composition suitable for fold[lr]-arguments *)
33 : blume 286 infix o'
34 :     fun (f o' g) (x, y) = f (g x, y)
35 : blume 275
36 : blume 293 (* make a Seq node when necessary *)
37 :     fun seq [] = Ref SS.empty
38 :     | seq [only] = only
39 :     | seq l = Seq l
40 :    
41 :     (* make a Par node when necessary and stick it in front of a given dl *)
42 :     fun par ([], d) = d
43 :     | par ([only], d) = only :: d
44 :     | par (l, d) = Par l :: d
45 :    
46 :     (* The main idea is to collect lists of decl ("dl"s).
47 :     * Normally, a dl will eventually become an argument to seq or par.
48 :     * As an important optimization, we always try to keep any "Ref s"
49 :     * at the front. At the moment, however, we do not pay attention
50 :     * to whether a "Ref" commutes with a "Bind" and such. In other words
51 :     * we are far from doing all conceivable optimizations. *)
52 :    
53 :     (* add the head of a symbol path to a given set *)
54 :     fun s_addP ([], set) = set
55 :     | s_addP (head :: _, set) = SS.add (set, head)
56 :    
57 :     (* same as s_addP except we ignore paths of length 1 because they
58 :     * do not involve module access. *)
59 : blume 291 fun s_addMP ([], set) = set (* can this happen at all? *)
60 :     | s_addMP ([only], set) = set (* no module name here *)
61 :     | s_addMP (head :: _, set) = SS.add (set, head)
62 : blume 275
63 : blume 291 (* add a reference to a symbol to a dl *)
64 :     fun dl_addSym (sy, []) = [Ref (SS.singleton sy)]
65 :     | dl_addSym (sy, Ref s :: dl) = Ref (SS.add (s, sy)) :: dl
66 :     | dl_addSym (sy, dl) = Ref (SS.singleton sy) :: dl
67 : blume 275
68 : blume 291 (* add the first element of a path to a dl *)
69 :     fun dl_addP ([], d) = d
70 :     | dl_addP (head :: _, d) = dl_addSym (head, d)
71 :    
72 :     (* add the first element of a path to a dl -- except if that element is
73 :     * the only one on the path*)
74 :     fun dl_addMP ([], dl) = dl
75 :     | dl_addMP ([only], dl) = dl
76 :     | dl_addMP (head :: _, dl) = dl_addSym (head, dl)
77 :    
78 : blume 286 (* given a set of module references, add it to a decl list *)
79 : blume 293 fun dl_addS (s, dl) =
80 :     if SS.isEmpty s then dl
81 :     else case dl of
82 : blume 286 [] => [Ref s]
83 : blume 293 | Ref s' :: dl' => Ref (SS.union (s, s')) :: dl'
84 :     | _ => Ref s :: dl
85 : blume 275
86 : blume 291 (* split initial ref set from a decl list *)
87 :     fun split_dl [] = (SS.empty, [])
88 :     | split_dl (Ref s :: d) = (s, d)
89 :     | split_dl d = (SS.empty, d)
90 : blume 275
91 : blume 291 (* join two definition sequences *)
92 :     fun join_dl ([], d) = d
93 :     | join_dl ([Ref s], d) = dl_addS (s, d)
94 :     | join_dl (h :: t, d) = h :: join_dl (t, d)
95 : blume 275
96 : blume 291 (* local definitions *)
97 :     fun local_dl ([], b, d) = join_dl (b, d)
98 :     | local_dl (Ref s :: t, b, d) = dl_addS (s, local_dl (t, b, d))
99 : blume 293 | local_dl (l, b, d) = Local (seq l, seq b) :: d
100 : blume 275
101 : blume 291 (* build a let expression *)
102 :     fun letexp (dl, (s, e)) =
103 :     case split_dl dl of
104 :     (s', []) => (SS.union (s', s), e)
105 :     | (s', dl') => let
106 :     val dl'' = if SS.isEmpty s then dl'
107 :     else rev (dl_addS (s, rev dl'))
108 :     in
109 : blume 293 (s', Let (dl'', e))
110 : blume 291 end
111 : blume 275
112 : blume 293 (* making an Ign1 where necessary ... *)
113 : blume 291 fun ign (p1, NONE) = p1
114 :     | ign ((s1, e1), SOME (s2, e2)) = (SS.union (s1, s2), Ign1 (e1, e2))
115 : blume 275
116 : blume 293 (* Open cancels Decl *)
117 :     fun open' (Decl dl, dl') = join_dl (dl, dl')
118 :     | open' (e, dl) = Open e :: dl
119 :    
120 : blume 291 (* generate a set of "parallel" bindings *)
121 :     fun parbind f l d = let
122 :     val (s, bl) = foldl f (SS.empty, []) l
123 :     in
124 : blume 293 dl_addS (s, par (bl, d))
125 : blume 291 end
126 : blume 275
127 : blume 291 (* get the ref set from a type *)
128 :     fun ty_s (VarTy _, set) = set
129 :     | ty_s (ConTy (cn, l), set) = s_addMP (cn, foldl ty_s set l)
130 :     | ty_s (RecordTy l, set) = foldl (ty_s o' #2) set l
131 :     | ty_s (TupleTy l, set) = foldl ty_s set l
132 :     | ty_s (MarkTy (arg, _), set) = ty_s (arg, set)
133 : blume 275
134 : blume 291 (* ... from a type option *)
135 :     fun tyopt_s (NONE, set) = set
136 :     | tyopt_s (SOME t, set) = ty_s (t, set)
137 : blume 275
138 : blume 291 (* ... from a pattern *)
139 :     fun pat_s (VarPat p, set) = s_addMP (p, set)
140 :     | pat_s (RecordPat { def, ... }, set) = foldl (pat_s o' #2) set def
141 :     | pat_s ((ListPat l | TuplePat l | VectorPat l | OrPat l), set) =
142 :     foldl pat_s set l
143 :     | pat_s (FlatAppPat l, set) = foldl (pat_s o' #item) set l
144 :     | pat_s (AppPat { constr, argument }, set) =
145 :     pat_s (constr, pat_s (argument, set))
146 :     | pat_s (ConstraintPat { pattern, constraint }, set) =
147 :     pat_s (pattern, ty_s (constraint, set))
148 :     | pat_s (LayeredPat { varPat, expPat }, set) =
149 :     pat_s (varPat, pat_s (expPat, set))
150 :     | pat_s (MarkPat (arg, _), set) = pat_s (arg, set)
151 :     | pat_s ((WildPat|IntPat _|WordPat _|StringPat _|CharPat _), set) = set
152 : blume 275
153 : blume 291 (* ... from an exception binding *)
154 :     fun eb_s (EbGen { exn, etype }, set) = tyopt_s (etype, set)
155 :     | eb_s (EbDef { exn, edef }, set) = s_addMP (edef, set)
156 :     | eb_s (MarkEb (arg, _), set) = eb_s (arg, set)
157 : blume 275
158 : blume 291 (* ... *)
159 :     fun dbrhs_s (Constrs l, set) = foldl (tyopt_s o' #2) set l
160 :     | dbrhs_s (Repl cn, set) = s_addMP (cn, set)
161 : blume 275
162 : blume 291 fun db_s (Db { tyc, tyvars, rhs, lazyp }, set) = dbrhs_s (rhs, set)
163 :     | db_s (MarkDb (arg, _), set) = db_s (arg, set)
164 : blume 275
165 : blume 291 fun tb_s (Tb { tyc, def, tyvars }, set) = ty_s (def, set)
166 :     | tb_s (MarkTb (arg, _), set) = tb_s (arg, set)
167 : blume 275
168 : blume 291 (* get a dl from an expression... *)
169 :     fun exp_dl (VarExp p, d) = dl_addMP (p, d)
170 :     | exp_dl (FnExp rl, d) = foldr rule_dl d rl
171 :     | exp_dl (FlatAppExp l, d) = foldr (exp_dl o' #item) d l
172 :     | exp_dl (AppExp { function, argument }, d) =
173 :     exp_dl (function, exp_dl (argument, d))
174 :     | exp_dl (CaseExp { expr, rules }, d) =
175 :     exp_dl (expr, foldr rule_dl d rules)
176 :     | exp_dl (LetExp { dec, expr }, d) =
177 :     local_dl (dec_dl (dec, []), exp_dl (expr, []), d)
178 :     | exp_dl ((SeqExp l | ListExp l | TupleExp l | VectorExp l), d) =
179 :     foldl exp_dl d l
180 :     | exp_dl (RecordExp l, d) = foldl (exp_dl o' #2) d l
181 :     | exp_dl (SelectorExp _, d) = d
182 :     | exp_dl (ConstraintExp { expr, constraint }, d) =
183 :     dl_addS (ty_s (constraint, SS.empty), exp_dl (expr, d))
184 :     | exp_dl (HandleExp { expr, rules }, d) =
185 :     exp_dl (expr, foldl rule_dl d rules)
186 :     | exp_dl (RaiseExp e, d) = exp_dl (e, d)
187 :     | exp_dl (IfExp { test, thenCase, elseCase }, d) =
188 :     exp_dl (test, exp_dl (thenCase, exp_dl (elseCase, d)))
189 :     | exp_dl ((AndalsoExp (e1, e2) | OrelseExp (e1, e2)), d) =
190 :     exp_dl (e1, exp_dl (e2, d))
191 :     | exp_dl (WhileExp { test, expr }, d) = exp_dl (test, exp_dl (expr, d))
192 :     | exp_dl (MarkExp (arg, _), d) = exp_dl (arg, d)
193 :     | exp_dl ((IntExp _|WordExp _|RealExp _|StringExp _|CharExp _), d) = d
194 : blume 275
195 : blume 291 and rule_dl (Rule { pat, exp }, d) =
196 :     dl_addS (pat_s (pat, SS.empty), exp_dl (exp, d))
197 : blume 275
198 : blume 291 and clause_dl (Clause { pats = p, resultty = t, exp = e }, d) =
199 :     dl_addS (foldl (pat_s o' #item) (tyopt_s (t, SS.empty)) p,
200 :     exp_dl (e, d))
201 :    
202 :     and fb_dl (Fb (l, _), d) = foldr clause_dl d l
203 :     | fb_dl (MarkFb (arg, _), d) = fb_dl (arg, d)
204 :    
205 :     and vb_dl (Vb { pat, exp, lazyp }, d) =
206 :     dl_addS (pat_s (pat, SS.empty), exp_dl (exp, d))
207 :     | vb_dl (MarkVb (arg, _), d) = vb_dl (arg, d)
208 :    
209 :     and rvb_dl (Rvb { var, exp, resultty, ... }, d) =
210 :     dl_addS (tyopt_s (resultty, SS.empty), exp_dl (exp, d))
211 :     | rvb_dl (MarkRvb (arg, _), d) = rvb_dl (arg, d)
212 :    
213 :     and spec_dl (MarkSpec (arg, _), d) = spec_dl (arg, d)
214 :     | spec_dl (StrSpec l, d) = let
215 : blume 293 (* strange case - optional: structure, mandatory: signature *)
216 : blume 291 fun one ((n, g, c), (s, bl)) = let
217 :     val (s', e) = sigexp_p g
218 :     val s'' = SS.union (s, s')
219 :     in
220 :     case c of
221 :     NONE => (s'', Bind (n, e) :: bl)
222 :     | SOME p => (s'', Bind (n, Ign1 (Var (SP.SPATH p), e)) :: bl)
223 :     end
224 :     val (s, bl) = foldr one (SS.empty, []) l
225 : blume 286 in
226 : blume 293 dl_addS (s, par (bl, d))
227 : blume 286 end
228 : blume 291 | spec_dl (TycSpec (l, _), d) = let
229 :     fun one_s ((_, _, SOME t), s) = ty_s (t, s)
230 :     | one_s (_, s) = s
231 : blume 286 in
232 : blume 291 dl_addS (foldl one_s SS.empty l, d)
233 : blume 286 end
234 : blume 291 | spec_dl (FctSpec l, d) = let
235 :     fun one ((n, g), (s, bl)) = let
236 :     val (s', e) = fsigexp_p g
237 :     in
238 :     (SS.union (s, s'), Bind (n, e) :: bl)
239 :     end
240 :     val (s, bl) = foldr one (SS.empty, []) l
241 :     in
242 : blume 293 dl_addS (s, par (bl, d))
243 : blume 291 end
244 :     | spec_dl (ValSpec l, d) = dl_addS (foldl (ty_s o' #2) SS.empty l, d)
245 :     | spec_dl (DataSpec { datatycs, withtycs }, d) =
246 :     dl_addS (foldl db_s (foldl tb_s SS.empty withtycs) datatycs, d)
247 :     | spec_dl (ExceSpec l, d) = dl_addS (foldl (tyopt_s o' #2) SS.empty l, d)
248 :     | spec_dl (ShareStrSpec l, d) = foldl dl_addP d l
249 :     | spec_dl (ShareTycSpec l, d) = dl_addS (foldl s_addMP SS.empty l, d)
250 :     | spec_dl (IncludeSpec g, d) = let
251 :     val (s, e) = sigexp_p g
252 :     in
253 : blume 293 dl_addS (s, open' (e, d))
254 : blume 291 end
255 : blume 275
256 : blume 291 and sigexp_p (VarSig s) = (SS.empty, Var (SP.SPATH [s]))
257 :     | sigexp_p (AugSig (g, whspecs)) = let
258 :     fun one_s (WhType (_, _, ty), s) = ty_s (ty, s)
259 : blume 293 | one_s (WhStruct (_, p), s) = s_addP (p, s)
260 : blume 291 val (s, e) = sigexp_p g
261 :     in
262 :     (foldl one_s s whspecs, e)
263 :     end
264 :     | sigexp_p (BaseSig l) = let
265 :     val (s, d) = split_dl (foldr spec_dl [] l)
266 :     in
267 : blume 293 (s, Decl d)
268 : blume 291 end
269 :     | sigexp_p (MarkSig (arg, _)) = sigexp_p arg
270 : blume 275
271 : blume 291 and fsigexp_p (VarFsig s) = (SS.empty, Var (SP.SPATH [s]))
272 :     | fsigexp_p (BaseFsig { param, result }) =
273 :     letexp (foldr fparam_d [] param, sigexp_p result)
274 :     | fsigexp_p (MarkFsig (arg, _)) = fsigexp_p arg
275 : blume 275
276 : blume 291 and fparam_d ((nopt, g), d) = let
277 :     val (s, e) = sigexp_p g
278 :     in
279 :     case nopt of
280 : blume 293 NONE => dl_addS (s, open' (e, d))
281 : blume 291 | SOME n => dl_addS (s, Bind (n, e) :: d)
282 :     end
283 : blume 275
284 : blume 291 and sigexpc_p NoSig = NONE
285 :     | sigexpc_p (Transparent g | Opaque g) = SOME (sigexp_p g)
286 : blume 275
287 : blume 291 and fsigexpc_p NoSig = NONE
288 :     | fsigexpc_p (Transparent fg | Opaque fg) = SOME (fsigexp_p fg)
289 : blume 275
290 : blume 291 and fctexp_p (VarFct (p, c)) =
291 :     ign ((SS.empty, Var (SP.SPATH p)), fsigexpc_p c)
292 :     | fctexp_p (BaseFct { params, body, constraint }) =
293 :     letexp (foldr fparam_d [] params,
294 :     ign (strexp_p body, sigexpc_p constraint))
295 :     | fctexp_p (AppFct (p, l, c)) = let
296 :     fun one ((str, _), (s, el)) = let
297 :     val (s', e) = strexp_p str
298 :     in
299 :     (SS.union (s, s'), e :: el)
300 :     end
301 :     val (s, el) = foldl one (SS.empty, []) l
302 :     val (s', e) = ign ((SS.empty, Var (SP.SPATH p)), fsigexpc_p c)
303 :     in
304 :     (SS.union (s, s'), foldl Ign1 e el)
305 :     end
306 :     | fctexp_p (LetFct (bdg, b)) = letexp (dec_dl (bdg, []), fctexp_p b)
307 :     | fctexp_p (MarkFct (arg, _)) = fctexp_p arg
308 : blume 275
309 : blume 291 and strexp_p (VarStr p) = (SS.empty, Var (SP.SPATH p))
310 :     | strexp_p (BaseStr dec) = let
311 :     val (s, dl) = split_dl (dec_dl (dec, []))
312 :     in
313 : blume 293 (s, Decl dl)
314 : blume 291 end
315 :     | strexp_p (ConstrainedStr (s, c)) = ign (strexp_p s, sigexpc_p c)
316 :     | strexp_p (AppStr (p, l) | AppStrI (p, l)) = let
317 :     fun one ((str, _), (s, el)) = let
318 :     val (s', e) = strexp_p str
319 :     in
320 :     (SS.union (s, s'), e :: el)
321 :     end
322 :     val (s, el) = foldl one (SS.empty, []) l
323 :     in
324 :     (s, foldl Ign1 (Var (SP.SPATH p)) el)
325 :     end
326 :     | strexp_p (LetStr (bdg, b)) = letexp (dec_dl (bdg, []), strexp_p b)
327 :     | strexp_p (MarkStr (s, _)) = strexp_p s
328 : blume 275
329 : blume 291 and dec_dl (ValDec (l, _), d) = foldl vb_dl d l
330 :     | dec_dl (ValrecDec (l, _), d) = foldl rvb_dl d l
331 :     | dec_dl (FunDec (l, _), d) = foldl fb_dl d l
332 :     | dec_dl (TypeDec l, d) = dl_addS (foldl tb_s SS.empty l, d)
333 :     | dec_dl (DatatypeDec { datatycs, withtycs }, d) =
334 :     dl_addS (foldl db_s (foldl tb_s SS.empty withtycs) datatycs, d)
335 :     | dec_dl (AbstypeDec { abstycs, withtycs, body }, d) =
336 :     dl_addS (foldl db_s (foldl tb_s SS.empty withtycs) abstycs,
337 :     dec_dl (body, d))
338 :     | dec_dl (ExceptionDec l, d) = dl_addS (foldl eb_s SS.empty l, d)
339 :     | dec_dl ((StrDec l | AbsDec l), d) = let
340 :     fun one (MarkStrb (arg, _), x) = one (arg, x)
341 :     | one (Strb { name, def, constraint }, (s, bl)) = let
342 :     val (s', e) = ign (strexp_p def, sigexpc_p constraint)
343 :     in
344 :     (SS.union (s, s'), Bind (name, e) :: bl)
345 :     end
346 :     in
347 :     parbind one l d
348 :     end
349 :     | dec_dl (FctDec l, d) = let
350 :     fun one (MarkFctb (arg, _), x) = one (arg, x)
351 :     | one (Fctb { name, def }, (s, bl)) = let
352 :     val (s', e) = fctexp_p def
353 :     in
354 :     (SS.union (s, s'), Bind (name, e) :: bl)
355 :     end
356 :     in
357 :     parbind one l d
358 :     end
359 :     | dec_dl (SigDec l, d) = let
360 :     fun one (MarkSigb (arg, _), x) = one (arg, x)
361 :     | one (Sigb { name, def }, (s, bl)) = let
362 :     val (s', e) = sigexp_p def
363 :     in
364 :     (SS.union (s, s'), Bind (name, e) :: bl)
365 :     end
366 :     in
367 :     parbind one l d
368 :     end
369 :     | dec_dl (FsigDec l, d) = let
370 :     fun one (MarkFsigb (arg, _), x) = one (arg, x)
371 :     | one (Fsigb { name, def }, (s, bl)) = let
372 :     val (s', e) = fsigexp_p def
373 :     in
374 :     (SS.union (s, s'), Bind (name, e) :: bl)
375 :     end
376 :     in
377 :     parbind one l d
378 :     end
379 :     | dec_dl (LocalDec (bdg, body), d) =
380 :     local_dl (dec_dl (bdg, []), dec_dl (body, []), d)
381 :     | dec_dl (SeqDec l, d) = foldr dec_dl d l
382 : blume 293 | dec_dl (OpenDec l, d) = par (map (Open o Var o SP.SPATH) l, d)
383 : blume 291 | dec_dl ((OvldDec _ | FixDec _), d) = d
384 :     | dec_dl (MarkDec (arg, _), d) = dec_dl (arg, d)
385 : blume 275
386 : blume 293 fun c_dec d = seq (dec_dl (d, []))
387 : blume 275
388 : blume 281 fun convert { tree, err } = let
389 :     (* build a function that will complain (once you call it)
390 :     * about any existing restriction violations *)
391 : blume 293 fun complainCM reg = let
392 : blume 281 fun sameReg (LocalDec (_, body), k) = sameReg (body, k)
393 :     | sameReg (SeqDec l, k) = foldl sameReg k l
394 :     | sameReg (OpenDec _, k) =
395 :     (fn () => (k (); err EM.COMPLAIN reg "toplevel open"))
396 : blume 293 | sameReg (MarkDec (arg, reg), k) = complainCM reg (arg, k)
397 : blume 281 | sameReg ((StrDec _ | AbsDec _ | FctDec _ | SigDec _ |
398 :     FsigDec _), k) = k
399 :     | sameReg (_, k) =
400 :     (fn () =>
401 :     (k (); err EM.WARN reg "definition not tracked by CM"))
402 :    
403 :     in
404 :     sameReg
405 :     end
406 : blume 293
407 :     fun warn0 () = ()
408 :     val complain = complainCM (0, 0) (tree, warn0)
409 : blume 275 in
410 : blume 293 { complain = complain, skeleton = c_dec tree }
411 : blume 275 end
412 :     end

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