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