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