annotate python/ppci/c3/analyse.py @ 301:6753763d3bec

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