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",
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
)
244 literal_string::literal_string (const string
& v
)
252 operator << (ostream
& o
, const exp_type
& e
)
256 case pe_unknown
: o
<< "unknown"; break;
257 case pe_long
: o
<< "long"; break;
258 case pe_string
: o
<< "string"; break;
259 case pe_stats
: o
<< "stats"; break;
260 default: o
<< "???"; break;
267 target_symbol::assert_no_components(const std::string
& tapset
, bool pretty_ok
)
269 if (components
.empty())
272 switch (components
[0].type
)
274 case comp_literal_array_index
:
275 case comp_expression_array_index
:
276 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as array",
277 tapset
.c_str(), name
.c_str()), components
[0].tok
);
278 case comp_struct_member
:
279 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as a structure",
280 tapset
.c_str(), name
.c_str()), components
[0].tok
);
281 case comp_pretty_print
:
283 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be pretty-printed",
284 tapset
.c_str(), name
.c_str()), components
[0].tok
);
287 throw SEMANTIC_ERROR (_F("invalid use of %s variable '%s'",
288 tapset
.c_str(), name
.c_str()), components
[0].tok
);
294 target_symbol::pretty_print_depth () const
296 if (! components
.empty ())
298 const component
& last
= components
.back ();
299 if (last
.type
== comp_pretty_print
)
300 return last
.member
.length ();
307 target_symbol::check_pretty_print (bool lvalue
) const
309 if (pretty_print_depth () == 0)
313 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), tok
);
319 void target_symbol::chain (const semantic_error
&er
)
321 semantic_error
* e
= new semantic_error(er
);
324 assert (e
->get_chain() == 0);
325 if (this->saved_conversion_error
)
327 e
->set_chain(*this->saved_conversion_error
);
328 delete this->saved_conversion_error
;
330 this->saved_conversion_error
= e
;
334 string
target_symbol::sym_name ()
336 return (string
)name
.substr(1);
340 string
atvar_op::sym_name ()
345 return (string
)target_name
.substr(0, target_name
.length() - cu_name
.length() - 1);
349 // ------------------------------------------------------------------------
350 // parse tree printing
352 ostream
& operator << (ostream
& o
, const expression
& k
)
359 void literal_string::print (ostream
& o
) const
362 for (unsigned i
=0; i
<value
.size(); i
++)
363 if (value
[i
] == '"') // or other escapeworthy characters?
371 void literal_number::print (ostream
& o
) const
374 o
<< hex
<< showbase
;
377 o
<< dec
<< noshowbase
;
381 void embedded_expr::print (ostream
& o
) const
383 o
<< "%{ " << code
<< " %}";
387 void binary_expression::print (ostream
& o
) const
389 o
<< "(" << *left
<< ") "
391 << " (" << *right
<< ")";
394 void regex_query::print (ostream
& o
) const
396 // NB: we need a custom printer, because the parser does not accept
397 // a parenthesized RHS.
398 o
<< "(" << *left
<< ") "
404 void unary_expression::print (ostream
& o
) const
406 o
<< op
<< '(' << *operand
<< ")";
409 void array_in::print (ostream
& o
) const
412 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
414 if (i
> 0) o
<< ", ";
415 if (operand
->indexes
[i
])
416 operand
->indexes
[i
]->print (o
);
421 operand
->base
->print (o
);
424 void post_crement::print (ostream
& o
) const
426 o
<< '(' << *operand
<< ")" << op
;
430 void ternary_expression::print (ostream
& o
) const
432 o
<< "(" << *cond
<< ")?("
433 << *truevalue
<< "):("
434 << *falsevalue
<< ")";
438 void symbol::print (ostream
& o
) const
444 void target_symbol::component::print (ostream
& o
) const
448 case comp_pretty_print
:
449 case comp_struct_member
:
452 case comp_literal_array_index
:
453 o
<< '[' << num_index
<< ']';
455 case comp_expression_array_index
:
456 o
<< '[' << *expr_index
<< ']';
462 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
469 void target_symbol::print (ostream
& o
) const
474 for (unsigned i
= 0; i
< components
.size(); ++i
)
479 void atvar_op::print (ostream
& o
) const
483 o
<< name
<< "(\"" << target_name
<< "\")";
484 for (unsigned i
= 0; i
< components
.size(); ++i
)
489 void cast_op::print (ostream
& o
) const
493 o
<< name
<< '(' << *operand
;
494 o
<< ", " << lex_cast_qstring (type_name
);
495 if (module
.length() > 0)
496 o
<< ", " << lex_cast_qstring (module
);
498 for (unsigned i
= 0; i
< components
.size(); ++i
)
503 void autocast_op::print (ostream
& o
) const
507 o
<< '(' << *operand
<< ')';
508 for (unsigned i
= 0; i
< components
.size(); ++i
)
513 void defined_op::print (ostream
& o
) const
515 o
<< "@defined(" << *operand
<< ")";
519 void entry_op::print (ostream
& o
) const
521 o
<< "@entry(" << *operand
<< ")";
525 void perf_op::print (ostream
& o
) const
527 o
<< "@perf(" << *operand
<< ")";
531 void vardecl::print (ostream
& o
) const
537 o
<< "[" << maxsize
<< "]";
538 if (arity
> 0 || index_types
.size() > 0)
548 void vardecl::printsig (ostream
& o
) const
554 o
<< "[" << maxsize
<< "]";
556 if (index_types
.size() > 0)
559 for (unsigned i
=0; i
<index_types
.size(); i
++)
560 o
<< (i
>0 ? ", " : "") << index_types
[i
];
566 void functiondecl::print (ostream
& o
) const
568 o
<< "function " << name
<< " (";
569 for (unsigned i
=0; i
<formal_args
.size(); i
++)
570 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
576 void functiondecl::printsig (ostream
& o
) const
578 o
<< name
<< ":" << type
<< " (";
579 for (unsigned i
=0; i
<formal_args
.size(); i
++)
580 o
<< (i
>0 ? ", " : "")
583 << formal_args
[i
]->type
;
587 embedded_tags_visitor::embedded_tags_visitor(bool all_tags
)
589 // populate the set of tags that could appear in embedded code/expressions
590 available_tags
.insert("/* guru */");
591 available_tags
.insert("/* unprivileged */");
592 available_tags
.insert("/* myproc-unprivileged */");
595 available_tags
.insert("/* pure */");
596 available_tags
.insert("/* unmangled */");
597 available_tags
.insert("/* unmodified-fnargs */");
598 available_tags
.insert("/* stable */");
602 bool embedded_tags_visitor::tagged_p (const std::string
& tag
)
604 return tags
.count(tag
);
607 void embedded_tags_visitor::find_tags_in_code (const string
& s
)
609 set
<string
>::iterator tag
;
610 for (tag
= available_tags
.begin(); tag
!= available_tags
.end(); ++tag
)
611 if(s
.find(*tag
) != string::npos
)
615 void embedded_tags_visitor::visit_embeddedcode (embeddedcode
*s
)
617 find_tags_in_code(s
->code
);
620 void embedded_tags_visitor::visit_embedded_expr (embedded_expr
*e
)
622 find_tags_in_code(e
->code
);
625 void functiondecl::printsigtags (ostream
& o
, bool all_tags
) const
629 // Visit the function's body to see if there's any embedded_code or
630 // embeddedexpr that have special tags (e.g. /* guru */)
631 embedded_tags_visitor
etv(all_tags
);
632 this->body
->visit(&etv
);
634 set
<string
, bool>::const_iterator tag
;
635 for (tag
= etv
.available_tags
.begin(); tag
!= etv
.available_tags
.end(); ++tag
)
636 if (etv
.tagged_p(*tag
))
640 void arrayindex::print (ostream
& o
) const
644 for (unsigned i
=0; i
<indexes
.size(); i
++)
646 o
<< (i
>0 ? ", " : "");
656 void functioncall::print (ostream
& o
) const
658 o
<< function
<< "(";
659 for (unsigned i
=0; i
<args
.size(); i
++)
660 o
<< (i
>0 ? ", " : "") << *args
[i
];
666 print_format::create(const token
*t
, const char *n
)
668 bool stream
, format
, delim
, newline
, _char
;
669 if (n
== NULL
) n
= t
->content
.c_str();
673 format
= delim
= newline
= _char
= false;
675 if (strcmp(n
, "print_char") == 0)
685 if (0 != strncmp(n
, "print", 5))
702 if (*n
== 'l' && *(n
+1) == 'n')
713 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
, o
);
720 print_format::components_to_string(vector
<format_component
> const & components
)
724 for (vector
<format_component
>::const_iterator i
= components
.begin();
725 i
!= components
.end(); ++i
)
728 assert (i
->type
!= conv_unspecified
);
730 if (i
->type
== conv_literal
)
732 assert(!i
->literal_string
.empty());
733 for (interned_string::const_iterator j
= i
->literal_string
.begin();
734 j
!= i
->literal_string
.end(); ++j
)
736 // See also: c_unparser::visit_literal_string and lex_cast_qstring
748 if (i
->test_flag (fmt_flag_zeropad
))
751 if (i
->test_flag (fmt_flag_plus
))
754 if (i
->test_flag (fmt_flag_space
))
757 if (i
->test_flag (fmt_flag_left
))
760 if (i
->test_flag (fmt_flag_special
))
763 if (i
->widthtype
== width_dynamic
)
765 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
768 if (i
->prectype
== prec_dynamic
)
770 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
771 oss
<< '.' << i
->precision
;
786 if (i
->test_flag (fmt_flag_large
))
791 else if (i
->base
== 8)
793 else if (i
->test_flag (fmt_flag_sign
))
811 case conv_memory_hex
:
823 vector
<print_format::format_component
>
824 print_format::string_to_components(interned_string str
)
826 format_component curr
;
827 vector
<format_component
> res
;
831 interned_string::const_iterator i
= str
.begin();
834 while (i
!= str
.end())
838 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
839 curr
.type
= conv_literal
;
844 else if (i
+1 == str
.end() || *(i
+1) == '%')
847 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
848 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
849 curr
.type
= conv_literal
;
857 if (curr
.type
!= conv_unspecified
)
859 // Flush any component we were previously accumulating
860 assert (curr
.type
== conv_literal
);
861 curr
.literal_string
= literal_str
;
872 // Now we are definitely parsing a conversion.
873 // Begin by parsing flags (which are optional).
878 curr
.set_flag (fmt_flag_zeropad
);
883 curr
.set_flag (fmt_flag_plus
);
888 curr
.set_flag (fmt_flag_left
);
893 curr
.set_flag (fmt_flag_space
);
898 curr
.set_flag (fmt_flag_special
);
909 // Parse optional width
912 curr
.widthtype
= width_dynamic
;
915 else if (isdigit(*i
))
917 curr
.widthtype
= width_static
;
922 curr
.width
+= (*i
- '0');
925 while (i
!= str
.end() && isdigit(*i
));
931 // Parse optional precision
939 curr
.prectype
= prec_dynamic
;
942 else if (isdigit(*i
))
944 curr
.prectype
= prec_static
;
948 curr
.precision
*= 10;
949 curr
.precision
+= (*i
- '0');
952 while (i
!= str
.end() && isdigit(*i
));
959 // Parse the type modifier
970 // Parse the actual conversion specifier (bcsmdioupxXn)
973 // Valid conversion types
975 curr
.type
= conv_binary
;
979 curr
.type
= conv_char
;
983 curr
.type
= conv_string
;
987 curr
.type
= conv_memory
;
991 curr
.type
= conv_memory_hex
;
996 curr
.set_flag (fmt_flag_sign
);
998 curr
.type
= conv_number
;
1003 curr
.type
= conv_number
;
1008 curr
.set_flag (fmt_flag_large
);
1010 curr
.type
= conv_number
;
1015 // Since stap 1.3, %p == %#x.
1016 curr
.set_flag (fmt_flag_special
);
1017 curr
.type
= conv_pointer
;
1019 // Oddness for stap < 1.3 is handled in translation
1026 if (curr
.type
== conv_unspecified
)
1027 throw PARSE_ERROR(_("invalid or missing conversion specifier"));
1030 curr
.literal_string
= literal_str
;
1031 res
.push_back(curr
);
1032 literal_str
.clear();
1036 // If there's a remaining partly-composed conversion, fail.
1037 if (!curr
.is_empty())
1039 if (curr
.type
== conv_literal
)
1041 curr
.literal_string
= literal_str
;
1042 res
.push_back(curr
);
1043 // no need to clear curr / literal_str; we're leaving
1046 throw PARSE_ERROR(_("trailing incomplete print format conversion"));
1053 void print_format::print (ostream
& o
) const
1055 o
<< print_format_type
<< "(";
1056 if (print_with_format
)
1057 o
<< lex_cast_qstring (raw_components
);
1058 if (print_with_delim
)
1059 o
<< lex_cast_qstring (delimiter
.literal_string
);
1062 for (vector
<expression
*>::const_iterator i
= args
.begin();
1063 i
!= args
.end(); ++i
)
1065 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
1072 void stat_op::print (ostream
& o
) const
1098 assert (0); // should not happen, as sc_none is only used in foreach sorts
1106 hist_op::print (ostream
& o
) const
1112 assert(params
.size() == 3);
1113 o
<< "hist_linear(";
1115 for (size_t i
= 0; i
< params
.size(); ++i
)
1117 o
<< ", " << params
[i
];
1123 assert(params
.size() == 0);
1131 ostream
& operator << (ostream
& o
, const statement
& k
)
1138 void embeddedcode::print (ostream
&o
) const
1145 void block::print (ostream
& o
) const
1148 for (unsigned i
=0; i
<statements
.size(); i
++)
1149 o
<< *statements
[i
] << ";" << endl
;
1153 block::block (statement
* car
, statement
* cdr
)
1155 statements
.push_back(car
);
1156 statements
.push_back(cdr
);
1157 this->tok
= car
->tok
;
1162 void try_block::print (ostream
& o
) const
1164 o
<< "try {" << endl
;
1165 if (try_block
) o
<< *try_block
<< endl
;
1167 if (catch_error_var
) o
<< "(" << *catch_error_var
<< ") ";
1169 if (catch_block
) o
<< *catch_block
<< endl
;
1174 void for_loop::print (ostream
& o
) const
1177 if (init
) init
->print (o
);
1181 if (incr
) incr
->print (o
);
1187 void foreach_loop::print (ostream
& o
) const
1196 for (unsigned i
=0; i
<indexes
.size(); i
++)
1198 if (i
> 0) o
<< ", ";
1199 indexes
[i
]->print (o
);
1200 if (sort_direction
!= 0 && sort_column
== i
+1)
1201 o
<< (sort_direction
> 0 ? "+" : "-");
1205 if (!array_slice
.empty())
1208 for (unsigned i
=0; i
<array_slice
.size(); i
++)
1210 if (i
> 0) o
<< ", ";
1212 array_slice
[i
]->print (o
);
1218 if (sort_direction
!= 0 && sort_column
== 0)
1222 case sc_count
: o
<< " @count"; break;
1223 case sc_average
: o
<< " @avg"; break;
1224 case sc_min
: o
<< " @min"; break;
1225 case sc_max
: o
<< " @max"; break;
1226 case sc_sum
: o
<< " @sum"; break;
1232 o
<< (sort_direction
> 0 ? "+" : "-");
1244 void null_statement::print (ostream
& o
) const
1250 void expr_statement::print (ostream
& o
) const
1256 void return_statement::print (ostream
& o
) const
1258 o
<< "return " << *value
;
1262 void delete_statement::print (ostream
& o
) const
1264 o
<< "delete " << *value
;
1267 void next_statement::print (ostream
& o
) const
1272 void break_statement::print (ostream
& o
) const
1277 void continue_statement::print (ostream
& o
) const
1282 void if_statement::print (ostream
& o
) const
1284 o
<< "if (" << *condition
<< ") "
1285 << *thenblock
<< endl
;
1287 o
<< "else " << *elseblock
<< endl
;
1291 void stapfile::print (ostream
& o
) const
1293 o
<< "# file " << name
<< endl
;
1295 for (unsigned i
=0; i
<embeds
.size(); i
++)
1296 embeds
[i
]->print (o
);
1298 for (unsigned i
=0; i
<globals
.size(); i
++)
1301 globals
[i
]->print (o
);
1305 for (unsigned i
=0; i
<aliases
.size(); i
++)
1307 aliases
[i
]->print (o
);
1311 for (unsigned i
=0; i
<probes
.size(); i
++)
1313 probes
[i
]->print (o
);
1317 for (unsigned j
= 0; j
< functions
.size(); j
++)
1319 functions
[j
]->print (o
);
1325 void probe::print (ostream
& o
) const
1333 void probe::printsig (ostream
& o
) const
1335 const probe_alias
*alias
= get_alias ();
1338 alias
->printsig (o
);
1342 for (unsigned i
=0; i
<locations
.size(); i
++)
1344 if (i
> 0) o
<< ",";
1345 locations
[i
]->print (o
);
1351 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
) const
1353 probes_list
.push_back(const_cast<probe
*>(this));
1355 base
->collect_derivation_chain(probes_list
);
1360 probe::collect_derivation_pp_chain (std::vector
<probe_point
*> &pp_list
) const
1362 pp_list
.push_back(const_cast<probe_point
*>(this->locations
[0]));
1364 base
->collect_derivation_pp_chain(pp_list
);
1369 void probe_point::print (ostream
& o
, bool print_extras
) const
1371 for (unsigned i
=0; i
<components
.size(); i
++)
1374 probe_point::component
* c
= components
[i
];
1377 // We might like to excise this weird 0 pointer, just in
1378 // case some recursion during the error-handling path, but
1379 // can't, because what we have here is a const *this.
1380 static int been_here
= 0;
1381 if (been_here
== 0) {
1383 throw SEMANTIC_ERROR (_("internal error: missing probe point component"));
1385 continue; // ... sad panda decides to skip the bad boy
1389 o
<< "(" << *c
->arg
<< ")";
1395 else if (optional
) // sufficient implies optional
1398 o
<< " if (" << *condition
<< ")";
1401 string
probe_point::str (bool print_extras
) const
1404 print(o
, print_extras
);
1409 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1410 probe (), alias_names (aliases
), epilogue_style(false)
1414 void probe_alias::printsig (ostream
& o
) const
1416 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1418 o
<< (i
>0 ? " = " : "");
1419 alias_names
[i
]->print (o
);
1421 o
<< (epilogue_style
? " += " : " = ");
1422 for (unsigned i
=0; i
<locations
.size(); i
++)
1424 if (i
> 0) o
<< ", ";
1425 locations
[i
]->print (o
);
1430 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1437 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1445 // ------------------------------------------------------------------------
1450 block::visit (visitor
* u
)
1452 u
->visit_block (this);
1457 try_block::visit (visitor
* u
)
1459 u
->visit_try_block (this);
1464 embeddedcode::visit (visitor
* u
)
1466 u
->visit_embeddedcode (this);
1471 for_loop::visit (visitor
* u
)
1473 u
->visit_for_loop (this);
1477 foreach_loop::visit (visitor
* u
)
1479 u
->visit_foreach_loop (this);
1483 null_statement::visit (visitor
* u
)
1485 u
->visit_null_statement (this);
1489 expr_statement::visit (visitor
* u
)
1491 u
->visit_expr_statement (this);
1495 return_statement::visit (visitor
* u
)
1497 u
->visit_return_statement (this);
1501 delete_statement::visit (visitor
* u
)
1503 u
->push_active_lvalue (this->value
);
1504 u
->visit_delete_statement (this);
1505 u
->pop_active_lvalue ();
1509 if_statement::visit (visitor
* u
)
1511 u
->visit_if_statement (this);
1515 next_statement::visit (visitor
* u
)
1517 u
->visit_next_statement (this);
1521 break_statement::visit (visitor
* u
)
1523 u
->visit_break_statement (this);
1527 continue_statement::visit (visitor
* u
)
1529 u
->visit_continue_statement (this);
1533 literal_string::visit(visitor
* u
)
1535 u
->visit_literal_string (this);
1539 literal_number::visit(visitor
* u
)
1541 u
->visit_literal_number (this);
1545 binary_expression::visit (visitor
* u
)
1547 u
->visit_binary_expression (this);
1551 embedded_expr::visit (visitor
* u
)
1553 u
->visit_embedded_expr (this);
1557 unary_expression::visit (visitor
* u
)
1559 u
->visit_unary_expression (this);
1563 pre_crement::visit (visitor
* u
)
1565 u
->push_active_lvalue (this->operand
);
1566 u
->visit_pre_crement (this);
1567 u
->pop_active_lvalue ();
1571 post_crement::visit (visitor
* u
)
1573 u
->push_active_lvalue (this->operand
);
1574 u
->visit_post_crement (this);
1575 u
->pop_active_lvalue ();
1579 logical_or_expr::visit (visitor
* u
)
1581 u
->visit_logical_or_expr (this);
1585 logical_and_expr::visit (visitor
* u
)
1587 u
->visit_logical_and_expr (this);
1591 array_in::visit (visitor
* u
)
1593 u
->visit_array_in (this);
1597 regex_query::visit (visitor
* u
)
1599 u
->visit_regex_query (this);
1603 comparison::visit (visitor
* u
)
1605 u
->visit_comparison (this);
1609 concatenation::visit (visitor
* u
)
1611 u
->visit_concatenation (this);
1615 ternary_expression::visit (visitor
* u
)
1617 u
->visit_ternary_expression (this);
1621 assignment::visit (visitor
* u
)
1623 u
->push_active_lvalue (this->left
);
1624 u
->visit_assignment (this);
1625 u
->pop_active_lvalue ();
1629 symbol::visit (visitor
* u
)
1631 u
->visit_symbol (this);
1635 target_symbol::visit (visitor
* u
)
1637 u
->visit_target_symbol(this);
1641 target_symbol::visit_components (visitor
* u
)
1643 for (unsigned i
= 0; i
< components
.size(); ++i
)
1644 if (components
[i
].type
== comp_expression_array_index
)
1645 components
[i
].expr_index
->visit (u
);
1649 target_symbol::visit_components (update_visitor
* u
)
1651 for (unsigned i
= 0; i
< components
.size(); ++i
)
1652 if (components
[i
].type
== comp_expression_array_index
)
1653 u
->replace (components
[i
].expr_index
);
1657 cast_op::visit (visitor
* u
)
1659 u
->visit_cast_op(this);
1664 autocast_op::visit (visitor
* u
)
1666 u
->visit_autocast_op(this);
1671 atvar_op::visit (visitor
* u
)
1673 u
->visit_atvar_op(this);
1678 defined_op::visit (visitor
* u
)
1680 u
->visit_defined_op(this);
1685 entry_op::visit (visitor
* u
)
1687 u
->visit_entry_op(this);
1692 perf_op::visit (visitor
* u
)
1694 u
->visit_perf_op(this);
1699 arrayindex::visit (visitor
* u
)
1701 u
->visit_arrayindex (this);
1705 functioncall::visit (visitor
* u
)
1707 u
->visit_functioncall (this);
1711 print_format::visit (visitor
*u
)
1713 u
->visit_print_format (this);
1717 stat_op::visit (visitor
*u
)
1719 u
->visit_stat_op (this);
1723 hist_op::visit (visitor
*u
)
1725 u
->visit_hist_op (this);
1730 expression::is_symbol(symbol
*& sym_out
)
1737 indexable::is_symbol(symbol
*& sym_out
)
1744 indexable::is_hist_op(hist_op
*& hist_out
)
1751 symbol::is_symbol(symbol
*& sym_out
)
1758 hist_op::is_hist_op(hist_op
*& hist_out
)
1765 classify_indexable(indexable
* ix
,
1766 symbol
*& array_out
,
1767 hist_op
*& hist_out
)
1772 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1773 throw SEMANTIC_ERROR(_("Expecting symbol or histogram operator"), ix
->tok
);
1774 if (!(hist_out
|| array_out
))
1775 throw SEMANTIC_ERROR(_("Failed to classify indexable"), ix
->tok
);
1779 // ------------------------------------------------------------------------
1782 visitor::is_active_lvalue(expression
*e
)
1784 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1786 if (active_lvalues
[i
] == e
)
1793 visitor::push_active_lvalue(expression
*e
)
1795 active_lvalues
.push_back(e
);
1799 visitor::pop_active_lvalue()
1801 assert(!active_lvalues
.empty());
1802 active_lvalues
.pop_back();
1807 // ------------------------------------------------------------------------
1810 traversing_visitor::visit_block (block
* s
)
1812 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1813 s
->statements
[i
]->visit (this);
1817 traversing_visitor::visit_try_block (try_block
* s
)
1820 s
->try_block
->visit (this);
1821 if (s
->catch_error_var
)
1822 s
->catch_error_var
->visit (this);
1824 s
->catch_block
->visit (this);
1828 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1833 traversing_visitor::visit_null_statement (null_statement
*)
1838 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1840 s
->value
->visit (this);
1844 traversing_visitor::visit_if_statement (if_statement
* s
)
1846 s
->condition
->visit (this);
1847 s
->thenblock
->visit (this);
1849 s
->elseblock
->visit (this);
1853 traversing_visitor::visit_for_loop (for_loop
* s
)
1855 if (s
->init
) s
->init
->visit (this);
1856 s
->cond
->visit (this);
1857 if (s
->incr
) s
->incr
->visit (this);
1858 s
->block
->visit (this);
1862 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1864 s
->base
->visit(this);
1866 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1867 s
->indexes
[i
]->visit (this);
1870 s
->value
->visit (this);
1873 s
->limit
->visit (this);
1875 s
->block
->visit (this);
1879 traversing_visitor::visit_return_statement (return_statement
* s
)
1881 s
->value
->visit (this);
1885 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1887 s
->value
->visit (this);
1891 traversing_visitor::visit_next_statement (next_statement
*)
1896 traversing_visitor::visit_break_statement (break_statement
*)
1901 traversing_visitor::visit_continue_statement (continue_statement
*)
1906 traversing_visitor::visit_literal_string (literal_string
*)
1911 traversing_visitor::visit_literal_number (literal_number
*)
1916 traversing_visitor::visit_embedded_expr (embedded_expr
*)
1921 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1923 e
->left
->visit (this);
1924 e
->right
->visit (this);
1928 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1930 e
->operand
->visit (this);
1934 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1936 e
->operand
->visit (this);
1940 traversing_visitor::visit_post_crement (post_crement
* e
)
1942 e
->operand
->visit (this);
1947 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1949 e
->left
->visit (this);
1950 e
->right
->visit (this);
1954 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1956 e
->left
->visit (this);
1957 e
->right
->visit (this);
1961 traversing_visitor::visit_array_in (array_in
* e
)
1963 e
->operand
->visit (this);
1967 traversing_visitor::visit_regex_query (regex_query
* e
)
1969 e
->left
->visit (this);
1970 e
->right
->visit (this);
1974 traversing_visitor::visit_comparison (comparison
* e
)
1976 e
->left
->visit (this);
1977 e
->right
->visit (this);
1981 traversing_visitor::visit_concatenation (concatenation
* e
)
1983 e
->left
->visit (this);
1984 e
->right
->visit (this);
1988 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1990 e
->cond
->visit (this);
1991 e
->truevalue
->visit (this);
1992 e
->falsevalue
->visit (this);
1996 traversing_visitor::visit_assignment (assignment
* e
)
1998 e
->left
->visit (this);
1999 e
->right
->visit (this);
2003 traversing_visitor::visit_symbol (symbol
*)
2008 traversing_visitor::visit_target_symbol (target_symbol
* e
)
2010 e
->visit_components (this);
2014 traversing_visitor::visit_cast_op (cast_op
* e
)
2016 e
->operand
->visit (this);
2017 e
->visit_components (this);
2021 traversing_visitor::visit_autocast_op (autocast_op
* e
)
2023 e
->operand
->visit (this);
2024 e
->visit_components (this);
2028 traversing_visitor::visit_atvar_op (atvar_op
* e
)
2030 e
->visit_components (this);
2034 traversing_visitor::visit_defined_op (defined_op
* e
)
2036 e
->operand
->visit (this);
2040 traversing_visitor::visit_entry_op (entry_op
* e
)
2042 e
->operand
->visit (this);
2047 traversing_visitor::visit_perf_op (perf_op
* e
)
2049 e
->operand
->visit (this);
2054 traversing_visitor::visit_arrayindex (arrayindex
* e
)
2056 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2058 e
->indexes
[i
]->visit (this);
2060 e
->base
->visit(this);
2064 traversing_visitor::visit_functioncall (functioncall
* e
)
2066 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2067 e
->args
[i
]->visit (this);
2071 traversing_visitor::visit_print_format (print_format
* e
)
2073 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2074 e
->args
[i
]->visit (this);
2076 e
->hist
->visit(this);
2080 traversing_visitor::visit_stat_op (stat_op
* e
)
2082 e
->stat
->visit (this);
2086 traversing_visitor::visit_hist_op (hist_op
* e
)
2088 e
->stat
->visit (this);
2093 expression_visitor::visit_literal_string (literal_string
* e
)
2095 traversing_visitor::visit_literal_string (e
);
2096 visit_expression (e
);
2100 expression_visitor::visit_literal_number (literal_number
* e
)
2102 traversing_visitor::visit_literal_number (e
);
2103 visit_expression (e
);
2107 expression_visitor::visit_embedded_expr (embedded_expr
* e
)
2109 traversing_visitor::visit_embedded_expr (e
);
2110 visit_expression (e
);
2114 expression_visitor::visit_binary_expression (binary_expression
* e
)
2116 traversing_visitor::visit_binary_expression (e
);
2117 visit_expression (e
);
2121 expression_visitor::visit_unary_expression (unary_expression
* e
)
2123 traversing_visitor::visit_unary_expression (e
);
2124 visit_expression (e
);
2128 expression_visitor::visit_pre_crement (pre_crement
* e
)
2130 traversing_visitor::visit_pre_crement (e
);
2131 visit_expression (e
);
2135 expression_visitor::visit_post_crement (post_crement
* e
)
2137 traversing_visitor::visit_post_crement (e
);
2138 visit_expression (e
);
2142 expression_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2144 traversing_visitor::visit_logical_or_expr (e
);
2145 visit_expression (e
);
2149 expression_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2151 traversing_visitor::visit_logical_and_expr (e
);
2152 visit_expression (e
);
2156 expression_visitor::visit_array_in (array_in
* e
)
2158 traversing_visitor::visit_array_in (e
);
2159 visit_expression (e
);
2163 expression_visitor::visit_regex_query (regex_query
* e
)
2165 traversing_visitor::visit_regex_query (e
);
2166 visit_expression (e
);
2170 expression_visitor::visit_comparison (comparison
* e
)
2172 traversing_visitor::visit_comparison (e
);
2173 visit_expression (e
);
2177 expression_visitor::visit_concatenation (concatenation
* e
)
2179 traversing_visitor::visit_concatenation (e
);
2180 visit_expression (e
);
2184 expression_visitor::visit_ternary_expression (ternary_expression
* e
)
2186 traversing_visitor::visit_ternary_expression (e
);
2187 visit_expression (e
);
2191 expression_visitor::visit_assignment (assignment
* e
)
2193 traversing_visitor::visit_assignment (e
);
2194 visit_expression (e
);
2198 expression_visitor::visit_symbol (symbol
* e
)
2200 traversing_visitor::visit_symbol (e
);
2201 visit_expression (e
);
2205 expression_visitor::visit_target_symbol (target_symbol
* e
)
2207 traversing_visitor::visit_target_symbol (e
);
2208 visit_expression (e
);
2212 expression_visitor::visit_arrayindex (arrayindex
* e
)
2214 traversing_visitor::visit_arrayindex (e
);
2215 visit_expression (e
);
2219 expression_visitor::visit_functioncall (functioncall
* e
)
2221 traversing_visitor::visit_functioncall (e
);
2222 visit_expression (e
);
2226 expression_visitor::visit_print_format (print_format
* e
)
2228 traversing_visitor::visit_print_format (e
);
2229 visit_expression (e
);
2233 expression_visitor::visit_stat_op (stat_op
* e
)
2235 traversing_visitor::visit_stat_op (e
);
2236 visit_expression (e
);
2240 expression_visitor::visit_hist_op (hist_op
* e
)
2242 traversing_visitor::visit_hist_op (e
);
2243 visit_expression (e
);
2247 expression_visitor::visit_cast_op (cast_op
* e
)
2249 traversing_visitor::visit_cast_op (e
);
2250 visit_expression (e
);
2254 expression_visitor::visit_autocast_op (autocast_op
* e
)
2256 traversing_visitor::visit_autocast_op (e
);
2257 visit_expression (e
);
2261 expression_visitor::visit_atvar_op (atvar_op
* e
)
2263 traversing_visitor::visit_atvar_op (e
);
2264 visit_expression (e
);
2268 expression_visitor::visit_defined_op (defined_op
* e
)
2270 traversing_visitor::visit_defined_op (e
);
2271 visit_expression (e
);
2275 expression_visitor::visit_entry_op (entry_op
* e
)
2277 traversing_visitor::visit_entry_op (e
);
2278 visit_expression (e
);
2282 expression_visitor::visit_perf_op (perf_op
* e
)
2284 traversing_visitor::visit_perf_op (e
);
2285 visit_expression (e
);
2290 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
2292 traversing_visitor::visit_functioncall (e
);
2293 this->enter_functioncall(e
);
2297 functioncall_traversing_visitor::enter_functioncall (functioncall
* e
)
2299 // prevent infinite recursion
2300 if (nested
.find (e
->referent
) == nested
.end ())
2302 if (seen
.find(e
->referent
) == seen
.end())
2303 seen
.insert (e
->referent
);
2304 nested
.insert (e
->referent
);
2306 functiondecl
* last_current_function
= current_function
;
2307 current_function
= e
->referent
;
2308 e
->referent
->body
->visit (this);
2309 current_function
= last_current_function
;
2310 nested
.erase (e
->referent
);
2312 else { this->note_recursive_functioncall(e
); }
2316 functioncall_traversing_visitor::note_recursive_functioncall (functioncall
* e
)
2321 varuse_collecting_visitor::visit_if_statement (if_statement
*s
)
2323 assert(!current_lvalue_read
);
2324 current_lvalue_read
= true;
2325 s
->condition
->visit (this);
2326 current_lvalue_read
= false;
2328 s
->thenblock
->visit (this);
2330 s
->elseblock
->visit (this);
2335 varuse_collecting_visitor::visit_for_loop (for_loop
*s
)
2337 if (s
->init
) s
->init
->visit (this);
2339 assert(!current_lvalue_read
);
2340 current_lvalue_read
= true;
2341 s
->cond
->visit (this);
2342 current_lvalue_read
= false;
2344 if (s
->incr
) s
->incr
->visit (this);
2345 s
->block
->visit (this);
2349 varuse_collecting_visitor::visit_try_block (try_block
*s
)
2352 s
->try_block
->visit (this);
2353 if (s
->catch_error_var
)
2354 written
.insert (s
->catch_error_var
->referent
);
2356 s
->catch_block
->visit (this);
2358 // NB: don't functioncall_traversing_visitor::visit_try_block (s);
2359 // since that would count s->catch_error_var as a read also.
2363 varuse_collecting_visitor::visit_functioncall (functioncall
* e
)
2365 // NB: don't call functioncall_traversing_visitor::visit_functioncall(). We
2366 // replicate functionality here but split argument visiting from actual
2367 // function visiting.
2369 bool last_lvalue_read
= current_lvalue_read
;
2371 // arguments are used
2372 current_lvalue_read
= true;
2373 traversing_visitor::visit_functioncall(e
);
2375 // but function body shouldn't all be marked used
2376 current_lvalue_read
= false;
2377 functioncall_traversing_visitor::enter_functioncall(e
);
2379 current_lvalue_read
= last_lvalue_read
;
2383 varuse_collecting_visitor::visit_return_statement (return_statement
*s
)
2385 assert(!current_lvalue_read
);
2386 current_lvalue_read
= true;
2387 functioncall_traversing_visitor::visit_return_statement(s
);
2388 current_lvalue_read
= false;
2393 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
2395 assert (current_function
); // only they get embedded code
2397 // Don't allow embedded C functions in unprivileged mode unless
2398 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2399 // or we're in a usermode runtime.
2400 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2401 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2402 ! session
.runtime_usermode_p () &&
2403 s
->code
.find ("/* unprivileged */") == string::npos
&&
2404 s
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2405 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
2406 pr_name (session
.privilege
)),
2407 current_function
->tok
);
2409 // Don't allow /* guru */ functions unless -g is active.
2410 if (!session
.guru_mode
&& s
->code
.find ("/* guru */") != string::npos
)
2411 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
2412 current_function
->tok
);
2414 // PR14524: Support old-style THIS->local syntax on per-function basis.
2415 if (s
->code
.find ("/* unmangled */") != string::npos
)
2416 current_function
->mangle_oldstyle
= true;
2418 // We want to elide embedded-C functions when possible. For
2419 // example, each $target variable access is expanded to an
2420 // embedded-C function call. Yet, for safety reasons, we should
2421 // presume that embedded-C functions have intentional side-effects.
2423 // To tell these two types of functions apart, we apply a
2424 // Kludge(tm): we look for a magic string within the function body.
2425 // $target variables as rvalues will have this; lvalues won't.
2426 // Also, explicit side-effect-free tapset functions will have this.
2428 if (s
->code
.find ("/* pure */") != string::npos
)
2431 embedded_seen
= true;
2435 // About the same case as above.
2437 varuse_collecting_visitor::visit_embedded_expr (embedded_expr
*e
)
2439 // Don't allow embedded C expressions in unprivileged mode unless
2440 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2441 // or we're in a usermode runtime.
2442 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2443 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2444 ! session
.runtime_usermode_p () &&
2445 e
->code
.find ("/* unprivileged */") == string::npos
&&
2446 e
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2447 throw SEMANTIC_ERROR (_F("embedded expression may not be used when --privilege=%s is specified",
2448 pr_name (session
.privilege
)),
2451 // Don't allow /* guru */ functions unless -g is active.
2452 if (!session
.guru_mode
&& e
->code
.find ("/* guru */") != string::npos
)
2453 throw SEMANTIC_ERROR (_("embedded expression may not be used unless -g is specified"),
2456 // We want to elide embedded-C functions when possible. For
2457 // example, each $target variable access is expanded to an
2458 // embedded-C function call. Yet, for safety reasons, we should
2459 // presume that embedded-C functions have intentional side-effects.
2461 // To tell these two types of functions apart, we apply a
2462 // Kludge(tm): we look for a magic string within the function body.
2463 // $target variables as rvalues will have this; lvalues won't.
2464 // Also, explicit side-effect-free tapset functions will have this.
2466 if (e
->code
.find ("/* pure */") != string::npos
)
2469 embedded_seen
= true;
2474 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
2476 // Still-unresolved target symbol assignments get treated as
2477 // generating side-effects like embedded-C, to prevent premature
2478 // elision and later error message suppression (PR5516). rvalue use
2479 // of unresolved target symbols is OTOH not considered a side-effect.
2481 if (is_active_lvalue (e
))
2482 embedded_seen
= true;
2484 functioncall_traversing_visitor::visit_target_symbol (e
);
2489 varuse_collecting_visitor::visit_atvar_op (atvar_op
*e
)
2491 // Similar to visit_target_symbol
2493 if (is_active_lvalue (e
))
2494 embedded_seen
= true;
2496 functioncall_traversing_visitor::visit_atvar_op (e
);
2501 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
2503 // As with target_symbols, unresolved cast assignments need to preserved
2504 // for later error handling.
2505 if (is_active_lvalue (e
))
2506 embedded_seen
= true;
2508 functioncall_traversing_visitor::visit_cast_op (e
);
2512 varuse_collecting_visitor::visit_autocast_op (autocast_op
*e
)
2514 // As with target_symbols, unresolved cast assignments need to preserved
2515 // for later error handling.
2516 if (is_active_lvalue (e
))
2517 embedded_seen
= true;
2519 functioncall_traversing_visitor::visit_autocast_op (e
);
2523 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
2526 functioncall_traversing_visitor::visit_defined_op (e
);
2530 varuse_collecting_visitor::visit_entry_op (entry_op
*e
)
2533 functioncall_traversing_visitor::visit_entry_op (e
);
2538 varuse_collecting_visitor::visit_perf_op (perf_op
*e
)
2540 functioncall_traversing_visitor::visit_perf_op (e
);
2545 varuse_collecting_visitor::visit_print_format (print_format
* e
)
2547 // NB: Instead of being top-level statements, "print" and "printf"
2548 // are implemented as statement-expressions containing a
2549 // print_format. They have side-effects, but not via the
2550 // embedded-code detection method above.
2552 // But sprint and sprintf don't have side-effects.
2554 bool last_lvalue_read
= current_lvalue_read
;
2555 current_lvalue_read
= true;
2556 if (e
->print_to_stream
)
2557 embedded_seen
= true; // a proxy for "has unknown side-effects"
2559 functioncall_traversing_visitor::visit_print_format (e
);
2560 current_lvalue_read
= last_lvalue_read
;
2565 varuse_collecting_visitor::visit_assignment (assignment
*e
)
2567 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
2569 expression
* last_lvalue
= current_lvalue
;
2570 bool last_lvalue_read
= current_lvalue_read
;
2571 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
2572 current_lvalue_read
= true;
2573 functioncall_traversing_visitor::visit_assignment (e
);
2574 current_lvalue
= last_lvalue
;
2575 current_lvalue_read
= last_lvalue_read
;
2577 else // read-modify-writes
2579 expression
* last_lrvalue
= current_lrvalue
;
2580 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
2581 functioncall_traversing_visitor::visit_assignment (e
);
2582 current_lrvalue
= last_lrvalue
;
2587 varuse_collecting_visitor::visit_ternary_expression (ternary_expression
* e
)
2589 // NB: don't call base class's implementation. We do the work here already.
2591 bool last_lvalue_read
= current_lvalue_read
;
2592 current_lvalue_read
= true;
2593 e
->cond
->visit (this);
2594 current_lvalue_read
= last_lvalue_read
;
2596 e
->truevalue
->visit (this);
2597 e
->falsevalue
->visit (this);
2601 varuse_collecting_visitor::visit_symbol (symbol
*e
)
2603 if (e
->referent
== 0)
2604 throw SEMANTIC_ERROR (_("symbol without referent"), e
->tok
);
2606 // We could handle initialized globals by marking them as "written".
2607 // However, this current visitor may be called for a function or
2608 // probe body, from the point of view of which this global is
2609 // already initialized, so not written.
2611 if (e->referent->init)
2612 written.insert (e->referent);
2615 if (current_lvalue
== e
|| current_lrvalue
== e
)
2617 written
.insert (e
->referent
);
2619 if (current_lvalue
!= e
|| current_lrvalue
== e
)
2621 read
.insert (e
->referent
);
2624 if (current_lrvalue
== e
)
2626 if (current_lvalue_read
)
2627 used
.insert (e
->referent
);
2629 else if (current_lvalue
!= e
)
2630 used
.insert (e
->referent
);
2633 // NB: stat_op need not be overridden, since it will get to
2634 // visit_symbol and only as a possible rvalue.
2638 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
2640 // NB: don't call parent implementation, we do the work here.
2642 // First let's visit the indexes separately
2643 bool old_lvalue_read
= current_lvalue_read
;
2644 current_lvalue_read
= true;
2645 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2647 e
->indexes
[i
]->visit (this);
2648 current_lvalue_read
= old_lvalue_read
;
2650 // Hooking this callback is necessary because of the hacky
2651 // statistics representation. For the expression "i[4] = 5", the
2652 // incoming lvalue will point to this arrayindex. However, the
2653 // symbol corresponding to the "i[4]" is multiply inherited with
2654 // arrayindex. If the symbol base part of this object is not at
2655 // offset 0, then static_cast<symbol*>(e) may result in a different
2656 // address, and not match lvalue by number when we recurse that way.
2657 // So we explicitly override the incoming lvalue/lrvalue values to
2658 // point at the embedded objects' actual base addresses.
2660 expression
* last_lrvalue
= current_lrvalue
;
2661 expression
* last_lvalue
= current_lvalue
;
2663 symbol
*array
= NULL
;
2664 hist_op
*hist
= NULL
;
2665 classify_indexable(e
->base
, array
, hist
);
2666 expression
*value
= array
?: hist
->stat
;
2668 if (current_lrvalue
== e
) current_lrvalue
= value
;
2669 if (current_lvalue
== e
) current_lvalue
= value
;
2671 e
->base
->visit(this);
2673 current_lrvalue
= last_lrvalue
;
2674 current_lvalue
= last_lvalue
;
2679 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
2681 expression
* last_lrvalue
= current_lrvalue
;
2682 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2683 functioncall_traversing_visitor::visit_pre_crement (e
);
2684 current_lrvalue
= last_lrvalue
;
2688 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
2690 expression
* last_lrvalue
= current_lrvalue
;
2691 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2692 functioncall_traversing_visitor::visit_post_crement (e
);
2693 current_lrvalue
= last_lrvalue
;
2697 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
2699 assert(!current_lvalue_read
);
2701 // NB: we duplicate so don't bother call
2702 // functioncall_traversing_visitor::visit_foreach_loop (s);
2704 s
->base
->visit(this);
2706 // If the collection is sorted, imply a "write" access to the
2707 // array in addition to the "read" one already noted above.
2708 if (s
->sort_direction
)
2710 symbol
*array
= NULL
;
2711 hist_op
*hist
= NULL
;
2712 classify_indexable (s
->base
, array
, hist
);
2713 if (array
) this->written
.insert (array
->referent
);
2714 // XXX: Can hist_op iterations be sorted?
2717 // NB: don't forget to visit the index expressions, which are lvalues.
2718 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
2720 expression
* last_lvalue
= current_lvalue
;
2721 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
2722 s
->indexes
[i
]->visit (this);
2723 current_lvalue
= last_lvalue
;
2726 // visit the additional specified array slice
2727 current_lvalue_read
= true;
2728 for (unsigned i
=0; i
<s
->array_slice
.size(); i
++)
2730 if (s
->array_slice
[i
])
2731 s
->array_slice
[i
]->visit (this);
2733 current_lvalue_read
= false;
2735 // The value is an lvalue too
2738 expression
* last_lvalue
= current_lvalue
;
2739 current_lvalue
= s
->value
; // leave a mark for ::visit_symbol
2740 s
->value
->visit (this);
2741 current_lvalue
= last_lvalue
;
2746 current_lvalue_read
= true;
2747 s
->limit
->visit (this);
2748 current_lvalue_read
= false;
2751 s
->block
->visit (this);
2756 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2758 // Ideally, this would be treated like an assignment: a plain write
2759 // to the underlying value ("lvalue"). XXX: However, the
2760 // optimization pass is not smart enough to remove an unneeded
2761 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2762 // should protect the underlying value from optimizional mischief.
2763 assert(!current_lvalue_read
);
2764 expression
* last_lrvalue
= current_lrvalue
;
2765 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2766 current_lvalue_read
= true;
2767 functioncall_traversing_visitor::visit_delete_statement (s
);
2768 current_lrvalue
= last_lrvalue
;
2769 current_lvalue_read
= false;
2773 varuse_collecting_visitor::side_effect_free ()
2775 return (written
.empty() && !embedded_seen
);
2780 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2782 // A looser notion of side-effect-freeness with respect to a given
2783 // list of variables.
2785 // That's useful because the written list may consist of local
2786 // variables of called functions. But visible side-effects only
2787 // occur if the client's locals, or any globals are written-to.
2789 set
<vardecl
*> intersection
;
2790 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2791 set_intersection (written
.begin(), written
.end(),
2792 vars
.begin(), vars
.end(),
2795 return (intersection
.empty() && !embedded_seen
);
2801 // ------------------------------------------------------------------------
2804 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2805 throwing_visitor::throwing_visitor (): msg (_("invalid element")) {}
2809 throwing_visitor::throwone (const token
* t
)
2811 throw SEMANTIC_ERROR (msg
, t
);
2815 throwing_visitor::visit_block (block
* s
)
2821 throwing_visitor::visit_try_block (try_block
* s
)
2828 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2834 throwing_visitor::visit_null_statement (null_statement
* s
)
2840 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2846 throwing_visitor::visit_if_statement (if_statement
* s
)
2852 throwing_visitor::visit_for_loop (for_loop
* s
)
2858 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2864 throwing_visitor::visit_return_statement (return_statement
* s
)
2870 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2876 throwing_visitor::visit_next_statement (next_statement
* s
)
2882 throwing_visitor::visit_break_statement (break_statement
* s
)
2888 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2894 throwing_visitor::visit_literal_string (literal_string
* e
)
2900 throwing_visitor::visit_literal_number (literal_number
* e
)
2906 throwing_visitor::visit_embedded_expr (embedded_expr
* e
)
2912 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2918 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2924 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2930 throwing_visitor::visit_post_crement (post_crement
* e
)
2937 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2943 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2949 throwing_visitor::visit_array_in (array_in
* e
)
2955 throwing_visitor::visit_regex_query (regex_query
* e
)
2961 throwing_visitor::visit_comparison (comparison
* e
)
2967 throwing_visitor::visit_concatenation (concatenation
* e
)
2973 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2979 throwing_visitor::visit_assignment (assignment
* e
)
2985 throwing_visitor::visit_symbol (symbol
* e
)
2991 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2997 throwing_visitor::visit_atvar_op (atvar_op
* e
)
3003 throwing_visitor::visit_cast_op (cast_op
* e
)
3009 throwing_visitor::visit_autocast_op (autocast_op
* e
)
3015 throwing_visitor::visit_defined_op (defined_op
* e
)
3021 throwing_visitor::visit_entry_op (entry_op
* e
)
3028 throwing_visitor::visit_perf_op (perf_op
* e
)
3035 throwing_visitor::visit_arrayindex (arrayindex
* e
)
3041 throwing_visitor::visit_functioncall (functioncall
* e
)
3047 throwing_visitor::visit_print_format (print_format
* e
)
3053 throwing_visitor::visit_stat_op (stat_op
* e
)
3059 throwing_visitor::visit_hist_op (hist_op
* e
)
3065 // ------------------------------------------------------------------------
3069 update_visitor::visit_block (block
* s
)
3071 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
3072 replace (s
->statements
[i
]);
3077 update_visitor::visit_try_block (try_block
* s
)
3079 replace (s
->try_block
);
3080 replace (s
->catch_error_var
);
3081 replace (s
->catch_block
);
3086 update_visitor::visit_embeddedcode (embeddedcode
* s
)
3092 update_visitor::visit_null_statement (null_statement
* s
)
3098 update_visitor::visit_expr_statement (expr_statement
* s
)
3105 update_visitor::visit_if_statement (if_statement
* s
)
3107 replace (s
->condition
);
3108 replace (s
->thenblock
);
3109 replace (s
->elseblock
);
3114 update_visitor::visit_for_loop (for_loop
* s
)
3124 update_visitor::visit_foreach_loop (foreach_loop
* s
)
3126 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
3127 replace (s
->indexes
[i
]);
3136 update_visitor::visit_return_statement (return_statement
* s
)
3143 update_visitor::visit_delete_statement (delete_statement
* s
)
3150 update_visitor::visit_next_statement (next_statement
* s
)
3156 update_visitor::visit_break_statement (break_statement
* s
)
3162 update_visitor::visit_continue_statement (continue_statement
* s
)
3168 update_visitor::visit_literal_string (literal_string
* e
)
3174 update_visitor::visit_literal_number (literal_number
* e
)
3180 update_visitor::visit_embedded_expr (embedded_expr
* e
)
3186 update_visitor::visit_binary_expression (binary_expression
* e
)
3194 update_visitor::visit_unary_expression (unary_expression
* e
)
3196 replace (e
->operand
);
3201 update_visitor::visit_pre_crement (pre_crement
* e
)
3203 replace (e
->operand
);
3208 update_visitor::visit_post_crement (post_crement
* e
)
3210 replace (e
->operand
);
3216 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3224 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3232 update_visitor::visit_array_in (array_in
* e
)
3234 replace (e
->operand
);
3239 update_visitor::visit_regex_query (regex_query
* e
)
3242 replace (e
->right
); // XXX: do we *need* to replace literal in RHS?
3247 update_visitor::visit_comparison (comparison
* e
)
3255 update_visitor::visit_concatenation (concatenation
* e
)
3263 update_visitor::visit_ternary_expression (ternary_expression
* e
)
3266 replace (e
->truevalue
);
3267 replace (e
->falsevalue
);
3272 update_visitor::visit_assignment (assignment
* e
)
3280 update_visitor::visit_symbol (symbol
* e
)
3286 update_visitor::visit_target_symbol (target_symbol
* e
)
3288 e
->visit_components (this);
3293 update_visitor::visit_cast_op (cast_op
* e
)
3295 replace (e
->operand
);
3296 e
->visit_components (this);
3301 update_visitor::visit_autocast_op (autocast_op
* e
)
3303 replace (e
->operand
);
3304 e
->visit_components (this);
3309 update_visitor::visit_atvar_op (atvar_op
* e
)
3311 e
->visit_components (this);
3316 update_visitor::visit_defined_op (defined_op
* e
)
3318 replace (e
->operand
);
3323 update_visitor::visit_entry_op (entry_op
* e
)
3325 replace (e
->operand
);
3330 update_visitor::visit_perf_op (perf_op
* e
)
3332 replace (e
->operand
);
3337 update_visitor::visit_arrayindex (arrayindex
* e
)
3340 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
3341 replace (e
->indexes
[i
]);
3346 update_visitor::visit_functioncall (functioncall
* e
)
3348 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3349 replace (e
->args
[i
]);
3354 update_visitor::visit_print_format (print_format
* e
)
3356 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3357 replace (e
->args
[i
]);
3363 update_visitor::visit_stat_op (stat_op
* e
)
3370 update_visitor::visit_hist_op (hist_op
* e
)
3377 // ------------------------------------------------------------------------
3381 deep_copy_visitor::visit_block (block
* s
)
3383 update_visitor::visit_block(new block(*s
));
3387 deep_copy_visitor::visit_try_block (try_block
* s
)
3389 update_visitor::visit_try_block(new try_block(*s
));
3393 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
3395 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
3399 deep_copy_visitor::visit_null_statement (null_statement
* s
)
3401 update_visitor::visit_null_statement(new null_statement(*s
));
3405 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
3407 update_visitor::visit_expr_statement(new expr_statement(*s
));
3411 deep_copy_visitor::visit_if_statement (if_statement
* s
)
3413 update_visitor::visit_if_statement(new if_statement(*s
));
3417 deep_copy_visitor::visit_for_loop (for_loop
* s
)
3419 update_visitor::visit_for_loop(new for_loop(*s
));
3423 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
3425 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
3429 deep_copy_visitor::visit_return_statement (return_statement
* s
)
3431 update_visitor::visit_return_statement(new return_statement(*s
));
3435 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
3437 update_visitor::visit_delete_statement(new delete_statement(*s
));
3441 deep_copy_visitor::visit_next_statement (next_statement
* s
)
3443 update_visitor::visit_next_statement(new next_statement(*s
));
3447 deep_copy_visitor::visit_break_statement (break_statement
* s
)
3449 update_visitor::visit_break_statement(new break_statement(*s
));
3453 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
3455 update_visitor::visit_continue_statement(new continue_statement(*s
));
3459 deep_copy_visitor::visit_literal_string (literal_string
* e
)
3461 update_visitor::visit_literal_string(new literal_string(*e
));
3465 deep_copy_visitor::visit_literal_number (literal_number
* e
)
3467 update_visitor::visit_literal_number(new literal_number(*e
));
3471 deep_copy_visitor::visit_embedded_expr (embedded_expr
* e
)
3473 update_visitor::visit_embedded_expr(new embedded_expr(*e
));
3477 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
3479 update_visitor::visit_binary_expression(new binary_expression(*e
));
3483 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
3485 update_visitor::visit_unary_expression(new unary_expression(*e
));
3489 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
3491 update_visitor::visit_pre_crement(new pre_crement(*e
));
3495 deep_copy_visitor::visit_post_crement (post_crement
* e
)
3497 update_visitor::visit_post_crement(new post_crement(*e
));
3502 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3504 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
3508 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3510 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
3514 deep_copy_visitor::visit_array_in (array_in
* e
)
3516 update_visitor::visit_array_in(new array_in(*e
));
3520 deep_copy_visitor::visit_regex_query (regex_query
* e
)
3522 update_visitor::visit_regex_query(new regex_query(*e
));
3526 deep_copy_visitor::visit_comparison (comparison
* e
)
3528 update_visitor::visit_comparison(new comparison(*e
));
3532 deep_copy_visitor::visit_concatenation (concatenation
* e
)
3534 update_visitor::visit_concatenation(new concatenation(*e
));
3538 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
3540 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
3544 deep_copy_visitor::visit_assignment (assignment
* e
)
3546 update_visitor::visit_assignment(new assignment(*e
));
3550 deep_copy_visitor::visit_symbol (symbol
* e
)
3552 symbol
* n
= new symbol(*e
);
3553 n
->referent
= NULL
; // don't copy!
3554 update_visitor::visit_symbol(n
);
3558 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
3560 target_symbol
* n
= new target_symbol(*e
);
3561 update_visitor::visit_target_symbol(n
);
3565 deep_copy_visitor::visit_cast_op (cast_op
* e
)
3567 update_visitor::visit_cast_op(new cast_op(*e
));
3571 deep_copy_visitor::visit_autocast_op (autocast_op
* e
)
3573 update_visitor::visit_autocast_op(new autocast_op(*e
));
3577 deep_copy_visitor::visit_atvar_op (atvar_op
* e
)
3579 update_visitor::visit_atvar_op(new atvar_op(*e
));
3583 deep_copy_visitor::visit_defined_op (defined_op
* e
)
3585 update_visitor::visit_defined_op(new defined_op(*e
));
3589 deep_copy_visitor::visit_entry_op (entry_op
* e
)
3591 update_visitor::visit_entry_op(new entry_op(*e
));
3595 deep_copy_visitor::visit_perf_op (perf_op
* e
)
3597 update_visitor::visit_perf_op(new perf_op(*e
));
3601 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
3603 update_visitor::visit_arrayindex(new arrayindex(*e
));
3607 deep_copy_visitor::visit_functioncall (functioncall
* e
)
3609 functioncall
* n
= new functioncall(*e
);
3610 n
->referent
= NULL
; // don't copy!
3611 update_visitor::visit_functioncall(n
);
3615 deep_copy_visitor::visit_print_format (print_format
* e
)
3617 update_visitor::visit_print_format(new print_format(*e
));
3621 deep_copy_visitor::visit_stat_op (stat_op
* e
)
3623 update_visitor::visit_stat_op(new stat_op(*e
));
3627 deep_copy_visitor::visit_hist_op (hist_op
* e
)
3629 update_visitor::visit_hist_op(new hist_op(*e
));
3632 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */