2 // Copyright (C) 2005-2013 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 struct token
; // parse.h
29 struct systemtap_session
; // session.h
31 struct semantic_error
: public std::runtime_error
35 const semantic_error
*chain
;
36 const std::string errsrc
;
38 ~semantic_error () throw () {}
40 semantic_error (const std::string
& src
, const std::string
& msg
, const token
* t1
=0):
41 runtime_error (msg
), tok1 (t1
), tok2 (0), chain (0), errsrc(src
) {}
43 semantic_error (const std::string
& src
, const std::string
& msg
, const token
* t1
,
45 runtime_error (msg
), tok1 (t1
), tok2 (t2
), chain (0), errsrc(src
) {}
47 std::string
errsrc_chain(void) const
49 return errsrc
+ (chain
? "|" + chain
->errsrc_chain() : "");
53 // ------------------------------------------------------------------------
55 /* struct statistic_decl moved to session.h */
57 // ------------------------------------------------------------------------
63 pe_string
, // std::string
67 std::ostream
& operator << (std::ostream
& o
, const exp_type
& e
);
71 struct update_visitor
;
75 virtual ~visitable ();
78 struct expression
: public visitable
83 virtual ~expression ();
84 virtual void print (std::ostream
& o
) const = 0;
85 virtual void visit (visitor
* u
) = 0;
88 std::ostream
& operator << (std::ostream
& o
, const expression
& k
);
91 struct literal
: public expression
96 struct literal_string
: public literal
99 literal_string (const std::string
& v
);
100 void print (std::ostream
& o
) const;
101 void visit (visitor
* u
);
105 struct literal_number
: public literal
109 literal_number (int64_t v
, bool hex
=false);
110 void print (std::ostream
& o
) const;
111 void visit (visitor
* u
);
115 struct embedded_expr
: public expression
118 void print (std::ostream
& o
) const;
119 void visit (visitor
* u
);
123 struct binary_expression
: public expression
128 void print (std::ostream
& o
) const;
129 void visit (visitor
* u
);
133 struct unary_expression
: public expression
137 void print (std::ostream
& o
) const;
138 void visit (visitor
* u
);
142 struct pre_crement
: public unary_expression
144 void visit (visitor
* u
);
148 struct post_crement
: public unary_expression
150 void print (std::ostream
& o
) const;
151 void visit (visitor
* u
);
155 struct logical_or_expr
: public binary_expression
157 void visit (visitor
* u
);
161 struct logical_and_expr
: public binary_expression
163 void visit (visitor
* u
);
168 struct array_in
: public expression
171 void print (std::ostream
& o
) const;
172 void visit (visitor
* u
);
175 struct regex_query
: public expression
179 literal_string
* right
;
180 void visit (visitor
* u
);
181 void print (std::ostream
& o
) const;
184 struct comparison
: public binary_expression
186 void visit (visitor
* u
);
190 struct concatenation
: public binary_expression
192 void visit (visitor
* u
);
196 struct ternary_expression
: public expression
199 expression
* truevalue
;
200 expression
* falsevalue
;
201 void print (std::ostream
& o
) const;
202 void visit (visitor
* u
);
206 struct assignment
: public binary_expression
208 void visit (visitor
* u
);
213 struct indexable
: public expression
215 // This is a helper class which, type-wise, acts as a disjoint union
216 // of symbols and histograms. You can ask it whether it's a
217 // histogram or a symbol, and downcast accordingly.
218 virtual bool is_symbol(symbol
*& sym_out
);
219 virtual bool is_hist_op(hist_op
*& hist_out
);
220 virtual ~indexable() {}
223 // Perform a downcast to one out-value and NULL the other, throwing an
224 // exception if neither downcast succeeds. This is (sadly) about the
225 // best we can accomplish in C++.
227 classify_indexable(indexable
* ix
,
229 hist_op
*& hist_out
);
232 struct symbol
: public indexable
237 void print (std::ostream
& o
) const;
238 void visit (visitor
* u
);
239 // overrides of type 'indexable'
240 bool is_symbol(symbol
*& sym_out
);
244 struct target_symbol
: public symbol
249 comp_literal_array_index
,
250 comp_expression_array_index
,
251 comp_pretty_print
, // must be final
258 std::string member
; // comp_struct_member, comp_pretty_print
259 int64_t num_index
; // comp_literal_array_index
260 expression
* expr_index
; // comp_expression_array_index
262 component(const token
* t
, const std::string
& m
, bool pprint
=false):
264 type(pprint
? comp_pretty_print
: comp_struct_member
),
265 member(m
), num_index(0), expr_index(0)
267 component(const token
* t
, int64_t n
):
268 tok(t
), type(comp_literal_array_index
), num_index(n
),
270 component(const token
* t
, expression
* e
):
271 tok(t
), type(comp_expression_array_index
), num_index(0),
273 void print (std::ostream
& o
) const;
277 std::vector
<component
> components
;
278 semantic_error
* saved_conversion_error
; // hand-made linked list
279 target_symbol(): addressof(false), saved_conversion_error (0) {}
280 virtual std::string
sym_name ();
281 void chain (const semantic_error
& er
);
282 void print (std::ostream
& o
) const;
283 void visit (visitor
* u
);
284 void visit_components (visitor
* u
);
285 void visit_components (update_visitor
* u
);
286 void assert_no_components(const std::string
& tapset
, bool pretty_ok
=false);
289 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
);
292 struct cast_op
: public target_symbol
295 std::string type_name
, module
;
296 void print (std::ostream
& o
) const;
297 void visit (visitor
* u
);
300 struct atvar_op
: public target_symbol
302 std::string target_name
, cu_name
, module
;
303 virtual std::string
sym_name ();
304 void print (std::ostream
& o
) const;
305 void visit (visitor
* u
);
308 struct defined_op
: public expression
310 target_symbol
*operand
;
311 void print (std::ostream
& o
) const;
312 void visit (visitor
* u
);
316 struct entry_op
: public expression
319 void print (std::ostream
& o
) const;
320 void visit (visitor
* u
);
324 struct perf_op
: public expression
326 literal_string
*operand
;
327 void print (std::ostream
& o
) const;
328 void visit (visitor
* u
);
332 struct arrayindex
: public expression
334 std::vector
<expression
*> indexes
;
337 void print (std::ostream
& o
) const;
338 void visit (visitor
* u
);
343 struct functioncall
: public expression
345 std::string function
;
346 std::vector
<expression
*> args
;
347 functiondecl
*referent
;
349 void print (std::ostream
& o
) const;
350 void visit (visitor
* u
);
354 struct print_format
: public expression
356 bool print_to_stream
;
357 bool print_with_format
;
358 bool print_with_delim
;
359 bool print_with_newline
;
362 // XXX match runtime/vsprintf.c's print_flag
363 // ... for use with number() & number_size()
366 fmt_flag_zeropad
= 1,
371 fmt_flag_special
= 32,
402 struct format_component
408 width_type widthtype
;
409 precision_type prectype
;
410 conversion_type type
;
411 std::string literal_string
;
412 bool is_empty() const
415 && widthtype
== width_unspecified
416 && prectype
== prec_unspecified
417 && type
== conv_unspecified
418 && literal_string
.empty();
423 widthtype
= width_unspecified
;
424 prectype
= prec_unspecified
;
425 type
= conv_unspecified
;
426 literal_string
.clear();
428 inline void set_flag(format_flag f
) { flags
|= f
; }
429 inline bool test_flag(format_flag f
) const { return flags
& f
; }
432 std::string raw_components
;
433 std::vector
<format_component
> components
;
434 format_component delimiter
;
435 std::vector
<expression
*> args
;
438 static std::string
components_to_string(std::vector
<format_component
> const & components
);
439 static std::vector
<format_component
> string_to_components(std::string
const & str
);
440 static print_format
* create(const token
*t
);
442 void print (std::ostream
& o
) const;
443 void visit (visitor
* u
);
446 print_format(bool stream
, bool format
, bool delim
, bool newline
, bool _char
):
447 print_to_stream(stream
), print_with_format(format
),
448 print_with_delim(delim
), print_with_newline(newline
),
449 print_char(_char
), hist(NULL
)
454 enum stat_component_type
464 struct stat_op
: public expression
466 stat_component_type ctype
;
468 void print (std::ostream
& o
) const;
469 void visit (visitor
* u
);
478 struct hist_op
: public indexable
480 histogram_type htype
;
482 std::vector
<int64_t> params
;
483 void print (std::ostream
& o
) const;
484 void visit (visitor
* u
);
485 // overrides of type 'indexable'
486 bool is_hist_op(hist_op
*& hist_out
);
489 // ------------------------------------------------------------------------
492 struct symboldecl
// unique object per (possibly implicit)
493 // symbol declaration
496 const token
* systemtap_v_conditional
; //checking systemtap compatibility
500 virtual ~symboldecl ();
501 virtual void print (std::ostream
&o
) const = 0;
502 virtual void printsig (std::ostream
&o
) const = 0;
506 std::ostream
& operator << (std::ostream
& o
, const symboldecl
& k
);
509 struct vardecl
: public symboldecl
511 void print (std::ostream
& o
) const;
512 void printsig (std::ostream
& o
) const;
514 void set_arity (int arity
, const token
* t
);
515 bool compatible_arity (int a
);
516 const token
* arity_tok
; // site where arity was first resolved
517 int arity
; // -1: unknown; 0: scalar; >0: array
518 int maxsize
; // upperbound on size for arrays
519 std::vector
<exp_type
> index_types
; // for arrays only
520 literal
*init
; // for global scalars only
521 bool synthetic
; // for probe locals only, don't init on entry
526 struct vardecl_builtin
: public vardecl
531 struct functiondecl
: public symboldecl
533 std::vector
<vardecl
*> formal_args
;
534 std::vector
<vardecl
*> locals
;
535 std::vector
<vardecl
*> unused_locals
;
538 bool mangle_oldstyle
;
540 void print (std::ostream
& o
) const;
541 void printsig (std::ostream
& o
) const;
542 void join (systemtap_session
& s
); // for synthetic functions only
546 // ------------------------------------------------------------------------
549 struct statement
: public visitable
551 virtual void print (std::ostream
& o
) const = 0;
552 virtual void visit (visitor
* u
) = 0;
555 statement (const token
* tok
);
556 virtual ~statement ();
559 std::ostream
& operator << (std::ostream
& o
, const statement
& k
);
562 struct embeddedcode
: public statement
565 void print (std::ostream
& o
) const;
566 void visit (visitor
* u
);
570 struct block
: public statement
572 std::vector
<statement
*> statements
;
573 void print (std::ostream
& o
) const;
574 void visit (visitor
* u
);
576 block (statement
* car
, statement
* cdr
);
581 struct try_block
: public statement
583 statement
* try_block
; // may be 0
584 statement
* catch_block
; // may be 0
585 symbol
* catch_error_var
; // may be 0
586 void print (std::ostream
& o
) const;
587 void visit (visitor
* u
);
591 struct expr_statement
;
592 struct for_loop
: public statement
594 expr_statement
* init
; // may be 0
596 expr_statement
* incr
; // may be 0
598 void print (std::ostream
& o
) const;
599 void visit (visitor
* u
);
603 struct foreach_loop
: public statement
605 // this part is a specialization of arrayindex
606 std::vector
<symbol
*> indexes
;
608 int sort_direction
; // -1: decreasing, 0: none, 1: increasing
609 unsigned sort_column
; // 0: value, 1..N: index
610 enum stat_component_type sort_aggr
; // for aggregate arrays, which aggregate to sort on
611 symbol
* value
; // optional iteration value
612 expression
* limit
; // optional iteration limit
615 void print (std::ostream
& o
) const;
616 void visit (visitor
* u
);
620 struct null_statement
: public statement
622 void print (std::ostream
& o
) const;
623 void visit (visitor
* u
);
624 null_statement (const token
* tok
);
628 struct expr_statement
: public statement
630 expression
* value
; // executed for side-effects
631 void print (std::ostream
& o
) const;
632 void visit (visitor
* u
);
636 struct if_statement
: public statement
638 expression
* condition
;
639 statement
* thenblock
;
640 statement
* elseblock
; // may be 0
641 void print (std::ostream
& o
) const;
642 void visit (visitor
* u
);
646 struct return_statement
: public expr_statement
648 void print (std::ostream
& o
) const;
649 void visit (visitor
* u
);
653 struct delete_statement
: public expr_statement
655 void print (std::ostream
& o
) const;
656 void visit (visitor
* u
);
660 struct break_statement
: public statement
662 void print (std::ostream
& o
) const;
663 void visit (visitor
* u
);
667 struct continue_statement
: public statement
669 void print (std::ostream
& o
) const;
670 void visit (visitor
* u
);
674 struct next_statement
: public statement
676 void print (std::ostream
& o
) const;
677 void visit (visitor
* u
);
682 struct derived_probe
;
688 std::vector
<probe
*> probes
;
689 std::vector
<probe_alias
*> aliases
;
690 std::vector
<functiondecl
*> functions
;
691 std::vector
<vardecl
*> globals
;
692 std::vector
<embeddedcode
*> embeds
;
693 std::string file_contents
;
695 stapfile (): file_contents (""),
696 privileged (false) {}
697 void print (std::ostream
& o
) const;
703 struct component
// XXX: sort of a restricted functioncall
706 literal
* arg
; // optional
708 const token
* tok
; // points to component's functor
709 component(std::string
const & f
, literal
* a
= NULL
);
711 std::vector
<component
*> components
;
714 expression
* condition
;
715 void print (std::ostream
& o
, bool print_extras
=true) const;
717 probe_point(const probe_point
& pp
);
718 probe_point(std::vector
<component
*> const & comps
);
719 std::string
str(bool print_extras
=true) const;
722 std::ostream
& operator << (std::ostream
& o
, const probe_point
& k
);
727 std::vector
<probe_point
*> locations
;
731 const token
* systemtap_v_conditional
; //checking systemtap compatibility
732 std::vector
<vardecl
*> locals
;
733 std::vector
<vardecl
*> unused_locals
;
734 static unsigned last_probeidx
;
736 probe (probe
* p
, probe_point
*l
);
737 void print (std::ostream
& o
) const;
738 virtual void printsig (std::ostream
&o
) const;
739 virtual void collect_derivation_chain (std::vector
<probe
*> &probes_list
) const;
740 virtual void collect_derivation_pp_chain (std::vector
<probe_point
*> &) const;
741 virtual const probe_alias
*get_alias () const { return 0; }
742 virtual probe_point
*get_alias_loc () const { return 0; }
743 virtual probe
* create_alias(probe_point
* l
, probe_point
* a
);
749 struct probe_alias
: public probe
751 probe_alias(std::vector
<probe_point
*> const & aliases
);
752 std::vector
<probe_point
*> alias_names
;
753 virtual void printsig (std::ostream
&o
) const;
758 // A derived visitor instance is used to visit the entire
759 // statement/expression tree.
762 // Machinery for differentiating lvalue visits from non-lvalue.
763 std::vector
<expression
*> active_lvalues
;
764 bool is_active_lvalue(expression
*e
);
765 void push_active_lvalue(expression
*e
);
766 void pop_active_lvalue();
768 virtual ~visitor () {}
769 virtual void visit_block (block
*s
) = 0;
770 virtual void visit_try_block (try_block
*s
) = 0;
771 virtual void visit_embeddedcode (embeddedcode
*s
) = 0;
772 virtual void visit_null_statement (null_statement
*s
) = 0;
773 virtual void visit_expr_statement (expr_statement
*s
) = 0;
774 virtual void visit_if_statement (if_statement
* s
) = 0;
775 virtual void visit_for_loop (for_loop
* s
) = 0;
776 virtual void visit_foreach_loop (foreach_loop
* s
) = 0;
777 virtual void visit_return_statement (return_statement
* s
) = 0;
778 virtual void visit_delete_statement (delete_statement
* s
) = 0;
779 virtual void visit_next_statement (next_statement
* s
) = 0;
780 virtual void visit_break_statement (break_statement
* s
) = 0;
781 virtual void visit_continue_statement (continue_statement
* s
) = 0;
782 virtual void visit_literal_string (literal_string
* e
) = 0;
783 virtual void visit_literal_number (literal_number
* e
) = 0;
784 virtual void visit_embedded_expr (embedded_expr
* e
) = 0;
785 virtual void visit_binary_expression (binary_expression
* e
) = 0;
786 virtual void visit_unary_expression (unary_expression
* e
) = 0;
787 virtual void visit_pre_crement (pre_crement
* e
) = 0;
788 virtual void visit_post_crement (post_crement
* e
) = 0;
789 virtual void visit_logical_or_expr (logical_or_expr
* e
) = 0;
790 virtual void visit_logical_and_expr (logical_and_expr
* e
) = 0;
791 virtual void visit_array_in (array_in
* e
) = 0;
792 virtual void visit_regex_query (regex_query
* e
) = 0;
793 virtual void visit_comparison (comparison
* e
) = 0;
794 virtual void visit_concatenation (concatenation
* e
) = 0;
795 virtual void visit_ternary_expression (ternary_expression
* e
) = 0;
796 virtual void visit_assignment (assignment
* e
) = 0;
797 virtual void visit_symbol (symbol
* e
) = 0;
798 virtual void visit_target_symbol (target_symbol
* e
) = 0;
799 virtual void visit_arrayindex (arrayindex
* e
) = 0;
800 virtual void visit_functioncall (functioncall
* e
) = 0;
801 virtual void visit_print_format (print_format
* e
) = 0;
802 virtual void visit_stat_op (stat_op
* e
) = 0;
803 virtual void visit_hist_op (hist_op
* e
) = 0;
804 virtual void visit_cast_op (cast_op
* e
) = 0;
805 virtual void visit_atvar_op (atvar_op
* e
) = 0;
806 virtual void visit_defined_op (defined_op
* e
) = 0;
807 virtual void visit_entry_op (entry_op
* e
) = 0;
808 virtual void visit_perf_op (perf_op
* e
) = 0;
812 // A simple kind of visitor, which travels down to the leaves of the
813 // statement/expression tree, up to but excluding following vardecls
814 // and functioncalls.
815 struct traversing_visitor
: public visitor
817 void visit_block (block
*s
);
818 void visit_try_block (try_block
*s
);
819 void visit_embeddedcode (embeddedcode
*s
);
820 void visit_null_statement (null_statement
*s
);
821 void visit_expr_statement (expr_statement
*s
);
822 void visit_if_statement (if_statement
* s
);
823 void visit_for_loop (for_loop
* s
);
824 void visit_foreach_loop (foreach_loop
* s
);
825 void visit_return_statement (return_statement
* s
);
826 void visit_delete_statement (delete_statement
* s
);
827 void visit_next_statement (next_statement
* s
);
828 void visit_break_statement (break_statement
* s
);
829 void visit_continue_statement (continue_statement
* s
);
830 void visit_literal_string (literal_string
* e
);
831 void visit_literal_number (literal_number
* e
);
832 void visit_embedded_expr (embedded_expr
* e
);
833 void visit_binary_expression (binary_expression
* e
);
834 void visit_unary_expression (unary_expression
* e
);
835 void visit_pre_crement (pre_crement
* e
);
836 void visit_post_crement (post_crement
* e
);
837 void visit_logical_or_expr (logical_or_expr
* e
);
838 void visit_logical_and_expr (logical_and_expr
* e
);
839 void visit_array_in (array_in
* e
);
840 void visit_regex_query (regex_query
* e
);
841 void visit_comparison (comparison
* e
);
842 void visit_concatenation (concatenation
* e
);
843 void visit_ternary_expression (ternary_expression
* e
);
844 void visit_assignment (assignment
* e
);
845 void visit_symbol (symbol
* e
);
846 void visit_target_symbol (target_symbol
* e
);
847 void visit_arrayindex (arrayindex
* e
);
848 void visit_functioncall (functioncall
* e
);
849 void visit_print_format (print_format
* e
);
850 void visit_stat_op (stat_op
* e
);
851 void visit_hist_op (hist_op
* e
);
852 void visit_cast_op (cast_op
* e
);
853 void visit_atvar_op (atvar_op
* e
);
854 void visit_defined_op (defined_op
* e
);
855 void visit_entry_op (entry_op
* e
);
856 void visit_perf_op (perf_op
* e
);
860 // A kind of traversing visitor, which also follows function calls.
861 // It uses an internal set object to prevent infinite recursion.
862 struct functioncall_traversing_visitor
: public traversing_visitor
864 std::set
<functiondecl
*> traversed
;
865 functiondecl
* current_function
;
866 functioncall_traversing_visitor(): current_function(0) {}
867 void visit_functioncall (functioncall
* e
);
871 // A kind of traversing visitor, which also follows function calls,
872 // and stores the vardecl* referent of each variable read and/or
873 // written and other such sundry side-effect data. It's used by
874 // the elaboration-time optimizer pass.
875 struct varuse_collecting_visitor
: public functioncall_traversing_visitor
877 systemtap_session
& session
;
878 std::set
<vardecl
*> read
;
879 std::set
<vardecl
*> written
;
880 std::set
<vardecl
*> used
;
882 bool current_lvalue_read
;
883 expression
* current_lvalue
;
884 expression
* current_lrvalue
;
885 varuse_collecting_visitor(systemtap_session
& s
):
887 embedded_seen (false),
888 current_lvalue_read (false),
890 current_lrvalue(0) {}
891 void visit_embeddedcode (embeddedcode
*s
);
892 void visit_embedded_expr (embedded_expr
*e
);
893 void visit_try_block (try_block
*s
);
894 void visit_delete_statement (delete_statement
*s
);
895 void visit_print_format (print_format
*e
);
896 void visit_assignment (assignment
*e
);
897 void visit_arrayindex (arrayindex
*e
);
898 void visit_target_symbol (target_symbol
*e
);
899 void visit_symbol (symbol
*e
);
900 void visit_pre_crement (pre_crement
*e
);
901 void visit_post_crement (post_crement
*e
);
902 void visit_foreach_loop (foreach_loop
*s
);
903 void visit_cast_op (cast_op
* e
);
904 void visit_atvar_op (atvar_op
*e
);
905 void visit_defined_op (defined_op
* e
);
906 void visit_entry_op (entry_op
* e
);
907 void visit_perf_op (perf_op
* e
);
908 bool side_effect_free ();
909 bool side_effect_free_wrt (const std::set
<vardecl
*>& vars
);
914 // A kind of visitor that throws an semantic_error exception
915 // whenever a non-overridden method is called.
916 struct throwing_visitor
: public visitor
919 throwing_visitor (const std::string
& m
);
922 virtual void throwone (const token
* t
);
924 void visit_block (block
*s
);
925 void visit_try_block (try_block
*s
);
926 void visit_embeddedcode (embeddedcode
*s
);
927 void visit_null_statement (null_statement
*s
);
928 void visit_expr_statement (expr_statement
*s
);
929 void visit_if_statement (if_statement
* s
);
930 void visit_for_loop (for_loop
* s
);
931 void visit_foreach_loop (foreach_loop
* s
);
932 void visit_return_statement (return_statement
* s
);
933 void visit_delete_statement (delete_statement
* s
);
934 void visit_next_statement (next_statement
* s
);
935 void visit_break_statement (break_statement
* s
);
936 void visit_continue_statement (continue_statement
* s
);
937 void visit_literal_string (literal_string
* e
);
938 void visit_literal_number (literal_number
* e
);
939 void visit_embedded_expr (embedded_expr
* e
);
940 void visit_binary_expression (binary_expression
* e
);
941 void visit_unary_expression (unary_expression
* e
);
942 void visit_pre_crement (pre_crement
* e
);
943 void visit_post_crement (post_crement
* e
);
944 void visit_logical_or_expr (logical_or_expr
* e
);
945 void visit_logical_and_expr (logical_and_expr
* e
);
946 void visit_array_in (array_in
* e
);
947 void visit_regex_query (regex_query
* e
);
948 void visit_comparison (comparison
* e
);
949 void visit_concatenation (concatenation
* e
);
950 void visit_ternary_expression (ternary_expression
* e
);
951 void visit_assignment (assignment
* e
);
952 void visit_symbol (symbol
* e
);
953 void visit_target_symbol (target_symbol
* e
);
954 void visit_arrayindex (arrayindex
* e
);
955 void visit_functioncall (functioncall
* e
);
956 void visit_print_format (print_format
* e
);
957 void visit_stat_op (stat_op
* e
);
958 void visit_hist_op (hist_op
* e
);
959 void visit_cast_op (cast_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
);
966 // A visitor similar to a traversing_visitor, but with the ability to rewrite
967 // parts of the tree through require/provide.
969 struct update_visitor
: public visitor
971 template <typename T
> T
* require (T
* src
, bool clearok
=false)
978 throw std::runtime_error(_("update_visitor wasn't provided a value"));
979 visitable
*v
= values
.top();
981 if (v
== NULL
&& !clearok
)
982 throw std::runtime_error(_("update_visitor was provided a NULL value"));
983 dst
= dynamic_cast<T
*>(v
);
984 if (v
!= NULL
&& dst
== NULL
)
985 throw std::runtime_error(_F("update_visitor can't set type \"%s\" with a \"%s\"",
986 typeid(T
).name(), typeid(*v
).name()));
991 template <typename T
> void provide (T
* src
)
996 template <typename T
> void replace (T
*& src
, bool clearok
=false)
998 src
= require(src
, clearok
);
1001 virtual ~update_visitor() { assert(values
.empty()); }
1003 virtual void visit_block (block
*s
);
1004 virtual void visit_try_block (try_block
*s
);
1005 virtual void visit_embeddedcode (embeddedcode
*s
);
1006 virtual void visit_null_statement (null_statement
*s
);
1007 virtual void visit_expr_statement (expr_statement
*s
);
1008 virtual void visit_if_statement (if_statement
* s
);
1009 virtual void visit_for_loop (for_loop
* s
);
1010 virtual void visit_foreach_loop (foreach_loop
* s
);
1011 virtual void visit_return_statement (return_statement
* s
);
1012 virtual void visit_delete_statement (delete_statement
* s
);
1013 virtual void visit_next_statement (next_statement
* s
);
1014 virtual void visit_break_statement (break_statement
* s
);
1015 virtual void visit_continue_statement (continue_statement
* s
);
1016 virtual void visit_literal_string (literal_string
* e
);
1017 virtual void visit_literal_number (literal_number
* e
);
1018 virtual void visit_embedded_expr (embedded_expr
* e
);
1019 virtual void visit_binary_expression (binary_expression
* e
);
1020 virtual void visit_unary_expression (unary_expression
* e
);
1021 virtual void visit_pre_crement (pre_crement
* e
);
1022 virtual void visit_post_crement (post_crement
* e
);
1023 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1024 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1025 virtual void visit_array_in (array_in
* e
);
1026 virtual void visit_regex_query (regex_query
* e
);
1027 virtual void visit_comparison (comparison
* e
);
1028 virtual void visit_concatenation (concatenation
* e
);
1029 virtual void visit_ternary_expression (ternary_expression
* e
);
1030 virtual void visit_assignment (assignment
* e
);
1031 virtual void visit_symbol (symbol
* e
);
1032 virtual void visit_target_symbol (target_symbol
* e
);
1033 virtual void visit_arrayindex (arrayindex
* e
);
1034 virtual void visit_functioncall (functioncall
* e
);
1035 virtual void visit_print_format (print_format
* e
);
1036 virtual void visit_stat_op (stat_op
* e
);
1037 virtual void visit_hist_op (hist_op
* e
);
1038 virtual void visit_cast_op (cast_op
* e
);
1039 virtual void visit_atvar_op (atvar_op
* e
);
1040 virtual void visit_defined_op (defined_op
* e
);
1041 virtual void visit_entry_op (entry_op
* e
);
1042 virtual void visit_perf_op (perf_op
* e
);
1045 std::stack
<visitable
*> values
;
1048 // A visitor which performs a deep copy of the root node it's applied
1049 // to. NB: It does not copy any of the variable or function
1050 // declarations; those fields are set to NULL, assuming you want to
1051 // re-infer the declarations in a new context (the one you're copying
1054 struct deep_copy_visitor
: public update_visitor
1056 template <typename T
> static T
* deep_copy (T
* e
)
1058 deep_copy_visitor v
;
1059 return v
.require (e
);
1062 virtual void visit_block (block
*s
);
1063 virtual void visit_try_block (try_block
*s
);
1064 virtual void visit_embeddedcode (embeddedcode
*s
);
1065 virtual void visit_null_statement (null_statement
*s
);
1066 virtual void visit_expr_statement (expr_statement
*s
);
1067 virtual void visit_if_statement (if_statement
* s
);
1068 virtual void visit_for_loop (for_loop
* s
);
1069 virtual void visit_foreach_loop (foreach_loop
* s
);
1070 virtual void visit_return_statement (return_statement
* s
);
1071 virtual void visit_delete_statement (delete_statement
* s
);
1072 virtual void visit_next_statement (next_statement
* s
);
1073 virtual void visit_break_statement (break_statement
* s
);
1074 virtual void visit_continue_statement (continue_statement
* s
);
1075 virtual void visit_literal_string (literal_string
* e
);
1076 virtual void visit_literal_number (literal_number
* e
);
1077 virtual void visit_embedded_expr (embedded_expr
* e
);
1078 virtual void visit_binary_expression (binary_expression
* e
);
1079 virtual void visit_unary_expression (unary_expression
* e
);
1080 virtual void visit_pre_crement (pre_crement
* e
);
1081 virtual void visit_post_crement (post_crement
* e
);
1082 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1083 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1084 virtual void visit_array_in (array_in
* e
);
1085 virtual void visit_regex_query (regex_query
* e
);
1086 virtual void visit_comparison (comparison
* e
);
1087 virtual void visit_concatenation (concatenation
* e
);
1088 virtual void visit_ternary_expression (ternary_expression
* e
);
1089 virtual void visit_assignment (assignment
* e
);
1090 virtual void visit_symbol (symbol
* e
);
1091 virtual void visit_target_symbol (target_symbol
* e
);
1092 virtual void visit_arrayindex (arrayindex
* e
);
1093 virtual void visit_functioncall (functioncall
* e
);
1094 virtual void visit_print_format (print_format
* e
);
1095 virtual void visit_stat_op (stat_op
* e
);
1096 virtual void visit_hist_op (hist_op
* e
);
1097 virtual void visit_cast_op (cast_op
* e
);
1098 virtual void visit_atvar_op (atvar_op
* e
);
1099 virtual void visit_defined_op (defined_op
* e
);
1100 virtual void visit_entry_op (entry_op
* e
);
1101 virtual void visit_perf_op (perf_op
* e
);
1104 #endif // STAPTREE_H
1106 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */