2 // Copyright (C) 2005-2015 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
27 #include "stringtable.h"
29 #if defined(HAVE_TR1_MEMORY)
31 using std::tr1::shared_ptr
;
32 #elif defined(HAVE_BOOST_SHARED_PTR_HPP)
33 #include <boost/shared_ptr.hpp>
34 using boost::shared_ptr
;
36 #error "No shared_ptr implementation found; get boost or modern g++"
40 struct token
; // parse.h
41 struct systemtap_session
; // session.h
43 struct semantic_error
: public std::runtime_error
49 // Extra details to explain the error or provide alternatives to the user.
50 // Each one printed after the main error message and tokens aligned on
51 // separate lines. Just push_back anything you want that better explains
52 // the error to the user (not meant for extra verbose developer messages).
53 std::vector
<std::string
> details
;
55 ~semantic_error () throw ()
61 semantic_error (const std::string
& src
, const std::string
& msg
, const token
* t1
=0):
62 runtime_error (msg
), tok1 (t1
), tok2 (0), errsrc (src
), chain (0) {}
64 semantic_error (const std::string
& src
, const std::string
& msg
, const token
* t1
,
65 const token
* t2
, const semantic_error
* chn
=0):
66 runtime_error (msg
), tok1 (t1
), tok2 (t2
), errsrc (src
), chain (0)
72 /* override copy-ctor to deep-copy chain */
73 semantic_error (const semantic_error
& other
):
74 runtime_error(other
), tok1(other
.tok1
), tok2(other
.tok2
),
75 errsrc(other
.errsrc
), details(other
.details
), chain (0)
78 set_chain(*other
.chain
);
81 std::string
errsrc_chain(void) const
83 return errsrc
+ (chain
? "|" + chain
->errsrc_chain() : "");
86 semantic_error
& set_chain(const semantic_error
& new_chain
)
90 chain
= new semantic_error(new_chain
);
94 const semantic_error
* get_chain(void) const
100 const semantic_error
* chain
;
103 // ------------------------------------------------------------------------
105 /* struct statistic_decl moved to session.h */
107 // ------------------------------------------------------------------------
113 pe_string
, // std::string
117 std::ostream
& operator << (std::ostream
& o
, const exp_type
& e
);
121 struct exp_type_details
123 virtual ~exp_type_details () {};
125 // A process-wide unique identifier; probably a pointer.
126 virtual uintptr_t id () const = 0;
127 bool operator==(const exp_type_details
& other
) const
128 { return id () == other
.id (); }
129 bool operator!=(const exp_type_details
& other
) const
130 { return !(*this == other
); }
132 // Expand this autocast_op into a function call
133 virtual bool expandable() const = 0;
134 virtual functioncall
*expand(autocast_op
* e
, bool lvalue
) = 0;
136 typedef shared_ptr
<exp_type_details
> exp_type_ptr
;
141 struct update_visitor
;
145 virtual ~visitable ();
149 struct expression
: public visitable
152 exp_type_ptr type_details
;
155 virtual ~expression ();
156 virtual void print (std::ostream
& o
) const = 0;
157 virtual void visit (visitor
* u
) = 0;
158 virtual bool is_symbol(symbol
*& sym_out
);
161 std::ostream
& operator << (std::ostream
& o
, const expression
& k
);
164 struct literal
: public expression
169 struct literal_string
: public literal
171 interned_string value
;
172 literal_string (interned_string v
);
173 void print (std::ostream
& o
) const;
174 void visit (visitor
* u
);
178 struct literal_number
: public literal
182 literal_number (int64_t v
, bool hex
=false);
183 void print (std::ostream
& o
) const;
184 void visit (visitor
* u
);
188 struct embedded_expr
: public expression
190 interned_string code
;
191 void print (std::ostream
& o
) const;
192 void visit (visitor
* u
);
196 struct binary_expression
: public expression
201 void print (std::ostream
& o
) const;
202 void visit (visitor
* u
);
206 struct unary_expression
: public expression
210 void print (std::ostream
& o
) const;
211 void visit (visitor
* u
);
215 struct pre_crement
: public unary_expression
217 void visit (visitor
* u
);
221 struct post_crement
: public unary_expression
223 void print (std::ostream
& o
) const;
224 void visit (visitor
* u
);
228 struct logical_or_expr
: public binary_expression
230 void visit (visitor
* u
);
234 struct logical_and_expr
: public binary_expression
236 void visit (visitor
* u
);
241 struct array_in
: public expression
244 void print (std::ostream
& o
) const;
245 void visit (visitor
* u
);
248 struct regex_query
: public expression
252 literal_string
* right
;
253 void visit (visitor
* u
);
254 void print (std::ostream
& o
) const;
257 struct comparison
: public binary_expression
259 void visit (visitor
* u
);
263 struct concatenation
: public binary_expression
265 void visit (visitor
* u
);
269 struct ternary_expression
: public expression
272 expression
* truevalue
;
273 expression
* falsevalue
;
274 void print (std::ostream
& o
) const;
275 void visit (visitor
* u
);
279 struct assignment
: public binary_expression
281 void visit (visitor
* u
);
285 struct indexable
: public expression
287 // This is a helper class which, type-wise, acts as a disjoint union
288 // of symbols and histograms. You can ask it whether it's a
289 // histogram or a symbol, and downcast accordingly.
290 virtual bool is_symbol(symbol
*& sym_out
);
291 virtual bool is_hist_op(hist_op
*& hist_out
);
292 virtual ~indexable() {}
295 // Perform a downcast to one out-value and NULL the other, throwing an
296 // exception if neither downcast succeeds. This is (sadly) about the
297 // best we can accomplish in C++.
299 classify_indexable(indexable
* ix
,
301 hist_op
*& hist_out
);
304 struct symbol
: public indexable
306 interned_string name
;
309 void print (std::ostream
& o
) const;
310 void visit (visitor
* u
);
311 // overrides of type 'indexable'
312 bool is_symbol(symbol
*& sym_out
);
316 struct target_symbol
: public expression
321 comp_literal_array_index
,
322 comp_expression_array_index
,
323 comp_pretty_print
, // must be final
330 std::string member
; // comp_struct_member, comp_pretty_print
331 int64_t num_index
; // comp_literal_array_index
332 expression
* expr_index
; // comp_expression_array_index
334 component(const token
* t
, const std::string
& m
, bool pprint
=false):
336 type(pprint
? comp_pretty_print
: comp_struct_member
),
337 member(m
), num_index(0), expr_index(0)
339 component(const token
* t
, int64_t n
):
340 tok(t
), type(comp_literal_array_index
), num_index(n
),
342 component(const token
* t
, expression
* e
):
343 tok(t
), type(comp_expression_array_index
), num_index(0),
345 void print (std::ostream
& o
) const;
348 interned_string name
;
350 std::vector
<component
> components
;
351 semantic_error
* saved_conversion_error
; // hand-made linked list
352 target_symbol(): addressof(false), saved_conversion_error (0) {}
353 virtual std::string
sym_name ();
354 void chain (const semantic_error
& er
);
355 void print (std::ostream
& o
) const;
356 void visit (visitor
* u
);
357 void visit_components (visitor
* u
);
358 void visit_components (update_visitor
* u
);
359 void assert_no_components(const std::string
& tapset
, bool pretty_ok
=false);
360 size_t pretty_print_depth () const;
361 bool check_pretty_print (bool lvalue
=false) const;
364 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
);
367 struct cast_op
: public target_symbol
370 interned_string type_name
, module
;
371 void print (std::ostream
& o
) const;
372 void visit (visitor
* u
);
375 // An autocast is like an implicit @cast on any expression, like
376 // (expr)->foo->var[baz], and the type is gleaned from the expr.
377 struct autocast_op
: public target_symbol
380 void print (std::ostream
& o
) const;
381 void visit (visitor
* u
);
384 struct atvar_op
: public target_symbol
386 interned_string target_name
, cu_name
, module
;
387 virtual std::string
sym_name ();
388 void print (std::ostream
& o
) const;
389 void visit (visitor
* u
);
392 struct defined_op
: public expression
395 void print (std::ostream
& o
) const;
396 void visit (visitor
* u
);
400 struct entry_op
: public expression
403 void print (std::ostream
& o
) const;
404 void visit (visitor
* u
);
408 struct perf_op
: public expression
410 literal_string
*operand
;
411 void print (std::ostream
& o
) const;
412 void visit (visitor
* u
);
416 struct arrayindex
: public expression
418 std::vector
<expression
*> indexes
;
421 void print (std::ostream
& o
) const;
422 void visit (visitor
* u
);
427 struct functioncall
: public expression
429 interned_string function
;
430 std::vector
<expression
*> args
;
431 functiondecl
*referent
;
433 void print (std::ostream
& o
) const;
434 void visit (visitor
* u
);
435 interned_string var_assigned_to_retval
;
439 struct print_format
: public expression
441 bool print_to_stream
;
442 bool print_with_format
;
443 bool print_with_delim
;
444 bool print_with_newline
;
447 // XXX match runtime/vsprintf.c's print_flag
448 // ... for use with number() & number_size()
451 fmt_flag_zeropad
= 1,
456 fmt_flag_special
= 32,
487 struct format_component
493 width_type widthtype
: 8;
494 precision_type prectype
: 8;
495 conversion_type type
: 8;
496 interned_string literal_string
;
497 bool is_empty() const
500 && widthtype
== width_unspecified
501 && prectype
== prec_unspecified
502 && type
== conv_unspecified
503 && literal_string
.empty();
510 widthtype
= width_unspecified
;
512 prectype
= prec_unspecified
;
513 type
= conv_unspecified
;
514 literal_string
.clear();
516 format_component() { clear(); }
517 inline void set_flag(format_flag f
) { flags
|= f
; }
518 inline bool test_flag(format_flag f
) const { return flags
& f
; }
521 std::string raw_components
;
522 std::vector
<format_component
> components
;
523 interned_string delimiter
;
524 std::vector
<expression
*> args
;
527 static std::string
components_to_string(std::vector
<format_component
> const & components
);
528 static std::vector
<format_component
> string_to_components(std::string
const & str
);
529 static print_format
* create(const token
*t
, const char *n
= NULL
);
531 void print (std::ostream
& o
) const;
532 void visit (visitor
* u
);
535 interned_string print_format_type
;
536 print_format(bool stream
, bool format
, bool delim
, bool newline
, bool _char
, interned_string type
):
537 print_to_stream(stream
), print_with_format(format
),
538 print_with_delim(delim
), print_with_newline(newline
),
539 print_char(_char
), hist(NULL
), print_format_type(type
)
544 enum stat_component_type
554 struct stat_op
: public expression
556 stat_component_type ctype
;
558 void print (std::ostream
& o
) const;
559 void visit (visitor
* u
);
568 struct hist_op
: public indexable
570 histogram_type htype
;
572 std::vector
<int64_t> params
;
573 void print (std::ostream
& o
) const;
574 void visit (visitor
* u
);
575 // overrides of type 'indexable'
576 bool is_hist_op(hist_op
*& hist_out
);
579 // ------------------------------------------------------------------------
582 struct symboldecl
// unique object per (possibly implicit)
583 // symbol declaration
586 const token
* systemtap_v_conditional
; //checking systemtap compatibility
587 interned_string name
;
589 exp_type_ptr type_details
;
591 virtual ~symboldecl ();
592 virtual void print (std::ostream
&o
) const = 0;
593 virtual void printsig (std::ostream
&o
) const = 0;
597 std::ostream
& operator << (std::ostream
& o
, const symboldecl
& k
);
600 struct vardecl
: public symboldecl
602 void print (std::ostream
& o
) const;
603 void printsig (std::ostream
& o
) const;
605 void set_arity (int arity
, const token
* t
);
606 bool compatible_arity (int a
);
607 const token
* arity_tok
; // site where arity was first resolved
608 int arity
; // -1: unknown; 0: scalar; >0: array
609 int maxsize
; // upperbound on size for arrays
610 std::vector
<exp_type
> index_types
; // for arrays only
611 literal
*init
; // for global scalars only
612 bool synthetic
; // for probe locals only, don't init on entry
614 bool char_ptr_arg
; // set in ::emit_common_header(), only used if a formal_arg
618 struct vardecl_builtin
: public vardecl
623 struct functiondecl
: public symboldecl
625 std::vector
<vardecl
*> formal_args
;
626 std::vector
<vardecl
*> locals
;
627 std::vector
<vardecl
*> unused_locals
;
630 bool mangle_oldstyle
;
632 void print (std::ostream
& o
) const;
633 void printsig (std::ostream
& o
) const;
634 void printsigtags (std::ostream
& o
, bool all_tags
) const;
635 void join (systemtap_session
& s
); // for synthetic functions only
639 // ------------------------------------------------------------------------
642 struct statement
: public visitable
644 virtual void print (std::ostream
& o
) const = 0;
645 virtual void visit (visitor
* u
) = 0;
648 statement (const token
* tok
);
649 virtual ~statement ();
652 std::ostream
& operator << (std::ostream
& o
, const statement
& k
);
655 struct embeddedcode
: public statement
657 interned_string code
;
658 void print (std::ostream
& o
) const;
659 void visit (visitor
* u
);
663 struct block
: public statement
665 std::vector
<statement
*> statements
;
666 void print (std::ostream
& o
) const;
667 void visit (visitor
* u
);
669 block (statement
* car
, statement
* cdr
);
674 struct try_block
: public statement
676 statement
* try_block
; // may be 0
677 statement
* catch_block
; // may be 0
678 symbol
* catch_error_var
; // may be 0
679 void print (std::ostream
& o
) const;
680 void visit (visitor
* u
);
684 struct expr_statement
;
685 struct for_loop
: public statement
687 expr_statement
* init
; // may be 0
689 expr_statement
* incr
; // may be 0
691 void print (std::ostream
& o
) const;
692 void visit (visitor
* u
);
696 struct foreach_loop
: public statement
698 // this part is a specialization of arrayindex
699 std::vector
<symbol
*> indexes
;
700 std::vector
<expression
*> array_slice
; // optional array slice to iterate over
702 int sort_direction
; // -1: decreasing, 0: none, 1: increasing
703 unsigned sort_column
; // 0: value, 1..N: index
704 enum stat_component_type sort_aggr
; // for aggregate arrays, which aggregate to sort on
705 symbol
* value
; // optional iteration value
706 expression
* limit
; // optional iteration limit
709 void print (std::ostream
& o
) const;
710 void visit (visitor
* u
);
714 struct null_statement
: public statement
716 void print (std::ostream
& o
) const;
717 void visit (visitor
* u
);
718 null_statement (const token
* tok
);
722 struct expr_statement
: public statement
724 expression
* value
; // executed for side-effects
725 void print (std::ostream
& o
) const;
726 void visit (visitor
* u
);
730 struct if_statement
: public statement
732 expression
* condition
;
733 statement
* thenblock
;
734 statement
* elseblock
; // may be 0
735 void print (std::ostream
& o
) const;
736 void visit (visitor
* u
);
740 struct return_statement
: public expr_statement
742 void print (std::ostream
& o
) const;
743 void visit (visitor
* u
);
747 struct delete_statement
: public expr_statement
749 void print (std::ostream
& o
) const;
750 void visit (visitor
* u
);
754 struct break_statement
: public statement
756 void print (std::ostream
& o
) const;
757 void visit (visitor
* u
);
761 struct continue_statement
: public statement
763 void print (std::ostream
& o
) const;
764 void visit (visitor
* u
);
768 struct next_statement
: public statement
770 void print (std::ostream
& o
) const;
771 void visit (visitor
* u
);
776 struct derived_probe
;
782 std::vector
<probe
*> probes
;
783 std::vector
<probe_alias
*> aliases
;
784 std::vector
<functiondecl
*> functions
;
785 std::vector
<vardecl
*> globals
;
786 std::vector
<embeddedcode
*> embeds
;
787 interned_string file_contents
;
789 bool synthetic
; // via parse_synthetic_*
791 privileged (false), synthetic (false) {}
792 void print (std::ostream
& o
) const;
798 struct component
// XXX: sort of a restricted functioncall
800 interned_string functor
;
801 literal
* arg
; // optional
804 const token
* tok
; // points to component's functor
805 component(interned_string f
, literal
*a
=NULL
, bool from_glob
=false);
807 std::vector
<component
*> components
;
810 bool well_formed
; // used in derived_probe::script_location()
811 expression
* condition
;
812 void print (std::ostream
& o
, bool print_extras
=true) const;
814 probe_point(const probe_point
& pp
);
815 probe_point(std::vector
<component
*> const & comps
);
816 std::string
str(bool print_extras
=true) const;
817 bool from_globby_comp(const std::string
& comp
);
820 std::ostream
& operator << (std::ostream
& o
, const probe_point
& k
);
825 static unsigned last_probeidx
;
827 std::vector
<probe_point
*> locations
;
831 const token
* systemtap_v_conditional
; //checking systemtap compatibility
832 std::vector
<vardecl
*> locals
;
833 std::vector
<vardecl
*> unused_locals
;
838 probe (probe
* p
, probe_point
*l
);
839 void print (std::ostream
& o
) const;
840 std::string
name () const;
841 virtual void printsig (std::ostream
&o
) const;
842 virtual void collect_derivation_chain (std::vector
<probe
*> &probes_list
) const;
843 virtual void collect_derivation_pp_chain (std::vector
<probe_point
*> &) const;
844 virtual const probe_alias
*get_alias () const { return 0; }
845 virtual probe_point
*get_alias_loc () const { return 0; }
850 probe (const probe
&);
851 probe
& operator = (const probe
&);
854 struct probe_alias
: public probe
856 probe_alias(std::vector
<probe_point
*> const & aliases
);
857 std::vector
<probe_point
*> alias_names
;
858 virtual void printsig (std::ostream
&o
) const;
863 // A derived visitor instance is used to visit the entire
864 // statement/expression tree.
867 // Machinery for differentiating lvalue visits from non-lvalue.
868 std::vector
<expression
*> active_lvalues
;
869 bool is_active_lvalue(expression
*e
);
870 void push_active_lvalue(expression
*e
);
871 void pop_active_lvalue();
873 virtual ~visitor () {}
874 virtual void visit_block (block
*s
) = 0;
875 virtual void visit_try_block (try_block
*s
) = 0;
876 virtual void visit_embeddedcode (embeddedcode
*s
) = 0;
877 virtual void visit_null_statement (null_statement
*s
) = 0;
878 virtual void visit_expr_statement (expr_statement
*s
) = 0;
879 virtual void visit_if_statement (if_statement
* s
) = 0;
880 virtual void visit_for_loop (for_loop
* s
) = 0;
881 virtual void visit_foreach_loop (foreach_loop
* s
) = 0;
882 virtual void visit_return_statement (return_statement
* s
) = 0;
883 virtual void visit_delete_statement (delete_statement
* s
) = 0;
884 virtual void visit_next_statement (next_statement
* s
) = 0;
885 virtual void visit_break_statement (break_statement
* s
) = 0;
886 virtual void visit_continue_statement (continue_statement
* s
) = 0;
887 virtual void visit_literal_string (literal_string
* e
) = 0;
888 virtual void visit_literal_number (literal_number
* e
) = 0;
889 virtual void visit_embedded_expr (embedded_expr
* e
) = 0;
890 virtual void visit_binary_expression (binary_expression
* e
) = 0;
891 virtual void visit_unary_expression (unary_expression
* e
) = 0;
892 virtual void visit_pre_crement (pre_crement
* e
) = 0;
893 virtual void visit_post_crement (post_crement
* e
) = 0;
894 virtual void visit_logical_or_expr (logical_or_expr
* e
) = 0;
895 virtual void visit_logical_and_expr (logical_and_expr
* e
) = 0;
896 virtual void visit_array_in (array_in
* e
) = 0;
897 virtual void visit_regex_query (regex_query
* e
) = 0;
898 virtual void visit_comparison (comparison
* e
) = 0;
899 virtual void visit_concatenation (concatenation
* e
) = 0;
900 virtual void visit_ternary_expression (ternary_expression
* e
) = 0;
901 virtual void visit_assignment (assignment
* e
) = 0;
902 virtual void visit_symbol (symbol
* e
) = 0;
903 virtual void visit_target_symbol (target_symbol
* e
) = 0;
904 virtual void visit_arrayindex (arrayindex
* e
) = 0;
905 virtual void visit_functioncall (functioncall
* e
) = 0;
906 virtual void visit_print_format (print_format
* e
) = 0;
907 virtual void visit_stat_op (stat_op
* e
) = 0;
908 virtual void visit_hist_op (hist_op
* e
) = 0;
909 virtual void visit_cast_op (cast_op
* e
) = 0;
910 virtual void visit_autocast_op (autocast_op
* e
) = 0;
911 virtual void visit_atvar_op (atvar_op
* e
) = 0;
912 virtual void visit_defined_op (defined_op
* e
) = 0;
913 virtual void visit_entry_op (entry_op
* e
) = 0;
914 virtual void visit_perf_op (perf_op
* e
) = 0;
918 // A simple kind of visitor, which travels down to the leaves of the
919 // statement/expression tree, up to but excluding following vardecls
920 // and functioncalls.
921 struct traversing_visitor
: public visitor
923 void visit_block (block
*s
);
924 void visit_try_block (try_block
*s
);
925 void visit_embeddedcode (embeddedcode
*s
);
926 void visit_null_statement (null_statement
*s
);
927 void visit_expr_statement (expr_statement
*s
);
928 void visit_if_statement (if_statement
* s
);
929 void visit_for_loop (for_loop
* s
);
930 void visit_foreach_loop (foreach_loop
* s
);
931 void visit_return_statement (return_statement
* s
);
932 void visit_delete_statement (delete_statement
* s
);
933 void visit_next_statement (next_statement
* s
);
934 void visit_break_statement (break_statement
* s
);
935 void visit_continue_statement (continue_statement
* s
);
936 void visit_literal_string (literal_string
* e
);
937 void visit_literal_number (literal_number
* e
);
938 void visit_embedded_expr (embedded_expr
* e
);
939 void visit_binary_expression (binary_expression
* e
);
940 void visit_unary_expression (unary_expression
* e
);
941 void visit_pre_crement (pre_crement
* e
);
942 void visit_post_crement (post_crement
* e
);
943 void visit_logical_or_expr (logical_or_expr
* e
);
944 void visit_logical_and_expr (logical_and_expr
* e
);
945 void visit_array_in (array_in
* e
);
946 void visit_regex_query (regex_query
* e
);
947 void visit_comparison (comparison
* e
);
948 void visit_concatenation (concatenation
* e
);
949 void visit_ternary_expression (ternary_expression
* e
);
950 void visit_assignment (assignment
* e
);
951 void visit_symbol (symbol
* e
);
952 void visit_target_symbol (target_symbol
* e
);
953 void visit_arrayindex (arrayindex
* e
);
954 void visit_functioncall (functioncall
* e
);
955 void visit_print_format (print_format
* e
);
956 void visit_stat_op (stat_op
* e
);
957 void visit_hist_op (hist_op
* e
);
958 void visit_cast_op (cast_op
* e
);
959 void visit_autocast_op (autocast_op
* e
);
960 void visit_atvar_op (atvar_op
* e
);
961 void visit_defined_op (defined_op
* e
);
962 void visit_entry_op (entry_op
* e
);
963 void visit_perf_op (perf_op
* e
);
967 // A visitor that calls a generic visit_expression on every expression.
968 struct expression_visitor
: public traversing_visitor
970 virtual void visit_expression(expression
*e
) = 0;
972 void visit_literal_string (literal_string
* e
);
973 void visit_literal_number (literal_number
* e
);
974 void visit_embedded_expr (embedded_expr
* e
);
975 void visit_binary_expression (binary_expression
* e
);
976 void visit_unary_expression (unary_expression
* e
);
977 void visit_pre_crement (pre_crement
* e
);
978 void visit_post_crement (post_crement
* e
);
979 void visit_logical_or_expr (logical_or_expr
* e
);
980 void visit_logical_and_expr (logical_and_expr
* e
);
981 void visit_array_in (array_in
* e
);
982 void visit_regex_query (regex_query
* e
);
983 void visit_comparison (comparison
* e
);
984 void visit_concatenation (concatenation
* e
);
985 void visit_ternary_expression (ternary_expression
* e
);
986 void visit_assignment (assignment
* e
);
987 void visit_symbol (symbol
* e
);
988 void visit_target_symbol (target_symbol
* e
);
989 void visit_arrayindex (arrayindex
* e
);
990 void visit_functioncall (functioncall
* e
);
991 void visit_print_format (print_format
* e
);
992 void visit_stat_op (stat_op
* e
);
993 void visit_hist_op (hist_op
* e
);
994 void visit_cast_op (cast_op
* e
);
995 void visit_autocast_op (autocast_op
* e
);
996 void visit_atvar_op (atvar_op
* e
);
997 void visit_defined_op (defined_op
* e
);
998 void visit_entry_op (entry_op
* e
);
999 void visit_perf_op (perf_op
* e
);
1003 // A kind of traversing visitor, which also follows function calls.
1004 // It uses an internal set object to prevent infinite recursion.
1005 struct functioncall_traversing_visitor
: public traversing_visitor
1007 std::set
<functiondecl
*> seen
;
1008 std::set
<functiondecl
*> nested
;
1009 functiondecl
* current_function
;
1010 functioncall_traversing_visitor(): current_function(0) {}
1011 void visit_functioncall (functioncall
* e
);
1012 void enter_functioncall (functioncall
* e
);
1013 virtual void note_recursive_functioncall (functioncall
* e
);
1017 // A kind of traversing visitor, which also follows function calls,
1018 // and stores the vardecl* referent of each variable read and/or
1019 // written and other such sundry side-effect data. It's used by
1020 // the elaboration-time optimizer pass.
1021 struct varuse_collecting_visitor
: public functioncall_traversing_visitor
1023 systemtap_session
& session
;
1024 std::set
<vardecl
*> read
;
1025 std::set
<vardecl
*> written
;
1026 std::set
<vardecl
*> used
;
1028 bool current_lvalue_read
;
1029 expression
* current_lvalue
;
1030 expression
* current_lrvalue
;
1031 varuse_collecting_visitor(systemtap_session
& s
):
1033 embedded_seen (false),
1034 current_lvalue_read (false),
1036 current_lrvalue(0) {}
1037 void visit_if_statement (if_statement
* s
);
1038 void visit_for_loop (for_loop
* s
);
1039 void visit_embeddedcode (embeddedcode
*s
);
1040 void visit_embedded_expr (embedded_expr
*e
);
1041 void visit_try_block (try_block
*s
);
1042 void visit_functioncall (functioncall
* e
);
1043 void visit_return_statement (return_statement
*s
);
1044 void visit_delete_statement (delete_statement
*s
);
1045 void visit_print_format (print_format
*e
);
1046 void visit_assignment (assignment
*e
);
1047 void visit_ternary_expression (ternary_expression
* e
);
1048 void visit_arrayindex (arrayindex
*e
);
1049 void visit_target_symbol (target_symbol
*e
);
1050 void visit_symbol (symbol
*e
);
1051 void visit_pre_crement (pre_crement
*e
);
1052 void visit_post_crement (post_crement
*e
);
1053 void visit_foreach_loop (foreach_loop
*s
);
1054 void visit_cast_op (cast_op
* e
);
1055 void visit_autocast_op (autocast_op
* e
);
1056 void visit_atvar_op (atvar_op
*e
);
1057 void visit_defined_op (defined_op
* e
);
1058 void visit_entry_op (entry_op
* e
);
1059 void visit_perf_op (perf_op
* e
);
1060 bool side_effect_free ();
1061 bool side_effect_free_wrt (const std::set
<vardecl
*>& vars
);
1066 // A kind of visitor that throws an semantic_error exception
1067 // whenever a non-overridden method is called.
1068 struct throwing_visitor
: public visitor
1071 throwing_visitor (const std::string
& m
);
1072 throwing_visitor ();
1074 virtual void throwone (const token
* t
);
1076 void visit_block (block
*s
);
1077 void visit_try_block (try_block
*s
);
1078 void visit_embeddedcode (embeddedcode
*s
);
1079 void visit_null_statement (null_statement
*s
);
1080 void visit_expr_statement (expr_statement
*s
);
1081 void visit_if_statement (if_statement
* s
);
1082 void visit_for_loop (for_loop
* s
);
1083 void visit_foreach_loop (foreach_loop
* s
);
1084 void visit_return_statement (return_statement
* s
);
1085 void visit_delete_statement (delete_statement
* s
);
1086 void visit_next_statement (next_statement
* s
);
1087 void visit_break_statement (break_statement
* s
);
1088 void visit_continue_statement (continue_statement
* s
);
1089 void visit_literal_string (literal_string
* e
);
1090 void visit_literal_number (literal_number
* e
);
1091 void visit_embedded_expr (embedded_expr
* e
);
1092 void visit_binary_expression (binary_expression
* e
);
1093 void visit_unary_expression (unary_expression
* e
);
1094 void visit_pre_crement (pre_crement
* e
);
1095 void visit_post_crement (post_crement
* e
);
1096 void visit_logical_or_expr (logical_or_expr
* e
);
1097 void visit_logical_and_expr (logical_and_expr
* e
);
1098 void visit_array_in (array_in
* e
);
1099 void visit_regex_query (regex_query
* e
);
1100 void visit_comparison (comparison
* e
);
1101 void visit_concatenation (concatenation
* e
);
1102 void visit_ternary_expression (ternary_expression
* e
);
1103 void visit_assignment (assignment
* e
);
1104 void visit_symbol (symbol
* e
);
1105 void visit_target_symbol (target_symbol
* e
);
1106 void visit_arrayindex (arrayindex
* e
);
1107 void visit_functioncall (functioncall
* e
);
1108 void visit_print_format (print_format
* e
);
1109 void visit_stat_op (stat_op
* e
);
1110 void visit_hist_op (hist_op
* e
);
1111 void visit_cast_op (cast_op
* e
);
1112 void visit_autocast_op (autocast_op
* e
);
1113 void visit_atvar_op (atvar_op
* e
);
1114 void visit_defined_op (defined_op
* e
);
1115 void visit_entry_op (entry_op
* e
);
1116 void visit_perf_op (perf_op
* e
);
1119 // A visitor similar to a traversing_visitor, but with the ability to rewrite
1120 // parts of the tree through require/provide.
1122 struct update_visitor
: public visitor
1124 template <typename T
> T
* require (T
* src
, bool clearok
=false)
1131 throw std::runtime_error(_("update_visitor wasn't provided a value"));
1132 visitable
*v
= values
.top();
1134 if (v
== NULL
&& !clearok
)
1135 throw std::runtime_error(_("update_visitor was provided a NULL value"));
1136 dst
= dynamic_cast<T
*>(v
);
1137 if (v
!= NULL
&& dst
== NULL
)
1138 throw std::runtime_error(_F("update_visitor can't set type \"%s\" with a \"%s\"",
1139 typeid(T
).name(), typeid(*v
).name()));
1144 template <typename T
> void provide (T
* src
)
1149 template <typename T
> void replace (T
*& src
, bool clearok
=false)
1151 src
= require(src
, clearok
);
1154 virtual ~update_visitor() { assert(values
.empty()); }
1156 virtual void visit_block (block
*s
);
1157 virtual void visit_try_block (try_block
*s
);
1158 virtual void visit_embeddedcode (embeddedcode
*s
);
1159 virtual void visit_null_statement (null_statement
*s
);
1160 virtual void visit_expr_statement (expr_statement
*s
);
1161 virtual void visit_if_statement (if_statement
* s
);
1162 virtual void visit_for_loop (for_loop
* s
);
1163 virtual void visit_foreach_loop (foreach_loop
* s
);
1164 virtual void visit_return_statement (return_statement
* s
);
1165 virtual void visit_delete_statement (delete_statement
* s
);
1166 virtual void visit_next_statement (next_statement
* s
);
1167 virtual void visit_break_statement (break_statement
* s
);
1168 virtual void visit_continue_statement (continue_statement
* s
);
1169 virtual void visit_literal_string (literal_string
* e
);
1170 virtual void visit_literal_number (literal_number
* e
);
1171 virtual void visit_embedded_expr (embedded_expr
* e
);
1172 virtual void visit_binary_expression (binary_expression
* e
);
1173 virtual void visit_unary_expression (unary_expression
* e
);
1174 virtual void visit_pre_crement (pre_crement
* e
);
1175 virtual void visit_post_crement (post_crement
* e
);
1176 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1177 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1178 virtual void visit_array_in (array_in
* e
);
1179 virtual void visit_regex_query (regex_query
* e
);
1180 virtual void visit_comparison (comparison
* e
);
1181 virtual void visit_concatenation (concatenation
* e
);
1182 virtual void visit_ternary_expression (ternary_expression
* e
);
1183 virtual void visit_assignment (assignment
* e
);
1184 virtual void visit_symbol (symbol
* e
);
1185 virtual void visit_target_symbol (target_symbol
* e
);
1186 virtual void visit_arrayindex (arrayindex
* e
);
1187 virtual void visit_functioncall (functioncall
* e
);
1188 virtual void visit_print_format (print_format
* e
);
1189 virtual void visit_stat_op (stat_op
* e
);
1190 virtual void visit_hist_op (hist_op
* e
);
1191 virtual void visit_cast_op (cast_op
* e
);
1192 virtual void visit_autocast_op (autocast_op
* e
);
1193 virtual void visit_atvar_op (atvar_op
* e
);
1194 virtual void visit_defined_op (defined_op
* e
);
1195 virtual void visit_entry_op (entry_op
* e
);
1196 virtual void visit_perf_op (perf_op
* e
);
1199 std::stack
<visitable
*> values
;
1202 // A visitor which performs a deep copy of the root node it's applied
1203 // to. NB: It does not copy any of the variable or function
1204 // declarations; those fields are set to NULL, assuming you want to
1205 // re-infer the declarations in a new context (the one you're copying
1208 struct deep_copy_visitor
: public update_visitor
1210 template <typename T
> static T
* deep_copy (T
* e
)
1212 deep_copy_visitor v
;
1213 return v
.require (e
);
1216 virtual void visit_block (block
*s
);
1217 virtual void visit_try_block (try_block
*s
);
1218 virtual void visit_embeddedcode (embeddedcode
*s
);
1219 virtual void visit_null_statement (null_statement
*s
);
1220 virtual void visit_expr_statement (expr_statement
*s
);
1221 virtual void visit_if_statement (if_statement
* s
);
1222 virtual void visit_for_loop (for_loop
* s
);
1223 virtual void visit_foreach_loop (foreach_loop
* s
);
1224 virtual void visit_return_statement (return_statement
* s
);
1225 virtual void visit_delete_statement (delete_statement
* s
);
1226 virtual void visit_next_statement (next_statement
* s
);
1227 virtual void visit_break_statement (break_statement
* s
);
1228 virtual void visit_continue_statement (continue_statement
* s
);
1229 virtual void visit_literal_string (literal_string
* e
);
1230 virtual void visit_literal_number (literal_number
* e
);
1231 virtual void visit_embedded_expr (embedded_expr
* e
);
1232 virtual void visit_binary_expression (binary_expression
* e
);
1233 virtual void visit_unary_expression (unary_expression
* e
);
1234 virtual void visit_pre_crement (pre_crement
* e
);
1235 virtual void visit_post_crement (post_crement
* e
);
1236 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1237 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1238 virtual void visit_array_in (array_in
* e
);
1239 virtual void visit_regex_query (regex_query
* e
);
1240 virtual void visit_comparison (comparison
* e
);
1241 virtual void visit_concatenation (concatenation
* e
);
1242 virtual void visit_ternary_expression (ternary_expression
* e
);
1243 virtual void visit_assignment (assignment
* e
);
1244 virtual void visit_symbol (symbol
* e
);
1245 virtual void visit_target_symbol (target_symbol
* e
);
1246 virtual void visit_arrayindex (arrayindex
* e
);
1247 virtual void visit_functioncall (functioncall
* e
);
1248 virtual void visit_print_format (print_format
* e
);
1249 virtual void visit_stat_op (stat_op
* e
);
1250 virtual void visit_hist_op (hist_op
* e
);
1251 virtual void visit_cast_op (cast_op
* e
);
1252 virtual void visit_autocast_op (autocast_op
* e
);
1253 virtual void visit_atvar_op (atvar_op
* e
);
1254 virtual void visit_defined_op (defined_op
* e
);
1255 virtual void visit_entry_op (entry_op
* e
);
1256 virtual void visit_perf_op (perf_op
* e
);
1259 struct embedded_tags_visitor
: public traversing_visitor
1261 std::set
<std::string
> available_tags
;
1262 std::set
<std::string
> tags
; // set of the tags that appear in the code
1263 embedded_tags_visitor(bool all_tags
);
1264 bool tagged_p (const std::string
&tag
);
1265 void find_tags_in_code (const std::string
& s
);
1266 void visit_embeddedcode (embeddedcode
*s
);
1267 void visit_embedded_expr (embedded_expr
*e
);
1270 #endif // STAPTREE_H
1272 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */