Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/compiler/typechecker/typechecker.sml
ViewVC logotype

Annotation of /trunk/src/compiler/typechecker/typechecker.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 86 - (view) (download)
Original Path: trunk/src/typechecker/typechecker.sml

1 : jhr 69 (* typechecker.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure Typechecker : sig
8 :    
9 : jhr 86 val check : Error.err_stream -> ParseTree.program -> AST.program
10 : jhr 69
11 :     end = struct
12 :    
13 : jhr 70 structure PT = ParseTree
14 : jhr 69 structure Ty = Types
15 : jhr 81 structure U = Util
16 : jhr 69
17 : jhr 86 type context = Error.err_stream * Error.span
18 :    
19 :     fun withContext ((errStrm, _), {span, tree}) =
20 :     ((errStrm, span), tree)
21 :     fun withEnvAndContext (env, (errStrm, _), {span, tree}) =
22 :     (env, (errStrm, span), tree)
23 :    
24 :     fun error ((errStrm, span), msg) = Error.errorAt(errStrm, span, msg)
25 :    
26 : jhr 83 val realZero = AST.E_Lit(Literal.Float(FloatLit.zero true))
27 :    
28 : jhr 70 (* check a differentiation level, which muse be >= 0 *)
29 :     fun checkDiff (cxt, k) =
30 :     if (k < 0)
31 :     then raise Fail "differentiation must be >= 0"
32 : jhr 75 else Ty.DiffConst(IntInf.toInt k)
33 : jhr 70
34 :     (* check a dimension, which must be 2 or 3 *)
35 :     fun checkDim (cxt, d) =
36 : jhr 82 if (d <= 0)
37 :     then raise Fail "invalid dimension; must be > 0"
38 : jhr 75 else Ty.DimConst(IntInf.toInt d)
39 : jhr 70
40 :     (* check a shape *)
41 :     fun checkShape (cxt, shape) = let
42 :     fun chkDim d = if (d < 1)
43 :     then raise Fail "invalid shape dimension; must be >= 1"
44 : jhr 75 else Ty.DimConst(IntInf.toInt d)
45 : jhr 70 in
46 :     Ty.Shape(List.map chkDim shape)
47 :     end
48 :    
49 : jhr 69 (* check the well-formedness of a type and translate it to an AST type *)
50 : jhr 70 fun checkTy (cxt, ty) = (case ty
51 : jhr 86 of PT.T_Mark m => checkTy(withContext(cxt, m))
52 : jhr 70 | PT.T_Bool => Ty.T_Bool
53 :     | PT.T_Int => Ty.T_Int
54 :     | PT.T_Real => Ty.realTy
55 :     | PT.T_String => Ty.T_String
56 :     | PT.T_Vec n => (* NOTE: the parser guarantees that 2 <= n <= 4 *)
57 :     Ty.vecTy(IntInf.toInt n)
58 :     | PT.T_Kernel k => Ty.T_Kernel(checkDiff(cxt, k))
59 :     | PT.T_Field{diff, dim, shape} => Ty.T_Field{
60 :     diff = checkDiff (cxt, diff),
61 :     dim = checkDim (cxt, dim),
62 :     shape = checkShape (cxt, shape)
63 :     }
64 :     | PT.T_Tensor shape => Ty.T_Tensor(checkShape(cxt, shape))
65 :     | PT.T_Image{dim, shape} => Ty.T_Image{
66 :     dim = checkDim (cxt, dim),
67 :     shape = checkShape (cxt, shape)
68 :     }
69 :     | PT.T_Array(ty, dims) => raise Fail "Array type"
70 : jhr 69 (* end case *))
71 :    
72 : jhr 71 fun checkLit lit = (case lit
73 :     of (Literal.Int _) => (AST.E_Lit lit, Ty.T_Int)
74 :     | (Literal.Float _) => (AST.E_Lit lit, Ty.realTy)
75 :     | (Literal.String s) => (AST.E_Lit lit, Ty.T_String)
76 :     | (Literal.Bool _) => (AST.E_Lit lit, Ty.T_Bool)
77 :     (* end case *))
78 :    
79 : jhr 85 (* resolve overloading: we use a simple scheme that selects the first operator in the
80 :     * list that matches the argument types.
81 :     *)
82 :     fun resolveOverload (rator, argTys, args, candidates) = let
83 :     fun tryCandidates [] = raise Fail(concat[
84 :     "unable to resolve overloaded operator \"", Atom.toString rator, "\""
85 :     ])
86 :     | tryCandidates (x::xs) = let
87 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf x)
88 :     in
89 :     if U.tryMatchTypes(domTy, argTys)
90 :     then (AST.E_Apply(x, tyArgs, args, rngTy), rngTy)
91 :     else tryCandidates xs
92 :     end
93 :     in
94 :     tryCandidates candidates
95 :     end
96 :    
97 : jhr 70 (* typecheck an expression and translate it to AST *)
98 : jhr 71 fun checkExpr (env, cxt, e) = (case e
99 : jhr 86 of PT.E_Mark m => checkExpr (withEnvAndContext (env, cxt, m))
100 : jhr 71 | PT.E_Var x => (case Env.findVar (env, x)
101 : jhr 81 of SOME x' => let
102 :     val (args, ty) = Util.instantiate(Var.typeOf x')
103 :     in
104 :     (AST.E_Var(x', args, ty), ty)
105 :     end
106 : jhr 71 | NONE => raise Fail "undefined variable"
107 :     (* end case *))
108 :     | PT.E_Lit lit => checkLit lit
109 : jhr 81 | PT.E_OrElse(e1, e2) => let
110 :     val (e1', ty1) = checkExpr(env, cxt, e1)
111 :     val (e2', ty2) = checkExpr(env, cxt, e2)
112 :     in
113 :     case (ty1, ty2)
114 :     of (Ty.T_Bool, Ty.T_Bool) =>
115 :     (AST.E_Cond(e1', AST.E_Lit(Literal.Bool true), e2'), Ty.T_Bool)
116 :     | _ => raise Fail "arguments to \"||\" must have bool type"
117 :     (* end case *)
118 :     end
119 :     | PT.E_AndAlso(e1, e2) => let
120 :     val (e1', ty1) = checkExpr(env, cxt, e1)
121 :     val (e2', ty2) = checkExpr(env, cxt, e2)
122 :     in
123 :     case (ty1, ty2)
124 :     of (Ty.T_Bool, Ty.T_Bool) =>
125 :     (AST.E_Cond(e1', e2', AST.E_Lit(Literal.Bool false)), Ty.T_Bool)
126 :     | _ => raise Fail "arguments to \"||\" must have bool type"
127 :     (* end case *)
128 :     end
129 :     | PT.E_BinOp(e1, rator, e2) => let
130 :     val (e1', ty1) = checkExpr(env, cxt, e1)
131 :     val (e2', ty2) = checkExpr(env, cxt, e2)
132 :     in
133 :     case Basis.findOp rator
134 :     of [rator] => let
135 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf rator)
136 :     in
137 :     if U.matchTypes(domTy, [ty1, ty2])
138 :     then (AST.E_Apply(rator, tyArgs, [e1', e2'], rngTy), rngTy)
139 : jhr 85 else raise Fail(concat[
140 :     "type error for binary operator \"", Var.nameOf rator, "\""
141 :     ])
142 : jhr 81 end
143 : jhr 85 | ovldList => resolveOverload (rator, [ty1, ty2], [e1', e2'], ovldList)
144 : jhr 81 (* end case *)
145 :     end
146 :     | PT.E_UnaryOp(rator, e) => let
147 :     val (e', ty) = checkExpr(env, cxt, e)
148 :     in
149 :     case Basis.findOp rator
150 :     of [rator] => let
151 :     val (tyArgs, Ty.T_Fun([domTy], rngTy)) = Util.instantiate(Var.typeOf rator)
152 :     in
153 :     if U.matchType(domTy, ty)
154 :     then (AST.E_Apply(rator, tyArgs, [e'], rngTy), rngTy)
155 : jhr 85 else raise Fail(concat[
156 :     "type error for unary operator \"", Var.nameOf rator, "\""
157 :     ])
158 : jhr 81 end
159 : jhr 85 | ovldList => resolveOverload (rator, [ty], [e'], ovldList)
160 : jhr 81 (* end case *)
161 :     end
162 :     | PT.E_Tuple args => let
163 :     val (args, tys) = checkExprList (env, cxt, args)
164 :     in
165 :     raise Fail "E_Tuple not yet implemented"
166 :     end
167 :     | PT.E_Apply(f, args) => let
168 :     val (args, tys) = checkExprList (env, cxt, args)
169 :     in
170 :     case Env.findVar (env, f)
171 :     of SOME f => (case Util.instantiate(Var.typeOf f)
172 :     of (tyArgs, Ty.T_Fun(domTy, rngTy)) =>
173 :     if U.matchTypes(domTy, tys)
174 :     then (AST.E_Apply(f, tyArgs, args, rngTy), rngTy)
175 :     else raise Fail "type error for application"
176 :     | _ => raise Fail "application of non-function"
177 :     (* end case *))
178 :     | NONE => raise Fail "unknown function"
179 :     (* end case *)
180 :     end
181 : jhr 86 | PT.E_Cons args => let
182 :     val (args, ty::tys) = checkExprList (env, cxt, args)
183 : jhr 81 in
184 : jhr 86 case Util.prune ty
185 :     of Ty.T_Tensor shape => let
186 :     fun chkTy ty' = U.matchType(ty, ty')
187 :     val resTy = Ty.T_Tensor(Ty.shapeExt(shape, Ty.DimConst(List.length args)))
188 : jhr 83 in
189 : jhr 86 if List.all chkTy tys
190 :     then (AST.E_Cons args, resTy)
191 :     else raise Fail "arguments of tensor construction must have same type"
192 : jhr 83 end
193 : jhr 86 | _ => raise Fail "Invalid argument type for tensor construction"
194 : jhr 83 (* end case *)
195 : jhr 81 end
196 : jhr 86 | PT.E_Real e => (case checkExpr (env, cxt, e)
197 :     of (e', Ty.T_Int) =>
198 :     (AST.E_Apply(BasisVars.i2r, [], [e'], Ty.realTy), Ty.realTy)
199 :     | _ => raise Fail "argument of real conversion must be int"
200 :     (* end case *))
201 : jhr 70 (* end case *))
202 :    
203 : jhr 81 (* typecheck a list of expressions returning a list of AST expressions and a list
204 :     * of types of the expressions.
205 :     *)
206 :     and checkExprList (env, cxt, exprs) = let
207 :     fun chk (e, (es, tys)) = let
208 :     val (e, ty) = checkExpr (env, cxt, e)
209 :     in
210 :     (e::es, ty::tys)
211 :     end
212 :     in
213 :     List.foldr chk ([], []) exprs
214 :     end
215 :    
216 : jhr 72 fun checkVarDecl (env, cxt, kind, d) = (case d
217 : jhr 86 of PT.VD_Mark m => checkVarDecl (env, (#1 cxt, #span m), kind, #tree m)
218 : jhr 72 | PT.VD_Decl(ty, x, e) => let
219 : jhr 81 val ty = checkTy (cxt, ty)
220 : jhr 72 val x' = Var.new (x, kind, ty)
221 :     val (e', ty') = checkExpr (env, cxt, e)
222 :     in
223 :     (* FIXME: check types *)
224 : jhr 81 (x, x', e')
225 : jhr 72 end
226 :     (* end case *))
227 :    
228 : jhr 70 (* typecheck a statement and translate it to AST *)
229 : jhr 71 fun checkStmt (env, cxt, s) = (case s
230 : jhr 86 of PT.S_Mark m => checkStmt (withEnvAndContext (env, cxt, m))
231 : jhr 72 | PT.S_Block stms => let
232 :     fun chk (_, [], stms) = AST.S_Block(List.rev stms)
233 :     | chk (env, s::ss, stms) = let
234 :     val (s', env') = checkStmt (env, cxt, s)
235 :     in
236 : jhr 81 chk (env', ss, s'::stms)
237 : jhr 72 end
238 :     in
239 :     (chk (env, stms, []), env)
240 :     end
241 :     | PT.S_Decl vd => let
242 : jhr 81 val (x, x', e) = checkVarDecl (env, cxt, Var.LocalVar, vd)
243 : jhr 72 in
244 : jhr 81 (AST.S_Decl(AST.VD_Decl(x', e)), Env.insertLocal(env, x, x'))
245 : jhr 72 end
246 :     | PT.S_IfThen(e, s) => let
247 :     val (e', ty) = checkExpr (env, cxt, e)
248 : jhr 81 val (s', _) = checkStmt (env, cxt, s)
249 : jhr 72 in
250 :     (* check that condition has bool type *)
251 :     case ty
252 :     of Ty.T_Bool => ()
253 :     | _ => raise Fail "condition not boolean type"
254 :     (* end case *);
255 :     (AST.S_IfThenElse(e', s', AST.S_Block[]), env)
256 :     end
257 :     | PT.S_IfThenElse(e, s1, s2) => let
258 :     val (e', ty) = checkExpr (env, cxt, e)
259 : jhr 81 val (s1', _) = checkStmt (env, cxt, s1)
260 :     val (s2', _) = checkStmt (env, cxt, s2)
261 : jhr 72 in
262 :     (* check that condition has bool type *)
263 :     case ty
264 :     of Ty.T_Bool => ()
265 :     | _ => raise Fail "condition not boolean type"
266 :     (* end case *);
267 :     (AST.S_IfThenElse(e', s1', s2'), env)
268 :     end
269 :     | PT.S_Assign(x, e) => (case Env.findVar (env, x)
270 :     of NONE => raise Fail "undefined variable"
271 :     | SOME x' => let
272 :     val (e', ty) = checkExpr (env, cxt, e)
273 :     in
274 :     (* FIXME: check types *)
275 :     (* check that x' is mutable *)
276 :     case Var.kindOf x'
277 :     of Var.ActorStateVar => ()
278 :     | Var.LocalVar => ()
279 :     | _ => raise Fail "assignment to immutable variable"
280 :     (* end case *);
281 :     (AST.S_Assign(x', e'), env)
282 :     end
283 :     (* end case *))
284 :     | PT.S_New(actor, args) => let
285 : jhr 81 val argsAndTys' = List.map (fn e => checkExpr(env, cxt, e)) args
286 :     val (args', tys') = ListPair.unzip argsAndTys'
287 : jhr 72 in
288 :     (* FIXME: check that actor is defined and has the argument types match *)
289 : jhr 81 (AST.S_New(actor, args'), env)
290 : jhr 72 end
291 :     | PT.S_Die => (AST.S_Die, env)
292 :     | PT.S_Stabilize => (AST.S_Stabilize, env)
293 : jhr 70 (* end case *))
294 :    
295 : jhr 82 fun checkParams (env, cxt, params) = let
296 :     fun chkParam (env, cxt, param) = (case param
297 : jhr 86 of PT.P_Mark m => chkParam (withEnvAndContext (env, cxt, m))
298 : jhr 82 | PT.P_Param(ty, x) => let
299 :     val x' = Var.new(x, AST.ActorParam, checkTy (cxt, ty))
300 :     in
301 :     (x', Env.insertLocal(env, x, x'))
302 :     end
303 :     (* end case *))
304 :     fun chk (param, (xs, env)) = let
305 :     val (x, env) = chkParam (env, cxt, param)
306 :     in
307 :     (x::xs, env)
308 :     end
309 :     in
310 :     (* FIXME: need to check for multiple occurences of the same parameter name! *)
311 :     List.foldr chk ([], env) params
312 :     end
313 :    
314 :     fun checkMethod (env, cxt, meth) = (case meth
315 : jhr 86 of PT.M_Mark m => checkMethod (withEnvAndContext (env, cxt, m))
316 : jhr 82 | PT.M_Method(name, body) => let
317 :     val (body, _) = checkStmt(env, cxt, body)
318 :     in
319 :     AST.M_Method(name, body)
320 :     end
321 :     (* end case *))
322 :    
323 :     fun checkActor (env, cxt, {name, params, state, methods}) = let
324 :     (* check the actor parameters *)
325 :     val (params, env) = checkParams (env, cxt, params)
326 :     (* check the actor state variable definitions *)
327 :     val (vds, env) = let
328 :     fun checkStateVar (vd, (vds, env)) = let
329 :     val (x, x', e') = checkVarDecl (env, cxt, AST.ActorStateVar, vd)
330 :     in
331 :     (AST.VD_Decl(x', e')::vds, Env.insertLocal(env, x, x'))
332 :     end
333 :     val (vds, env) = List.foldl checkStateVar ([], env) state
334 :     in
335 :     (List.rev vds, env)
336 :     end
337 :     (* check the actor methods *)
338 :     val methods = List.map (fn m => checkMethod (env, cxt, m)) methods
339 :     in
340 :     AST.D_Actor{name = name, params = params, state = vds, methods = methods}
341 :     end
342 :    
343 : jhr 71 fun checkDecl (env, cxt, d) = (case d
344 : jhr 86 of PT.D_Mark m => checkDecl (withEnvAndContext (env, cxt, m))
345 : jhr 72 | PT.D_Input(ty, x, optExp) => let
346 : jhr 71 val ty = checkTy(cxt, ty)
347 :     val x' = Var.new(x, Var.InputVar, ty)
348 :     val dcl = (case optExp
349 :     of NONE => AST.D_Input(x', NONE)
350 :     | SOME e => let
351 :     val (e', ty') = checkExpr (env, cxt, e)
352 :     in
353 :     (* FIXME: check types *)
354 :     AST.D_Input(x', SOME e')
355 :     end
356 :     (* end case *))
357 :     in
358 :     (dcl, Env.insertGlobal(env, x, x'))
359 :     end
360 : jhr 72 | PT.D_Var vd => let
361 : jhr 81 val (x, x', e') = checkVarDecl (env, cxt, Var.GlobalVar, vd)
362 : jhr 72 in
363 : jhr 81 (AST.D_Var(AST.VD_Decl(x', e')), Env.insertGlobal(env, x, x'))
364 : jhr 72 end
365 : jhr 82 | PT.D_Actor arg => (checkActor(env, cxt, arg), env)
366 : jhr 81 | PT.D_InitialArray(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
367 :     | PT.D_InitialCollection(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
368 : jhr 70 (* end case *))
369 :    
370 : jhr 86 fun check errStrm (PT.Program{span, tree}) = let
371 :     val cxt = (errStrm, span)
372 : jhr 81 fun chk (env, [], dcls') = AST.Program(List.rev dcls')
373 :     | chk (env, dcl::dcls, dcls') = let
374 : jhr 86 val (dcl', env) = checkDecl (env, cxt, dcl)
375 : jhr 81 in
376 :     chk (env, dcls, dcl'::dcls')
377 :     end
378 :     in
379 :     chk (Basis.env, tree, [])
380 :     end
381 : jhr 70
382 : jhr 69 end

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