]> sourceware.org Git - systemtap.git/blame - staptree.cxx
2005-07-05 Graydon Hoare <graydon@redhat.com>
[systemtap.git] / staptree.cxx
CommitLineData
56099f08 1// parse tree functions
69c68955
FCE
2// Copyright (C) 2005 Red Hat Inc.
3//
4// This file is part of systemtap, and is free software. You can
5// redistribute it and/or modify it under the terms of the GNU General
6// Public License (GPL); either version 2, or (at your option) any
7// later version.
2f1a1aea 8
2b066ec1 9#include "config.h"
2f1a1aea
FCE
10#include "staptree.h"
11#include "parse.h"
12#include <iostream>
56099f08
FCE
13#include <typeinfo>
14#include <cassert>
2f1a1aea 15
2b066ec1
FCE
16using namespace std;
17
2f1a1aea 18
56099f08
FCE
19expression::expression ():
20 type (pe_unknown), tok (0)
21{
22}
23
24
25expression::~expression ()
26{
27}
28
29
30statement::statement ():
31 tok (0)
32{
33}
34
35
36statement::~statement ()
37{
38}
39
40
41symbol::symbol ():
42 referent (0)
43{
44}
45
46
47arrayindex::arrayindex ():
48 referent (0)
49{
50}
51
52
53functioncall::functioncall ():
54 referent (0)
55{
56}
57
58
59symboldecl::symboldecl ():
60 tok (0),
61 type (pe_unknown)
62{
63}
64
65
66symboldecl::~symboldecl ()
67{
68}
69
70
9c0c0e46 71probe_point::probe_point ():
82919855
FCE
72 tok (0)
73{
74}
75
76
77probe::probe ():
78 body (0), tok (0)
9c0c0e46
FCE
79{
80}
81
82
83probe_point::component::component ():
84 arg (0)
85{
86}
87
88
2b066ec1
FCE
89vardecl::vardecl ():
90 arity (-1)
56099f08
FCE
91{
92}
93
94
2b066ec1
FCE
95void
96vardecl::set_arity (int a)
56099f08 97{
2b066ec1 98 assert (a >= 0);
8846477c
FCE
99
100 if (arity != a && arity >= 0)
101 throw semantic_error ("inconsistent arity", tok);
102
103 if (arity != a)
104 {
105 arity = a;
106 index_types.resize (arity);
107 for (int i=0; i<arity; i++)
108 index_types[i] = pe_unknown;
109 }
56099f08
FCE
110}
111
112
113functiondecl::functiondecl ():
114 body (0)
115{
116}
117
118
119literal_number::literal_number (long v)
120{
121 value = v;
122 type = pe_long;
123}
124
2f1a1aea 125
56099f08
FCE
126literal_string::literal_string (const string& v)
127{
128 value = v;
129 type = pe_string;
130}
131
132
133ostream&
134operator << (ostream& o, const exp_type& e)
135{
136 switch (e)
137 {
138 case pe_unknown: o << "unknown"; break;
139 case pe_long: o << "long"; break;
140 case pe_string: o << "string"; break;
141 case pe_stats: o << "stats"; break;
142 default: o << "???"; break;
143 }
144 return o;
145}
146
147
148// ------------------------------------------------------------------------
149// parse tree printing
150
151ostream& operator << (ostream& o, expression& k)
152{
153 k.print (o);
154 return o;
155}
156
157
158void literal_string::print (ostream& o)
159{
2b066ec1 160 // XXX: quote special chars
56099f08
FCE
161 o << '"' << value << '"';
162}
163
f3c26ea5 164
56099f08
FCE
165void literal_number::print (ostream& o)
166{
167 o << value;
168}
169
170
171void binary_expression::print (ostream& o)
172{
173 o << '(' << *left << ")"
174 << op
175 << '(' << *right << ")";
176}
2f1a1aea
FCE
177
178
56099f08 179void unary_expression::print (ostream& o)
2f1a1aea 180{
56099f08
FCE
181 o << op << '(' << *operand << ")";
182}
183
69c68955
FCE
184void array_in::print (ostream& o)
185{
186 o << "[";
187 for (unsigned i=0; i<operand->indexes.size(); i++)
188 {
189 if (i > 0) o << ", ";
190 operand->indexes[i]->print (o);
191 }
192 o << "] in " << operand->base;
193}
56099f08
FCE
194
195void post_crement::print (ostream& o)
196{
197 o << '(' << *operand << ")" << op;
198}
199
200
201void ternary_expression::print (ostream& o)
202{
203 o << "(" << *cond << ") ? ("
204 << *truevalue << ") : ("
205 << *falsevalue << ")";
206}
207
208
209void symbol::print (ostream& o)
210{
211 o << name;
212}
213
214
215void vardecl::print (ostream& o)
216{
217 o << name;
bed7c0af 218 if (arity > 0 || index_types.size() > 0)
56099f08
FCE
219 o << "[...]";
220}
221
2f1a1aea 222
56099f08
FCE
223void vardecl::printsig (ostream& o)
224{
225 o << name << ":" << type;
226 if (index_types.size() > 0)
2f1a1aea 227 {
56099f08
FCE
228 o << " [";
229 for (unsigned i=0; i<index_types.size(); i++)
230 o << (i>0 ? ", " : "") << index_types[i];
231 o << "]";
232 }
233}
234
235
236void functiondecl::print (ostream& o)
237{
238 o << "function " << name << " (";
239 for (unsigned i=0; i<formal_args.size(); i++)
240 o << (i>0 ? ", " : "") << *formal_args[i];
241 o << ")" << endl;
242 body->print(o);
243}
244
245
246void functiondecl::printsig (ostream& o)
247{
248 o << name << ":" << type << " (";
249 for (unsigned i=0; i<formal_args.size(); i++)
250 o << (i>0 ? ", " : "")
251 << *formal_args[i]
252 << ":"
253 << formal_args[i]->type;
254 o << ")";
255}
256
257
258void arrayindex::print (ostream& o)
259{
260 o << base << "[";
261 for (unsigned i=0; i<indexes.size(); i++)
262 o << (i>0 ? ", " : "") << *indexes[i];
263 o << "]";
264}
265
266
267void functioncall::print (ostream& o)
268{
269 o << function << "(";
270 for (unsigned i=0; i<args.size(); i++)
271 o << (i>0 ? ", " : "") << *args[i];
272 o << ")";
273}
274
275
276ostream& operator << (ostream& o, statement& k)
277{
278 k.print (o);
279 return o;
280}
281
282
283void block::print (ostream& o)
284{
285 o << "{" << endl;
286 for (unsigned i=0; i<statements.size(); i++)
69c68955 287 o << *statements [i] << endl;
bb2e3076 288 o << "}";
56099f08
FCE
289}
290
291
292void for_loop::print (ostream& o)
293{
f3c26ea5
FCE
294 o << "for (";
295 init->print (o);
296 o << "; ";
297 cond->print (o);
298 o << "; ";
299 incr->print (o);
bb2e3076 300 o << ") ";
f3c26ea5 301 block->print (o);
56099f08
FCE
302}
303
304
69c68955
FCE
305void foreach_loop::print (ostream& o)
306{
307 o << "foreach ([";
308 for (unsigned i=0; i<indexes.size(); i++)
309 {
310 if (i > 0) o << ", ";
311 indexes[i]->print (o);
312 }
bb2e3076 313 o << "] in " << base << ") ";
69c68955
FCE
314 block->print (o);
315}
316
317
56099f08
FCE
318void null_statement::print (ostream& o)
319{
320 o << ";";
321}
322
323
324void expr_statement::print (ostream& o)
325{
326 o << *value;
327}
328
329
330void return_statement::print (ostream& o)
331{
332 o << "return " << *value;
333}
334
335
336void delete_statement::print (ostream& o)
337{
338 o << "delete " << *value;
339}
340
f3c26ea5
FCE
341void next_statement::print (ostream& o)
342{
343 o << "next";
344}
345
346void break_statement::print (ostream& o)
347{
348 o << "break";
349}
350
351void continue_statement::print (ostream& o)
352{
353 o << "continue";
354}
56099f08
FCE
355
356void if_statement::print (ostream& o)
357{
bb2e3076 358 o << "if (" << *condition << ") "
56099f08
FCE
359 << *thenblock << endl;
360 if (elseblock)
361 o << "else " << *elseblock << endl;
362}
363
364
365void stapfile::print (ostream& o)
366{
367 o << "# file " << name << endl;
368
bed7c0af
FCE
369 for (unsigned i=0; i<globals.size(); i++)
370 {
371 o << "global ";
372 globals[i]->print (o);
373 o << endl;
374 }
375
20c6c071
GH
376 for (unsigned i=0; i<aliases.size(); i++)
377 {
378 aliases[i]->print (o);
379 o << endl;
380 }
381
bed7c0af 382 for (unsigned i=0; i<probes.size(); i++)
56099f08
FCE
383 {
384 probes[i]->print (o);
385 o << endl;
386 }
387
388 for (unsigned j = 0; j < functions.size(); j++)
389 {
390 functions[j]->print (o);
391 o << endl;
392 }
393}
394
395
396void probe::print (ostream& o)
397{
398 o << "probe ";
40a1cb62
FCE
399 printsig (o);
400 o << *body;
401}
402
403
404void probe::printsig (ostream& o)
405{
9c0c0e46 406 for (unsigned i=0; i<locations.size(); i++)
56099f08 407 {
9c0c0e46
FCE
408 o << (i>0 ? ", " : "");
409 locations[i]->print (o);
56099f08 410 }
56099f08
FCE
411}
412
413
9c0c0e46 414void probe_point::print (ostream& o)
56099f08 415{
9c0c0e46
FCE
416 for (unsigned i=0; i<components.size(); i++)
417 {
418 if (i>0) o << ".";
419 probe_point::component* c = components[i];
420 o << c->functor;
421 if (c->arg)
422 o << "(" << *c->arg << ")";
423 }
56099f08
FCE
424}
425
20c6c071
GH
426probe_alias::probe_alias(std::vector<probe_point*> const & aliases):
427 probe (), alias_names (aliases)
428{
429}
430
431void probe_alias::printsig (ostream& o)
432{
433 for (unsigned i=0; i<alias_names.size(); i++)
434 {
435 o << (i>0 ? " = " : "");
436 alias_names[i]->print (o);
437 }
5227f1ea 438 o << " = ";
20c6c071
GH
439 for (unsigned i=0; i<locations.size(); i++)
440 {
441 o << (i>0 ? ", " : "");
442 locations[i]->print (o);
443 }
444}
445
56099f08 446
82919855
FCE
447ostream& operator << (ostream& o, probe_point& k)
448{
449 k.print (o);
450 return o;
451}
452
453
56099f08
FCE
454ostream& operator << (ostream& o, symboldecl& k)
455{
456 k.print (o);
457 return o;
458}
459
460
82919855
FCE
461
462// ------------------------------------------------------------------------
463// visitors
464
465
466void
467block::visit (visitor* u)
468{
469 u->visit_block (this);
470}
471
472void
473for_loop::visit (visitor* u)
474{
475 u->visit_for_loop (this);
476}
477
69c68955
FCE
478void
479foreach_loop::visit (visitor* u)
480{
481 u->visit_foreach_loop (this);
482}
483
82919855
FCE
484void
485null_statement::visit (visitor* u)
486{
487 u->visit_null_statement (this);
488}
489
490void
491expr_statement::visit (visitor* u)
492{
493 u->visit_expr_statement (this);
494}
495
496void
497return_statement::visit (visitor* u)
498{
499 u->visit_return_statement (this);
500}
501
502void
503delete_statement::visit (visitor* u)
504{
505 u->visit_delete_statement (this);
506}
507
508void
509if_statement::visit (visitor* u)
510{
511 u->visit_if_statement (this);
512}
513
f3c26ea5
FCE
514void
515next_statement::visit (visitor* u)
516{
517 u->visit_next_statement (this);
518}
519
520void
521break_statement::visit (visitor* u)
522{
523 u->visit_break_statement (this);
524}
525
526void
527continue_statement::visit (visitor* u)
528{
529 u->visit_continue_statement (this);
530}
531
82919855
FCE
532void
533literal_string::visit(visitor* u)
534{
535 u->visit_literal_string (this);
536}
537
538void
539literal_number::visit(visitor* u)
540{
541 u->visit_literal_number (this);
542}
543
544void
545binary_expression::visit (visitor* u)
546{
547 u->visit_binary_expression (this);
548}
549
550void
551unary_expression::visit (visitor* u)
552{
553 u->visit_unary_expression (this);
554}
555
556void
557pre_crement::visit (visitor* u)
558{
559 u->visit_pre_crement (this);
560}
561
562void
563post_crement::visit (visitor* u)
564{
565 u->visit_post_crement (this);
566}
567
568void
569logical_or_expr::visit (visitor* u)
570{
571 u->visit_logical_or_expr (this);
572}
573
574void
575logical_and_expr::visit (visitor* u)
576{
577 u->visit_logical_and_expr (this);
578}
579
580void
581array_in::visit (visitor* u)
582{
583 u->visit_array_in (this);
584}
585
586void
587comparison::visit (visitor* u)
588{
589 u->visit_comparison (this);
590}
591
592void
593concatenation::visit (visitor* u)
594{
595 u->visit_concatenation (this);
596}
597
82919855
FCE
598void
599ternary_expression::visit (visitor* u)
600{
601 u->visit_ternary_expression (this);
602}
603
604void
605assignment::visit (visitor* u)
606{
607 u->visit_assignment (this);
608}
609
610void
611symbol::visit (visitor* u)
612{
613 u->visit_symbol (this);
614}
615
616void
617arrayindex::visit (visitor* u)
618{
619 u->visit_arrayindex (this);
620}
621
622void
623functioncall::visit (visitor* u)
624{
625 u->visit_functioncall (this);
626}
627
628
629// ------------------------------------------------------------------------
630
631void
632traversing_visitor::visit_block (block *s)
633{
634 for (unsigned i=0; i<s->statements.size(); i++)
635 s->statements[i]->visit (this);
636}
637
638void
639traversing_visitor::visit_null_statement (null_statement *s)
640{
641}
642
643void
644traversing_visitor::visit_expr_statement (expr_statement *s)
645{
646 s->value->visit (this);
647}
648
649void
650traversing_visitor::visit_if_statement (if_statement* s)
651{
652 s->condition->visit (this);
653 s->thenblock->visit (this);
2b066ec1
FCE
654 if (s->elseblock)
655 s->elseblock->visit (this);
82919855
FCE
656}
657
658void
659traversing_visitor::visit_for_loop (for_loop* s)
660{
661 s->init->visit (this);
662 s->cond->visit (this);
663 s->incr->visit (this);
664 s->block->visit (this);
665}
666
69c68955
FCE
667void
668traversing_visitor::visit_foreach_loop (foreach_loop* s)
669{
670 for (unsigned i=0; i<s->indexes.size(); i++)
671 s->indexes[i]->visit (this);
672 s->block->visit (this);
673}
674
82919855
FCE
675void
676traversing_visitor::visit_return_statement (return_statement* s)
677{
678 s->value->visit (this);
679}
680
681void
682traversing_visitor::visit_delete_statement (delete_statement* s)
683{
684 s->value->visit (this);
685}
686
f3c26ea5
FCE
687void
688traversing_visitor::visit_next_statement (next_statement* s)
689{
690}
691
692void
693traversing_visitor::visit_break_statement (break_statement* s)
694{
695}
696
697void
698traversing_visitor::visit_continue_statement (continue_statement* s)
699{
700}
701
82919855
FCE
702void
703traversing_visitor::visit_literal_string (literal_string* e)
704{
705}
706
707void
708traversing_visitor::visit_literal_number (literal_number* e)
709{
710}
711
712void
713traversing_visitor::visit_binary_expression (binary_expression* e)
714{
715 e->left->visit (this);
716 e->right->visit (this);
717}
718
719void
720traversing_visitor::visit_unary_expression (unary_expression* e)
721{
722 e->operand->visit (this);
723}
724
725void
726traversing_visitor::visit_pre_crement (pre_crement* e)
727{
728 e->operand->visit (this);
729}
730
731void
732traversing_visitor::visit_post_crement (post_crement* e)
733{
734 e->operand->visit (this);
735}
736
737
738void
739traversing_visitor::visit_logical_or_expr (logical_or_expr* e)
740{
741 e->left->visit (this);
742 e->right->visit (this);
743}
744
745void
746traversing_visitor::visit_logical_and_expr (logical_and_expr* e)
747{
748 e->left->visit (this);
749 e->right->visit (this);
750}
751
752void
753traversing_visitor::visit_array_in (array_in* e)
754{
ce10591c 755 e->operand->visit (this);
82919855
FCE
756}
757
758void
759traversing_visitor::visit_comparison (comparison* e)
760{
761 e->left->visit (this);
762 e->right->visit (this);
763}
764
765void
766traversing_visitor::visit_concatenation (concatenation* e)
767{
768 e->left->visit (this);
769 e->right->visit (this);
770}
771
82919855
FCE
772void
773traversing_visitor::visit_ternary_expression (ternary_expression* e)
774{
775 e->cond->visit (this);
776 e->truevalue->visit (this);
777 e->falsevalue->visit (this);
778}
779
780void
781traversing_visitor::visit_assignment (assignment* e)
782{
783 e->left->visit (this);
784 e->right->visit (this);
785}
786
787void
788traversing_visitor::visit_symbol (symbol* e)
789{
790}
791
792void
793traversing_visitor::visit_arrayindex (arrayindex* e)
794{
795 for (unsigned i=0; i<e->indexes.size(); i++)
796 e->indexes[i]->visit (this);
797}
798
799void
800traversing_visitor::visit_functioncall (functioncall* e)
801{
802 for (unsigned i=0; i<e->args.size(); i++)
803 e->args[i]->visit (this);
804}
805
806
807// ------------------------------------------------------------------------
808
809
810throwing_visitor::throwing_visitor (const std::string& m): msg (m) {}
811throwing_visitor::throwing_visitor (): msg ("invalid element") {}
812
813
814void
815throwing_visitor::throwone (const token* t)
816{
817 throw semantic_error (msg, t);
818}
819
820void
821throwing_visitor::visit_block (block *s)
822{
823 throwone (s->tok);
824}
825
826void
827throwing_visitor::visit_null_statement (null_statement *s)
828{
829 throwone (s->tok);
830}
831
832void
833throwing_visitor::visit_expr_statement (expr_statement *s)
834{
835 throwone (s->tok);
836}
837
838void
839throwing_visitor::visit_if_statement (if_statement* s)
840{
841 throwone (s->tok);
842}
843
844void
845throwing_visitor::visit_for_loop (for_loop* s)
846{
847 throwone (s->tok);
848}
849
69c68955
FCE
850void
851throwing_visitor::visit_foreach_loop (foreach_loop* s)
852{
853 throwone (s->tok);
854}
855
82919855
FCE
856void
857throwing_visitor::visit_return_statement (return_statement* s)
858{
859 throwone (s->tok);
860}
861
862void
863throwing_visitor::visit_delete_statement (delete_statement* s)
864{
865 throwone (s->tok);
866}
867
f3c26ea5
FCE
868void
869throwing_visitor::visit_next_statement (next_statement* s)
870{
871 throwone (s->tok);
872}
873
874void
875throwing_visitor::visit_break_statement (break_statement* s)
876{
877 throwone (s->tok);
878}
879
880void
881throwing_visitor::visit_continue_statement (continue_statement* s)
882{
883 throwone (s->tok);
884}
885
82919855
FCE
886void
887throwing_visitor::visit_literal_string (literal_string* e)
888{
889 throwone (e->tok);
890}
891
892void
893throwing_visitor::visit_literal_number (literal_number* e)
894{
895 throwone (e->tok);
896}
897
898void
899throwing_visitor::visit_binary_expression (binary_expression* e)
900{
901 throwone (e->tok);
902}
903
904void
905throwing_visitor::visit_unary_expression (unary_expression* e)
906{
907 throwone (e->tok);
908}
909
910void
911throwing_visitor::visit_pre_crement (pre_crement* e)
912{
913 throwone (e->tok);
914}
915
916void
917throwing_visitor::visit_post_crement (post_crement* e)
918{
919 throwone (e->tok);
0054a7ea
FCE
920}
921
922
82919855
FCE
923void
924throwing_visitor::visit_logical_or_expr (logical_or_expr* e)
925{
926 throwone (e->tok);
927}
928
929void
930throwing_visitor::visit_logical_and_expr (logical_and_expr* e)
931{
932 throwone (e->tok);
933}
934
935void
936throwing_visitor::visit_array_in (array_in* e)
937{
938 throwone (e->tok);
939}
940
941void
942throwing_visitor::visit_comparison (comparison* e)
943{
944 throwone (e->tok);
945}
946
947void
948throwing_visitor::visit_concatenation (concatenation* e)
949{
950 throwone (e->tok);
951}
952
82919855
FCE
953void
954throwing_visitor::visit_ternary_expression (ternary_expression* e)
955{
956 throwone (e->tok);
957}
958
959void
960throwing_visitor::visit_assignment (assignment* e)
961{
962 throwone (e->tok);
963}
964
965void
966throwing_visitor::visit_symbol (symbol* e)
967{
968 throwone (e->tok);
969}
970
971void
972throwing_visitor::visit_arrayindex (arrayindex* e)
973{
974 throwone (e->tok);
975}
976
977void
978throwing_visitor::visit_functioncall (functioncall* e)
979{
980 throwone (e->tok);
981}
This page took 0.126473 seconds and 5 git commands to generate.