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