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
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
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.
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
11 This class checks names and references.
215
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()
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
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
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)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
37 return self.ok
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
38
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
39 def error(self, msg, loc=None):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
40 self.ok = False
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
41 self.diag.error(msg, loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
47 # Scope creation:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
50 self.error('Redefinition of {0}'.format(sym.name), sym.loc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
51 else:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
52 self.CurrentScope.addSymbol(sym)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
53
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
54 def enterScope(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
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 # 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
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
60 self.addSymbol(sym)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
61
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
62 # Create subscope:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
67
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
68 def quitScope(self, sym):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
69 # Pop out of scope:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
72
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
73 # Reference fixups:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
76 assert type(scope) is Scope
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
77 if scope.hasSymbol(d.tname):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
78 s = scope.getSymbol(d.tname)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
79 if hasattr(s, 'addRef'):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
80 # TODO: make this nicer
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
81 s.addRef(None)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
82 return s
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
83 else:
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
84 self.error('Cannot resolve name {0}'.format(d.tname), d.loc)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
90 elif type(t) is StructureType:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
91 offset = 0
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
92 for mem in t.mems:
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
93 mem.offset = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
94 mem.typ = self.resolveType(mem.typ, scope)
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
95 offset += theType(mem.typ).bytesize
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
96 t.bytesize = offset
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
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
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
99 t = self.resolveDesignator(t, scope)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
100 if t:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 231
diff changeset
101 return self.resolveType(t, scope)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
102 elif isinstance(t, Type):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
103 # Already resolved??
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
104 return t
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
105 else:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
106 raise Exception('Error resolving type {} {}'.format(t, type(t)))
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 227
diff changeset
107
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
108 def findRefs(self, sym):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 255
diff changeset
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
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
111 elif type(sym) is TypeCast:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
112 sym.to_type = self.resolveType(sym.to_type, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
118 elif type(sym) is Function:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
119 # Checkup function type:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
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
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
123 # Mark local variables:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
124 for d in sym.declarations:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
125 if isinstance(d, Variable):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 272
diff changeset
126 d.isLocal = True
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
127 elif type(sym) is DefinedType:
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
128 sym.typ = self.resolveType(sym.typ, sym.scope)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
129
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
130 # Type checking:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
131
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
132 def theType(t):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
133 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
134 Recurse until a 'real' type is found
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
135 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
136 if type(t) is DefinedType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
137 return theType(t.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
138 return t
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
139
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
140 def equalTypes(a, b):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
141 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
142 Compare types a and b for equality.
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
143 Not equal until proven otherwise.
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
144 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
145 # Recurse into named types:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
146 a = theType(a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
147 b = theType(b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
148
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
149 # Compare for structural equivalence:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
150 if type(a) is type(b):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
151 if type(a) is BaseType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
152 return a.name == b.name
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
153 elif type(a) is PointerType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
154 return equalTypes(a.ptype, b.ptype)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
155 elif type(a) is StructureType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
156 if len(a.mems) != len(b.mems):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
157 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
158 for amem, bmem in zip(a.mems, b.mems):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
159 if not equalTypes(amem.typ, bmem.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
160 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
161 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
162 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
163 raise Exception('Type compare for {} not implemented'.format(type(a)))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
164 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
165
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
166 def canCast(fromT, toT):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
167 fromT = theType(fromT)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
168 toT = theType(toT)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
169 if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
170 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
171 elif fromT is intType and isinstance(toT, PointerType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
172 return True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
173 return False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
174
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
175 def expectRval(s):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
176 # TODO: solve this better
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
177 s.expect_rvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
178
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
179 class TypeChecker:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
180 def __init__(self, diag):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
181 self.diag = diag
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
182
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
183 def error(self, msg, loc):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
184 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
185 Wrapper that registers the message and marks the result invalid
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
186 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
187 self.diag.error(msg, loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
188 self.ok = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
189
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
190 def checkPackage(self, pkg):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
191 self.ok = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
192 visitor = Visitor()
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
193 visitor.visit(pkg, f_post=self.check2)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
194 return self.ok
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
195
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
196 def check2(self, sym):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
197 if type(sym) in [IfStatement, WhileStatement]:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
198 if not equalTypes(sym.condition.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
199 msg = 'Condition must be of type {}'.format(boolType)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
200 self.error(msg, sym.condition.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
201 elif type(sym) is Assignment:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
202 l, r = sym.lval, sym.rval
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
203 if not equalTypes(l.typ, r.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
204 msg = 'Cannot assign {} to {}'.format(r.typ, l.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
205 self.error(msg, sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
206 if not l.lvalue:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
207 self.error('No valid lvalue {}'.format(l), l.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
208 #if sym.rval.lvalue:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
209 # self.error('Right hand side must be an rvalue', sym.rval.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
210 expectRval(sym.rval)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
211 elif type(sym) is ReturnStatement:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
212 pass
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
213 elif type(sym) is FunctionCall:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
214 # Check arguments:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
215 ngiv = len(sym.args)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
216 ptypes = sym.proc.typ.parametertypes
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
217 nreq = len(ptypes)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
218 if ngiv != nreq:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
219 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
220 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
221 for a, at in zip(sym.args, ptypes):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
222 expectRval(a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
223 if not equalTypes(a.typ, at):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
224 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
225 # determine return type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
226 sym.typ = sym.proc.typ.returntype
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
227 elif type(sym) is VariableUse:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
228 sym.lvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
229 if isinstance(sym.target, Variable):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
230 sym.typ = sym.target.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
231 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
232 print('warning {} has no target, defaulting to int'.format(sym))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
233 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
234 elif type(sym) is Literal:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
235 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
236 if type(sym.val) is int:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
237 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
238 elif type(sym.val) is float:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
239 sym.typ = doubleType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
240 elif type(sym.val) is bool:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
241 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
242 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
243 raise Exception('Unknown literal type'.format(sym.val))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
244 elif type(sym) is Unop:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
245 if sym.op == '&':
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
246 sym.typ = PointerType(sym.a.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
247 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
248 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
249 raise Exception('Unknown unop {0}'.format(sym.op))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
250 elif type(sym) is Deref:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
251 # pointer deref
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
252 sym.lvalue = True
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
253 # check if the to be dereferenced variable is a pointer type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
254 ptype = theType(sym.ptr.typ)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
255 if type(ptype) is PointerType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
256 sym.typ = ptype.ptype
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
257 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
258 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
259 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
260 elif type(sym) is FieldRef:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
261 basetype = sym.base.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
262 sym.lvalue = sym.base.lvalue
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
263 basetype = theType(basetype)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
264 if type(basetype) is StructureType:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
265 if basetype.hasField(sym.field):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
266 sym.typ = basetype.fieldType(sym.field)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
267 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
268 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
269 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
270 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
271 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
272 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
273 elif type(sym) is Binop:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
274 sym.lvalue = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
275 if sym.op in ['+', '-', '*', '/', '<<', '>>', '|', '&']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
276 expectRval(sym.a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
277 expectRval(sym.b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
278 if equalTypes(sym.a.typ, sym.b.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
279 if equalTypes(sym.a.typ, intType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
280 sym.typ = sym.a.typ
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
281 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
282 self.error('Can only add integers', sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
283 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
284 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
285 # assume void here? TODO: throw exception!
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
286 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
287 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
288 elif sym.op in ['>', '<', '==', '<=', '>=']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
289 expectRval(sym.a)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
290 expectRval(sym.b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
291 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
292 if not equalTypes(sym.a.typ, sym.b.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
293 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
294 elif sym.op in ['or', 'and']:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
295 sym.typ = boolType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
296 if not equalTypes(sym.a.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
297 self.error('Must be {0}'.format(boolType), sym.a.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
298 if not equalTypes(sym.b.typ, boolType):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
299 self.error('Must be {0}'.format(boolType), sym.b.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
300 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
301 raise Exception('Unknown binop {0}'.format(sym.op))
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
302 elif isinstance(sym, Variable):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
303 # check initial value type:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
304 # TODO
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
305 pass
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
306 elif type(sym) is TypeCast:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
307 if canCast(sym.a.typ, sym.to_type):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
308 sym.typ = sym.to_type
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
309 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
310 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
311 sym.typ = intType
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
312 elif type(sym) is Constant:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
313 if not equalTypes(sym.typ, sym.value.typ):
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
314 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
315 elif type(sym) in [CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
316 pass
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
317 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 275
diff changeset
318 raise NotImplementedError('Unknown type check {0}'.format(sym))