2 // Copyright (C) 2005-2014 Red Hat Inc.
3 // Copyright (C) 2006 Intel Corporation.
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
28 #if defined(HAVE_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
;
35 #error "No shared_ptr implementation found; get boost or modern g++"
38 struct token
; // parse.h
39 struct systemtap_session
; // session.h
41 struct semantic_error
: public std::runtime_error
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
;
53 ~semantic_error () throw ()
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) {}
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)
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)
76 set_chain(*other
.chain
);
79 std::string
errsrc_chain(void) const
81 return errsrc
+ (chain
? "|" + chain
->errsrc_chain() : "");
84 void set_chain(const semantic_error
& new_chain
)
88 chain
= new semantic_error(new_chain
);
91 const semantic_error
* get_chain(void) const
97 const semantic_error
* chain
;
100 // ------------------------------------------------------------------------
102 /* struct statistic_decl moved to session.h */
104 // ------------------------------------------------------------------------
110 pe_string
, // std::string
114 std::ostream
& operator << (std::ostream
& o
, const exp_type
& e
);
118 struct exp_type_details
120 virtual ~exp_type_details () {};
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
); }
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;
133 typedef shared_ptr
<exp_type_details
> exp_type_ptr
;
138 struct update_visitor
;
142 virtual ~visitable ();
146 struct expression
: public visitable
149 exp_type_ptr type_details
;
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
);
158 std::ostream
& operator << (std::ostream
& o
, const expression
& k
);
161 struct literal
: public expression
166 struct literal_string
: public literal
169 literal_string (const std::string
& v
);
170 void print (std::ostream
& o
) const;
171 void visit (visitor
* u
);
175 struct literal_number
: public literal
179 literal_number (int64_t v
, bool hex
=false);
180 void print (std::ostream
& o
) const;
181 void visit (visitor
* u
);
185 struct embedded_expr
: public expression
188 void print (std::ostream
& o
) const;
189 void visit (visitor
* u
);
193 struct binary_expression
: public expression
198 void print (std::ostream
& o
) const;
199 void visit (visitor
* u
);
203 struct unary_expression
: public expression
207 void print (std::ostream
& o
) const;
208 void visit (visitor
* u
);
212 struct pre_crement
: public unary_expression
214 void visit (visitor
* u
);
218 struct post_crement
: public unary_expression
220 void print (std::ostream
& o
) const;
221 void visit (visitor
* u
);
225 struct logical_or_expr
: public binary_expression
227 void visit (visitor
* u
);
231 struct logical_and_expr
: public binary_expression
233 void visit (visitor
* u
);
238 struct array_in
: public expression
241 void print (std::ostream
& o
) const;
242 void visit (visitor
* u
);
245 struct regex_query
: public expression
249 literal_string
* right
;
250 void visit (visitor
* u
);
251 void print (std::ostream
& o
) const;
254 struct comparison
: public binary_expression
256 void visit (visitor
* u
);
260 struct concatenation
: public binary_expression
262 void visit (visitor
* u
);
266 struct ternary_expression
: public expression
269 expression
* truevalue
;
270 expression
* falsevalue
;
271 void print (std::ostream
& o
) const;
272 void visit (visitor
* u
);
276 struct assignment
: public binary_expression
278 void visit (visitor
* u
);
282 struct indexable
: public expression
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() {}
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++.
296 classify_indexable(indexable
* ix
,
298 hist_op
*& hist_out
);
301 struct symbol
: public indexable
306 void print (std::ostream
& o
) const;
307 void visit (visitor
* u
);
308 // overrides of type 'indexable'
309 bool is_symbol(symbol
*& sym_out
);
313 struct target_symbol
: public expression
318 comp_literal_array_index
,
319 comp_expression_array_index
,
320 comp_pretty_print
, // must be final
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
331 component(const token
* t
, const std::string
& m
, bool pprint
=false):
333 type(pprint
? comp_pretty_print
: comp_struct_member
),
334 member(m
), num_index(0), expr_index(0)
336 component(const token
* t
, int64_t n
):
337 tok(t
), type(comp_literal_array_index
), num_index(n
),
339 component(const token
* t
, expression
* e
):
340 tok(t
), type(comp_expression_array_index
), num_index(0),
342 void print (std::ostream
& o
) const;
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;
361 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
);
364 struct cast_op
: public target_symbol
367 std::string type_name
, module
;
368 void print (std::ostream
& o
) const;
369 void visit (visitor
* u
);
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
377 void print (std::ostream
& o
) const;
378 void visit (visitor
* u
);
381 struct atvar_op
: public target_symbol
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
);
389 struct defined_op
: public expression
392 void print (std::ostream
& o
) const;
393 void visit (visitor
* u
);
397 struct entry_op
: public expression
400 void print (std::ostream
& o
) const;
401 void visit (visitor
* u
);
405 struct perf_op
: public expression
407 literal_string
*operand
;
408 void print (std::ostream
& o
) const;
409 void visit (visitor
* u
);
413 struct arrayindex
: public expression
415 std::vector
<expression
*> indexes
;
418 void print (std::ostream
& o
) const;
419 void visit (visitor
* u
);
424 struct functioncall
: public expression
426 std::string function
;
427 std::vector
<expression
*> args
;
428 functiondecl
*referent
;
430 void print (std::ostream
& o
) const;
431 void visit (visitor
* u
);
435 struct print_format
: public expression
437 bool print_to_stream
;
438 bool print_with_format
;
439 bool print_with_delim
;
440 bool print_with_newline
;
443 // XXX match runtime/vsprintf.c's print_flag
444 // ... for use with number() & number_size()
447 fmt_flag_zeropad
= 1,
452 fmt_flag_special
= 32,
483 struct format_component
489 width_type widthtype
;
490 precision_type prectype
;
491 conversion_type type
;
492 std::string literal_string
;
493 bool is_empty() const
496 && widthtype
== width_unspecified
497 && prectype
== prec_unspecified
498 && type
== conv_unspecified
499 && literal_string
.empty();
504 widthtype
= width_unspecified
;
506 prectype
= prec_unspecified
;
507 type
= conv_unspecified
;
508 literal_string
.clear();
510 format_component() { clear(); }
511 inline void set_flag(format_flag f
) { flags
|= f
; }
512 inline bool test_flag(format_flag f
) const { return flags
& f
; }
515 std::string raw_components
;
516 std::vector
<format_component
> components
;
517 format_component delimiter
;
518 std::vector
<expression
*> args
;
521 static std::string
components_to_string(std::vector
<format_component
> const & components
);
522 static std::vector
<format_component
> string_to_components(std::string
const & str
);
523 static print_format
* create(const token
*t
, const char *n
= NULL
);
525 void print (std::ostream
& o
) const;
526 void visit (visitor
* u
);
529 std::string print_format_type
;
530 print_format(bool stream
, bool format
, bool delim
, bool newline
, bool _char
, std::string type
):
531 print_to_stream(stream
), print_with_format(format
),
532 print_with_delim(delim
), print_with_newline(newline
),
533 print_char(_char
), hist(NULL
), print_format_type(type
)
538 enum stat_component_type
548 struct stat_op
: public expression
550 stat_component_type ctype
;
552 void print (std::ostream
& o
) const;
553 void visit (visitor
* u
);
562 struct hist_op
: public indexable
564 histogram_type htype
;
566 std::vector
<int64_t> params
;
567 void print (std::ostream
& o
) const;
568 void visit (visitor
* u
);
569 // overrides of type 'indexable'
570 bool is_hist_op(hist_op
*& hist_out
);
573 // ------------------------------------------------------------------------
576 struct symboldecl
// unique object per (possibly implicit)
577 // symbol declaration
580 const token
* systemtap_v_conditional
; //checking systemtap compatibility
583 exp_type_ptr type_details
;
585 virtual ~symboldecl ();
586 virtual void print (std::ostream
&o
) const = 0;
587 virtual void printsig (std::ostream
&o
) const = 0;
591 std::ostream
& operator << (std::ostream
& o
, const symboldecl
& k
);
594 struct vardecl
: public symboldecl
596 void print (std::ostream
& o
) const;
597 void printsig (std::ostream
& o
) const;
599 void set_arity (int arity
, const token
* t
);
600 bool compatible_arity (int a
);
601 const token
* arity_tok
; // site where arity was first resolved
602 int arity
; // -1: unknown; 0: scalar; >0: array
603 int maxsize
; // upperbound on size for arrays
604 std::vector
<exp_type
> index_types
; // for arrays only
605 literal
*init
; // for global scalars only
606 bool synthetic
; // for probe locals only, don't init on entry
608 bool char_ptr_arg
; // set in ::emit_common_header(), only used if a formal_arg
612 struct vardecl_builtin
: public vardecl
617 struct functiondecl
: public symboldecl
619 std::vector
<vardecl
*> formal_args
;
620 std::vector
<vardecl
*> locals
;
621 std::vector
<vardecl
*> unused_locals
;
622 std::vector
<vardecl
*> unmodified_args
; // not modified after assignment
625 bool mangle_oldstyle
;
627 void print (std::ostream
& o
) const;
628 void printsig (std::ostream
& o
) const;
629 void printsigtags (std::ostream
& o
, bool all_tags
) const;
630 void join (systemtap_session
& s
); // for synthetic functions only
634 // ------------------------------------------------------------------------
637 struct statement
: public visitable
639 virtual void print (std::ostream
& o
) const = 0;
640 virtual void visit (visitor
* u
) = 0;
643 statement (const token
* tok
);
644 virtual ~statement ();
647 std::ostream
& operator << (std::ostream
& o
, const statement
& k
);
650 struct embeddedcode
: public statement
653 void print (std::ostream
& o
) const;
654 void visit (visitor
* u
);
658 struct block
: public statement
660 std::vector
<statement
*> statements
;
661 void print (std::ostream
& o
) const;
662 void visit (visitor
* u
);
664 block (statement
* car
, statement
* cdr
);
669 struct try_block
: public statement
671 statement
* try_block
; // may be 0
672 statement
* catch_block
; // may be 0
673 symbol
* catch_error_var
; // may be 0
674 void print (std::ostream
& o
) const;
675 void visit (visitor
* u
);
679 struct expr_statement
;
680 struct for_loop
: public statement
682 expr_statement
* init
; // may be 0
684 expr_statement
* incr
; // may be 0
686 void print (std::ostream
& o
) const;
687 void visit (visitor
* u
);
691 struct foreach_loop
: public statement
693 // this part is a specialization of arrayindex
694 std::vector
<symbol
*> indexes
;
695 std::vector
<expression
*> array_slice
; // optional array slice to iterate over
697 int sort_direction
; // -1: decreasing, 0: none, 1: increasing
698 unsigned sort_column
; // 0: value, 1..N: index
699 enum stat_component_type sort_aggr
; // for aggregate arrays, which aggregate to sort on
700 symbol
* value
; // optional iteration value
701 expression
* limit
; // optional iteration limit
704 void print (std::ostream
& o
) const;
705 void visit (visitor
* u
);
709 struct null_statement
: public statement
711 void print (std::ostream
& o
) const;
712 void visit (visitor
* u
);
713 null_statement (const token
* tok
);
717 struct expr_statement
: public statement
719 expression
* value
; // executed for side-effects
720 void print (std::ostream
& o
) const;
721 void visit (visitor
* u
);
725 struct if_statement
: public statement
727 expression
* condition
;
728 statement
* thenblock
;
729 statement
* elseblock
; // may be 0
730 void print (std::ostream
& o
) const;
731 void visit (visitor
* u
);
735 struct return_statement
: public expr_statement
737 void print (std::ostream
& o
) const;
738 void visit (visitor
* u
);
742 struct delete_statement
: public expr_statement
744 void print (std::ostream
& o
) const;
745 void visit (visitor
* u
);
749 struct break_statement
: public statement
751 void print (std::ostream
& o
) const;
752 void visit (visitor
* u
);
756 struct continue_statement
: public statement
758 void print (std::ostream
& o
) const;
759 void visit (visitor
* u
);
763 struct next_statement
: public statement
765 void print (std::ostream
& o
) const;
766 void visit (visitor
* u
);
771 struct derived_probe
;
777 std::vector
<probe
*> probes
;
778 std::vector
<probe_alias
*> aliases
;
779 std::vector
<functiondecl
*> functions
;
780 std::vector
<vardecl
*> globals
;
781 std::vector
<embeddedcode
*> embeds
;
782 std::string file_contents
;
784 bool synthetic
; // via parse_synthetic_*
785 stapfile (): file_contents (""),
786 privileged (false), synthetic (false) {}
787 void print (std::ostream
& o
) const;
793 struct component
// XXX: sort of a restricted functioncall
796 literal
* arg
; // optional
798 const token
* tok
; // points to component's functor
799 component(std::string
const & f
, literal
* a
= NULL
);
801 std::vector
<component
*> components
;
805 bool well_formed
; // used in derived_probe::script_location()
806 expression
* condition
;
807 void print (std::ostream
& o
, bool print_extras
=true) const;
809 probe_point(const probe_point
& pp
);
810 probe_point(std::vector
<component
*> const & comps
);
811 std::string
str(bool print_extras
=true) const;
814 std::ostream
& operator << (std::ostream
& o
, const probe_point
& k
);
819 std::vector
<probe_point
*> locations
;
823 const token
* systemtap_v_conditional
; //checking systemtap compatibility
824 std::vector
<vardecl
*> locals
;
825 std::vector
<vardecl
*> unused_locals
;
826 static unsigned last_probeidx
;
828 probe (probe
* p
, probe_point
*l
);
829 void print (std::ostream
& o
) const;
830 virtual void printsig (std::ostream
&o
) const;
831 virtual void collect_derivation_chain (std::vector
<probe
*> &probes_list
) const;
832 virtual void collect_derivation_pp_chain (std::vector
<probe_point
*> &) const;
833 virtual const probe_alias
*get_alias () const { return 0; }
834 virtual probe_point
*get_alias_loc () const { return 0; }
840 struct probe_alias
: public probe
842 probe_alias(std::vector
<probe_point
*> const & aliases
);
843 std::vector
<probe_point
*> alias_names
;
844 virtual void printsig (std::ostream
&o
) const;
849 // A derived visitor instance is used to visit the entire
850 // statement/expression tree.
853 // Machinery for differentiating lvalue visits from non-lvalue.
854 std::vector
<expression
*> active_lvalues
;
855 bool is_active_lvalue(expression
*e
);
856 void push_active_lvalue(expression
*e
);
857 void pop_active_lvalue();
859 virtual ~visitor () {}
860 virtual void visit_block (block
*s
) = 0;
861 virtual void visit_try_block (try_block
*s
) = 0;
862 virtual void visit_embeddedcode (embeddedcode
*s
) = 0;
863 virtual void visit_null_statement (null_statement
*s
) = 0;
864 virtual void visit_expr_statement (expr_statement
*s
) = 0;
865 virtual void visit_if_statement (if_statement
* s
) = 0;
866 virtual void visit_for_loop (for_loop
* s
) = 0;
867 virtual void visit_foreach_loop (foreach_loop
* s
) = 0;
868 virtual void visit_return_statement (return_statement
* s
) = 0;
869 virtual void visit_delete_statement (delete_statement
* s
) = 0;
870 virtual void visit_next_statement (next_statement
* s
) = 0;
871 virtual void visit_break_statement (break_statement
* s
) = 0;
872 virtual void visit_continue_statement (continue_statement
* s
) = 0;
873 virtual void visit_literal_string (literal_string
* e
) = 0;
874 virtual void visit_literal_number (literal_number
* e
) = 0;
875 virtual void visit_embedded_expr (embedded_expr
* e
) = 0;
876 virtual void visit_binary_expression (binary_expression
* e
) = 0;
877 virtual void visit_unary_expression (unary_expression
* e
) = 0;
878 virtual void visit_pre_crement (pre_crement
* e
) = 0;
879 virtual void visit_post_crement (post_crement
* e
) = 0;
880 virtual void visit_logical_or_expr (logical_or_expr
* e
) = 0;
881 virtual void visit_logical_and_expr (logical_and_expr
* e
) = 0;
882 virtual void visit_array_in (array_in
* e
) = 0;
883 virtual void visit_regex_query (regex_query
* e
) = 0;
884 virtual void visit_comparison (comparison
* e
) = 0;
885 virtual void visit_concatenation (concatenation
* e
) = 0;
886 virtual void visit_ternary_expression (ternary_expression
* e
) = 0;
887 virtual void visit_assignment (assignment
* e
) = 0;
888 virtual void visit_symbol (symbol
* e
) = 0;
889 virtual void visit_target_symbol (target_symbol
* e
) = 0;
890 virtual void visit_arrayindex (arrayindex
* e
) = 0;
891 virtual void visit_functioncall (functioncall
* e
) = 0;
892 virtual void visit_print_format (print_format
* e
) = 0;
893 virtual void visit_stat_op (stat_op
* e
) = 0;
894 virtual void visit_hist_op (hist_op
* e
) = 0;
895 virtual void visit_cast_op (cast_op
* e
) = 0;
896 virtual void visit_autocast_op (autocast_op
* e
) = 0;
897 virtual void visit_atvar_op (atvar_op
* e
) = 0;
898 virtual void visit_defined_op (defined_op
* e
) = 0;
899 virtual void visit_entry_op (entry_op
* e
) = 0;
900 virtual void visit_perf_op (perf_op
* e
) = 0;
904 // A simple kind of visitor, which travels down to the leaves of the
905 // statement/expression tree, up to but excluding following vardecls
906 // and functioncalls.
907 struct traversing_visitor
: public visitor
909 void visit_block (block
*s
);
910 void visit_try_block (try_block
*s
);
911 void visit_embeddedcode (embeddedcode
*s
);
912 void visit_null_statement (null_statement
*s
);
913 void visit_expr_statement (expr_statement
*s
);
914 void visit_if_statement (if_statement
* s
);
915 void visit_for_loop (for_loop
* s
);
916 void visit_foreach_loop (foreach_loop
* s
);
917 void visit_return_statement (return_statement
* s
);
918 void visit_delete_statement (delete_statement
* s
);
919 void visit_next_statement (next_statement
* s
);
920 void visit_break_statement (break_statement
* s
);
921 void visit_continue_statement (continue_statement
* s
);
922 void visit_literal_string (literal_string
* e
);
923 void visit_literal_number (literal_number
* e
);
924 void visit_embedded_expr (embedded_expr
* e
);
925 void visit_binary_expression (binary_expression
* e
);
926 void visit_unary_expression (unary_expression
* e
);
927 void visit_pre_crement (pre_crement
* e
);
928 void visit_post_crement (post_crement
* e
);
929 void visit_logical_or_expr (logical_or_expr
* e
);
930 void visit_logical_and_expr (logical_and_expr
* e
);
931 void visit_array_in (array_in
* e
);
932 void visit_regex_query (regex_query
* e
);
933 void visit_comparison (comparison
* e
);
934 void visit_concatenation (concatenation
* e
);
935 void visit_ternary_expression (ternary_expression
* e
);
936 void visit_assignment (assignment
* e
);
937 void visit_symbol (symbol
* e
);
938 void visit_target_symbol (target_symbol
* e
);
939 void visit_arrayindex (arrayindex
* e
);
940 void visit_functioncall (functioncall
* e
);
941 void visit_print_format (print_format
* e
);
942 void visit_stat_op (stat_op
* e
);
943 void visit_hist_op (hist_op
* e
);
944 void visit_cast_op (cast_op
* e
);
945 void visit_autocast_op (autocast_op
* e
);
946 void visit_atvar_op (atvar_op
* e
);
947 void visit_defined_op (defined_op
* e
);
948 void visit_entry_op (entry_op
* e
);
949 void visit_perf_op (perf_op
* e
);
953 // A visitor that calls a generic visit_expression on every expression.
954 struct expression_visitor
: public traversing_visitor
956 virtual void visit_expression(expression
*e
) = 0;
958 void visit_literal_string (literal_string
* e
);
959 void visit_literal_number (literal_number
* e
);
960 void visit_embedded_expr (embedded_expr
* e
);
961 void visit_binary_expression (binary_expression
* e
);
962 void visit_unary_expression (unary_expression
* e
);
963 void visit_pre_crement (pre_crement
* e
);
964 void visit_post_crement (post_crement
* e
);
965 void visit_logical_or_expr (logical_or_expr
* e
);
966 void visit_logical_and_expr (logical_and_expr
* e
);
967 void visit_array_in (array_in
* e
);
968 void visit_regex_query (regex_query
* e
);
969 void visit_comparison (comparison
* e
);
970 void visit_concatenation (concatenation
* e
);
971 void visit_ternary_expression (ternary_expression
* e
);
972 void visit_assignment (assignment
* e
);
973 void visit_symbol (symbol
* e
);
974 void visit_target_symbol (target_symbol
* e
);
975 void visit_arrayindex (arrayindex
* e
);
976 void visit_functioncall (functioncall
* e
);
977 void visit_print_format (print_format
* e
);
978 void visit_stat_op (stat_op
* e
);
979 void visit_hist_op (hist_op
* e
);
980 void visit_cast_op (cast_op
* e
);
981 void visit_autocast_op (autocast_op
* e
);
982 void visit_atvar_op (atvar_op
* e
);
983 void visit_defined_op (defined_op
* e
);
984 void visit_entry_op (entry_op
* e
);
985 void visit_perf_op (perf_op
* e
);
989 // A kind of traversing visitor, which also follows function calls.
990 // It uses an internal set object to prevent infinite recursion.
991 struct functioncall_traversing_visitor
: public traversing_visitor
993 std::set
<functiondecl
*> seen
;
994 std::set
<functiondecl
*> nested
;
995 functiondecl
* current_function
;
996 functioncall_traversing_visitor(): current_function(0) {}
997 void visit_functioncall (functioncall
* e
);
998 virtual void note_recursive_functioncall (functioncall
* e
);
1002 // A kind of traversing visitor, which also follows function calls,
1003 // and stores the vardecl* referent of each variable read and/or
1004 // written and other such sundry side-effect data. It's used by
1005 // the elaboration-time optimizer pass.
1006 struct varuse_collecting_visitor
: public functioncall_traversing_visitor
1008 systemtap_session
& session
;
1009 std::set
<vardecl
*> read
;
1010 std::set
<vardecl
*> written
;
1011 std::set
<vardecl
*> used
;
1013 bool current_lvalue_read
;
1014 expression
* current_lvalue
;
1015 expression
* current_lrvalue
;
1016 varuse_collecting_visitor(systemtap_session
& s
):
1018 embedded_seen (false),
1019 current_lvalue_read (false),
1021 current_lrvalue(0) {}
1022 void visit_embeddedcode (embeddedcode
*s
);
1023 void visit_embedded_expr (embedded_expr
*e
);
1024 void visit_try_block (try_block
*s
);
1025 void visit_delete_statement (delete_statement
*s
);
1026 void visit_print_format (print_format
*e
);
1027 void visit_assignment (assignment
*e
);
1028 void visit_arrayindex (arrayindex
*e
);
1029 void visit_target_symbol (target_symbol
*e
);
1030 void visit_symbol (symbol
*e
);
1031 void visit_pre_crement (pre_crement
*e
);
1032 void visit_post_crement (post_crement
*e
);
1033 void visit_foreach_loop (foreach_loop
*s
);
1034 void visit_cast_op (cast_op
* e
);
1035 void visit_autocast_op (autocast_op
* e
);
1036 void visit_atvar_op (atvar_op
*e
);
1037 void visit_defined_op (defined_op
* e
);
1038 void visit_entry_op (entry_op
* e
);
1039 void visit_perf_op (perf_op
* e
);
1040 bool side_effect_free ();
1041 bool side_effect_free_wrt (const std::set
<vardecl
*>& vars
);
1046 // A kind of visitor that throws an semantic_error exception
1047 // whenever a non-overridden method is called.
1048 struct throwing_visitor
: public visitor
1051 throwing_visitor (const std::string
& m
);
1052 throwing_visitor ();
1054 virtual void throwone (const token
* t
);
1056 void visit_block (block
*s
);
1057 void visit_try_block (try_block
*s
);
1058 void visit_embeddedcode (embeddedcode
*s
);
1059 void visit_null_statement (null_statement
*s
);
1060 void visit_expr_statement (expr_statement
*s
);
1061 void visit_if_statement (if_statement
* s
);
1062 void visit_for_loop (for_loop
* s
);
1063 void visit_foreach_loop (foreach_loop
* s
);
1064 void visit_return_statement (return_statement
* s
);
1065 void visit_delete_statement (delete_statement
* s
);
1066 void visit_next_statement (next_statement
* s
);
1067 void visit_break_statement (break_statement
* s
);
1068 void visit_continue_statement (continue_statement
* s
);
1069 void visit_literal_string (literal_string
* e
);
1070 void visit_literal_number (literal_number
* e
);
1071 void visit_embedded_expr (embedded_expr
* e
);
1072 void visit_binary_expression (binary_expression
* e
);
1073 void visit_unary_expression (unary_expression
* e
);
1074 void visit_pre_crement (pre_crement
* e
);
1075 void visit_post_crement (post_crement
* e
);
1076 void visit_logical_or_expr (logical_or_expr
* e
);
1077 void visit_logical_and_expr (logical_and_expr
* e
);
1078 void visit_array_in (array_in
* e
);
1079 void visit_regex_query (regex_query
* e
);
1080 void visit_comparison (comparison
* e
);
1081 void visit_concatenation (concatenation
* e
);
1082 void visit_ternary_expression (ternary_expression
* e
);
1083 void visit_assignment (assignment
* e
);
1084 void visit_symbol (symbol
* e
);
1085 void visit_target_symbol (target_symbol
* e
);
1086 void visit_arrayindex (arrayindex
* e
);
1087 void visit_functioncall (functioncall
* e
);
1088 void visit_print_format (print_format
* e
);
1089 void visit_stat_op (stat_op
* e
);
1090 void visit_hist_op (hist_op
* e
);
1091 void visit_cast_op (cast_op
* e
);
1092 void visit_autocast_op (autocast_op
* e
);
1093 void visit_atvar_op (atvar_op
* e
);
1094 void visit_defined_op (defined_op
* e
);
1095 void visit_entry_op (entry_op
* e
);
1096 void visit_perf_op (perf_op
* e
);
1099 // A visitor similar to a traversing_visitor, but with the ability to rewrite
1100 // parts of the tree through require/provide.
1102 struct update_visitor
: public visitor
1104 template <typename T
> T
* require (T
* src
, bool clearok
=false)
1111 throw std::runtime_error(_("update_visitor wasn't provided a value"));
1112 visitable
*v
= values
.top();
1114 if (v
== NULL
&& !clearok
)
1115 throw std::runtime_error(_("update_visitor was provided a NULL value"));
1116 dst
= dynamic_cast<T
*>(v
);
1117 if (v
!= NULL
&& dst
== NULL
)
1118 throw std::runtime_error(_F("update_visitor can't set type \"%s\" with a \"%s\"",
1119 typeid(T
).name(), typeid(*v
).name()));
1124 template <typename T
> void provide (T
* src
)
1129 template <typename T
> void replace (T
*& src
, bool clearok
=false)
1131 src
= require(src
, clearok
);
1134 virtual ~update_visitor() { assert(values
.empty()); }
1136 virtual void visit_block (block
*s
);
1137 virtual void visit_try_block (try_block
*s
);
1138 virtual void visit_embeddedcode (embeddedcode
*s
);
1139 virtual void visit_null_statement (null_statement
*s
);
1140 virtual void visit_expr_statement (expr_statement
*s
);
1141 virtual void visit_if_statement (if_statement
* s
);
1142 virtual void visit_for_loop (for_loop
* s
);
1143 virtual void visit_foreach_loop (foreach_loop
* s
);
1144 virtual void visit_return_statement (return_statement
* s
);
1145 virtual void visit_delete_statement (delete_statement
* s
);
1146 virtual void visit_next_statement (next_statement
* s
);
1147 virtual void visit_break_statement (break_statement
* s
);
1148 virtual void visit_continue_statement (continue_statement
* s
);
1149 virtual void visit_literal_string (literal_string
* e
);
1150 virtual void visit_literal_number (literal_number
* e
);
1151 virtual void visit_embedded_expr (embedded_expr
* e
);
1152 virtual void visit_binary_expression (binary_expression
* e
);
1153 virtual void visit_unary_expression (unary_expression
* e
);
1154 virtual void visit_pre_crement (pre_crement
* e
);
1155 virtual void visit_post_crement (post_crement
* e
);
1156 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1157 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1158 virtual void visit_array_in (array_in
* e
);
1159 virtual void visit_regex_query (regex_query
* e
);
1160 virtual void visit_comparison (comparison
* e
);
1161 virtual void visit_concatenation (concatenation
* e
);
1162 virtual void visit_ternary_expression (ternary_expression
* e
);
1163 virtual void visit_assignment (assignment
* e
);
1164 virtual void visit_symbol (symbol
* e
);
1165 virtual void visit_target_symbol (target_symbol
* e
);
1166 virtual void visit_arrayindex (arrayindex
* e
);
1167 virtual void visit_functioncall (functioncall
* e
);
1168 virtual void visit_print_format (print_format
* e
);
1169 virtual void visit_stat_op (stat_op
* e
);
1170 virtual void visit_hist_op (hist_op
* e
);
1171 virtual void visit_cast_op (cast_op
* e
);
1172 virtual void visit_autocast_op (autocast_op
* e
);
1173 virtual void visit_atvar_op (atvar_op
* e
);
1174 virtual void visit_defined_op (defined_op
* e
);
1175 virtual void visit_entry_op (entry_op
* e
);
1176 virtual void visit_perf_op (perf_op
* e
);
1179 std::stack
<visitable
*> values
;
1182 // A visitor which performs a deep copy of the root node it's applied
1183 // to. NB: It does not copy any of the variable or function
1184 // declarations; those fields are set to NULL, assuming you want to
1185 // re-infer the declarations in a new context (the one you're copying
1188 struct deep_copy_visitor
: public update_visitor
1190 template <typename T
> static T
* deep_copy (T
* e
)
1192 deep_copy_visitor v
;
1193 return v
.require (e
);
1196 virtual void visit_block (block
*s
);
1197 virtual void visit_try_block (try_block
*s
);
1198 virtual void visit_embeddedcode (embeddedcode
*s
);
1199 virtual void visit_null_statement (null_statement
*s
);
1200 virtual void visit_expr_statement (expr_statement
*s
);
1201 virtual void visit_if_statement (if_statement
* s
);
1202 virtual void visit_for_loop (for_loop
* s
);
1203 virtual void visit_foreach_loop (foreach_loop
* s
);
1204 virtual void visit_return_statement (return_statement
* s
);
1205 virtual void visit_delete_statement (delete_statement
* s
);
1206 virtual void visit_next_statement (next_statement
* s
);
1207 virtual void visit_break_statement (break_statement
* s
);
1208 virtual void visit_continue_statement (continue_statement
* s
);
1209 virtual void visit_literal_string (literal_string
* e
);
1210 virtual void visit_literal_number (literal_number
* e
);
1211 virtual void visit_embedded_expr (embedded_expr
* e
);
1212 virtual void visit_binary_expression (binary_expression
* e
);
1213 virtual void visit_unary_expression (unary_expression
* e
);
1214 virtual void visit_pre_crement (pre_crement
* e
);
1215 virtual void visit_post_crement (post_crement
* e
);
1216 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1217 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1218 virtual void visit_array_in (array_in
* e
);
1219 virtual void visit_regex_query (regex_query
* e
);
1220 virtual void visit_comparison (comparison
* e
);
1221 virtual void visit_concatenation (concatenation
* e
);
1222 virtual void visit_ternary_expression (ternary_expression
* e
);
1223 virtual void visit_assignment (assignment
* e
);
1224 virtual void visit_symbol (symbol
* e
);
1225 virtual void visit_target_symbol (target_symbol
* e
);
1226 virtual void visit_arrayindex (arrayindex
* e
);
1227 virtual void visit_functioncall (functioncall
* e
);
1228 virtual void visit_print_format (print_format
* e
);
1229 virtual void visit_stat_op (stat_op
* e
);
1230 virtual void visit_hist_op (hist_op
* e
);
1231 virtual void visit_cast_op (cast_op
* e
);
1232 virtual void visit_autocast_op (autocast_op
* e
);
1233 virtual void visit_atvar_op (atvar_op
* e
);
1234 virtual void visit_defined_op (defined_op
* e
);
1235 virtual void visit_entry_op (entry_op
* e
);
1236 virtual void visit_perf_op (perf_op
* e
);
1239 #endif // STAPTREE_H
1241 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */