2 // Copyright (C) 2005-2019 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 #include "stringtable.h"
31 struct token
; // parse.h
32 struct systemtap_session
; // session.h
34 struct semantic_error
: public std::runtime_error
40 // Extra details to explain the error or provide alternatives to the user.
41 // Each one printed after the main error message and tokens aligned on
42 // separate lines. Just push_back anything you want that better explains
43 // the error to the user (not meant for extra verbose developer messages).
44 std::vector
<std::string
> details
;
46 ~semantic_error () throw ()
52 semantic_error (const std::string
& src
, const std::string
& msg
, const token
* t1
=0):
53 runtime_error (msg
), tok1 (t1
), tok2 (0), errsrc (src
), chain (0) {}
55 semantic_error (const std::string
& src
, const std::string
& msg
, const token
* t1
,
56 const token
* t2
, const semantic_error
* chn
=0):
57 runtime_error (msg
), tok1 (t1
), tok2 (t2
), errsrc (src
), chain (0)
63 /* override copy-ctor to deep-copy chain */
64 semantic_error (const semantic_error
& other
):
65 runtime_error(other
), tok1(other
.tok1
), tok2(other
.tok2
),
66 errsrc(other
.errsrc
), details(other
.details
), chain (0)
69 set_chain(*other
.chain
);
72 std::string
errsrc_chain(void) const
74 return errsrc
+ (chain
? "|" + chain
->errsrc_chain() : "");
77 semantic_error
& set_chain(const semantic_error
& new_chain
)
81 chain
= new semantic_error(new_chain
);
85 const semantic_error
* get_chain(void) const
91 const semantic_error
* chain
;
94 // ------------------------------------------------------------------------
96 /* struct statistic_decl moved to session.h */
98 // ------------------------------------------------------------------------
104 pe_string
, // std::string
108 std::ostream
& operator << (std::ostream
& o
, const exp_type
& e
);
112 struct exp_type_details
114 virtual ~exp_type_details () {};
116 // A process-wide unique identifier; probably a pointer.
117 virtual uintptr_t id () const = 0;
118 bool operator==(const exp_type_details
& other
) const
119 { return id () == other
.id (); }
120 bool operator!=(const exp_type_details
& other
) const
121 { return !(*this == other
); }
123 // Expand this autocast_op into a function call
124 virtual bool expandable() const = 0;
125 virtual functioncall
*expand(autocast_op
* e
, bool lvalue
) = 0;
127 typedef std::shared_ptr
<exp_type_details
> exp_type_ptr
;
132 struct update_visitor
;
136 virtual ~visitable ();
140 struct expression
: public visitable
143 exp_type_ptr type_details
;
146 virtual ~expression ();
147 // NB: this pretty-printer function must generate such text that
148 // allows distinguishing operationally-different instances. During
149 // stap -p2/-p3, function bodies with identical pretty-printed
150 // bodies may be duplicate-eliminated. So print any relevant member
151 // variables somehow.
152 virtual void print (std::ostream
& o
) const = 0;
153 virtual void visit (visitor
* u
) = 0;
154 virtual bool is_symbol(symbol
*& sym_out
);
157 std::ostream
& operator << (std::ostream
& o
, const expression
& k
);
160 struct literal
: public expression
165 struct literal_string
: public literal
167 interned_string value
;
168 literal_string (interned_string v
);
169 void print (std::ostream
& o
) const;
170 void visit (visitor
* u
);
174 struct literal_number
: public literal
178 literal_number (int64_t v
, bool hex
=false);
179 void print (std::ostream
& o
) const;
180 void visit (visitor
* u
);
185 struct embedded_expr
: public expression
187 interned_string code
;
188 interned_string code_referents
;
189 std::vector
<vardecl
*> read_referents
; /* pragma:read:FOO */
190 std::vector
<vardecl
*> write_referents
; /* pragma:write:BAR */
191 bool tagged_p (const char *tag
) const;
192 bool tagged_p (const std::string
&tag
) const;
193 bool tagged_p (const interned_string
& tag
) const;
194 void print (std::ostream
& o
) const;
195 void visit (visitor
* u
);
199 struct binary_expression
: public expression
204 void print (std::ostream
& o
) const;
205 void visit (visitor
* u
);
209 struct unary_expression
: public expression
213 void print (std::ostream
& o
) const;
214 void visit (visitor
* u
);
218 struct pre_crement
: public unary_expression
220 void visit (visitor
* u
);
224 struct post_crement
: public unary_expression
226 void print (std::ostream
& o
) const;
227 void visit (visitor
* u
);
231 struct logical_or_expr
: public binary_expression
233 void visit (visitor
* u
);
237 struct logical_and_expr
: public binary_expression
239 void visit (visitor
* u
);
244 struct array_in
: public expression
247 void print (std::ostream
& o
) const;
248 void visit (visitor
* u
);
251 struct regex_query
: public expression
255 literal_string
* right
;
256 void visit (visitor
* u
);
257 void print (std::ostream
& o
) const;
260 struct compound_expression
: public binary_expression
262 compound_expression() { op
= ","; }
263 void visit (visitor
* u
);
266 struct comparison
: public binary_expression
268 void visit (visitor
* u
);
271 struct concatenation
: public binary_expression
273 void visit (visitor
* u
);
277 struct ternary_expression
: public expression
280 expression
* truevalue
;
281 expression
* falsevalue
;
282 void print (std::ostream
& o
) const;
283 void visit (visitor
* u
);
287 struct assignment
: public binary_expression
289 void visit (visitor
* u
);
293 struct indexable
: public expression
295 // This is a helper class which, type-wise, acts as a disjoint union
296 // of symbols and histograms. You can ask it whether it's a
297 // histogram or a symbol, and downcast accordingly.
298 virtual bool is_symbol(symbol
*& sym_out
);
299 virtual bool is_hist_op(hist_op
*& hist_out
);
300 virtual ~indexable() {}
303 // Perform a downcast to one out-value and NULL the other, throwing an
304 // exception if neither downcast succeeds. This is (sadly) about the
305 // best we can accomplish in C++.
307 classify_indexable(indexable
* ix
,
309 hist_op
*& hist_out
);
312 struct symbol
: public indexable
314 interned_string name
;
317 void print (std::ostream
& o
) const;
318 void visit (visitor
* u
);
319 // overrides of type 'indexable'
320 bool is_symbol(symbol
*& sym_out
);
323 struct target_register
: public expression
327 void print (std::ostream
& o
) const;
328 void visit (visitor
* u
);
331 struct target_deref
: public expression
337 void print (std::ostream
& o
) const;
338 void visit (visitor
* u
);
341 struct target_bitfield
: public expression
347 void print (std::ostream
& o
) const;
348 void visit (visitor
* u
);
351 struct target_symbol
: public expression
356 comp_literal_array_index
,
357 comp_expression_array_index
,
358 comp_pretty_print
, // must be final
365 std::string member
; // comp_struct_member, comp_pretty_print
366 int64_t num_index
; // comp_literal_array_index
367 expression
* expr_index
; // comp_expression_array_index
369 component(const token
* t
, const std::string
& m
, bool pprint
=false):
371 type(pprint
? comp_pretty_print
: comp_struct_member
),
372 member(m
), num_index(0), expr_index(0)
374 component(const token
* t
, int64_t n
):
375 tok(t
), type(comp_literal_array_index
), num_index(n
),
377 component(const token
* t
, expression
* e
):
378 tok(t
), type(comp_expression_array_index
), num_index(0),
380 void print (std::ostream
& o
) const;
383 interned_string name
;
386 std::vector
<component
> components
;
387 semantic_error
* saved_conversion_error
; // hand-made linked list
388 target_symbol(): addressof(false), synthetic(false), saved_conversion_error (0) {}
389 virtual std::string
sym_name ();
390 void chain (const semantic_error
& er
);
391 void print (std::ostream
& o
) const;
392 void visit (visitor
* u
);
393 void visit_components (visitor
* u
);
394 void visit_components (update_visitor
* u
);
395 void assert_no_components(const std::string
& tapset
, bool pretty_ok
=false);
396 size_t pretty_print_depth () const;
397 bool check_pretty_print (bool lvalue
=false) const;
400 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
);
403 struct cast_op
: public target_symbol
406 interned_string type_name
, module
;
407 void print (std::ostream
& o
) const;
408 void visit (visitor
* u
);
411 // An autocast is like an implicit @cast on any expression, like
412 // (expr)->foo->var[baz], and the type is gleaned from the expr.
413 struct autocast_op
: public target_symbol
416 void print (std::ostream
& o
) const;
417 void visit (visitor
* u
);
420 struct atvar_op
: public target_symbol
422 interned_string target_name
, cu_name
, module
;
423 virtual std::string
sym_name ();
424 void print (std::ostream
& o
) const;
425 void visit (visitor
* u
);
428 struct defined_op
: public expression
431 void print (std::ostream
& o
) const;
432 void visit (visitor
* u
);
435 struct entry_op
: public expression
438 void print (std::ostream
& o
) const;
439 void visit (visitor
* u
);
443 struct perf_op
: public expression
445 literal_string
*operand
;
446 void print (std::ostream
& o
) const;
447 void visit (visitor
* u
);
451 struct arrayindex
: public expression
453 std::vector
<expression
*> indexes
;
456 void print (std::ostream
& o
) const;
457 void visit (visitor
* u
);
462 struct functioncall
: public expression
464 interned_string function
;
465 std::vector
<expression
*> args
;
466 std::vector
<functiondecl
*> referents
;
469 void print (std::ostream
& o
) const;
470 void visit (visitor
* u
);
474 struct print_format
: public expression
476 bool print_to_stream
;
477 bool print_with_format
;
478 bool print_with_delim
;
479 bool print_with_newline
;
482 // XXX match runtime/vsprintf.c's print_flag
483 // ... for use with number() & number_size()
486 fmt_flag_zeropad
= 1,
491 fmt_flag_special
= 32,
522 struct format_component
528 width_type widthtype
: 8;
529 precision_type prectype
: 8;
530 conversion_type type
: 8;
531 interned_string literal_string
;
532 bool is_empty() const
535 && widthtype
== width_unspecified
536 && prectype
== prec_unspecified
537 && type
== conv_unspecified
538 && literal_string
.empty();
545 widthtype
= width_unspecified
;
547 prectype
= prec_unspecified
;
548 type
= conv_unspecified
;
549 literal_string
.clear();
551 format_component() { clear(); }
552 inline void set_flag(format_flag f
) { flags
|= f
; }
553 inline bool test_flag(format_flag f
) const { return flags
& f
; }
556 std::string raw_components
;
557 std::vector
<format_component
> components
;
558 interned_string delimiter
;
559 std::vector
<expression
*> args
;
563 static std::string
components_to_string(std::vector
<format_component
> const & components
);
564 static std::vector
<format_component
> string_to_components(std::string
const & str
);
565 static print_format
* create(const token
*t
, const char *n
= NULL
);
567 void print (std::ostream
& o
) const;
568 void visit (visitor
* u
);
571 interned_string print_format_type
;
572 print_format(bool stream
, bool format
, bool delim
, bool newline
, bool _char
, interned_string type
):
573 print_to_stream(stream
), print_with_format(format
),
574 print_with_delim(delim
), print_with_newline(newline
),
575 print_char(_char
), hist(NULL
), tag(false), print_format_type(type
)
580 enum stat_component_type
591 struct stat_op
: public expression
593 stat_component_type ctype
;
595 std::vector
<int64_t> params
;
596 void print (std::ostream
& o
) const;
597 void visit (visitor
* u
);
606 struct hist_op
: public indexable
608 histogram_type htype
;
610 std::vector
<int64_t> params
;
611 void print (std::ostream
& o
) const;
612 void visit (visitor
* u
);
613 // overrides of type 'indexable'
614 bool is_hist_op(hist_op
*& hist_out
);
617 // ------------------------------------------------------------------------
620 struct symboldecl
// unique object per (possibly implicit)
621 // symbol declaration
624 const token
* systemtap_v_conditional
; //checking systemtap compatibility
625 interned_string name
; // mangled name
626 interned_string unmangled_name
;
628 exp_type_ptr type_details
;
630 virtual ~symboldecl ();
631 virtual void print (std::ostream
&o
) const = 0;
632 virtual void printsig (std::ostream
&o
) const = 0;
636 std::ostream
& operator << (std::ostream
& o
, const symboldecl
& k
);
639 struct vardecl
: public symboldecl
641 void print (std::ostream
& o
) const;
642 void printsig (std::ostream
& o
) const;
644 void set_arity (int arity
, const token
* t
);
645 bool compatible_arity (int a
);
646 const token
* arity_tok
; // site where arity was first resolved
647 int arity
; // -1: unknown; 0: scalar; >0: array
648 int maxsize
; // upperbound on size for arrays
649 std::vector
<exp_type
> index_types
; // for arrays only
650 literal
*init
; // for global scalars only
651 bool synthetic
; // for probe locals only, don't init on entry; for globals, skip some checking
653 bool char_ptr_arg
; // set in ::emit_common_header(), only used if a formal_arg
657 struct vardecl_builtin
: public vardecl
661 struct bpf_context_vardecl
: public vardecl
669 struct functiondecl
: public symboldecl
671 std::vector
<vardecl
*> formal_args
;
672 std::vector
<vardecl
*> locals
;
673 std::vector
<vardecl
*> unused_locals
;
676 bool mangle_oldstyle
;
680 void print (std::ostream
& o
) const;
681 void printsig (std::ostream
& o
) const;
682 void printsigtags (std::ostream
& o
, bool all_tags
) const;
683 void join (systemtap_session
& s
); // for synthetic functions only
686 struct function_priority_order
688 bool operator() (const functiondecl
* f1
, const functiondecl
* f2
)
690 return f1
->priority
< f2
->priority
;
695 // ------------------------------------------------------------------------
698 struct statement
: public visitable
700 virtual void print (std::ostream
& o
) const = 0;
701 virtual void visit (visitor
* u
) = 0;
704 statement (const token
* tok
);
705 virtual ~statement ();
708 std::ostream
& operator << (std::ostream
& o
, const statement
& k
);
711 struct embeddedcode
: public statement
713 interned_string code
;
714 interned_string code_referents
;
715 std::vector
<vardecl
*> read_referents
; /* pragma:read:FOO */
716 std::vector
<vardecl
*> write_referents
; /* pragma:write:BAR */
717 bool tagged_p (const char *tag
) const;
718 bool tagged_p (const std::string
& tag
) const;
719 bool tagged_p (const interned_string
& tag
) const;
720 void print (std::ostream
& o
) const;
721 void visit (visitor
* u
);
725 struct block
: public statement
727 std::vector
<statement
*> statements
;
728 void print (std::ostream
& o
) const;
729 void visit (visitor
* u
);
731 block (statement
* car
, statement
* cdr
);
736 struct try_block
: public statement
738 statement
* try_block
; // may be 0
739 statement
* catch_block
; // may be 0
740 symbol
* catch_error_var
; // may be 0
741 void print (std::ostream
& o
) const;
742 void visit (visitor
* u
);
746 struct expr_statement
;
747 struct for_loop
: public statement
749 expr_statement
* init
; // may be 0
751 expr_statement
* incr
; // may be 0
753 void print (std::ostream
& o
) const;
754 void visit (visitor
* u
);
758 struct foreach_loop
: public statement
760 // this part is a specialization of arrayindex
761 std::vector
<symbol
*> indexes
;
762 std::vector
<expression
*> array_slice
; // optional array slice to iterate over
764 int sort_direction
; // -1: decreasing, 0: none, 1: increasing
765 unsigned sort_column
; // 0: value, 1..N: index
766 enum stat_component_type sort_aggr
; // for aggregate arrays, which aggregate to sort on
767 symbol
* value
; // optional iteration value
768 expression
* limit
; // optional iteration limit
771 void print (std::ostream
& o
) const;
772 void visit (visitor
* u
);
776 struct null_statement
: public statement
778 void print (std::ostream
& o
) const;
779 void visit (visitor
* u
);
780 null_statement (const token
* tok
);
784 struct expr_statement
: public statement
786 expression
* value
; // executed for side-effects
787 void print (std::ostream
& o
) const;
788 void visit (visitor
* u
);
792 struct if_statement
: public statement
794 expression
* condition
;
795 statement
* thenblock
;
796 statement
* elseblock
; // may be 0
797 void print (std::ostream
& o
) const;
798 void visit (visitor
* u
);
802 struct return_statement
: public expr_statement
804 void print (std::ostream
& o
) const;
805 void visit (visitor
* u
);
809 struct delete_statement
: public expr_statement
811 void print (std::ostream
& o
) const;
812 void visit (visitor
* u
);
816 struct break_statement
: public statement
818 void print (std::ostream
& o
) const;
819 void visit (visitor
* u
);
823 struct continue_statement
: public statement
825 void print (std::ostream
& o
) const;
826 void visit (visitor
* u
);
830 struct next_statement
: public statement
832 void print (std::ostream
& o
) const;
833 void visit (visitor
* u
);
838 struct derived_probe
;
844 std::vector
<probe
*> probes
;
845 std::vector
<probe_alias
*> aliases
;
846 std::vector
<functiondecl
*> functions
;
847 std::vector
<vardecl
*> globals
;
848 std::vector
<embeddedcode
*> embeds
;
849 interned_string file_contents
;
851 bool synthetic
; // via parse_synthetic_*
853 privileged (false), synthetic (false) {}
854 void print (std::ostream
& o
) const;
860 struct component
// XXX: sort of a restricted functioncall
862 interned_string functor
;
863 literal
* arg
; // optional
866 const token
* tok
; // points to component's functor
867 component(interned_string f
, literal
*a
=NULL
, bool from_glob
=false);
869 std::vector
<component
*> components
;
872 bool well_formed
; // used in derived_probe::script_location()
873 expression
* condition
;
874 std::string auto_path
;
875 void print (std::ostream
& o
, bool print_extras
=true) const;
877 probe_point(const probe_point
& pp
);
878 probe_point(std::vector
<component
*> const & comps
);
879 std::string
str(bool print_extras
=true) const;
880 bool from_globby_comp(const std::string
& comp
);
883 std::ostream
& operator << (std::ostream
& o
, const probe_point
& k
);
888 static unsigned last_probeidx
;
890 std::vector
<probe_point
*> locations
;
894 const token
* systemtap_v_conditional
; //checking systemtap compatibility
895 std::vector
<vardecl
*> locals
;
896 std::vector
<vardecl
*> unused_locals
;
902 probe (probe
* p
, probe_point
*l
);
903 void print (std::ostream
& o
) const;
904 std::string
name () const;
905 virtual void printsig (std::ostream
&o
) const;
906 virtual void collect_derivation_chain (std::vector
<probe
*> &probes_list
) const;
907 virtual void collect_derivation_pp_chain (std::vector
<probe_point
*> &) const;
908 virtual const probe_alias
*get_alias () const { return 0; }
909 virtual probe_point
*get_alias_loc () const { return 0; }
914 probe (const probe
&);
915 probe
& operator = (const probe
&);
918 struct probe_alias
: public probe
920 probe_alias(std::vector
<probe_point
*> const & aliases
);
921 std::vector
<probe_point
*> alias_names
;
922 virtual void printsig (std::ostream
&o
) const;
927 // A derived visitor instance is used to visit the entire
928 // statement/expression tree.
931 // Machinery for differentiating lvalue visits from non-lvalue.
932 std::vector
<expression
*> active_lvalues
;
933 bool is_active_lvalue(expression
*e
);
934 void push_active_lvalue(expression
*e
);
935 void pop_active_lvalue();
937 virtual ~visitor () {}
938 virtual void visit_block (block
*s
) = 0;
939 virtual void visit_try_block (try_block
*s
) = 0;
940 virtual void visit_embeddedcode (embeddedcode
*s
) = 0;
941 virtual void visit_null_statement (null_statement
*s
) = 0;
942 virtual void visit_expr_statement (expr_statement
*s
) = 0;
943 virtual void visit_if_statement (if_statement
* s
) = 0;
944 virtual void visit_for_loop (for_loop
* s
) = 0;
945 virtual void visit_foreach_loop (foreach_loop
* s
) = 0;
946 virtual void visit_return_statement (return_statement
* s
) = 0;
947 virtual void visit_delete_statement (delete_statement
* s
) = 0;
948 virtual void visit_next_statement (next_statement
* s
) = 0;
949 virtual void visit_break_statement (break_statement
* s
) = 0;
950 virtual void visit_continue_statement (continue_statement
* s
) = 0;
951 virtual void visit_literal_string (literal_string
* e
) = 0;
952 virtual void visit_literal_number (literal_number
* e
) = 0;
953 virtual void visit_embedded_expr (embedded_expr
* e
) = 0;
954 virtual void visit_binary_expression (binary_expression
* e
) = 0;
955 virtual void visit_unary_expression (unary_expression
* e
) = 0;
956 virtual void visit_pre_crement (pre_crement
* e
) = 0;
957 virtual void visit_post_crement (post_crement
* e
) = 0;
958 virtual void visit_logical_or_expr (logical_or_expr
* e
) = 0;
959 virtual void visit_logical_and_expr (logical_and_expr
* e
) = 0;
960 virtual void visit_array_in (array_in
* e
) = 0;
961 virtual void visit_regex_query (regex_query
* e
) = 0;
962 virtual void visit_compound_expression (compound_expression
* e
) = 0;
963 virtual void visit_comparison (comparison
* e
) = 0;
964 virtual void visit_concatenation (concatenation
* e
) = 0;
965 virtual void visit_ternary_expression (ternary_expression
* e
) = 0;
966 virtual void visit_assignment (assignment
* e
) = 0;
967 virtual void visit_symbol (symbol
* e
) = 0;
968 virtual void visit_target_register (target_register
* e
) = 0;
969 virtual void visit_target_deref (target_deref
* e
) = 0;
970 virtual void visit_target_bitfield (target_bitfield
* e
) = 0;
971 virtual void visit_target_symbol (target_symbol
* e
) = 0;
972 virtual void visit_arrayindex (arrayindex
* e
) = 0;
973 virtual void visit_functioncall (functioncall
* e
) = 0;
974 virtual void visit_print_format (print_format
* e
) = 0;
975 virtual void visit_stat_op (stat_op
* e
) = 0;
976 virtual void visit_hist_op (hist_op
* e
) = 0;
977 virtual void visit_cast_op (cast_op
* e
) = 0;
978 virtual void visit_autocast_op (autocast_op
* e
) = 0;
979 virtual void visit_atvar_op (atvar_op
* e
) = 0;
980 virtual void visit_defined_op (defined_op
* e
) = 0;
981 virtual void visit_entry_op (entry_op
* e
) = 0;
982 virtual void visit_perf_op (perf_op
* e
) = 0;
986 // A NOP visitor doesn't do anything. It's a useful base class if you need to
987 // take action only for specific types, without even traversing the rest.
988 struct nop_visitor
: public visitor
990 virtual ~nop_visitor () {}
991 virtual void visit_block (block
*) {};
992 virtual void visit_try_block (try_block
*) {};
993 virtual void visit_embeddedcode (embeddedcode
*) {};
994 virtual void visit_null_statement (null_statement
*) {};
995 virtual void visit_expr_statement (expr_statement
*) {};
996 virtual void visit_if_statement (if_statement
*) {};
997 virtual void visit_for_loop (for_loop
*) {};
998 virtual void visit_foreach_loop (foreach_loop
*) {};
999 virtual void visit_return_statement (return_statement
*) {};
1000 virtual void visit_delete_statement (delete_statement
*) {};
1001 virtual void visit_next_statement (next_statement
*) {};
1002 virtual void visit_break_statement (break_statement
*) {};
1003 virtual void visit_continue_statement (continue_statement
*) {};
1004 virtual void visit_literal_string (literal_string
*) {};
1005 virtual void visit_literal_number (literal_number
*) {};
1006 virtual void visit_embedded_expr (embedded_expr
*) {};
1007 virtual void visit_binary_expression (binary_expression
*) {};
1008 virtual void visit_unary_expression (unary_expression
*) {};
1009 virtual void visit_pre_crement (pre_crement
*) {};
1010 virtual void visit_post_crement (post_crement
*) {};
1011 virtual void visit_logical_or_expr (logical_or_expr
*) {};
1012 virtual void visit_logical_and_expr (logical_and_expr
*) {};
1013 virtual void visit_array_in (array_in
*) {};
1014 virtual void visit_regex_query (regex_query
*) {};
1015 virtual void visit_compound_expression (compound_expression
*) {};
1016 virtual void visit_comparison (comparison
*) {};
1017 virtual void visit_concatenation (concatenation
*) {};
1018 virtual void visit_ternary_expression (ternary_expression
*) {};
1019 virtual void visit_assignment (assignment
*) {};
1020 virtual void visit_symbol (symbol
*) {};
1021 virtual void visit_target_register (target_register
*) {};
1022 virtual void visit_target_deref (target_deref
*) {};
1023 virtual void visit_target_bitfield (target_bitfield
*) {};
1024 virtual void visit_target_symbol (target_symbol
*) {};
1025 virtual void visit_arrayindex (arrayindex
*) {};
1026 virtual void visit_functioncall (functioncall
*) {};
1027 virtual void visit_print_format (print_format
*) {};
1028 virtual void visit_stat_op (stat_op
*) {};
1029 virtual void visit_hist_op (hist_op
*) {};
1030 virtual void visit_cast_op (cast_op
*) {};
1031 virtual void visit_autocast_op (autocast_op
*) {};
1032 virtual void visit_atvar_op (atvar_op
*) {};
1033 virtual void visit_defined_op (defined_op
*) {};
1034 virtual void visit_entry_op (entry_op
*) {};
1035 virtual void visit_perf_op (perf_op
*) {};
1039 // A simple kind of visitor, which travels down to the leaves of the
1040 // statement/expression tree, up to but excluding following vardecls
1041 // and functioncalls.
1042 struct traversing_visitor
: public visitor
1044 void visit_block (block
*s
);
1045 void visit_try_block (try_block
*s
);
1046 void visit_embeddedcode (embeddedcode
*s
);
1047 void visit_null_statement (null_statement
*s
);
1048 void visit_expr_statement (expr_statement
*s
);
1049 void visit_if_statement (if_statement
* s
);
1050 void visit_for_loop (for_loop
* s
);
1051 void visit_foreach_loop (foreach_loop
* s
);
1052 void visit_return_statement (return_statement
* s
);
1053 void visit_delete_statement (delete_statement
* s
);
1054 void visit_next_statement (next_statement
* s
);
1055 void visit_break_statement (break_statement
* s
);
1056 void visit_continue_statement (continue_statement
* s
);
1057 void visit_literal_string (literal_string
* e
);
1058 void visit_literal_number (literal_number
* e
);
1059 void visit_embedded_expr (embedded_expr
* e
);
1060 void visit_binary_expression (binary_expression
* e
);
1061 void visit_unary_expression (unary_expression
* e
);
1062 void visit_pre_crement (pre_crement
* e
);
1063 void visit_post_crement (post_crement
* e
);
1064 void visit_logical_or_expr (logical_or_expr
* e
);
1065 void visit_logical_and_expr (logical_and_expr
* e
);
1066 void visit_array_in (array_in
* e
);
1067 void visit_regex_query (regex_query
* e
);
1068 void visit_compound_expression(compound_expression
* e
);
1069 void visit_comparison (comparison
* e
);
1070 void visit_concatenation (concatenation
* e
);
1071 void visit_ternary_expression (ternary_expression
* e
);
1072 void visit_assignment (assignment
* e
);
1073 void visit_symbol (symbol
* e
);
1074 void visit_target_register (target_register
* e
);
1075 void visit_target_deref (target_deref
* e
);
1076 void visit_target_bitfield (target_bitfield
* e
);
1077 void visit_target_symbol (target_symbol
* e
);
1078 void visit_arrayindex (arrayindex
* e
);
1079 void visit_functioncall (functioncall
* e
);
1080 void visit_print_format (print_format
* e
);
1081 void visit_stat_op (stat_op
* e
);
1082 void visit_hist_op (hist_op
* e
);
1083 void visit_cast_op (cast_op
* e
);
1084 void visit_autocast_op (autocast_op
* e
);
1085 void visit_atvar_op (atvar_op
* e
);
1086 void visit_defined_op (defined_op
* e
);
1087 void visit_entry_op (entry_op
* e
);
1088 void visit_perf_op (perf_op
* e
);
1092 // A visitor that calls a generic visit_expression on every expression.
1093 struct expression_visitor
: public traversing_visitor
1095 virtual void visit_expression(expression
*e
) = 0;
1097 void visit_literal_string (literal_string
* e
);
1098 void visit_literal_number (literal_number
* e
);
1099 void visit_embedded_expr (embedded_expr
* e
);
1100 void visit_binary_expression (binary_expression
* e
);
1101 void visit_unary_expression (unary_expression
* e
);
1102 void visit_pre_crement (pre_crement
* e
);
1103 void visit_post_crement (post_crement
* e
);
1104 void visit_logical_or_expr (logical_or_expr
* e
);
1105 void visit_logical_and_expr (logical_and_expr
* e
);
1106 void visit_array_in (array_in
* e
);
1107 void visit_regex_query (regex_query
* e
);
1108 void visit_compound_expression (compound_expression
* e
);
1109 void visit_comparison (comparison
* e
);
1110 void visit_concatenation (concatenation
* e
);
1111 void visit_ternary_expression (ternary_expression
* e
);
1112 void visit_assignment (assignment
* e
);
1113 void visit_symbol (symbol
* e
);
1114 void visit_target_register (target_register
* e
);
1115 void visit_target_deref (target_deref
* e
);
1116 void visit_target_bitfield (target_bitfield
* e
);
1117 void visit_target_symbol (target_symbol
* e
);
1118 void visit_arrayindex (arrayindex
* e
);
1119 void visit_functioncall (functioncall
* e
);
1120 void visit_print_format (print_format
* e
);
1121 void visit_stat_op (stat_op
* e
);
1122 void visit_hist_op (hist_op
* e
);
1123 void visit_cast_op (cast_op
* e
);
1124 void visit_autocast_op (autocast_op
* e
);
1125 void visit_atvar_op (atvar_op
* e
);
1126 void visit_defined_op (defined_op
* e
);
1127 void visit_entry_op (entry_op
* e
);
1128 void visit_perf_op (perf_op
* e
);
1132 // A kind of traversing visitor, which also follows function calls.
1133 // It uses an internal set object to prevent infinite recursion.
1134 struct functioncall_traversing_visitor
: public traversing_visitor
1136 std::set
<functiondecl
*> seen
;
1137 std::set
<functiondecl
*> nested
;
1138 functiondecl
* current_function
;
1139 functioncall_traversing_visitor(): current_function(0) {}
1140 void visit_functioncall (functioncall
* e
);
1141 void enter_functioncall (functioncall
* e
);
1142 virtual void note_recursive_functioncall (functioncall
* e
);
1146 // A kind of traversing visitor, which also follows function calls,
1147 // and stores the vardecl* referent of each variable read and/or
1148 // written and other such sundry side-effect data. It's used by
1149 // the elaboration-time optimizer pass.
1150 struct varuse_collecting_visitor
: public functioncall_traversing_visitor
1152 systemtap_session
& session
;
1153 std::set
<vardecl
*> read
;
1154 std::set
<vardecl
*> written
;
1155 std::set
<vardecl
*> used
;
1157 bool current_lvalue_read
;
1158 expression
* current_lvalue
;
1159 expression
* current_lrvalue
;
1160 varuse_collecting_visitor(systemtap_session
& s
):
1162 embedded_seen (false),
1163 current_lvalue_read (false),
1165 current_lrvalue(0) {}
1166 void visit_if_statement (if_statement
* s
);
1167 void visit_for_loop (for_loop
* s
);
1168 void visit_embeddedcode (embeddedcode
*s
);
1169 void visit_embedded_expr (embedded_expr
*e
);
1170 void visit_try_block (try_block
*s
);
1171 void visit_functioncall (functioncall
* e
);
1172 void visit_return_statement (return_statement
*s
);
1173 void visit_delete_statement (delete_statement
*s
);
1174 void visit_print_format (print_format
*e
);
1175 void visit_assignment (assignment
*e
);
1176 void visit_ternary_expression (ternary_expression
* e
);
1177 void visit_arrayindex (arrayindex
*e
);
1178 void visit_target_register (target_register
* e
);
1179 void visit_target_deref (target_deref
* e
);
1180 void visit_target_symbol (target_symbol
*e
);
1181 void visit_symbol (symbol
*e
);
1182 void visit_pre_crement (pre_crement
*e
);
1183 void visit_post_crement (post_crement
*e
);
1184 void visit_foreach_loop (foreach_loop
*s
);
1185 void visit_cast_op (cast_op
* e
);
1186 void visit_autocast_op (autocast_op
* e
);
1187 void visit_atvar_op (atvar_op
*e
);
1188 void visit_defined_op (defined_op
* e
);
1189 void visit_entry_op (entry_op
* e
);
1190 void visit_perf_op (perf_op
* e
);
1191 bool side_effect_free ();
1192 bool side_effect_free_wrt (const std::set
<vardecl
*>& vars
);
1197 // A kind of visitor that throws an semantic_error exception
1198 // whenever a non-overridden method is called.
1199 struct throwing_visitor
: public visitor
1202 throwing_visitor (const std::string
& m
);
1203 throwing_visitor ();
1205 virtual void throwone (const token
* t
);
1207 void visit_block (block
*s
);
1208 void visit_try_block (try_block
*s
);
1209 void visit_embeddedcode (embeddedcode
*s
);
1210 void visit_null_statement (null_statement
*s
);
1211 void visit_expr_statement (expr_statement
*s
);
1212 void visit_if_statement (if_statement
* s
);
1213 void visit_for_loop (for_loop
* s
);
1214 void visit_foreach_loop (foreach_loop
* s
);
1215 void visit_return_statement (return_statement
* s
);
1216 void visit_delete_statement (delete_statement
* s
);
1217 void visit_next_statement (next_statement
* s
);
1218 void visit_break_statement (break_statement
* s
);
1219 void visit_continue_statement (continue_statement
* s
);
1220 void visit_literal_string (literal_string
* e
);
1221 void visit_literal_number (literal_number
* e
);
1222 void visit_embedded_expr (embedded_expr
* e
);
1223 void visit_binary_expression (binary_expression
* e
);
1224 void visit_unary_expression (unary_expression
* e
);
1225 void visit_pre_crement (pre_crement
* e
);
1226 void visit_post_crement (post_crement
* e
);
1227 void visit_logical_or_expr (logical_or_expr
* e
);
1228 void visit_logical_and_expr (logical_and_expr
* e
);
1229 void visit_array_in (array_in
* e
);
1230 void visit_regex_query (regex_query
* e
);
1231 void visit_compound_expression (compound_expression
* e
);
1232 void visit_comparison (comparison
* e
);
1233 void visit_concatenation (concatenation
* e
);
1234 void visit_ternary_expression (ternary_expression
* e
);
1235 void visit_assignment (assignment
* e
);
1236 void visit_symbol (symbol
* e
);
1237 void visit_target_register (target_register
* e
);
1238 void visit_target_deref (target_deref
* e
);
1239 void visit_target_bitfield (target_bitfield
* e
);
1240 void visit_target_symbol (target_symbol
* e
);
1241 void visit_arrayindex (arrayindex
* e
);
1242 void visit_functioncall (functioncall
* e
);
1243 void visit_print_format (print_format
* e
);
1244 void visit_stat_op (stat_op
* e
);
1245 void visit_hist_op (hist_op
* e
);
1246 void visit_cast_op (cast_op
* e
);
1247 void visit_autocast_op (autocast_op
* e
);
1248 void visit_atvar_op (atvar_op
* e
);
1249 void visit_defined_op (defined_op
* e
);
1250 void visit_entry_op (entry_op
* e
);
1251 void visit_perf_op (perf_op
* e
);
1254 // A visitor similar to a traversing_visitor, but with the ability to rewrite
1255 // parts of the tree through require/provide.
1257 // The relaxed_p member variable is cleared if an update occurred.
1259 struct update_visitor
: public visitor
1261 template <typename T
> T
* require (T
* src
, bool clearok
=false)
1268 throw std::runtime_error(_("update_visitor wasn't provided a value"));
1269 visitable
*v
= values
.top();
1271 if (v
== NULL
&& !clearok
)
1272 throw std::runtime_error(_("update_visitor was provided a NULL value"));
1273 dst
= dynamic_cast<T
*>(v
);
1274 if (v
!= NULL
&& dst
== NULL
)
1275 throw std::runtime_error(_F("update_visitor can't set type \"%s\" with a \"%s\"",
1276 typeid(T
).name(), typeid(*v
).name()));
1281 template <typename T
> void provide (T
* src
)
1286 template <typename T
> void abort_provide (T
* src
)
1292 template <typename T
> void replace (T
*& src
, bool clearok
=false)
1296 const T
* old_src
= src
;
1297 T
* new_src
= require(src
, clearok
);
1298 if (old_src
!= new_src
)
1300 if (this->verbose
> 3)
1302 std::clog
<< _("replaced ");
1303 if (old_src
) old_src
->print(std::clog
); else std::clog
<< "0";
1304 std::clog
<< _(" with ");
1305 if (new_src
) new_src
->print(std::clog
); else std::clog
<< "0";
1306 std::clog
<< std::endl
;
1314 update_visitor(unsigned v
= 0): verbose(v
), aborted_p(false), relaxed_p(true) {}
1315 virtual ~update_visitor() { assert(values
.empty()); }
1317 // Permit reuse of the visitor object.
1318 virtual void reset() { aborted_p
= false; relaxed_p
= true; }
1319 virtual bool relaxed() { return relaxed_p
; }
1321 virtual void visit_block (block
*s
);
1322 virtual void visit_try_block (try_block
*s
);
1323 virtual void visit_embeddedcode (embeddedcode
*s
);
1324 virtual void visit_null_statement (null_statement
*s
);
1325 virtual void visit_expr_statement (expr_statement
*s
);
1326 virtual void visit_if_statement (if_statement
* s
);
1327 virtual void visit_for_loop (for_loop
* s
);
1328 virtual void visit_foreach_loop (foreach_loop
* s
);
1329 virtual void visit_return_statement (return_statement
* s
);
1330 virtual void visit_delete_statement (delete_statement
* s
);
1331 virtual void visit_next_statement (next_statement
* s
);
1332 virtual void visit_break_statement (break_statement
* s
);
1333 virtual void visit_continue_statement (continue_statement
* s
);
1334 virtual void visit_literal_string (literal_string
* e
);
1335 virtual void visit_literal_number (literal_number
* e
);
1336 virtual void visit_embedded_expr (embedded_expr
* e
);
1337 virtual void visit_binary_expression (binary_expression
* e
);
1338 virtual void visit_unary_expression (unary_expression
* e
);
1339 virtual void visit_pre_crement (pre_crement
* e
);
1340 virtual void visit_post_crement (post_crement
* e
);
1341 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1342 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1343 virtual void visit_array_in (array_in
* e
);
1344 virtual void visit_regex_query (regex_query
* e
);
1345 virtual void visit_compound_expression (compound_expression
* e
);
1346 virtual void visit_comparison (comparison
* e
);
1347 virtual void visit_concatenation (concatenation
* e
);
1348 virtual void visit_ternary_expression (ternary_expression
* e
);
1349 virtual void visit_assignment (assignment
* e
);
1350 virtual void visit_symbol (symbol
* e
);
1351 virtual void visit_target_register (target_register
* e
);
1352 virtual void visit_target_deref (target_deref
* e
);
1353 virtual void visit_target_bitfield (target_bitfield
* e
);
1354 virtual void visit_target_symbol (target_symbol
* e
);
1355 virtual void visit_arrayindex (arrayindex
* e
);
1356 virtual void visit_functioncall (functioncall
* e
);
1357 virtual void visit_print_format (print_format
* e
);
1358 virtual void visit_stat_op (stat_op
* e
);
1359 virtual void visit_hist_op (hist_op
* e
);
1360 virtual void visit_cast_op (cast_op
* e
);
1361 virtual void visit_autocast_op (autocast_op
* e
);
1362 virtual void visit_atvar_op (atvar_op
* e
);
1363 virtual void visit_defined_op (defined_op
* e
);
1364 virtual void visit_entry_op (entry_op
* e
);
1365 virtual void visit_perf_op (perf_op
* e
);
1368 std::stack
<visitable
*> values
;
1375 // A visitor which performs a deep copy of the root node it's applied
1376 // to. NB: It does not copy any of the variable or function
1377 // declarations; those fields are set to NULL, assuming you want to
1378 // re-infer the declarations in a new context (the one you're copying
1381 struct deep_copy_visitor
: public update_visitor
1383 template <typename T
> static T
* deep_copy (T
* e
)
1385 deep_copy_visitor v
;
1386 return v
.require (e
);
1389 virtual void visit_block (block
*s
);
1390 virtual void visit_try_block (try_block
*s
);
1391 virtual void visit_embeddedcode (embeddedcode
*s
);
1392 virtual void visit_null_statement (null_statement
*s
);
1393 virtual void visit_expr_statement (expr_statement
*s
);
1394 virtual void visit_if_statement (if_statement
* s
);
1395 virtual void visit_for_loop (for_loop
* s
);
1396 virtual void visit_foreach_loop (foreach_loop
* s
);
1397 virtual void visit_return_statement (return_statement
* s
);
1398 virtual void visit_delete_statement (delete_statement
* s
);
1399 virtual void visit_next_statement (next_statement
* s
);
1400 virtual void visit_break_statement (break_statement
* s
);
1401 virtual void visit_continue_statement (continue_statement
* s
);
1402 virtual void visit_literal_string (literal_string
* e
);
1403 virtual void visit_literal_number (literal_number
* e
);
1404 virtual void visit_embedded_expr (embedded_expr
* e
);
1405 virtual void visit_binary_expression (binary_expression
* e
);
1406 virtual void visit_unary_expression (unary_expression
* e
);
1407 virtual void visit_pre_crement (pre_crement
* e
);
1408 virtual void visit_post_crement (post_crement
* e
);
1409 virtual void visit_logical_or_expr (logical_or_expr
* e
);
1410 virtual void visit_logical_and_expr (logical_and_expr
* e
);
1411 virtual void visit_array_in (array_in
* e
);
1412 virtual void visit_regex_query (regex_query
* e
);
1413 virtual void visit_compound_expression(compound_expression
* e
);
1414 virtual void visit_comparison (comparison
* e
);
1415 virtual void visit_concatenation (concatenation
* e
);
1416 virtual void visit_ternary_expression (ternary_expression
* e
);
1417 virtual void visit_assignment (assignment
* e
);
1418 virtual void visit_symbol (symbol
* e
);
1419 virtual void visit_target_register (target_register
* e
);
1420 virtual void visit_target_deref (target_deref
* e
);
1421 virtual void visit_target_bitfield (target_bitfield
* e
);
1422 virtual void visit_target_symbol (target_symbol
* e
);
1423 virtual void visit_arrayindex (arrayindex
* e
);
1424 virtual void visit_functioncall (functioncall
* e
);
1425 virtual void visit_print_format (print_format
* e
);
1426 virtual void visit_stat_op (stat_op
* e
);
1427 virtual void visit_hist_op (hist_op
* e
);
1428 virtual void visit_cast_op (cast_op
* e
);
1429 virtual void visit_autocast_op (autocast_op
* e
);
1430 virtual void visit_atvar_op (atvar_op
* e
);
1431 virtual void visit_defined_op (defined_op
* e
);
1432 virtual void visit_entry_op (entry_op
* e
);
1433 virtual void visit_perf_op (perf_op
* e
);
1436 struct embedded_tags_visitor
: public traversing_visitor
1438 std::set
<interned_string
> available_tags
;
1439 std::set
<interned_string
> tags
; // set of the tags that appear in the code
1440 embedded_tags_visitor(bool all_tags
);
1441 void visit_embeddedcode (embeddedcode
*s
);
1442 void visit_embedded_expr (embedded_expr
*e
);
1445 #endif // STAPTREE_H
1447 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */