forked from BachiLi/loma_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.py
186 lines (145 loc) · 5.97 KB
/
error.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import ast
import attrs
import ir
ir.generate_asdl_file()
import _asdl.loma as loma_ir
import pretty_print
class CompileError(Exception):
pass
class UserError(CompileError):
pass
@attrs.define(frozen=True)
class FuncArgNotAnnotated(UserError):
node : ast.AST # Python AST Node
def to_string(self):
return (f'Function argument not annotated as In/Out.\n'
f'Line {self.node.lineno}: {ast.unparse(self.node)}')
@attrs.define(frozen=True)
class DuplicateVariable(UserError):
# the name of the variable that is duplicated
var : str
# the first declare statement (or arg)
first_declare_stmt : loma_ir.stmt | loma_ir.arg
# the duplicate statement (or arg)
duplicate_declare_stmt : loma_ir.stmt | loma_ir.arg
def to_string(self):
return (f'Duplicated variable declaration detected.\n'
f'Variable name: {self.var}.\n'
f'First declared (line {self.first_declare_stmt.lineno}): {pretty_print.loma_to_str(self.first_declare_stmt)}'
f'Duplicated declared (line {self.duplicate_declare_stmt.lineno}): {pretty_print.loma_to_str(self.duplicate_declare_stmt)}')
@attrs.define(frozen=True)
class UndeclaredVariable(UserError):
# the name of the variable that is undeclared
var : str
# the statement or expr which this occurs
stmt : loma_ir.stmt | loma_ir.expr
def to_string(self):
return (f'Undeclared variable use detected.\n'
f'Variable name: {self.var}.\n'
f'Statement/Expr (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class ReturnNotLastStmt(UserError):
# the return statement
stmt : loma_ir.stmt
def to_string(self):
return (f'Return is not the last statement, or is inside an if/while statement.\n'
f'Statement (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class DeclareUnboundedArray(UserError):
# the declare statement
stmt : loma_ir.stmt
def to_string(self):
return (f'Variable declaration with unbounded size detected.\n'
f'Statement (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class DeclarationNotOutmostLevel(UserError):
# the declare statement
stmt : loma_ir.stmt
def to_string(self):
return (f'Variable declarations must be at outmost level of a function.\n'
f'Statement (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class CallWithOutArgNotInCallStmt(UserError):
# the call expr
expr : loma_ir.expr
def to_string(self):
return (f'Function calls with output arguments must be inside CallStmt.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class ArrayAccessTypeMismatch(UserError):
# the access expr
expr : loma_ir.expr
def to_string(self):
return (f'Detected an expression that index from a variable that is not an array.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class StructAccessTypeMismatch(UserError):
# the access expr
expr : loma_ir.expr
def to_string(self):
return (f'Detected an expression that access members from a variable that is not a struct.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class StructMemberNotFound(UserError):
# the access expr
expr : loma_ir.expr
def to_string(self):
return (f'The struct member does not exist in the struct access expression.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class BinaryOpTypeMismatch(UserError):
# the access expr
expr : loma_ir.expr
def to_string(self):
return (f'Invalid BinaryOp expression.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class CallTypeMismatch(UserError):
# the call expr
expr : loma_ir.expr
def to_string(self):
return (f'Invalid Call arguments.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class ReturnTypeMismatch(UserError):
# the return statement
stmt : loma_ir.stmt
def to_string(self):
return (f'Invalid Return type.\n'
f'Stmt (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class AssignTypeMismatch(UserError):
# the assign statement
stmt : loma_ir.stmt
def to_string(self):
return (f'Invalid Assign type.\n'
f'Stmt (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class DeclareTypeMismatch(UserError):
# the assign statement
stmt : loma_ir.stmt
def to_string(self):
return (f'Invalid Declare type.\n'
f'Stmt (line {self.stmt.lineno}): {pretty_print.loma_to_str(self.stmt)}')
@attrs.define(frozen=True)
class IfElseCondTypeMismatch(UserError):
# the ifelse cond expr
expr : loma_ir.expr
def to_string(self):
return (f'Invalid IfElse condition.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
@attrs.define(frozen=True)
class CallIDNotFound(UserError):
# the call expr
expr : loma_ir.expr
def to_string(self):
return (f'Call ID not found.\n'
f'Expr (line {self.expr.lineno}): {pretty_print.loma_to_str(self.expr)}')
class InternalError(CompileError):
pass
@attrs.define(frozen=True)
class UnhandledDifferentiation(InternalError):
func : loma_ir.func
def to_string(self):
return (f'Unhandled Differentiation.\n'
f'Func (line {self.func.lineno}): {pretty_print.loma_to_str(self.func)}')