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/semant/semant.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/semant/semant.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 735 - (view) (download)

1 : blume 267 (*
2 :     * semantic actions to go with the grammar for CM description files
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 : blume 265 signature CM_SEMANT = sig
9 :    
10 : blume 666 type context = SrcPath.dir
11 : blume 297 type region = GenericVC.SourceMap.region
12 : blume 265 type ml_symbol
13 :     type cm_symbol
14 : blume 380 type cm_class
15 : blume 632 type cm_version = Version.t
16 : blume 265
17 : blume 294 type group = GroupGraph.group
18 : blume 265
19 : blume 305 type privilegespec
20 : blume 265 type aexp
21 :     type exp
22 :     type members (* still conditional *)
23 :     type exports (* still conditional *)
24 :    
25 : blume 588 type toolopt
26 : blume 735 type toolregistry
27 : blume 588
28 : blume 266 type complainer = string -> unit
29 :    
30 : blume 735 val newToolRegistry : unit -> toolregistry
31 :    
32 : blume 267 (* getting elements of primitive types (pathnames and symbols) *)
33 : blume 666 val file_native : string * context * complainer -> SrcPath.prefile
34 : blume 632 val file_standard :
35 : blume 666 GeneralParams.info -> string * context * complainer -> SrcPath.prefile
36 : blume 265 val cm_symbol : string -> cm_symbol
37 : blume 632 val cm_version : string * complainer -> cm_version
38 : blume 265 val ml_structure : string -> ml_symbol
39 :     val ml_signature : string -> ml_symbol
40 :     val ml_functor : string -> ml_symbol
41 :     val ml_funsig : string -> ml_symbol
42 : blume 380 val class : cm_symbol -> cm_class
43 : blume 265
44 : blume 270 (* getting the full analysis for a group/library *)
45 : blume 666 val group : { path: SrcPath.file,
46 : blume 632 privileges: privilegespec,
47 :     exports: exports option,
48 :     members: members,
49 :     gp: GeneralParams.info,
50 : blume 666 curlib: SrcPath.file option,
51 :     owner: SrcPath.file option,
52 : blume 632 error: complainer,
53 :     initgroup: group } -> group
54 : blume 666 val library : { path: SrcPath.file,
55 : blume 632 privileges: privilegespec,
56 :     exports: exports,
57 :     version : cm_version option,
58 :     members: members,
59 :     gp: GeneralParams.info,
60 :     initgroup: group } -> group
61 : blume 265
62 : blume 283 (* assembling privilege lists *)
63 :     val initialPrivilegeSpec : privilegespec
64 :     val require : privilegespec * cm_symbol * complainer -> privilegespec
65 : blume 348 val wrap : privilegespec * cm_symbol * complainer -> privilegespec
66 : blume 265
67 : blume 267 (* constructing member collections *)
68 : blume 265 val emptyMembers : members
69 : blume 297 val member :
70 : blume 632 { gp: GeneralParams.info,
71 : blume 666 rparse: SrcPath.file option ->
72 :     SrcPath.file * Version.t option * SrcPath.rebindings ->
73 :     group,
74 :     load_plugin: SrcPath.dir -> string -> bool }
75 : blume 587 -> { name: string,
76 : blume 666 mkpath: string -> SrcPath.prefile,
77 :     group: SrcPath.file * region,
78 : blume 587 class: cm_class option,
79 : blume 588 tooloptions: toolopt list option,
80 : blume 735 local_registry: toolregistry,
81 : blume 666 context: SrcPath.dir }
82 : blume 270 -> members
83 : blume 265 val members : members * members -> members
84 : blume 443 val guarded_members :
85 :     exp * (members * members) * (string -> unit) -> members
86 : blume 275 val error_member : (unit -> unit) -> members
87 : blume 265
88 : blume 267 (* constructing export lists *)
89 : blume 265 val emptyExports : exports
90 : blume 356 val export : ml_symbol * complainer -> exports
91 : blume 265 val exports : exports * exports -> exports
92 : blume 275 val guarded_exports :
93 :     exp * (exports * exports) * (string -> unit) -> exports
94 :     val error_export : (unit -> unit) -> exports
95 : blume 265
96 : blume 632 (* groups of operator symbols (to make grammar smaller) *)
97 :     type addsym
98 :     val PLUS : addsym
99 :     val MINUS : addsym
100 :    
101 :     type mulsym
102 :     val TIMES : mulsym
103 :     val DIV : mulsym
104 :     val MOD : mulsym
105 :    
106 :     type eqsym
107 :     val EQ : eqsym
108 :     val NE : eqsym
109 :    
110 :     type ineqsym
111 :     val GT : ineqsym
112 :     val GE : ineqsym
113 :     val LT : ineqsym
114 :     val LE : ineqsym
115 :    
116 : blume 267 (* arithmetic (number-valued) expression *)
117 : blume 265 val number : int -> aexp
118 : blume 336 val variable : GeneralParams.info -> cm_symbol -> aexp
119 : blume 632 val add : aexp * addsym * aexp -> aexp
120 :     val mul : aexp * mulsym * aexp -> aexp
121 :     val sign : addsym * aexp -> aexp
122 : blume 265 val negate : aexp -> aexp
123 :    
124 : blume 267 (* (bool-valued) expressions *)
125 : blume 265 val ml_defined : ml_symbol -> exp
126 : blume 336 val cm_defined : GeneralParams.info -> cm_symbol -> exp
127 : blume 265 val conj : exp * exp -> exp
128 :     val disj : exp * exp -> exp
129 : blume 632 val beq : exp * eqsym * exp -> exp
130 : blume 265 val not : exp -> exp
131 : blume 632 val ineq : aexp * ineqsym * aexp -> exp
132 :     val eq : aexp * eqsym * aexp -> exp
133 : blume 588
134 :     (* tool options *)
135 : blume 666 val string : { name: string, mkpath: string -> SrcPath.prefile } -> toolopt
136 : blume 588 val subopts : { name: string, opts: toolopt list } -> toolopt
137 : blume 265 end
138 :    
139 :     structure CMSemant :> CM_SEMANT = struct
140 :    
141 : blume 267 structure SymPath = GenericVC.SymPath
142 : blume 294 structure EM = GenericVC.ErrorMsg
143 :     structure GG = GroupGraph
144 : blume 267
145 : blume 666 type context = SrcPath.dir
146 : blume 297 type region = GenericVC.SourceMap.region
147 : blume 267 type ml_symbol = Symbol.symbol
148 : blume 265 type cm_symbol = string
149 : blume 380 type cm_class = string
150 : blume 632 type cm_version = Version.t
151 : blume 265
152 : blume 294 type group = GG.group
153 : blume 348 type privilegespec = { required: GG.privileges, wrapped: GG.privileges }
154 : blume 265
155 : blume 268 type environment = MemberCollection.collection
156 : blume 265
157 :     type aexp = environment -> int
158 :     type exp = environment -> bool
159 : blume 666 type members =
160 :     environment * SrcPath.file option -> MemberCollection.collection
161 : blume 267 type exports = environment -> SymbolSet.set
162 : blume 265
163 : blume 588 type toolopt = PrivateTools.toolopt
164 : blume 735 type toolregistry = PrivateTools.registry
165 : blume 588
166 : blume 735 val newToolRegistry = PrivateTools.newRegistry
167 :    
168 : blume 266 type complainer = string -> unit
169 :    
170 : blume 275 fun saveEval (exp, env, error) =
171 : blume 267 exp env
172 :     handle exn =>
173 : blume 275 (error ("expression raises exception: " ^ General.exnMessage exn);
174 :     false)
175 : blume 267
176 : blume 666 fun file_native (s, d, err) =
177 :     SrcPath.native { err = err } { context = d, spec = s }
178 : blume 632 fun file_standard (gp: GeneralParams.info) (s, d, err) =
179 : blume 666 SrcPath.standard { env = #penv (#param gp), err = err }
180 :     { context = d, spec = s }
181 : blume 265 fun cm_symbol s = s
182 : blume 632 fun cm_version (s, error) =
183 :     case Version.fromString s of
184 :     SOME v => v
185 :     | NONE => (error "ill-formed version specification"; Version.zero)
186 : blume 267 val ml_structure = Symbol.strSymbol
187 :     val ml_signature = Symbol.sigSymbol
188 :     val ml_functor = Symbol.fctSymbol
189 :     val ml_funsig = Symbol.fsigSymbol
190 : blume 265
191 : blume 380 fun class s = String.map Char.toLower s
192 :    
193 : blume 282 fun applyTo mc e = e mc
194 :    
195 : blume 340 fun sgl2sll subgroups = let
196 : blume 666 fun sameSL (p, _, _) (p', _, _) = SrcPath.compare (p, p') = EQUAL
197 : blume 340 fun add (x, l) =
198 :     if List.exists (sameSL x) l then l else x :: l
199 : blume 666 fun oneSG (x as (_, gth, _), l) =
200 : blume 652 case gth () of
201 :     GG.GROUP { kind, sublibs, ... } =>
202 :     (case kind of
203 : blume 733 GG.NOLIB _ => foldr add l sublibs
204 : blume 652 | _ => add (x, l))
205 :     | _ => l
206 : blume 340 in
207 : blume 733 foldr oneSG [] subgroups
208 : blume 340 end
209 :    
210 : blume 733 (* Filter out unused stuff and thunkify the group. *)
211 :     fun filt_th_sgl (sgl, imp_syms) = let
212 :     (* Add fake "structure <Pervasive>" so that we are sure not to lose
213 :     * the initgroup when filtering. *)
214 :     val ss = SymbolSet.add (imp_syms, PervAccess.pervStrSym)
215 :     fun add ((_, GG.ERRORGROUP, _), l) = l
216 :     | add ((p, g as GG.GROUP { exports, ... }, rb), l) = let
217 :     fun defined_here sy = SymbolMap.inDomain (exports, sy)
218 :     in
219 :     if SymbolSet.exists defined_here ss then
220 :     (p, fn () => g, rb) :: l
221 :     else l
222 :     end
223 :     in
224 :     foldr add [] sgl
225 :     end
226 :    
227 : blume 632 fun group arg = let
228 :     val { path = g, privileges = p, exports = e, members = m,
229 :     gp, curlib, owner, error, initgroup } = arg
230 : blume 642 val mc = applyTo (MemberCollection.implicit gp initgroup, curlib) m
231 : blume 283 val filter = Option.map (applyTo mc) e
232 : blume 537 val pfsbn = let
233 : blume 587 val { exports, ... } =
234 : blume 632 case initgroup of
235 : blume 587 GG.GROUP x => x
236 :     | GG.ERRORGROUP =>
237 : blume 632 EM.impossible "semant.sml: group: bad init group"
238 : blume 537 in
239 : blume 592 #1 (valOf (SymbolMap.find (exports, PervAccess.pervStrSym)))
240 : blume 537 end
241 : blume 733 val (exports, rp, isl) =
242 :     MemberCollection.build (mc, filter, gp, pfsbn ())
243 :     val subgroups = filt_th_sgl (MemberCollection.subgroups mc, isl)
244 : blume 348 val { required = rp', wrapped = wr } = p
245 :     val rp'' = StringSet.union (rp', StringSet.union (rp, wr))
246 : blume 280 in
247 : blume 632 if StringSet.isEmpty wr then ()
248 :     else EM.impossible "group with wrapped privileges";
249 : blume 348 GG.GROUP { exports = exports,
250 : blume 632 kind = GG.NOLIB { subgroups = subgroups, owner = owner },
251 : blume 348 required = rp'',
252 : blume 305 grouppath = g,
253 : blume 642 sources = MemberCollection.sources mc,
254 : blume 348 sublibs = sgl2sll subgroups }
255 : blume 280 end
256 :    
257 : blume 632 fun library arg = let
258 :     val { path = g, privileges = p, exports = e, members = m,
259 :     version, gp, initgroup } = arg
260 : blume 642 val mc = applyTo (MemberCollection.implicit gp initgroup, SOME g) m
261 : blume 632 val filter = SOME (applyTo mc e)
262 :     val pfsbn = let
263 :     val { exports, ... } =
264 :     case initgroup of
265 :     GG.GROUP x => x
266 :     | GG.ERRORGROUP =>
267 :     EM.impossible "semant.sml: lib: bad init group"
268 :     in
269 :     #1 (valOf (SymbolMap.find (exports, PervAccess.pervStrSym)))
270 :     end
271 : blume 733 val (exports, rp, isl) =
272 :     MemberCollection.build (mc, filter, gp, pfsbn ())
273 :     val subgroups = filt_th_sgl (MemberCollection.subgroups mc, isl)
274 : blume 632 val { required = rp', wrapped = wr } = p
275 :     val rp'' = StringSet.union (rp', StringSet.union (rp, wr))
276 :     in
277 :     GG.GROUP { exports = exports,
278 :     kind = GG.LIB { version = version,
279 :     kind = GG.DEVELOPED { subgroups = subgroups,
280 :     wrapped = wr } },
281 :     required = rp'',
282 :     grouppath = g,
283 : blume 642 sources = MemberCollection.sources mc,
284 : blume 632 sublibs = sgl2sll subgroups }
285 :     end
286 : blume 265
287 : blume 266 local
288 : blume 267 val isMember = StringSet.member
289 : blume 348 fun sanity ({ required, wrapped }, s, error) =
290 :     if isMember (required, s) orelse isMember (wrapped, s) then
291 : blume 283 error ("duplicate privilege name: " ^ s)
292 : blume 266 else ()
293 :     in
294 : blume 283 val initialPrivilegeSpec = { required = StringSet.empty,
295 : blume 348 wrapped = StringSet.empty }
296 :     fun require (a as ({ required, wrapped }, s, _)) =
297 : blume 266 (sanity a;
298 : blume 348 { required = StringSet.add (required, s), wrapped = wrapped })
299 :     fun wrap (a as ({ required, wrapped }, s, _)) =
300 : blume 266 (sanity a;
301 : blume 348 { required = required, wrapped = StringSet.add (wrapped, s) })
302 : blume 266 end
303 : blume 265
304 : blume 367 fun emptyMembers (env, _) = env
305 : blume 632 fun member { gp, rparse, load_plugin } arg (env, curlib) = let
306 :     val coll = MemberCollection.expandOne
307 :     { gp = gp, rparse = rparse curlib,
308 :     load_plugin = load_plugin }
309 :     arg
310 : blume 297 val group = #group arg
311 : blume 299 val error = GroupReg.error (#groupreg gp) group
312 : blume 294 fun e0 s = error EM.COMPLAIN s EM.nullErrorBody
313 : blume 666 fun checkowner (_, GG.GROUP { kind = GG.NOLIB { owner, ... }, ...},
314 :     _) =
315 :     let fun libname NONE = "<toplevel>"
316 : blume 573 | libname (SOME p) = SrcPath.descr p
317 :     fun eq (NONE, NONE) = true
318 :     | eq (SOME p, SOME p') = SrcPath.compare (p, p') = EQUAL
319 :     | eq _ = false
320 :     in
321 :     if eq (curlib, owner) then ()
322 : blume 587 else e0 (concat ["owner of subgroup (", libname owner,
323 : blume 573 ") does not match current library (",
324 : monnier 581 libname curlib, ")"])
325 : blume 573 end
326 :     | checkowner _ = ()
327 : blume 270 in
328 : blume 573 app checkowner (MemberCollection.subgroups coll);
329 : blume 277 MemberCollection.sequential (env, coll, e0)
330 : blume 270 end
331 : blume 367 fun members (m1, m2) (env, curlib) = m2 (m1 (env, curlib), curlib)
332 :     fun guarded_members (c, (m1, m2), error) (env, curlib) =
333 :     if saveEval (c, env, error) then m1 (env, curlib) else m2 (env, curlib)
334 :     fun error_member thunk (env, _) = (thunk (); env)
335 : blume 265
336 : blume 267 fun emptyExports env = SymbolSet.empty
337 : blume 356 fun export (s, error) env =
338 :     if MemberCollection.ml_look env s then SymbolSet.singleton s
339 :     else (error (concat ["exported ",
340 :     Symbol.nameSpaceToString (Symbol.nameSpace s),
341 :     " not defined: ", Symbol.name s]);
342 :     SymbolSet.empty)
343 : blume 267 fun exports (e1, e2) env = SymbolSet.union (e1 env, e2 env)
344 : blume 275 fun guarded_exports (c, (e1, e2), error) env =
345 :     if saveEval (c, env, error) then e1 env else e2 env
346 :     fun error_export thunk env = (thunk (); SymbolSet.empty)
347 : blume 265
348 : blume 632 datatype addsym = PLUS | MINUS
349 :     datatype mulsym = TIMES | DIV | MOD
350 :     datatype eqsym = EQ | NE
351 :     datatype ineqsym = GT | GE | LT | LE
352 :    
353 : blume 265 fun number i _ = i
354 : blume 336 fun variable gp v e = MemberCollection.num_look gp e v
355 : blume 632 fun add (e1, PLUS, e2) e = e1 e + e2 e
356 :     | add (e1, MINUS, e2) e = e1 e - e2 e
357 :     fun mul (e1, TIMES, e2) e = e1 e * e2 e
358 :     | mul (e1, DIV, e2) e = e1 e div e2 e
359 :     | mul (e1, MOD, e2) e = e1 e mod e2 e
360 :     fun sign (PLUS, ex) e = ex e
361 :     | sign (MINUS, ex) e = ~(ex e)
362 : blume 265 fun negate ex e = ~(ex e)
363 :    
364 : blume 267 fun ml_defined s e = MemberCollection.ml_look e s
365 : blume 336 fun cm_defined gp s e = MemberCollection.cm_look gp e s
366 : blume 265 fun conj (e1, e2) e = e1 e andalso e2 e
367 :     fun disj (e1, e2) e = e1 e orelse e2 e
368 : blume 632 fun beq (e1: exp, EQ, e2) e = e1 e = e2 e
369 :     | beq (e1, NE, e2) e = e1 e <> e2 e
370 : blume 265 fun not ex e = Bool.not (ex e)
371 : blume 632 fun ineq (e1, LT, e2) e = e1 e < e2 e
372 :     | ineq (e1, LE, e2) e = e1 e <= e2 e
373 :     | ineq (e1, GT, e2) e = e1 e > e2 e
374 :     | ineq (e1, GE, e2) e = e1 e >= e2 e
375 :     fun eq (e1: aexp, EQ, e2) e = e1 e = e2 e
376 :     | eq (e1, NE, e2) e = e1 e <> e2 e
377 : blume 588
378 :     val string = PrivateTools.STRING
379 :     val subopts = PrivateTools.SUBOPTS
380 : blume 265 end

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