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