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