1 (* Copyright (C) 2012,2017 Matthew Fluet.
2 * Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 * Copyright (C) 1997-2000 NEC Research Institute.
6 * MLton is released under a BSD-style license.
7 * See the file MLton-LICENSE for details.
10 functor ElaborateModules (S: ELABORATE_MODULES_STRUCTS): ELABORATE_MODULES =
16 open Control.Elaborate
18 val resolveScope = fn () => current resolveScope
24 structure FctArg = FctArg
25 structure Fctid = Fctid
26 structure Longstrid = Longstrid
27 structure SigConst = SigConst
28 structure Sigid = Sigid
29 structure Sigexp = Sigexp
30 structure Strdec = Strdec
31 structure Strexp = Strexp
32 structure Strid = Strid
33 structure Topdec = Topdec
40 structure FunctorClosure = FunctorClosure
41 structure Structure = Structure
44 structure ElaborateSigexp = ElaborateSigexp (structure Ast = Ast
47 structure ElaborateCore = ElaborateCore (structure Ast = Ast
48 structure CoreML = CoreML
52 val elabStrdecInfo = Trace.info "ElaborateModules.elabStrdec"
53 val elabStrexpInfo = Trace.info "ElaborateModules.elabStrexp"
54 val elabTopdecInfo = Trace.info "ElaborateModules.elabTopdec"
56 fun elaborateTopdec (topdec, {env = E: Env.t}) =
58 fun elabSigexp (s, no) =
59 ElaborateSigexp.elaborateSigexp
60 (s, {env = E, nest = case no of NONE => [] | SOME n => [n]})
61 fun elabSigexpConstraint (cons: SigConst.t,
62 S: Structure.t option,
64 : Decs.t * Structure.t option =
66 fun s (sigexp, opaque) =
71 | _ => concat (List.fold (nest, [], fn (s, ac) =>
75 NONE => (Decs.empty, NONE)
79 case elabSigexp (sigexp, SOME (Strid.toString Strid.uSig)) of
80 NONE => (S, Decs.empty)
93 SigConst.None => (Decs.empty, S)
94 | SigConst.Opaque sigexp => s (sigexp, true)
95 | SigConst.Transparent sigexp => s (sigexp, false)
97 fun elabStrdec (arg: Strdec.t * string list): Decs.t =
98 Trace.traceInfo' (elabStrdecInfo,
99 Layout.tuple2 (Strdec.layout,
100 List.layout String.layout),
102 (fn (d: Strdec.t, nest: string list) =>
104 val d = Strdec.coalesce d
105 val elabStrdec = fn d => elabStrdec (d, nest)
107 case Strdec.node d of
108 Strdec.Core d => (* rule 56 *)
109 ElaborateCore.elaborateDec
110 (d, {env = E, nest = nest})
111 | Strdec.Local (d, d') => (* rule 58 *)
113 fn () => elabStrdec d,
114 fn d => Decs.append (d, elabStrdec d'))
115 | Strdec.Seq ds => (* rule 60 *)
117 (ds, Decs.empty, fn (d, decs) =>
118 Decs.append (decs, elabStrdec d))
119 | Strdec.ShowBasis file =>
127 {compact = !Control.showBasisCompact,
128 def = !Control.showBasisDef,
129 flat = !Control.showBasisFlat,
131 prefixUnset = true}))
135 str "Exception raised processing #showBasis",
136 align [seq [str "file: ", File.layout file],
137 seq [str "exn: ", Exn.layout exn]])
141 | Strdec.Structure strbinds => (* rules 57, 61 *)
145 (strbinds, fn {name, def, constraint} =>
147 val nest = Strid.toString name :: nest
148 val (decs', S) = elabStrexp (def, nest)
150 elabSigexpConstraint (constraint, S, nest)
152 {decs = Decs.append (decs', decs''),
158 (strbinds, fn {name, S, ...} =>
159 Option.app (S, fn S => Env.extendStrid (E, name, S)))
161 Decs.appendsV (Vector.map (strbinds, #decs))
164 case resolveScope () of
165 Control.Elaborate.ResolveScope.Strdec =>
166 (ElaborateCore.reportUnresolvedFlexRecords ()
167 ; ElaborateCore.resolveOverloads ())
172 and elabStrexp (arg: Strexp.t * string list): Decs.t * Structure.t option =
173 Trace.traceInfo' (elabStrexpInfo,
174 Layout.tuple2 (Strexp.layout,
175 List.layout String.layout),
176 Layout.tuple2 (Decs.layout,
177 Option.layout Structure.layout))
178 (fn (e: Strexp.t, nest: string list) =>
180 val elabStrexp = fn e => elabStrexp (e, nest)
182 case Strexp.node e of
183 Strexp.App (fctid, strexp) => (* rules 54, 154 *)
185 val (decs, S) = elabStrexp strexp
190 case Env.lookupFctid (E, fctid) of
197 FunctorClosure.argInterface fct,
203 Strexp.region strexp))
204 val resId = Strid.uRes (Fctid.toString fctid)
207 (fct, S, [Strid.toString resId])
209 (Decs.appends [decs, decs', decs''], S)
212 | Strexp.Constrained (e, c) => (* rules 52, 53 *)
214 val (decs, S) = elabStrexp e
215 val (decs', S) = elabSigexpConstraint (c, S, nest)
217 (Decs.append (decs, decs'), S)
219 | Strexp.Let (d, e) => (* rule 55 *)
223 val decs = elabStrdec (d, nest)
224 val (decs', S) = elabStrexp e
226 (Decs.append (decs, decs'), S)
228 | Strexp.Struct d => (* rule 50 *)
231 Env.makeStructure (E, fn () => elabStrdec (d, nest))
235 | Strexp.Var p => (* rule 51 *)
236 (Decs.empty, Env.lookupLongstrid (E, p))
238 fun elabFunctor {arg, body, name, result}: FunctorClosure.t option =
240 val body = Strexp.constrained (body, result)
241 val argId = Strid.uArg (Fctid.toString name)
242 val (argSig, argDec) =
243 case FctArg.node arg of
244 FctArg.Structure (arg, argSig) =>
248 def = Strexp.var (Longstrid.short argId),
249 constraint = SigConst.None})
250 | FctArg.Spec spec =>
252 Strdec.openn (Vector.new1 (Longstrid.short argId)))
253 val body = Strexp.lett (argDec, body)
255 Option.map (elabSigexp (argSig, SOME (Strid.toString argId)), fn argInt =>
261 (Env.extendStrid (E, argId, formal)
262 ; elabStrexp (body, nest)))))
264 fun elabTopdec arg: Decs.t =
265 Trace.traceInfo' (elabTopdecInfo,
271 case Topdec.node d of
272 Topdec.Signature sigbinds =>
276 (sigbinds, fn (sigid, sigexp) =>
277 (sigid, elabSigexp (sigexp, SOME (Sigid.toString sigid))))
280 (sigbinds, fn (sigid, I) =>
281 Option.app (I, fn I => Env.extendSigid (E, sigid, I)))
285 | Topdec.Strdec d => elabStrdec (d, [])
286 | Topdec.Functor funbinds =>
287 (* Rules 85, 86. Appendix A, p.58 *)
291 (funbinds, fn {arg, body, name, result} =>
292 {closure = elabFunctor {arg = arg,
298 Vector.foreach (funbinds, fn {closure, name} =>
300 (closure, fn closure =>
301 Env.extendFctid (E, name, closure)))
306 case resolveScope () of
307 Control.Elaborate.ResolveScope.Topdec =>
308 (ElaborateCore.reportUnresolvedFlexRecords ()
309 ; ElaborateCore.resolveOverloads ())
311 val _ = ElaborateCore.reportUndeterminedTypes ()
312 val _ = ElaborateCore.reportSequenceNonUnit ()
320 val reportSequenceNonUnit = ElaborateCore.reportSequenceNonUnit
321 val reportUndeterminedTypes = ElaborateCore.reportUndeterminedTypes
322 val reportUnresolvedFlexRecords = ElaborateCore.reportUnresolvedFlexRecords
323 val resolveOverloads = ElaborateCore.resolveOverloads