]> sourceware.org Git - systemtap.git/blob - staptree.h
fix regex_query class definition
[systemtap.git] / staptree.h
1 // -*- C++ -*-
2 // Copyright (C) 2005-2013 Red Hat Inc.
3 // Copyright (C) 2006 Intel Corporation.
4 //
5 // This file is part of systemtap, and is free software. You can
6 // redistribute it and/or modify it under the terms of the GNU General
7 // Public License (GPL); either version 2, or (at your option) any
8 // later version.
9
10 #ifndef STAPTREE_H
11 #define STAPTREE_H
12
13 #include <map>
14 #include <stack>
15 #include <set>
16 #include <string>
17 #include <vector>
18 #include <iostream>
19 #include <stdexcept>
20 #include <cassert>
21 extern "C" {
22 #include <stdint.h>
23 }
24
25 struct token; // parse.h
26 struct systemtap_session; // session.h
27
28 struct semantic_error: public std::runtime_error
29 {
30 const token* tok1;
31 const token* tok2;
32 const semantic_error *chain;
33
34 ~semantic_error () throw () {}
35
36 semantic_error (const std::string& msg, const token* t1=0):
37 runtime_error (msg), tok1 (t1), tok2 (0), chain (0) {}
38
39 semantic_error (const std::string& msg, const token* t1,
40 const token* t2):
41 runtime_error (msg), tok1 (t1), tok2 (t2), chain (0) {}
42 };
43
44 // ------------------------------------------------------------------------
45
46 /* struct statistic_decl moved to session.h */
47
48 // ------------------------------------------------------------------------
49
50 enum exp_type
51 {
52 pe_unknown,
53 pe_long, // int64_t
54 pe_string, // std::string
55 pe_stats
56 };
57
58 std::ostream& operator << (std::ostream& o, const exp_type& e);
59
60 struct token;
61 struct visitor;
62 struct update_visitor;
63
64 struct expression
65 {
66 exp_type type;
67 const token* tok;
68 expression ();
69 virtual ~expression ();
70 virtual void print (std::ostream& o) const = 0;
71 virtual void visit (visitor* u) = 0;
72 };
73
74 std::ostream& operator << (std::ostream& o, const expression& k);
75
76
77 struct literal: public expression
78 {
79 };
80
81
82 struct literal_string: public literal
83 {
84 std::string value;
85 literal_string (const std::string& v);
86 void print (std::ostream& o) const;
87 void visit (visitor* u);
88 };
89
90
91 struct literal_number: public literal
92 {
93 int64_t value;
94 bool print_hex;
95 literal_number (int64_t v, bool hex=false);
96 void print (std::ostream& o) const;
97 void visit (visitor* u);
98 };
99
100
101 struct embedded_expr: public expression
102 {
103 std::string code;
104 void print (std::ostream& o) const;
105 void visit (visitor* u);
106 };
107
108
109 struct binary_expression: public expression
110 {
111 expression* left;
112 std::string op;
113 expression* right;
114 void print (std::ostream& o) const;
115 void visit (visitor* u);
116 };
117
118
119 struct unary_expression: public expression
120 {
121 std::string op;
122 expression* operand;
123 void print (std::ostream& o) const;
124 void visit (visitor* u);
125 };
126
127
128 struct pre_crement: public unary_expression
129 {
130 void visit (visitor* u);
131 };
132
133
134 struct post_crement: public unary_expression
135 {
136 void print (std::ostream& o) const;
137 void visit (visitor* u);
138 };
139
140
141 struct logical_or_expr: public binary_expression
142 {
143 void visit (visitor* u);
144 };
145
146
147 struct logical_and_expr: public binary_expression
148 {
149 void visit (visitor* u);
150 };
151
152
153 struct arrayindex;
154 struct array_in: public expression
155 {
156 arrayindex* operand;
157 void print (std::ostream& o) const;
158 void visit (visitor* u);
159 };
160
161 struct regex_query: public binary_expression
162 {
163 literal_string *re; // XXX somewhat redundant with right
164 void visit (visitor* u);
165 };
166
167 struct comparison: public binary_expression
168 {
169 void visit (visitor* u);
170 };
171
172
173 struct concatenation: public binary_expression
174 {
175 void visit (visitor* u);
176 };
177
178
179 struct ternary_expression: public expression
180 {
181 expression* cond;
182 expression* truevalue;
183 expression* falsevalue;
184 void print (std::ostream& o) const;
185 void visit (visitor* u);
186 };
187
188
189 struct assignment: public binary_expression
190 {
191 void visit (visitor* u);
192 };
193
194 struct symbol;
195 struct hist_op;
196 struct indexable
197 {
198 // This is a helper class which, type-wise, acts as a disjoint union
199 // of symbols and histograms. You can ask it whether it's a
200 // histogram or a symbol, and downcast accordingly.
201 void print_indexable (std::ostream& o) const;
202 void visit_indexable (visitor* u);
203 virtual bool is_symbol(symbol *& sym_out);
204 virtual bool is_hist_op(hist_op *& hist_out);
205 virtual bool is_const_symbol(const symbol *& sym_out) const;
206 virtual bool is_const_hist_op(const hist_op *& hist_out) const;
207 virtual const token *get_tok() const = 0;
208 virtual ~indexable() {}
209 };
210
211 // Perform a downcast to one out-value and NULL the other, throwing an
212 // exception if neither downcast succeeds. This is (sadly) about the
213 // best we can accomplish in C++.
214 void
215 classify_indexable(indexable* ix,
216 symbol *& array_out,
217 hist_op *& hist_out);
218
219 void
220 classify_const_indexable(const indexable* ix,
221 symbol const *& array_out,
222 hist_op const *& hist_out);
223
224 struct vardecl;
225 struct symbol:
226 public expression,
227 public indexable
228 {
229 std::string name;
230 vardecl *referent;
231 symbol ();
232 void print (std::ostream& o) const;
233 void visit (visitor* u);
234 // overrides of type 'indexable'
235 const token *get_tok() const;
236 bool is_const_symbol(const symbol *& sym_out) const;
237 bool is_symbol(symbol *& sym_out);
238 };
239
240
241 struct target_symbol: public symbol
242 {
243 enum component_type
244 {
245 comp_struct_member,
246 comp_literal_array_index,
247 comp_expression_array_index,
248 comp_pretty_print, // must be final
249 };
250
251 struct component
252 {
253 const token* tok;
254 component_type type;
255 std::string member; // comp_struct_member, comp_pretty_print
256 int64_t num_index; // comp_literal_array_index
257 expression* expr_index; // comp_expression_array_index
258
259 component(const token* t, const std::string& m, bool pprint=false):
260 tok(t),
261 type(pprint ? comp_pretty_print : comp_struct_member),
262 member(m), num_index(0), expr_index(0)
263 {}
264 component(const token* t, int64_t n):
265 tok(t), type(comp_literal_array_index), num_index(n),
266 expr_index(0) {}
267 component(const token* t, expression* e):
268 tok(t), type(comp_expression_array_index), num_index(0),
269 expr_index(e) {}
270 void print (std::ostream& o) const;
271 };
272
273 bool addressof;
274 std::string target_name;
275 std::string cu_name;
276 std::vector<component> components;
277 semantic_error* saved_conversion_error; // hand-made linked list
278 target_symbol(): addressof(false), saved_conversion_error (0) {}
279 std::string sym_name ();
280 void chain (const semantic_error& er);
281 void print (std::ostream& o) const;
282 void visit (visitor* u);
283 void visit_components (visitor* u);
284 void visit_components (update_visitor* u);
285 void assert_no_components(const std::string& tapset, bool pretty_ok=false);
286 };
287
288 std::ostream& operator << (std::ostream& o, const target_symbol::component& c);
289
290
291 struct cast_op: public target_symbol
292 {
293 expression *operand;
294 std::string type_name, module;
295 void print (std::ostream& o) const;
296 void visit (visitor* u);
297 };
298
299
300 struct defined_op: public expression
301 {
302 target_symbol *operand;
303 void print (std::ostream& o) const;
304 void visit (visitor* u);
305 };
306
307
308 struct entry_op: public expression
309 {
310 expression *operand;
311 void print (std::ostream& o) const;
312 void visit (visitor* u);
313 };
314
315
316 struct perf_op: public expression
317 {
318 literal *operand;
319 void print (std::ostream& o) const;
320 void visit (visitor* u);
321 };
322
323
324 struct arrayindex: public expression
325 {
326 std::vector<expression*> indexes;
327 indexable *base;
328 arrayindex ();
329 void print (std::ostream& o) const;
330 void visit (visitor* u);
331 };
332
333
334 struct functiondecl;
335 struct functioncall: public expression
336 {
337 std::string function;
338 std::vector<expression*> args;
339 functiondecl *referent;
340 functioncall ();
341 void print (std::ostream& o) const;
342 void visit (visitor* u);
343 };
344
345
346 struct print_format: public expression
347 {
348 bool print_to_stream;
349 bool print_with_format;
350 bool print_with_delim;
351 bool print_with_newline;
352 bool print_char;
353
354 // XXX match runtime/vsprintf.c's print_flag
355 // ... for use with number() & number_size()
356 enum format_flag
357 {
358 fmt_flag_zeropad = 1,
359 fmt_flag_sign = 2,
360 fmt_flag_plus = 4,
361 fmt_flag_space = 8,
362 fmt_flag_left = 16,
363 fmt_flag_special = 32,
364 fmt_flag_large = 64,
365 };
366
367 enum conversion_type
368 {
369 conv_unspecified,
370 conv_pointer,
371 conv_number,
372 conv_string,
373 conv_char,
374 conv_memory,
375 conv_memory_hex,
376 conv_literal,
377 conv_binary
378 };
379
380 enum width_type
381 {
382 width_unspecified,
383 width_static,
384 width_dynamic
385 };
386
387 enum precision_type
388 {
389 prec_unspecified,
390 prec_static,
391 prec_dynamic
392 };
393
394 struct format_component
395 {
396 unsigned long flags;
397 unsigned base;
398 unsigned width;
399 unsigned precision;
400 width_type widthtype;
401 precision_type prectype;
402 conversion_type type;
403 std::string literal_string;
404 bool is_empty() const
405 {
406 return flags == 0
407 && widthtype == width_unspecified
408 && prectype == prec_unspecified
409 && type == conv_unspecified
410 && literal_string.empty();
411 }
412 void clear()
413 {
414 flags = 0;
415 widthtype = width_unspecified;
416 prectype = prec_unspecified;
417 type = conv_unspecified;
418 literal_string.clear();
419 }
420 inline void set_flag(format_flag f) { flags |= f; }
421 inline bool test_flag(format_flag f) const { return flags & f; }
422 };
423
424 std::string raw_components;
425 std::vector<format_component> components;
426 format_component delimiter;
427 std::vector<expression*> args;
428 hist_op *hist;
429
430 static std::string components_to_string(std::vector<format_component> const & components);
431 static std::vector<format_component> string_to_components(std::string const & str);
432 static print_format* create(const token *t);
433
434 void print (std::ostream& o) const;
435 void visit (visitor* u);
436
437 private:
438 print_format(bool stream, bool format, bool delim, bool newline, bool _char):
439 print_to_stream(stream), print_with_format(format),
440 print_with_delim(delim), print_with_newline(newline),
441 print_char(_char), hist(NULL)
442 {}
443 };
444
445
446 enum stat_component_type
447 {
448 sc_average,
449 sc_count,
450 sc_sum,
451 sc_min,
452 sc_max,
453 sc_none,
454 };
455
456 struct stat_op: public expression
457 {
458 stat_component_type ctype;
459 expression* stat;
460 void print (std::ostream& o) const;
461 void visit (visitor* u);
462 };
463
464 enum histogram_type
465 {
466 hist_linear,
467 hist_log
468 };
469
470 struct hist_op: public indexable
471 {
472 const token* tok;
473 histogram_type htype;
474 expression* stat;
475 std::vector<int64_t> params;
476 void print (std::ostream& o) const;
477 void visit (visitor* u);
478 // overrides of type 'indexable'
479 const token *get_tok() const;
480 bool is_const_hist_op(const hist_op *& hist_out) const;
481 bool is_hist_op(hist_op *& hist_out);
482 };
483
484 // ------------------------------------------------------------------------
485
486
487 struct symboldecl // unique object per (possibly implicit)
488 // symbol declaration
489 {
490 const token* tok;
491 const token* systemtap_v_conditional; //checking systemtap compatibility
492 std::string name;
493 exp_type type;
494 symboldecl ();
495 virtual ~symboldecl ();
496 virtual void print (std::ostream &o) const = 0;
497 virtual void printsig (std::ostream &o) const = 0;
498 };
499
500
501 std::ostream& operator << (std::ostream& o, const symboldecl& k);
502
503
504 struct vardecl: public symboldecl
505 {
506 void print (std::ostream& o) const;
507 void printsig (std::ostream& o) const;
508 vardecl ();
509 void set_arity (int arity, const token* t);
510 bool compatible_arity (int a);
511 const token* arity_tok; // site where arity was first resolved
512 int arity; // -1: unknown; 0: scalar; >0: array
513 int maxsize; // upperbound on size for arrays
514 std::vector<exp_type> index_types; // for arrays only
515 literal *init; // for global scalars only
516 bool synthetic; // for probe locals only, don't init on entry
517 bool wrap;
518 };
519
520
521 struct vardecl_builtin: public vardecl
522 {
523 };
524
525 struct statement;
526 struct functiondecl: public symboldecl
527 {
528 std::vector<vardecl*> formal_args;
529 std::vector<vardecl*> locals;
530 std::vector<vardecl*> unused_locals;
531 statement* body;
532 bool synthetic;
533 bool mangle_oldstyle;
534 functiondecl ();
535 void print (std::ostream& o) const;
536 void printsig (std::ostream& o) const;
537 void join (systemtap_session& s); // for synthetic functions only
538 };
539
540
541 // ------------------------------------------------------------------------
542
543
544 struct statement
545 {
546 virtual void print (std::ostream& o) const = 0;
547 virtual void visit (visitor* u) = 0;
548 const token* tok;
549 statement ();
550 statement (const token* tok);
551 virtual ~statement ();
552 };
553
554 std::ostream& operator << (std::ostream& o, const statement& k);
555
556
557 struct embeddedcode: public statement
558 {
559 std::string code;
560 void print (std::ostream& o) const;
561 void visit (visitor* u);
562 };
563
564
565 struct block: public statement
566 {
567 std::vector<statement*> statements;
568 void print (std::ostream& o) const;
569 void visit (visitor* u);
570 block () {}
571 block (statement* car, statement* cdr);
572 virtual ~block () {}
573 };
574
575
576 struct try_block: public statement
577 {
578 statement* try_block; // may be 0
579 statement* catch_block; // may be 0
580 symbol* catch_error_var; // may be 0
581 void print (std::ostream& o) const;
582 void visit (visitor* u);
583 };
584
585
586 struct expr_statement;
587 struct for_loop: public statement
588 {
589 expr_statement* init; // may be 0
590 expression* cond;
591 expr_statement* incr; // may be 0
592 statement* block;
593 void print (std::ostream& o) const;
594 void visit (visitor* u);
595 };
596
597
598 struct foreach_loop: public statement
599 {
600 // this part is a specialization of arrayindex
601 std::vector<symbol*> indexes;
602 indexable *base;
603 int sort_direction; // -1: decreasing, 0: none, 1: increasing
604 unsigned sort_column; // 0: value, 1..N: index
605 enum stat_component_type sort_aggr; // for aggregate arrays, which aggregate to sort on
606 symbol* value; // optional iteration value
607 expression* limit; // optional iteration limit
608
609 statement* block;
610 void print (std::ostream& o) const;
611 void visit (visitor* u);
612 };
613
614
615 struct null_statement: public statement
616 {
617 void print (std::ostream& o) const;
618 void visit (visitor* u);
619 null_statement (const token* tok);
620 };
621
622
623 struct expr_statement: public statement
624 {
625 expression* value; // executed for side-effects
626 void print (std::ostream& o) const;
627 void visit (visitor* u);
628 };
629
630
631 struct if_statement: public statement
632 {
633 expression* condition;
634 statement* thenblock;
635 statement* elseblock; // may be 0
636 void print (std::ostream& o) const;
637 void visit (visitor* u);
638 };
639
640
641 struct return_statement: public expr_statement
642 {
643 void print (std::ostream& o) const;
644 void visit (visitor* u);
645 };
646
647
648 struct delete_statement: public expr_statement
649 {
650 void print (std::ostream& o) const;
651 void visit (visitor* u);
652 };
653
654
655 struct break_statement: public statement
656 {
657 void print (std::ostream& o) const;
658 void visit (visitor* u);
659 };
660
661
662 struct continue_statement: public statement
663 {
664 void print (std::ostream& o) const;
665 void visit (visitor* u);
666 };
667
668
669 struct next_statement: public statement
670 {
671 void print (std::ostream& o) const;
672 void visit (visitor* u);
673 };
674
675
676 struct probe;
677 struct derived_probe;
678 struct probe_alias;
679 struct embeddedcode;
680 struct stapfile
681 {
682 std::string name;
683 std::vector<probe*> probes;
684 std::vector<probe_alias*> aliases;
685 std::vector<functiondecl*> functions;
686 std::vector<vardecl*> globals;
687 std::vector<embeddedcode*> embeds;
688 std::string file_contents;
689 bool privileged;
690 stapfile (): file_contents (""),
691 privileged (false) {}
692 void print (std::ostream& o) const;
693 };
694
695
696 struct probe_point
697 {
698 struct component // XXX: sort of a restricted functioncall
699 {
700 std::string functor;
701 literal* arg; // optional
702 component ();
703 const token* tok; // points to component's functor
704 component(std::string const & f, literal * a = NULL);
705 };
706 std::vector<component*> components;
707 bool optional;
708 bool sufficient;
709 expression* condition;
710 void print (std::ostream& o, bool print_extras=true) const;
711 probe_point ();
712 probe_point(const probe_point& pp);
713 probe_point(std::vector<component*> const & comps);
714 std::string str(bool print_extras=true) const;
715 };
716
717 std::ostream& operator << (std::ostream& o, const probe_point& k);
718
719
720 struct probe
721 {
722 std::vector<probe_point*> locations;
723 statement* body;
724 const token* tok;
725 const token* systemtap_v_conditional; //checking systemtap compatibility
726 std::vector<vardecl*> locals;
727 std::vector<vardecl*> unused_locals;
728 static unsigned last_probeidx;
729 probe ();
730 probe (const probe& p, probe_point *l);
731 void print (std::ostream& o) const;
732 virtual void printsig (std::ostream &o) const;
733 virtual void collect_derivation_chain (std::vector<probe*> &probes_list);
734 virtual void collect_derivation_pp_chain (std::vector<probe_point*> &) {}
735 virtual const probe_alias *get_alias () const { return 0; }
736 virtual probe_point *get_alias_loc () const { return 0; }
737 virtual probe* create_alias(probe_point* l, probe_point* a);
738 virtual const probe* basest () const { return this; }
739 virtual const probe* almost_basest () const { return 0; }
740 virtual ~probe() {}
741 bool privileged;
742 std::string name;
743 };
744
745 struct probe_alias: public probe
746 {
747 probe_alias(std::vector<probe_point*> const & aliases);
748 std::vector<probe_point*> alias_names;
749 virtual void printsig (std::ostream &o) const;
750 bool epilogue_style;
751 };
752
753
754 // A derived visitor instance is used to visit the entire
755 // statement/expression tree.
756 struct visitor
757 {
758 // Machinery for differentiating lvalue visits from non-lvalue.
759 std::vector<expression *> active_lvalues;
760 bool is_active_lvalue(expression *e);
761 void push_active_lvalue(expression *e);
762 void pop_active_lvalue();
763
764 virtual ~visitor () {}
765 virtual void visit_block (block *s) = 0;
766 virtual void visit_try_block (try_block *s) = 0;
767 virtual void visit_embeddedcode (embeddedcode *s) = 0;
768 virtual void visit_null_statement (null_statement *s) = 0;
769 virtual void visit_expr_statement (expr_statement *s) = 0;
770 virtual void visit_if_statement (if_statement* s) = 0;
771 virtual void visit_for_loop (for_loop* s) = 0;
772 virtual void visit_foreach_loop (foreach_loop* s) = 0;
773 virtual void visit_return_statement (return_statement* s) = 0;
774 virtual void visit_delete_statement (delete_statement* s) = 0;
775 virtual void visit_next_statement (next_statement* s) = 0;
776 virtual void visit_break_statement (break_statement* s) = 0;
777 virtual void visit_continue_statement (continue_statement* s) = 0;
778 virtual void visit_literal_string (literal_string* e) = 0;
779 virtual void visit_literal_number (literal_number* e) = 0;
780 virtual void visit_embedded_expr (embedded_expr* e) = 0;
781 virtual void visit_binary_expression (binary_expression* e) = 0;
782 virtual void visit_unary_expression (unary_expression* e) = 0;
783 virtual void visit_pre_crement (pre_crement* e) = 0;
784 virtual void visit_post_crement (post_crement* e) = 0;
785 virtual void visit_logical_or_expr (logical_or_expr* e) = 0;
786 virtual void visit_logical_and_expr (logical_and_expr* e) = 0;
787 virtual void visit_array_in (array_in* e) = 0;
788 virtual void visit_regex_query (regex_query* e) = 0;
789 virtual void visit_comparison (comparison* e) = 0;
790 virtual void visit_concatenation (concatenation* e) = 0;
791 virtual void visit_ternary_expression (ternary_expression* e) = 0;
792 virtual void visit_assignment (assignment* e) = 0;
793 virtual void visit_symbol (symbol* e) = 0;
794 virtual void visit_target_symbol (target_symbol* e) = 0;
795 virtual void visit_arrayindex (arrayindex* e) = 0;
796 virtual void visit_functioncall (functioncall* e) = 0;
797 virtual void visit_print_format (print_format* e) = 0;
798 virtual void visit_stat_op (stat_op* e) = 0;
799 virtual void visit_hist_op (hist_op* e) = 0;
800 virtual void visit_cast_op (cast_op* e) = 0;
801 virtual void visit_defined_op (defined_op* e) = 0;
802 virtual void visit_entry_op (entry_op* e) = 0;
803 virtual void visit_perf_op (perf_op* e) = 0;
804 };
805
806
807 // A simple kind of visitor, which travels down to the leaves of the
808 // statement/expression tree, up to but excluding following vardecls
809 // and functioncalls.
810 struct traversing_visitor: public visitor
811 {
812 void visit_block (block *s);
813 void visit_try_block (try_block *s);
814 void visit_embeddedcode (embeddedcode *s);
815 void visit_null_statement (null_statement *s);
816 void visit_expr_statement (expr_statement *s);
817 void visit_if_statement (if_statement* s);
818 void visit_for_loop (for_loop* s);
819 void visit_foreach_loop (foreach_loop* s);
820 void visit_return_statement (return_statement* s);
821 void visit_delete_statement (delete_statement* s);
822 void visit_next_statement (next_statement* s);
823 void visit_break_statement (break_statement* s);
824 void visit_continue_statement (continue_statement* s);
825 void visit_literal_string (literal_string* e);
826 void visit_literal_number (literal_number* e);
827 void visit_embedded_expr (embedded_expr* e);
828 void visit_binary_expression (binary_expression* e);
829 void visit_unary_expression (unary_expression* e);
830 void visit_pre_crement (pre_crement* e);
831 void visit_post_crement (post_crement* e);
832 void visit_logical_or_expr (logical_or_expr* e);
833 void visit_logical_and_expr (logical_and_expr* e);
834 void visit_array_in (array_in* e);
835 void visit_regex_query (regex_query* e);
836 void visit_comparison (comparison* e);
837 void visit_concatenation (concatenation* e);
838 void visit_ternary_expression (ternary_expression* e);
839 void visit_assignment (assignment* e);
840 void visit_symbol (symbol* e);
841 void visit_target_symbol (target_symbol* e);
842 void visit_arrayindex (arrayindex* e);
843 void visit_functioncall (functioncall* e);
844 void visit_print_format (print_format* e);
845 void visit_stat_op (stat_op* e);
846 void visit_hist_op (hist_op* e);
847 void visit_cast_op (cast_op* e);
848 void visit_defined_op (defined_op* e);
849 void visit_entry_op (entry_op* e);
850 void visit_perf_op (perf_op* e);
851 };
852
853
854 // A kind of traversing visitor, which also follows function calls.
855 // It uses an internal set object to prevent infinite recursion.
856 struct functioncall_traversing_visitor: public traversing_visitor
857 {
858 std::set<functiondecl*> traversed;
859 functiondecl* current_function;
860 functioncall_traversing_visitor(): current_function(0) {}
861 void visit_functioncall (functioncall* e);
862 };
863
864
865 // A kind of traversing visitor, which also follows function calls,
866 // and stores the vardecl* referent of each variable read and/or
867 // written and other such sundry side-effect data. It's used by
868 // the elaboration-time optimizer pass.
869 struct varuse_collecting_visitor: public functioncall_traversing_visitor
870 {
871 systemtap_session& session;
872 std::set<vardecl*> read;
873 std::set<vardecl*> written;
874 std::set<vardecl*> used;
875 bool embedded_seen;
876 bool current_lvalue_read;
877 expression* current_lvalue;
878 expression* current_lrvalue;
879 varuse_collecting_visitor(systemtap_session& s):
880 session (s),
881 embedded_seen (false),
882 current_lvalue_read (false),
883 current_lvalue(0),
884 current_lrvalue(0) {}
885 void visit_embeddedcode (embeddedcode *s);
886 void visit_embedded_expr (embedded_expr *e);
887 void visit_try_block (try_block *s);
888 void visit_delete_statement (delete_statement *s);
889 void visit_print_format (print_format *e);
890 void visit_assignment (assignment *e);
891 void visit_arrayindex (arrayindex *e);
892 void visit_target_symbol (target_symbol *e);
893 void visit_symbol (symbol *e);
894 void visit_pre_crement (pre_crement *e);
895 void visit_post_crement (post_crement *e);
896 void visit_foreach_loop (foreach_loop *s);
897 void visit_cast_op (cast_op* e);
898 void visit_defined_op (defined_op* e);
899 void visit_entry_op (entry_op* e);
900 void visit_perf_op (perf_op* e);
901 bool side_effect_free ();
902 bool side_effect_free_wrt (const std::set<vardecl*>& vars);
903 };
904
905
906
907 // A kind of visitor that throws an semantic_error exception
908 // whenever a non-overridden method is called.
909 struct throwing_visitor: public visitor
910 {
911 std::string msg;
912 throwing_visitor (const std::string& m);
913 throwing_visitor ();
914
915 virtual void throwone (const token* t);
916
917 void visit_block (block *s);
918 void visit_try_block (try_block *s);
919 void visit_embeddedcode (embeddedcode *s);
920 void visit_null_statement (null_statement *s);
921 void visit_expr_statement (expr_statement *s);
922 void visit_if_statement (if_statement* s);
923 void visit_for_loop (for_loop* s);
924 void visit_foreach_loop (foreach_loop* s);
925 void visit_return_statement (return_statement* s);
926 void visit_delete_statement (delete_statement* s);
927 void visit_next_statement (next_statement* s);
928 void visit_break_statement (break_statement* s);
929 void visit_continue_statement (continue_statement* s);
930 void visit_literal_string (literal_string* e);
931 void visit_literal_number (literal_number* e);
932 void visit_embedded_expr (embedded_expr* e);
933 void visit_binary_expression (binary_expression* e);
934 void visit_unary_expression (unary_expression* e);
935 void visit_pre_crement (pre_crement* e);
936 void visit_post_crement (post_crement* e);
937 void visit_logical_or_expr (logical_or_expr* e);
938 void visit_logical_and_expr (logical_and_expr* e);
939 void visit_array_in (array_in* e);
940 void visit_regex_query (regex_query* e);
941 void visit_comparison (comparison* e);
942 void visit_concatenation (concatenation* e);
943 void visit_ternary_expression (ternary_expression* e);
944 void visit_assignment (assignment* e);
945 void visit_symbol (symbol* e);
946 void visit_target_symbol (target_symbol* e);
947 void visit_arrayindex (arrayindex* e);
948 void visit_functioncall (functioncall* e);
949 void visit_print_format (print_format* e);
950 void visit_stat_op (stat_op* e);
951 void visit_hist_op (hist_op* e);
952 void visit_cast_op (cast_op* e);
953 void visit_defined_op (defined_op* e);
954 void visit_entry_op (entry_op* e);
955 void visit_perf_op (perf_op* e);
956 };
957
958 // A visitor similar to a traversing_visitor, but with the ability to rewrite
959 // parts of the tree through require/provide.
960
961 struct update_visitor: public visitor
962 {
963 template <typename T> T* require (T* src, bool clearok=false)
964 {
965 T* dst = NULL;
966 if (src != NULL)
967 {
968 src->visit(this);
969 assert(!targets.empty());
970 dst = static_cast<T*>(targets.top()); // XXX: danger will robinson: not typesafe!
971 targets.pop();
972 assert(clearok || dst);
973 }
974 return dst;
975 }
976
977 template <typename T> void provide (T* src)
978 {
979 targets.push(static_cast<void*>(src)); // XXX: not typesafe!
980 }
981
982 template <typename T> void replace (T*& src, bool clearok=false)
983 {
984 src = require(src, clearok);
985 }
986
987 virtual ~update_visitor() { assert(targets.empty()); }
988
989 virtual void visit_block (block *s);
990 virtual void visit_try_block (try_block *s);
991 virtual void visit_embeddedcode (embeddedcode *s);
992 virtual void visit_null_statement (null_statement *s);
993 virtual void visit_expr_statement (expr_statement *s);
994 virtual void visit_if_statement (if_statement* s);
995 virtual void visit_for_loop (for_loop* s);
996 virtual void visit_foreach_loop (foreach_loop* s);
997 virtual void visit_return_statement (return_statement* s);
998 virtual void visit_delete_statement (delete_statement* s);
999 virtual void visit_next_statement (next_statement* s);
1000 virtual void visit_break_statement (break_statement* s);
1001 virtual void visit_continue_statement (continue_statement* s);
1002 virtual void visit_literal_string (literal_string* e);
1003 virtual void visit_literal_number (literal_number* e);
1004 virtual void visit_embedded_expr (embedded_expr* e);
1005 virtual void visit_binary_expression (binary_expression* e);
1006 virtual void visit_unary_expression (unary_expression* e);
1007 virtual void visit_pre_crement (pre_crement* e);
1008 virtual void visit_post_crement (post_crement* e);
1009 virtual void visit_logical_or_expr (logical_or_expr* e);
1010 virtual void visit_logical_and_expr (logical_and_expr* e);
1011 virtual void visit_array_in (array_in* e);
1012 virtual void visit_regex_query (regex_query* e);
1013 virtual void visit_comparison (comparison* e);
1014 virtual void visit_concatenation (concatenation* e);
1015 virtual void visit_ternary_expression (ternary_expression* e);
1016 virtual void visit_assignment (assignment* e);
1017 virtual void visit_symbol (symbol* e);
1018 virtual void visit_target_symbol (target_symbol* e);
1019 virtual void visit_arrayindex (arrayindex* e);
1020 virtual void visit_functioncall (functioncall* e);
1021 virtual void visit_print_format (print_format* e);
1022 virtual void visit_stat_op (stat_op* e);
1023 virtual void visit_hist_op (hist_op* e);
1024 virtual void visit_cast_op (cast_op* e);
1025 virtual void visit_defined_op (defined_op* e);
1026 virtual void visit_entry_op (entry_op* e);
1027 virtual void visit_perf_op (perf_op* e);
1028
1029 private:
1030 std::stack<void *> targets;
1031 };
1032
1033 template <> indexable*
1034 update_visitor::require <indexable> (indexable* src, bool clearok);
1035
1036 // A visitor which performs a deep copy of the root node it's applied
1037 // to. NB: It does not copy any of the variable or function
1038 // declarations; those fields are set to NULL, assuming you want to
1039 // re-infer the declarations in a new context (the one you're copying
1040 // to).
1041
1042 struct deep_copy_visitor: public update_visitor
1043 {
1044 template <typename T> static T* deep_copy (T* e)
1045 {
1046 deep_copy_visitor v;
1047 return v.require (e);
1048 }
1049
1050 virtual void visit_block (block *s);
1051 virtual void visit_try_block (try_block *s);
1052 virtual void visit_embeddedcode (embeddedcode *s);
1053 virtual void visit_null_statement (null_statement *s);
1054 virtual void visit_expr_statement (expr_statement *s);
1055 virtual void visit_if_statement (if_statement* s);
1056 virtual void visit_for_loop (for_loop* s);
1057 virtual void visit_foreach_loop (foreach_loop* s);
1058 virtual void visit_return_statement (return_statement* s);
1059 virtual void visit_delete_statement (delete_statement* s);
1060 virtual void visit_next_statement (next_statement* s);
1061 virtual void visit_break_statement (break_statement* s);
1062 virtual void visit_continue_statement (continue_statement* s);
1063 virtual void visit_literal_string (literal_string* e);
1064 virtual void visit_literal_number (literal_number* e);
1065 virtual void visit_embedded_expr (embedded_expr* e);
1066 virtual void visit_binary_expression (binary_expression* e);
1067 virtual void visit_unary_expression (unary_expression* e);
1068 virtual void visit_pre_crement (pre_crement* e);
1069 virtual void visit_post_crement (post_crement* e);
1070 virtual void visit_logical_or_expr (logical_or_expr* e);
1071 virtual void visit_logical_and_expr (logical_and_expr* e);
1072 virtual void visit_array_in (array_in* e);
1073 virtual void visit_regex_query (regex_query* e);
1074 virtual void visit_comparison (comparison* e);
1075 virtual void visit_concatenation (concatenation* e);
1076 virtual void visit_ternary_expression (ternary_expression* e);
1077 virtual void visit_assignment (assignment* e);
1078 virtual void visit_symbol (symbol* e);
1079 virtual void visit_target_symbol (target_symbol* e);
1080 virtual void visit_arrayindex (arrayindex* e);
1081 virtual void visit_functioncall (functioncall* e);
1082 virtual void visit_print_format (print_format* e);
1083 virtual void visit_stat_op (stat_op* e);
1084 virtual void visit_hist_op (hist_op* e);
1085 virtual void visit_cast_op (cast_op* e);
1086 virtual void visit_defined_op (defined_op* e);
1087 virtual void visit_entry_op (entry_op* e);
1088 virtual void visit_perf_op (perf_op* e);
1089 };
1090
1091 #endif // STAPTREE_H
1092
1093 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.08557 seconds and 6 git commands to generate.