Mercurial > lcfOS
annotate python/c3/typecheck.py @ 272:e64bae57cda8
refactor ir
author | Windel Bouwman |
---|---|
date | Sat, 31 Aug 2013 17:58:54 +0200 |
parents | c4370696ccc7 |
children | 1c7c1e619be8 |
rev | line source |
---|---|
163 | 1 from .astnodes import * |
2 from .scope import * | |
3 from .visitor import Visitor | |
4 | |
231 | 5 def theType(t): |
6 """ | |
7 Recurse until a 'real' type is found | |
8 """ | |
230 | 9 if type(t) is DefinedType: |
231 | 10 return theType(t.typ) |
230 | 11 return t |
12 | |
163 | 13 def equalTypes(a, b): |
230 | 14 """ |
228 | 15 Compare types a and b for equality. |
16 Not equal until proven otherwise. | |
17 """ | |
226 | 18 # Recurse into named types: |
231 | 19 a = theType(a) |
20 b = theType(b) | |
230 | 21 |
226 | 22 # Compare for structural equivalence: |
23 if type(a) is type(b): | |
24 if type(a) is BaseType: | |
25 return a.name == b.name | |
26 elif type(a) is PointerType: | |
27 return equalTypes(a.ptype, b.ptype) | |
227 | 28 elif type(a) is StructureType: |
29 if len(a.mems) != len(b.mems): | |
30 return False | |
31 for amem, bmem in zip(a.mems, b.mems): | |
32 if not equalTypes(amem.typ, bmem.typ): | |
33 return False | |
34 return True | |
35 else: | |
272 | 36 raise Exception('Type compare for {} not implemented'.format(type(a))) |
226 | 37 return False |
150 | 38 |
222 | 39 def canCast(fromT, toT): |
231 | 40 fromT = theType(fromT) |
41 toT = theType(toT) | |
222 | 42 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): |
43 return True | |
44 elif fromT is intType and isinstance(toT, PointerType): | |
45 return True | |
46 return False | |
47 | |
230 | 48 def expectRval(s): |
49 # TODO: solve this better | |
50 s.expect_rvalue = True | |
51 | |
150 | 52 class TypeChecker: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
53 def __init__(self, diag): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
54 self.diag = diag |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
55 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
56 def error(self, msg, loc): |
230 | 57 """ |
58 Wrapper that registers the message and marks the result invalid | |
228 | 59 """ |
186 | 60 self.diag.error(msg, loc) |
61 self.ok = False | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
62 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
63 def checkPackage(self, pkg): |
186 | 64 self.ok = True |
215 | 65 visitor = Visitor() |
66 visitor.visit(pkg, f_post=self.check2) | |
186 | 67 return self.ok |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
68 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
69 def check2(self, sym): |
225 | 70 if type(sym) in [IfStatement, WhileStatement]: |
71 if not equalTypes(sym.condition.typ, boolType): | |
230 | 72 msg = 'Condition must be of type {}'.format(boolType) |
73 self.error(msg, sym.condition.loc) | |
225 | 74 elif type(sym) is Assignment: |
230 | 75 l, r = sym.lval, sym.rval |
76 if not equalTypes(l.typ, r.typ): | |
77 msg = 'Cannot assign {} to {}'.format(r.typ, l.typ) | |
78 self.error(msg, sym.loc) | |
79 if not l.lvalue: | |
80 self.error('No valid lvalue {}'.format(l), l.loc) | |
221 | 81 #if sym.rval.lvalue: |
82 # self.error('Right hand side must be an rvalue', sym.rval.loc) | |
230 | 83 expectRval(sym.rval) |
225 | 84 elif type(sym) is ReturnStatement: |
85 pass | |
86 elif type(sym) is FunctionCall: | |
167 | 87 # Check arguments: |
88 ngiv = len(sym.args) | |
89 ptypes = sym.proc.typ.parametertypes | |
90 nreq = len(ptypes) | |
91 if ngiv != nreq: | |
186 | 92 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) |
167 | 93 else: |
94 for a, at in zip(sym.args, ptypes): | |
230 | 95 expectRval(a) |
167 | 96 if not equalTypes(a.typ, at): |
186 | 97 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) |
167 | 98 # determine return type: |
99 sym.typ = sym.proc.typ.returntype | |
225 | 100 elif type(sym) is VariableUse: |
221 | 101 sym.lvalue = True |
272 | 102 if isinstance(sym.target, Variable): |
221 | 103 sym.typ = sym.target.typ |
104 else: | |
225 | 105 print('warning {} has no target, defaulting to int'.format(sym)) |
221 | 106 sym.typ = intType |
225 | 107 elif type(sym) is Literal: |
221 | 108 sym.lvalue = False |
109 if type(sym.val) is int: | |
110 sym.typ = intType | |
111 elif type(sym.val) is float: | |
112 sym.typ = doubleType | |
113 elif type(sym.val) is bool: | |
114 sym.typ = boolType | |
115 else: | |
225 | 116 raise Exception('Unknown literal type'.format(sym.val)) |
117 elif type(sym) is Unop: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
118 if sym.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
119 sym.typ = PointerType(sym.a.typ) |
221 | 120 sym.lvalue = False |
225 | 121 else: |
122 raise Exception('Unknown unop {0}'.format(sym.op)) | |
123 elif type(sym) is Deref: | |
124 # pointer deref | |
125 sym.lvalue = True | |
126 # check if the to be dereferenced variable is a pointer type: | |
231 | 127 ptype = theType(sym.ptr.typ) |
226 | 128 if type(ptype) is PointerType: |
129 sym.typ = ptype.ptype | |
225 | 130 else: |
226 | 131 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc) |
225 | 132 sym.typ = intType |
133 elif type(sym) is FieldRef: | |
134 basetype = sym.base.typ | |
230 | 135 sym.lvalue = sym.base.lvalue |
231 | 136 basetype = theType(basetype) |
225 | 137 if type(basetype) is StructureType: |
138 if basetype.hasField(sym.field): | |
139 sym.typ = basetype.fieldType(sym.field) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
140 else: |
226 | 141 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc) |
221 | 142 sym.typ = intType |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
143 else: |
225 | 144 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) |
145 sym.typ = intType | |
146 elif type(sym) is Binop: | |
227 | 147 sym.lvalue = False |
232 | 148 if sym.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']: |
230 | 149 expectRval(sym.a) |
150 expectRval(sym.b) | |
227 | 151 if equalTypes(sym.a.typ, sym.b.typ): |
152 if equalTypes(sym.a.typ, intType): | |
153 sym.typ = sym.a.typ | |
154 else: | |
155 self.error('Can only add integers', sym.loc) | |
156 sym.typ = intType | |
157 else: | |
158 # assume void here? TODO: throw exception! | |
159 sym.typ = intType | |
160 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) | |
161 elif sym.op in ['>', '<', '==', '<=', '>=']: | |
252 | 162 expectRval(sym.a) |
163 expectRval(sym.b) | |
227 | 164 sym.typ = boolType |
165 if not equalTypes(sym.a.typ, sym.b.typ): | |
166 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) | |
167 elif sym.op in ['or', 'and']: | |
168 sym.typ = boolType | |
169 if not equalTypes(sym.a.typ, boolType): | |
170 self.error('Must be {0}'.format(boolType), sym.a.loc) | |
171 if not equalTypes(sym.b.typ, boolType): | |
172 self.error('Must be {0}'.format(boolType), sym.b.loc) | |
163 | 173 else: |
227 | 174 raise Exception('Unknown binop {0}'.format(sym.op)) |
272 | 175 elif isinstance(sym, Variable): |
231 | 176 # check initial value type: |
177 # TODO | |
178 pass | |
225 | 179 elif type(sym) is TypeCast: |
222 | 180 if canCast(sym.a.typ, sym.to_type): |
181 sym.typ = sym.to_type | |
182 else: | |
230 | 183 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type), sym.loc) |
184 sym.typ = intType | |
225 | 185 elif type(sym) is Constant: |
186 if not equalTypes(sym.typ, sym.value.typ): | |
187 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) | |
228 | 188 elif type(sym) in [CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]: |
230 | 189 pass |
225 | 190 else: |
252 | 191 raise NotImplementedError('Unknown type check {0}'.format(sym)) |
150 | 192 |