annotate python/c3/analyse.py @ 284:05184b95fa16

Moved tests to seperate folder
author Windel Bouwman
date Fri, 15 Nov 2013 13:43:22 +0100
parents 6f2423df0675
children 1c7c1e619be8
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 *
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
4 from .scope import Scope, topScope
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
5 from .typecheck import theType
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
6
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
7 class Analyzer:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
8 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
9 Context handling is done here.
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
10 Scope is attached to the correct modules.
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
11 This class checks names and references
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
12 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
13 def __init__(self, diag):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
14 self.diag = diag
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
15 self.logger = logging.getLogger('c3')
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
16
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
17 def analyzePackage(self, pkg, packageProvider):
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
18 self.logger.info('Checking package {}'.format(pkg.name))
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
19 self.ok = True
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
20 visitor = Visitor()
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
21 # Prepare top level scope:
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
36 visitor.visit(pkg, self.findRefs)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
37 visitor.visit(pkg, self.sanity)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
38 return self.ok
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
39
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
40 def error(self, msg, loc=None):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
41 self.ok = False
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
42 self.diag.error(msg, loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
43
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
44 @property
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
45 def CurrentScope(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
46 return self.scopeStack[-1]
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
47
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
48 # Scope creation:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
49 def addSymbol(self, sym):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
50 if self.CurrentScope.hasSymbol(sym.name):
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
51 self.error('Redefinition of {0}'.format(sym.name), sym.loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
52 else:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
53 self.CurrentScope.addSymbol(sym)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
54
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
55 def enterScope(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
56 # Distribute the scope:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
57 sym.scope = self.CurrentScope
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
58
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
59 # Add symbols to current scope:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
60 if isinstance(sym, Symbol) or isinstance(sym, DefinedType):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
61 self.addSymbol(sym)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
62
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
63 # Create subscope:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
64 if type(sym) in [Package, Function]:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
65 newScope = Scope(self.CurrentScope)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
66 self.scopeStack.append(newScope)
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
67 sym.innerScope = self.CurrentScope
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
68
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
69 def quitScope(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
70 # Pop out of scope:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
71 if type(sym) in [Package, Function]:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
72 self.scopeStack.pop(-1)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
73
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
74 # Reference fixups:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
75 def resolveDesignator(self, d, scope):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
76 assert type(d) is Designator, type(d)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
77 assert type(scope) is Scope
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
78 if scope.hasSymbol(d.tname):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
79 s = scope.getSymbol(d.tname)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
80 if hasattr(s, 'addRef'):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
81 # TODO: make this nicer
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
82 s.addRef(None)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
83 return s
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
84 else:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
85 self.ok = False
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
86 msg = 'Cannot resolve name {0}'.format(d.tname)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
87 self.diag.error(msg, d.loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
88
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
89 def resolveType(self, t, scope):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
90 # TODO: what about structs?
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
91 if type(t) is PointerType:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
92 t.ptype = self.resolveType(t.ptype, scope)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
93 return t
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
94 elif type(t) is StructureType:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
95 offset = 0
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
96 for mem in t.mems:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
97 mem.offset = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
98 mem.typ = self.resolveType(mem.typ, scope)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
99 offset += theType(mem.typ).bytesize
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
100 t.bytesize = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
101 return t
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
102 elif type(t) is Designator:
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
103 t = self.resolveDesignator(t, scope)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
104 if t:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
105 return self.resolveType(t, scope)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
106 elif isinstance(t, Type):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
107 # Already resolved??
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
108 return t
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
109 else:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
110 raise Exception('Error resolving type {} {}'.format(t, type(t)))
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
111
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
112 def findRefs(self, sym):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 255
diff changeset
113 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
114 sym.typ = self.resolveType(sym.typ, sym.scope)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
115 elif type(sym) is TypeCast:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
116 sym.to_type = self.resolveType(sym.to_type, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
117 elif type(sym) is VariableUse:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
118 sym.target = self.resolveDesignator(sym.target, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
119 elif type(sym) is FunctionCall:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
120 varuse = sym.proc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
121 sym.proc = self.resolveDesignator(varuse.target, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
122 elif type(sym) is Function:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
123 # Checkup function type:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
124 ft = sym.typ
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
125 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
126 ft.parametertypes = [self.resolveType(pt, sym.scope) for pt in ft.parametertypes]
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
127 # Mark local variables:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
128 for d in sym.declarations:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
129 if isinstance(d, Variable):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
130 d.isLocal = True
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
131 elif type(sym) is DefinedType:
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
132 sym.typ = self.resolveType(sym.typ, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
133
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
134 def sanity(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
135 if type(sym) is FunctionType:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
136 pass
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
137 elif type(sym) is Function:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
138 pass
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
139