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