1 // parse tree functions
2 // Copyright (C) 2005-2015 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
14 #include "stringtable.h"
27 visitable::~visitable ()
32 expression::expression ():
33 type (pe_unknown
), tok (0)
38 expression::~expression ()
43 statement::statement ():
49 statement::statement (const token
* tok
):
55 null_statement::null_statement (const token
* tok
):
61 statement::~statement ()
72 arrayindex::arrayindex ():
78 functioncall::functioncall ():
84 symboldecl::symboldecl ():
85 tok (0), systemtap_v_conditional (0),
91 symboldecl::~symboldecl ()
95 probe_point::probe_point (std::vector
<component
*> const & comps
):
96 components(comps
), optional (false), sufficient (false),
97 well_formed (false), condition (0)
101 // NB: shallow-copy of compoonents & condition!
102 probe_point::probe_point (const probe_point
& pp
):
103 components(pp
.components
), optional (pp
.optional
), sufficient (pp
.sufficient
),
104 well_formed (pp
.well_formed
), condition (pp
.condition
)
109 probe_point::probe_point ():
110 optional (false), sufficient (false), well_formed (false), condition (0)
115 probe_point::from_globby_comp(const std::string
& comp
)
117 vector
<component
*>::const_iterator it
;
118 for (it
= components
.begin(); it
!= components
.end(); it
++)
119 if ((*it
)->functor
== comp
)
120 return (*it
)->from_glob
;
124 unsigned probe::last_probeidx
= 0;
127 body (0), base (0), tok (0), systemtap_v_conditional (0), privileged (false)
129 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
133 // Copy constructor, but with overriding probe-point. To be used when
134 // mapping script-level probe points to another one, early during pass
135 // 2. There should be no symbol resolution done yet.
136 probe::probe(probe
* p
, probe_point
* l
)
139 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
141 this->locations
.push_back(l
);
142 this->body
= deep_copy_visitor::deep_copy(p
->body
);
143 this->privileged
= p
->privileged
;
144 this->systemtap_v_conditional
= p
->systemtap_v_conditional
;
145 assert (p
->locals
.size() == 0);
146 assert (p
->unused_locals
.size() == 0);
150 probe_point::component::component ():
151 arg (0), from_glob(false), tok(0)
156 probe_point::component::component (interned_string f
,
157 literal
* a
, bool from_glob
):
158 functor(f
), arg(a
), from_glob(from_glob
), tok(0)
164 arity_tok(0), arity (-1), maxsize(0), init(NULL
), synthetic(false), wrap(false),
171 vardecl::set_arity (int a
, const token
* t
)
176 if (a
== 0 && maxsize
> 0)
177 throw SEMANTIC_ERROR (_("inconsistent arity"), tok
);
179 if (arity
!= a
&& arity
>= 0)
181 semantic_error
err (ERR_SRC
, _F("inconsistent arity (%s vs %d)",
182 lex_cast(arity
).c_str(), a
), t
?:tok
);
185 semantic_error
chain(ERR_SRC
, _F("arity %s first inferred here",
186 lex_cast(arity
).c_str()), arity_tok
);
187 err
.set_chain(chain
);
196 index_types
.resize (arity
);
197 for (int i
=0; i
<arity
; i
++)
198 index_types
[i
] = pe_unknown
;
203 vardecl::compatible_arity (int a
)
205 if (a
== 0 && maxsize
> 0)
207 if (arity
== -1 || a
== -1)
213 functiondecl::functiondecl ():
214 body (0), synthetic (false), mangle_oldstyle (false)
219 functiondecl::join (systemtap_session
& s
)
222 throw SEMANTIC_ERROR (_("internal error, joining a non-synthetic function"), tok
);
223 if (!s
.functions
.insert (make_pair (name
, this)).second
)
224 throw SEMANTIC_ERROR (_F("synthetic function '%s' conflicts with an existing function",
225 name
.to_string().c_str()), tok
);
226 tok
->location
.file
->functions
.push_back (this);
230 literal_number::literal_number (int64_t v
, bool hex
)
238 literal_string::literal_string (interned_string v
)
246 operator << (ostream
& o
, const exp_type
& e
)
250 case pe_unknown
: o
<< "unknown"; break;
251 case pe_long
: o
<< "long"; break;
252 case pe_string
: o
<< "string"; break;
253 case pe_stats
: o
<< "stats"; break;
254 default: o
<< "???"; break;
261 target_symbol::assert_no_components(const std::string
& tapset
, bool pretty_ok
)
263 if (components
.empty())
266 const string
& name
= this->name
;
267 switch (components
[0].type
)
269 case comp_literal_array_index
:
270 case comp_expression_array_index
:
271 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as array",
272 tapset
.c_str(), name
.c_str()), components
[0].tok
);
273 case comp_struct_member
:
274 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as a structure",
275 tapset
.c_str(), name
.c_str()), components
[0].tok
);
276 case comp_pretty_print
:
278 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be pretty-printed",
279 tapset
.c_str(), name
.c_str()), components
[0].tok
);
282 throw SEMANTIC_ERROR (_F("invalid use of %s variable '%s'",
283 tapset
.c_str(), name
.c_str()), components
[0].tok
);
289 target_symbol::pretty_print_depth () const
291 if (! components
.empty ())
293 const component
& last
= components
.back ();
294 if (last
.type
== comp_pretty_print
)
295 return last
.member
.length ();
302 target_symbol::check_pretty_print (bool lvalue
) const
304 if (pretty_print_depth () == 0)
308 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), tok
);
314 void target_symbol::chain (const semantic_error
&er
)
316 semantic_error
* e
= new semantic_error(er
);
319 assert (e
->get_chain() == 0);
320 if (this->saved_conversion_error
)
322 e
->set_chain(*this->saved_conversion_error
);
323 delete this->saved_conversion_error
;
325 this->saved_conversion_error
= e
;
329 string
target_symbol::sym_name ()
331 return (string
)name
.substr(1);
335 string
atvar_op::sym_name ()
340 return (string
)target_name
.substr(0, target_name
.length() - cu_name
.length() - 1);
344 // ------------------------------------------------------------------------
345 // parse tree printing
347 ostream
& operator << (ostream
& o
, const expression
& k
)
354 void literal_string::print (ostream
& o
) const
357 for (unsigned i
=0; i
<value
.size(); i
++)
358 if (value
[i
] == '"') // or other escapeworthy characters?
366 void literal_number::print (ostream
& o
) const
369 o
<< hex
<< showbase
;
372 o
<< dec
<< noshowbase
;
376 void embedded_expr::print (ostream
& o
) const
378 o
<< "%{ " << code
<< " %}";
382 void binary_expression::print (ostream
& o
) const
384 o
<< "(" << *left
<< ") "
386 << " (" << *right
<< ")";
389 void regex_query::print (ostream
& o
) const
391 // NB: we need a custom printer, because the parser does not accept
392 // a parenthesized RHS.
393 o
<< "(" << *left
<< ") "
399 void unary_expression::print (ostream
& o
) const
401 o
<< op
<< '(' << *operand
<< ")";
404 void array_in::print (ostream
& o
) const
407 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
409 if (i
> 0) o
<< ", ";
410 if (operand
->indexes
[i
])
411 operand
->indexes
[i
]->print (o
);
416 operand
->base
->print (o
);
419 void post_crement::print (ostream
& o
) const
421 o
<< '(' << *operand
<< ")" << op
;
425 void ternary_expression::print (ostream
& o
) const
427 o
<< "(" << *cond
<< ")?("
428 << *truevalue
<< "):("
429 << *falsevalue
<< ")";
433 void symbol::print (ostream
& o
) const
439 void target_symbol::component::print (ostream
& o
) const
443 case comp_pretty_print
:
444 case comp_struct_member
:
447 case comp_literal_array_index
:
448 o
<< '[' << num_index
<< ']';
450 case comp_expression_array_index
:
451 o
<< '[' << *expr_index
<< ']';
457 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
464 void target_symbol::print (ostream
& o
) const
469 for (unsigned i
= 0; i
< components
.size(); ++i
)
474 void atvar_op::print (ostream
& o
) const
478 o
<< name
<< "(\"" << target_name
<< "\")";
479 for (unsigned i
= 0; i
< components
.size(); ++i
)
484 void cast_op::print (ostream
& o
) const
488 o
<< name
<< '(' << *operand
;
489 o
<< ", " << lex_cast_qstring (type_name
);
490 if (module
.length() > 0)
491 o
<< ", " << lex_cast_qstring (module
);
493 for (unsigned i
= 0; i
< components
.size(); ++i
)
498 void autocast_op::print (ostream
& o
) const
502 o
<< '(' << *operand
<< ')';
503 for (unsigned i
= 0; i
< components
.size(); ++i
)
508 void defined_op::print (ostream
& o
) const
510 o
<< "@defined(" << *operand
<< ")";
514 void entry_op::print (ostream
& o
) const
516 o
<< "@entry(" << *operand
<< ")";
520 void perf_op::print (ostream
& o
) const
522 o
<< "@perf(" << *operand
<< ")";
526 void vardecl::print (ostream
& o
) const
532 o
<< "[" << maxsize
<< "]";
533 if (arity
> 0 || index_types
.size() > 0)
543 void vardecl::printsig (ostream
& o
) const
549 o
<< "[" << maxsize
<< "]";
551 if (index_types
.size() > 0)
554 for (unsigned i
=0; i
<index_types
.size(); i
++)
555 o
<< (i
>0 ? ", " : "") << index_types
[i
];
561 void functiondecl::print (ostream
& o
) const
563 o
<< "function " << name
<< " (";
564 for (unsigned i
=0; i
<formal_args
.size(); i
++)
565 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
571 void functiondecl::printsig (ostream
& o
) const
573 o
<< tok
->content
<< ":" << type
<< " (";
574 for (unsigned i
=0; i
<formal_args
.size(); i
++)
575 o
<< (i
>0 ? ", " : "")
578 << formal_args
[i
]->type
;
582 embedded_tags_visitor::embedded_tags_visitor(bool all_tags
)
584 // populate the set of tags that could appear in embedded code/expressions
585 available_tags
.insert("/* guru */");
586 available_tags
.insert("/* unprivileged */");
587 available_tags
.insert("/* myproc-unprivileged */");
590 available_tags
.insert("/* pure */");
591 available_tags
.insert("/* unmangled */");
592 available_tags
.insert("/* unmodified-fnargs */");
593 available_tags
.insert("/* stable */");
597 bool embedded_tags_visitor::tagged_p (const std::string
& tag
)
599 return tags
.count(tag
);
602 void embedded_tags_visitor::find_tags_in_code (const string
& s
)
604 set
<string
>::iterator tag
;
605 for (tag
= available_tags
.begin(); tag
!= available_tags
.end(); ++tag
)
606 if(s
.find(*tag
) != string::npos
)
610 void embedded_tags_visitor::visit_embeddedcode (embeddedcode
*s
)
612 find_tags_in_code(s
->code
);
615 void embedded_tags_visitor::visit_embedded_expr (embedded_expr
*e
)
617 find_tags_in_code(e
->code
);
620 void functiondecl::printsigtags (ostream
& o
, bool all_tags
) const
624 // Visit the function's body to see if there's any embedded_code or
625 // embeddedexpr that have special tags (e.g. /* guru */)
626 embedded_tags_visitor
etv(all_tags
);
627 this->body
->visit(&etv
);
629 set
<string
, bool>::const_iterator tag
;
630 for (tag
= etv
.available_tags
.begin(); tag
!= etv
.available_tags
.end(); ++tag
)
631 if (etv
.tagged_p(*tag
))
635 void arrayindex::print (ostream
& o
) const
639 for (unsigned i
=0; i
<indexes
.size(); i
++)
641 o
<< (i
>0 ? ", " : "");
651 void functioncall::print (ostream
& o
) const
653 o
<< function
<< "(";
654 for (unsigned i
=0; i
<args
.size(); i
++)
655 o
<< (i
>0 ? ", " : "") << *args
[i
];
661 print_format::create(const token
*t
, const char *n
)
663 bool stream
, format
, delim
, newline
, _char
;
664 interned_string type
;
671 n
= str_type
.c_str();
677 format
= delim
= newline
= _char
= false;
679 if (strcmp(n
, "print_char") == 0)
689 if (0 != strncmp(n
, "print", 5))
706 if (*n
== 'l' && *(n
+1) == 'n')
717 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
, type
);
724 print_format::components_to_string(vector
<format_component
> const & components
)
728 for (vector
<format_component
>::const_iterator i
= components
.begin();
729 i
!= components
.end(); ++i
)
732 assert (i
->type
!= conv_unspecified
);
734 if (i
->type
== conv_literal
)
736 assert(!i
->literal_string
.empty());
737 for (interned_string::const_iterator j
= i
->literal_string
.begin();
738 j
!= i
->literal_string
.end(); ++j
)
740 // See also: c_unparser::visit_literal_string and lex_cast_qstring
752 if (i
->test_flag (fmt_flag_zeropad
))
755 if (i
->test_flag (fmt_flag_plus
))
758 if (i
->test_flag (fmt_flag_space
))
761 if (i
->test_flag (fmt_flag_left
))
764 if (i
->test_flag (fmt_flag_special
))
767 if (i
->widthtype
== width_dynamic
)
769 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
772 if (i
->prectype
== prec_dynamic
)
774 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
775 oss
<< '.' << i
->precision
;
790 if (i
->test_flag (fmt_flag_large
))
795 else if (i
->base
== 8)
797 else if (i
->test_flag (fmt_flag_sign
))
815 case conv_memory_hex
:
827 vector
<print_format::format_component
>
828 print_format::string_to_components(string
const & str
)
830 format_component curr
;
831 vector
<format_component
> res
;
835 string::const_iterator i
= str
.begin();
838 while (i
!= str
.end())
842 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
843 curr
.type
= conv_literal
;
848 else if (i
+1 == str
.end() || *(i
+1) == '%')
851 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
852 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
853 curr
.type
= conv_literal
;
861 if (curr
.type
!= conv_unspecified
)
863 // Flush any component we were previously accumulating
864 assert (curr
.type
== conv_literal
);
865 curr
.literal_string
= literal_str
;
876 // Now we are definitely parsing a conversion.
877 // Begin by parsing flags (which are optional).
882 curr
.set_flag (fmt_flag_zeropad
);
887 curr
.set_flag (fmt_flag_plus
);
892 curr
.set_flag (fmt_flag_left
);
897 curr
.set_flag (fmt_flag_space
);
902 curr
.set_flag (fmt_flag_special
);
913 // Parse optional width
916 curr
.widthtype
= width_dynamic
;
919 else if (isdigit(*i
))
921 curr
.widthtype
= width_static
;
926 curr
.width
+= (*i
- '0');
929 while (i
!= str
.end() && isdigit(*i
));
935 // Parse optional precision
943 curr
.prectype
= prec_dynamic
;
946 else if (isdigit(*i
))
948 curr
.prectype
= prec_static
;
952 curr
.precision
*= 10;
953 curr
.precision
+= (*i
- '0');
956 while (i
!= str
.end() && isdigit(*i
));
963 // Parse the type modifier
974 // Parse the actual conversion specifier (bcsmdioupxXn)
977 // Valid conversion types
979 curr
.type
= conv_binary
;
983 curr
.type
= conv_char
;
987 curr
.type
= conv_string
;
991 curr
.type
= conv_memory
;
995 curr
.type
= conv_memory_hex
;
1000 curr
.set_flag (fmt_flag_sign
);
1002 curr
.type
= conv_number
;
1007 curr
.type
= conv_number
;
1012 curr
.set_flag (fmt_flag_large
);
1014 curr
.type
= conv_number
;
1019 // Since stap 1.3, %p == %#x.
1020 curr
.set_flag (fmt_flag_special
);
1021 curr
.type
= conv_pointer
;
1023 // Oddness for stap < 1.3 is handled in translation
1030 if (curr
.type
== conv_unspecified
)
1031 throw PARSE_ERROR(_("invalid or missing conversion specifier"));
1034 curr
.literal_string
= literal_str
;
1035 res
.push_back(curr
);
1036 literal_str
.clear();
1040 // If there's a remaining partly-composed conversion, fail.
1041 if (!curr
.is_empty())
1043 if (curr
.type
== conv_literal
)
1045 curr
.literal_string
= literal_str
;
1046 res
.push_back(curr
);
1047 // no need to clear curr / literal_str; we're leaving
1050 throw PARSE_ERROR(_("trailing incomplete print format conversion"));
1057 void print_format::print (ostream
& o
) const
1059 o
<< print_format_type
<< "(";
1060 if (print_with_format
)
1061 o
<< lex_cast_qstring (raw_components
);
1062 if (print_with_delim
)
1063 o
<< lex_cast_qstring (delimiter
.literal_string
);
1066 for (vector
<expression
*>::const_iterator i
= args
.begin();
1067 i
!= args
.end(); ++i
)
1069 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
1076 void stat_op::print (ostream
& o
) const
1102 assert (0); // should not happen, as sc_none is only used in foreach sorts
1110 hist_op::print (ostream
& o
) const
1116 assert(params
.size() == 3);
1117 o
<< "hist_linear(";
1119 for (size_t i
= 0; i
< params
.size(); ++i
)
1121 o
<< ", " << params
[i
];
1127 assert(params
.size() == 0);
1135 ostream
& operator << (ostream
& o
, const statement
& k
)
1142 void embeddedcode::print (ostream
&o
) const
1149 void block::print (ostream
& o
) const
1152 for (unsigned i
=0; i
<statements
.size(); i
++)
1153 o
<< *statements
[i
] << endl
;
1157 block::block (statement
* car
, statement
* cdr
)
1159 statements
.push_back(car
);
1160 statements
.push_back(cdr
);
1161 this->tok
= car
->tok
;
1166 void try_block::print (ostream
& o
) const
1168 o
<< "try {" << endl
;
1169 if (try_block
) o
<< *try_block
<< endl
;
1171 if (catch_error_var
) o
<< "(" << *catch_error_var
<< ") ";
1173 if (catch_block
) o
<< *catch_block
<< endl
;
1178 void for_loop::print (ostream
& o
) const
1181 if (init
) init
->print (o
);
1185 if (incr
) incr
->print (o
);
1191 void foreach_loop::print (ostream
& o
) const
1200 for (unsigned i
=0; i
<indexes
.size(); i
++)
1202 if (i
> 0) o
<< ", ";
1203 indexes
[i
]->print (o
);
1204 if (sort_direction
!= 0 && sort_column
== i
+1)
1205 o
<< (sort_direction
> 0 ? "+" : "-");
1209 if (!array_slice
.empty())
1212 for (unsigned i
=0; i
<array_slice
.size(); i
++)
1214 if (i
> 0) o
<< ", ";
1216 array_slice
[i
]->print (o
);
1222 if (sort_direction
!= 0 && sort_column
== 0)
1226 case sc_count
: o
<< " @count"; break;
1227 case sc_average
: o
<< " @avg"; break;
1228 case sc_min
: o
<< " @min"; break;
1229 case sc_max
: o
<< " @max"; break;
1230 case sc_sum
: o
<< " @sum"; break;
1236 o
<< (sort_direction
> 0 ? "+" : "-");
1248 void null_statement::print (ostream
& o
) const
1254 void expr_statement::print (ostream
& o
) const
1260 void return_statement::print (ostream
& o
) const
1262 o
<< "return " << *value
;
1266 void delete_statement::print (ostream
& o
) const
1268 o
<< "delete " << *value
;
1271 void next_statement::print (ostream
& o
) const
1276 void break_statement::print (ostream
& o
) const
1281 void continue_statement::print (ostream
& o
) const
1286 void if_statement::print (ostream
& o
) const
1288 o
<< "if (" << *condition
<< ") "
1289 << *thenblock
<< endl
;
1291 o
<< "else " << *elseblock
<< endl
;
1295 void stapfile::print (ostream
& o
) const
1297 o
<< "# file " << name
<< endl
;
1299 for (unsigned i
=0; i
<embeds
.size(); i
++)
1300 embeds
[i
]->print (o
);
1302 for (unsigned i
=0; i
<globals
.size(); i
++)
1305 globals
[i
]->print (o
);
1309 for (unsigned i
=0; i
<aliases
.size(); i
++)
1311 aliases
[i
]->print (o
);
1315 for (unsigned i
=0; i
<probes
.size(); i
++)
1317 probes
[i
]->print (o
);
1321 for (unsigned j
= 0; j
< functions
.size(); j
++)
1323 functions
[j
]->print (o
);
1329 void probe::print (ostream
& o
) const
1337 void probe::printsig (ostream
& o
) const
1339 const probe_alias
*alias
= get_alias ();
1342 alias
->printsig (o
);
1346 for (unsigned i
=0; i
<locations
.size(); i
++)
1348 if (i
> 0) o
<< ",";
1349 locations
[i
]->print (o
);
1355 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
) const
1357 probes_list
.push_back(const_cast<probe
*>(this));
1359 base
->collect_derivation_chain(probes_list
);
1364 probe::collect_derivation_pp_chain (std::vector
<probe_point
*> &pp_list
) const
1366 pp_list
.push_back(const_cast<probe_point
*>(this->locations
[0]));
1368 base
->collect_derivation_pp_chain(pp_list
);
1373 void probe_point::print (ostream
& o
, bool print_extras
) const
1375 for (unsigned i
=0; i
<components
.size(); i
++)
1378 probe_point::component
* c
= components
[i
];
1381 // We might like to excise this weird 0 pointer, just in
1382 // case some recursion during the error-handling path, but
1383 // can't, because what we have here is a const *this.
1384 static int been_here
= 0;
1385 if (been_here
== 0) {
1387 throw SEMANTIC_ERROR (_("internal error: missing probe point component"));
1389 continue; // ... sad panda decides to skip the bad boy
1393 o
<< "(" << *c
->arg
<< ")";
1399 else if (optional
) // sufficient implies optional
1402 o
<< " if (" << *condition
<< ")";
1405 string
probe_point::str (bool print_extras
) const
1408 print(o
, print_extras
);
1413 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1414 probe (), alias_names (aliases
), epilogue_style(false)
1418 void probe_alias::printsig (ostream
& o
) const
1420 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1422 o
<< (i
>0 ? " = " : "");
1423 alias_names
[i
]->print (o
);
1425 o
<< (epilogue_style
? " += " : " = ");
1426 for (unsigned i
=0; i
<locations
.size(); i
++)
1428 if (i
> 0) o
<< ", ";
1429 locations
[i
]->print (o
);
1434 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1441 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1449 // ------------------------------------------------------------------------
1454 block::visit (visitor
* u
)
1456 u
->visit_block (this);
1461 try_block::visit (visitor
* u
)
1463 u
->visit_try_block (this);
1468 embeddedcode::visit (visitor
* u
)
1470 u
->visit_embeddedcode (this);
1475 for_loop::visit (visitor
* u
)
1477 u
->visit_for_loop (this);
1481 foreach_loop::visit (visitor
* u
)
1483 u
->visit_foreach_loop (this);
1487 null_statement::visit (visitor
* u
)
1489 u
->visit_null_statement (this);
1493 expr_statement::visit (visitor
* u
)
1495 u
->visit_expr_statement (this);
1499 return_statement::visit (visitor
* u
)
1501 u
->visit_return_statement (this);
1505 delete_statement::visit (visitor
* u
)
1507 u
->push_active_lvalue (this->value
);
1508 u
->visit_delete_statement (this);
1509 u
->pop_active_lvalue ();
1513 if_statement::visit (visitor
* u
)
1515 u
->visit_if_statement (this);
1519 next_statement::visit (visitor
* u
)
1521 u
->visit_next_statement (this);
1525 break_statement::visit (visitor
* u
)
1527 u
->visit_break_statement (this);
1531 continue_statement::visit (visitor
* u
)
1533 u
->visit_continue_statement (this);
1537 literal_string::visit(visitor
* u
)
1539 u
->visit_literal_string (this);
1543 literal_number::visit(visitor
* u
)
1545 u
->visit_literal_number (this);
1549 binary_expression::visit (visitor
* u
)
1551 u
->visit_binary_expression (this);
1555 embedded_expr::visit (visitor
* u
)
1557 u
->visit_embedded_expr (this);
1561 unary_expression::visit (visitor
* u
)
1563 u
->visit_unary_expression (this);
1567 pre_crement::visit (visitor
* u
)
1569 u
->push_active_lvalue (this->operand
);
1570 u
->visit_pre_crement (this);
1571 u
->pop_active_lvalue ();
1575 post_crement::visit (visitor
* u
)
1577 u
->push_active_lvalue (this->operand
);
1578 u
->visit_post_crement (this);
1579 u
->pop_active_lvalue ();
1583 logical_or_expr::visit (visitor
* u
)
1585 u
->visit_logical_or_expr (this);
1589 logical_and_expr::visit (visitor
* u
)
1591 u
->visit_logical_and_expr (this);
1595 array_in::visit (visitor
* u
)
1597 u
->visit_array_in (this);
1601 regex_query::visit (visitor
* u
)
1603 u
->visit_regex_query (this);
1607 comparison::visit (visitor
* u
)
1609 u
->visit_comparison (this);
1613 concatenation::visit (visitor
* u
)
1615 u
->visit_concatenation (this);
1619 ternary_expression::visit (visitor
* u
)
1621 u
->visit_ternary_expression (this);
1625 assignment::visit (visitor
* u
)
1627 u
->push_active_lvalue (this->left
);
1628 u
->visit_assignment (this);
1629 u
->pop_active_lvalue ();
1633 symbol::visit (visitor
* u
)
1635 u
->visit_symbol (this);
1639 target_symbol::visit (visitor
* u
)
1641 u
->visit_target_symbol(this);
1645 target_symbol::visit_components (visitor
* u
)
1647 for (unsigned i
= 0; i
< components
.size(); ++i
)
1648 if (components
[i
].type
== comp_expression_array_index
)
1649 components
[i
].expr_index
->visit (u
);
1653 target_symbol::visit_components (update_visitor
* u
)
1655 for (unsigned i
= 0; i
< components
.size(); ++i
)
1656 if (components
[i
].type
== comp_expression_array_index
)
1657 u
->replace (components
[i
].expr_index
);
1661 cast_op::visit (visitor
* u
)
1663 u
->visit_cast_op(this);
1668 autocast_op::visit (visitor
* u
)
1670 u
->visit_autocast_op(this);
1675 atvar_op::visit (visitor
* u
)
1677 u
->visit_atvar_op(this);
1682 defined_op::visit (visitor
* u
)
1684 u
->visit_defined_op(this);
1689 entry_op::visit (visitor
* u
)
1691 u
->visit_entry_op(this);
1696 perf_op::visit (visitor
* u
)
1698 u
->visit_perf_op(this);
1703 arrayindex::visit (visitor
* u
)
1705 u
->visit_arrayindex (this);
1709 functioncall::visit (visitor
* u
)
1711 u
->visit_functioncall (this);
1715 print_format::visit (visitor
*u
)
1717 u
->visit_print_format (this);
1721 stat_op::visit (visitor
*u
)
1723 u
->visit_stat_op (this);
1727 hist_op::visit (visitor
*u
)
1729 u
->visit_hist_op (this);
1734 expression::is_symbol(symbol
*& sym_out
)
1741 indexable::is_symbol(symbol
*& sym_out
)
1748 indexable::is_hist_op(hist_op
*& hist_out
)
1755 symbol::is_symbol(symbol
*& sym_out
)
1762 hist_op::is_hist_op(hist_op
*& hist_out
)
1769 classify_indexable(indexable
* ix
,
1770 symbol
*& array_out
,
1771 hist_op
*& hist_out
)
1776 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1777 throw SEMANTIC_ERROR(_("Expecting symbol or histogram operator"), ix
->tok
);
1778 if (!(hist_out
|| array_out
))
1779 throw SEMANTIC_ERROR(_("Failed to classify indexable"), ix
->tok
);
1783 // ------------------------------------------------------------------------
1786 visitor::is_active_lvalue(expression
*e
)
1788 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1790 if (active_lvalues
[i
] == e
)
1797 visitor::push_active_lvalue(expression
*e
)
1799 active_lvalues
.push_back(e
);
1803 visitor::pop_active_lvalue()
1805 assert(!active_lvalues
.empty());
1806 active_lvalues
.pop_back();
1811 // ------------------------------------------------------------------------
1814 traversing_visitor::visit_block (block
* s
)
1816 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1817 s
->statements
[i
]->visit (this);
1821 traversing_visitor::visit_try_block (try_block
* s
)
1824 s
->try_block
->visit (this);
1825 if (s
->catch_error_var
)
1826 s
->catch_error_var
->visit (this);
1828 s
->catch_block
->visit (this);
1832 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1837 traversing_visitor::visit_null_statement (null_statement
*)
1842 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1844 s
->value
->visit (this);
1848 traversing_visitor::visit_if_statement (if_statement
* s
)
1850 s
->condition
->visit (this);
1851 s
->thenblock
->visit (this);
1853 s
->elseblock
->visit (this);
1857 traversing_visitor::visit_for_loop (for_loop
* s
)
1859 if (s
->init
) s
->init
->visit (this);
1860 s
->cond
->visit (this);
1861 if (s
->incr
) s
->incr
->visit (this);
1862 s
->block
->visit (this);
1866 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1868 s
->base
->visit(this);
1870 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1871 s
->indexes
[i
]->visit (this);
1874 s
->value
->visit (this);
1877 s
->limit
->visit (this);
1879 s
->block
->visit (this);
1883 traversing_visitor::visit_return_statement (return_statement
* s
)
1885 s
->value
->visit (this);
1889 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1891 s
->value
->visit (this);
1895 traversing_visitor::visit_next_statement (next_statement
*)
1900 traversing_visitor::visit_break_statement (break_statement
*)
1905 traversing_visitor::visit_continue_statement (continue_statement
*)
1910 traversing_visitor::visit_literal_string (literal_string
*)
1915 traversing_visitor::visit_literal_number (literal_number
*)
1920 traversing_visitor::visit_embedded_expr (embedded_expr
*)
1925 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1927 e
->left
->visit (this);
1928 e
->right
->visit (this);
1932 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1934 e
->operand
->visit (this);
1938 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1940 e
->operand
->visit (this);
1944 traversing_visitor::visit_post_crement (post_crement
* e
)
1946 e
->operand
->visit (this);
1951 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1953 e
->left
->visit (this);
1954 e
->right
->visit (this);
1958 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1960 e
->left
->visit (this);
1961 e
->right
->visit (this);
1965 traversing_visitor::visit_array_in (array_in
* e
)
1967 e
->operand
->visit (this);
1971 traversing_visitor::visit_regex_query (regex_query
* e
)
1973 e
->left
->visit (this);
1974 e
->right
->visit (this);
1978 traversing_visitor::visit_comparison (comparison
* e
)
1980 e
->left
->visit (this);
1981 e
->right
->visit (this);
1985 traversing_visitor::visit_concatenation (concatenation
* e
)
1987 e
->left
->visit (this);
1988 e
->right
->visit (this);
1992 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1994 e
->cond
->visit (this);
1995 e
->truevalue
->visit (this);
1996 e
->falsevalue
->visit (this);
2000 traversing_visitor::visit_assignment (assignment
* e
)
2002 e
->left
->visit (this);
2003 e
->right
->visit (this);
2007 traversing_visitor::visit_symbol (symbol
*)
2012 traversing_visitor::visit_target_symbol (target_symbol
* e
)
2014 e
->visit_components (this);
2018 traversing_visitor::visit_cast_op (cast_op
* e
)
2020 e
->operand
->visit (this);
2021 e
->visit_components (this);
2025 traversing_visitor::visit_autocast_op (autocast_op
* e
)
2027 e
->operand
->visit (this);
2028 e
->visit_components (this);
2032 traversing_visitor::visit_atvar_op (atvar_op
* e
)
2034 e
->visit_components (this);
2038 traversing_visitor::visit_defined_op (defined_op
* e
)
2040 e
->operand
->visit (this);
2044 traversing_visitor::visit_entry_op (entry_op
* e
)
2046 e
->operand
->visit (this);
2051 traversing_visitor::visit_perf_op (perf_op
* e
)
2053 e
->operand
->visit (this);
2058 traversing_visitor::visit_arrayindex (arrayindex
* e
)
2060 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2062 e
->indexes
[i
]->visit (this);
2064 e
->base
->visit(this);
2068 traversing_visitor::visit_functioncall (functioncall
* e
)
2070 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2071 e
->args
[i
]->visit (this);
2075 traversing_visitor::visit_print_format (print_format
* e
)
2077 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2078 e
->args
[i
]->visit (this);
2080 e
->hist
->visit(this);
2084 traversing_visitor::visit_stat_op (stat_op
* e
)
2086 e
->stat
->visit (this);
2090 traversing_visitor::visit_hist_op (hist_op
* e
)
2092 e
->stat
->visit (this);
2097 expression_visitor::visit_literal_string (literal_string
* e
)
2099 traversing_visitor::visit_literal_string (e
);
2100 visit_expression (e
);
2104 expression_visitor::visit_literal_number (literal_number
* e
)
2106 traversing_visitor::visit_literal_number (e
);
2107 visit_expression (e
);
2111 expression_visitor::visit_embedded_expr (embedded_expr
* e
)
2113 traversing_visitor::visit_embedded_expr (e
);
2114 visit_expression (e
);
2118 expression_visitor::visit_binary_expression (binary_expression
* e
)
2120 traversing_visitor::visit_binary_expression (e
);
2121 visit_expression (e
);
2125 expression_visitor::visit_unary_expression (unary_expression
* e
)
2127 traversing_visitor::visit_unary_expression (e
);
2128 visit_expression (e
);
2132 expression_visitor::visit_pre_crement (pre_crement
* e
)
2134 traversing_visitor::visit_pre_crement (e
);
2135 visit_expression (e
);
2139 expression_visitor::visit_post_crement (post_crement
* e
)
2141 traversing_visitor::visit_post_crement (e
);
2142 visit_expression (e
);
2146 expression_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2148 traversing_visitor::visit_logical_or_expr (e
);
2149 visit_expression (e
);
2153 expression_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2155 traversing_visitor::visit_logical_and_expr (e
);
2156 visit_expression (e
);
2160 expression_visitor::visit_array_in (array_in
* e
)
2162 traversing_visitor::visit_array_in (e
);
2163 visit_expression (e
);
2167 expression_visitor::visit_regex_query (regex_query
* e
)
2169 traversing_visitor::visit_regex_query (e
);
2170 visit_expression (e
);
2174 expression_visitor::visit_comparison (comparison
* e
)
2176 traversing_visitor::visit_comparison (e
);
2177 visit_expression (e
);
2181 expression_visitor::visit_concatenation (concatenation
* e
)
2183 traversing_visitor::visit_concatenation (e
);
2184 visit_expression (e
);
2188 expression_visitor::visit_ternary_expression (ternary_expression
* e
)
2190 traversing_visitor::visit_ternary_expression (e
);
2191 visit_expression (e
);
2195 expression_visitor::visit_assignment (assignment
* e
)
2197 traversing_visitor::visit_assignment (e
);
2198 visit_expression (e
);
2202 expression_visitor::visit_symbol (symbol
* e
)
2204 traversing_visitor::visit_symbol (e
);
2205 visit_expression (e
);
2209 expression_visitor::visit_target_symbol (target_symbol
* e
)
2211 traversing_visitor::visit_target_symbol (e
);
2212 visit_expression (e
);
2216 expression_visitor::visit_arrayindex (arrayindex
* e
)
2218 traversing_visitor::visit_arrayindex (e
);
2219 visit_expression (e
);
2223 expression_visitor::visit_functioncall (functioncall
* e
)
2225 traversing_visitor::visit_functioncall (e
);
2226 visit_expression (e
);
2230 expression_visitor::visit_print_format (print_format
* e
)
2232 traversing_visitor::visit_print_format (e
);
2233 visit_expression (e
);
2237 expression_visitor::visit_stat_op (stat_op
* e
)
2239 traversing_visitor::visit_stat_op (e
);
2240 visit_expression (e
);
2244 expression_visitor::visit_hist_op (hist_op
* e
)
2246 traversing_visitor::visit_hist_op (e
);
2247 visit_expression (e
);
2251 expression_visitor::visit_cast_op (cast_op
* e
)
2253 traversing_visitor::visit_cast_op (e
);
2254 visit_expression (e
);
2258 expression_visitor::visit_autocast_op (autocast_op
* e
)
2260 traversing_visitor::visit_autocast_op (e
);
2261 visit_expression (e
);
2265 expression_visitor::visit_atvar_op (atvar_op
* e
)
2267 traversing_visitor::visit_atvar_op (e
);
2268 visit_expression (e
);
2272 expression_visitor::visit_defined_op (defined_op
* e
)
2274 traversing_visitor::visit_defined_op (e
);
2275 visit_expression (e
);
2279 expression_visitor::visit_entry_op (entry_op
* e
)
2281 traversing_visitor::visit_entry_op (e
);
2282 visit_expression (e
);
2286 expression_visitor::visit_perf_op (perf_op
* e
)
2288 traversing_visitor::visit_perf_op (e
);
2289 visit_expression (e
);
2294 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
2296 traversing_visitor::visit_functioncall (e
);
2297 this->enter_functioncall(e
);
2301 functioncall_traversing_visitor::enter_functioncall (functioncall
* e
)
2303 // prevent infinite recursion
2304 if (nested
.find (e
->referent
) == nested
.end ())
2306 if (seen
.find(e
->referent
) == seen
.end())
2307 seen
.insert (e
->referent
);
2308 nested
.insert (e
->referent
);
2310 functiondecl
* last_current_function
= current_function
;
2311 current_function
= e
->referent
;
2312 e
->referent
->body
->visit (this);
2313 current_function
= last_current_function
;
2314 nested
.erase (e
->referent
);
2316 else { this->note_recursive_functioncall(e
); }
2320 functioncall_traversing_visitor::note_recursive_functioncall (functioncall
* e
)
2325 varuse_collecting_visitor::visit_if_statement (if_statement
*s
)
2327 assert(!current_lvalue_read
);
2328 current_lvalue_read
= true;
2329 s
->condition
->visit (this);
2330 current_lvalue_read
= false;
2332 s
->thenblock
->visit (this);
2334 s
->elseblock
->visit (this);
2339 varuse_collecting_visitor::visit_for_loop (for_loop
*s
)
2341 if (s
->init
) s
->init
->visit (this);
2343 assert(!current_lvalue_read
);
2344 current_lvalue_read
= true;
2345 s
->cond
->visit (this);
2346 current_lvalue_read
= false;
2348 if (s
->incr
) s
->incr
->visit (this);
2349 s
->block
->visit (this);
2353 varuse_collecting_visitor::visit_try_block (try_block
*s
)
2356 s
->try_block
->visit (this);
2357 if (s
->catch_error_var
)
2358 written
.insert (s
->catch_error_var
->referent
);
2360 s
->catch_block
->visit (this);
2362 // NB: don't functioncall_traversing_visitor::visit_try_block (s);
2363 // since that would count s->catch_error_var as a read also.
2367 varuse_collecting_visitor::visit_functioncall (functioncall
* e
)
2369 // NB: don't call functioncall_traversing_visitor::visit_functioncall(). We
2370 // replicate functionality here but split argument visiting from actual
2371 // function visiting.
2373 bool last_lvalue_read
= current_lvalue_read
;
2375 // arguments are used
2376 current_lvalue_read
= true;
2377 traversing_visitor::visit_functioncall(e
);
2379 // but function body shouldn't all be marked used
2380 current_lvalue_read
= false;
2381 functioncall_traversing_visitor::enter_functioncall(e
);
2383 current_lvalue_read
= last_lvalue_read
;
2387 varuse_collecting_visitor::visit_return_statement (return_statement
*s
)
2389 assert(!current_lvalue_read
);
2390 current_lvalue_read
= true;
2391 functioncall_traversing_visitor::visit_return_statement(s
);
2392 current_lvalue_read
= false;
2397 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
2399 assert (current_function
); // only they get embedded code
2401 // Don't allow embedded C functions in unprivileged mode unless
2402 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2403 // or we're in a usermode runtime.
2404 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2405 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2406 ! session
.runtime_usermode_p () &&
2407 s
->code
.find ("/* unprivileged */") == string::npos
&&
2408 s
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2409 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
2410 pr_name (session
.privilege
)),
2411 current_function
->tok
);
2413 // Don't allow /* guru */ functions unless -g is active.
2414 if (!session
.guru_mode
&& s
->code
.find ("/* guru */") != string::npos
)
2415 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
2416 current_function
->tok
);
2418 // PR14524: Support old-style THIS->local syntax on per-function basis.
2419 if (s
->code
.find ("/* unmangled */") != string::npos
)
2420 current_function
->mangle_oldstyle
= true;
2422 // We want to elide embedded-C functions when possible. For
2423 // example, each $target variable access is expanded to an
2424 // embedded-C function call. Yet, for safety reasons, we should
2425 // presume that embedded-C functions have intentional side-effects.
2427 // To tell these two types of functions apart, we apply a
2428 // Kludge(tm): we look for a magic string within the function body.
2429 // $target variables as rvalues will have this; lvalues won't.
2430 // Also, explicit side-effect-free tapset functions will have this.
2432 if (s
->code
.find ("/* pure */") != string::npos
)
2435 embedded_seen
= true;
2439 // About the same case as above.
2441 varuse_collecting_visitor::visit_embedded_expr (embedded_expr
*e
)
2443 // Don't allow embedded C expressions in unprivileged mode unless
2444 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2445 // or we're in a usermode runtime.
2446 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2447 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2448 ! session
.runtime_usermode_p () &&
2449 e
->code
.find ("/* unprivileged */") == string::npos
&&
2450 e
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2451 throw SEMANTIC_ERROR (_F("embedded expression may not be used when --privilege=%s is specified",
2452 pr_name (session
.privilege
)),
2455 // Don't allow /* guru */ functions unless -g is active.
2456 if (!session
.guru_mode
&& e
->code
.find ("/* guru */") != string::npos
)
2457 throw SEMANTIC_ERROR (_("embedded expression may not be used unless -g is specified"),
2460 // We want to elide embedded-C functions when possible. For
2461 // example, each $target variable access is expanded to an
2462 // embedded-C function call. Yet, for safety reasons, we should
2463 // presume that embedded-C functions have intentional side-effects.
2465 // To tell these two types of functions apart, we apply a
2466 // Kludge(tm): we look for a magic string within the function body.
2467 // $target variables as rvalues will have this; lvalues won't.
2468 // Also, explicit side-effect-free tapset functions will have this.
2470 if (e
->code
.find ("/* pure */") != string::npos
)
2473 embedded_seen
= true;
2478 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
2480 // Still-unresolved target symbol assignments get treated as
2481 // generating side-effects like embedded-C, to prevent premature
2482 // elision and later error message suppression (PR5516). rvalue use
2483 // of unresolved target symbols is OTOH not considered a side-effect.
2485 if (is_active_lvalue (e
))
2486 embedded_seen
= true;
2488 functioncall_traversing_visitor::visit_target_symbol (e
);
2493 varuse_collecting_visitor::visit_atvar_op (atvar_op
*e
)
2495 // Similar to visit_target_symbol
2497 if (is_active_lvalue (e
))
2498 embedded_seen
= true;
2500 functioncall_traversing_visitor::visit_atvar_op (e
);
2505 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
2507 // As with target_symbols, unresolved cast assignments need to preserved
2508 // for later error handling.
2509 if (is_active_lvalue (e
))
2510 embedded_seen
= true;
2512 functioncall_traversing_visitor::visit_cast_op (e
);
2516 varuse_collecting_visitor::visit_autocast_op (autocast_op
*e
)
2518 // As with target_symbols, unresolved cast assignments need to preserved
2519 // for later error handling.
2520 if (is_active_lvalue (e
))
2521 embedded_seen
= true;
2523 functioncall_traversing_visitor::visit_autocast_op (e
);
2527 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
2530 functioncall_traversing_visitor::visit_defined_op (e
);
2534 varuse_collecting_visitor::visit_entry_op (entry_op
*e
)
2537 functioncall_traversing_visitor::visit_entry_op (e
);
2542 varuse_collecting_visitor::visit_perf_op (perf_op
*e
)
2544 functioncall_traversing_visitor::visit_perf_op (e
);
2549 varuse_collecting_visitor::visit_print_format (print_format
* e
)
2551 // NB: Instead of being top-level statements, "print" and "printf"
2552 // are implemented as statement-expressions containing a
2553 // print_format. They have side-effects, but not via the
2554 // embedded-code detection method above.
2556 // But sprint and sprintf don't have side-effects.
2558 bool last_lvalue_read
= current_lvalue_read
;
2559 current_lvalue_read
= true;
2560 if (e
->print_to_stream
)
2561 embedded_seen
= true; // a proxy for "has unknown side-effects"
2563 functioncall_traversing_visitor::visit_print_format (e
);
2564 current_lvalue_read
= last_lvalue_read
;
2569 varuse_collecting_visitor::visit_assignment (assignment
*e
)
2571 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
2573 expression
* last_lvalue
= current_lvalue
;
2574 bool last_lvalue_read
= current_lvalue_read
;
2575 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
2576 current_lvalue_read
= true;
2577 functioncall_traversing_visitor::visit_assignment (e
);
2578 current_lvalue
= last_lvalue
;
2579 current_lvalue_read
= last_lvalue_read
;
2581 else // read-modify-writes
2583 expression
* last_lrvalue
= current_lrvalue
;
2584 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
2585 functioncall_traversing_visitor::visit_assignment (e
);
2586 current_lrvalue
= last_lrvalue
;
2591 varuse_collecting_visitor::visit_ternary_expression (ternary_expression
* e
)
2593 // NB: don't call base class's implementation. We do the work here already.
2595 bool last_lvalue_read
= current_lvalue_read
;
2596 current_lvalue_read
= true;
2597 e
->cond
->visit (this);
2598 current_lvalue_read
= last_lvalue_read
;
2600 e
->truevalue
->visit (this);
2601 e
->falsevalue
->visit (this);
2605 varuse_collecting_visitor::visit_symbol (symbol
*e
)
2607 if (e
->referent
== 0)
2608 throw SEMANTIC_ERROR (_("symbol without referent"), e
->tok
);
2610 // We could handle initialized globals by marking them as "written".
2611 // However, this current visitor may be called for a function or
2612 // probe body, from the point of view of which this global is
2613 // already initialized, so not written.
2615 if (e->referent->init)
2616 written.insert (e->referent);
2619 if (current_lvalue
== e
|| current_lrvalue
== e
)
2621 written
.insert (e
->referent
);
2623 if (current_lvalue
!= e
|| current_lrvalue
== e
)
2625 read
.insert (e
->referent
);
2628 if (current_lrvalue
== e
)
2630 if (current_lvalue_read
)
2631 used
.insert (e
->referent
);
2633 else if (current_lvalue
!= e
)
2634 used
.insert (e
->referent
);
2637 // NB: stat_op need not be overridden, since it will get to
2638 // visit_symbol and only as a possible rvalue.
2642 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
2644 // NB: don't call parent implementation, we do the work here.
2646 // First let's visit the indexes separately
2647 bool old_lvalue_read
= current_lvalue_read
;
2648 current_lvalue_read
= true;
2649 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2651 e
->indexes
[i
]->visit (this);
2652 current_lvalue_read
= old_lvalue_read
;
2654 // Hooking this callback is necessary because of the hacky
2655 // statistics representation. For the expression "i[4] = 5", the
2656 // incoming lvalue will point to this arrayindex. However, the
2657 // symbol corresponding to the "i[4]" is multiply inherited with
2658 // arrayindex. If the symbol base part of this object is not at
2659 // offset 0, then static_cast<symbol*>(e) may result in a different
2660 // address, and not match lvalue by number when we recurse that way.
2661 // So we explicitly override the incoming lvalue/lrvalue values to
2662 // point at the embedded objects' actual base addresses.
2664 expression
* last_lrvalue
= current_lrvalue
;
2665 expression
* last_lvalue
= current_lvalue
;
2667 symbol
*array
= NULL
;
2668 hist_op
*hist
= NULL
;
2669 classify_indexable(e
->base
, array
, hist
);
2670 expression
*value
= array
?: hist
->stat
;
2672 if (current_lrvalue
== e
) current_lrvalue
= value
;
2673 if (current_lvalue
== e
) current_lvalue
= value
;
2675 e
->base
->visit(this);
2677 current_lrvalue
= last_lrvalue
;
2678 current_lvalue
= last_lvalue
;
2683 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
2685 expression
* last_lrvalue
= current_lrvalue
;
2686 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2687 functioncall_traversing_visitor::visit_pre_crement (e
);
2688 current_lrvalue
= last_lrvalue
;
2692 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
2694 expression
* last_lrvalue
= current_lrvalue
;
2695 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2696 functioncall_traversing_visitor::visit_post_crement (e
);
2697 current_lrvalue
= last_lrvalue
;
2701 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
2703 assert(!current_lvalue_read
);
2705 // NB: we duplicate so don't bother call
2706 // functioncall_traversing_visitor::visit_foreach_loop (s);
2708 s
->base
->visit(this);
2710 // If the collection is sorted, imply a "write" access to the
2711 // array in addition to the "read" one already noted above.
2712 if (s
->sort_direction
)
2714 symbol
*array
= NULL
;
2715 hist_op
*hist
= NULL
;
2716 classify_indexable (s
->base
, array
, hist
);
2717 if (array
) this->written
.insert (array
->referent
);
2718 // XXX: Can hist_op iterations be sorted?
2721 // NB: don't forget to visit the index expressions, which are lvalues.
2722 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
2724 expression
* last_lvalue
= current_lvalue
;
2725 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
2726 s
->indexes
[i
]->visit (this);
2727 current_lvalue
= last_lvalue
;
2730 // visit the additional specified array slice
2731 current_lvalue_read
= true;
2732 for (unsigned i
=0; i
<s
->array_slice
.size(); i
++)
2734 if (s
->array_slice
[i
])
2735 s
->array_slice
[i
]->visit (this);
2737 current_lvalue_read
= false;
2739 // The value is an lvalue too
2742 expression
* last_lvalue
= current_lvalue
;
2743 current_lvalue
= s
->value
; // leave a mark for ::visit_symbol
2744 s
->value
->visit (this);
2745 current_lvalue
= last_lvalue
;
2750 current_lvalue_read
= true;
2751 s
->limit
->visit (this);
2752 current_lvalue_read
= false;
2755 s
->block
->visit (this);
2760 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2762 // Ideally, this would be treated like an assignment: a plain write
2763 // to the underlying value ("lvalue"). XXX: However, the
2764 // optimization pass is not smart enough to remove an unneeded
2765 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2766 // should protect the underlying value from optimizional mischief.
2767 assert(!current_lvalue_read
);
2768 expression
* last_lrvalue
= current_lrvalue
;
2769 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2770 current_lvalue_read
= true;
2771 functioncall_traversing_visitor::visit_delete_statement (s
);
2772 current_lrvalue
= last_lrvalue
;
2773 current_lvalue_read
= false;
2777 varuse_collecting_visitor::side_effect_free ()
2779 return (written
.empty() && !embedded_seen
);
2784 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2786 // A looser notion of side-effect-freeness with respect to a given
2787 // list of variables.
2789 // That's useful because the written list may consist of local
2790 // variables of called functions. But visible side-effects only
2791 // occur if the client's locals, or any globals are written-to.
2793 set
<vardecl
*> intersection
;
2794 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2795 set_intersection (written
.begin(), written
.end(),
2796 vars
.begin(), vars
.end(),
2799 return (intersection
.empty() && !embedded_seen
);
2805 // ------------------------------------------------------------------------
2808 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2809 throwing_visitor::throwing_visitor (): msg (_("invalid element")) {}
2813 throwing_visitor::throwone (const token
* t
)
2815 throw SEMANTIC_ERROR (msg
, t
);
2819 throwing_visitor::visit_block (block
* s
)
2825 throwing_visitor::visit_try_block (try_block
* s
)
2832 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2838 throwing_visitor::visit_null_statement (null_statement
* s
)
2844 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2850 throwing_visitor::visit_if_statement (if_statement
* s
)
2856 throwing_visitor::visit_for_loop (for_loop
* s
)
2862 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2868 throwing_visitor::visit_return_statement (return_statement
* s
)
2874 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2880 throwing_visitor::visit_next_statement (next_statement
* s
)
2886 throwing_visitor::visit_break_statement (break_statement
* s
)
2892 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2898 throwing_visitor::visit_literal_string (literal_string
* e
)
2904 throwing_visitor::visit_literal_number (literal_number
* e
)
2910 throwing_visitor::visit_embedded_expr (embedded_expr
* e
)
2916 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2922 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2928 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2934 throwing_visitor::visit_post_crement (post_crement
* e
)
2941 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2947 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2953 throwing_visitor::visit_array_in (array_in
* e
)
2959 throwing_visitor::visit_regex_query (regex_query
* e
)
2965 throwing_visitor::visit_comparison (comparison
* e
)
2971 throwing_visitor::visit_concatenation (concatenation
* e
)
2977 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2983 throwing_visitor::visit_assignment (assignment
* e
)
2989 throwing_visitor::visit_symbol (symbol
* e
)
2995 throwing_visitor::visit_target_symbol (target_symbol
* e
)
3001 throwing_visitor::visit_atvar_op (atvar_op
* e
)
3007 throwing_visitor::visit_cast_op (cast_op
* e
)
3013 throwing_visitor::visit_autocast_op (autocast_op
* e
)
3019 throwing_visitor::visit_defined_op (defined_op
* e
)
3025 throwing_visitor::visit_entry_op (entry_op
* e
)
3032 throwing_visitor::visit_perf_op (perf_op
* e
)
3039 throwing_visitor::visit_arrayindex (arrayindex
* e
)
3045 throwing_visitor::visit_functioncall (functioncall
* e
)
3051 throwing_visitor::visit_print_format (print_format
* e
)
3057 throwing_visitor::visit_stat_op (stat_op
* e
)
3063 throwing_visitor::visit_hist_op (hist_op
* e
)
3069 // ------------------------------------------------------------------------
3073 update_visitor::visit_block (block
* s
)
3075 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
3076 replace (s
->statements
[i
]);
3081 update_visitor::visit_try_block (try_block
* s
)
3083 replace (s
->try_block
);
3084 replace (s
->catch_error_var
);
3085 replace (s
->catch_block
);
3090 update_visitor::visit_embeddedcode (embeddedcode
* s
)
3096 update_visitor::visit_null_statement (null_statement
* s
)
3102 update_visitor::visit_expr_statement (expr_statement
* s
)
3109 update_visitor::visit_if_statement (if_statement
* s
)
3111 replace (s
->condition
);
3112 replace (s
->thenblock
);
3113 replace (s
->elseblock
);
3118 update_visitor::visit_for_loop (for_loop
* s
)
3128 update_visitor::visit_foreach_loop (foreach_loop
* s
)
3130 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
3131 replace (s
->indexes
[i
]);
3140 update_visitor::visit_return_statement (return_statement
* s
)
3147 update_visitor::visit_delete_statement (delete_statement
* s
)
3154 update_visitor::visit_next_statement (next_statement
* s
)
3160 update_visitor::visit_break_statement (break_statement
* s
)
3166 update_visitor::visit_continue_statement (continue_statement
* s
)
3172 update_visitor::visit_literal_string (literal_string
* e
)
3178 update_visitor::visit_literal_number (literal_number
* e
)
3184 update_visitor::visit_embedded_expr (embedded_expr
* e
)
3190 update_visitor::visit_binary_expression (binary_expression
* e
)
3198 update_visitor::visit_unary_expression (unary_expression
* e
)
3200 replace (e
->operand
);
3205 update_visitor::visit_pre_crement (pre_crement
* e
)
3207 replace (e
->operand
);
3212 update_visitor::visit_post_crement (post_crement
* e
)
3214 replace (e
->operand
);
3220 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3228 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3236 update_visitor::visit_array_in (array_in
* e
)
3238 replace (e
->operand
);
3243 update_visitor::visit_regex_query (regex_query
* e
)
3246 replace (e
->right
); // XXX: do we *need* to replace literal in RHS?
3251 update_visitor::visit_comparison (comparison
* e
)
3259 update_visitor::visit_concatenation (concatenation
* e
)
3267 update_visitor::visit_ternary_expression (ternary_expression
* e
)
3270 replace (e
->truevalue
);
3271 replace (e
->falsevalue
);
3276 update_visitor::visit_assignment (assignment
* e
)
3284 update_visitor::visit_symbol (symbol
* e
)
3290 update_visitor::visit_target_symbol (target_symbol
* e
)
3292 e
->visit_components (this);
3297 update_visitor::visit_cast_op (cast_op
* e
)
3299 replace (e
->operand
);
3300 e
->visit_components (this);
3305 update_visitor::visit_autocast_op (autocast_op
* e
)
3307 replace (e
->operand
);
3308 e
->visit_components (this);
3313 update_visitor::visit_atvar_op (atvar_op
* e
)
3315 e
->visit_components (this);
3320 update_visitor::visit_defined_op (defined_op
* e
)
3322 replace (e
->operand
);
3327 update_visitor::visit_entry_op (entry_op
* e
)
3329 replace (e
->operand
);
3334 update_visitor::visit_perf_op (perf_op
* e
)
3336 replace (e
->operand
);
3341 update_visitor::visit_arrayindex (arrayindex
* e
)
3344 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
3345 replace (e
->indexes
[i
]);
3350 update_visitor::visit_functioncall (functioncall
* e
)
3352 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3353 replace (e
->args
[i
]);
3358 update_visitor::visit_print_format (print_format
* e
)
3360 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3361 replace (e
->args
[i
]);
3367 update_visitor::visit_stat_op (stat_op
* e
)
3374 update_visitor::visit_hist_op (hist_op
* e
)
3381 // ------------------------------------------------------------------------
3385 deep_copy_visitor::visit_block (block
* s
)
3387 update_visitor::visit_block(new block(*s
));
3391 deep_copy_visitor::visit_try_block (try_block
* s
)
3393 update_visitor::visit_try_block(new try_block(*s
));
3397 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
3399 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
3403 deep_copy_visitor::visit_null_statement (null_statement
* s
)
3405 update_visitor::visit_null_statement(new null_statement(*s
));
3409 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
3411 update_visitor::visit_expr_statement(new expr_statement(*s
));
3415 deep_copy_visitor::visit_if_statement (if_statement
* s
)
3417 update_visitor::visit_if_statement(new if_statement(*s
));
3421 deep_copy_visitor::visit_for_loop (for_loop
* s
)
3423 update_visitor::visit_for_loop(new for_loop(*s
));
3427 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
3429 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
3433 deep_copy_visitor::visit_return_statement (return_statement
* s
)
3435 update_visitor::visit_return_statement(new return_statement(*s
));
3439 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
3441 update_visitor::visit_delete_statement(new delete_statement(*s
));
3445 deep_copy_visitor::visit_next_statement (next_statement
* s
)
3447 update_visitor::visit_next_statement(new next_statement(*s
));
3451 deep_copy_visitor::visit_break_statement (break_statement
* s
)
3453 update_visitor::visit_break_statement(new break_statement(*s
));
3457 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
3459 update_visitor::visit_continue_statement(new continue_statement(*s
));
3463 deep_copy_visitor::visit_literal_string (literal_string
* e
)
3465 update_visitor::visit_literal_string(new literal_string(*e
));
3469 deep_copy_visitor::visit_literal_number (literal_number
* e
)
3471 update_visitor::visit_literal_number(new literal_number(*e
));
3475 deep_copy_visitor::visit_embedded_expr (embedded_expr
* e
)
3477 update_visitor::visit_embedded_expr(new embedded_expr(*e
));
3481 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
3483 update_visitor::visit_binary_expression(new binary_expression(*e
));
3487 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
3489 update_visitor::visit_unary_expression(new unary_expression(*e
));
3493 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
3495 update_visitor::visit_pre_crement(new pre_crement(*e
));
3499 deep_copy_visitor::visit_post_crement (post_crement
* e
)
3501 update_visitor::visit_post_crement(new post_crement(*e
));
3506 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3508 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
3512 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3514 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
3518 deep_copy_visitor::visit_array_in (array_in
* e
)
3520 update_visitor::visit_array_in(new array_in(*e
));
3524 deep_copy_visitor::visit_regex_query (regex_query
* e
)
3526 update_visitor::visit_regex_query(new regex_query(*e
));
3530 deep_copy_visitor::visit_comparison (comparison
* e
)
3532 update_visitor::visit_comparison(new comparison(*e
));
3536 deep_copy_visitor::visit_concatenation (concatenation
* e
)
3538 update_visitor::visit_concatenation(new concatenation(*e
));
3542 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
3544 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
3548 deep_copy_visitor::visit_assignment (assignment
* e
)
3550 update_visitor::visit_assignment(new assignment(*e
));
3554 deep_copy_visitor::visit_symbol (symbol
* e
)
3556 symbol
* n
= new symbol(*e
);
3557 n
->referent
= NULL
; // don't copy!
3558 update_visitor::visit_symbol(n
);
3562 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
3564 target_symbol
* n
= new target_symbol(*e
);
3565 update_visitor::visit_target_symbol(n
);
3569 deep_copy_visitor::visit_cast_op (cast_op
* e
)
3571 update_visitor::visit_cast_op(new cast_op(*e
));
3575 deep_copy_visitor::visit_autocast_op (autocast_op
* e
)
3577 update_visitor::visit_autocast_op(new autocast_op(*e
));
3581 deep_copy_visitor::visit_atvar_op (atvar_op
* e
)
3583 update_visitor::visit_atvar_op(new atvar_op(*e
));
3587 deep_copy_visitor::visit_defined_op (defined_op
* e
)
3589 update_visitor::visit_defined_op(new defined_op(*e
));
3593 deep_copy_visitor::visit_entry_op (entry_op
* e
)
3595 update_visitor::visit_entry_op(new entry_op(*e
));
3599 deep_copy_visitor::visit_perf_op (perf_op
* e
)
3601 update_visitor::visit_perf_op(new perf_op(*e
));
3605 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
3607 update_visitor::visit_arrayindex(new arrayindex(*e
));
3611 deep_copy_visitor::visit_functioncall (functioncall
* e
)
3613 functioncall
* n
= new functioncall(*e
);
3614 n
->referent
= NULL
; // don't copy!
3615 update_visitor::visit_functioncall(n
);
3619 deep_copy_visitor::visit_print_format (print_format
* e
)
3621 update_visitor::visit_print_format(new print_format(*e
));
3625 deep_copy_visitor::visit_stat_op (stat_op
* e
)
3627 update_visitor::visit_stat_op(new stat_op(*e
));
3631 deep_copy_visitor::visit_hist_op (hist_op
* e
)
3633 update_visitor::visit_hist_op(new hist_op(*e
));
3636 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */