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
25 struct token
; // parse.h
26 struct systemtap_session
; // session.h
28 struct semantic_error
: public std::runtime_error
32 const semantic_error
*chain
;
34 ~semantic_error () throw () {}
36 semantic_error (const std::string
& msg
, const token
* t1
=0):
37 runtime_error (msg
), tok1 (t1
), tok2 (0), chain (0) {}
39 semantic_error (const std::string
& msg
, const token
* t1
,
41 runtime_error (msg
), tok1 (t1
), tok2 (t2
), chain (0) {}
44 // ------------------------------------------------------------------------
46 /* struct statistic_decl moved to session.h */
48 // ------------------------------------------------------------------------
54 pe_string
, // std::string
58 std::ostream
& operator << (std::ostream
& o
, const exp_type
& e
);
62 struct update_visitor
;
69 virtual ~expression ();
70 virtual void print (std::ostream
& o
) const = 0;
71 virtual void visit (visitor
* u
) = 0;
74 std::ostream
& operator << (std::ostream
& o
, const expression
& k
);
77 struct literal
: public expression
82 struct literal_string
: public literal
85 literal_string (const std::string
& v
);
86 void print (std::ostream
& o
) const;
87 void visit (visitor
* u
);
91 struct literal_number
: public literal
95 literal_number (int64_t v
, bool hex
=false);
96 void print (std::ostream
& o
) const;
97 void visit (visitor
* u
);
101 struct embedded_expr
: public expression
104 void print (std::ostream
& o
) const;
105 void visit (visitor
* u
);
109 struct binary_expression
: public expression
114 void print (std::ostream
& o
) const;
115 void visit (visitor
* u
);
119 struct unary_expression
: public expression
123 void print (std::ostream
& o
) const;
124 void visit (visitor
* u
);
128 struct pre_crement
: public unary_expression
130 void visit (visitor
* u
);
134 struct post_crement
: public unary_expression
136 void print (std::ostream
& o
) const;
137 void visit (visitor
* u
);
141 struct logical_or_expr
: public binary_expression
143 void visit (visitor
* u
);
147 struct logical_and_expr
: public binary_expression
149 void visit (visitor
* u
);
154 struct array_in
: public expression
157 void print (std::ostream
& o
) const;
158 void visit (visitor
* u
);
161 struct regex_query
: public binary_expression
163 literal_string
*re
; // XXX somewhat redundant with right
164 void visit (visitor
* u
);
167 struct comparison
: public binary_expression
169 void visit (visitor
* u
);
173 struct concatenation
: public binary_expression
175 void visit (visitor
* u
);
179 struct ternary_expression
: public expression
182 expression
* truevalue
;
183 expression
* falsevalue
;
184 void print (std::ostream
& o
) const;
185 void visit (visitor
* u
);
189 struct assignment
: public binary_expression
191 void visit (visitor
* u
);
198 // This is a helper class which, type-wise, acts as a disjoint union
199 // of symbols and histograms. You can ask it whether it's a
200 // histogram or a symbol, and downcast accordingly.
201 void print_indexable (std::ostream
& o
) const;
202 void visit_indexable (visitor
* u
);
203 virtual bool is_symbol(symbol
*& sym_out
);
204 virtual bool is_hist_op(hist_op
*& hist_out
);
205 virtual bool is_const_symbol(const symbol
*& sym_out
) const;
206 virtual bool is_const_hist_op(const hist_op
*& hist_out
) const;
207 virtual const token
*get_tok() const = 0;
208 virtual ~indexable() {}
211 // Perform a downcast to one out-value and NULL the other, throwing an
212 // exception if neither downcast succeeds. This is (sadly) about the
213 // best we can accomplish in C++.
215 classify_indexable(indexable
* ix
,
217 hist_op
*& hist_out
);
220 classify_const_indexable(const indexable
* ix
,
221 symbol
const *& array_out
,
222 hist_op
const *& hist_out
);
232 void print (std::ostream
& o
) const;
233 void visit (visitor
* u
);
234 // overrides of type 'indexable'
235 const token
*get_tok() const;
236 bool is_const_symbol(const symbol
*& sym_out
) const;
237 bool is_symbol(symbol
*& sym_out
);
241 struct target_symbol
: public symbol
246 comp_literal_array_index
,
247 comp_expression_array_index
,
248 comp_pretty_print
, // must be final
255 std::string member
; // comp_struct_member, comp_pretty_print
256 int64_t num_index
; // comp_literal_array_index
257 expression
* expr_index
; // comp_expression_array_index
259 component(const token
* t
, const std::string
& m
, bool pprint
=false):
261 type(pprint
? comp_pretty_print
: comp_struct_member
),
262 member(m
), num_index(0), expr_index(0)
264 component(const token
* t
, int64_t n
):
265 tok(t
), type(comp_literal_array_index
), num_index(n
),
267 component(const token
* t
, expression
* e
):
268 tok(t
), type(comp_expression_array_index
), num_index(0),
270 void print (std::ostream
& o
) const;
274 std::string target_name
;
276 std::vector
<component
> components
;
277 semantic_error
* saved_conversion_error
; // hand-made linked list
278 target_symbol(): addressof(false), saved_conversion_error (0) {}
279 std::string
sym_name ();
280 void chain (const semantic_error
& er
);
281 void print (std::ostream
& o
) const;
282 void visit (visitor
* u
);
283 void visit_components (visitor
* u
);
284 void visit_components (update_visitor
* u
);
285 void assert_no_components(const std::string
& tapset
, bool pretty_ok
=false);
288 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
);
291 struct cast_op
: public target_symbol
294 std::string type_name
, module
;
295 void print (std::ostream
& o
) const;
296 void visit (visitor
* u
);
300 struct defined_op
: public expression
302 target_symbol
*operand
;
303 void print (std::ostream
& o
) const;
304 void visit (visitor
* u
);
308 struct entry_op
: public expression
311 void print (std::ostream
& o
) const;
312 void visit (visitor
* u
);
316 struct perf_op
: public expression
319 void print (std::ostream
& o
) const;
320 void visit (visitor
* u
);
324 struct arrayindex
: public expression
326 std::vector
<expression
*> indexes
;
329 void print (std::ostream
& o
) const;
330 void visit (visitor
* u
);
335 struct functioncall
: public expression
337 std::string function
;
338 std::vector
<expression
*> args
;
339 functiondecl
*referent
;
341 void print (std::ostream
& o
) const;
342 void visit (visitor
* u
);
346 struct print_format
: public expression
348 bool print_to_stream
;
349 bool print_with_format
;
350 bool print_with_delim
;
351 bool print_with_newline
;
354 // XXX match runtime/vsprintf.c's print_flag
355 // ... for use with number() & number_size()
358 fmt_flag_zeropad
= 1,
363 fmt_flag_special
= 32,
394 struct format_component
400 width_type widthtype
;
401 precision_type prectype
;
402 conversion_type type
;
403 std::string literal_string
;
404 bool is_empty() const
407 && widthtype
== width_unspecified
408 && prectype
== prec_unspecified
409 && type
== conv_unspecified
410 && literal_string
.empty();
415 widthtype
= width_unspecified
;
416 prectype
= prec_unspecified
;
417 type
= conv_unspecified
;
418 literal_string
.clear();
420 inline void set_flag(format_flag f
) { flags
|= f
; }
421 inline bool test_flag(format_flag f
) const { return flags
& f
; }
424 std::string raw_components
;
425 std::vector
<format_component
> components
;
426 format_component delimiter
;
427 std::vector
<expression
*> args
;
430 static std::string
components_to_string(std::vector
<format_component
> const & components
);
431 static std::vector
<format_component
> string_to_components(std::string
const & str
);
432 static print_format
* create(const token
*t
);
434 void print (std::ostream
& o
) const;
435 void visit (visitor
* u
);
438 print_format(bool stream
, bool format
, bool delim
, bool newline
, bool _char
):
439 print_to_stream(stream
), print_with_format(format
),
440 print_with_delim(delim
), print_with_newline(newline
),
441 print_char(_char
), hist(NULL
)
446 enum stat_component_type
456 struct stat_op
: public expression
458 stat_component_type ctype
;
460 void print (std::ostream
& o
) const;
461 void visit (visitor
* u
);
470 struct hist_op
: public indexable
473 histogram_type htype
;
475 std::vector
<int64_t> params
;
476 void print (std::ostream
& o
) const;
477 void visit (visitor
* u
);
478 // overrides of type 'indexable'
479 const token
*get_tok() const;
480 bool is_const_hist_op(const hist_op
*& hist_out
) const;
481 bool is_hist_op(hist_op
*& hist_out
);
484 // ------------------------------------------------------------------------
487 struct symboldecl
// unique object per (possibly implicit)
488 // symbol declaration
491 const token
* systemtap_v_conditional
; //checking systemtap compatibility
495 virtual ~symboldecl ();
496 virtual void print (std::ostream
&o
) const = 0;
497 virtual void printsig (std::ostream
&o
) const = 0;
501 std::ostream
& operator << (std::ostream
& o
, const symboldecl
& k
);
504 struct vardecl
: public symboldecl
506 void print (std::ostream
& o
) const;
507 void printsig (std::ostream
& o
) const;
509 void set_arity (int arity
, const token
* t
);
510 bool compatible_arity (int a
);
511 const token
* arity_tok
; // site where arity was first resolved
512 int arity
; // -1: unknown; 0: scalar; >0: array
513 int maxsize
; // upperbound on size for arrays
514 std::vector
<exp_type
> index_types
; // for arrays only
515 literal
*init
; // for global scalars only
516 bool synthetic
; // for probe locals only, don't init on entry
521 struct vardecl_builtin
: public vardecl
526 struct functiondecl
: public symboldecl
528 std::vector
<vardecl
*> formal_args
;
529 std::vector
<vardecl
*> locals
;
530 std::vector
<vardecl
*> unused_locals
;
533 bool mangle_oldstyle
;
535 void print (std::ostream
& o
) const;
536 void printsig (std::ostream
& o
) const;
537 void join (systemtap_session
& s
); // for synthetic functions only
541 // ------------------------------------------------------------------------
546 virtual void print (std::ostream
& o
) const = 0;
547 virtual void visit (visitor
* u
) = 0;
550 statement (const token
* tok
);
551 virtual ~statement ();
554 std::ostream
& operator << (std::ostream
& o
, const statement
& k
);
557 struct embeddedcode
: public statement
560 void print (std::ostream
& o
) const;
561 void visit (visitor
* u
);
565 struct block
: public statement
567 std::vector
<statement
*> statements
;
568 void print (std::ostream
& o
) const;
569 void visit (visitor
* u
);
571 block (statement
* car
, statement
* cdr
);
576 struct try_block
: public statement
578 statement
* try_block
; // may be 0
579 statement
* catch_block
; // may be 0
580 symbol
* catch_error_var
; // may be 0
581 void print (std::ostream
& o
) const;
582 void visit (visitor
* u
);
586 struct expr_statement
;
587 struct for_loop
: public statement
589 expr_statement
* init
; // may be 0
591 expr_statement
* incr
; // may be 0
593 void print (std::ostream
& o
) const;
594 void visit (visitor
* u
);
598 struct foreach_loop
: public statement
600 // this part is a specialization of arrayindex
601 std::vector
<symbol
*> indexes
;
603 int sort_direction
; // -1: decreasing, 0: none, 1: increasing
604 unsigned sort_column
; // 0: value, 1..N: index
605 enum stat_component_type sort_aggr
; // for aggregate arrays, which aggregate to sort on
606 symbol
* value
; // optional iteration value
607 expression
* limit
; // optional iteration limit
610 void print (std::ostream
& o
) const;
611 void visit (visitor
* u
);
615 struct null_statement
: public statement
617 void print (std::ostream
& o
) const;
618 void visit (visitor
* u
);
619 null_statement (const token
* tok
);
623 struct expr_statement
: public statement
625 expression
* value
; // executed for side-effects
626 void print (std::ostream
& o
) const;
627 void visit (visitor
* u
);
631 struct if_statement
: public statement
633 expression
* condition
;
634 statement
* thenblock
;
635 statement
* elseblock
; // may be 0
636 void print (std::ostream
& o
) const;
637 void visit (visitor
* u
);
641 struct return_statement
: public expr_statement
643 void print (std::ostream
& o
) const;
644 void visit (visitor
* u
);
648 struct delete_statement
: public expr_statement
650 void print (std::ostream
& o
) const;
651 void visit (visitor
* u
);
655 struct break_statement
: public statement
657 void print (std::ostream
& o
) const;
658 void visit (visitor
* u
);
662 struct continue_statement
: public statement
664 void print (std::ostream
& o
) const;
665 void visit (visitor
* u
);
669 struct next_statement
: public statement
671 void print (std::ostream
& o
) const;
672 void visit (visitor
* u
);
677 struct derived_probe
;
683 std::vector
<probe
*> probes
;
684 std::vector
<probe_alias
*> aliases
;
685 std::vector
<functiondecl
*> functions
;
686 std::vector
<vardecl
*> globals
;
687 std::vector
<embeddedcode
*> embeds
;
688 std::string file_contents
;
690 stapfile (): file_contents (""),
691 privileged (false) {}
692 void print (std::ostream
& o
) const;
698 struct component
// XXX: sort of a restricted functioncall
701 literal
* arg
; // optional
703 const token
* tok
; // points to component's functor
704 component(std::string
const & f
, literal
* a
= NULL
);
706 std::vector
<component
*> components
;
709 expression
* condition
;
710 void print (std::ostream
& o
, bool print_extras
=true) const;
712 probe_point(const probe_point
& pp
);
713 probe_point(std::vector
<component
*> const & comps
);
714 std::string
str(bool print_extras
=true) const;
717 std::ostream
& operator << (std::ostream
& o
, const probe_point
& k
);
722 std::vector
<probe_point
*> locations
;
725 const token
* systemtap_v_conditional
; //checking systemtap compatibility
726 std::vector
<vardecl
*> locals
;
727 std::vector
<vardecl
*> unused_locals
;
728 static unsigned last_probeidx
;
730 probe (const probe
& p
, probe_point
*l
);
731 void print (std::ostream
& o
) const;
732 virtual void printsig (std::ostream
&o
) const;
733 virtual void collect_derivation_chain (std::vector
<probe
*> &probes_list
);
734 virtual void collect_derivation_pp_chain (std::vector
<probe_point
*> &) {}
735 virtual const probe_alias
*get_alias () const { return 0; }
736 virtual probe_point
*get_alias_loc () const { return 0; }
737 virtual probe
* create_alias(probe_point
* l
, probe_point
* a
);
738 virtual const probe
* basest () const { return this; }
739 virtual const probe
* almost_basest () const { return 0; }
745 struct probe_alias
: public probe
747 probe_alias(std::vector
<probe_point
*> const & aliases
);
748 std::vector
<probe_point
*> alias_names
;
749 virtual void printsig (std::ostream
&o
) const;
754 // A derived visitor instance is used to visit the entire
755 // statement/expression tree.
758 // Machinery for differentiating lvalue visits from non-lvalue.
759 std::vector
<expression
*> active_lvalues
;
760 bool is_active_lvalue(expression
*e
);
761 void push_active_lvalue(expression
*e
);
762 void pop_active_lvalue();
764 virtual ~visitor () {}
765 virtual void visit_block (block
*s
) = 0;
766 virtual void visit_try_block (try_block
*s
) = 0;
767 virtual void visit_embeddedcode (embeddedcode
*s
) = 0;
768 virtual void visit_null_statement (null_statement
*s
) = 0;
769 virtual void visit_expr_statement (expr_statement
*s
) = 0;
770 virtual void visit_if_statement (if_statement
* s
) = 0;
771 virtual void visit_for_loop (for_loop
* s
) = 0;
772 virtual void visit_foreach_loop (foreach_loop
* s
) = 0;
773 virtual void visit_return_statement (return_statement
* s
) = 0;
774 virtual void visit_delete_statement (delete_statement
* s
) = 0;
775 virtual void visit_next_statement (next_statement
* s
) = 0;
776 virtual void visit_break_statement (break_statement
* s
) = 0;
777 virtual void visit_continue_statement (continue_statement
* s
) = 0;
778 virtual void visit_literal_string (literal_string
* e
) = 0;
779 virtual void visit_literal_number (literal_number
* e
) = 0;
780 virtual void visit_embedded_expr (embedded_expr
* e
) = 0;
781 virtual void visit_binary_expression (binary_expression
* e
) = 0;
782 virtual void visit_unary_expression (unary_expression
* e
) = 0;
783 virtual void visit_pre_crement (pre_crement
* e
) = 0;
784 virtual void visit_post_crement (post_crement
* e
) = 0;
785 virtual void visit_logical_or_expr (logical_or_expr
* e
) = 0;
786 virtual void visit_logical_and_expr (logical_and_expr
* e
) = 0;
787 virtual void visit_array_in (array_in
* e
) = 0;
788 virtual void visit_regex_query (regex_query
* e
) = 0;
789 virtual void visit_comparison (comparison
* e
) = 0;
790 virtual void visit_concatenation (concatenation
* e
) = 0;
791 virtual void visit_ternary_expression (ternary_expression
* e
) = 0;
792 virtual void visit_assignment (assignment
* e
) = 0;
793 virtual void visit_symbol (symbol
* e
) = 0;
794 virtual void visit_target_symbol (target_symbol
* e
) = 0;
795 virtual void visit_arrayindex (arrayindex
* e
) = 0;
796 virtual void visit_functioncall (functioncall
* e
) = 0;
797 virtual void visit_print_format (print_format
* e
) = 0;
798 virtual void visit_stat_op (stat_op
* e
) = 0;
799 virtual void visit_hist_op (hist_op
* e
) = 0;
800 virtual void visit_cast_op (cast_op
* e
) = 0;
801 virtual void visit_defined_op (defined_op
* e
) = 0;
802 virtual void visit_entry_op (entry_op
* e
) = 0;
803 virtual void visit_perf_op (perf_op
* e
) = 0;
807 // A simple kind of visitor, which travels down to the leaves of the
808 // statement/expression tree, up to but excluding following vardecls
809 // and functioncalls.
810 struct traversing_visitor
: public visitor
812 void visit_block (block
*s
);
813 void visit_try_block (try_block
*s
);
814 void visit_embeddedcode (embeddedcode
*s
);
815 void visit_null_statement (null_statement
*s
);
816 void visit_expr_statement (expr_statement
*s
);
817 void visit_if_statement (if_statement
* s
);
818 void visit_for_loop (for_loop
* s
);
819 void visit_foreach_loop (foreach_loop
* s
);
820 void visit_return_statement (return_statement
* s
);
821 void visit_delete_statement (delete_statement
* s
);
822 void visit_next_statement (next_statement
* s
);
823 void visit_break_statement (break_statement
* s
);
824 void visit_continue_statement (continue_statement
* s
);
825 void visit_literal_string (literal_string
* e
);
826 void visit_literal_number (literal_number
* e
);
827 void visit_embedded_expr (embedded_expr
* e
);
828 void visit_binary_expression (binary_expression
* e
);
829 void visit_unary_expression (unary_expression
* e
);
830 void visit_pre_crement (pre_crement
* e
);
831 void visit_post_crement (post_crement
* e
);
832 void visit_logical_or_expr (logical_or_expr
* e
);
833 void visit_logical_and_expr (logical_and_expr
* e
);
834 void visit_array_in (array_in
* e
);
835 void visit_regex_query (regex_query
* e
);
836 void visit_comparison (comparison
* e
);
837 void visit_concatenation (concatenation
* e
);
838 void visit_ternary_expression (ternary_expression
* e
);
839 void visit_assignment (assignment
* e
);
840 void visit_symbol (symbol
* e
);
841 void visit_target_symbol (target_symbol
* e
);
842 void visit_arrayindex (arrayindex
* e
);
843 void visit_functioncall (functioncall
* e
);
844 void visit_print_format (print_format
* e
);
845 void visit_stat_op (stat_op
* e
);
846 void visit_hist_op (hist_op
* e
);
847 void visit_cast_op (cast_op
* e
);
848 void visit_defined_op (defined_op
* e
);
849 void visit_entry_op (entry_op
* e
);
850 void visit_perf_op (perf_op
* e
);
854 // A kind of traversing visitor, which also follows function calls.
855 // It uses an internal set object to prevent infinite recursion.
856 struct functioncall_traversing_visitor
: public traversing_visitor
858 std::set
<functiondecl
*> traversed
;
859 functiondecl
* current_function
;
860 functioncall_traversing_visitor(): current_function(0) {}
861 void visit_functioncall (functioncall
* e
);
865 // A kind of traversing visitor, which also follows function calls,
866 // and stores the vardecl* referent of each variable read and/or
867 // written and other such sundry side-effect data. It's used by
868 // the elaboration-time optimizer pass.
869 struct varuse_collecting_visitor
: public functioncall_traversing_visitor
871 systemtap_session
& session
;
872 std::set
<vardecl
*> read
;
873 std::set
<vardecl
*> written
;
874 std::set
<vardecl
*> used
;
876 bool current_lvalue_read
;
877 expression
* current_lvalue
;
878 expression
* current_lrvalue
;
879 varuse_collecting_visitor(systemtap_session
& s
):
881 embedded_seen (false),
882 current_lvalue_read (false),
884 current_lrvalue(0) {}
885 void visit_embeddedcode (embeddedcode
*s
);
886 void visit_embedded_expr (embedded_expr
*e
);
887 void visit_try_block (try_block
*s
);
888 void visit_delete_statement (delete_statement
*s
);
889 void visit_print_format (print_format
*e
);
890 void visit_assignment (assignment
*e
);
891 void visit_arrayindex (arrayindex
*e
);
892 void visit_target_symbol (target_symbol
*e
);
893 void visit_symbol (symbol
*e
);
894 void visit_pre_crement (pre_crement
*e
);
895 void visit_post_crement (post_crement
*e
);
896 void visit_foreach_loop (foreach_loop
*s
);
897 void visit_cast_op (cast_op
* e
);
898 void visit_defined_op (defined_op
* e
);
899 void visit_entry_op (entry_op
* e
);
900 void visit_perf_op (perf_op
* e
);
901 bool side_effect_free ();
902 bool side_effect_free_wrt (const std::set
<vardecl
*>& vars
);
907 // A kind of visitor that throws an semantic_error exception
908 // whenever a non-overridden method is called.
909 struct throwing_visitor
: public visitor
912 throwing_visitor (const std::string
& m
);
915 virtual void throwone (const token
* t
);
917 void visit_block (block
*s
);
918 void visit_try_block (try_block
*s
);
919 void visit_embeddedcode (embeddedcode
*s
);
920 void visit_null_statement (null_statement
*s
);
921 void visit_expr_statement (expr_statement
*s
);
922 void visit_if_statement (if_statement
* s
);
923 void visit_for_loop (for_loop
* s
);
924 void visit_foreach_loop (foreach_loop
* s
);
925 void visit_return_statement (return_statement
* s
);
926 void visit_delete_statement (delete_statement
* s
);
927 void visit_next_statement (next_statement
* s
);
928 void visit_break_statement (break_statement
* s
);
929 void visit_continue_statement (continue_statement
* s
);
930 void visit_literal_string (literal_string
* e
);
931 void visit_literal_number (literal_number
* e
);
932 void visit_embedded_expr (embedded_expr
* e
);
933 void visit_binary_expression (binary_expression
* e
);
934 void visit_unary_expression (unary_expression
* e
);
935 void visit_pre_crement (pre_crement
* e
);
936 void visit_post_crement (post_crement
* e
);
937 void visit_logical_or_expr (logical_or_expr
* e
);
938 void visit_logical_and_expr (logical_and_expr
* e
);
939 void visit_array_in (array_in
* e
);
940 void visit_regex_query (regex_query
* e
);
941 void visit_comparison (comparison
* e
);
942 void visit_concatenation (concatenation
* e
);
943 void visit_ternary_expression (ternary_expression
* e
);
944 void visit_assignment (assignment
* e
);
945 void visit_symbol (symbol
* e
);
946 void visit_target_symbol (target_symbol
* e
);
947 void visit_arrayindex (arrayindex
* e
);
948 void visit_functioncall (functioncall
* e
);
949 void visit_print_format (print_format
* e
);
950 void visit_stat_op (stat_op
* e
);
951 void visit_hist_op (hist_op
* e
);
952 void visit_cast_op (cast_op
* e
);
953 void visit_defined_op (defined_op
* e
);
954 void visit_entry_op (entry_op
* e
);
955 void visit_perf_op (perf_op
* e
);
958 // A visitor similar to a traversing_visitor, but with the ability to rewrite
959 // parts of the tree through require/provide.
961 struct update_visitor
: public visitor
963 template <typename T
> T
* require (T
* src
, bool clearok
=false)
969 assert(!targets
.empty());
970 dst
= static_cast<T
*>(targets
.top()); // XXX: danger will robinson: not typesafe!
972 assert(clearok
|| dst
);
977 template <typename T
> void provide (T
* src
)
979 targets
.push(static_cast<void*>(src
)); // XXX: not typesafe!
982 template <typename T
> void replace (T
*& src
, bool clearok
=false)
984 src
= require(src
, clearok
);
987 virtual ~update_visitor() { assert(targets
.empty()); }
989 virtual void visit_block (block
*s
);
990 virtual void visit_try_block (try_block
*s
);
991 virtual void visit_embeddedcode (embeddedcode
*s
);
992 virtual void visit_null_statement (null_statement
*s
);
993 virtual void visit_expr_statement (expr_statement
*s
);
994 virtual void visit_if_statement (if_statement
* s
);
995 virtual void visit_for_loop (for_loop
* s
);
996 virtual void visit_foreach_loop (foreach_loop
* s
);
997 virtual void visit_return_statement (return_statement
* s
);
998 virtual void visit_delete_statement (delete_statement
* s
);
999 virtual void visit_next_statement (next_statement
* s
);
1000 virtual void visit_break_statement (break_statement
* s
);
1001 virtual void visit_continue_statement (continue_statement
* s
);
1002 virtual void visit_literal_string (literal_string
* e
);
1003 virtual void visit_literal_number (literal_number
* e
);
1004 virtual void visit_embedded_expr (embedded_expr
* e
);
1005 virtual void visit_binary_expression (binary_expression
* e
);
1006 virtual void visit_unary_expression (unary_expression
* e
);
1007 virtual void visit_pre_crement (pre_crement
* e
);
1008 virtual void visit_post_crement (post_crement
* e
);
1009 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1010 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1011 virtual void visit_array_in (array_in
* e
);
1012 virtual void visit_regex_query (regex_query
* e
);
1013 virtual void visit_comparison (comparison
* e
);
1014 virtual void visit_concatenation (concatenation
* e
);
1015 virtual void visit_ternary_expression (ternary_expression
* e
);
1016 virtual void visit_assignment (assignment
* e
);
1017 virtual void visit_symbol (symbol
* e
);
1018 virtual void visit_target_symbol (target_symbol
* e
);
1019 virtual void visit_arrayindex (arrayindex
* e
);
1020 virtual void visit_functioncall (functioncall
* e
);
1021 virtual void visit_print_format (print_format
* e
);
1022 virtual void visit_stat_op (stat_op
* e
);
1023 virtual void visit_hist_op (hist_op
* e
);
1024 virtual void visit_cast_op (cast_op
* e
);
1025 virtual void visit_defined_op (defined_op
* e
);
1026 virtual void visit_entry_op (entry_op
* e
);
1027 virtual void visit_perf_op (perf_op
* e
);
1030 std::stack
<void *> targets
;
1033 template <> indexable
*
1034 update_visitor::require
<indexable
> (indexable
* src
, bool clearok
);
1036 // A visitor which performs a deep copy of the root node it's applied
1037 // to. NB: It does not copy any of the variable or function
1038 // declarations; those fields are set to NULL, assuming you want to
1039 // re-infer the declarations in a new context (the one you're copying
1042 struct deep_copy_visitor
: public update_visitor
1044 template <typename T
> static T
* deep_copy (T
* e
)
1046 deep_copy_visitor v
;
1047 return v
.require (e
);
1050 virtual void visit_block (block
*s
);
1051 virtual void visit_try_block (try_block
*s
);
1052 virtual void visit_embeddedcode (embeddedcode
*s
);
1053 virtual void visit_null_statement (null_statement
*s
);
1054 virtual void visit_expr_statement (expr_statement
*s
);
1055 virtual void visit_if_statement (if_statement
* s
);
1056 virtual void visit_for_loop (for_loop
* s
);
1057 virtual void visit_foreach_loop (foreach_loop
* s
);
1058 virtual void visit_return_statement (return_statement
* s
);
1059 virtual void visit_delete_statement (delete_statement
* s
);
1060 virtual void visit_next_statement (next_statement
* s
);
1061 virtual void visit_break_statement (break_statement
* s
);
1062 virtual void visit_continue_statement (continue_statement
* s
);
1063 virtual void visit_literal_string (literal_string
* e
);
1064 virtual void visit_literal_number (literal_number
* e
);
1065 virtual void visit_embedded_expr (embedded_expr
* e
);
1066 virtual void visit_binary_expression (binary_expression
* e
);
1067 virtual void visit_unary_expression (unary_expression
* e
);
1068 virtual void visit_pre_crement (pre_crement
* e
);
1069 virtual void visit_post_crement (post_crement
* e
);
1070 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1071 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1072 virtual void visit_array_in (array_in
* e
);
1073 virtual void visit_regex_query (regex_query
* e
);
1074 virtual void visit_comparison (comparison
* e
);
1075 virtual void visit_concatenation (concatenation
* e
);
1076 virtual void visit_ternary_expression (ternary_expression
* e
);
1077 virtual void visit_assignment (assignment
* e
);
1078 virtual void visit_symbol (symbol
* e
);
1079 virtual void visit_target_symbol (target_symbol
* e
);
1080 virtual void visit_arrayindex (arrayindex
* e
);
1081 virtual void visit_functioncall (functioncall
* e
);
1082 virtual void visit_print_format (print_format
* e
);
1083 virtual void visit_stat_op (stat_op
* e
);
1084 virtual void visit_hist_op (hist_op
* e
);
1085 virtual void visit_cast_op (cast_op
* e
);
1086 virtual void visit_defined_op (defined_op
* e
);
1087 virtual void visit_entry_op (entry_op
* e
);
1088 virtual void visit_perf_op (perf_op
* e
);
1091 #endif // STAPTREE_H
1093 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */