forked from pclewis/lslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_walk.cc
311 lines (274 loc) · 11.1 KB
/
final_walk.cc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "lslmini.hh"
void LLASTNode::final_walk() {
LLASTNode *node;
final_pre_checks();
for ( node = get_children(); node; node = node->get_next() )
node->final_walk();
final_post_checks();
}
bool allret(LLASTNode *p) {
bool ret = false;
if (p->get_node_type() == NODE_STATEMENT && p->get_node_sub_type() == NODE_RETURN_STATEMENT) {
// TODO check next value here for unreachable code
return true;
}
else if (p->get_node_type() == NODE_STATEMENT && p->get_node_sub_type() == NODE_IF_STATEMENT) {
bool true_branch = p->get_child(1) && allret(p->get_child(1));
bool false_branch = p->get_child(2) && allret(p->get_child(2));
return (true_branch && false_branch);
}
else if (p->get_node_type() == NODE_STATEMENT && p->get_node_sub_type() == NODE_COMPOUND_STATEMENT) {
LLASTNode *q;
for (q = p->get_children(); q; q = q->get_next()) {
ret |= allret(q);
}
}
else {
#if 0
if (p->get_next()) {
ret |= allret(p->get_next());
}
if (p->get_children()) {
ret |= allret(p->get_children());
}
#endif
}
return ret;
}
void LLScriptGlobalFunction::final_pre_checks() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
//LLScriptFunctionDec *decl = (LLScriptFunctionDec *) get_child(1);
LLScriptStatement *statement = (LLScriptStatement *) get_child(2);
if (id->get_symbol() == NULL) {
id->resolve_symbol(SYM_FUNCTION);
}
if (id->get_symbol() != NULL) {
LLScriptType *type = id->get_symbol()->get_type();
if (type->get_itype() != LST_NULL && !allret(statement)) {
ERROR(IN(get_child(0)), E_NOT_ALL_PATHS_RETURN);
}
}
}
static bool isKey(const char *value) {
if (strlen(value) != 36 || value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-')
return false;
char buf[37];
strcpy(buf, value);
// change dashes to valid letters, and check the whole string
buf[8] = 'A'; buf[13] = 'A'; buf[18] = 'A'; buf[23] = 'A';
return strspn(buf, "0123456789ABCDEFabcdef") == 36;
}
void check_cond(LLScriptExpression *expr, bool warn_if_true) {
// see if expression is constant
if ( expr->get_constant_value() != NULL ) {
int truth = 2; // 2 denotes that it hasn't been handled
LLNodeSubType type = expr->get_constant_value()->get_node_sub_type();
if ( type == NODE_INTEGER_CONSTANT ) {
truth = ((LLScriptIntegerConstant*)expr->get_constant_value())->get_value() != 0;
} else if ( type == NODE_FLOAT_CONSTANT ) {
truth = ((LLScriptFloatConstant*)expr->get_constant_value())->get_value() != 0.f;
} else if ( type == NODE_STRING_CONSTANT ) {
truth = ((LLScriptStringConstant*)expr->get_constant_value())->get_value()[0] != 0;
} else if ( type == NODE_KEY_CONSTANT) {
const char *value = ((LLScriptKeyConstant*)expr->get_constant_value())->get_value();
truth = value && isKey(value) && strcmp(value, "00000000-0000-0000-0000-000000000000");
} else if ( type == NODE_VECTOR_CONSTANT ) {
LLVector *value = ((LLScriptVectorConstant*)expr->get_constant_value())->get_value();
truth = value->x != 0.f || value->y != 0.f || value->z != 0.f;
} else if ( type == NODE_QUATERNION_CONSTANT ) {
LLQuaternion *value = ((LLScriptQuaternionConstant*)expr->get_constant_value())->get_value();
truth = value->x != 0.f || value->y != 0.f || value->z != 0.f || value->s != 1.0f;
} else if ( type == NODE_LIST_CONSTANT ) {
int length = ((LLScriptListConstant*)expr->get_constant_value())->get_length();
truth = length > (mono_mode ? 0 : 1);
} else {
// you can't handle the truth
}
if (truth != 2) {
// valid
if (truth) {
if (warn_if_true) {
ERROR( IN(expr), W_CONDITION_ALWAYS_TRUE );
}
} else {
ERROR( IN(expr), W_CONDITION_ALWAYS_FALSE );
}
}
}
// see if expression is an assignment
if ( expr->get_operation() == '=' ) {
ERROR( IN(expr), W_ASSIGNMENT_IN_COMPARISON );
}
}
void LLScriptListExpression::final_pre_checks() {
LLASTNode *elem;
for ( elem = get_children(); elem; elem = elem->get_next() )
if ( elem->get_type()->get_itype() == LST_LIST )
ERROR( IN(elem), E_LIST_IN_LIST );
}
void LLScriptListConstant::final_pre_checks() {
LLASTNode *elem;
for ( elem = get_children(); elem; elem = elem->get_next() ) {
if ( elem->get_type()->get_itype() == LST_LIST ) {
if (elem->get_node_type() == NODE_SIMPLE_ASSIGNABLE) {
ERROR( IN(elem->get_child(0)), E_LIST_IN_LIST );
} else {
ERROR( IN(elem), E_LIST_IN_LIST );
}
}
}
}
void LLScriptTypecastExpression::final_pre_checks() {
if (!mono_mode && get_type()->get_itype() == LST_LIST) {
// Check if it only has a variable of type string or key
LLASTNode *child = get_children();
if (child && !child->get_next() && child->get_node_type() == NODE_EXPRESSION && child->get_node_sub_type() == NODE_LVALUE_EXPRESSION) {
// We're on the right track. Go one level deeper.
child = child->get_children();
if (child && !child->get_next() && child->get_node_type() == NODE_IDENTIFIER) {
LST_TYPE type = child->get_type()->get_itype();
// A-ha!
if (type == LST_STRING) {
ERROR(HERE, W_KEY_OR_STR_TO_LIST, "string", "key", "string");
} else if (type == LST_KEY) {
ERROR(HERE, W_KEY_OR_STR_TO_LIST, "key", "string", "key");
}
}
}
}
}
void LLScriptIfStatement::final_pre_checks() {
check_cond((LLScriptExpression*)get_child(0), true);
}
void LLScriptWhileStatement::final_pre_checks() {
check_cond((LLScriptExpression*)get_child(0), false);
}
void LLScriptDoStatement::final_pre_checks() {
check_cond((LLScriptExpression*)get_child(1), false);
}
void LLScriptForStatement::final_pre_checks() {
check_cond((LLScriptExpression*)get_child(1), false);
}
void LLScriptEventHandler::final_pre_checks() {
LLASTNode *node = NULL;
bool is_last = true;
int found = 0;
LLScriptIdentifier *id = (LLScriptIdentifier *)get_child(0);
// check for duplicates
for (node = get_parent()->get_children(); node; node = node->get_next()) {
if ( node->get_node_type() != NODE_EVENT_HANDLER )
continue;
LLScriptIdentifier *other_id = (LLScriptIdentifier *)node->get_child(0);
if (!strcmp(id->get_name(), other_id->get_name())) {
found++;
is_last = (node == this);
}
}
if (found > 1 && is_last) {
ERROR( HERE, E_MULTIPLE_EVENT_HANDLERS, id->get_name() );
}
// check parameters
if (id->get_symbol() == NULL) {
id->resolve_symbol(SYM_EVENT);
}
if (id->get_symbol() != NULL) {
// check argument types
LLScriptFunctionDec *function_decl;
LLScriptIdentifier *declared_param_id;
LLScriptIdentifier *passed_param_id;
int param_num = 1;
function_decl = id->get_symbol()->get_function_decl();
declared_param_id = (LLScriptIdentifier*) function_decl->get_children();
passed_param_id = (LLScriptIdentifier*) get_child(1)->get_children();
while ( declared_param_id != NULL && passed_param_id != NULL ) {
if ( !passed_param_id->get_type()->can_coerce(
declared_param_id->get_type()) ) {
ERROR( IN(passed_param_id), E_ARGUMENT_WRONG_TYPE_EVENT,
passed_param_id->get_type()->get_node_name(),
param_num,
id->get_name(),
declared_param_id->get_type()->get_node_name(),
declared_param_id->get_name()
);
return;
}
passed_param_id = (LLScriptIdentifier*) passed_param_id->get_next();
declared_param_id = (LLScriptIdentifier*) declared_param_id->get_next();
++param_num;
}
if ( passed_param_id != NULL ) {
// printf("too many, extra is %s\n", passed_param_id->get_name());
ERROR( IN(passed_param_id), E_TOO_MANY_ARGUMENTS_EVENT, id->get_name() );
} else if ( declared_param_id != NULL ) {
// printf("too few, extra is %s\n", declared_param_id->get_name());
ERROR( HERE, E_TOO_FEW_ARGUMENTS_EVENT, id->get_name() );
}
}
else {
ERROR( HERE, E_INVALID_EVENT, id->get_name());
}
}
void LLScriptSwitchStatement::final_pre_checks() {
int num_defaults = 0;
LLASTNode *node = get_children(), *node2, *second_default = NULL;
// first child is switch expression
if (node->get_constant_value() != NULL) {
// rather than warning on each case, warn once at the top
ERROR( IN(node), W_CONSTANT_SWITCH );
}
LLScriptType *switch_type = node->get_type();
if (switch_type->get_itype() == LST_LIST) {
// This error may sound a bit awkward to the user, but since == is
// used internally by the switch converter, it should be OK.
ERROR( IN(node), W_LIST_COMPARE );
}
// subsequent children are case or default blocks
for (node = node->get_next() ; node; node = node->get_next() ) {
// node is a case block or a default block (default blocks have NULL
// as the first child)
if (node->get_child(0)->get_node_type() != NODE_NULL) {
LLScriptConstant *const1 = node->get_child(0)->get_constant_value();
if (const1) {
// check type compatibility
if (switch_type->get_result_type(EQ, const1->get_type())) {
// compare every case block with every other case block
// (skipping default blocks)
for ( node2 = node->get_next(); node2; node2 = node2->get_next() ) {
if (node2->get_child(0)) {
LLScriptConstant *const2 = node2->get_child(0)->get_constant_value();
if (const2) {
LLScriptIntegerConstant *comparison = (LLScriptIntegerConstant *)const1->operation(EQ, const2, const1->get_lloc());
if (comparison) {
if (comparison->get_value()) {
ERROR( IN(const2), W_DUPLICATE_CASE );
} // else it will err later, as type equality is transitive
delete comparison;
}
}
}
}
} else {
ERROR( IN(const1), E_INCOMPATIBLE_CASE_TYPE );
}
}
} else {
if (++num_defaults == 2) {
second_default = node;
}
}
}
if (! num_defaults) {
ERROR( HERE, W_SWITCH_NO_DEFAULT );
} else if (num_defaults > 1) {
ERROR( IN(second_default), E_SWITCH_MULTIPLE_DEFAULTS );
}
script->inc_switchlevel();
}
void LLScriptSwitchStatement::final_post_checks() {
script->dec_switchlevel();
}
void LLScriptBreakStatement::final_pre_checks() {
if (!script->get_switchlevel()) {
ERROR( HERE, E_BREAK_WITHOUT_SWITCH );
}
}