forked from pclewis/lslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lslmini.cc
1207 lines (1077 loc) · 43.3 KB
/
lslmini.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string>
#include "lslmini.hh"
#include "logger.hh"
// Workaround for makeless builds
#ifndef VERSION
#define VERSION "0.4.3-xh"
#endif
#ifndef BUILD_DATE
#include <time.h>
#endif
// For Wed 04/20/2016
// we had "04/2-/2-16"
// NOTE: I'm not too sure why we should do this. Free free to properly implement. - Xenhat
//#define BUILD_DATE " asdasdsadsa"
// Get current date/time, format is MM-DD-YYYY
std::string getBuildDate() {
// I hope this works for you guys
#ifdef BUILD_DATE
return static_cast<std::string>(BUILD_DATE);
#else
return "2017-01-25";
// This will do for now - Xenhat
/*
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%m/%d/%Y", &tstruct);
return buf;
*/
#endif
}
extern FILE *yyin;
extern const char *builtins_file;
//extern int yynerrs;
int yynwarns = 0; // not defined by flex but named for consistency
int yyparse(void*);
//int yyerror(const char *fmt, ... );
//int yywarning(const char *fmt, ...);
YYLTYPE LLASTNode::glloc = {0,0,0,0};
const char *DEPRECATED_FUNCTIONS[][2] = {
{"llSoundPreload", "llPreloadSound"},
{"llSound", "llPlaySound, llLoopSound, or llTriggerSound"},
{"llMakeExplosion", "llParticleSystem"},
{"llMakeFire", "llParticleSystem"},
{"llMakeFountain", "llParticleSystem"},
{"llMakeSmoke", "llParticleSystem"},
{"llRemoteLoadScript","llRemoteLoadScriptPin and llSetRemoteScriptAccessPin"},
{"llXorBase64Strings","llXorBase64"},
{"llXorBase64StringsCorrect", "llXorBase64"},
{"llRemoteDataSetRegion", "llOpenRemoteDataChannel"},
{"llSetPrimURL", "llSetPrimMediaParams"},
{"llRefreshPrimURL", "llSetPrimMediaParams"},
{"llTakeCamera", "llSetCameraParams"},
{"llReleaseCamera", "llClearCameraParams"},
{"llPointAt", NULL},
{"llStopPointAt", NULL},
{"llCloud", NULL},
{"llClearExperiencePermissions", NULL},
{"llCollisionSprite", NULL},
{"llGetExperienceList", NULL},
{NULL, NULL}
};
const char *GOD_MODE_FUNCTIONS[] = {
"llGodLikeRezObject",
"llSetInventoryPermMask",
"llSetObjectPermMask",
NULL
};
static int walklevel = 0;
bool mono_mode = true;
bool skip_preproc = false;
bool warn_unused_evparam = false;
bool switch_stmt = false;
bool lazy_lists = false;
bool override_fn = false;
bool god_mode = false;
void print_walk( char *str ) {
int i;
for ( i = 0; i < walklevel ; i++ )
printf(" ");
printf("%s\n", str);
}
void do_walk( LLASTNode *node ) {
walklevel++;
node->walk();
walklevel--;
}
void LLASTNode::add_children( int num, va_list ap ) {
LLASTNode *node;
for ( ; num--; ) {
node = va_arg(ap, LLASTNode*);
if ( node == NULL )
node = new LLASTNullNode();
push_child( node );
}
}
void LLASTNode::walk() {
LLASTNode *child = children;
char buf[256];
sprintf(buf, "%s [%s] (cv=%s) (%d,%d)", get_node_name(), type ? type->get_node_name() : NULL, constant_value ? constant_value->get_node_name() : NULL, lloc.first_line, lloc.first_column );
print_walk(buf);
//print_walk(get_node_name());
if ( child == NULL ) return;
if ( child->get_prev() != NULL ) {
printf("first child %s has non-null prev %s\n", child->get_node_name(), child->get_prev()->get_node_name() );
exit(1);
}
while ( child ) {
do_walk(child);
child = child->get_next();
}
}
// Lookup a symbol, propagating up the tree until it is found.
LLScriptSymbol *LLASTNode::lookup_symbol(const char *name, LLSymbolType type, bool is_case_sensitive) {
LLScriptSymbol *sym = NULL;
// If we have a symbol table of our own, look for it there
if ( symbol_table )
sym = symbol_table->lookup( name, type, is_case_sensitive );
// If we have no symbol table, or it wasn't in it, but we have a parent, ask them
if ( (sym == NULL || (type == SYM_VARIABLE && !sym->was_declared())) && parent )
sym = parent->lookup_symbol( name, type, is_case_sensitive );
return sym;
}
// Define a symbol, propagating up the tree to the nearest scope level.
void LLASTNode::define_symbol(LLScriptSymbol *symbol) {
// If we have a symbol table, define it there
if ( symbol_table ) {
LLScriptSymbol *shadow;
DEBUG( LOG_DEBUG_SPAM, NULL, "symbol definition caught in %s\n", get_node_name() );
// Check if already defined
shadow = symbol_table->lookup( symbol->get_name() );
// Override?
if ( shadow // warning: testing shadow first is important
&& (override_fn
|| (lazy_lists && !strcmp(shadow->get_name(), "lazy_list_set")))
&& shadow->get_symbol_type() == SYM_FUNCTION
&& shadow->get_sub_type() != SYM_BUILTIN ) {
script->get_symbol_table()->remove(shadow);
delete shadow;
shadow = NULL;
}
if ( shadow ) {
if (shadow->get_symbol_type() == SYM_EVENT) {
ERROR( IN(symbol), E_DUPLICATE_DECLARATION_BUILTIN, symbol->get_name(), "an event", " function" );
} else if (shadow->get_symbol_type() == SYM_FUNCTION && shadow->get_sub_type() == SYM_BUILTIN) {
ERROR( IN(symbol), E_DUPLICATE_DECLARATION_BUILTIN, symbol->get_name(), "a library function", " user function" );
} else {
ERROR( IN(symbol), E_DUPLICATE_DECLARATION, symbol->get_name(), shadow->get_lloc()->first_line, shadow->get_lloc()->first_column );
}
} else {
bool ok_to_define = true;
// Check for shadowed declarations
if ( parent ) {
shadow = parent->lookup_symbol(symbol->get_name(), SYM_ANY);
if ( shadow != NULL ) {
if (shadow->get_sub_type() == SYM_BUILTIN) {
// Events and constants are reserved; functions aren't.
if (shadow->get_symbol_type() == SYM_EVENT) {
ok_to_define = false;
ERROR( IN(symbol), E_WRONG_TYPE, symbol->get_name(),
LLScriptSymbol::get_type_name(symbol->get_symbol_type()),
"n", "event" );
} else if (shadow->get_symbol_type() == SYM_VARIABLE) {
ok_to_define = false;
ERROR( IN(symbol), E_WRONG_TYPE, symbol->get_name(),
LLScriptSymbol::get_type_name(symbol->get_symbol_type()),
"", "constant");
}
} else if (shadow->get_symbol_type() == symbol->get_symbol_type()) {
ERROR( IN(symbol), W_SHADOW_DECLARATION, symbol->get_name(), LINECOL(shadow->get_lloc()) );
}
}
}
if (ok_to_define) {
symbol_table->define(symbol);
}
}
// Otherwise, ask our parent to define it
} else if ( parent ) {
parent->define_symbol(symbol);
// .. but if we don't have a parent, we're in trouble.
} else {
throw "nowhere to define symbol!";
}
}
// Define any symbols we have, and ask our children to
void LLASTNode::collect_symbols() {
LLASTNode *child = children;
define_symbols();
while ( child ) {
child->collect_symbols();
child = child->get_next();
}
}
void LLASTNode::define_symbols() {
/* nothing */
}
void LLScriptDeclaration::define_symbols() {
LLNodeSubType type = get_parent()->get_node_sub_type();
if ( type == NODE_IF_STATEMENT || type == NODE_FOR_STATEMENT
|| type == NODE_DO_STATEMENT || type == NODE_WHILE_STATEMENT) {
ERROR( HERE, E_DECLARATION_NOT_ALLOWED );
// fall through to define the symbol anyway (issue #73)
}
LLScriptIdentifier *identifier = (LLScriptIdentifier *)get_children();
identifier->set_symbol( new LLScriptSymbol(identifier->get_name(), identifier->get_type(), SYM_VARIABLE, SYM_LOCAL, identifier->get_lloc()) );
define_symbol(identifier->get_symbol());
}
void LLScriptGlobalVariable::define_symbols() {
LLScriptIdentifier *identifier = (LLScriptIdentifier *)get_children();
identifier->set_symbol( new LLScriptSymbol(identifier->get_name(), identifier->get_type(), SYM_VARIABLE, SYM_GLOBAL, identifier->get_lloc()));
define_symbol(identifier->get_symbol());
identifier->get_symbol()->set_global();
// if it's initialized, set its constant value
if ( get_child(1)->get_node_type() == NODE_SIMPLE_ASSIGNABLE )
identifier->get_symbol()->set_constant_value( get_child(1)->get_child(0)->get_constant_value() );
}
void LLScriptState::define_symbols() {
LLASTNode *node = get_children();
LLScriptIdentifier *identifier;
if ( node->get_node_type() == NODE_NULL ) // null identifier = default state, nothing to define
return;
identifier = (LLScriptIdentifier *)node;
identifier->set_symbol( new LLScriptSymbol(identifier->get_name(), identifier->get_type(), SYM_STATE, SYM_GLOBAL, identifier->get_lloc()) );
define_symbol( identifier->get_symbol() );
}
void LLScriptGlobalFunction::define_symbols() {
LLScriptIdentifier *identifier = (LLScriptIdentifier *)get_child(0);
// define function in parent scope since we have our own
identifier->set_symbol(
new LLScriptSymbol( identifier->get_name(), identifier->get_type(), SYM_FUNCTION, SYM_GLOBAL, identifier->get_lloc(), (LLScriptFunctionDec*)get_child(1) )
);
get_parent()->define_symbol(identifier->get_symbol());
}
void LLScriptFunctionDec::define_symbols() {
LLScriptIdentifier *identifier;
LLASTNode *node = get_children();
while (node) {
identifier = (LLScriptIdentifier *)node;
identifier->set_symbol( new LLScriptSymbol( identifier->get_name(), identifier->get_type(), SYM_VARIABLE, SYM_FUNCTION_PARAMETER, node->get_lloc() ) );
define_symbol( identifier->get_symbol() );
identifier->get_symbol()->set_declared();
node = node->get_next();
}
}
void LLScriptEventDec::define_symbols() {
LLScriptIdentifier *identifier;
LLASTNode *node = get_children();
while (node) {
identifier = (LLScriptIdentifier *)node;
identifier->set_symbol( new LLScriptSymbol( identifier->get_name(), identifier->get_type(), SYM_VARIABLE, SYM_EVENT_PARAMETER, node->get_lloc() ) );
define_symbol( identifier->get_symbol() );
identifier->get_symbol()->set_declared();
node = node->get_next();
}
}
void LLScriptLabel::define_symbols() {
LLScriptIdentifier *identifier = (LLScriptIdentifier*)get_children();
identifier->set_symbol( new LLScriptSymbol(identifier->get_name(), identifier->get_type(), SYM_LABEL, SYM_LOCAL, identifier->get_lloc()) );
define_symbol( identifier->get_symbol() );
// Define it also in the function or event if it does not exist
LLASTNode *fn = get_parent();
while (fn && fn->get_node_type() != NODE_GLOBAL_FUNCTION && fn->get_node_type() != NODE_EVENT_HANDLER) {
fn = fn->get_parent();
}
if (!fn) {
printf("Label not inside a function or event - this should not happen!");
exit(1);
}
LabelMap *labels;
if (fn->get_node_type() == NODE_GLOBAL_FUNCTION)
labels = &((LLScriptGlobalFunction *)fn)->labels;
else
labels = &((LLScriptEventHandler *)fn)->labels;
std::string label = std::string(identifier->get_name());
if (labels->find(label) != labels->end()) {
if (mono_mode)
ERROR( IN(identifier), E_DUPLICATE_LABEL_MONO, identifier->get_name() );
else
ERROR( IN(identifier), W_DUPLICATE_LABEL_LSO, identifier->get_name() );
} else {
labels->insert(label);
}
}
// walk tree post-order and propagate types
void LLASTNode::propagate_types() {
LLASTNode *node = get_children();
while ( node ) {
node->propagate_types();
node = node->get_next();
}
determine_type();
}
void LLASTNode::determine_type() {
if ( type == NULL ) type = LLScriptType::get( LST_NULL );
}
static const char* operation_str(int operation) {
static char buf[16+1];
switch (operation) {
case EQ: return "==";
case NEQ: return "!=";
case POSTINC_OP:
case INC_OP: return "++";
case POSTDEC_OP:
case DEC_OP: return "--";
case BOOLEAN_AND: return "&&";
case BOOLEAN_OR: return "||";
case SHIFT_LEFT: return "<<";
case SHIFT_RIGHT: return ">>";
default:
if ( isprint(operation) ) {
buf[0] = operation;
buf[1] = 0;
} else {
sprintf(buf, "%d", operation);
}
return (const char *)buf;
}
}
void LLScriptExpression::determine_type() {
if ( operation == 0 ) type = get_child(0)->get_type();
else {
type = get_child(0)->get_type()->get_result_type( operation, get_child(1) ? get_child(1)->get_type() : NULL );
if ( type == NULL ) {
if (get_child(1)) {
// Binary operator
ERROR( HERE, E_INVALID_OPERATOR, get_child(0)->get_type()->get_node_name(), " ", operation_str(operation), " ", get_child(1)->get_type()->get_node_name() );
} else if (operation == POSTINC_OP || operation == POSTDEC_OP) {
// Unary postfix operator
ERROR( HERE, E_INVALID_OPERATOR, get_child(0)->get_type()->get_node_name(), " ", operation_str(operation), "", "" );
} else {
// Unary prefix operator
ERROR( HERE, E_INVALID_OPERATOR, operation_str(operation), " ", get_child(0)->get_type()->get_node_name(), "", "" );
}
// Assign a reasonable type based on operation
LST_TYPE t = operation == EQ ? LST_INTEGER :
operation == NEQ ? LST_INTEGER :
operation == '<' ? LST_INTEGER :
operation == '>' ? LST_INTEGER :
operation == GEQ ? LST_INTEGER :
operation == LEQ ? LST_INTEGER :
operation == '!' ? LST_INTEGER :
operation == '~' ? LST_INTEGER :
operation == '^' ? LST_INTEGER :
operation == '&' ? LST_INTEGER :
operation == '|' ? LST_INTEGER :
operation == SHIFT_LEFT ? LST_INTEGER :
operation == SHIFT_RIGHT ? LST_INTEGER :
operation == BOOLEAN_AND ? LST_INTEGER :
operation == BOOLEAN_OR ? LST_INTEGER :
LST_NULL;
type = t == LST_NULL ? get_child(0)->get_type() : new LLScriptType(t);
} else {
if ( operation == '=' || operation == INC_OP || operation == DEC_OP || operation == POSTINC_OP || operation == POSTDEC_OP ) {
// unused variable // LLASTNode *last_node = this;
// unused variable // LLASTNode *node = get_parent();
// add assignment
if ( get_child(0)->get_node_sub_type() == NODE_LVALUE_EXPRESSION && get_child(0)->get_child(0)->get_node_type() == NODE_IDENTIFIER ) {
LLScriptIdentifier *id = (LLScriptIdentifier*)get_child(0)->get_child(0);
if ( id->get_symbol() ) {
if (id->get_symbol()->get_sub_type() == SYM_BUILTIN) {
ERROR( HERE, E_WRONG_TYPE, id->get_symbol()->get_name(),
"variable", "", "constant");
}
id->get_symbol()->add_assignment();
}
}
}
}
}
}
static void find_suggestions(LLScriptIdentifier *id, char *buffer) {
// look for typos
// FIXME: this is mostly hacked together and unsafe (bp can overrun buffer, cur_sug can overrun suggestions)
// maybe a better way would be to go through all the symtabs looking for names within a certain "string distance"
char *bp;
const char *suggestions[16];
int cur_sug = 0;
int i;
const char *name = id->get_name();
LLScriptSymbol *symbol = id->get_symbol();
for ( i = 16; i--; )
suggestions[i] = NULL;
// try case insensitive
symbol = id->lookup_symbol( name, SYM_ANY, false );
if ( symbol != NULL )
suggestions[cur_sug++] = symbol->get_name();
if ( strstr(name, "To") ) {
// try replacing "To" with "2"
for (i = 0, bp = buffer; name[i]; i++) {
if ( (name[i] == 'T' || name[i] == 't') && (name[i+1] == 'O' || name[i+1] == 'o')) {
*bp++ = '2'; i++;
} else
*bp++ = name[i];
}
*bp = 0;
symbol = id->lookup_symbol( buffer, SYM_ANY, false );
if ( symbol != NULL )
suggestions[cur_sug++] = symbol->get_name();
}
// try replacing "2" with "To"
if ( strstr(name, "2") ) {
for (i = 0, bp = buffer; name[i]; i++) {
if ( name[i] == '2') {
*bp++ = 'T'; *bp++ = 'o';
} else
*bp++ = name[i];
}
*bp = 0;
symbol = id->lookup_symbol( buffer, SYM_ANY, false );
if ( symbol != NULL )
suggestions[cur_sug++] = symbol->get_name();
}
for (i = 0, buffer[0] = 0; suggestions[i] != NULL; i++ ) {
// add comma if not first
if ( i != 0 ) {
strncat(buffer, ", ", BUFFER_SIZE);
// add "or" if not last
if ( suggestions[i+1] == NULL )
strncat(buffer, "or ", BUFFER_SIZE);
}
strncat(buffer, "`", BUFFER_SIZE);
strncat(buffer, suggestions[i], BUFFER_SIZE);
strncat(buffer, "'", BUFFER_SIZE);
}
}
/// Identifiers should have their type/symbol set by their parent node, because they don't know what
/// kind of symbol they represent by themselves. For example, this should work:
// string test() { return "hi"; }
// string func() {
// integer test = 1;
// llOwnerSay(test());
// }
// But if "test" looked itself up, it would think it is an integer. Its parent function
// expression node can tell it what it needs to be before determining its own type.
void LLScriptIdentifier::resolve_symbol(LLSymbolType symbol_type, bool is_simple) {
// If we already have a symbol, we don't need to look it up.
/*
// Should never happen: no reason to ever have visited this node
// before during this tree walk.
if ( symbol != NULL ) {
type = symbol->get_type();
return;
}
*/
// If it's a builtin, check for deprecation/godmode
if ( symbol_type == SYM_FUNCTION ) {
int i;
for ( i = 0; DEPRECATED_FUNCTIONS[i][0]; ++i ) {
if ( !strcmp(name, DEPRECATED_FUNCTIONS[i][0]) ) {
if ( DEPRECATED_FUNCTIONS[i][1] == NULL ) {
ERROR(HERE, E_DEPRECATED, name);
} else {
ERROR(HERE, E_DEPRECATED_WITH_REPLACEMENT, name, DEPRECATED_FUNCTIONS[i][1]);
}
symbol = NULL;
type = TYPE(LST_ERROR);
return;
}
}
if (!god_mode) {
for ( i = 0; GOD_MODE_FUNCTIONS[i]; ++i ) {
if ( !strcmp(name, GOD_MODE_FUNCTIONS[i]) ) {
ERROR(HERE, E_GOD_MODE_FUNCTION, name);
symbol = NULL;
type = TYPE(LST_ERROR);
return;
}
}
}
}
// Look up the symbol with the requested type
symbol = lookup_symbol( name, symbol_type );
bool visible = true;
// At this point, we have all symbols defined, but some are not legal.
// Check the cases where we should report an error even if it already
// exists in the symbol table.
if ( symbol && symbol_type == SYM_VARIABLE && !symbol->was_declared()
&& (!symbol->is_global() || is_simple) ) {
visible = false;
}
if ( symbol == NULL || !visible) { // no symbol of the right type
symbol = lookup_symbol( name, SYM_ANY ); // so try the wrong one, so we can have a more descriptive error message in that case.
if ( symbol && symbol_type == SYM_VARIABLE && !symbol->was_declared()
&& (!symbol->is_global() || is_simple) ) {
visible = false;
}
if ( symbol != NULL && visible ) {
if (symbol->get_symbol_type() == SYM_EVENT) {
ERROR( HERE, E_WRONG_TYPE, name,
LLScriptSymbol::get_type_name(symbol_type),
"n", "event"
);
} else {
ERROR( HERE, E_WRONG_TYPE, name,
LLScriptSymbol::get_type_name(symbol_type),
"", LLScriptSymbol::get_type_name(symbol->get_symbol_type()));
}
} else if (symbol_type != SYM_EVENT) { // if it's an event, don't report undefined identifier
char buffer[BUFFER_SIZE+1];
buffer[0] = 0;
// check that the symbol really does not exist,
// otherwise we get: "x is undeclared, did you mean x?"
// for symbols that already exist but are not visible
if (!symbol) {
find_suggestions(this, buffer);
}
if ( buffer[0] == 0 ) {
ERROR( HERE, E_UNDECLARED, name );
} else {
ERROR( HERE, E_UNDECLARED_WITH_SUGGESTION, name, buffer );
}
}
// Set our symbol to null and type to error since we don't know what they should be.
symbol = NULL;
type = TYPE(LST_ERROR);
return;
}
/// If we're requesting a member, like var.x or var.y
if ( member != NULL ) {
// all members must be single letters
if ( member[1] != 0 ) {
ERROR( HERE, E_INVALID_MEMBER, name, member );
type = TYPE(LST_ERROR);
return;
}
// Make sure it's a variable
if ( symbol_type != SYM_VARIABLE ) {
ERROR( HERE, E_MEMBER_NOT_VARIABLE, name, member, LLScriptSymbol::get_type_name(symbol_type));
symbol = NULL;
type = TYPE(LST_ERROR);
return;
}
// Make sure it's a vector or quaternion. TODO: is there a better way to do this?
switch ( symbol->get_type()->get_itype() ) {
case LST_QUATERNION:
if ( member[0] == 's' ) {
type = TYPE(LST_FLOATINGPOINT);
break;
}
// FALL THROUGH
case LST_VECTOR:
switch ( member[0] ) {
case 'x':
case 'y':
case 'z':
type =TYPE(LST_FLOATINGPOINT);
break;
default:
ERROR( HERE, E_INVALID_MEMBER, name, member );
type = TYPE(LST_ERROR);
break;
}
break;
default:
ERROR( HERE, E_MEMBER_WRONG_TYPE, name, member );
type = TYPE(LST_ERROR);
break;
}
} else {
// Set our type to our symbol's type.
type = symbol->get_type();
}
// Add a reference
symbol->add_reference();
}
void LLScriptSimpleAssignable::determine_type() {
LLASTNode *node = get_child(0);
if ( node == NULL )
return;
if ( node->get_node_type() == NODE_IDENTIFIER ) {
LLScriptIdentifier *id = (LLScriptIdentifier *) node;
id->resolve_symbol( SYM_VARIABLE, true );
type = id->get_type();
} else if ( node->get_node_type() == NODE_CONSTANT ) {
type = node->get_type();
}
}
void LLScriptFunctionExpression::determine_type() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
id->resolve_symbol( SYM_FUNCTION );
type = id->get_type();
// can't check types if function is undeclared
if ( id->get_symbol() == NULL )
return;
// 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);
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,
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 ) {
ERROR( IN(passed_param_id), E_TOO_MANY_ARGUMENTS, id->get_name() );
} else if ( declared_param_id != NULL ) {
ERROR( HERE, E_TOO_FEW_ARGUMENTS, id->get_name() );
}
}
void LLScriptLValueExpression::determine_type() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
id->resolve_symbol( SYM_VARIABLE );
if ( id->get_symbol() != NULL && id->get_symbol()->get_sub_type() == SYM_BUILTIN && id->get_member() != NULL ) {
ERROR( HERE, E_INVALID_MEMBER, id->get_name(), id->get_member() );
}
type = id->get_type();
}
void LLScriptStateStatement::determine_type() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
type = TYPE(LST_NULL);
if ( id != NULL )
id->resolve_symbol( SYM_STATE );
// see if we're in a state or function, and if it's allowed
bool state_allowed = false;
LLASTNode *node = NULL;
for ( node = get_parent(); node; node = node->get_parent() ) {
switch (node->get_node_type()) {
case NODE_STATEMENT:
switch (node->get_node_sub_type()) {
case NODE_DO_STATEMENT:
case NODE_FOR_STATEMENT:
case NODE_WHILE_STATEMENT:
state_allowed = true;
break;
case NODE_IF_STATEMENT:
if (node->get_child(2) == NULL || node->get_child(2)->get_node_type() == NODE_NULL) {
state_allowed = true;
}
break;
default:
break;
}
break;
case NODE_STATE:
// we're in a state, see if it's the same one we're calling
if (
// in default and calling state default
(node->get_child(0)->get_node_type() == NODE_NULL && id == NULL) ||
(
// make sure neither current nor target is default
(id != NULL && node->get_child(0)->get_node_type() ==
NODE_IDENTIFIER) &&
// in state x calling state x
!strcmp( ((LLScriptIdentifier*)node->get_child(0))->get_name(),
id->get_name() )
)
) {
ERROR( HERE, W_CHANGE_TO_CURRENT_STATE );
}
return;
case NODE_GLOBAL_FUNCTION:
if ( state_allowed ) {
if ( mono_mode ) {
ERROR( HERE, W_CHANGE_STATE_HACK );
} else {
// see what kind of function it is
switch (node->get_child(0)->get_type()->get_itype()) {
case LST_LIST:
case LST_STRING:
ERROR( HERE, W_CHANGE_STATE_HACK_CORRUPT );
break;
default:
ERROR( HERE, W_CHANGE_STATE_HACK );
break;
}
}
} else {
ERROR( HERE, E_CHANGE_STATE_IN_FUNCTION );
}
return;
default:
break;
}
}
LOG( LOG_ERROR, HERE, "INTERNAL ERROR: encountered state change statement "
"not in function or state!" );
}
void LLScriptJumpStatement::determine_type() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
id->resolve_symbol( SYM_LABEL );
type = id->get_type();
if ( !mono_mode && id->get_symbol() != NULL && id->get_symbol()->get_references() == 2 ) {
ERROR( HERE, W_MULTIPLE_JUMPS_FOR_LABEL, id->get_symbol()->get_name() );
}
}
void LLScriptGlobalVariable::determine_type() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
if ( id->get_symbol() ) id->get_symbol()->set_declared();
LLASTNode *node = get_child(1);
if ( node == NULL || node->get_node_type() == NODE_NULL ) return;
if ( !node->get_type()->can_coerce(id->get_type()) ) {
ERROR( HERE, E_WRONG_TYPE_IN_ASSIGNMENT, id->get_type()->get_node_name(),
id->get_name(), node->get_type()->get_node_name() );
}
if (!mono_mode) {
LST_TYPE type = id->get_type()->get_itype();
LLASTNode *val_node = get_child(1)->get_child(0);
if (type == LST_STRING || type == LST_KEY) {
if (val_node->get_node_type() == NODE_IDENTIFIER && !((LLScriptIdentifier*)val_node)->get_symbol()->is_initialized()) {
ERROR( IN(val_node), W_UNINITIALIZED_VAR_IN_STR_KEY );
} else {
id->get_symbol()->set_initialized();
}
} else {
id->get_symbol()->set_initialized();
if (val_node->get_node_type() == NODE_CONSTANT && val_node->get_node_sub_type() == NODE_LIST_CONSTANT) {
LLASTNode *elem = ((LLScriptListConstant *)val_node)->get_children();
// Check every element to see if one of them is an uninitialized variable
while (elem && elem->get_node_type() == NODE_SIMPLE_ASSIGNABLE) {
if (elem->get_child(0)->get_node_type() == NODE_IDENTIFIER && ((LLScriptIdentifier *)elem->get_child(0))->get_symbol() && !((LLScriptIdentifier *)elem->get_child(0))->get_symbol()->is_initialized()) {
ERROR( IN(elem->get_child(0)), E_UNINITIALIZED_VAR_IN_LIST );
}
elem = elem->get_next();
}
}
}
}
}
void LLScriptDeclaration::determine_type() {
LLScriptIdentifier *id = (LLScriptIdentifier *) get_child(0);
LLASTNode *node = get_child(1);
if ( id->get_symbol() ) id->get_symbol()->set_declared();
if ( node == NULL || node->get_node_type() == NODE_NULL ) return;
if ( !node->get_type()->can_coerce(id->get_type()) ) {
ERROR( HERE, E_WRONG_TYPE_IN_ASSIGNMENT, id->get_type()->get_node_name(),
id->get_name(), node->get_type()->get_node_name() );
}
}
void LLScriptVectorExpression::determine_type() {
type = TYPE(LST_VECTOR);
LLASTNode *node = get_children();
for ( ; node ; node = node->get_next() ) {
if ( !node->get_type()->can_coerce(TYPE(LST_FLOATINGPOINT)) ) {
ERROR( HERE, E_WRONG_TYPE_IN_MEMBER_ASSIGNMENT, "vector",
node->get_type()->get_node_name() );
return;
}
}
}
void LLScriptVectorConstant::determine_type() {
type = TYPE(LST_VECTOR);
LLASTNode *node = get_children();
for ( ; node ; node = node->get_next() ) {
if ( !node->get_type()->can_coerce(TYPE(LST_FLOATINGPOINT)) ) {
ERROR( HERE, E_WRONG_TYPE_IN_MEMBER_ASSIGNMENT, "vector",
node->get_type()->get_node_name() );
return;
}
}
}
void LLScriptQuaternionExpression::determine_type() {
type = TYPE(LST_QUATERNION);
LLASTNode *node = get_children();
for ( ; node ; node = node->get_next() ) {
if ( !node->get_type()->can_coerce(TYPE(LST_FLOATINGPOINT)) ) {
ERROR( HERE, E_WRONG_TYPE_IN_MEMBER_ASSIGNMENT, "quaternion",
node->get_type()->get_node_name() );
return;
}
}
}
void LLScriptQuaternionConstant::determine_type() {
type = TYPE(LST_QUATERNION);
LLASTNode *node = get_children();
for ( ; node ; node = node->get_next() ) {
if ( !node->get_type()->can_coerce(TYPE(LST_FLOATINGPOINT)) ) {
ERROR( HERE, E_WRONG_TYPE_IN_MEMBER_ASSIGNMENT, "quaternion",
node->get_type()->get_node_name() );
return;
}
}
}
void LLScriptReturnStatement::determine_type() {
LLASTNode *node = get_parent();
// crawl up until we find an event handler or global func
while ( node->get_node_type() != NODE_EVENT_HANDLER &&
node->get_node_type() != NODE_GLOBAL_FUNCTION )
node = node->get_parent();
// if an event handler
if ( node->get_node_type() == NODE_EVENT_HANDLER ) {
// make sure we're not returning anything
if ( get_child(0)->get_node_type() != NODE_NULL ) {
ERROR( HERE, E_RETURN_VALUE_IN_EVENT_HANDLER );
}
} else { // otherwise it's a function
// the return type of the function is stored in the identifier which is
// the first child
if ( !get_child(0)->get_type()->can_coerce(
node->get_child(0)->get_type()) ) {
ERROR( HERE, E_BAD_RETURN_TYPE,
get_child(0)->get_type()->get_node_name(),
node->get_child(0)->get_type()->get_node_name() );
}
}
}
void LLScriptIfStatement::determine_type() {
type = TYPE(LST_NULL);
// warn if main branch is an empty statement and secondary branch is null
// or empty
if ( get_child(1)->get_node_type() == NODE_STATEMENT &&
get_child(1)->get_node_sub_type() == NODE_NO_SUB_TYPE &&
get_child(1)->get_children() == NULL &&
(get_child(2)->get_node_type() == NODE_NULL ||
(get_child(2)->get_node_type() == NODE_STATEMENT &&
get_child(2)->get_node_sub_type() == NODE_NO_SUB_TYPE &&
get_child(2)->get_children() == NULL
)
)
) {
ERROR( IN(get_child(0)), W_EMPTY_IF );
DEBUG( LOG_DEBUG_SPAM, NULL, "TYPE=%d SUBTYPE=%d CHILDREN=%p n=%s\n",
get_child(1)->get_node_type(), get_child(1)->get_node_sub_type(),
get_child(1)->get_children(), get_child(1)->get_node_name() );
// do_walk( this );
}
}
void LLASTNode::check_symbols() {
LLASTNode *node;
if ( get_symbol_table() != NULL )
get_symbol_table()->check_symbols();
for ( node = get_children(); node; node = node->get_next() )
node->check_symbols();
}
void usage(const char *name) {
printf("Usage: %s [options] [input]\n", name);
printf("Options: \n");
printf("\t-m\t\tFlag: Use Mono rules for the analysis (default on)\n");
printf("\t-b <file>\tLoad builtin functions from file\n");
printf("\t-t\t\tShow tree structure.\n");
printf("\t-l\t\tShow line/column information as range\n");
printf("\t-p\t\tAdd the file path to the result\n");
printf("\t-v\t\tBe verbose\n");
printf("\t-S\t\tDon't sort log messages\n");
printf("\t-#\t\tShow error codes (for debugging/testing)\n");
printf("\t-A\t\tCheck error assertions (for debugging/testing)\n");
printf("\t-a\t\tFlag: Don't report marked warnings/errors (default on)\n");
printf("\t-i\t\tFlag: Ignore preprocessor directives (default off)\n");
printf("\t-u\t\tFlag: Warn about unused event parameters (default off)\n");
printf("\t-w\t\tFlag: Enable switch statements (default off)\n");
printf("\t-z\t\tFlag: Enable lazy list syntax (default off)\n");
printf("\t-G\t\tFlag: Enable god mode (default off)\n");
#ifdef COMPILE_ENABLED
printf("\t-c\t\tFlag: Compile (default off)\n");
#endif /* COMPILE_ENABLED */
printf("\t-V\t\tPrint long version info and exit.\n");
printf("\t--version\tPrint short version info and exit.\n");
printf("\t--help\t\tPrint this help text and exit.\n");
printf("\n");
printf("Options that are flags can have a - sign at the end to disable them\n");
printf("(e.g. -m- to disable Mono and enable LSO)\n");
return;
}
void version() {
/*
fprintf(stderr, "lslint " VERSION " by masa, built " BUILD_DATE "\n");
fprintf(stderr, "by using this program you agree to smile :o)\n");
*/
fprintf(stderr, " lcllok ;....; ccc:\n");
fprintf(stderr, " ,dNWX0xolodddd:.','''..:OXWWX::\n");
fprintf(stderr, " ::WMMMMMMMMMMMN;.,'.....':xkxx:, =] W-Hat KReW PreZentZ [=\n");
fprintf(stderr, " :,0WMMMMMMMMMMN;','...',,'...'..\n");
fprintf(stderr, " .cNMMMMMMMMMMMK:,,;;.',,'...,,', lslint %s\n", VERSION);
fprintf(stderr, " ,dWMMMMMMMMMMMMMMWWWMKdl,...''..\n");
fprintf(stderr, " ,lWMMMMMMMMMMMMMMMMMMMMMMNo,'':o CODiNG::\n");