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 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 well_formed (pp
.well_formed
), condition (pp
.condition
)
110 probe_point::probe_point ():
111 optional (false), sufficient (false), well_formed (false), condition (0)
116 probe_point::from_globby_comp(const std::string
& comp
)
118 vector
<component
*>::const_iterator it
;
119 for (it
= components
.begin(); it
!= components
.end(); it
++)
120 if ((*it
)->functor
== comp
)
121 return (*it
)->from_glob
;
125 unsigned probe::last_probeidx
= 0;
128 body (0), base (0), tok (0), systemtap_v_conditional (0), privileged (false)
130 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
134 // Copy constructor, but with overriding probe-point. To be used when
135 // mapping script-level probe points to another one, early during pass
136 // 2. There should be no symbol resolution done yet.
137 probe::probe(probe
* p
, probe_point
* l
)
140 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
142 this->locations
.push_back(l
);
143 this->body
= deep_copy_visitor::deep_copy(p
->body
);
144 this->privileged
= p
->privileged
;
145 this->systemtap_v_conditional
= p
->systemtap_v_conditional
;
146 assert (p
->locals
.size() == 0);
147 assert (p
->unused_locals
.size() == 0);
151 probe_point::component::component ():
152 arg (0), from_glob(false), tok(0)
157 probe_point::component::component (std::string
const & f
,
158 literal
* a
, bool from_glob
):
159 functor(f
), arg(a
), from_glob(from_glob
), tok(0)
165 arity_tok(0), arity (-1), maxsize(0), init(NULL
), synthetic(false), wrap(false),
172 vardecl::set_arity (int a
, const token
* t
)
177 if (a
== 0 && maxsize
> 0)
178 throw SEMANTIC_ERROR (_("inconsistent arity"), tok
);
180 if (arity
!= a
&& arity
>= 0)
182 semantic_error
err (ERR_SRC
, _F("inconsistent arity (%s vs %d)",
183 lex_cast(arity
).c_str(), a
), t
?:tok
);
186 semantic_error
chain(ERR_SRC
, _F("arity %s first inferred here",
187 lex_cast(arity
).c_str()), arity_tok
);
188 err
.set_chain(chain
);
197 index_types
.resize (arity
);
198 for (int i
=0; i
<arity
; i
++)
199 index_types
[i
] = pe_unknown
;
204 vardecl::compatible_arity (int a
)
206 if (a
== 0 && maxsize
> 0)
208 if (arity
== -1 || a
== -1)
214 functiondecl::functiondecl ():
215 body (0), synthetic (false), mangle_oldstyle (false)
220 functiondecl::join (systemtap_session
& s
)
223 throw SEMANTIC_ERROR (_("internal error, joining a non-synthetic function"), tok
);
224 if (!s
.functions
.insert (make_pair (name
, this)).second
)
225 throw SEMANTIC_ERROR (_F("synthetic function '%s' conflicts with an existing function",
227 tok
->location
.file
->functions
.push_back (this);
231 literal_number::literal_number (int64_t v
, bool hex
)
239 literal_string::literal_string (const string
& v
)
247 operator << (ostream
& o
, const exp_type
& e
)
251 case pe_unknown
: o
<< "unknown"; break;
252 case pe_long
: o
<< "long"; break;
253 case pe_string
: o
<< "string"; break;
254 case pe_stats
: o
<< "stats"; break;
255 default: o
<< "???"; break;
262 target_symbol::assert_no_components(const std::string
& tapset
, bool pretty_ok
)
264 if (components
.empty())
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 name
.substr(1);
335 string
atvar_op::sym_name ()
340 return 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
<< name
<< ":" << 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 if (n
== NULL
) n
= t
->content
.to_string().c_str();
668 format
= delim
= newline
= _char
= false;
670 if (strcmp(n
, "print_char") == 0)
680 if (0 != strncmp(n
, "print", 5))
697 if (*n
== 'l' && *(n
+1) == 'n')
708 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
, o
);
715 print_format::components_to_string(vector
<format_component
> const & components
)
719 for (vector
<format_component
>::const_iterator i
= components
.begin();
720 i
!= components
.end(); ++i
)
723 assert (i
->type
!= conv_unspecified
);
725 if (i
->type
== conv_literal
)
727 assert(!i
->literal_string
.empty());
728 for (string::const_iterator j
= i
->literal_string
.begin();
729 j
!= i
->literal_string
.end(); ++j
)
731 // See also: c_unparser::visit_literal_string and lex_cast_qstring
743 if (i
->test_flag (fmt_flag_zeropad
))
746 if (i
->test_flag (fmt_flag_plus
))
749 if (i
->test_flag (fmt_flag_space
))
752 if (i
->test_flag (fmt_flag_left
))
755 if (i
->test_flag (fmt_flag_special
))
758 if (i
->widthtype
== width_dynamic
)
760 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
763 if (i
->prectype
== prec_dynamic
)
765 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
766 oss
<< '.' << i
->precision
;
781 if (i
->test_flag (fmt_flag_large
))
786 else if (i
->base
== 8)
788 else if (i
->test_flag (fmt_flag_sign
))
806 case conv_memory_hex
:
818 vector
<print_format::format_component
>
819 print_format::string_to_components(string
const & str
)
821 format_component curr
;
822 vector
<format_component
> res
;
826 string::const_iterator i
= str
.begin();
828 while (i
!= str
.end())
832 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
833 curr
.type
= conv_literal
;
834 curr
.literal_string
+= *i
;
838 else if (i
+1 == str
.end() || *(i
+1) == '%')
841 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
842 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
843 curr
.type
= conv_literal
;
844 curr
.literal_string
+= '%';
851 if (curr
.type
!= conv_unspecified
)
853 // Flush any component we were previously accumulating
854 assert (curr
.type
== conv_literal
);
864 // Now we are definitely parsing a conversion.
865 // Begin by parsing flags (which are optional).
870 curr
.set_flag (fmt_flag_zeropad
);
875 curr
.set_flag (fmt_flag_plus
);
880 curr
.set_flag (fmt_flag_left
);
885 curr
.set_flag (fmt_flag_space
);
890 curr
.set_flag (fmt_flag_special
);
901 // Parse optional width
904 curr
.widthtype
= width_dynamic
;
907 else if (isdigit(*i
))
909 curr
.widthtype
= width_static
;
914 curr
.width
+= (*i
- '0');
917 while (i
!= str
.end() && isdigit(*i
));
923 // Parse optional precision
931 curr
.prectype
= prec_dynamic
;
934 else if (isdigit(*i
))
936 curr
.prectype
= prec_static
;
940 curr
.precision
*= 10;
941 curr
.precision
+= (*i
- '0');
944 while (i
!= str
.end() && isdigit(*i
));
951 // Parse the type modifier
962 // Parse the actual conversion specifier (bcsmdioupxXn)
965 // Valid conversion types
967 curr
.type
= conv_binary
;
971 curr
.type
= conv_char
;
975 curr
.type
= conv_string
;
979 curr
.type
= conv_memory
;
983 curr
.type
= conv_memory_hex
;
988 curr
.set_flag (fmt_flag_sign
);
990 curr
.type
= conv_number
;
995 curr
.type
= conv_number
;
1000 curr
.set_flag (fmt_flag_large
);
1002 curr
.type
= conv_number
;
1007 // Since stap 1.3, %p == %#x.
1008 curr
.set_flag (fmt_flag_special
);
1009 curr
.type
= conv_pointer
;
1011 // Oddness for stap < 1.3 is handled in translation
1018 if (curr
.type
== conv_unspecified
)
1019 throw PARSE_ERROR(_("invalid or missing conversion specifier"));
1022 res
.push_back(curr
);
1026 // If there's a remaining partly-composed conversion, fail.
1027 if (!curr
.is_empty())
1029 if (curr
.type
== conv_literal
)
1030 res
.push_back(curr
);
1032 throw PARSE_ERROR(_("trailing incomplete print format conversion"));
1039 void print_format::print (ostream
& o
) const
1041 o
<< print_format_type
<< "(";
1042 if (print_with_format
)
1043 o
<< lex_cast_qstring (raw_components
);
1044 if (print_with_delim
)
1045 o
<< lex_cast_qstring (delimiter
.literal_string
);
1048 for (vector
<expression
*>::const_iterator i
= args
.begin();
1049 i
!= args
.end(); ++i
)
1051 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
1058 void stat_op::print (ostream
& o
) const
1084 assert (0); // should not happen, as sc_none is only used in foreach sorts
1092 hist_op::print (ostream
& o
) const
1098 assert(params
.size() == 3);
1099 o
<< "hist_linear(";
1101 for (size_t i
= 0; i
< params
.size(); ++i
)
1103 o
<< ", " << params
[i
];
1109 assert(params
.size() == 0);
1117 ostream
& operator << (ostream
& o
, const statement
& k
)
1124 void embeddedcode::print (ostream
&o
) const
1131 void block::print (ostream
& o
) const
1134 for (unsigned i
=0; i
<statements
.size(); i
++)
1135 o
<< *statements
[i
] << endl
;
1139 block::block (statement
* car
, statement
* cdr
)
1141 statements
.push_back(car
);
1142 statements
.push_back(cdr
);
1143 this->tok
= car
->tok
;
1148 void try_block::print (ostream
& o
) const
1150 o
<< "try {" << endl
;
1151 if (try_block
) o
<< *try_block
<< endl
;
1153 if (catch_error_var
) o
<< "(" << *catch_error_var
<< ") ";
1155 if (catch_block
) o
<< *catch_block
<< endl
;
1160 void for_loop::print (ostream
& o
) const
1163 if (init
) init
->print (o
);
1167 if (incr
) incr
->print (o
);
1173 void foreach_loop::print (ostream
& o
) const
1182 for (unsigned i
=0; i
<indexes
.size(); i
++)
1184 if (i
> 0) o
<< ", ";
1185 indexes
[i
]->print (o
);
1186 if (sort_direction
!= 0 && sort_column
== i
+1)
1187 o
<< (sort_direction
> 0 ? "+" : "-");
1191 if (!array_slice
.empty())
1194 for (unsigned i
=0; i
<array_slice
.size(); i
++)
1196 if (i
> 0) o
<< ", ";
1198 array_slice
[i
]->print (o
);
1204 if (sort_direction
!= 0 && sort_column
== 0)
1208 case sc_count
: o
<< " @count"; break;
1209 case sc_average
: o
<< " @avg"; break;
1210 case sc_min
: o
<< " @min"; break;
1211 case sc_max
: o
<< " @max"; break;
1212 case sc_sum
: o
<< " @sum"; break;
1218 o
<< (sort_direction
> 0 ? "+" : "-");
1230 void null_statement::print (ostream
& o
) const
1236 void expr_statement::print (ostream
& o
) const
1242 void return_statement::print (ostream
& o
) const
1244 o
<< "return " << *value
;
1248 void delete_statement::print (ostream
& o
) const
1250 o
<< "delete " << *value
;
1253 void next_statement::print (ostream
& o
) const
1258 void break_statement::print (ostream
& o
) const
1263 void continue_statement::print (ostream
& o
) const
1268 void if_statement::print (ostream
& o
) const
1270 o
<< "if (" << *condition
<< ") "
1271 << *thenblock
<< endl
;
1273 o
<< "else " << *elseblock
<< endl
;
1277 void stapfile::print (ostream
& o
) const
1279 o
<< "# file " << name
<< endl
;
1281 for (unsigned i
=0; i
<embeds
.size(); i
++)
1282 embeds
[i
]->print (o
);
1284 for (unsigned i
=0; i
<globals
.size(); i
++)
1287 globals
[i
]->print (o
);
1291 for (unsigned i
=0; i
<aliases
.size(); i
++)
1293 aliases
[i
]->print (o
);
1297 for (unsigned i
=0; i
<probes
.size(); i
++)
1299 probes
[i
]->print (o
);
1303 for (unsigned j
= 0; j
< functions
.size(); j
++)
1305 functions
[j
]->print (o
);
1311 void probe::print (ostream
& o
) const
1319 void probe::printsig (ostream
& o
) const
1321 const probe_alias
*alias
= get_alias ();
1324 alias
->printsig (o
);
1328 for (unsigned i
=0; i
<locations
.size(); i
++)
1330 if (i
> 0) o
<< ",";
1331 locations
[i
]->print (o
);
1337 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
) const
1339 probes_list
.push_back(const_cast<probe
*>(this));
1341 base
->collect_derivation_chain(probes_list
);
1346 probe::collect_derivation_pp_chain (std::vector
<probe_point
*> &pp_list
) const
1348 pp_list
.push_back(const_cast<probe_point
*>(this->locations
[0]));
1350 base
->collect_derivation_pp_chain(pp_list
);
1355 void probe_point::print (ostream
& o
, bool print_extras
) const
1357 for (unsigned i
=0; i
<components
.size(); i
++)
1360 probe_point::component
* c
= components
[i
];
1363 // We might like to excise this weird 0 pointer, just in
1364 // case some recursion during the error-handling path, but
1365 // can't, because what we have here is a const *this.
1366 static int been_here
= 0;
1367 if (been_here
== 0) {
1369 throw SEMANTIC_ERROR (_("internal error: missing probe point component"));
1371 continue; // ... sad panda decides to skip the bad boy
1375 o
<< "(" << *c
->arg
<< ")";
1381 else if (optional
) // sufficient implies optional
1384 o
<< " if (" << *condition
<< ")";
1387 string
probe_point::str (bool print_extras
) const
1390 print(o
, print_extras
);
1395 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1396 probe (), alias_names (aliases
), epilogue_style(false)
1400 void probe_alias::printsig (ostream
& o
) const
1402 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1404 o
<< (i
>0 ? " = " : "");
1405 alias_names
[i
]->print (o
);
1407 o
<< (epilogue_style
? " += " : " = ");
1408 for (unsigned i
=0; i
<locations
.size(); i
++)
1410 if (i
> 0) o
<< ", ";
1411 locations
[i
]->print (o
);
1416 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1423 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1431 // ------------------------------------------------------------------------
1436 block::visit (visitor
* u
)
1438 u
->visit_block (this);
1443 try_block::visit (visitor
* u
)
1445 u
->visit_try_block (this);
1450 embeddedcode::visit (visitor
* u
)
1452 u
->visit_embeddedcode (this);
1457 for_loop::visit (visitor
* u
)
1459 u
->visit_for_loop (this);
1463 foreach_loop::visit (visitor
* u
)
1465 u
->visit_foreach_loop (this);
1469 null_statement::visit (visitor
* u
)
1471 u
->visit_null_statement (this);
1475 expr_statement::visit (visitor
* u
)
1477 u
->visit_expr_statement (this);
1481 return_statement::visit (visitor
* u
)
1483 u
->visit_return_statement (this);
1487 delete_statement::visit (visitor
* u
)
1489 u
->push_active_lvalue (this->value
);
1490 u
->visit_delete_statement (this);
1491 u
->pop_active_lvalue ();
1495 if_statement::visit (visitor
* u
)
1497 u
->visit_if_statement (this);
1501 next_statement::visit (visitor
* u
)
1503 u
->visit_next_statement (this);
1507 break_statement::visit (visitor
* u
)
1509 u
->visit_break_statement (this);
1513 continue_statement::visit (visitor
* u
)
1515 u
->visit_continue_statement (this);
1519 literal_string::visit(visitor
* u
)
1521 u
->visit_literal_string (this);
1525 literal_number::visit(visitor
* u
)
1527 u
->visit_literal_number (this);
1531 binary_expression::visit (visitor
* u
)
1533 u
->visit_binary_expression (this);
1537 embedded_expr::visit (visitor
* u
)
1539 u
->visit_embedded_expr (this);
1543 unary_expression::visit (visitor
* u
)
1545 u
->visit_unary_expression (this);
1549 pre_crement::visit (visitor
* u
)
1551 u
->push_active_lvalue (this->operand
);
1552 u
->visit_pre_crement (this);
1553 u
->pop_active_lvalue ();
1557 post_crement::visit (visitor
* u
)
1559 u
->push_active_lvalue (this->operand
);
1560 u
->visit_post_crement (this);
1561 u
->pop_active_lvalue ();
1565 logical_or_expr::visit (visitor
* u
)
1567 u
->visit_logical_or_expr (this);
1571 logical_and_expr::visit (visitor
* u
)
1573 u
->visit_logical_and_expr (this);
1577 array_in::visit (visitor
* u
)
1579 u
->visit_array_in (this);
1583 regex_query::visit (visitor
* u
)
1585 u
->visit_regex_query (this);
1589 comparison::visit (visitor
* u
)
1591 u
->visit_comparison (this);
1595 concatenation::visit (visitor
* u
)
1597 u
->visit_concatenation (this);
1601 ternary_expression::visit (visitor
* u
)
1603 u
->visit_ternary_expression (this);
1607 assignment::visit (visitor
* u
)
1609 u
->push_active_lvalue (this->left
);
1610 u
->visit_assignment (this);
1611 u
->pop_active_lvalue ();
1615 symbol::visit (visitor
* u
)
1617 u
->visit_symbol (this);
1621 target_symbol::visit (visitor
* u
)
1623 u
->visit_target_symbol(this);
1627 target_symbol::visit_components (visitor
* u
)
1629 for (unsigned i
= 0; i
< components
.size(); ++i
)
1630 if (components
[i
].type
== comp_expression_array_index
)
1631 components
[i
].expr_index
->visit (u
);
1635 target_symbol::visit_components (update_visitor
* u
)
1637 for (unsigned i
= 0; i
< components
.size(); ++i
)
1638 if (components
[i
].type
== comp_expression_array_index
)
1639 u
->replace (components
[i
].expr_index
);
1643 cast_op::visit (visitor
* u
)
1645 u
->visit_cast_op(this);
1650 autocast_op::visit (visitor
* u
)
1652 u
->visit_autocast_op(this);
1657 atvar_op::visit (visitor
* u
)
1659 u
->visit_atvar_op(this);
1664 defined_op::visit (visitor
* u
)
1666 u
->visit_defined_op(this);
1671 entry_op::visit (visitor
* u
)
1673 u
->visit_entry_op(this);
1678 perf_op::visit (visitor
* u
)
1680 u
->visit_perf_op(this);
1685 arrayindex::visit (visitor
* u
)
1687 u
->visit_arrayindex (this);
1691 functioncall::visit (visitor
* u
)
1693 u
->visit_functioncall (this);
1697 print_format::visit (visitor
*u
)
1699 u
->visit_print_format (this);
1703 stat_op::visit (visitor
*u
)
1705 u
->visit_stat_op (this);
1709 hist_op::visit (visitor
*u
)
1711 u
->visit_hist_op (this);
1716 expression::is_symbol(symbol
*& sym_out
)
1723 indexable::is_symbol(symbol
*& sym_out
)
1730 indexable::is_hist_op(hist_op
*& hist_out
)
1737 symbol::is_symbol(symbol
*& sym_out
)
1744 hist_op::is_hist_op(hist_op
*& hist_out
)
1751 classify_indexable(indexable
* ix
,
1752 symbol
*& array_out
,
1753 hist_op
*& hist_out
)
1758 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1759 throw SEMANTIC_ERROR(_("Expecting symbol or histogram operator"), ix
->tok
);
1760 if (!(hist_out
|| array_out
))
1761 throw SEMANTIC_ERROR(_("Failed to classify indexable"), ix
->tok
);
1765 // ------------------------------------------------------------------------
1768 visitor::is_active_lvalue(expression
*e
)
1770 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1772 if (active_lvalues
[i
] == e
)
1779 visitor::push_active_lvalue(expression
*e
)
1781 active_lvalues
.push_back(e
);
1785 visitor::pop_active_lvalue()
1787 assert(!active_lvalues
.empty());
1788 active_lvalues
.pop_back();
1793 // ------------------------------------------------------------------------
1796 traversing_visitor::visit_block (block
* s
)
1798 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1799 s
->statements
[i
]->visit (this);
1803 traversing_visitor::visit_try_block (try_block
* s
)
1806 s
->try_block
->visit (this);
1807 if (s
->catch_error_var
)
1808 s
->catch_error_var
->visit (this);
1810 s
->catch_block
->visit (this);
1814 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1819 traversing_visitor::visit_null_statement (null_statement
*)
1824 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1826 s
->value
->visit (this);
1830 traversing_visitor::visit_if_statement (if_statement
* s
)
1832 s
->condition
->visit (this);
1833 s
->thenblock
->visit (this);
1835 s
->elseblock
->visit (this);
1839 traversing_visitor::visit_for_loop (for_loop
* s
)
1841 if (s
->init
) s
->init
->visit (this);
1842 s
->cond
->visit (this);
1843 if (s
->incr
) s
->incr
->visit (this);
1844 s
->block
->visit (this);
1848 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1850 s
->base
->visit(this);
1852 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1853 s
->indexes
[i
]->visit (this);
1856 s
->value
->visit (this);
1859 s
->limit
->visit (this);
1861 s
->block
->visit (this);
1865 traversing_visitor::visit_return_statement (return_statement
* s
)
1867 s
->value
->visit (this);
1871 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1873 s
->value
->visit (this);
1877 traversing_visitor::visit_next_statement (next_statement
*)
1882 traversing_visitor::visit_break_statement (break_statement
*)
1887 traversing_visitor::visit_continue_statement (continue_statement
*)
1892 traversing_visitor::visit_literal_string (literal_string
*)
1897 traversing_visitor::visit_literal_number (literal_number
*)
1902 traversing_visitor::visit_embedded_expr (embedded_expr
*)
1907 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1909 e
->left
->visit (this);
1910 e
->right
->visit (this);
1914 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1916 e
->operand
->visit (this);
1920 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1922 e
->operand
->visit (this);
1926 traversing_visitor::visit_post_crement (post_crement
* e
)
1928 e
->operand
->visit (this);
1933 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1935 e
->left
->visit (this);
1936 e
->right
->visit (this);
1940 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1942 e
->left
->visit (this);
1943 e
->right
->visit (this);
1947 traversing_visitor::visit_array_in (array_in
* e
)
1949 e
->operand
->visit (this);
1953 traversing_visitor::visit_regex_query (regex_query
* e
)
1955 e
->left
->visit (this);
1956 e
->right
->visit (this);
1960 traversing_visitor::visit_comparison (comparison
* e
)
1962 e
->left
->visit (this);
1963 e
->right
->visit (this);
1967 traversing_visitor::visit_concatenation (concatenation
* e
)
1969 e
->left
->visit (this);
1970 e
->right
->visit (this);
1974 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1976 e
->cond
->visit (this);
1977 e
->truevalue
->visit (this);
1978 e
->falsevalue
->visit (this);
1982 traversing_visitor::visit_assignment (assignment
* e
)
1984 e
->left
->visit (this);
1985 e
->right
->visit (this);
1989 traversing_visitor::visit_symbol (symbol
*)
1994 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1996 e
->visit_components (this);
2000 traversing_visitor::visit_cast_op (cast_op
* e
)
2002 e
->operand
->visit (this);
2003 e
->visit_components (this);
2007 traversing_visitor::visit_autocast_op (autocast_op
* e
)
2009 e
->operand
->visit (this);
2010 e
->visit_components (this);
2014 traversing_visitor::visit_atvar_op (atvar_op
* e
)
2016 e
->visit_components (this);
2020 traversing_visitor::visit_defined_op (defined_op
* e
)
2022 e
->operand
->visit (this);
2026 traversing_visitor::visit_entry_op (entry_op
* e
)
2028 e
->operand
->visit (this);
2033 traversing_visitor::visit_perf_op (perf_op
* e
)
2035 e
->operand
->visit (this);
2040 traversing_visitor::visit_arrayindex (arrayindex
* e
)
2042 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2044 e
->indexes
[i
]->visit (this);
2046 e
->base
->visit(this);
2050 traversing_visitor::visit_functioncall (functioncall
* e
)
2052 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2053 e
->args
[i
]->visit (this);
2057 traversing_visitor::visit_print_format (print_format
* e
)
2059 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2060 e
->args
[i
]->visit (this);
2062 e
->hist
->visit(this);
2066 traversing_visitor::visit_stat_op (stat_op
* e
)
2068 e
->stat
->visit (this);
2072 traversing_visitor::visit_hist_op (hist_op
* e
)
2074 e
->stat
->visit (this);
2079 expression_visitor::visit_literal_string (literal_string
* e
)
2081 traversing_visitor::visit_literal_string (e
);
2082 visit_expression (e
);
2086 expression_visitor::visit_literal_number (literal_number
* e
)
2088 traversing_visitor::visit_literal_number (e
);
2089 visit_expression (e
);
2093 expression_visitor::visit_embedded_expr (embedded_expr
* e
)
2095 traversing_visitor::visit_embedded_expr (e
);
2096 visit_expression (e
);
2100 expression_visitor::visit_binary_expression (binary_expression
* e
)
2102 traversing_visitor::visit_binary_expression (e
);
2103 visit_expression (e
);
2107 expression_visitor::visit_unary_expression (unary_expression
* e
)
2109 traversing_visitor::visit_unary_expression (e
);
2110 visit_expression (e
);
2114 expression_visitor::visit_pre_crement (pre_crement
* e
)
2116 traversing_visitor::visit_pre_crement (e
);
2117 visit_expression (e
);
2121 expression_visitor::visit_post_crement (post_crement
* e
)
2123 traversing_visitor::visit_post_crement (e
);
2124 visit_expression (e
);
2128 expression_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2130 traversing_visitor::visit_logical_or_expr (e
);
2131 visit_expression (e
);
2135 expression_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2137 traversing_visitor::visit_logical_and_expr (e
);
2138 visit_expression (e
);
2142 expression_visitor::visit_array_in (array_in
* e
)
2144 traversing_visitor::visit_array_in (e
);
2145 visit_expression (e
);
2149 expression_visitor::visit_regex_query (regex_query
* e
)
2151 traversing_visitor::visit_regex_query (e
);
2152 visit_expression (e
);
2156 expression_visitor::visit_comparison (comparison
* e
)
2158 traversing_visitor::visit_comparison (e
);
2159 visit_expression (e
);
2163 expression_visitor::visit_concatenation (concatenation
* e
)
2165 traversing_visitor::visit_concatenation (e
);
2166 visit_expression (e
);
2170 expression_visitor::visit_ternary_expression (ternary_expression
* e
)
2172 traversing_visitor::visit_ternary_expression (e
);
2173 visit_expression (e
);
2177 expression_visitor::visit_assignment (assignment
* e
)
2179 traversing_visitor::visit_assignment (e
);
2180 visit_expression (e
);
2184 expression_visitor::visit_symbol (symbol
* e
)
2186 traversing_visitor::visit_symbol (e
);
2187 visit_expression (e
);
2191 expression_visitor::visit_target_symbol (target_symbol
* e
)
2193 traversing_visitor::visit_target_symbol (e
);
2194 visit_expression (e
);
2198 expression_visitor::visit_arrayindex (arrayindex
* e
)
2200 traversing_visitor::visit_arrayindex (e
);
2201 visit_expression (e
);
2205 expression_visitor::visit_functioncall (functioncall
* e
)
2207 traversing_visitor::visit_functioncall (e
);
2208 visit_expression (e
);
2212 expression_visitor::visit_print_format (print_format
* e
)
2214 traversing_visitor::visit_print_format (e
);
2215 visit_expression (e
);
2219 expression_visitor::visit_stat_op (stat_op
* e
)
2221 traversing_visitor::visit_stat_op (e
);
2222 visit_expression (e
);
2226 expression_visitor::visit_hist_op (hist_op
* e
)
2228 traversing_visitor::visit_hist_op (e
);
2229 visit_expression (e
);
2233 expression_visitor::visit_cast_op (cast_op
* e
)
2235 traversing_visitor::visit_cast_op (e
);
2236 visit_expression (e
);
2240 expression_visitor::visit_autocast_op (autocast_op
* e
)
2242 traversing_visitor::visit_autocast_op (e
);
2243 visit_expression (e
);
2247 expression_visitor::visit_atvar_op (atvar_op
* e
)
2249 traversing_visitor::visit_atvar_op (e
);
2250 visit_expression (e
);
2254 expression_visitor::visit_defined_op (defined_op
* e
)
2256 traversing_visitor::visit_defined_op (e
);
2257 visit_expression (e
);
2261 expression_visitor::visit_entry_op (entry_op
* e
)
2263 traversing_visitor::visit_entry_op (e
);
2264 visit_expression (e
);
2268 expression_visitor::visit_perf_op (perf_op
* e
)
2270 traversing_visitor::visit_perf_op (e
);
2271 visit_expression (e
);
2276 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
2278 traversing_visitor::visit_functioncall (e
);
2279 this->enter_functioncall(e
);
2283 functioncall_traversing_visitor::enter_functioncall (functioncall
* e
)
2285 // prevent infinite recursion
2286 if (nested
.find (e
->referent
) == nested
.end ())
2288 if (seen
.find(e
->referent
) == seen
.end())
2289 seen
.insert (e
->referent
);
2290 nested
.insert (e
->referent
);
2292 functiondecl
* last_current_function
= current_function
;
2293 current_function
= e
->referent
;
2294 e
->referent
->body
->visit (this);
2295 current_function
= last_current_function
;
2296 nested
.erase (e
->referent
);
2298 else { this->note_recursive_functioncall(e
); }
2302 functioncall_traversing_visitor::note_recursive_functioncall (functioncall
* e
)
2307 varuse_collecting_visitor::visit_if_statement (if_statement
*s
)
2309 assert(!current_lvalue_read
);
2310 current_lvalue_read
= true;
2311 s
->condition
->visit (this);
2312 current_lvalue_read
= false;
2314 s
->thenblock
->visit (this);
2316 s
->elseblock
->visit (this);
2321 varuse_collecting_visitor::visit_for_loop (for_loop
*s
)
2323 if (s
->init
) s
->init
->visit (this);
2325 assert(!current_lvalue_read
);
2326 current_lvalue_read
= true;
2327 s
->cond
->visit (this);
2328 current_lvalue_read
= false;
2330 if (s
->incr
) s
->incr
->visit (this);
2331 s
->block
->visit (this);
2335 varuse_collecting_visitor::visit_try_block (try_block
*s
)
2338 s
->try_block
->visit (this);
2339 if (s
->catch_error_var
)
2340 written
.insert (s
->catch_error_var
->referent
);
2342 s
->catch_block
->visit (this);
2344 // NB: don't functioncall_traversing_visitor::visit_try_block (s);
2345 // since that would count s->catch_error_var as a read also.
2349 varuse_collecting_visitor::visit_functioncall (functioncall
* e
)
2351 // NB: don't call functioncall_traversing_visitor::visit_functioncall(). We
2352 // replicate functionality here but split argument visiting from actual
2353 // function visiting.
2355 bool last_lvalue_read
= current_lvalue_read
;
2357 // arguments are used
2358 current_lvalue_read
= true;
2359 traversing_visitor::visit_functioncall(e
);
2361 // but function body shouldn't all be marked used
2362 current_lvalue_read
= false;
2363 functioncall_traversing_visitor::enter_functioncall(e
);
2365 current_lvalue_read
= last_lvalue_read
;
2369 varuse_collecting_visitor::visit_return_statement (return_statement
*s
)
2371 assert(!current_lvalue_read
);
2372 current_lvalue_read
= true;
2373 functioncall_traversing_visitor::visit_return_statement(s
);
2374 current_lvalue_read
= false;
2379 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
2381 assert (current_function
); // only they get embedded code
2383 // Don't allow embedded C functions in unprivileged mode unless
2384 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2385 // or we're in a usermode runtime.
2386 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2387 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2388 ! session
.runtime_usermode_p () &&
2389 s
->code
.find ("/* unprivileged */") == string::npos
&&
2390 s
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2391 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
2392 pr_name (session
.privilege
)),
2393 current_function
->tok
);
2395 // Don't allow /* guru */ functions unless -g is active.
2396 if (!session
.guru_mode
&& s
->code
.find ("/* guru */") != string::npos
)
2397 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
2398 current_function
->tok
);
2400 // PR14524: Support old-style THIS->local syntax on per-function basis.
2401 if (s
->code
.find ("/* unmangled */") != string::npos
)
2402 current_function
->mangle_oldstyle
= true;
2404 // We want to elide embedded-C functions when possible. For
2405 // example, each $target variable access is expanded to an
2406 // embedded-C function call. Yet, for safety reasons, we should
2407 // presume that embedded-C functions have intentional side-effects.
2409 // To tell these two types of functions apart, we apply a
2410 // Kludge(tm): we look for a magic string within the function body.
2411 // $target variables as rvalues will have this; lvalues won't.
2412 // Also, explicit side-effect-free tapset functions will have this.
2414 if (s
->code
.find ("/* pure */") != string::npos
)
2417 embedded_seen
= true;
2421 // About the same case as above.
2423 varuse_collecting_visitor::visit_embedded_expr (embedded_expr
*e
)
2425 // Don't allow embedded C expressions in unprivileged mode unless
2426 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2427 // or we're in a usermode runtime.
2428 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2429 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2430 ! session
.runtime_usermode_p () &&
2431 e
->code
.find ("/* unprivileged */") == string::npos
&&
2432 e
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2433 throw SEMANTIC_ERROR (_F("embedded expression may not be used when --privilege=%s is specified",
2434 pr_name (session
.privilege
)),
2437 // Don't allow /* guru */ functions unless -g is active.
2438 if (!session
.guru_mode
&& e
->code
.find ("/* guru */") != string::npos
)
2439 throw SEMANTIC_ERROR (_("embedded expression may not be used unless -g is specified"),
2442 // We want to elide embedded-C functions when possible. For
2443 // example, each $target variable access is expanded to an
2444 // embedded-C function call. Yet, for safety reasons, we should
2445 // presume that embedded-C functions have intentional side-effects.
2447 // To tell these two types of functions apart, we apply a
2448 // Kludge(tm): we look for a magic string within the function body.
2449 // $target variables as rvalues will have this; lvalues won't.
2450 // Also, explicit side-effect-free tapset functions will have this.
2452 if (e
->code
.find ("/* pure */") != string::npos
)
2455 embedded_seen
= true;
2460 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
2462 // Still-unresolved target symbol assignments get treated as
2463 // generating side-effects like embedded-C, to prevent premature
2464 // elision and later error message suppression (PR5516). rvalue use
2465 // of unresolved target symbols is OTOH not considered a side-effect.
2467 if (is_active_lvalue (e
))
2468 embedded_seen
= true;
2470 functioncall_traversing_visitor::visit_target_symbol (e
);
2475 varuse_collecting_visitor::visit_atvar_op (atvar_op
*e
)
2477 // Similar to visit_target_symbol
2479 if (is_active_lvalue (e
))
2480 embedded_seen
= true;
2482 functioncall_traversing_visitor::visit_atvar_op (e
);
2487 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
2489 // As with target_symbols, unresolved cast assignments need to preserved
2490 // for later error handling.
2491 if (is_active_lvalue (e
))
2492 embedded_seen
= true;
2494 functioncall_traversing_visitor::visit_cast_op (e
);
2498 varuse_collecting_visitor::visit_autocast_op (autocast_op
*e
)
2500 // As with target_symbols, unresolved cast assignments need to preserved
2501 // for later error handling.
2502 if (is_active_lvalue (e
))
2503 embedded_seen
= true;
2505 functioncall_traversing_visitor::visit_autocast_op (e
);
2509 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
2512 functioncall_traversing_visitor::visit_defined_op (e
);
2516 varuse_collecting_visitor::visit_entry_op (entry_op
*e
)
2519 functioncall_traversing_visitor::visit_entry_op (e
);
2524 varuse_collecting_visitor::visit_perf_op (perf_op
*e
)
2526 functioncall_traversing_visitor::visit_perf_op (e
);
2531 varuse_collecting_visitor::visit_print_format (print_format
* e
)
2533 // NB: Instead of being top-level statements, "print" and "printf"
2534 // are implemented as statement-expressions containing a
2535 // print_format. They have side-effects, but not via the
2536 // embedded-code detection method above.
2538 // But sprint and sprintf don't have side-effects.
2540 bool last_lvalue_read
= current_lvalue_read
;
2541 current_lvalue_read
= true;
2542 if (e
->print_to_stream
)
2543 embedded_seen
= true; // a proxy for "has unknown side-effects"
2545 functioncall_traversing_visitor::visit_print_format (e
);
2546 current_lvalue_read
= last_lvalue_read
;
2551 varuse_collecting_visitor::visit_assignment (assignment
*e
)
2553 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
2555 expression
* last_lvalue
= current_lvalue
;
2556 bool last_lvalue_read
= current_lvalue_read
;
2557 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
2558 current_lvalue_read
= true;
2559 functioncall_traversing_visitor::visit_assignment (e
);
2560 current_lvalue
= last_lvalue
;
2561 current_lvalue_read
= last_lvalue_read
;
2563 else // read-modify-writes
2565 expression
* last_lrvalue
= current_lrvalue
;
2566 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
2567 functioncall_traversing_visitor::visit_assignment (e
);
2568 current_lrvalue
= last_lrvalue
;
2573 varuse_collecting_visitor::visit_ternary_expression (ternary_expression
* e
)
2575 // NB: don't call base class's implementation. We do the work here already.
2577 bool last_lvalue_read
= current_lvalue_read
;
2578 current_lvalue_read
= true;
2579 e
->cond
->visit (this);
2580 current_lvalue_read
= last_lvalue_read
;
2582 e
->truevalue
->visit (this);
2583 e
->falsevalue
->visit (this);
2587 varuse_collecting_visitor::visit_symbol (symbol
*e
)
2589 if (e
->referent
== 0)
2590 throw SEMANTIC_ERROR (_("symbol without referent"), e
->tok
);
2592 // We could handle initialized globals by marking them as "written".
2593 // However, this current visitor may be called for a function or
2594 // probe body, from the point of view of which this global is
2595 // already initialized, so not written.
2597 if (e->referent->init)
2598 written.insert (e->referent);
2601 if (current_lvalue
== e
|| current_lrvalue
== e
)
2603 written
.insert (e
->referent
);
2605 if (current_lvalue
!= e
|| current_lrvalue
== e
)
2607 read
.insert (e
->referent
);
2610 if (current_lrvalue
== e
)
2612 if (current_lvalue_read
)
2613 used
.insert (e
->referent
);
2615 else if (current_lvalue
!= e
)
2616 used
.insert (e
->referent
);
2619 // NB: stat_op need not be overridden, since it will get to
2620 // visit_symbol and only as a possible rvalue.
2624 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
2626 // NB: don't call parent implementation, we do the work here.
2628 // First let's visit the indexes separately
2629 bool old_lvalue_read
= current_lvalue_read
;
2630 current_lvalue_read
= true;
2631 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2633 e
->indexes
[i
]->visit (this);
2634 current_lvalue_read
= old_lvalue_read
;
2636 // Hooking this callback is necessary because of the hacky
2637 // statistics representation. For the expression "i[4] = 5", the
2638 // incoming lvalue will point to this arrayindex. However, the
2639 // symbol corresponding to the "i[4]" is multiply inherited with
2640 // arrayindex. If the symbol base part of this object is not at
2641 // offset 0, then static_cast<symbol*>(e) may result in a different
2642 // address, and not match lvalue by number when we recurse that way.
2643 // So we explicitly override the incoming lvalue/lrvalue values to
2644 // point at the embedded objects' actual base addresses.
2646 expression
* last_lrvalue
= current_lrvalue
;
2647 expression
* last_lvalue
= current_lvalue
;
2649 symbol
*array
= NULL
;
2650 hist_op
*hist
= NULL
;
2651 classify_indexable(e
->base
, array
, hist
);
2652 expression
*value
= array
?: hist
->stat
;
2654 if (current_lrvalue
== e
) current_lrvalue
= value
;
2655 if (current_lvalue
== e
) current_lvalue
= value
;
2657 e
->base
->visit(this);
2659 current_lrvalue
= last_lrvalue
;
2660 current_lvalue
= last_lvalue
;
2665 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
2667 expression
* last_lrvalue
= current_lrvalue
;
2668 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2669 functioncall_traversing_visitor::visit_pre_crement (e
);
2670 current_lrvalue
= last_lrvalue
;
2674 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
2676 expression
* last_lrvalue
= current_lrvalue
;
2677 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2678 functioncall_traversing_visitor::visit_post_crement (e
);
2679 current_lrvalue
= last_lrvalue
;
2683 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
2685 assert(!current_lvalue_read
);
2687 // NB: we duplicate so don't bother call
2688 // functioncall_traversing_visitor::visit_foreach_loop (s);
2690 s
->base
->visit(this);
2692 // If the collection is sorted, imply a "write" access to the
2693 // array in addition to the "read" one already noted above.
2694 if (s
->sort_direction
)
2696 symbol
*array
= NULL
;
2697 hist_op
*hist
= NULL
;
2698 classify_indexable (s
->base
, array
, hist
);
2699 if (array
) this->written
.insert (array
->referent
);
2700 // XXX: Can hist_op iterations be sorted?
2703 // NB: don't forget to visit the index expressions, which are lvalues.
2704 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
2706 expression
* last_lvalue
= current_lvalue
;
2707 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
2708 s
->indexes
[i
]->visit (this);
2709 current_lvalue
= last_lvalue
;
2712 // visit the additional specified array slice
2713 current_lvalue_read
= true;
2714 for (unsigned i
=0; i
<s
->array_slice
.size(); i
++)
2716 if (s
->array_slice
[i
])
2717 s
->array_slice
[i
]->visit (this);
2719 current_lvalue_read
= false;
2721 // The value is an lvalue too
2724 expression
* last_lvalue
= current_lvalue
;
2725 current_lvalue
= s
->value
; // leave a mark for ::visit_symbol
2726 s
->value
->visit (this);
2727 current_lvalue
= last_lvalue
;
2732 current_lvalue_read
= true;
2733 s
->limit
->visit (this);
2734 current_lvalue_read
= false;
2737 s
->block
->visit (this);
2742 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2744 // Ideally, this would be treated like an assignment: a plain write
2745 // to the underlying value ("lvalue"). XXX: However, the
2746 // optimization pass is not smart enough to remove an unneeded
2747 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2748 // should protect the underlying value from optimizional mischief.
2749 assert(!current_lvalue_read
);
2750 expression
* last_lrvalue
= current_lrvalue
;
2751 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2752 current_lvalue_read
= true;
2753 functioncall_traversing_visitor::visit_delete_statement (s
);
2754 current_lrvalue
= last_lrvalue
;
2755 current_lvalue_read
= false;
2759 varuse_collecting_visitor::side_effect_free ()
2761 return (written
.empty() && !embedded_seen
);
2766 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2768 // A looser notion of side-effect-freeness with respect to a given
2769 // list of variables.
2771 // That's useful because the written list may consist of local
2772 // variables of called functions. But visible side-effects only
2773 // occur if the client's locals, or any globals are written-to.
2775 set
<vardecl
*> intersection
;
2776 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2777 set_intersection (written
.begin(), written
.end(),
2778 vars
.begin(), vars
.end(),
2781 return (intersection
.empty() && !embedded_seen
);
2787 // ------------------------------------------------------------------------
2790 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2791 throwing_visitor::throwing_visitor (): msg (_("invalid element")) {}
2795 throwing_visitor::throwone (const token
* t
)
2797 throw SEMANTIC_ERROR (msg
, t
);
2801 throwing_visitor::visit_block (block
* s
)
2807 throwing_visitor::visit_try_block (try_block
* s
)
2814 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2820 throwing_visitor::visit_null_statement (null_statement
* s
)
2826 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2832 throwing_visitor::visit_if_statement (if_statement
* s
)
2838 throwing_visitor::visit_for_loop (for_loop
* s
)
2844 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2850 throwing_visitor::visit_return_statement (return_statement
* s
)
2856 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2862 throwing_visitor::visit_next_statement (next_statement
* s
)
2868 throwing_visitor::visit_break_statement (break_statement
* s
)
2874 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2880 throwing_visitor::visit_literal_string (literal_string
* e
)
2886 throwing_visitor::visit_literal_number (literal_number
* e
)
2892 throwing_visitor::visit_embedded_expr (embedded_expr
* e
)
2898 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2904 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2910 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2916 throwing_visitor::visit_post_crement (post_crement
* e
)
2923 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2929 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2935 throwing_visitor::visit_array_in (array_in
* e
)
2941 throwing_visitor::visit_regex_query (regex_query
* e
)
2947 throwing_visitor::visit_comparison (comparison
* e
)
2953 throwing_visitor::visit_concatenation (concatenation
* e
)
2959 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2965 throwing_visitor::visit_assignment (assignment
* e
)
2971 throwing_visitor::visit_symbol (symbol
* e
)
2977 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2983 throwing_visitor::visit_atvar_op (atvar_op
* e
)
2989 throwing_visitor::visit_cast_op (cast_op
* e
)
2995 throwing_visitor::visit_autocast_op (autocast_op
* e
)
3001 throwing_visitor::visit_defined_op (defined_op
* e
)
3007 throwing_visitor::visit_entry_op (entry_op
* e
)
3014 throwing_visitor::visit_perf_op (perf_op
* e
)
3021 throwing_visitor::visit_arrayindex (arrayindex
* e
)
3027 throwing_visitor::visit_functioncall (functioncall
* e
)
3033 throwing_visitor::visit_print_format (print_format
* e
)
3039 throwing_visitor::visit_stat_op (stat_op
* e
)
3045 throwing_visitor::visit_hist_op (hist_op
* e
)
3051 // ------------------------------------------------------------------------
3055 update_visitor::visit_block (block
* s
)
3057 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
3058 replace (s
->statements
[i
]);
3063 update_visitor::visit_try_block (try_block
* s
)
3065 replace (s
->try_block
);
3066 replace (s
->catch_error_var
);
3067 replace (s
->catch_block
);
3072 update_visitor::visit_embeddedcode (embeddedcode
* s
)
3078 update_visitor::visit_null_statement (null_statement
* s
)
3084 update_visitor::visit_expr_statement (expr_statement
* s
)
3091 update_visitor::visit_if_statement (if_statement
* s
)
3093 replace (s
->condition
);
3094 replace (s
->thenblock
);
3095 replace (s
->elseblock
);
3100 update_visitor::visit_for_loop (for_loop
* s
)
3110 update_visitor::visit_foreach_loop (foreach_loop
* s
)
3112 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
3113 replace (s
->indexes
[i
]);
3122 update_visitor::visit_return_statement (return_statement
* s
)
3129 update_visitor::visit_delete_statement (delete_statement
* s
)
3136 update_visitor::visit_next_statement (next_statement
* s
)
3142 update_visitor::visit_break_statement (break_statement
* s
)
3148 update_visitor::visit_continue_statement (continue_statement
* s
)
3154 update_visitor::visit_literal_string (literal_string
* e
)
3160 update_visitor::visit_literal_number (literal_number
* e
)
3166 update_visitor::visit_embedded_expr (embedded_expr
* e
)
3172 update_visitor::visit_binary_expression (binary_expression
* e
)
3180 update_visitor::visit_unary_expression (unary_expression
* e
)
3182 replace (e
->operand
);
3187 update_visitor::visit_pre_crement (pre_crement
* e
)
3189 replace (e
->operand
);
3194 update_visitor::visit_post_crement (post_crement
* e
)
3196 replace (e
->operand
);
3202 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3210 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3218 update_visitor::visit_array_in (array_in
* e
)
3220 replace (e
->operand
);
3225 update_visitor::visit_regex_query (regex_query
* e
)
3228 replace (e
->right
); // XXX: do we *need* to replace literal in RHS?
3233 update_visitor::visit_comparison (comparison
* e
)
3241 update_visitor::visit_concatenation (concatenation
* e
)
3249 update_visitor::visit_ternary_expression (ternary_expression
* e
)
3252 replace (e
->truevalue
);
3253 replace (e
->falsevalue
);
3258 update_visitor::visit_assignment (assignment
* e
)
3266 update_visitor::visit_symbol (symbol
* e
)
3272 update_visitor::visit_target_symbol (target_symbol
* e
)
3274 e
->visit_components (this);
3279 update_visitor::visit_cast_op (cast_op
* e
)
3281 replace (e
->operand
);
3282 e
->visit_components (this);
3287 update_visitor::visit_autocast_op (autocast_op
* e
)
3289 replace (e
->operand
);
3290 e
->visit_components (this);
3295 update_visitor::visit_atvar_op (atvar_op
* e
)
3297 e
->visit_components (this);
3302 update_visitor::visit_defined_op (defined_op
* e
)
3304 replace (e
->operand
);
3309 update_visitor::visit_entry_op (entry_op
* e
)
3311 replace (e
->operand
);
3316 update_visitor::visit_perf_op (perf_op
* e
)
3318 replace (e
->operand
);
3323 update_visitor::visit_arrayindex (arrayindex
* e
)
3326 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
3327 replace (e
->indexes
[i
]);
3332 update_visitor::visit_functioncall (functioncall
* e
)
3334 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3335 replace (e
->args
[i
]);
3340 update_visitor::visit_print_format (print_format
* e
)
3342 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3343 replace (e
->args
[i
]);
3349 update_visitor::visit_stat_op (stat_op
* e
)
3356 update_visitor::visit_hist_op (hist_op
* e
)
3363 // ------------------------------------------------------------------------
3367 deep_copy_visitor::visit_block (block
* s
)
3369 update_visitor::visit_block(new block(*s
));
3373 deep_copy_visitor::visit_try_block (try_block
* s
)
3375 update_visitor::visit_try_block(new try_block(*s
));
3379 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
3381 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
3385 deep_copy_visitor::visit_null_statement (null_statement
* s
)
3387 update_visitor::visit_null_statement(new null_statement(*s
));
3391 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
3393 update_visitor::visit_expr_statement(new expr_statement(*s
));
3397 deep_copy_visitor::visit_if_statement (if_statement
* s
)
3399 update_visitor::visit_if_statement(new if_statement(*s
));
3403 deep_copy_visitor::visit_for_loop (for_loop
* s
)
3405 update_visitor::visit_for_loop(new for_loop(*s
));
3409 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
3411 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
3415 deep_copy_visitor::visit_return_statement (return_statement
* s
)
3417 update_visitor::visit_return_statement(new return_statement(*s
));
3421 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
3423 update_visitor::visit_delete_statement(new delete_statement(*s
));
3427 deep_copy_visitor::visit_next_statement (next_statement
* s
)
3429 update_visitor::visit_next_statement(new next_statement(*s
));
3433 deep_copy_visitor::visit_break_statement (break_statement
* s
)
3435 update_visitor::visit_break_statement(new break_statement(*s
));
3439 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
3441 update_visitor::visit_continue_statement(new continue_statement(*s
));
3445 deep_copy_visitor::visit_literal_string (literal_string
* e
)
3447 update_visitor::visit_literal_string(new literal_string(*e
));
3451 deep_copy_visitor::visit_literal_number (literal_number
* e
)
3453 update_visitor::visit_literal_number(new literal_number(*e
));
3457 deep_copy_visitor::visit_embedded_expr (embedded_expr
* e
)
3459 update_visitor::visit_embedded_expr(new embedded_expr(*e
));
3463 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
3465 update_visitor::visit_binary_expression(new binary_expression(*e
));
3469 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
3471 update_visitor::visit_unary_expression(new unary_expression(*e
));
3475 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
3477 update_visitor::visit_pre_crement(new pre_crement(*e
));
3481 deep_copy_visitor::visit_post_crement (post_crement
* e
)
3483 update_visitor::visit_post_crement(new post_crement(*e
));
3488 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3490 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
3494 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3496 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
3500 deep_copy_visitor::visit_array_in (array_in
* e
)
3502 update_visitor::visit_array_in(new array_in(*e
));
3506 deep_copy_visitor::visit_regex_query (regex_query
* e
)
3508 update_visitor::visit_regex_query(new regex_query(*e
));
3512 deep_copy_visitor::visit_comparison (comparison
* e
)
3514 update_visitor::visit_comparison(new comparison(*e
));
3518 deep_copy_visitor::visit_concatenation (concatenation
* e
)
3520 update_visitor::visit_concatenation(new concatenation(*e
));
3524 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
3526 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
3530 deep_copy_visitor::visit_assignment (assignment
* e
)
3532 update_visitor::visit_assignment(new assignment(*e
));
3536 deep_copy_visitor::visit_symbol (symbol
* e
)
3538 symbol
* n
= new symbol(*e
);
3539 n
->referent
= NULL
; // don't copy!
3540 update_visitor::visit_symbol(n
);
3544 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
3546 target_symbol
* n
= new target_symbol(*e
);
3547 update_visitor::visit_target_symbol(n
);
3551 deep_copy_visitor::visit_cast_op (cast_op
* e
)
3553 update_visitor::visit_cast_op(new cast_op(*e
));
3557 deep_copy_visitor::visit_autocast_op (autocast_op
* e
)
3559 update_visitor::visit_autocast_op(new autocast_op(*e
));
3563 deep_copy_visitor::visit_atvar_op (atvar_op
* e
)
3565 update_visitor::visit_atvar_op(new atvar_op(*e
));
3569 deep_copy_visitor::visit_defined_op (defined_op
* e
)
3571 update_visitor::visit_defined_op(new defined_op(*e
));
3575 deep_copy_visitor::visit_entry_op (entry_op
* e
)
3577 update_visitor::visit_entry_op(new entry_op(*e
));
3581 deep_copy_visitor::visit_perf_op (perf_op
* e
)
3583 update_visitor::visit_perf_op(new perf_op(*e
));
3587 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
3589 update_visitor::visit_arrayindex(new arrayindex(*e
));
3593 deep_copy_visitor::visit_functioncall (functioncall
* e
)
3595 functioncall
* n
= new functioncall(*e
);
3596 n
->referent
= NULL
; // don't copy!
3597 update_visitor::visit_functioncall(n
);
3601 deep_copy_visitor::visit_print_format (print_format
* e
)
3603 update_visitor::visit_print_format(new print_format(*e
));
3607 deep_copy_visitor::visit_stat_op (stat_op
* e
)
3609 update_visitor::visit_stat_op(new stat_op(*e
));
3613 deep_copy_visitor::visit_hist_op (hist_op
* e
)
3615 update_visitor::visit_hist_op(new hist_op(*e
));
3618 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */