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