annotate python/c3/analyse.py @ 289:bd2593de3ff8

Semifix burn2
author Windel Bouwman
date Thu, 21 Nov 2013 15:46:50 +0100
parents a747a45dcd78
children 6aa721e7b10b
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
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
75 self.logger.info('Resolving imports for package {}'.format(pkg.name))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
76 # Handle imports:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
77 for i in pkg.imports:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
78 ip = packageDict[i]
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
79 if not ip:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
80 self.error('Cannot import {}'.format(i))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
81 continue
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
82 pkg.scope.addSymbol(ip)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
83 FixRefs(self.diag).fixRefs(pkg)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
84 return self.ok
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
85
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
86
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
87 class FixRefs(C3Pass):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
88 def fixRefs(self, pkg):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
89 self.visitor.visit(pkg, self.findRefs)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
90
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
91 # Reference fixups:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
92 def resolveDesignator(self, d, scope):
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
93 assert isinstance(d, Designator), type(d)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
94 assert type(scope) is Scope
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
95 if scope.hasSymbol(d.tname):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
96 s = scope.getSymbol(d.tname)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
97 if isinstance(d, ImportDesignator):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
98 if s.innerScope.hasSymbol(d.vname):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
99 return s.innerScope.getSymbol(d.vname)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
100 else:
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
101 if hasattr(s, 'addRef'):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
102 # TODO: make this nicer
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
103 s.addRef(None)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
104 return s
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
105 else:
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
106 self.error('Cannot resolve name {0}'.format(d.tname), d.loc)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
107
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
108 def resolveImportDesignator(self, d, scope):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
109 assert isinstance(d, ImportDesignator), type(d)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
110 assert type(scope) is Scope
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
111 if scope.hasSymbol(d.tname):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
112 s = scope.getSymbol(d.tname)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
113 if hasattr(s, 'addRef'):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
114 # TODO: make this nicer
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
115 s.addRef(None)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
116 return s
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
117 else:
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
118 self.error('Cannot resolve name {0}'.format(d.tname), d.loc)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
119
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
120 def resolveType(self, t, scope):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
121 if type(t) is PointerType:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
122 t.ptype = self.resolveType(t.ptype, scope)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
123 return t
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
124 elif type(t) is StructureType:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
125 offset = 0
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
126 for mem in t.mems:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
127 mem.offset = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
128 mem.typ = self.resolveType(mem.typ, scope)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
129 offset += theType(mem.typ).bytesize
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
130 t.bytesize = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
131 return t
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
132 elif isinstance(t, Designator):
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
133 t = self.resolveDesignator(t, scope)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
134 if t:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
135 return self.resolveType(t, scope)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
136 elif isinstance(t, Type):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
137 # Already resolved??
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
138 return t
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
139 else:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
140 raise Exception('Error resolving type {} {}'.format(t, type(t)))
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
141
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
142 def findRefs(self, sym):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 255
diff changeset
143 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
144 sym.typ = self.resolveType(sym.typ, sym.scope)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
145 elif type(sym) is TypeCast:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
146 sym.to_type = self.resolveType(sym.to_type, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
147 elif type(sym) is VariableUse:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
148 sym.target = self.resolveDesignator(sym.target, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
149 elif type(sym) is FunctionCall:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
150 varuse = sym.proc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
151 sym.proc = self.resolveDesignator(varuse.target, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
152 elif type(sym) is Function:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
153 # Checkup function type:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
154 ft = sym.typ
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
155 ft.returntype = self.resolveType(ft.returntype, sym.scope)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
156 ft.parametertypes = [self.resolveType(pt, sym.scope) for pt in
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
157 ft.parametertypes]
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
158 # Mark local variables:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
159 for d in sym.declarations:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
160 if isinstance(d, Variable):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
161 d.isLocal = True
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
162 elif type(sym) is DefinedType:
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
163 sym.typ = self.resolveType(sym.typ, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
164
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
165
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
166 # Type checking:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
167
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
168 def theType(t):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
169 """ Recurse until a 'real' type is found """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
170 if type(t) is DefinedType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
171 return theType(t.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
172 return t
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
173
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
174
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
175 def equalTypes(a, b):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
176 """ Compare types a and b for structural equavalence. """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
177 # Recurse into named types:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
178 a, b = theType(a), theType(b)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
179
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
180 if type(a) is type(b):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
181 if type(a) is BaseType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
182 return a.name == b.name
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
183 elif type(a) is PointerType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
184 return equalTypes(a.ptype, b.ptype)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
185 elif type(a) is StructureType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
186 if len(a.mems) != len(b.mems):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
187 return False
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
188 return all(equalTypes(am.typ, bm.typ) for am, bm in
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
189 zip(a.mems, b.mems))
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
190 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
191 raise NotImplementedError(
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
192 'Type compare for {} not implemented'.format(type(a)))
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
193 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
194
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
195
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
196 def canCast(fromT, toT):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
197 fromT = theType(fromT)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
198 toT = theType(toT)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
199 if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
200 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
201 elif fromT is intType and isinstance(toT, PointerType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
202 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
203 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
204
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
205
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
206 def expectRval(s):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
207 # TODO: solve this better
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
208 s.expect_rvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
209
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
210
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
211 class TypeChecker(C3Pass):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
212 def checkPackage(self, pkg):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
213 self.ok = True
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
214 self.visit(pkg, None, self.check2)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
215 return self.ok
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
216
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
217 def check2(self, sym):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
218 if type(sym) in [IfStatement, WhileStatement]:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
219 if not equalTypes(sym.condition.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
220 msg = 'Condition must be of type {}'.format(boolType)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
221 self.error(msg, sym.condition.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
222 elif type(sym) is Assignment:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
223 l, r = sym.lval, sym.rval
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
224 if not equalTypes(l.typ, r.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
225 msg = 'Cannot assign {} to {}'.format(r.typ, l.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
226 self.error(msg, sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
227 if not l.lvalue:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
228 self.error('No valid lvalue {}'.format(l), l.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
229 #if sym.rval.lvalue:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
230 # self.error('Right hand side must be an rvalue', sym.rval.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
231 expectRval(sym.rval)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
232 elif type(sym) is ReturnStatement:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
233 pass
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
234 elif type(sym) is FunctionCall:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
235 # Check arguments:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
236 ngiv = len(sym.args)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
237 ptypes = sym.proc.typ.parametertypes
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
238 nreq = len(ptypes)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
239 if ngiv != nreq:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
240 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
241 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
242 for a, at in zip(sym.args, ptypes):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
243 expectRval(a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
244 if not equalTypes(a.typ, at):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
245 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
246 # determine return type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
247 sym.typ = sym.proc.typ.returntype
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
248 elif type(sym) is VariableUse:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
249 sym.lvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
250 if isinstance(sym.target, Variable):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
251 sym.typ = sym.target.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
252 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
253 print('warning {} has no target, defaulting to int'.format(sym))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
254 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
255 elif type(sym) is Literal:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
256 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
257 if type(sym.val) is int:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
258 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
259 elif type(sym.val) is float:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
260 sym.typ = doubleType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
261 elif type(sym.val) is bool:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
262 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
263 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
264 raise Exception('Unknown literal type'.format(sym.val))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
265 elif type(sym) is Unop:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
266 if sym.op == '&':
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
267 sym.typ = PointerType(sym.a.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
268 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
269 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
270 raise Exception('Unknown unop {0}'.format(sym.op))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
271 elif type(sym) is Deref:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
272 # pointer deref
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
273 sym.lvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
274 # check if the to be dereferenced variable is a pointer type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
275 ptype = theType(sym.ptr.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
276 if type(ptype) is PointerType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
277 sym.typ = ptype.ptype
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
278 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
279 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
280 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
281 elif type(sym) is FieldRef:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
282 basetype = sym.base.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
283 sym.lvalue = sym.base.lvalue
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
284 basetype = theType(basetype)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
285 if type(basetype) is StructureType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
286 if basetype.hasField(sym.field):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
287 sym.typ = basetype.fieldType(sym.field)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
288 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
289 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
290 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
291 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
292 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
293 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
294 elif type(sym) is Binop:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
295 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
296 if sym.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
297 expectRval(sym.a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
298 expectRval(sym.b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
299 if equalTypes(sym.a.typ, sym.b.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
300 if equalTypes(sym.a.typ, intType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
301 sym.typ = sym.a.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
302 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
303 self.error('Can only add integers', sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
304 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
305 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
306 # assume void here? TODO: throw exception!
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
307 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
308 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
309 elif sym.op in ['>', '<', '==', '<=', '>=']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
310 expectRval(sym.a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
311 expectRval(sym.b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
312 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
313 if not equalTypes(sym.a.typ, sym.b.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
314 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
315 elif sym.op in ['or', 'and']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
316 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
317 if not equalTypes(sym.a.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
318 self.error('Must be {0}'.format(boolType), sym.a.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
319 if not equalTypes(sym.b.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
320 self.error('Must be {0}'.format(boolType), sym.b.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
321 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
322 raise Exception('Unknown binop {0}'.format(sym.op))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
323 elif isinstance(sym, Variable):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
324 # check initial value type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
325 # TODO
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
326 pass
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
327 elif type(sym) is TypeCast:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
328 if canCast(sym.a.typ, sym.to_type):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
329 sym.typ = sym.to_type
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
330 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
331 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
332 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
333 elif type(sym) is Constant:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
334 if not equalTypes(sym.typ, sym.value.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
335 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
336 elif type(sym) in [CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
337 pass
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
338 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
339 raise NotImplementedError('Unknown type check {0}'.format(sym))