1 // parse tree functions
2 // Copyright (C) 2005-2014 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
28 visitable::~visitable ()
33 expression::expression ():
34 type (pe_unknown
), tok (0)
39 expression::~expression ()
44 statement::statement ():
50 statement::statement (const token
* tok
):
56 null_statement::null_statement (const token
* tok
):
62 statement::~statement ()
73 arrayindex::arrayindex ():
79 functioncall::functioncall ():
85 symboldecl::symboldecl ():
86 tok (0), systemtap_v_conditional (0),
92 symboldecl::~symboldecl ()
96 probe_point::probe_point (std::vector
<component
*> const & comps
):
97 components(comps
), optional (false), sufficient (false),
98 from_glob (false), well_formed (false), condition (0)
102 // NB: shallow-copy of compoonents & condition!
103 probe_point::probe_point (const probe_point
& pp
):
104 components(pp
.components
), optional (pp
.optional
), sufficient (pp
.sufficient
),
105 from_glob (pp
.from_glob
), well_formed (pp
.well_formed
),
106 condition (pp
.condition
)
111 probe_point::probe_point ():
112 optional (false), sufficient (false), from_glob (false),
113 well_formed (false), condition (0)
118 unsigned probe::last_probeidx
= 0;
121 body (0), base (0), tok (0), systemtap_v_conditional (0), privileged (false)
123 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
127 // Copy constructor, but with overriding probe-point. To be used when
128 // mapping script-level probe points to another one, early during pass
129 // 2. There should be no symbol resolution done yet.
130 probe::probe(probe
* p
, probe_point
* l
)
133 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
135 this->locations
.push_back(l
);
136 this->body
= deep_copy_visitor::deep_copy(p
->body
);
137 this->privileged
= p
->privileged
;
138 this->systemtap_v_conditional
= p
->systemtap_v_conditional
;
139 assert (p
->locals
.size() == 0);
140 assert (p
->unused_locals
.size() == 0);
144 probe_point::component::component ():
150 probe_point::component::component (std::string
const & f
, literal
* a
):
151 functor(f
), arg(a
), tok(0)
157 arity_tok(0), arity (-1), maxsize(0), init(NULL
), synthetic(false), wrap(false),
164 vardecl::set_arity (int a
, const token
* t
)
169 if (a
== 0 && maxsize
> 0)
170 throw SEMANTIC_ERROR (_("inconsistent arity"), tok
);
172 if (arity
!= a
&& arity
>= 0)
174 semantic_error
err (ERR_SRC
, _F("inconsistent arity (%s vs %d)",
175 lex_cast(arity
).c_str(), a
), t
?:tok
);
178 semantic_error
chain(ERR_SRC
, _F("arity %s first inferred here",
179 lex_cast(arity
).c_str()), arity_tok
);
180 err
.set_chain(chain
);
189 index_types
.resize (arity
);
190 for (int i
=0; i
<arity
; i
++)
191 index_types
[i
] = pe_unknown
;
196 vardecl::compatible_arity (int a
)
198 if (a
== 0 && maxsize
> 0)
200 if (arity
== -1 || a
== -1)
206 functiondecl::functiondecl ():
207 body (0), synthetic (false), mangle_oldstyle (false)
212 functiondecl::join (systemtap_session
& s
)
215 throw SEMANTIC_ERROR (_("internal error, joining a non-synthetic function"), tok
);
216 if (!s
.functions
.insert (make_pair (name
, this)).second
)
217 throw SEMANTIC_ERROR (_F("synthetic function '%s' conflicts with an existing function",
219 tok
->location
.file
->functions
.push_back (this);
223 literal_number::literal_number (int64_t v
, bool hex
)
231 literal_string::literal_string (const string
& v
)
239 operator << (ostream
& o
, const exp_type
& e
)
243 case pe_unknown
: o
<< "unknown"; break;
244 case pe_long
: o
<< "long"; break;
245 case pe_string
: o
<< "string"; break;
246 case pe_stats
: o
<< "stats"; break;
247 default: o
<< "???"; break;
254 target_symbol::assert_no_components(const std::string
& tapset
, bool pretty_ok
)
256 if (components
.empty())
259 switch (components
[0].type
)
261 case comp_literal_array_index
:
262 case comp_expression_array_index
:
263 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as array",
264 tapset
.c_str(), name
.c_str()), components
[0].tok
);
265 case comp_struct_member
:
266 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as a structure",
267 tapset
.c_str(), name
.c_str()), components
[0].tok
);
268 case comp_pretty_print
:
270 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be pretty-printed",
271 tapset
.c_str(), name
.c_str()), components
[0].tok
);
274 throw SEMANTIC_ERROR (_F("invalid use of %s variable '%s'",
275 tapset
.c_str(), name
.c_str()), components
[0].tok
);
281 target_symbol::pretty_print_depth () const
283 if (! components
.empty ())
285 const component
& last
= components
.back ();
286 if (last
.type
== comp_pretty_print
)
287 return last
.member
.length ();
294 target_symbol::check_pretty_print (bool lvalue
) const
296 if (pretty_print_depth () == 0)
300 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), tok
);
306 void target_symbol::chain (const semantic_error
&er
)
308 semantic_error
* e
= new semantic_error(er
);
311 assert (e
->get_chain() == 0);
312 if (this->saved_conversion_error
)
314 e
->set_chain(*this->saved_conversion_error
);
315 delete this->saved_conversion_error
;
317 this->saved_conversion_error
= e
;
321 string
target_symbol::sym_name ()
323 return name
.substr(1);
327 string
atvar_op::sym_name ()
332 return target_name
.substr(0, target_name
.length() - cu_name
.length() - 1);
336 // ------------------------------------------------------------------------
337 // parse tree printing
339 ostream
& operator << (ostream
& o
, const expression
& k
)
346 void literal_string::print (ostream
& o
) const
349 for (unsigned i
=0; i
<value
.size(); i
++)
350 if (value
[i
] == '"') // or other escapeworthy characters?
358 void literal_number::print (ostream
& o
) const
361 o
<< hex
<< showbase
;
364 o
<< dec
<< noshowbase
;
368 void embedded_expr::print (ostream
& o
) const
370 o
<< "%{ " << code
<< " %}";
374 void binary_expression::print (ostream
& o
) const
376 o
<< "(" << *left
<< ") "
378 << " (" << *right
<< ")";
381 void regex_query::print (ostream
& o
) const
383 // NB: we need a custom printer, because the parser does not accept
384 // a parenthesized RHS.
385 o
<< "(" << *left
<< ") "
391 void unary_expression::print (ostream
& o
) const
393 o
<< op
<< '(' << *operand
<< ")";
396 void array_in::print (ostream
& o
) const
399 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
401 if (i
> 0) o
<< ", ";
402 if (operand
->indexes
[i
])
403 operand
->indexes
[i
]->print (o
);
408 operand
->base
->print (o
);
411 void post_crement::print (ostream
& o
) const
413 o
<< '(' << *operand
<< ")" << op
;
417 void ternary_expression::print (ostream
& o
) const
419 o
<< "(" << *cond
<< ")?("
420 << *truevalue
<< "):("
421 << *falsevalue
<< ")";
425 void symbol::print (ostream
& o
) const
431 void target_symbol::component::print (ostream
& o
) const
435 case comp_pretty_print
:
436 case comp_struct_member
:
439 case comp_literal_array_index
:
440 o
<< '[' << num_index
<< ']';
442 case comp_expression_array_index
:
443 o
<< '[' << *expr_index
<< ']';
449 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
456 void target_symbol::print (ostream
& o
) const
461 for (unsigned i
= 0; i
< components
.size(); ++i
)
466 void atvar_op::print (ostream
& o
) const
470 o
<< name
<< "(\"" << target_name
<< "\")";
471 for (unsigned i
= 0; i
< components
.size(); ++i
)
476 void cast_op::print (ostream
& o
) const
480 o
<< name
<< '(' << *operand
;
481 o
<< ", " << lex_cast_qstring (type_name
);
482 if (module
.length() > 0)
483 o
<< ", " << lex_cast_qstring (module
);
485 for (unsigned i
= 0; i
< components
.size(); ++i
)
490 void autocast_op::print (ostream
& o
) const
494 o
<< '(' << *operand
<< ')';
495 for (unsigned i
= 0; i
< components
.size(); ++i
)
500 void defined_op::print (ostream
& o
) const
502 o
<< "@defined(" << *operand
<< ")";
506 void entry_op::print (ostream
& o
) const
508 o
<< "@entry(" << *operand
<< ")";
512 void perf_op::print (ostream
& o
) const
514 o
<< "@perf(" << *operand
<< ")";
518 void vardecl::print (ostream
& o
) const
524 o
<< "[" << maxsize
<< "]";
525 if (arity
> 0 || index_types
.size() > 0)
535 void vardecl::printsig (ostream
& o
) const
541 o
<< "[" << maxsize
<< "]";
543 if (index_types
.size() > 0)
546 for (unsigned i
=0; i
<index_types
.size(); i
++)
547 o
<< (i
>0 ? ", " : "") << index_types
[i
];
553 void functiondecl::print (ostream
& o
) const
555 o
<< "function " << name
<< " (";
556 for (unsigned i
=0; i
<formal_args
.size(); i
++)
557 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
563 void functiondecl::printsig (ostream
& o
) const
565 o
<< name
<< ":" << type
<< " (";
566 for (unsigned i
=0; i
<formal_args
.size(); i
++)
567 o
<< (i
>0 ? ", " : "")
570 << formal_args
[i
]->type
;
574 embedded_tags_visitor::embedded_tags_visitor(bool all_tags
)
576 // populate the set of tags that could appear in embedded code/expressions
577 available_tags
.insert("/* guru */");
578 available_tags
.insert("/* unprivileged */");
579 available_tags
.insert("/* myproc-unprivileged */");
582 available_tags
.insert("/* pure */");
583 available_tags
.insert("/* unmangled */");
584 available_tags
.insert("/* unmodified-fnargs */");
588 bool embedded_tags_visitor::tagged_p (const std::string
& tag
)
590 return tags
.count(tag
);
593 void embedded_tags_visitor::find_tags_in_code (const string
& s
)
595 set
<string
>::iterator tag
;
596 for (tag
= available_tags
.begin(); tag
!= available_tags
.end(); ++tag
)
597 if(s
.find(*tag
) != string::npos
)
601 void embedded_tags_visitor::visit_embeddedcode (embeddedcode
*s
)
603 find_tags_in_code(s
->code
);
606 void embedded_tags_visitor::visit_embedded_expr (embedded_expr
*e
)
608 find_tags_in_code(e
->code
);
611 void functiondecl::printsigtags (ostream
& o
, bool all_tags
) const
615 // Visit the function's body to see if there's any embedded_code or
616 // embeddedexpr that have special tags (e.g. /* guru */)
617 embedded_tags_visitor
etv(all_tags
);
618 this->body
->visit(&etv
);
620 set
<string
, bool>::const_iterator tag
;
621 for (tag
= etv
.available_tags
.begin(); tag
!= etv
.available_tags
.end(); ++tag
)
622 if (etv
.tagged_p(*tag
))
626 void arrayindex::print (ostream
& o
) const
630 for (unsigned i
=0; i
<indexes
.size(); i
++)
632 o
<< (i
>0 ? ", " : "");
642 void functioncall::print (ostream
& o
) const
644 o
<< function
<< "(";
645 for (unsigned i
=0; i
<args
.size(); i
++)
646 o
<< (i
>0 ? ", " : "") << *args
[i
];
652 print_format::create(const token
*t
, const char *n
)
654 bool stream
, format
, delim
, newline
, _char
;
655 if (n
== NULL
) n
= t
->content
.c_str();
659 format
= delim
= newline
= _char
= false;
661 if (strcmp(n
, "print_char") == 0)
671 if (0 != strncmp(n
, "print", 5))
688 if (*n
== 'l' && *(n
+1) == 'n')
699 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
, o
);
706 print_format::components_to_string(vector
<format_component
> const & components
)
710 for (vector
<format_component
>::const_iterator i
= components
.begin();
711 i
!= components
.end(); ++i
)
714 assert (i
->type
!= conv_unspecified
);
716 if (i
->type
== conv_literal
)
718 assert(!i
->literal_string
.empty());
719 for (string::const_iterator j
= i
->literal_string
.begin();
720 j
!= i
->literal_string
.end(); ++j
)
722 // See also: c_unparser::visit_literal_string and lex_cast_qstring
734 if (i
->test_flag (fmt_flag_zeropad
))
737 if (i
->test_flag (fmt_flag_plus
))
740 if (i
->test_flag (fmt_flag_space
))
743 if (i
->test_flag (fmt_flag_left
))
746 if (i
->test_flag (fmt_flag_special
))
749 if (i
->widthtype
== width_dynamic
)
751 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
754 if (i
->prectype
== prec_dynamic
)
756 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
757 oss
<< '.' << i
->precision
;
772 if (i
->test_flag (fmt_flag_large
))
777 else if (i
->base
== 8)
779 else if (i
->test_flag (fmt_flag_sign
))
797 case conv_memory_hex
:
809 vector
<print_format::format_component
>
810 print_format::string_to_components(string
const & str
)
812 format_component curr
;
813 vector
<format_component
> res
;
817 string::const_iterator i
= str
.begin();
819 while (i
!= str
.end())
823 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
824 curr
.type
= conv_literal
;
825 curr
.literal_string
+= *i
;
829 else if (i
+1 == str
.end() || *(i
+1) == '%')
832 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
833 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
834 curr
.type
= conv_literal
;
835 curr
.literal_string
+= '%';
842 if (curr
.type
!= conv_unspecified
)
844 // Flush any component we were previously accumulating
845 assert (curr
.type
== conv_literal
);
855 // Now we are definitely parsing a conversion.
856 // Begin by parsing flags (which are optional).
861 curr
.set_flag (fmt_flag_zeropad
);
866 curr
.set_flag (fmt_flag_plus
);
871 curr
.set_flag (fmt_flag_left
);
876 curr
.set_flag (fmt_flag_space
);
881 curr
.set_flag (fmt_flag_special
);
892 // Parse optional width
895 curr
.widthtype
= width_dynamic
;
898 else if (isdigit(*i
))
900 curr
.widthtype
= width_static
;
905 curr
.width
+= (*i
- '0');
908 while (i
!= str
.end() && isdigit(*i
));
914 // Parse optional precision
922 curr
.prectype
= prec_dynamic
;
925 else if (isdigit(*i
))
927 curr
.prectype
= prec_static
;
931 curr
.precision
*= 10;
932 curr
.precision
+= (*i
- '0');
935 while (i
!= str
.end() && isdigit(*i
));
942 // Parse the type modifier
953 // Parse the actual conversion specifier (bcsmdioupxXn)
956 // Valid conversion types
958 curr
.type
= conv_binary
;
962 curr
.type
= conv_char
;
966 curr
.type
= conv_string
;
970 curr
.type
= conv_memory
;
974 curr
.type
= conv_memory_hex
;
979 curr
.set_flag (fmt_flag_sign
);
981 curr
.type
= conv_number
;
986 curr
.type
= conv_number
;
991 curr
.set_flag (fmt_flag_large
);
993 curr
.type
= conv_number
;
998 // Since stap 1.3, %p == %#x.
999 curr
.set_flag (fmt_flag_special
);
1000 curr
.type
= conv_pointer
;
1002 // Oddness for stap < 1.3 is handled in translation
1009 if (curr
.type
== conv_unspecified
)
1010 throw PARSE_ERROR(_("invalid or missing conversion specifier"));
1013 res
.push_back(curr
);
1017 // If there's a remaining partly-composed conversion, fail.
1018 if (!curr
.is_empty())
1020 if (curr
.type
== conv_literal
)
1021 res
.push_back(curr
);
1023 throw PARSE_ERROR(_("trailing incomplete print format conversion"));
1030 void print_format::print (ostream
& o
) const
1032 o
<< print_format_type
<< "(";
1033 if (print_with_format
)
1034 o
<< lex_cast_qstring (raw_components
);
1035 if (print_with_delim
)
1036 o
<< lex_cast_qstring (delimiter
.literal_string
);
1039 for (vector
<expression
*>::const_iterator i
= args
.begin();
1040 i
!= args
.end(); ++i
)
1042 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
1049 void stat_op::print (ostream
& o
) const
1075 assert (0); // should not happen, as sc_none is only used in foreach sorts
1083 hist_op::print (ostream
& o
) const
1089 assert(params
.size() == 3);
1090 o
<< "hist_linear(";
1092 for (size_t i
= 0; i
< params
.size(); ++i
)
1094 o
<< ", " << params
[i
];
1100 assert(params
.size() == 0);
1108 ostream
& operator << (ostream
& o
, const statement
& k
)
1115 void embeddedcode::print (ostream
&o
) const
1122 void block::print (ostream
& o
) const
1125 for (unsigned i
=0; i
<statements
.size(); i
++)
1126 o
<< *statements
[i
] << endl
;
1130 block::block (statement
* car
, statement
* cdr
)
1132 statements
.push_back(car
);
1133 statements
.push_back(cdr
);
1134 this->tok
= car
->tok
;
1139 void try_block::print (ostream
& o
) const
1141 o
<< "try {" << endl
;
1142 if (try_block
) o
<< *try_block
<< endl
;
1144 if (catch_error_var
) o
<< "(" << *catch_error_var
<< ") ";
1146 if (catch_block
) o
<< *catch_block
<< endl
;
1151 void for_loop::print (ostream
& o
) const
1154 if (init
) init
->print (o
);
1158 if (incr
) incr
->print (o
);
1164 void foreach_loop::print (ostream
& o
) const
1173 for (unsigned i
=0; i
<indexes
.size(); i
++)
1175 if (i
> 0) o
<< ", ";
1176 indexes
[i
]->print (o
);
1177 if (sort_direction
!= 0 && sort_column
== i
+1)
1178 o
<< (sort_direction
> 0 ? "+" : "-");
1182 if (!array_slice
.empty())
1185 for (unsigned i
=0; i
<array_slice
.size(); i
++)
1187 if (i
> 0) o
<< ", ";
1189 array_slice
[i
]->print (o
);
1195 if (sort_direction
!= 0 && sort_column
== 0)
1199 case sc_count
: o
<< " @count"; break;
1200 case sc_average
: o
<< " @avg"; break;
1201 case sc_min
: o
<< " @min"; break;
1202 case sc_max
: o
<< " @max"; break;
1203 case sc_sum
: o
<< " @sum"; break;
1209 o
<< (sort_direction
> 0 ? "+" : "-");
1221 void null_statement::print (ostream
& o
) const
1227 void expr_statement::print (ostream
& o
) const
1233 void return_statement::print (ostream
& o
) const
1235 o
<< "return " << *value
;
1239 void delete_statement::print (ostream
& o
) const
1241 o
<< "delete " << *value
;
1244 void next_statement::print (ostream
& o
) const
1249 void break_statement::print (ostream
& o
) const
1254 void continue_statement::print (ostream
& o
) const
1259 void if_statement::print (ostream
& o
) const
1261 o
<< "if (" << *condition
<< ") "
1262 << *thenblock
<< endl
;
1264 o
<< "else " << *elseblock
<< endl
;
1268 void stapfile::print (ostream
& o
) const
1270 o
<< "# file " << name
<< endl
;
1272 for (unsigned i
=0; i
<embeds
.size(); i
++)
1273 embeds
[i
]->print (o
);
1275 for (unsigned i
=0; i
<globals
.size(); i
++)
1278 globals
[i
]->print (o
);
1282 for (unsigned i
=0; i
<aliases
.size(); i
++)
1284 aliases
[i
]->print (o
);
1288 for (unsigned i
=0; i
<probes
.size(); i
++)
1290 probes
[i
]->print (o
);
1294 for (unsigned j
= 0; j
< functions
.size(); j
++)
1296 functions
[j
]->print (o
);
1302 void probe::print (ostream
& o
) const
1310 void probe::printsig (ostream
& o
) const
1312 const probe_alias
*alias
= get_alias ();
1315 alias
->printsig (o
);
1319 for (unsigned i
=0; i
<locations
.size(); i
++)
1321 if (i
> 0) o
<< ",";
1322 locations
[i
]->print (o
);
1328 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
) const
1330 probes_list
.push_back(const_cast<probe
*>(this));
1332 base
->collect_derivation_chain(probes_list
);
1337 probe::collect_derivation_pp_chain (std::vector
<probe_point
*> &pp_list
) const
1339 pp_list
.push_back(const_cast<probe_point
*>(this->locations
[0]));
1341 base
->collect_derivation_pp_chain(pp_list
);
1346 void probe_point::print (ostream
& o
, bool print_extras
) const
1348 for (unsigned i
=0; i
<components
.size(); i
++)
1351 probe_point::component
* c
= components
[i
];
1354 // We might like to excise this weird 0 pointer, just in
1355 // case some recursion during the error-handling path, but
1356 // can't, because what we have here is a const *this.
1357 static int been_here
= 0;
1358 if (been_here
== 0) {
1360 throw SEMANTIC_ERROR (_("internal error: missing probe point component"));
1362 continue; // ... sad panda decides to skip the bad boy
1366 o
<< "(" << *c
->arg
<< ")";
1372 else if (optional
) // sufficient implies optional
1375 o
<< " if (" << *condition
<< ")";
1378 string
probe_point::str (bool print_extras
) const
1381 print(o
, print_extras
);
1386 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1387 probe (), alias_names (aliases
), epilogue_style(false)
1391 void probe_alias::printsig (ostream
& o
) const
1393 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1395 o
<< (i
>0 ? " = " : "");
1396 alias_names
[i
]->print (o
);
1398 o
<< (epilogue_style
? " += " : " = ");
1399 for (unsigned i
=0; i
<locations
.size(); i
++)
1401 if (i
> 0) o
<< ", ";
1402 locations
[i
]->print (o
);
1407 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1414 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1422 // ------------------------------------------------------------------------
1427 block::visit (visitor
* u
)
1429 u
->visit_block (this);
1434 try_block::visit (visitor
* u
)
1436 u
->visit_try_block (this);
1441 embeddedcode::visit (visitor
* u
)
1443 u
->visit_embeddedcode (this);
1448 for_loop::visit (visitor
* u
)
1450 u
->visit_for_loop (this);
1454 foreach_loop::visit (visitor
* u
)
1456 u
->visit_foreach_loop (this);
1460 null_statement::visit (visitor
* u
)
1462 u
->visit_null_statement (this);
1466 expr_statement::visit (visitor
* u
)
1468 u
->visit_expr_statement (this);
1472 return_statement::visit (visitor
* u
)
1474 u
->visit_return_statement (this);
1478 delete_statement::visit (visitor
* u
)
1480 u
->push_active_lvalue (this->value
);
1481 u
->visit_delete_statement (this);
1482 u
->pop_active_lvalue ();
1486 if_statement::visit (visitor
* u
)
1488 u
->visit_if_statement (this);
1492 next_statement::visit (visitor
* u
)
1494 u
->visit_next_statement (this);
1498 break_statement::visit (visitor
* u
)
1500 u
->visit_break_statement (this);
1504 continue_statement::visit (visitor
* u
)
1506 u
->visit_continue_statement (this);
1510 literal_string::visit(visitor
* u
)
1512 u
->visit_literal_string (this);
1516 literal_number::visit(visitor
* u
)
1518 u
->visit_literal_number (this);
1522 binary_expression::visit (visitor
* u
)
1524 u
->visit_binary_expression (this);
1528 embedded_expr::visit (visitor
* u
)
1530 u
->visit_embedded_expr (this);
1534 unary_expression::visit (visitor
* u
)
1536 u
->visit_unary_expression (this);
1540 pre_crement::visit (visitor
* u
)
1542 u
->push_active_lvalue (this->operand
);
1543 u
->visit_pre_crement (this);
1544 u
->pop_active_lvalue ();
1548 post_crement::visit (visitor
* u
)
1550 u
->push_active_lvalue (this->operand
);
1551 u
->visit_post_crement (this);
1552 u
->pop_active_lvalue ();
1556 logical_or_expr::visit (visitor
* u
)
1558 u
->visit_logical_or_expr (this);
1562 logical_and_expr::visit (visitor
* u
)
1564 u
->visit_logical_and_expr (this);
1568 array_in::visit (visitor
* u
)
1570 u
->visit_array_in (this);
1574 regex_query::visit (visitor
* u
)
1576 u
->visit_regex_query (this);
1580 comparison::visit (visitor
* u
)
1582 u
->visit_comparison (this);
1586 concatenation::visit (visitor
* u
)
1588 u
->visit_concatenation (this);
1592 ternary_expression::visit (visitor
* u
)
1594 u
->visit_ternary_expression (this);
1598 assignment::visit (visitor
* u
)
1600 u
->push_active_lvalue (this->left
);
1601 u
->visit_assignment (this);
1602 u
->pop_active_lvalue ();
1606 symbol::visit (visitor
* u
)
1608 u
->visit_symbol (this);
1612 target_symbol::visit (visitor
* u
)
1614 u
->visit_target_symbol(this);
1618 target_symbol::visit_components (visitor
* u
)
1620 for (unsigned i
= 0; i
< components
.size(); ++i
)
1621 if (components
[i
].type
== comp_expression_array_index
)
1622 components
[i
].expr_index
->visit (u
);
1626 target_symbol::visit_components (update_visitor
* u
)
1628 for (unsigned i
= 0; i
< components
.size(); ++i
)
1629 if (components
[i
].type
== comp_expression_array_index
)
1630 u
->replace (components
[i
].expr_index
);
1634 cast_op::visit (visitor
* u
)
1636 u
->visit_cast_op(this);
1641 autocast_op::visit (visitor
* u
)
1643 u
->visit_autocast_op(this);
1648 atvar_op::visit (visitor
* u
)
1650 u
->visit_atvar_op(this);
1655 defined_op::visit (visitor
* u
)
1657 u
->visit_defined_op(this);
1662 entry_op::visit (visitor
* u
)
1664 u
->visit_entry_op(this);
1669 perf_op::visit (visitor
* u
)
1671 u
->visit_perf_op(this);
1676 arrayindex::visit (visitor
* u
)
1678 u
->visit_arrayindex (this);
1682 functioncall::visit (visitor
* u
)
1684 u
->visit_functioncall (this);
1688 print_format::visit (visitor
*u
)
1690 u
->visit_print_format (this);
1694 stat_op::visit (visitor
*u
)
1696 u
->visit_stat_op (this);
1700 hist_op::visit (visitor
*u
)
1702 u
->visit_hist_op (this);
1707 expression::is_symbol(symbol
*& sym_out
)
1714 indexable::is_symbol(symbol
*& sym_out
)
1721 indexable::is_hist_op(hist_op
*& hist_out
)
1728 symbol::is_symbol(symbol
*& sym_out
)
1735 hist_op::is_hist_op(hist_op
*& hist_out
)
1742 classify_indexable(indexable
* ix
,
1743 symbol
*& array_out
,
1744 hist_op
*& hist_out
)
1749 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1750 throw SEMANTIC_ERROR(_("Expecting symbol or histogram operator"), ix
->tok
);
1751 if (!(hist_out
|| array_out
))
1752 throw SEMANTIC_ERROR(_("Failed to classify indexable"), ix
->tok
);
1756 // ------------------------------------------------------------------------
1759 visitor::is_active_lvalue(expression
*e
)
1761 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1763 if (active_lvalues
[i
] == e
)
1770 visitor::push_active_lvalue(expression
*e
)
1772 active_lvalues
.push_back(e
);
1776 visitor::pop_active_lvalue()
1778 assert(!active_lvalues
.empty());
1779 active_lvalues
.pop_back();
1784 // ------------------------------------------------------------------------
1787 traversing_visitor::visit_block (block
* s
)
1789 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1790 s
->statements
[i
]->visit (this);
1794 traversing_visitor::visit_try_block (try_block
* s
)
1797 s
->try_block
->visit (this);
1798 if (s
->catch_error_var
)
1799 s
->catch_error_var
->visit (this);
1801 s
->catch_block
->visit (this);
1805 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1810 traversing_visitor::visit_null_statement (null_statement
*)
1815 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1817 s
->value
->visit (this);
1821 traversing_visitor::visit_if_statement (if_statement
* s
)
1823 s
->condition
->visit (this);
1824 s
->thenblock
->visit (this);
1826 s
->elseblock
->visit (this);
1830 traversing_visitor::visit_for_loop (for_loop
* s
)
1832 if (s
->init
) s
->init
->visit (this);
1833 s
->cond
->visit (this);
1834 if (s
->incr
) s
->incr
->visit (this);
1835 s
->block
->visit (this);
1839 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1841 s
->base
->visit(this);
1843 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1844 s
->indexes
[i
]->visit (this);
1847 s
->value
->visit (this);
1850 s
->limit
->visit (this);
1852 s
->block
->visit (this);
1856 traversing_visitor::visit_return_statement (return_statement
* s
)
1858 s
->value
->visit (this);
1862 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1864 s
->value
->visit (this);
1868 traversing_visitor::visit_next_statement (next_statement
*)
1873 traversing_visitor::visit_break_statement (break_statement
*)
1878 traversing_visitor::visit_continue_statement (continue_statement
*)
1883 traversing_visitor::visit_literal_string (literal_string
*)
1888 traversing_visitor::visit_literal_number (literal_number
*)
1893 traversing_visitor::visit_embedded_expr (embedded_expr
*)
1898 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1900 e
->left
->visit (this);
1901 e
->right
->visit (this);
1905 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1907 e
->operand
->visit (this);
1911 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1913 e
->operand
->visit (this);
1917 traversing_visitor::visit_post_crement (post_crement
* e
)
1919 e
->operand
->visit (this);
1924 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1926 e
->left
->visit (this);
1927 e
->right
->visit (this);
1931 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1933 e
->left
->visit (this);
1934 e
->right
->visit (this);
1938 traversing_visitor::visit_array_in (array_in
* e
)
1940 e
->operand
->visit (this);
1944 traversing_visitor::visit_regex_query (regex_query
* e
)
1946 e
->left
->visit (this);
1947 e
->right
->visit (this);
1951 traversing_visitor::visit_comparison (comparison
* e
)
1953 e
->left
->visit (this);
1954 e
->right
->visit (this);
1958 traversing_visitor::visit_concatenation (concatenation
* e
)
1960 e
->left
->visit (this);
1961 e
->right
->visit (this);
1965 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1967 e
->cond
->visit (this);
1968 e
->truevalue
->visit (this);
1969 e
->falsevalue
->visit (this);
1973 traversing_visitor::visit_assignment (assignment
* e
)
1975 e
->left
->visit (this);
1976 e
->right
->visit (this);
1980 traversing_visitor::visit_symbol (symbol
*)
1985 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1987 e
->visit_components (this);
1991 traversing_visitor::visit_cast_op (cast_op
* e
)
1993 e
->operand
->visit (this);
1994 e
->visit_components (this);
1998 traversing_visitor::visit_autocast_op (autocast_op
* e
)
2000 e
->operand
->visit (this);
2001 e
->visit_components (this);
2005 traversing_visitor::visit_atvar_op (atvar_op
* e
)
2007 e
->visit_components (this);
2011 traversing_visitor::visit_defined_op (defined_op
* e
)
2013 e
->operand
->visit (this);
2017 traversing_visitor::visit_entry_op (entry_op
* e
)
2019 e
->operand
->visit (this);
2024 traversing_visitor::visit_perf_op (perf_op
* e
)
2026 e
->operand
->visit (this);
2031 traversing_visitor::visit_arrayindex (arrayindex
* e
)
2033 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2035 e
->indexes
[i
]->visit (this);
2037 e
->base
->visit(this);
2041 traversing_visitor::visit_functioncall (functioncall
* e
)
2043 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2044 e
->args
[i
]->visit (this);
2048 traversing_visitor::visit_print_format (print_format
* e
)
2050 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2051 e
->args
[i
]->visit (this);
2053 e
->hist
->visit(this);
2057 traversing_visitor::visit_stat_op (stat_op
* e
)
2059 e
->stat
->visit (this);
2063 traversing_visitor::visit_hist_op (hist_op
* e
)
2065 e
->stat
->visit (this);
2070 expression_visitor::visit_literal_string (literal_string
* e
)
2072 traversing_visitor::visit_literal_string (e
);
2073 visit_expression (e
);
2077 expression_visitor::visit_literal_number (literal_number
* e
)
2079 traversing_visitor::visit_literal_number (e
);
2080 visit_expression (e
);
2084 expression_visitor::visit_embedded_expr (embedded_expr
* e
)
2086 traversing_visitor::visit_embedded_expr (e
);
2087 visit_expression (e
);
2091 expression_visitor::visit_binary_expression (binary_expression
* e
)
2093 traversing_visitor::visit_binary_expression (e
);
2094 visit_expression (e
);
2098 expression_visitor::visit_unary_expression (unary_expression
* e
)
2100 traversing_visitor::visit_unary_expression (e
);
2101 visit_expression (e
);
2105 expression_visitor::visit_pre_crement (pre_crement
* e
)
2107 traversing_visitor::visit_pre_crement (e
);
2108 visit_expression (e
);
2112 expression_visitor::visit_post_crement (post_crement
* e
)
2114 traversing_visitor::visit_post_crement (e
);
2115 visit_expression (e
);
2119 expression_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2121 traversing_visitor::visit_logical_or_expr (e
);
2122 visit_expression (e
);
2126 expression_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2128 traversing_visitor::visit_logical_and_expr (e
);
2129 visit_expression (e
);
2133 expression_visitor::visit_array_in (array_in
* e
)
2135 traversing_visitor::visit_array_in (e
);
2136 visit_expression (e
);
2140 expression_visitor::visit_regex_query (regex_query
* e
)
2142 traversing_visitor::visit_regex_query (e
);
2143 visit_expression (e
);
2147 expression_visitor::visit_comparison (comparison
* e
)
2149 traversing_visitor::visit_comparison (e
);
2150 visit_expression (e
);
2154 expression_visitor::visit_concatenation (concatenation
* e
)
2156 traversing_visitor::visit_concatenation (e
);
2157 visit_expression (e
);
2161 expression_visitor::visit_ternary_expression (ternary_expression
* e
)
2163 traversing_visitor::visit_ternary_expression (e
);
2164 visit_expression (e
);
2168 expression_visitor::visit_assignment (assignment
* e
)
2170 traversing_visitor::visit_assignment (e
);
2171 visit_expression (e
);
2175 expression_visitor::visit_symbol (symbol
* e
)
2177 traversing_visitor::visit_symbol (e
);
2178 visit_expression (e
);
2182 expression_visitor::visit_target_symbol (target_symbol
* e
)
2184 traversing_visitor::visit_target_symbol (e
);
2185 visit_expression (e
);
2189 expression_visitor::visit_arrayindex (arrayindex
* e
)
2191 traversing_visitor::visit_arrayindex (e
);
2192 visit_expression (e
);
2196 expression_visitor::visit_functioncall (functioncall
* e
)
2198 traversing_visitor::visit_functioncall (e
);
2199 visit_expression (e
);
2203 expression_visitor::visit_print_format (print_format
* e
)
2205 traversing_visitor::visit_print_format (e
);
2206 visit_expression (e
);
2210 expression_visitor::visit_stat_op (stat_op
* e
)
2212 traversing_visitor::visit_stat_op (e
);
2213 visit_expression (e
);
2217 expression_visitor::visit_hist_op (hist_op
* e
)
2219 traversing_visitor::visit_hist_op (e
);
2220 visit_expression (e
);
2224 expression_visitor::visit_cast_op (cast_op
* e
)
2226 traversing_visitor::visit_cast_op (e
);
2227 visit_expression (e
);
2231 expression_visitor::visit_autocast_op (autocast_op
* e
)
2233 traversing_visitor::visit_autocast_op (e
);
2234 visit_expression (e
);
2238 expression_visitor::visit_atvar_op (atvar_op
* e
)
2240 traversing_visitor::visit_atvar_op (e
);
2241 visit_expression (e
);
2245 expression_visitor::visit_defined_op (defined_op
* e
)
2247 traversing_visitor::visit_defined_op (e
);
2248 visit_expression (e
);
2252 expression_visitor::visit_entry_op (entry_op
* e
)
2254 traversing_visitor::visit_entry_op (e
);
2255 visit_expression (e
);
2259 expression_visitor::visit_perf_op (perf_op
* e
)
2261 traversing_visitor::visit_perf_op (e
);
2262 visit_expression (e
);
2267 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
2269 traversing_visitor::visit_functioncall (e
);
2271 // prevent infinite recursion
2272 if (nested
.find (e
->referent
) == nested
.end ())
2274 if (seen
.find(e
->referent
) == seen
.end())
2275 seen
.insert (e
->referent
);
2276 nested
.insert (e
->referent
);
2278 functiondecl
* last_current_function
= current_function
;
2279 current_function
= e
->referent
;
2280 e
->referent
->body
->visit (this);
2281 current_function
= last_current_function
;
2282 nested
.erase (e
->referent
);
2284 else { this->note_recursive_functioncall(e
); }
2288 functioncall_traversing_visitor::note_recursive_functioncall (functioncall
* e
)
2293 varuse_collecting_visitor::visit_try_block (try_block
*s
)
2296 s
->try_block
->visit (this);
2297 if (s
->catch_error_var
)
2298 written
.insert (s
->catch_error_var
->referent
);
2300 s
->catch_block
->visit (this);
2302 // NB: don't functioncall_traversing_visitor::visit_try_block (s);
2303 // since that would count s->catch_error_var as a read also.
2308 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
2310 assert (current_function
); // only they get embedded code
2312 // Don't allow embedded C functions in unprivileged mode unless
2313 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2314 // or we're in a usermode runtime.
2315 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2316 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2317 ! session
.runtime_usermode_p () &&
2318 s
->code
.find ("/* unprivileged */") == string::npos
&&
2319 s
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2320 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
2321 pr_name (session
.privilege
)),
2322 current_function
->tok
);
2324 // Don't allow /* guru */ functions unless -g is active.
2325 if (!session
.guru_mode
&& s
->code
.find ("/* guru */") != string::npos
)
2326 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
2327 current_function
->tok
);
2329 // PR14524: Support old-style THIS->local syntax on per-function basis.
2330 if (s
->code
.find ("/* unmangled */") != string::npos
)
2331 current_function
->mangle_oldstyle
= true;
2333 // We want to elide embedded-C functions when possible. For
2334 // example, each $target variable access is expanded to an
2335 // embedded-C function call. Yet, for safety reasons, we should
2336 // presume that embedded-C functions have intentional side-effects.
2338 // To tell these two types of functions apart, we apply a
2339 // Kludge(tm): we look for a magic string within the function body.
2340 // $target variables as rvalues will have this; lvalues won't.
2341 // Also, explicit side-effect-free tapset functions will have this.
2343 if (s
->code
.find ("/* pure */") != string::npos
)
2346 embedded_seen
= true;
2350 // About the same case as above.
2352 varuse_collecting_visitor::visit_embedded_expr (embedded_expr
*e
)
2354 // Don't allow embedded C expressions in unprivileged mode unless
2355 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2356 // or we're in a usermode runtime.
2357 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2358 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2359 ! session
.runtime_usermode_p () &&
2360 e
->code
.find ("/* unprivileged */") == string::npos
&&
2361 e
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2362 throw SEMANTIC_ERROR (_F("embedded expression may not be used when --privilege=%s is specified",
2363 pr_name (session
.privilege
)),
2366 // Don't allow /* guru */ functions unless -g is active.
2367 if (!session
.guru_mode
&& e
->code
.find ("/* guru */") != string::npos
)
2368 throw SEMANTIC_ERROR (_("embedded expression may not be used unless -g is specified"),
2371 // We want to elide embedded-C functions when possible. For
2372 // example, each $target variable access is expanded to an
2373 // embedded-C function call. Yet, for safety reasons, we should
2374 // presume that embedded-C functions have intentional side-effects.
2376 // To tell these two types of functions apart, we apply a
2377 // Kludge(tm): we look for a magic string within the function body.
2378 // $target variables as rvalues will have this; lvalues won't.
2379 // Also, explicit side-effect-free tapset functions will have this.
2381 if (e
->code
.find ("/* pure */") != string::npos
)
2384 embedded_seen
= true;
2389 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
2391 // Still-unresolved target symbol assignments get treated as
2392 // generating side-effects like embedded-C, to prevent premature
2393 // elision and later error message suppression (PR5516). rvalue use
2394 // of unresolved target symbols is OTOH not considered a side-effect.
2396 if (is_active_lvalue (e
))
2397 embedded_seen
= true;
2399 functioncall_traversing_visitor::visit_target_symbol (e
);
2404 varuse_collecting_visitor::visit_atvar_op (atvar_op
*e
)
2406 // Similar to visit_target_symbol
2408 if (is_active_lvalue (e
))
2409 embedded_seen
= true;
2411 functioncall_traversing_visitor::visit_atvar_op (e
);
2416 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
2418 // As with target_symbols, unresolved cast assignments need to preserved
2419 // for later error handling.
2420 if (is_active_lvalue (e
))
2421 embedded_seen
= true;
2423 functioncall_traversing_visitor::visit_cast_op (e
);
2427 varuse_collecting_visitor::visit_autocast_op (autocast_op
*e
)
2429 // As with target_symbols, unresolved cast assignments need to preserved
2430 // for later error handling.
2431 if (is_active_lvalue (e
))
2432 embedded_seen
= true;
2434 functioncall_traversing_visitor::visit_autocast_op (e
);
2438 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
2441 functioncall_traversing_visitor::visit_defined_op (e
);
2445 varuse_collecting_visitor::visit_entry_op (entry_op
*e
)
2448 functioncall_traversing_visitor::visit_entry_op (e
);
2453 varuse_collecting_visitor::visit_perf_op (perf_op
*e
)
2455 functioncall_traversing_visitor::visit_perf_op (e
);
2460 varuse_collecting_visitor::visit_print_format (print_format
* e
)
2462 // NB: Instead of being top-level statements, "print" and "printf"
2463 // are implemented as statement-expressions containing a
2464 // print_format. They have side-effects, but not via the
2465 // embedded-code detection method above.
2467 // But sprint and sprintf don't have side-effects.
2469 bool last_lvalue_read
= current_lvalue_read
;
2470 current_lvalue_read
= true;
2471 if (e
->print_to_stream
)
2472 embedded_seen
= true; // a proxy for "has unknown side-effects"
2474 functioncall_traversing_visitor::visit_print_format (e
);
2475 current_lvalue_read
= last_lvalue_read
;
2480 varuse_collecting_visitor::visit_assignment (assignment
*e
)
2482 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
2484 expression
* last_lvalue
= current_lvalue
;
2485 bool last_lvalue_read
= current_lvalue_read
;
2486 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
2487 current_lvalue_read
= true;
2488 functioncall_traversing_visitor::visit_assignment (e
);
2489 current_lvalue
= last_lvalue
;
2490 current_lvalue_read
= last_lvalue_read
;
2492 else // read-modify-writes
2494 expression
* last_lrvalue
= current_lrvalue
;
2495 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
2496 functioncall_traversing_visitor::visit_assignment (e
);
2497 current_lrvalue
= last_lrvalue
;
2502 varuse_collecting_visitor::visit_symbol (symbol
*e
)
2504 if (e
->referent
== 0)
2505 throw SEMANTIC_ERROR (_("symbol without referent"), e
->tok
);
2507 // We could handle initialized globals by marking them as "written".
2508 // However, this current visitor may be called for a function or
2509 // probe body, from the point of view of which this global is
2510 // already initialized, so not written.
2512 if (e->referent->init)
2513 written.insert (e->referent);
2516 if (current_lvalue
== e
|| current_lrvalue
== e
)
2518 written
.insert (e
->referent
);
2520 if (current_lvalue
!= e
|| current_lrvalue
== e
)
2522 read
.insert (e
->referent
);
2525 if (current_lrvalue
== e
)
2527 if (current_lvalue_read
)
2528 used
.insert (e
->referent
);
2530 else if (current_lvalue
!= e
)
2531 used
.insert (e
->referent
);
2534 // NB: stat_op need not be overridden, since it will get to
2535 // visit_symbol and only as a possible rvalue.
2539 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
2541 // Hooking this callback is necessary because of the hacky
2542 // statistics representation. For the expression "i[4] = 5", the
2543 // incoming lvalue will point to this arrayindex. However, the
2544 // symbol corresponding to the "i[4]" is multiply inherited with
2545 // arrayindex. If the symbol base part of this object is not at
2546 // offset 0, then static_cast<symbol*>(e) may result in a different
2547 // address, and not match lvalue by number when we recurse that way.
2548 // So we explicitly override the incoming lvalue/lrvalue values to
2549 // point at the embedded objects' actual base addresses.
2551 expression
* last_lrvalue
= current_lrvalue
;
2552 expression
* last_lvalue
= current_lvalue
;
2554 symbol
*array
= NULL
;
2555 hist_op
*hist
= NULL
;
2556 classify_indexable(e
->base
, array
, hist
);
2557 expression
*value
= array
?: hist
->stat
;
2559 if (current_lrvalue
== e
) current_lrvalue
= value
;
2560 if (current_lvalue
== e
) current_lvalue
= value
;
2561 functioncall_traversing_visitor::visit_arrayindex (e
);
2563 current_lrvalue
= last_lrvalue
;
2564 current_lvalue
= last_lvalue
;
2569 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
2571 expression
* last_lrvalue
= current_lrvalue
;
2572 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2573 functioncall_traversing_visitor::visit_pre_crement (e
);
2574 current_lrvalue
= last_lrvalue
;
2578 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
2580 expression
* last_lrvalue
= current_lrvalue
;
2581 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2582 functioncall_traversing_visitor::visit_post_crement (e
);
2583 current_lrvalue
= last_lrvalue
;
2587 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
2589 // NB: we duplicate so don't bother call
2590 // functioncall_traversing_visitor::visit_foreach_loop (s);
2592 s
->base
->visit(this);
2594 // If the collection is sorted, imply a "write" access to the
2595 // array in addition to the "read" one already noted above.
2596 if (s
->sort_direction
)
2598 symbol
*array
= NULL
;
2599 hist_op
*hist
= NULL
;
2600 classify_indexable (s
->base
, array
, hist
);
2601 if (array
) this->written
.insert (array
->referent
);
2602 // XXX: Can hist_op iterations be sorted?
2605 // NB: don't forget to visit the index expressions, which are lvalues.
2606 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
2608 expression
* last_lvalue
= current_lvalue
;
2609 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
2610 s
->indexes
[i
]->visit (this);
2611 current_lvalue
= last_lvalue
;
2614 // visit the additional specified array slice
2615 for (unsigned i
=0; i
<s
->array_slice
.size(); i
++)
2617 if (s
->array_slice
[i
])
2618 s
->array_slice
[i
]->visit (this);
2621 // The value is an lvalue too
2624 expression
* last_lvalue
= current_lvalue
;
2625 current_lvalue
= s
->value
; // leave a mark for ::visit_symbol
2626 s
->value
->visit (this);
2627 current_lvalue
= last_lvalue
;
2631 s
->limit
->visit (this);
2633 s
->block
->visit (this);
2638 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2640 // Ideally, this would be treated like an assignment: a plain write
2641 // to the underlying value ("lvalue"). XXX: However, the
2642 // optimization pass is not smart enough to remove an unneeded
2643 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2644 // should protect the underlying value from optimizional mischief.
2645 expression
* last_lrvalue
= current_lrvalue
;
2646 bool last_lvalue_read
= current_lvalue_read
;
2647 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2648 current_lvalue_read
= true;
2649 functioncall_traversing_visitor::visit_delete_statement (s
);
2650 current_lrvalue
= last_lrvalue
;
2651 current_lvalue_read
= last_lvalue_read
;
2655 varuse_collecting_visitor::side_effect_free ()
2657 return (written
.empty() && !embedded_seen
);
2662 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2664 // A looser notion of side-effect-freeness with respect to a given
2665 // list of variables.
2667 // That's useful because the written list may consist of local
2668 // variables of called functions. But visible side-effects only
2669 // occur if the client's locals, or any globals are written-to.
2671 set
<vardecl
*> intersection
;
2672 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2673 set_intersection (written
.begin(), written
.end(),
2674 vars
.begin(), vars
.end(),
2677 return (intersection
.empty() && !embedded_seen
);
2683 // ------------------------------------------------------------------------
2686 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2687 throwing_visitor::throwing_visitor (): msg (_("invalid element")) {}
2691 throwing_visitor::throwone (const token
* t
)
2693 throw SEMANTIC_ERROR (msg
, t
);
2697 throwing_visitor::visit_block (block
* s
)
2703 throwing_visitor::visit_try_block (try_block
* s
)
2710 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2716 throwing_visitor::visit_null_statement (null_statement
* s
)
2722 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2728 throwing_visitor::visit_if_statement (if_statement
* s
)
2734 throwing_visitor::visit_for_loop (for_loop
* s
)
2740 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2746 throwing_visitor::visit_return_statement (return_statement
* s
)
2752 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2758 throwing_visitor::visit_next_statement (next_statement
* s
)
2764 throwing_visitor::visit_break_statement (break_statement
* s
)
2770 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2776 throwing_visitor::visit_literal_string (literal_string
* e
)
2782 throwing_visitor::visit_literal_number (literal_number
* e
)
2788 throwing_visitor::visit_embedded_expr (embedded_expr
* e
)
2794 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2800 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2806 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2812 throwing_visitor::visit_post_crement (post_crement
* e
)
2819 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2825 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2831 throwing_visitor::visit_array_in (array_in
* e
)
2837 throwing_visitor::visit_regex_query (regex_query
* e
)
2843 throwing_visitor::visit_comparison (comparison
* e
)
2849 throwing_visitor::visit_concatenation (concatenation
* e
)
2855 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2861 throwing_visitor::visit_assignment (assignment
* e
)
2867 throwing_visitor::visit_symbol (symbol
* e
)
2873 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2879 throwing_visitor::visit_atvar_op (atvar_op
* e
)
2885 throwing_visitor::visit_cast_op (cast_op
* e
)
2891 throwing_visitor::visit_autocast_op (autocast_op
* e
)
2897 throwing_visitor::visit_defined_op (defined_op
* e
)
2903 throwing_visitor::visit_entry_op (entry_op
* e
)
2910 throwing_visitor::visit_perf_op (perf_op
* e
)
2917 throwing_visitor::visit_arrayindex (arrayindex
* e
)
2923 throwing_visitor::visit_functioncall (functioncall
* e
)
2929 throwing_visitor::visit_print_format (print_format
* e
)
2935 throwing_visitor::visit_stat_op (stat_op
* e
)
2941 throwing_visitor::visit_hist_op (hist_op
* e
)
2947 // ------------------------------------------------------------------------
2951 update_visitor::visit_block (block
* s
)
2953 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
2954 replace (s
->statements
[i
]);
2959 update_visitor::visit_try_block (try_block
* s
)
2961 replace (s
->try_block
);
2962 replace (s
->catch_error_var
);
2963 replace (s
->catch_block
);
2968 update_visitor::visit_embeddedcode (embeddedcode
* s
)
2974 update_visitor::visit_null_statement (null_statement
* s
)
2980 update_visitor::visit_expr_statement (expr_statement
* s
)
2987 update_visitor::visit_if_statement (if_statement
* s
)
2989 replace (s
->condition
);
2990 replace (s
->thenblock
);
2991 replace (s
->elseblock
);
2996 update_visitor::visit_for_loop (for_loop
* s
)
3006 update_visitor::visit_foreach_loop (foreach_loop
* s
)
3008 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
3009 replace (s
->indexes
[i
]);
3018 update_visitor::visit_return_statement (return_statement
* s
)
3025 update_visitor::visit_delete_statement (delete_statement
* s
)
3032 update_visitor::visit_next_statement (next_statement
* s
)
3038 update_visitor::visit_break_statement (break_statement
* s
)
3044 update_visitor::visit_continue_statement (continue_statement
* s
)
3050 update_visitor::visit_literal_string (literal_string
* e
)
3056 update_visitor::visit_literal_number (literal_number
* e
)
3062 update_visitor::visit_embedded_expr (embedded_expr
* e
)
3068 update_visitor::visit_binary_expression (binary_expression
* e
)
3076 update_visitor::visit_unary_expression (unary_expression
* e
)
3078 replace (e
->operand
);
3083 update_visitor::visit_pre_crement (pre_crement
* e
)
3085 replace (e
->operand
);
3090 update_visitor::visit_post_crement (post_crement
* e
)
3092 replace (e
->operand
);
3098 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3106 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3114 update_visitor::visit_array_in (array_in
* e
)
3116 replace (e
->operand
);
3121 update_visitor::visit_regex_query (regex_query
* e
)
3124 replace (e
->right
); // XXX: do we *need* to replace literal in RHS?
3129 update_visitor::visit_comparison (comparison
* e
)
3137 update_visitor::visit_concatenation (concatenation
* e
)
3145 update_visitor::visit_ternary_expression (ternary_expression
* e
)
3148 replace (e
->truevalue
);
3149 replace (e
->falsevalue
);
3154 update_visitor::visit_assignment (assignment
* e
)
3162 update_visitor::visit_symbol (symbol
* e
)
3168 update_visitor::visit_target_symbol (target_symbol
* e
)
3170 e
->visit_components (this);
3175 update_visitor::visit_cast_op (cast_op
* e
)
3177 replace (e
->operand
);
3178 e
->visit_components (this);
3183 update_visitor::visit_autocast_op (autocast_op
* e
)
3185 replace (e
->operand
);
3186 e
->visit_components (this);
3191 update_visitor::visit_atvar_op (atvar_op
* e
)
3193 e
->visit_components (this);
3198 update_visitor::visit_defined_op (defined_op
* e
)
3200 replace (e
->operand
);
3205 update_visitor::visit_entry_op (entry_op
* e
)
3207 replace (e
->operand
);
3212 update_visitor::visit_perf_op (perf_op
* e
)
3214 replace (e
->operand
);
3219 update_visitor::visit_arrayindex (arrayindex
* e
)
3222 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
3223 replace (e
->indexes
[i
]);
3228 update_visitor::visit_functioncall (functioncall
* e
)
3230 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3231 replace (e
->args
[i
]);
3236 update_visitor::visit_print_format (print_format
* e
)
3238 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3239 replace (e
->args
[i
]);
3245 update_visitor::visit_stat_op (stat_op
* e
)
3252 update_visitor::visit_hist_op (hist_op
* e
)
3259 // ------------------------------------------------------------------------
3263 deep_copy_visitor::visit_block (block
* s
)
3265 update_visitor::visit_block(new block(*s
));
3269 deep_copy_visitor::visit_try_block (try_block
* s
)
3271 update_visitor::visit_try_block(new try_block(*s
));
3275 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
3277 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
3281 deep_copy_visitor::visit_null_statement (null_statement
* s
)
3283 update_visitor::visit_null_statement(new null_statement(*s
));
3287 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
3289 update_visitor::visit_expr_statement(new expr_statement(*s
));
3293 deep_copy_visitor::visit_if_statement (if_statement
* s
)
3295 update_visitor::visit_if_statement(new if_statement(*s
));
3299 deep_copy_visitor::visit_for_loop (for_loop
* s
)
3301 update_visitor::visit_for_loop(new for_loop(*s
));
3305 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
3307 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
3311 deep_copy_visitor::visit_return_statement (return_statement
* s
)
3313 update_visitor::visit_return_statement(new return_statement(*s
));
3317 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
3319 update_visitor::visit_delete_statement(new delete_statement(*s
));
3323 deep_copy_visitor::visit_next_statement (next_statement
* s
)
3325 update_visitor::visit_next_statement(new next_statement(*s
));
3329 deep_copy_visitor::visit_break_statement (break_statement
* s
)
3331 update_visitor::visit_break_statement(new break_statement(*s
));
3335 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
3337 update_visitor::visit_continue_statement(new continue_statement(*s
));
3341 deep_copy_visitor::visit_literal_string (literal_string
* e
)
3343 update_visitor::visit_literal_string(new literal_string(*e
));
3347 deep_copy_visitor::visit_literal_number (literal_number
* e
)
3349 update_visitor::visit_literal_number(new literal_number(*e
));
3353 deep_copy_visitor::visit_embedded_expr (embedded_expr
* e
)
3355 update_visitor::visit_embedded_expr(new embedded_expr(*e
));
3359 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
3361 update_visitor::visit_binary_expression(new binary_expression(*e
));
3365 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
3367 update_visitor::visit_unary_expression(new unary_expression(*e
));
3371 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
3373 update_visitor::visit_pre_crement(new pre_crement(*e
));
3377 deep_copy_visitor::visit_post_crement (post_crement
* e
)
3379 update_visitor::visit_post_crement(new post_crement(*e
));
3384 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3386 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
3390 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3392 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
3396 deep_copy_visitor::visit_array_in (array_in
* e
)
3398 update_visitor::visit_array_in(new array_in(*e
));
3402 deep_copy_visitor::visit_regex_query (regex_query
* e
)
3404 update_visitor::visit_regex_query(new regex_query(*e
));
3408 deep_copy_visitor::visit_comparison (comparison
* e
)
3410 update_visitor::visit_comparison(new comparison(*e
));
3414 deep_copy_visitor::visit_concatenation (concatenation
* e
)
3416 update_visitor::visit_concatenation(new concatenation(*e
));
3420 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
3422 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
3426 deep_copy_visitor::visit_assignment (assignment
* e
)
3428 update_visitor::visit_assignment(new assignment(*e
));
3432 deep_copy_visitor::visit_symbol (symbol
* e
)
3434 symbol
* n
= new symbol(*e
);
3435 n
->referent
= NULL
; // don't copy!
3436 update_visitor::visit_symbol(n
);
3440 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
3442 target_symbol
* n
= new target_symbol(*e
);
3443 update_visitor::visit_target_symbol(n
);
3447 deep_copy_visitor::visit_cast_op (cast_op
* e
)
3449 update_visitor::visit_cast_op(new cast_op(*e
));
3453 deep_copy_visitor::visit_autocast_op (autocast_op
* e
)
3455 update_visitor::visit_autocast_op(new autocast_op(*e
));
3459 deep_copy_visitor::visit_atvar_op (atvar_op
* e
)
3461 update_visitor::visit_atvar_op(new atvar_op(*e
));
3465 deep_copy_visitor::visit_defined_op (defined_op
* e
)
3467 update_visitor::visit_defined_op(new defined_op(*e
));
3471 deep_copy_visitor::visit_entry_op (entry_op
* e
)
3473 update_visitor::visit_entry_op(new entry_op(*e
));
3477 deep_copy_visitor::visit_perf_op (perf_op
* e
)
3479 update_visitor::visit_perf_op(new perf_op(*e
));
3483 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
3485 update_visitor::visit_arrayindex(new arrayindex(*e
));
3489 deep_copy_visitor::visit_functioncall (functioncall
* e
)
3491 functioncall
* n
= new functioncall(*e
);
3492 n
->referent
= NULL
; // don't copy!
3493 update_visitor::visit_functioncall(n
);
3497 deep_copy_visitor::visit_print_format (print_format
* e
)
3499 update_visitor::visit_print_format(new print_format(*e
));
3503 deep_copy_visitor::visit_stat_op (stat_op
* e
)
3505 update_visitor::visit_stat_op(new stat_op(*e
));
3509 deep_copy_visitor::visit_hist_op (hist_op
* e
)
3511 update_visitor::visit_hist_op(new hist_op(*e
));
3514 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */