annotate python/c3/analyse.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents 1c7c1e619be8
children bd2593de3ff8
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
1 import logging
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
2 from .visitor import Visitor
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
3 from .astnodes import *
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
4 from .scope import *
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
5
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
6
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
7 class C3Pass:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
8 def __init__(self, diag):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
9 self.diag = diag
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
10 self.logger = logging.getLogger('c3')
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
11 self.ok = True
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
12 self.visitor = Visitor()
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
13
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
14 def error(self, msg, loc=None):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
15 self.ok = False
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
16 self.diag.error(msg, loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
17
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
18 def visit(self, pkg, pre, post):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
19 self.visitor.visit(pkg, pre, post)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
20
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
21
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
22 class AddScope(C3Pass):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
23 """ Scope is attached to the correct modules. """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
24 def addScope(self, pkg):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
25 self.logger.info('Adding scoping to package {}'.format(pkg.name))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
26 # Prepare top level scope and set scope to all objects:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
27 self.scopeStack = [topScope]
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
28 modScope = Scope(self.CurrentScope)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
29 self.scopeStack.append(modScope)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
30 self.visit(pkg, self.enterScope, self.quitScope)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
31 assert len(self.scopeStack) == 2
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
32 return self.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
33
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
34 @property
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
35 def CurrentScope(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
36 return self.scopeStack[-1]
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
37
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
38 def addSymbol(self, sym):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
39 if self.CurrentScope.hasSymbol(sym.name):
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
40 self.error('Redefinition of {0}'.format(sym.name), sym.loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
41 else:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
42 self.CurrentScope.addSymbol(sym)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
43
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
44 def enterScope(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
45 # Distribute the scope:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
46 sym.scope = self.CurrentScope
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
47
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
48 # Add symbols to current scope:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
49 if isinstance(sym, Symbol) or isinstance(sym, DefinedType):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
50 self.addSymbol(sym)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
51
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
52 # Create subscope:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
53 if type(sym) in [Package, Function]:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
54 newScope = Scope(self.CurrentScope)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
55 self.scopeStack.append(newScope)
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
56 sym.innerScope = self.CurrentScope
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
57
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
58 def quitScope(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
59 # Pop out of scope:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
60 if type(sym) in [Package, Function]:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
61 self.scopeStack.pop(-1)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
62
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
63
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
64 class Analyzer(C3Pass):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
65 """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
66 Context handling is done here.
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
67 Scope is attached to the correct modules.
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
68 This class checks names and references.
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
69 """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
70
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
71 def analyzePackage(self, pkg, packageDict):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
72 self.ok = True
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
73 # Prepare top level scope and set scope to all objects:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
74 AddScope(self.diag).addScope(pkg)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
75
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
76 self.logger.info('Resolving imports for package {}'.format(pkg.name))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
77 # Handle imports:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
78 for i in pkg.imports:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
79 ip = packageDict[i]
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
80 if not ip:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
81 self.error('Cannot import {}'.format(i))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
82 continue
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
83 pkg.scope.addSymbol(ip)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
84 FixRefs(self.diag).fixRefs(pkg)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
85 return self.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
86
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
87
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
88 class FixRefs(C3Pass):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
89 def fixRefs(self, pkg):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
90 self.visitor.visit(pkg, self.findRefs)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
91
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
92 # Reference fixups:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
93 def resolveDesignator(self, d, scope):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
94 assert type(d) is Designator, type(d)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
95 assert type(scope) is Scope
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
96 if scope.hasSymbol(d.tname):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
97 s = scope.getSymbol(d.tname)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
98 if hasattr(s, 'addRef'):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
99 # TODO: make this nicer
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
100 s.addRef(None)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
101 return s
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
102 else:
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
103 self.error('Cannot resolve name {0}'.format(d.tname), d.loc)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
104
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
105 def resolveType(self, t, scope):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
106 if type(t) is PointerType:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
107 t.ptype = self.resolveType(t.ptype, scope)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
108 return t
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
109 elif type(t) is StructureType:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
110 offset = 0
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
111 for mem in t.mems:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
112 mem.offset = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
113 mem.typ = self.resolveType(mem.typ, scope)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
114 offset += theType(mem.typ).bytesize
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
115 t.bytesize = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
116 return t
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
117 elif type(t) is Designator:
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
118 t = self.resolveDesignator(t, scope)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
119 if t:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
120 return self.resolveType(t, scope)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
121 elif isinstance(t, Type):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
122 # Already resolved??
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
123 return t
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
124 else:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
125 raise Exception('Error resolving type {} {}'.format(t, type(t)))
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
126
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
127 def findRefs(self, sym):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 255
diff changeset
128 if type(sym) in [Constant] or isinstance(sym, Variable):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
129 sym.typ = self.resolveType(sym.typ, sym.scope)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
130 elif type(sym) is TypeCast:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
131 sym.to_type = self.resolveType(sym.to_type, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
132 elif type(sym) is VariableUse:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
133 sym.target = self.resolveDesignator(sym.target, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
134 elif type(sym) is FunctionCall:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
135 varuse = sym.proc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
136 sym.proc = self.resolveDesignator(varuse.target, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
137 elif type(sym) is Function:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
138 # Checkup function type:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
139 ft = sym.typ
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
140 ft.returntype = self.resolveType(ft.returntype, sym.scope)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
141 ft.parametertypes = [self.resolveType(pt, sym.scope) for pt in
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
142 ft.parametertypes]
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
143 # Mark local variables:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
144 for d in sym.declarations:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
145 if isinstance(d, Variable):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
146 d.isLocal = True
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
147 elif type(sym) is DefinedType:
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
148 sym.typ = self.resolveType(sym.typ, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
149
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
150
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
151 # Type checking:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
152
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
153 def theType(t):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
154 """ Recurse until a 'real' type is found """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
155 if type(t) is DefinedType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
156 return theType(t.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
157 return t
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
158
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
159
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
160 def equalTypes(a, b):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
161 """ Compare types a and b for structural equavalence. """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
162 # Recurse into named types:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
163 a, b = theType(a), theType(b)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
164
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
165 if type(a) is type(b):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
166 if type(a) is BaseType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
167 return a.name == b.name
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
168 elif type(a) is PointerType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
169 return equalTypes(a.ptype, b.ptype)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
170 elif type(a) is StructureType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
171 if len(a.mems) != len(b.mems):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
172 return False
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
173 return all(equalTypes(am.typ, bm.typ) for am, bm in
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
174 zip(a.mems, b.mems))
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
175 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
176 raise NotImplementedError(
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
177 'Type compare for {} not implemented'.format(type(a)))
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
178 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
179
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
180
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
181 def canCast(fromT, toT):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
182 fromT = theType(fromT)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
183 toT = theType(toT)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
184 if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
185 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
186 elif fromT is intType and isinstance(toT, PointerType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
187 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
188 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
189
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
190
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
191 def expectRval(s):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
192 # TODO: solve this better
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
193 s.expect_rvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
194
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
195
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
196 class TypeChecker(C3Pass):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
197 def checkPackage(self, pkg):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
198 self.ok = True
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
199 self.visit(pkg, None, self.check2)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
200 return self.ok
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
201
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
202 def check2(self, sym):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
203 if type(sym) in [IfStatement, WhileStatement]:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
204 if not equalTypes(sym.condition.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
205 msg = 'Condition must be of type {}'.format(boolType)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
206 self.error(msg, sym.condition.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
207 elif type(sym) is Assignment:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
208 l, r = sym.lval, sym.rval
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
209 if not equalTypes(l.typ, r.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
210 msg = 'Cannot assign {} to {}'.format(r.typ, l.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
211 self.error(msg, sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
212 if not l.lvalue:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
213 self.error('No valid lvalue {}'.format(l), l.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
214 #if sym.rval.lvalue:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
215 # self.error('Right hand side must be an rvalue', sym.rval.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
216 expectRval(sym.rval)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
217 elif type(sym) is ReturnStatement:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
218 pass
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
219 elif type(sym) is FunctionCall:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
220 # Check arguments:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
221 ngiv = len(sym.args)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
222 ptypes = sym.proc.typ.parametertypes
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
223 nreq = len(ptypes)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
224 if ngiv != nreq:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
225 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
226 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
227 for a, at in zip(sym.args, ptypes):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
228 expectRval(a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
229 if not equalTypes(a.typ, at):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
230 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
231 # determine return type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
232 sym.typ = sym.proc.typ.returntype
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
233 elif type(sym) is VariableUse:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
234 sym.lvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
235 if isinstance(sym.target, Variable):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
236 sym.typ = sym.target.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
237 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
238 print('warning {} has no target, defaulting to int'.format(sym))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
239 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
240 elif type(sym) is Literal:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
241 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
242 if type(sym.val) is int:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
243 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
244 elif type(sym.val) is float:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
245 sym.typ = doubleType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
246 elif type(sym.val) is bool:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
247 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
248 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
249 raise Exception('Unknown literal type'.format(sym.val))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
250 elif type(sym) is Unop:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
251 if sym.op == '&':
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
252 sym.typ = PointerType(sym.a.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
253 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
254 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
255 raise Exception('Unknown unop {0}'.format(sym.op))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
256 elif type(sym) is Deref:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
257 # pointer deref
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
258 sym.lvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
259 # check if the to be dereferenced variable is a pointer type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
260 ptype = theType(sym.ptr.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
261 if type(ptype) is PointerType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
262 sym.typ = ptype.ptype
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
263 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
264 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
265 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
266 elif type(sym) is FieldRef:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
267 basetype = sym.base.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
268 sym.lvalue = sym.base.lvalue
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
269 basetype = theType(basetype)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
270 if type(basetype) is StructureType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
271 if basetype.hasField(sym.field):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
272 sym.typ = basetype.fieldType(sym.field)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
273 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
274 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
275 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
276 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
277 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
278 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
279 elif type(sym) is Binop:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
280 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
281 if sym.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
282 expectRval(sym.a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
283 expectRval(sym.b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
284 if equalTypes(sym.a.typ, sym.b.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
285 if equalTypes(sym.a.typ, intType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
286 sym.typ = sym.a.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
287 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
288 self.error('Can only add integers', sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
289 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
290 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
291 # assume void here? TODO: throw exception!
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
292 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
293 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
294 elif sym.op in ['>', '<', '==', '<=', '>=']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
295 expectRval(sym.a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
296 expectRval(sym.b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
297 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
298 if not equalTypes(sym.a.typ, sym.b.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
299 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
300 elif sym.op in ['or', 'and']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
301 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
302 if not equalTypes(sym.a.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
303 self.error('Must be {0}'.format(boolType), sym.a.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
304 if not equalTypes(sym.b.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
305 self.error('Must be {0}'.format(boolType), sym.b.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
306 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
307 raise Exception('Unknown binop {0}'.format(sym.op))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
308 elif isinstance(sym, Variable):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
309 # check initial value type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
310 # TODO
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
311 pass
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
312 elif type(sym) is TypeCast:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
313 if canCast(sym.a.typ, sym.to_type):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
314 sym.typ = sym.to_type
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
315 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
316 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
317 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
318 elif type(sym) is Constant:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
319 if not equalTypes(sym.typ, sym.value.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
320 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
321 elif type(sym) in [CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
322 pass
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
323 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
324 raise NotImplementedError('Unknown type check {0}'.format(sym))