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