]> sourceware.org Git - systemtap.git/blob - staptree.h
Don't compile csclient.cxx and cscommon.cxx when HAVE_NSS is false.
[systemtap.git] / staptree.h
1 // -*- C++ -*-
2 // Copyright (C) 2005-2012 Red Hat Inc.
3 // Copyright (C) 2006 Intel Corporation.
4 //
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
8 // later version.
9
10 #ifndef STAPTREE_H
11 #define STAPTREE_H
12
13 #include <map>
14 #include <stack>
15 #include <set>
16 #include <string>
17 #include <vector>
18 #include <iostream>
19 #include <stdexcept>
20 #include <cassert>
21 extern "C" {
22 #include <stdint.h>
23 }
24
25 struct token; // parse.h
26 struct systemtap_session; // session.h
27
28 struct semantic_error: public std::runtime_error
29 {
30 const token* tok1;
31 const token* tok2;
32 const semantic_error *chain;
33
34 ~semantic_error () throw () {}
35 semantic_error (const std::string& msg, const token* t1=0):
36 runtime_error (msg), tok1 (t1), tok2 (0), chain (0) {}
37
38 semantic_error (const std::string& msg, const token* t1,
39 const token* t2):
40 runtime_error (msg), tok1 (t1), tok2 (t2), chain (0) {}
41 };
42
43 // ------------------------------------------------------------------------
44
45 /* struct statistic_decl moved to session.h */
46
47 // ------------------------------------------------------------------------
48
49 enum exp_type
50 {
51 pe_unknown,
52 pe_long, // int64_t
53 pe_string, // std::string
54 pe_stats
55 };
56
57 std::ostream& operator << (std::ostream& o, const exp_type& e);
58
59 struct token;
60 struct visitor;
61 struct update_visitor;
62
63 struct expression
64 {
65 exp_type type;
66 const token* tok;
67 expression ();
68 virtual ~expression ();
69 virtual void print (std::ostream& o) const = 0;
70 virtual void visit (visitor* u) = 0;
71 };
72
73 std::ostream& operator << (std::ostream& o, const expression& k);
74
75
76 struct literal: public expression
77 {
78 };
79
80
81 struct literal_string: public literal
82 {
83 std::string value;
84 literal_string (const std::string& v);
85 void print (std::ostream& o) const;
86 void visit (visitor* u);
87 };
88
89
90 struct literal_number: public literal
91 {
92 int64_t value;
93 bool print_hex;
94 literal_number (int64_t v, bool hex=false);
95 void print (std::ostream& o) const;
96 void visit (visitor* u);
97 };
98
99
100 struct embedded_expr: public expression
101 {
102 std::string code;
103 void print (std::ostream& o) const;
104 void visit (visitor* u);
105 };
106
107
108 struct binary_expression: public expression
109 {
110 expression* left;
111 std::string op;
112 expression* right;
113 void print (std::ostream& o) const;
114 void visit (visitor* u);
115 };
116
117
118 struct unary_expression: public expression
119 {
120 std::string op;
121 expression* operand;
122 void print (std::ostream& o) const;
123 void visit (visitor* u);
124 };
125
126
127 struct pre_crement: public unary_expression
128 {
129 void visit (visitor* u);
130 };
131
132
133 struct post_crement: public unary_expression
134 {
135 void print (std::ostream& o) const;
136 void visit (visitor* u);
137 };
138
139
140 struct logical_or_expr: public binary_expression
141 {
142 void visit (visitor* u);
143 };
144
145
146 struct logical_and_expr: public binary_expression
147 {
148 void visit (visitor* u);
149 };
150
151
152 struct arrayindex;
153 struct array_in: public expression
154 {
155 arrayindex* operand;
156 void print (std::ostream& o) const;
157 void visit (visitor* u);
158 };
159
160
161 struct comparison: public binary_expression
162 {
163 void visit (visitor* u);
164 };
165
166
167 struct concatenation: public binary_expression
168 {
169 void visit (visitor* u);
170 };
171
172
173 struct ternary_expression: public expression
174 {
175 expression* cond;
176 expression* truevalue;
177 expression* falsevalue;
178 void print (std::ostream& o) const;
179 void visit (visitor* u);
180 };
181
182
183 struct assignment: public binary_expression
184 {
185 void visit (visitor* u);
186 };
187
188 struct symbol;
189 struct hist_op;
190 struct indexable
191 {
192 // This is a helper class which, type-wise, acts as a disjoint union
193 // of symbols and histograms. You can ask it whether it's a
194 // histogram or a symbol, and downcast accordingly.
195 void print_indexable (std::ostream& o) const;
196 void visit_indexable (visitor* u);
197 virtual bool is_symbol(symbol *& sym_out);
198 virtual bool is_hist_op(hist_op *& hist_out);
199 virtual bool is_const_symbol(const symbol *& sym_out) const;
200 virtual bool is_const_hist_op(const hist_op *& hist_out) const;
201 virtual const token *get_tok() const = 0;
202 virtual ~indexable() {}
203 };
204
205 // Perform a downcast to one out-value and NULL the other, throwing an
206 // exception if neither downcast succeeds. This is (sadly) about the
207 // best we can accomplish in C++.
208 void
209 classify_indexable(indexable* ix,
210 symbol *& array_out,
211 hist_op *& hist_out);
212
213 void
214 classify_const_indexable(const indexable* ix,
215 symbol const *& array_out,
216 hist_op const *& hist_out);
217
218 class vardecl;
219 struct symbol:
220 public expression,
221 public indexable
222 {
223 std::string name;
224 vardecl *referent;
225 symbol ();
226 void print (std::ostream& o) const;
227 void visit (visitor* u);
228 // overrides of type 'indexable'
229 const token *get_tok() const;
230 bool is_const_symbol(const symbol *& sym_out) const;
231 bool is_symbol(symbol *& sym_out);
232 };
233
234
235 struct target_symbol: public symbol
236 {
237 enum component_type
238 {
239 comp_struct_member,
240 comp_literal_array_index,
241 comp_expression_array_index,
242 comp_pretty_print, // must be final
243 };
244
245 struct component
246 {
247 const token* tok;
248 component_type type;
249 std::string member; // comp_struct_member, comp_pretty_print
250 int64_t num_index; // comp_literal_array_index
251 expression* expr_index; // comp_expression_array_index
252
253 component(const token* t, const std::string& m, bool pprint=false):
254 tok(t),
255 type(pprint ? comp_pretty_print : comp_struct_member),
256 member(m), num_index(0), expr_index(0)
257 {}
258 component(const token* t, int64_t n):
259 tok(t), type(comp_literal_array_index), num_index(n),
260 expr_index(0) {}
261 component(const token* t, expression* e):
262 tok(t), type(comp_expression_array_index), num_index(0),
263 expr_index(e) {}
264 void print (std::ostream& o) const;
265 };
266
267 bool addressof;
268 std::string target_name;
269 std::string cu_name;
270 std::vector<component> components;
271 semantic_error* saved_conversion_error; // hand-made linked list
272 target_symbol(): addressof(false), saved_conversion_error (0) {}
273 std::string sym_name ();
274 void chain (const semantic_error& er);
275 void print (std::ostream& o) const;
276 void visit (visitor* u);
277 void visit_components (visitor* u);
278 void visit_components (update_visitor* u);
279 void assert_no_components(const std::string& tapset, bool pretty_ok=false);
280 };
281
282 std::ostream& operator << (std::ostream& o, const target_symbol::component& c);
283
284
285 struct cast_op: public target_symbol
286 {
287 expression *operand;
288 std::string type_name, module;
289 void print (std::ostream& o) const;
290 void visit (visitor* u);
291 };
292
293
294 struct defined_op: public expression
295 {
296 target_symbol *operand;
297 void print (std::ostream& o) const;
298 void visit (visitor* u);
299 };
300
301
302 struct entry_op: public expression
303 {
304 expression *operand;
305 void print (std::ostream& o) const;
306 void visit (visitor* u);
307 };
308
309
310 struct arrayindex: public expression
311 {
312 std::vector<expression*> indexes;
313 indexable *base;
314 arrayindex ();
315 void print (std::ostream& o) const;
316 void visit (visitor* u);
317 };
318
319
320 class functiondecl;
321 struct functioncall: public expression
322 {
323 std::string function;
324 std::vector<expression*> args;
325 functiondecl *referent;
326 functioncall ();
327 void print (std::ostream& o) const;
328 void visit (visitor* u);
329 };
330
331
332 struct print_format: public expression
333 {
334 bool print_to_stream;
335 bool print_with_format;
336 bool print_with_delim;
337 bool print_with_newline;
338 bool print_char;
339
340 // XXX match runtime/vsprintf.c's print_flag
341 // ... for use with number() & number_size()
342 enum format_flag
343 {
344 fmt_flag_zeropad = 1,
345 fmt_flag_sign = 2,
346 fmt_flag_plus = 4,
347 fmt_flag_space = 8,
348 fmt_flag_left = 16,
349 fmt_flag_special = 32,
350 fmt_flag_large = 64,
351 };
352
353 enum conversion_type
354 {
355 conv_unspecified,
356 conv_pointer,
357 conv_number,
358 conv_string,
359 conv_char,
360 conv_memory,
361 conv_memory_hex,
362 conv_literal,
363 conv_binary
364 };
365
366 enum width_type
367 {
368 width_unspecified,
369 width_static,
370 width_dynamic
371 };
372
373 enum precision_type
374 {
375 prec_unspecified,
376 prec_static,
377 prec_dynamic
378 };
379
380 struct format_component
381 {
382 unsigned long flags;
383 unsigned base;
384 unsigned width;
385 unsigned precision;
386 width_type widthtype;
387 precision_type prectype;
388 conversion_type type;
389 std::string literal_string;
390 bool is_empty() const
391 {
392 return flags == 0
393 && widthtype == width_unspecified
394 && prectype == prec_unspecified
395 && type == conv_unspecified
396 && literal_string.empty();
397 }
398 void clear()
399 {
400 flags = 0;
401 widthtype = width_unspecified;
402 prectype = prec_unspecified;
403 type = conv_unspecified;
404 literal_string.clear();
405 }
406 inline void set_flag(format_flag f) { flags |= f; }
407 inline bool test_flag(format_flag f) const { return flags & f; }
408 };
409
410 std::string raw_components;
411 std::vector<format_component> components;
412 format_component delimiter;
413 std::vector<expression*> args;
414 hist_op *hist;
415
416 static std::string components_to_string(std::vector<format_component> const & components);
417 static std::vector<format_component> string_to_components(std::string const & str);
418 static print_format* create(const token *t);
419
420 void print (std::ostream& o) const;
421 void visit (visitor* u);
422
423 private:
424 print_format(bool stream, bool format, bool delim, bool newline, bool _char):
425 print_to_stream(stream), print_with_format(format),
426 print_with_delim(delim), print_with_newline(newline),
427 print_char(_char), hist(NULL)
428 {}
429 };
430
431
432 enum stat_component_type
433 {
434 sc_average,
435 sc_count,
436 sc_sum,
437 sc_min,
438 sc_max,
439 };
440
441 struct stat_op: public expression
442 {
443 stat_component_type ctype;
444 expression* stat;
445 void print (std::ostream& o) const;
446 void visit (visitor* u);
447 };
448
449 enum histogram_type
450 {
451 hist_linear,
452 hist_log
453 };
454
455 struct hist_op: public indexable
456 {
457 const token* tok;
458 histogram_type htype;
459 expression* stat;
460 std::vector<int64_t> params;
461 void print (std::ostream& o) const;
462 void visit (visitor* u);
463 // overrides of type 'indexable'
464 const token *get_tok() const;
465 bool is_const_hist_op(const hist_op *& hist_out) const;
466 bool is_hist_op(hist_op *& hist_out);
467 };
468
469 // ------------------------------------------------------------------------
470
471
472 struct symboldecl // unique object per (possibly implicit)
473 // symbol declaration
474 {
475 const token* tok;
476 const token* systemtap_v_conditional; //checking systemtap compatibility
477 std::string name;
478 exp_type type;
479 symboldecl ();
480 virtual ~symboldecl ();
481 virtual void print (std::ostream &o) const = 0;
482 virtual void printsig (std::ostream &o) const = 0;
483 };
484
485
486 std::ostream& operator << (std::ostream& o, const symboldecl& k);
487
488
489 struct vardecl: public symboldecl
490 {
491 void print (std::ostream& o) const;
492 void printsig (std::ostream& o) const;
493 vardecl ();
494 void set_arity (int arity, const token* t);
495 bool compatible_arity (int a);
496 const token* arity_tok; // site where arity was first resolved
497 int arity; // -1: unknown; 0: scalar; >0: array
498 int maxsize; // upperbound on size for arrays
499 std::vector<exp_type> index_types; // for arrays only
500 literal *init; // for global scalars only
501 bool skip_init; // for probe locals only, don't init on entry
502 bool wrap;
503 };
504
505
506 struct vardecl_builtin: public vardecl
507 {
508 };
509
510
511 struct statement;
512 struct functiondecl: public symboldecl
513 {
514 std::vector<vardecl*> formal_args;
515 std::vector<vardecl*> locals;
516 std::vector<vardecl*> unused_locals;
517 statement* body;
518 bool synthetic;
519 functiondecl ();
520 void print (std::ostream& o) const;
521 void printsig (std::ostream& o) const;
522 void join (systemtap_session& s); // for synthetic functions only
523 };
524
525
526 // ------------------------------------------------------------------------
527
528
529 struct statement
530 {
531 virtual void print (std::ostream& o) const = 0;
532 virtual void visit (visitor* u) = 0;
533 const token* tok;
534 statement ();
535 statement (const token* tok);
536 virtual ~statement ();
537 };
538
539 std::ostream& operator << (std::ostream& o, const statement& k);
540
541
542 struct embeddedcode: public statement
543 {
544 std::string code;
545 void print (std::ostream& o) const;
546 void visit (visitor* u);
547 };
548
549
550 struct block: public statement
551 {
552 std::vector<statement*> statements;
553 void print (std::ostream& o) const;
554 void visit (visitor* u);
555 block () {}
556 block (statement* car, statement* cdr);
557 virtual ~block () {}
558 };
559
560
561 struct try_block: public statement
562 {
563 statement* try_block; // may be 0
564 statement* catch_block; // may be 0
565 symbol* catch_error_var; // may be 0
566 void print (std::ostream& o) const;
567 void visit (visitor* u);
568 };
569
570
571 struct expr_statement;
572 struct for_loop: public statement
573 {
574 expr_statement* init; // may be 0
575 expression* cond;
576 expr_statement* incr; // may be 0
577 statement* block;
578 void print (std::ostream& o) const;
579 void visit (visitor* u);
580 };
581
582
583 struct foreach_loop: public statement
584 {
585 // this part is a specialization of arrayindex
586 std::vector<symbol*> indexes;
587 indexable *base;
588 int sort_direction; // -1: decreasing, 0: none, 1: increasing
589 unsigned sort_column; // 0: value, 1..N: index
590 symbol* value; // optional iteration value
591 expression* limit; // optional iteration limit
592
593 statement* block;
594 void print (std::ostream& o) const;
595 void visit (visitor* u);
596 };
597
598
599 struct null_statement: public statement
600 {
601 void print (std::ostream& o) const;
602 void visit (visitor* u);
603 null_statement (const token* tok);
604 };
605
606
607 struct expr_statement: public statement
608 {
609 expression* value; // executed for side-effects
610 void print (std::ostream& o) const;
611 void visit (visitor* u);
612 };
613
614
615 struct if_statement: public statement
616 {
617 expression* condition;
618 statement* thenblock;
619 statement* elseblock; // may be 0
620 void print (std::ostream& o) const;
621 void visit (visitor* u);
622 };
623
624
625 struct return_statement: public expr_statement
626 {
627 void print (std::ostream& o) const;
628 void visit (visitor* u);
629 };
630
631
632 struct delete_statement: public expr_statement
633 {
634 void print (std::ostream& o) const;
635 void visit (visitor* u);
636 };
637
638
639 struct break_statement: public statement
640 {
641 void print (std::ostream& o) const;
642 void visit (visitor* u);
643 };
644
645
646 struct continue_statement: public statement
647 {
648 void print (std::ostream& o) const;
649 void visit (visitor* u);
650 };
651
652
653 struct next_statement: public statement
654 {
655 void print (std::ostream& o) const;
656 void visit (visitor* u);
657 };
658
659
660 struct probe;
661 struct derived_probe;
662 struct probe_alias;
663 struct embeddedcode;
664 struct stapfile
665 {
666 std::string name;
667 std::vector<probe*> probes;
668 std::vector<probe_alias*> aliases;
669 std::vector<functiondecl*> functions;
670 std::vector<vardecl*> globals;
671 std::vector<embeddedcode*> embeds;
672 std::string file_contents;
673 bool privileged;
674 stapfile (): file_contents (""),
675 privileged (false) {}
676 void print (std::ostream& o) const;
677 };
678
679
680 struct probe_point
681 {
682 struct component // XXX: sort of a restricted functioncall
683 {
684 std::string functor;
685 literal* arg; // optional
686 component ();
687 const token* tok; // points to component's functor
688 component(std::string const & f, literal * a = NULL);
689 };
690 std::vector<component*> components;
691 bool optional;
692 bool sufficient;
693 expression* condition;
694 void print (std::ostream& o, bool print_extras=true) const;
695 probe_point ();
696 probe_point(const probe_point& pp);
697 probe_point(std::vector<component*> const & comps);
698 std::string str(bool print_extras=true) const;
699 };
700
701 std::ostream& operator << (std::ostream& o, const probe_point& k);
702
703
704 struct probe
705 {
706 std::vector<probe_point*> locations;
707 statement* body;
708 const token* tok;
709 const token* systemtap_v_conditional; //checking systemtap compatibility
710 std::vector<vardecl*> locals;
711 std::vector<vardecl*> unused_locals;
712 static unsigned last_probeidx;
713 probe ();
714 probe (const probe& p, probe_point *l);
715 void print (std::ostream& o) const;
716 virtual void printsig (std::ostream &o) const;
717 virtual void collect_derivation_chain (std::vector<probe*> &probes_list);
718 virtual void collect_derivation_pp_chain (std::vector<probe_point*> &) {}
719 virtual const probe_alias *get_alias () const { return 0; }
720 virtual probe* create_alias(probe_point* l, probe_point* a);
721 virtual const probe* basest () const { return this; }
722 virtual const probe* almost_basest () const { return 0; }
723 virtual ~probe() {}
724 bool privileged;
725 std::string name;
726 };
727
728 struct probe_alias: public probe
729 {
730 probe_alias(std::vector<probe_point*> const & aliases);
731 std::vector<probe_point*> alias_names;
732 virtual void printsig (std::ostream &o) const;
733 bool epilogue_style;
734 };
735
736
737 // A derived visitor instance is used to visit the entire
738 // statement/expression tree.
739 struct visitor
740 {
741 // Machinery for differentiating lvalue visits from non-lvalue.
742 std::vector<expression *> active_lvalues;
743 bool is_active_lvalue(expression *e);
744 void push_active_lvalue(expression *e);
745 void pop_active_lvalue();
746
747 virtual ~visitor () {}
748 virtual void visit_block (block *s) = 0;
749 virtual void visit_try_block (try_block *s) = 0;
750 virtual void visit_embeddedcode (embeddedcode *s) = 0;
751 virtual void visit_null_statement (null_statement *s) = 0;
752 virtual void visit_expr_statement (expr_statement *s) = 0;
753 virtual void visit_if_statement (if_statement* s) = 0;
754 virtual void visit_for_loop (for_loop* s) = 0;
755 virtual void visit_foreach_loop (foreach_loop* s) = 0;
756 virtual void visit_return_statement (return_statement* s) = 0;
757 virtual void visit_delete_statement (delete_statement* s) = 0;
758 virtual void visit_next_statement (next_statement* s) = 0;
759 virtual void visit_break_statement (break_statement* s) = 0;
760 virtual void visit_continue_statement (continue_statement* s) = 0;
761 virtual void visit_literal_string (literal_string* e) = 0;
762 virtual void visit_literal_number (literal_number* e) = 0;
763 virtual void visit_embedded_expr (embedded_expr* e) = 0;
764 virtual void visit_binary_expression (binary_expression* e) = 0;
765 virtual void visit_unary_expression (unary_expression* e) = 0;
766 virtual void visit_pre_crement (pre_crement* e) = 0;
767 virtual void visit_post_crement (post_crement* e) = 0;
768 virtual void visit_logical_or_expr (logical_or_expr* e) = 0;
769 virtual void visit_logical_and_expr (logical_and_expr* e) = 0;
770 virtual void visit_array_in (array_in* e) = 0;
771 virtual void visit_comparison (comparison* e) = 0;
772 virtual void visit_concatenation (concatenation* e) = 0;
773 virtual void visit_ternary_expression (ternary_expression* e) = 0;
774 virtual void visit_assignment (assignment* e) = 0;
775 virtual void visit_symbol (symbol* e) = 0;
776 virtual void visit_target_symbol (target_symbol* e) = 0;
777 virtual void visit_arrayindex (arrayindex* e) = 0;
778 virtual void visit_functioncall (functioncall* e) = 0;
779 virtual void visit_print_format (print_format* e) = 0;
780 virtual void visit_stat_op (stat_op* e) = 0;
781 virtual void visit_hist_op (hist_op* e) = 0;
782 virtual void visit_cast_op (cast_op* e) = 0;
783 virtual void visit_defined_op (defined_op* e) = 0;
784 virtual void visit_entry_op (entry_op* e) = 0;
785 };
786
787
788 // A simple kind of visitor, which travels down to the leaves of the
789 // statement/expression tree, up to but excluding following vardecls
790 // and functioncalls.
791 struct traversing_visitor: public visitor
792 {
793 void visit_block (block *s);
794 void visit_try_block (try_block *s);
795 void visit_embeddedcode (embeddedcode *s);
796 void visit_null_statement (null_statement *s);
797 void visit_expr_statement (expr_statement *s);
798 void visit_if_statement (if_statement* s);
799 void visit_for_loop (for_loop* s);
800 void visit_foreach_loop (foreach_loop* s);
801 void visit_return_statement (return_statement* s);
802 void visit_delete_statement (delete_statement* s);
803 void visit_next_statement (next_statement* s);
804 void visit_break_statement (break_statement* s);
805 void visit_continue_statement (continue_statement* s);
806 void visit_literal_string (literal_string* e);
807 void visit_literal_number (literal_number* e);
808 void visit_embedded_expr (embedded_expr* e);
809 void visit_binary_expression (binary_expression* e);
810 void visit_unary_expression (unary_expression* e);
811 void visit_pre_crement (pre_crement* e);
812 void visit_post_crement (post_crement* e);
813 void visit_logical_or_expr (logical_or_expr* e);
814 void visit_logical_and_expr (logical_and_expr* e);
815 void visit_array_in (array_in* e);
816 void visit_comparison (comparison* e);
817 void visit_concatenation (concatenation* e);
818 void visit_ternary_expression (ternary_expression* e);
819 void visit_assignment (assignment* e);
820 void visit_symbol (symbol* e);
821 void visit_target_symbol (target_symbol* e);
822 void visit_arrayindex (arrayindex* e);
823 void visit_functioncall (functioncall* e);
824 void visit_print_format (print_format* e);
825 void visit_stat_op (stat_op* e);
826 void visit_hist_op (hist_op* e);
827 void visit_cast_op (cast_op* e);
828 void visit_defined_op (defined_op* e);
829 void visit_entry_op (entry_op* e);
830 };
831
832
833 // A kind of traversing visitor, which also follows function calls.
834 // It uses an internal set object to prevent infinite recursion.
835 struct functioncall_traversing_visitor: public traversing_visitor
836 {
837 std::set<functiondecl*> traversed;
838 functiondecl* current_function;
839 functioncall_traversing_visitor(): current_function(0) {}
840 void visit_functioncall (functioncall* e);
841 };
842
843
844 // A kind of traversing visitor, which also follows function calls,
845 // and stores the vardecl* referent of each variable read and/or
846 // written and other such sundry side-effect data. It's used by
847 // the elaboration-time optimizer pass.
848 struct varuse_collecting_visitor: public functioncall_traversing_visitor
849 {
850 systemtap_session& session;
851 std::set<vardecl*> read;
852 std::set<vardecl*> written;
853 std::set<vardecl*> used;
854 bool embedded_seen;
855 bool current_lvalue_read;
856 expression* current_lvalue;
857 expression* current_lrvalue;
858 varuse_collecting_visitor(systemtap_session& s):
859 session (s),
860 embedded_seen (false),
861 current_lvalue_read (false),
862 current_lvalue(0),
863 current_lrvalue(0) {}
864 void visit_embeddedcode (embeddedcode *s);
865 void visit_embedded_expr (embedded_expr *e);
866 void visit_try_block (try_block *s);
867 void visit_delete_statement (delete_statement *s);
868 void visit_print_format (print_format *e);
869 void visit_assignment (assignment *e);
870 void visit_arrayindex (arrayindex *e);
871 void visit_target_symbol (target_symbol *e);
872 void visit_symbol (symbol *e);
873 void visit_pre_crement (pre_crement *e);
874 void visit_post_crement (post_crement *e);
875 void visit_foreach_loop (foreach_loop *s);
876 void visit_cast_op (cast_op* e);
877 void visit_defined_op (defined_op* e);
878 void visit_entry_op (entry_op* e);
879
880 bool side_effect_free ();
881 bool side_effect_free_wrt (const std::set<vardecl*>& vars);
882 };
883
884
885
886 // A kind of visitor that throws an semantic_error exception
887 // whenever a non-overridden method is called.
888 struct throwing_visitor: public visitor
889 {
890 std::string msg;
891 throwing_visitor (const std::string& m);
892 throwing_visitor ();
893
894 virtual void throwone (const token* t);
895
896 void visit_block (block *s);
897 void visit_try_block (try_block *s);
898 void visit_embeddedcode (embeddedcode *s);
899 void visit_null_statement (null_statement *s);
900 void visit_expr_statement (expr_statement *s);
901 void visit_if_statement (if_statement* s);
902 void visit_for_loop (for_loop* s);
903 void visit_foreach_loop (foreach_loop* s);
904 void visit_return_statement (return_statement* s);
905 void visit_delete_statement (delete_statement* s);
906 void visit_next_statement (next_statement* s);
907 void visit_break_statement (break_statement* s);
908 void visit_continue_statement (continue_statement* s);
909 void visit_literal_string (literal_string* e);
910 void visit_literal_number (literal_number* e);
911 void visit_embedded_expr (embedded_expr* e);
912 void visit_binary_expression (binary_expression* e);
913 void visit_unary_expression (unary_expression* e);
914 void visit_pre_crement (pre_crement* e);
915 void visit_post_crement (post_crement* e);
916 void visit_logical_or_expr (logical_or_expr* e);
917 void visit_logical_and_expr (logical_and_expr* e);
918 void visit_array_in (array_in* e);
919 void visit_comparison (comparison* e);
920 void visit_concatenation (concatenation* e);
921 void visit_ternary_expression (ternary_expression* e);
922 void visit_assignment (assignment* e);
923 void visit_symbol (symbol* e);
924 void visit_target_symbol (target_symbol* e);
925 void visit_arrayindex (arrayindex* e);
926 void visit_functioncall (functioncall* e);
927 void visit_print_format (print_format* e);
928 void visit_stat_op (stat_op* e);
929 void visit_hist_op (hist_op* e);
930 void visit_cast_op (cast_op* e);
931 void visit_defined_op (defined_op* e);
932 void visit_entry_op (entry_op* e);
933 };
934
935 // A visitor similar to a traversing_visitor, but with the ability to rewrite
936 // parts of the tree through require/provide.
937
938 struct update_visitor: public visitor
939 {
940 template <typename T> T* require (T* src, bool clearok=false)
941 {
942 T* dst = NULL;
943 if (src != NULL)
944 {
945 src->visit(this);
946 assert(!targets.empty());
947 dst = static_cast<T*>(targets.top()); // XXX: danger will robinson: not typesafe!
948 targets.pop();
949 assert(clearok || dst);
950 }
951 return dst;
952 }
953
954 template <typename T> void provide (T* src)
955 {
956 targets.push(static_cast<void*>(src)); // XXX: not typesafe!
957 }
958
959 template <typename T> void replace (T*& src, bool clearok=false)
960 {
961 src = require(src, clearok);
962 }
963
964 virtual ~update_visitor() { assert(targets.empty()); }
965
966 virtual void visit_block (block *s);
967 virtual void visit_try_block (try_block *s);
968 virtual void visit_embeddedcode (embeddedcode *s);
969 virtual void visit_null_statement (null_statement *s);
970 virtual void visit_expr_statement (expr_statement *s);
971 virtual void visit_if_statement (if_statement* s);
972 virtual void visit_for_loop (for_loop* s);
973 virtual void visit_foreach_loop (foreach_loop* s);
974 virtual void visit_return_statement (return_statement* s);
975 virtual void visit_delete_statement (delete_statement* s);
976 virtual void visit_next_statement (next_statement* s);
977 virtual void visit_break_statement (break_statement* s);
978 virtual void visit_continue_statement (continue_statement* s);
979 virtual void visit_literal_string (literal_string* e);
980 virtual void visit_literal_number (literal_number* e);
981 virtual void visit_embedded_expr (embedded_expr* e);
982 virtual void visit_binary_expression (binary_expression* e);
983 virtual void visit_unary_expression (unary_expression* e);
984 virtual void visit_pre_crement (pre_crement* e);
985 virtual void visit_post_crement (post_crement* e);
986 virtual void visit_logical_or_expr (logical_or_expr* e);
987 virtual void visit_logical_and_expr (logical_and_expr* e);
988 virtual void visit_array_in (array_in* e);
989 virtual void visit_comparison (comparison* e);
990 virtual void visit_concatenation (concatenation* e);
991 virtual void visit_ternary_expression (ternary_expression* e);
992 virtual void visit_assignment (assignment* e);
993 virtual void visit_symbol (symbol* e);
994 virtual void visit_target_symbol (target_symbol* e);
995 virtual void visit_arrayindex (arrayindex* e);
996 virtual void visit_functioncall (functioncall* e);
997 virtual void visit_print_format (print_format* e);
998 virtual void visit_stat_op (stat_op* e);
999 virtual void visit_hist_op (hist_op* e);
1000 virtual void visit_cast_op (cast_op* e);
1001 virtual void visit_defined_op (defined_op* e);
1002 virtual void visit_entry_op (entry_op* e);
1003
1004 private:
1005 std::stack<void *> targets;
1006 };
1007
1008 template <> indexable*
1009 update_visitor::require <indexable> (indexable* src, bool clearok);
1010
1011 // A visitor which performs a deep copy of the root node it's applied
1012 // to. NB: It does not copy any of the variable or function
1013 // declarations; those fields are set to NULL, assuming you want to
1014 // re-infer the declarations in a new context (the one you're copying
1015 // to).
1016
1017 struct deep_copy_visitor: public update_visitor
1018 {
1019 template <typename T> static T* deep_copy (T* e)
1020 {
1021 deep_copy_visitor v;
1022 return v.require (e);
1023 }
1024
1025 virtual void visit_block (block *s);
1026 virtual void visit_try_block (try_block *s);
1027 virtual void visit_embeddedcode (embeddedcode *s);
1028 virtual void visit_null_statement (null_statement *s);
1029 virtual void visit_expr_statement (expr_statement *s);
1030 virtual void visit_if_statement (if_statement* s);
1031 virtual void visit_for_loop (for_loop* s);
1032 virtual void visit_foreach_loop (foreach_loop* s);
1033 virtual void visit_return_statement (return_statement* s);
1034 virtual void visit_delete_statement (delete_statement* s);
1035 virtual void visit_next_statement (next_statement* s);
1036 virtual void visit_break_statement (break_statement* s);
1037 virtual void visit_continue_statement (continue_statement* s);
1038 virtual void visit_literal_string (literal_string* e);
1039 virtual void visit_literal_number (literal_number* e);
1040 virtual void visit_embedded_expr (embedded_expr* e);
1041 virtual void visit_binary_expression (binary_expression* e);
1042 virtual void visit_unary_expression (unary_expression* e);
1043 virtual void visit_pre_crement (pre_crement* e);
1044 virtual void visit_post_crement (post_crement* e);
1045 virtual void visit_logical_or_expr (logical_or_expr* e);
1046 virtual void visit_logical_and_expr (logical_and_expr* e);
1047 virtual void visit_array_in (array_in* e);
1048 virtual void visit_comparison (comparison* e);
1049 virtual void visit_concatenation (concatenation* e);
1050 virtual void visit_ternary_expression (ternary_expression* e);
1051 virtual void visit_assignment (assignment* e);
1052 virtual void visit_symbol (symbol* e);
1053 virtual void visit_target_symbol (target_symbol* e);
1054 virtual void visit_arrayindex (arrayindex* e);
1055 virtual void visit_functioncall (functioncall* e);
1056 virtual void visit_print_format (print_format* e);
1057 virtual void visit_stat_op (stat_op* e);
1058 virtual void visit_hist_op (hist_op* e);
1059 virtual void visit_cast_op (cast_op* e);
1060 virtual void visit_defined_op (defined_op* e);
1061 virtual void visit_entry_op (entry_op* e);
1062 };
1063
1064 #endif // STAPTREE_H
1065
1066 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.083665 seconds and 5 git commands to generate.