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