annotate python/c3/typecheck.py @ 222:c3f1ce8b638f

Fixup of parser
author Windel Bouwman
date Tue, 09 Jul 2013 17:36:31 +0200
parents 848c4b15fd0b
children 1c7364bd74c7
rev   line source
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
1 from .astnodes import *
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
2 from .scope import *
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
3 from .visitor import Visitor
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
4
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
5 def equalTypes(a, b):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
6 """ Compare types a and b for equality. Not equal until proven otherwise. """
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
7 if type(a) is type(b):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
8 if type(a) is BaseType:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
9 return a.name == b.name
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
10 elif type(a) is PointerType:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
11 return equalTypes(a.ptype, b.ptype)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
12 return False
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
13
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
14 def canCast(fromT, toT):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
15 if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
16 return True
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
17 elif fromT is intType and isinstance(toT, PointerType):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
18 return True
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
19 return False
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
20
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
21 class TypeChecker:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
22 def __init__(self, diag):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
23 self.diag = diag
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
24
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
25 def error(self, msg, loc):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
26 """ Wrapper that registers the message and marks the result invalid """
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
27 self.diag.error(msg, loc)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
28 self.ok = False
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
29
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
30 def checkPackage(self, pkg):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
31 self.ok = True
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
32 visitor = Visitor()
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
33 visitor.visit(pkg, f_post=self.check2)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
34 return self.ok
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 check2(self, sym):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
37 if type(sym) in [IfStatement, WhileStatement]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
38 if not equalTypes(sym.condition.typ, boolType):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
39 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
40 elif type(sym) is Assignment:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
41 if not equalTypes(sym.lval.typ, sym.rval.typ):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
42 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
43 if not sym.lval.lvalue:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
44 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
45 #if sym.rval.lvalue:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
46 # self.error('Right hand side must be an rvalue', sym.rval.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
47 elif type(sym) is ReturnStatement:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
48 pass
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
49 elif type(sym) is FunctionCall:
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
50 if sym.proc:
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
51 # Check arguments:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
52 ngiv = len(sym.args)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
53 ptypes = sym.proc.typ.parametertypes
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
54 nreq = len(ptypes)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
55 if ngiv != nreq:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
56 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
57 else:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
58 for a, at in zip(sym.args, ptypes):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
59 if not equalTypes(a.typ, at):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
60 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
61 # determine return type:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
62 sym.typ = sym.proc.typ.returntype
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
63 else:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
64 sym.typ = intType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
65 elif type(sym) is VariableUse:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
66 sym.lvalue = True
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
67 if sym.target:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
68 sym.typ = sym.target.typ
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
69 else:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
70 sym.typ = intType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
71 elif type(sym) is Literal:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
72 sym.lvalue = False
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
73 if type(sym.val) is int:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
74 sym.typ = intType
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
75 elif type(sym.val) is float:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
76 sym.typ = doubleType
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
77 elif type(sym.val) is bool:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
78 sym.typ = boolType
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
79 else:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
80 self.error('Unknown literal type', sym.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
81 elif type(sym) is Unop:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
82 if sym.op == '&':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
83 sym.typ = PointerType(sym.a.typ)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
84 sym.lvalue = False
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
85 elif sym.op == '*':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
86 # pointer deref
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
87 sym.lvalue = True
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
88 if type(sym.a.typ) is PointerType:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
89 sym.typ = sym.a.typ.ptype
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
90 else:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
91 sym.typ = intType
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
92 self.error('Cannot dereference non-pointer type {}'.format(sym.a.typ), sym.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
93 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
94 print('unknown unop', sym.op)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
95 elif type(sym) is Binop:
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
96 sym.lvalue = False
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
97 if sym.op in ['+', '-', '*', '/']:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
98 if equalTypes(sym.a.typ, sym.b.typ):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
99 if equalTypes(sym.a.typ, intType):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
100 sym.typ = sym.a.typ
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
101 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
102 self.error('Can only add integers', sym.loc)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
103 sym.typ = intType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
104 else:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
105 # assume void here? TODO: throw exception!
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
106 sym.typ = intType
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
107 self.error('Types unequal', sym.loc)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
108 elif sym.op in ['>', '<', '==', '<=', '>=']:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
109 sym.typ = boolType
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
110 if not equalTypes(sym.a.typ, sym.b.typ):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
111 self.error('Types unequal', sym.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
112 elif sym.op in ['or', 'and']:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
113 sym.typ = boolType
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
114 if not equalTypes(sym.a.typ, boolType):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
115 self.error('Must be {0}'.format(boolType), sym.a.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
116 if not equalTypes(sym.b.typ, boolType):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
117 self.error('Must be {0}'.format(boolType), sym.b.loc)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
118 elif sym.op in ['|', '&']:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
119 sym.typ = intType
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
120 # TODO: elaborate?
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
121 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
122 sym.typ = voidType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
123 print('unknown binop', sym.op)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
124 elif type(sym) is Variable:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
125 # check initial value type:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
126 # TODO
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
127 pass
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
128 elif type(sym) is TypeCast:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
129 if canCast(sym.a.typ, sym.to_type):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
130 sym.typ = sym.to_type
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
131 else:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
132 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type))
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
133 elif type(sym) is Constant:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
134 if not equalTypes(sym.typ, sym.value.typ):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
135 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
136 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement]:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
137 pass
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
138 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
139 raise Exception('Unknown type check {0}'.format(sym))
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
140