comparison python/c3/typecheck.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents c3f1ce8b638f
children 240111e0456f
comparison
equal deleted inserted replaced
224:5af52987f5bd 225:1c7364bd74c7
32 visitor = Visitor() 32 visitor = Visitor()
33 visitor.visit(pkg, f_post=self.check2) 33 visitor.visit(pkg, f_post=self.check2)
34 return self.ok 34 return self.ok
35 35
36 def check2(self, sym): 36 def check2(self, sym):
37 if type(sym) in [IfStatement, WhileStatement]: 37 if type(sym) in [IfStatement, WhileStatement]:
38 if not equalTypes(sym.condition.typ, boolType): 38 if not equalTypes(sym.condition.typ, boolType):
39 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) 39 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc)
40 elif type(sym) is Assignment: 40 elif type(sym) is Assignment:
41 if not equalTypes(sym.lval.typ, sym.rval.typ): 41 if not equalTypes(sym.lval.typ, sym.rval.typ):
42 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) 42 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
43 if not sym.lval.lvalue: 43 if not sym.lval.lvalue:
44 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc) 44 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc)
45 #if sym.rval.lvalue: 45 #if sym.rval.lvalue:
46 # self.error('Right hand side must be an rvalue', sym.rval.loc) 46 # self.error('Right hand side must be an rvalue', sym.rval.loc)
47 elif type(sym) is ReturnStatement: 47 elif type(sym) is ReturnStatement:
48 pass 48 pass
49 elif type(sym) is FunctionCall: 49 elif type(sym) is FunctionCall:
50 if sym.proc: 50 if sym.proc:
51 # Check arguments: 51 # Check arguments:
52 ngiv = len(sym.args) 52 ngiv = len(sym.args)
53 ptypes = sym.proc.typ.parametertypes 53 ptypes = sym.proc.typ.parametertypes
54 nreq = len(ptypes) 54 nreq = len(ptypes)
60 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) 60 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
61 # determine return type: 61 # determine return type:
62 sym.typ = sym.proc.typ.returntype 62 sym.typ = sym.proc.typ.returntype
63 else: 63 else:
64 sym.typ = intType 64 sym.typ = intType
65 elif type(sym) is VariableUse: 65 elif type(sym) is VariableUse:
66 sym.lvalue = True 66 sym.lvalue = True
67 if sym.target: 67 if type(sym.target) is Variable:
68 sym.typ = sym.target.typ 68 sym.typ = sym.target.typ
69 else: 69 else:
70 print('warning {} has no target, defaulting to int'.format(sym))
70 sym.typ = intType 71 sym.typ = intType
71 elif type(sym) is Literal: 72 elif type(sym) is Literal:
72 sym.lvalue = False 73 sym.lvalue = False
73 if type(sym.val) is int: 74 if type(sym.val) is int:
74 sym.typ = intType 75 sym.typ = intType
75 elif type(sym.val) is float: 76 elif type(sym.val) is float:
76 sym.typ = doubleType 77 sym.typ = doubleType
77 elif type(sym.val) is bool: 78 elif type(sym.val) is bool:
78 sym.typ = boolType 79 sym.typ = boolType
79 else: 80 else:
80 self.error('Unknown literal type', sym.loc) 81 raise Exception('Unknown literal type'.format(sym.val))
81 elif type(sym) is Unop: 82 elif type(sym) is Unop:
82 if sym.op == '&': 83 if sym.op == '&':
83 sym.typ = PointerType(sym.a.typ) 84 sym.typ = PointerType(sym.a.typ)
84 sym.lvalue = False 85 sym.lvalue = False
85 elif sym.op == '*': 86 else:
86 # pointer deref 87 raise Exception('Unknown unop {0}'.format(sym.op))
87 sym.lvalue = True 88 elif type(sym) is Deref:
88 if type(sym.a.typ) is PointerType: 89 # pointer deref
89 sym.typ = sym.a.typ.ptype 90 sym.lvalue = True
91 # check if the to be dereferenced variable is a pointer type:
92 if type(sym.ptr.typ) is PointerType:
93 sym.typ = sym.ptr.typ.ptype
94 else:
95 self.error('Cannot dereference non-pointer type {}'.format(sym.ptr.typ), sym.loc)
96 sym.typ = intType
97 elif type(sym) is FieldRef:
98 basetype = sym.base.typ
99 sym.lvalue = True
100 if type(basetype) is StructureType:
101 if basetype.hasField(sym.field):
102 sym.typ = basetype.fieldType(sym.field)
90 else: 103 else:
104 self.error('{} does not contain field {}'.format(basetype, symfield), sym.loc)
91 sym.typ = intType 105 sym.typ = intType
92 self.error('Cannot dereference non-pointer type {}'.format(sym.a.typ), sym.loc)
93 else: 106 else:
94 print('unknown unop', sym.op) 107 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc)
95 elif type(sym) is Binop: 108 sym.typ = intType
109 elif type(sym) is Binop:
96 sym.lvalue = False 110 sym.lvalue = False
97 if sym.op in ['+', '-', '*', '/']: 111 if sym.op in ['+', '-', '*', '/']:
98 if equalTypes(sym.a.typ, sym.b.typ): 112 if equalTypes(sym.a.typ, sym.b.typ):
99 if equalTypes(sym.a.typ, intType): 113 if equalTypes(sym.a.typ, intType):
100 sym.typ = sym.a.typ 114 sym.typ = sym.a.typ
102 self.error('Can only add integers', sym.loc) 116 self.error('Can only add integers', sym.loc)
103 sym.typ = intType 117 sym.typ = intType
104 else: 118 else:
105 # assume void here? TODO: throw exception! 119 # assume void here? TODO: throw exception!
106 sym.typ = intType 120 sym.typ = intType
107 self.error('Types unequal', sym.loc) 121 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
108 elif sym.op in ['>', '<', '==', '<=', '>=']: 122 elif sym.op in ['>', '<', '==', '<=', '>=']:
109 sym.typ = boolType 123 sym.typ = boolType
110 if not equalTypes(sym.a.typ, sym.b.typ): 124 if not equalTypes(sym.a.typ, sym.b.typ):
111 self.error('Types unequal', sym.loc) 125 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
112 elif sym.op in ['or', 'and']: 126 elif sym.op in ['or', 'and']:
113 sym.typ = boolType 127 sym.typ = boolType
114 if not equalTypes(sym.a.typ, boolType): 128 if not equalTypes(sym.a.typ, boolType):
115 self.error('Must be {0}'.format(boolType), sym.a.loc) 129 self.error('Must be {0}'.format(boolType), sym.a.loc)
116 if not equalTypes(sym.b.typ, boolType): 130 if not equalTypes(sym.b.typ, boolType):
117 self.error('Must be {0}'.format(boolType), sym.b.loc) 131 self.error('Must be {0}'.format(boolType), sym.b.loc)
118 elif sym.op in ['|', '&']: 132 elif sym.op in ['|', '&']:
119 sym.typ = intType 133 sym.typ = intType
134 raise Exception('Not implemented')
120 # TODO: elaborate? 135 # TODO: elaborate?
121 else: 136 else:
122 sym.typ = voidType 137 raise Exception('Unknown binop {0}'.format(sym.op))
123 print('unknown binop', sym.op) 138 elif type(sym) is Variable:
124 elif type(sym) is Variable:
125 # check initial value type: 139 # check initial value type:
126 # TODO 140 # TODO
127 pass 141 pass
128 elif type(sym) is TypeCast: 142 elif type(sym) is TypeCast:
129 if canCast(sym.a.typ, sym.to_type): 143 if canCast(sym.a.typ, sym.to_type):
130 sym.typ = sym.to_type 144 sym.typ = sym.to_type
131 else: 145 else:
132 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type)) 146 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type))
133 elif type(sym) is Constant: 147 elif type(sym) is Constant:
134 if not equalTypes(sym.typ, sym.value.typ): 148 if not equalTypes(sym.typ, sym.value.typ):
135 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) 149 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
136 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement]: 150 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]:
137 pass 151 pass
138 else: 152 else:
139 raise Exception('Unknown type check {0}'.format(sym)) 153 raise Exception('Unknown type check {0}'.format(sym))
140 154