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 291 - (view) (download)

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

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