1 // parse tree functions
2 // Copyright (C) 2005-2014 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
28 visitable::~visitable ()
33 expression::expression ():
34 type (pe_unknown
), tok (0)
39 expression::~expression ()
44 statement::statement ():
50 statement::statement (const token
* tok
):
56 null_statement::null_statement (const token
* tok
):
62 statement::~statement ()
73 arrayindex::arrayindex ():
79 functioncall::functioncall ():
85 symboldecl::symboldecl ():
86 tok (0), systemtap_v_conditional (0),
92 symboldecl::~symboldecl ()
96 probe_point::probe_point (std::vector
<component
*> const & comps
):
97 components(comps
), optional (false), sufficient (false),
98 from_glob (false), well_formed (false), condition (0)
102 // NB: shallow-copy of compoonents & condition!
103 probe_point::probe_point (const probe_point
& pp
):
104 components(pp
.components
), optional (pp
.optional
), sufficient (pp
.sufficient
),
105 from_glob (pp
.from_glob
), well_formed (pp
.well_formed
),
106 condition (pp
.condition
)
111 probe_point::probe_point ():
112 optional (false), sufficient (false), from_glob (false),
113 well_formed (false), condition (0)
118 unsigned probe::last_probeidx
= 0;
121 body (0), base (0), tok (0), systemtap_v_conditional (0), privileged (false)
123 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
127 // Copy constructor, but with overriding probe-point. To be used when
128 // mapping script-level probe points to another one, early during pass
129 // 2. There should be no symbol resolution done yet.
130 probe::probe(probe
* p
, probe_point
* l
)
133 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
135 this->locations
.push_back(l
);
136 this->body
= deep_copy_visitor::deep_copy(p
->body
);
137 this->privileged
= p
->privileged
;
138 this->systemtap_v_conditional
= p
->systemtap_v_conditional
;
139 assert (p
->locals
.size() == 0);
140 assert (p
->unused_locals
.size() == 0);
144 probe_point::component::component ():
150 probe_point::component::component (std::string
const & f
, literal
* a
):
151 functor(f
), arg(a
), tok(0)
157 arity_tok(0), arity (-1), maxsize(0), init(NULL
), synthetic(false), wrap(false)
163 vardecl::set_arity (int a
, const token
* t
)
168 if (a
== 0 && maxsize
> 0)
169 throw SEMANTIC_ERROR (_("inconsistent arity"), tok
);
171 if (arity
!= a
&& arity
>= 0)
173 semantic_error
err (ERR_SRC
, _F("inconsistent arity (%s vs %d)",
174 lex_cast(arity
).c_str(), a
), t
?:tok
);
177 semantic_error
chain(ERR_SRC
, _F("arity %s first inferred here",
178 lex_cast(arity
).c_str()), arity_tok
);
179 err
.set_chain(chain
);
188 index_types
.resize (arity
);
189 for (int i
=0; i
<arity
; i
++)
190 index_types
[i
] = pe_unknown
;
195 vardecl::compatible_arity (int a
)
197 if (a
== 0 && maxsize
> 0)
199 if (arity
== -1 || a
== -1)
205 functiondecl::functiondecl ():
206 body (0), synthetic (false), mangle_oldstyle (false)
211 functiondecl::join (systemtap_session
& s
)
214 throw SEMANTIC_ERROR (_("internal error, joining a non-synthetic function"), tok
);
215 if (!s
.functions
.insert (make_pair (name
, this)).second
)
216 throw SEMANTIC_ERROR (_F("synthetic function '%s' conflicts with an existing function",
218 tok
->location
.file
->functions
.push_back (this);
222 literal_number::literal_number (int64_t v
, bool hex
)
230 literal_string::literal_string (const string
& v
)
238 operator << (ostream
& o
, const exp_type
& e
)
242 case pe_unknown
: o
<< "unknown"; break;
243 case pe_long
: o
<< "long"; break;
244 case pe_string
: o
<< "string"; break;
245 case pe_stats
: o
<< "stats"; break;
246 default: o
<< "???"; break;
253 target_symbol::assert_no_components(const std::string
& tapset
, bool pretty_ok
)
255 if (components
.empty())
258 switch (components
[0].type
)
260 case comp_literal_array_index
:
261 case comp_expression_array_index
:
262 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as array",
263 tapset
.c_str(), name
.c_str()), components
[0].tok
);
264 case comp_struct_member
:
265 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be used as a structure",
266 tapset
.c_str(), name
.c_str()), components
[0].tok
);
267 case comp_pretty_print
:
269 throw SEMANTIC_ERROR(_F("%s variable '%s' may not be pretty-printed",
270 tapset
.c_str(), name
.c_str()), components
[0].tok
);
273 throw SEMANTIC_ERROR (_F("invalid use of %s variable '%s'",
274 tapset
.c_str(), name
.c_str()), components
[0].tok
);
280 target_symbol::pretty_print_depth () const
282 if (! components
.empty ())
284 const component
& last
= components
.back ();
285 if (last
.type
== comp_pretty_print
)
286 return last
.member
.length ();
293 target_symbol::check_pretty_print (bool lvalue
) const
295 if (pretty_print_depth () == 0)
299 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), tok
);
305 void target_symbol::chain (const semantic_error
&er
)
307 semantic_error
* e
= new semantic_error(er
);
310 assert (e
->get_chain() == 0);
311 if (this->saved_conversion_error
)
313 e
->set_chain(*this->saved_conversion_error
);
314 delete this->saved_conversion_error
;
316 this->saved_conversion_error
= e
;
320 string
target_symbol::sym_name ()
322 return name
.substr(1);
326 string
atvar_op::sym_name ()
331 return target_name
.substr(0, target_name
.length() - cu_name
.length() - 1);
335 // ------------------------------------------------------------------------
336 // parse tree printing
338 ostream
& operator << (ostream
& o
, const expression
& k
)
345 void literal_string::print (ostream
& o
) const
348 for (unsigned i
=0; i
<value
.size(); i
++)
349 if (value
[i
] == '"') // or other escapeworthy characters?
357 void literal_number::print (ostream
& o
) const
360 o
<< hex
<< showbase
;
363 o
<< dec
<< noshowbase
;
367 void embedded_expr::print (ostream
& o
) const
369 o
<< "%{ " << code
<< " %}";
373 void binary_expression::print (ostream
& o
) const
375 o
<< "(" << *left
<< ") "
377 << " (" << *right
<< ")";
380 void regex_query::print (ostream
& o
) const
382 // NB: we need a custom printer, because the parser does not accept
383 // a parenthesized RHS.
384 o
<< "(" << *left
<< ") "
390 void unary_expression::print (ostream
& o
) const
392 o
<< op
<< '(' << *operand
<< ")";
395 void array_in::print (ostream
& o
) const
398 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
400 if (i
> 0) o
<< ", ";
401 if (operand
->indexes
[i
])
402 operand
->indexes
[i
]->print (o
);
407 operand
->base
->print (o
);
410 void post_crement::print (ostream
& o
) const
412 o
<< '(' << *operand
<< ")" << op
;
416 void ternary_expression::print (ostream
& o
) const
418 o
<< "(" << *cond
<< ")?("
419 << *truevalue
<< "):("
420 << *falsevalue
<< ")";
424 void symbol::print (ostream
& o
) const
430 void target_symbol::component::print (ostream
& o
) const
434 case comp_pretty_print
:
435 case comp_struct_member
:
438 case comp_literal_array_index
:
439 o
<< '[' << num_index
<< ']';
441 case comp_expression_array_index
:
442 o
<< '[' << *expr_index
<< ']';
448 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
455 void target_symbol::print (ostream
& o
) const
460 for (unsigned i
= 0; i
< components
.size(); ++i
)
465 void atvar_op::print (ostream
& o
) const
469 o
<< name
<< "(\"" << target_name
<< "\")";
470 for (unsigned i
= 0; i
< components
.size(); ++i
)
475 void cast_op::print (ostream
& o
) const
479 o
<< name
<< '(' << *operand
;
480 o
<< ", " << lex_cast_qstring (type_name
);
481 if (module
.length() > 0)
482 o
<< ", " << lex_cast_qstring (module
);
484 for (unsigned i
= 0; i
< components
.size(); ++i
)
489 void autocast_op::print (ostream
& o
) const
493 o
<< '(' << *operand
<< ')';
494 for (unsigned i
= 0; i
< components
.size(); ++i
)
499 void defined_op::print (ostream
& o
) const
501 o
<< "@defined(" << *operand
<< ")";
505 void entry_op::print (ostream
& o
) const
507 o
<< "@entry(" << *operand
<< ")";
511 void perf_op::print (ostream
& o
) const
513 o
<< "@perf(" << *operand
<< ")";
517 void vardecl::print (ostream
& o
) const
523 o
<< "[" << maxsize
<< "]";
524 if (arity
> 0 || index_types
.size() > 0)
534 void vardecl::printsig (ostream
& o
) const
540 o
<< "[" << maxsize
<< "]";
542 if (index_types
.size() > 0)
545 for (unsigned i
=0; i
<index_types
.size(); i
++)
546 o
<< (i
>0 ? ", " : "") << index_types
[i
];
552 void functiondecl::print (ostream
& o
) const
554 o
<< "function " << name
<< " (";
555 for (unsigned i
=0; i
<formal_args
.size(); i
++)
556 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
562 void functiondecl::printsig (ostream
& o
) const
564 o
<< name
<< ":" << type
<< " (";
565 for (unsigned i
=0; i
<formal_args
.size(); i
++)
566 o
<< (i
>0 ? ", " : "")
569 << formal_args
[i
]->type
;
573 struct embedded_tags_visitor
: public traversing_visitor
575 map
<string
, bool> tags
;
577 embedded_tags_visitor(bool all_tags
)
579 tags
["/* guru */"] = false;
580 tags
["/* unprivileged */"] = false;
581 tags
["/* myproc-unprivileged */"] = false;
584 tags
["/* pure */"] = false;
585 tags
["/* unmangled */"] = false;
589 void find_tags_in_code (const string
& s
)
591 map
<string
, bool>::iterator tag
;
592 for (tag
= tags
.begin(); tag
!= tags
.end(); ++tag
)
594 tag
->second
= s
.find(tag
->first
) != string::npos
;
597 void visit_embeddedcode (embeddedcode
*s
)
599 find_tags_in_code(s
->code
);
602 void visit_embedded_expr (embedded_expr
*e
)
604 find_tags_in_code(e
->code
);
608 void functiondecl::printsigtags (ostream
& o
, bool all_tags
) const
612 // Visit the function's body to see if there's any embedded_code or
613 // embeddedexpr that have special tags (e.g. /* guru */)
614 embedded_tags_visitor
etv(all_tags
);
615 this->body
->visit(&etv
);
617 map
<string
, bool>::const_iterator tag
;
618 for (tag
= etv
.tags
.begin(); tag
!= etv
.tags
.end(); ++tag
)
620 o
<< " " << tag
->first
;
623 void arrayindex::print (ostream
& o
) const
627 for (unsigned i
=0; i
<indexes
.size(); i
++)
629 o
<< (i
>0 ? ", " : "");
639 void functioncall::print (ostream
& o
) const
641 o
<< function
<< "(";
642 for (unsigned i
=0; i
<args
.size(); i
++)
643 o
<< (i
>0 ? ", " : "") << *args
[i
];
649 print_format::create(const token
*t
, const char *n
)
651 bool stream
, format
, delim
, newline
, _char
;
652 if (n
== NULL
) n
= t
->content
.c_str();
656 format
= delim
= newline
= _char
= false;
658 if (strcmp(n
, "print_char") == 0)
668 if (0 != strncmp(n
, "print", 5))
685 if (*n
== 'l' && *(n
+1) == 'n')
696 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
, o
);
703 print_format::components_to_string(vector
<format_component
> const & components
)
707 for (vector
<format_component
>::const_iterator i
= components
.begin();
708 i
!= components
.end(); ++i
)
711 assert (i
->type
!= conv_unspecified
);
713 if (i
->type
== conv_literal
)
715 assert(!i
->literal_string
.empty());
716 for (string::const_iterator j
= i
->literal_string
.begin();
717 j
!= i
->literal_string
.end(); ++j
)
719 // See also: c_unparser::visit_literal_string and lex_cast_qstring
731 if (i
->test_flag (fmt_flag_zeropad
))
734 if (i
->test_flag (fmt_flag_plus
))
737 if (i
->test_flag (fmt_flag_space
))
740 if (i
->test_flag (fmt_flag_left
))
743 if (i
->test_flag (fmt_flag_special
))
746 if (i
->widthtype
== width_dynamic
)
748 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
751 if (i
->prectype
== prec_dynamic
)
753 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
754 oss
<< '.' << i
->precision
;
769 if (i
->test_flag (fmt_flag_large
))
774 else if (i
->base
== 8)
776 else if (i
->test_flag (fmt_flag_sign
))
794 case conv_memory_hex
:
806 vector
<print_format::format_component
>
807 print_format::string_to_components(string
const & str
)
809 format_component curr
;
810 vector
<format_component
> res
;
814 string::const_iterator i
= str
.begin();
816 while (i
!= str
.end())
820 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
821 curr
.type
= conv_literal
;
822 curr
.literal_string
+= *i
;
826 else if (i
+1 == str
.end() || *(i
+1) == '%')
829 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
830 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
831 curr
.type
= conv_literal
;
832 curr
.literal_string
+= '%';
839 if (curr
.type
!= conv_unspecified
)
841 // Flush any component we were previously accumulating
842 assert (curr
.type
== conv_literal
);
852 // Now we are definitely parsing a conversion.
853 // Begin by parsing flags (which are optional).
858 curr
.set_flag (fmt_flag_zeropad
);
863 curr
.set_flag (fmt_flag_plus
);
868 curr
.set_flag (fmt_flag_left
);
873 curr
.set_flag (fmt_flag_space
);
878 curr
.set_flag (fmt_flag_special
);
889 // Parse optional width
892 curr
.widthtype
= width_dynamic
;
895 else if (isdigit(*i
))
897 curr
.widthtype
= width_static
;
902 curr
.width
+= (*i
- '0');
905 while (i
!= str
.end() && isdigit(*i
));
911 // Parse optional precision
919 curr
.prectype
= prec_dynamic
;
922 else if (isdigit(*i
))
924 curr
.prectype
= prec_static
;
928 curr
.precision
*= 10;
929 curr
.precision
+= (*i
- '0');
932 while (i
!= str
.end() && isdigit(*i
));
939 // Parse the type modifier
950 // Parse the actual conversion specifier (bcsmdioupxXn)
953 // Valid conversion types
955 curr
.type
= conv_binary
;
959 curr
.type
= conv_char
;
963 curr
.type
= conv_string
;
967 curr
.type
= conv_memory
;
971 curr
.type
= conv_memory_hex
;
976 curr
.set_flag (fmt_flag_sign
);
978 curr
.type
= conv_number
;
983 curr
.type
= conv_number
;
988 curr
.set_flag (fmt_flag_large
);
990 curr
.type
= conv_number
;
995 // Since stap 1.3, %p == %#x.
996 curr
.set_flag (fmt_flag_special
);
997 curr
.type
= conv_pointer
;
999 // Oddness for stap < 1.3 is handled in translation
1006 if (curr
.type
== conv_unspecified
)
1007 throw PARSE_ERROR(_("invalid or missing conversion specifier"));
1010 res
.push_back(curr
);
1014 // If there's a remaining partly-composed conversion, fail.
1015 if (!curr
.is_empty())
1017 if (curr
.type
== conv_literal
)
1018 res
.push_back(curr
);
1020 throw PARSE_ERROR(_("trailing incomplete print format conversion"));
1027 void print_format::print (ostream
& o
) const
1029 o
<< print_format_type
<< "(";
1030 if (print_with_format
)
1031 o
<< lex_cast_qstring (raw_components
);
1032 if (print_with_delim
)
1033 o
<< lex_cast_qstring (delimiter
.literal_string
);
1036 for (vector
<expression
*>::const_iterator i
= args
.begin();
1037 i
!= args
.end(); ++i
)
1039 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
1046 void stat_op::print (ostream
& o
) const
1072 assert (0); // should not happen, as sc_none is only used in foreach sorts
1080 hist_op::print (ostream
& o
) const
1086 assert(params
.size() == 3);
1087 o
<< "hist_linear(";
1089 for (size_t i
= 0; i
< params
.size(); ++i
)
1091 o
<< ", " << params
[i
];
1097 assert(params
.size() == 0);
1105 ostream
& operator << (ostream
& o
, const statement
& k
)
1112 void embeddedcode::print (ostream
&o
) const
1119 void block::print (ostream
& o
) const
1122 for (unsigned i
=0; i
<statements
.size(); i
++)
1123 o
<< *statements
[i
] << endl
;
1127 block::block (statement
* car
, statement
* cdr
)
1129 statements
.push_back(car
);
1130 statements
.push_back(cdr
);
1131 this->tok
= car
->tok
;
1136 void try_block::print (ostream
& o
) const
1138 o
<< "try {" << endl
;
1139 if (try_block
) o
<< *try_block
<< endl
;
1141 if (catch_error_var
) o
<< "(" << *catch_error_var
<< ") ";
1143 if (catch_block
) o
<< *catch_block
<< endl
;
1148 void for_loop::print (ostream
& o
) const
1151 if (init
) init
->print (o
);
1155 if (incr
) incr
->print (o
);
1161 void foreach_loop::print (ostream
& o
) const
1170 for (unsigned i
=0; i
<indexes
.size(); i
++)
1172 if (i
> 0) o
<< ", ";
1173 indexes
[i
]->print (o
);
1174 if (sort_direction
!= 0 && sort_column
== i
+1)
1175 o
<< (sort_direction
> 0 ? "+" : "-");
1179 if (!array_slice
.empty())
1182 for (unsigned i
=0; i
<array_slice
.size(); i
++)
1184 if (i
> 0) o
<< ", ";
1186 array_slice
[i
]->print (o
);
1192 if (sort_direction
!= 0 && sort_column
== 0)
1196 case sc_count
: o
<< " @count"; break;
1197 case sc_average
: o
<< " @avg"; break;
1198 case sc_min
: o
<< " @min"; break;
1199 case sc_max
: o
<< " @max"; break;
1200 case sc_sum
: o
<< " @sum"; break;
1206 o
<< (sort_direction
> 0 ? "+" : "-");
1218 void null_statement::print (ostream
& o
) const
1224 void expr_statement::print (ostream
& o
) const
1230 void return_statement::print (ostream
& o
) const
1232 o
<< "return " << *value
;
1236 void delete_statement::print (ostream
& o
) const
1238 o
<< "delete " << *value
;
1241 void next_statement::print (ostream
& o
) const
1246 void break_statement::print (ostream
& o
) const
1251 void continue_statement::print (ostream
& o
) const
1256 void if_statement::print (ostream
& o
) const
1258 o
<< "if (" << *condition
<< ") "
1259 << *thenblock
<< endl
;
1261 o
<< "else " << *elseblock
<< endl
;
1265 void stapfile::print (ostream
& o
) const
1267 o
<< "# file " << name
<< endl
;
1269 for (unsigned i
=0; i
<embeds
.size(); i
++)
1270 embeds
[i
]->print (o
);
1272 for (unsigned i
=0; i
<globals
.size(); i
++)
1275 globals
[i
]->print (o
);
1279 for (unsigned i
=0; i
<aliases
.size(); i
++)
1281 aliases
[i
]->print (o
);
1285 for (unsigned i
=0; i
<probes
.size(); i
++)
1287 probes
[i
]->print (o
);
1291 for (unsigned j
= 0; j
< functions
.size(); j
++)
1293 functions
[j
]->print (o
);
1299 void probe::print (ostream
& o
) const
1307 void probe::printsig (ostream
& o
) const
1309 const probe_alias
*alias
= get_alias ();
1312 alias
->printsig (o
);
1316 for (unsigned i
=0; i
<locations
.size(); i
++)
1318 if (i
> 0) o
<< ",";
1319 locations
[i
]->print (o
);
1325 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
) const
1327 probes_list
.push_back(const_cast<probe
*>(this));
1329 base
->collect_derivation_chain(probes_list
);
1334 probe::collect_derivation_pp_chain (std::vector
<probe_point
*> &pp_list
) const
1336 pp_list
.push_back(const_cast<probe_point
*>(this->locations
[0]));
1338 base
->collect_derivation_pp_chain(pp_list
);
1343 void probe_point::print (ostream
& o
, bool print_extras
) const
1345 for (unsigned i
=0; i
<components
.size(); i
++)
1348 probe_point::component
* c
= components
[i
];
1351 // We might like to excise this weird 0 pointer, just in
1352 // case some recursion during the error-handling path, but
1353 // can't, because what we have here is a const *this.
1354 static int been_here
= 0;
1355 if (been_here
== 0) {
1357 throw SEMANTIC_ERROR (_("internal error: missing probe point component"));
1359 continue; // ... sad panda decides to skip the bad boy
1363 o
<< "(" << *c
->arg
<< ")";
1369 else if (optional
) // sufficient implies optional
1372 o
<< " if (" << *condition
<< ")";
1375 string
probe_point::str (bool print_extras
) const
1378 print(o
, print_extras
);
1383 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1384 probe (), alias_names (aliases
), epilogue_style(false)
1388 void probe_alias::printsig (ostream
& o
) const
1390 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1392 o
<< (i
>0 ? " = " : "");
1393 alias_names
[i
]->print (o
);
1395 o
<< (epilogue_style
? " += " : " = ");
1396 for (unsigned i
=0; i
<locations
.size(); i
++)
1398 if (i
> 0) o
<< ", ";
1399 locations
[i
]->print (o
);
1404 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1411 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1419 // ------------------------------------------------------------------------
1424 block::visit (visitor
* u
)
1426 u
->visit_block (this);
1431 try_block::visit (visitor
* u
)
1433 u
->visit_try_block (this);
1438 embeddedcode::visit (visitor
* u
)
1440 u
->visit_embeddedcode (this);
1445 for_loop::visit (visitor
* u
)
1447 u
->visit_for_loop (this);
1451 foreach_loop::visit (visitor
* u
)
1453 u
->visit_foreach_loop (this);
1457 null_statement::visit (visitor
* u
)
1459 u
->visit_null_statement (this);
1463 expr_statement::visit (visitor
* u
)
1465 u
->visit_expr_statement (this);
1469 return_statement::visit (visitor
* u
)
1471 u
->visit_return_statement (this);
1475 delete_statement::visit (visitor
* u
)
1477 u
->push_active_lvalue (this->value
);
1478 u
->visit_delete_statement (this);
1479 u
->pop_active_lvalue ();
1483 if_statement::visit (visitor
* u
)
1485 u
->visit_if_statement (this);
1489 next_statement::visit (visitor
* u
)
1491 u
->visit_next_statement (this);
1495 break_statement::visit (visitor
* u
)
1497 u
->visit_break_statement (this);
1501 continue_statement::visit (visitor
* u
)
1503 u
->visit_continue_statement (this);
1507 literal_string::visit(visitor
* u
)
1509 u
->visit_literal_string (this);
1513 literal_number::visit(visitor
* u
)
1515 u
->visit_literal_number (this);
1519 binary_expression::visit (visitor
* u
)
1521 u
->visit_binary_expression (this);
1525 embedded_expr::visit (visitor
* u
)
1527 u
->visit_embedded_expr (this);
1531 unary_expression::visit (visitor
* u
)
1533 u
->visit_unary_expression (this);
1537 pre_crement::visit (visitor
* u
)
1539 u
->push_active_lvalue (this->operand
);
1540 u
->visit_pre_crement (this);
1541 u
->pop_active_lvalue ();
1545 post_crement::visit (visitor
* u
)
1547 u
->push_active_lvalue (this->operand
);
1548 u
->visit_post_crement (this);
1549 u
->pop_active_lvalue ();
1553 logical_or_expr::visit (visitor
* u
)
1555 u
->visit_logical_or_expr (this);
1559 logical_and_expr::visit (visitor
* u
)
1561 u
->visit_logical_and_expr (this);
1565 array_in::visit (visitor
* u
)
1567 u
->visit_array_in (this);
1571 regex_query::visit (visitor
* u
)
1573 u
->visit_regex_query (this);
1577 comparison::visit (visitor
* u
)
1579 u
->visit_comparison (this);
1583 concatenation::visit (visitor
* u
)
1585 u
->visit_concatenation (this);
1589 ternary_expression::visit (visitor
* u
)
1591 u
->visit_ternary_expression (this);
1595 assignment::visit (visitor
* u
)
1597 u
->push_active_lvalue (this->left
);
1598 u
->visit_assignment (this);
1599 u
->pop_active_lvalue ();
1603 symbol::visit (visitor
* u
)
1605 u
->visit_symbol (this);
1609 target_symbol::visit (visitor
* u
)
1611 u
->visit_target_symbol(this);
1615 target_symbol::visit_components (visitor
* u
)
1617 for (unsigned i
= 0; i
< components
.size(); ++i
)
1618 if (components
[i
].type
== comp_expression_array_index
)
1619 components
[i
].expr_index
->visit (u
);
1623 target_symbol::visit_components (update_visitor
* u
)
1625 for (unsigned i
= 0; i
< components
.size(); ++i
)
1626 if (components
[i
].type
== comp_expression_array_index
)
1627 u
->replace (components
[i
].expr_index
);
1631 cast_op::visit (visitor
* u
)
1633 u
->visit_cast_op(this);
1638 autocast_op::visit (visitor
* u
)
1640 u
->visit_autocast_op(this);
1645 atvar_op::visit (visitor
* u
)
1647 u
->visit_atvar_op(this);
1652 defined_op::visit (visitor
* u
)
1654 u
->visit_defined_op(this);
1659 entry_op::visit (visitor
* u
)
1661 u
->visit_entry_op(this);
1666 perf_op::visit (visitor
* u
)
1668 u
->visit_perf_op(this);
1673 arrayindex::visit (visitor
* u
)
1675 u
->visit_arrayindex (this);
1679 functioncall::visit (visitor
* u
)
1681 u
->visit_functioncall (this);
1685 print_format::visit (visitor
*u
)
1687 u
->visit_print_format (this);
1691 stat_op::visit (visitor
*u
)
1693 u
->visit_stat_op (this);
1697 hist_op::visit (visitor
*u
)
1699 u
->visit_hist_op (this);
1704 indexable::is_symbol(symbol
*& sym_out
)
1711 indexable::is_hist_op(hist_op
*& hist_out
)
1718 symbol::is_symbol(symbol
*& sym_out
)
1725 hist_op::is_hist_op(hist_op
*& hist_out
)
1732 classify_indexable(indexable
* ix
,
1733 symbol
*& array_out
,
1734 hist_op
*& hist_out
)
1739 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1740 throw SEMANTIC_ERROR(_("Expecting symbol or histogram operator"), ix
->tok
);
1741 if (!(hist_out
|| array_out
))
1742 throw SEMANTIC_ERROR(_("Failed to classify indexable"), ix
->tok
);
1746 // ------------------------------------------------------------------------
1749 visitor::is_active_lvalue(expression
*e
)
1751 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1753 if (active_lvalues
[i
] == e
)
1760 visitor::push_active_lvalue(expression
*e
)
1762 active_lvalues
.push_back(e
);
1766 visitor::pop_active_lvalue()
1768 assert(!active_lvalues
.empty());
1769 active_lvalues
.pop_back();
1774 // ------------------------------------------------------------------------
1777 traversing_visitor::visit_block (block
* s
)
1779 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1780 s
->statements
[i
]->visit (this);
1784 traversing_visitor::visit_try_block (try_block
* s
)
1787 s
->try_block
->visit (this);
1788 if (s
->catch_error_var
)
1789 s
->catch_error_var
->visit (this);
1791 s
->catch_block
->visit (this);
1795 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1800 traversing_visitor::visit_null_statement (null_statement
*)
1805 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1807 s
->value
->visit (this);
1811 traversing_visitor::visit_if_statement (if_statement
* s
)
1813 s
->condition
->visit (this);
1814 s
->thenblock
->visit (this);
1816 s
->elseblock
->visit (this);
1820 traversing_visitor::visit_for_loop (for_loop
* s
)
1822 if (s
->init
) s
->init
->visit (this);
1823 s
->cond
->visit (this);
1824 if (s
->incr
) s
->incr
->visit (this);
1825 s
->block
->visit (this);
1829 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1831 s
->base
->visit(this);
1833 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1834 s
->indexes
[i
]->visit (this);
1837 s
->value
->visit (this);
1840 s
->limit
->visit (this);
1842 s
->block
->visit (this);
1846 traversing_visitor::visit_return_statement (return_statement
* s
)
1848 s
->value
->visit (this);
1852 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1854 s
->value
->visit (this);
1858 traversing_visitor::visit_next_statement (next_statement
*)
1863 traversing_visitor::visit_break_statement (break_statement
*)
1868 traversing_visitor::visit_continue_statement (continue_statement
*)
1873 traversing_visitor::visit_literal_string (literal_string
*)
1878 traversing_visitor::visit_literal_number (literal_number
*)
1883 traversing_visitor::visit_embedded_expr (embedded_expr
*)
1888 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1890 e
->left
->visit (this);
1891 e
->right
->visit (this);
1895 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1897 e
->operand
->visit (this);
1901 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1903 e
->operand
->visit (this);
1907 traversing_visitor::visit_post_crement (post_crement
* e
)
1909 e
->operand
->visit (this);
1914 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1916 e
->left
->visit (this);
1917 e
->right
->visit (this);
1921 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1923 e
->left
->visit (this);
1924 e
->right
->visit (this);
1928 traversing_visitor::visit_array_in (array_in
* e
)
1930 e
->operand
->visit (this);
1934 traversing_visitor::visit_regex_query (regex_query
* e
)
1936 e
->left
->visit (this);
1937 e
->right
->visit (this);
1941 traversing_visitor::visit_comparison (comparison
* e
)
1943 e
->left
->visit (this);
1944 e
->right
->visit (this);
1948 traversing_visitor::visit_concatenation (concatenation
* e
)
1950 e
->left
->visit (this);
1951 e
->right
->visit (this);
1955 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1957 e
->cond
->visit (this);
1958 e
->truevalue
->visit (this);
1959 e
->falsevalue
->visit (this);
1963 traversing_visitor::visit_assignment (assignment
* e
)
1965 e
->left
->visit (this);
1966 e
->right
->visit (this);
1970 traversing_visitor::visit_symbol (symbol
*)
1975 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1977 e
->visit_components (this);
1981 traversing_visitor::visit_cast_op (cast_op
* e
)
1983 e
->operand
->visit (this);
1984 e
->visit_components (this);
1988 traversing_visitor::visit_autocast_op (autocast_op
* e
)
1990 e
->operand
->visit (this);
1991 e
->visit_components (this);
1995 traversing_visitor::visit_atvar_op (atvar_op
* e
)
1997 e
->visit_components (this);
2001 traversing_visitor::visit_defined_op (defined_op
* e
)
2003 e
->operand
->visit (this);
2007 traversing_visitor::visit_entry_op (entry_op
* e
)
2009 e
->operand
->visit (this);
2014 traversing_visitor::visit_perf_op (perf_op
* e
)
2016 e
->operand
->visit (this);
2021 traversing_visitor::visit_arrayindex (arrayindex
* e
)
2023 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2025 e
->indexes
[i
]->visit (this);
2027 e
->base
->visit(this);
2031 traversing_visitor::visit_functioncall (functioncall
* e
)
2033 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2034 e
->args
[i
]->visit (this);
2038 traversing_visitor::visit_print_format (print_format
* e
)
2040 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2041 e
->args
[i
]->visit (this);
2043 e
->hist
->visit(this);
2047 traversing_visitor::visit_stat_op (stat_op
* e
)
2049 e
->stat
->visit (this);
2053 traversing_visitor::visit_hist_op (hist_op
* e
)
2055 e
->stat
->visit (this);
2060 expression_visitor::visit_literal_string (literal_string
* e
)
2062 traversing_visitor::visit_literal_string (e
);
2063 visit_expression (e
);
2067 expression_visitor::visit_literal_number (literal_number
* e
)
2069 traversing_visitor::visit_literal_number (e
);
2070 visit_expression (e
);
2074 expression_visitor::visit_embedded_expr (embedded_expr
* e
)
2076 traversing_visitor::visit_embedded_expr (e
);
2077 visit_expression (e
);
2081 expression_visitor::visit_binary_expression (binary_expression
* e
)
2083 traversing_visitor::visit_binary_expression (e
);
2084 visit_expression (e
);
2088 expression_visitor::visit_unary_expression (unary_expression
* e
)
2090 traversing_visitor::visit_unary_expression (e
);
2091 visit_expression (e
);
2095 expression_visitor::visit_pre_crement (pre_crement
* e
)
2097 traversing_visitor::visit_pre_crement (e
);
2098 visit_expression (e
);
2102 expression_visitor::visit_post_crement (post_crement
* e
)
2104 traversing_visitor::visit_post_crement (e
);
2105 visit_expression (e
);
2109 expression_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2111 traversing_visitor::visit_logical_or_expr (e
);
2112 visit_expression (e
);
2116 expression_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2118 traversing_visitor::visit_logical_and_expr (e
);
2119 visit_expression (e
);
2123 expression_visitor::visit_array_in (array_in
* e
)
2125 traversing_visitor::visit_array_in (e
);
2126 visit_expression (e
);
2130 expression_visitor::visit_regex_query (regex_query
* e
)
2132 traversing_visitor::visit_regex_query (e
);
2133 visit_expression (e
);
2137 expression_visitor::visit_comparison (comparison
* e
)
2139 traversing_visitor::visit_comparison (e
);
2140 visit_expression (e
);
2144 expression_visitor::visit_concatenation (concatenation
* e
)
2146 traversing_visitor::visit_concatenation (e
);
2147 visit_expression (e
);
2151 expression_visitor::visit_ternary_expression (ternary_expression
* e
)
2153 traversing_visitor::visit_ternary_expression (e
);
2154 visit_expression (e
);
2158 expression_visitor::visit_assignment (assignment
* e
)
2160 traversing_visitor::visit_assignment (e
);
2161 visit_expression (e
);
2165 expression_visitor::visit_symbol (symbol
* e
)
2167 traversing_visitor::visit_symbol (e
);
2168 visit_expression (e
);
2172 expression_visitor::visit_target_symbol (target_symbol
* e
)
2174 traversing_visitor::visit_target_symbol (e
);
2175 visit_expression (e
);
2179 expression_visitor::visit_arrayindex (arrayindex
* e
)
2181 traversing_visitor::visit_arrayindex (e
);
2182 visit_expression (e
);
2186 expression_visitor::visit_functioncall (functioncall
* e
)
2188 traversing_visitor::visit_functioncall (e
);
2189 visit_expression (e
);
2193 expression_visitor::visit_print_format (print_format
* e
)
2195 traversing_visitor::visit_print_format (e
);
2196 visit_expression (e
);
2200 expression_visitor::visit_stat_op (stat_op
* e
)
2202 traversing_visitor::visit_stat_op (e
);
2203 visit_expression (e
);
2207 expression_visitor::visit_hist_op (hist_op
* e
)
2209 traversing_visitor::visit_hist_op (e
);
2210 visit_expression (e
);
2214 expression_visitor::visit_cast_op (cast_op
* e
)
2216 traversing_visitor::visit_cast_op (e
);
2217 visit_expression (e
);
2221 expression_visitor::visit_autocast_op (autocast_op
* e
)
2223 traversing_visitor::visit_autocast_op (e
);
2224 visit_expression (e
);
2228 expression_visitor::visit_atvar_op (atvar_op
* e
)
2230 traversing_visitor::visit_atvar_op (e
);
2231 visit_expression (e
);
2235 expression_visitor::visit_defined_op (defined_op
* e
)
2237 traversing_visitor::visit_defined_op (e
);
2238 visit_expression (e
);
2242 expression_visitor::visit_entry_op (entry_op
* e
)
2244 traversing_visitor::visit_entry_op (e
);
2245 visit_expression (e
);
2249 expression_visitor::visit_perf_op (perf_op
* e
)
2251 traversing_visitor::visit_perf_op (e
);
2252 visit_expression (e
);
2257 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
2259 traversing_visitor::visit_functioncall (e
);
2261 // prevent infinite recursion
2262 if (nested
.find (e
->referent
) == nested
.end ())
2264 if (seen
.find(e
->referent
) == seen
.end())
2265 seen
.insert (e
->referent
);
2266 nested
.insert (e
->referent
);
2268 functiondecl
* last_current_function
= current_function
;
2269 current_function
= e
->referent
;
2270 e
->referent
->body
->visit (this);
2271 current_function
= last_current_function
;
2272 nested
.erase (e
->referent
);
2274 else { this->note_recursive_functioncall(e
); }
2278 functioncall_traversing_visitor::note_recursive_functioncall (functioncall
* e
)
2283 varuse_collecting_visitor::visit_try_block (try_block
*s
)
2286 s
->try_block
->visit (this);
2287 if (s
->catch_error_var
)
2288 written
.insert (s
->catch_error_var
->referent
);
2290 s
->catch_block
->visit (this);
2292 // NB: don't functioncall_traversing_visitor::visit_try_block (s);
2293 // since that would count s->catch_error_var as a read also.
2298 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
2300 assert (current_function
); // only they get embedded code
2302 // Don't allow embedded C functions in unprivileged mode unless
2303 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2304 // or we're in a usermode runtime.
2305 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2306 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2307 ! session
.runtime_usermode_p () &&
2308 s
->code
.find ("/* unprivileged */") == string::npos
&&
2309 s
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2310 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
2311 pr_name (session
.privilege
)),
2312 current_function
->tok
);
2314 // Don't allow /* guru */ functions unless -g is active.
2315 if (!session
.guru_mode
&& s
->code
.find ("/* guru */") != string::npos
)
2316 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
2317 current_function
->tok
);
2319 // PR14524: Support old-style THIS->local syntax on per-function basis.
2320 if (s
->code
.find ("/* unmangled */") != string::npos
)
2321 current_function
->mangle_oldstyle
= true;
2323 // We want to elide embedded-C functions when possible. For
2324 // example, each $target variable access is expanded to an
2325 // embedded-C function call. Yet, for safety reasons, we should
2326 // presume that embedded-C functions have intentional side-effects.
2328 // To tell these two types of functions apart, we apply a
2329 // Kludge(tm): we look for a magic string within the function body.
2330 // $target variables as rvalues will have this; lvalues won't.
2331 // Also, explicit side-effect-free tapset functions will have this.
2333 if (s
->code
.find ("/* pure */") != string::npos
)
2336 embedded_seen
= true;
2340 // About the same case as above.
2342 varuse_collecting_visitor::visit_embedded_expr (embedded_expr
*e
)
2344 // Don't allow embedded C expressions in unprivileged mode unless
2345 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2346 // or we're in a usermode runtime.
2347 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2348 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2349 ! session
.runtime_usermode_p () &&
2350 e
->code
.find ("/* unprivileged */") == string::npos
&&
2351 e
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2352 throw SEMANTIC_ERROR (_F("embedded expression may not be used when --privilege=%s is specified",
2353 pr_name (session
.privilege
)),
2356 // Don't allow /* guru */ functions unless -g is active.
2357 if (!session
.guru_mode
&& e
->code
.find ("/* guru */") != string::npos
)
2358 throw SEMANTIC_ERROR (_("embedded expression may not be used unless -g is specified"),
2361 // We want to elide embedded-C functions when possible. For
2362 // example, each $target variable access is expanded to an
2363 // embedded-C function call. Yet, for safety reasons, we should
2364 // presume that embedded-C functions have intentional side-effects.
2366 // To tell these two types of functions apart, we apply a
2367 // Kludge(tm): we look for a magic string within the function body.
2368 // $target variables as rvalues will have this; lvalues won't.
2369 // Also, explicit side-effect-free tapset functions will have this.
2371 if (e
->code
.find ("/* pure */") != string::npos
)
2374 embedded_seen
= true;
2379 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
2381 // Still-unresolved target symbol assignments get treated as
2382 // generating side-effects like embedded-C, to prevent premature
2383 // elision and later error message suppression (PR5516). rvalue use
2384 // of unresolved target symbols is OTOH not considered a side-effect.
2386 if (is_active_lvalue (e
))
2387 embedded_seen
= true;
2389 functioncall_traversing_visitor::visit_target_symbol (e
);
2394 varuse_collecting_visitor::visit_atvar_op (atvar_op
*e
)
2396 // Similar to visit_target_symbol
2398 if (is_active_lvalue (e
))
2399 embedded_seen
= true;
2401 functioncall_traversing_visitor::visit_atvar_op (e
);
2406 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
2408 // As with target_symbols, unresolved cast assignments need to preserved
2409 // for later error handling.
2410 if (is_active_lvalue (e
))
2411 embedded_seen
= true;
2413 functioncall_traversing_visitor::visit_cast_op (e
);
2417 varuse_collecting_visitor::visit_autocast_op (autocast_op
*e
)
2419 // As with target_symbols, unresolved cast assignments need to preserved
2420 // for later error handling.
2421 if (is_active_lvalue (e
))
2422 embedded_seen
= true;
2424 functioncall_traversing_visitor::visit_autocast_op (e
);
2428 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
2431 functioncall_traversing_visitor::visit_defined_op (e
);
2435 varuse_collecting_visitor::visit_entry_op (entry_op
*e
)
2438 functioncall_traversing_visitor::visit_entry_op (e
);
2443 varuse_collecting_visitor::visit_perf_op (perf_op
*e
)
2445 functioncall_traversing_visitor::visit_perf_op (e
);
2450 varuse_collecting_visitor::visit_print_format (print_format
* e
)
2452 // NB: Instead of being top-level statements, "print" and "printf"
2453 // are implemented as statement-expressions containing a
2454 // print_format. They have side-effects, but not via the
2455 // embedded-code detection method above.
2457 // But sprint and sprintf don't have side-effects.
2459 bool last_lvalue_read
= current_lvalue_read
;
2460 current_lvalue_read
= true;
2461 if (e
->print_to_stream
)
2462 embedded_seen
= true; // a proxy for "has unknown side-effects"
2464 functioncall_traversing_visitor::visit_print_format (e
);
2465 current_lvalue_read
= last_lvalue_read
;
2470 varuse_collecting_visitor::visit_assignment (assignment
*e
)
2472 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
2474 expression
* last_lvalue
= current_lvalue
;
2475 bool last_lvalue_read
= current_lvalue_read
;
2476 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
2477 current_lvalue_read
= true;
2478 functioncall_traversing_visitor::visit_assignment (e
);
2479 current_lvalue
= last_lvalue
;
2480 current_lvalue_read
= last_lvalue_read
;
2482 else // read-modify-writes
2484 expression
* last_lrvalue
= current_lrvalue
;
2485 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
2486 functioncall_traversing_visitor::visit_assignment (e
);
2487 current_lrvalue
= last_lrvalue
;
2492 varuse_collecting_visitor::visit_symbol (symbol
*e
)
2494 if (e
->referent
== 0)
2495 throw SEMANTIC_ERROR (_("symbol without referent"), e
->tok
);
2497 // We could handle initialized globals by marking them as "written".
2498 // However, this current visitor may be called for a function or
2499 // probe body, from the point of view of which this global is
2500 // already initialized, so not written.
2502 if (e->referent->init)
2503 written.insert (e->referent);
2506 if (current_lvalue
== e
|| current_lrvalue
== e
)
2508 written
.insert (e
->referent
);
2510 if (current_lvalue
!= e
|| current_lrvalue
== e
)
2512 read
.insert (e
->referent
);
2515 if (current_lrvalue
== e
)
2517 if (current_lvalue_read
)
2518 used
.insert (e
->referent
);
2520 else if (current_lvalue
!= e
)
2521 used
.insert (e
->referent
);
2524 // NB: stat_op need not be overridden, since it will get to
2525 // visit_symbol and only as a possible rvalue.
2529 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
2531 // Hooking this callback is necessary because of the hacky
2532 // statistics representation. For the expression "i[4] = 5", the
2533 // incoming lvalue will point to this arrayindex. However, the
2534 // symbol corresponding to the "i[4]" is multiply inherited with
2535 // arrayindex. If the symbol base part of this object is not at
2536 // offset 0, then static_cast<symbol*>(e) may result in a different
2537 // address, and not match lvalue by number when we recurse that way.
2538 // So we explicitly override the incoming lvalue/lrvalue values to
2539 // point at the embedded objects' actual base addresses.
2541 expression
* last_lrvalue
= current_lrvalue
;
2542 expression
* last_lvalue
= current_lvalue
;
2544 symbol
*array
= NULL
;
2545 hist_op
*hist
= NULL
;
2546 classify_indexable(e
->base
, array
, hist
);
2547 expression
*value
= array
?: hist
->stat
;
2549 if (current_lrvalue
== e
) current_lrvalue
= value
;
2550 if (current_lvalue
== e
) current_lvalue
= value
;
2551 functioncall_traversing_visitor::visit_arrayindex (e
);
2553 current_lrvalue
= last_lrvalue
;
2554 current_lvalue
= last_lvalue
;
2559 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
2561 expression
* last_lrvalue
= current_lrvalue
;
2562 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2563 functioncall_traversing_visitor::visit_pre_crement (e
);
2564 current_lrvalue
= last_lrvalue
;
2568 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
2570 expression
* last_lrvalue
= current_lrvalue
;
2571 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2572 functioncall_traversing_visitor::visit_post_crement (e
);
2573 current_lrvalue
= last_lrvalue
;
2577 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
2579 // NB: we duplicate so don't bother call
2580 // functioncall_traversing_visitor::visit_foreach_loop (s);
2582 s
->base
->visit(this);
2584 // If the collection is sorted, imply a "write" access to the
2585 // array in addition to the "read" one already noted above.
2586 if (s
->sort_direction
)
2588 symbol
*array
= NULL
;
2589 hist_op
*hist
= NULL
;
2590 classify_indexable (s
->base
, array
, hist
);
2591 if (array
) this->written
.insert (array
->referent
);
2592 // XXX: Can hist_op iterations be sorted?
2595 // NB: don't forget to visit the index expressions, which are lvalues.
2596 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
2598 expression
* last_lvalue
= current_lvalue
;
2599 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
2600 s
->indexes
[i
]->visit (this);
2601 current_lvalue
= last_lvalue
;
2604 // visit the additional specified array slice
2605 for (unsigned i
=0; i
<s
->array_slice
.size(); i
++)
2607 if (s
->array_slice
[i
])
2608 s
->array_slice
[i
]->visit (this);
2611 // The value is an lvalue too
2614 expression
* last_lvalue
= current_lvalue
;
2615 current_lvalue
= s
->value
; // leave a mark for ::visit_symbol
2616 s
->value
->visit (this);
2617 current_lvalue
= last_lvalue
;
2621 s
->limit
->visit (this);
2623 s
->block
->visit (this);
2628 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2630 // Ideally, this would be treated like an assignment: a plain write
2631 // to the underlying value ("lvalue"). XXX: However, the
2632 // optimization pass is not smart enough to remove an unneeded
2633 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2634 // should protect the underlying value from optimizional mischief.
2635 expression
* last_lrvalue
= current_lrvalue
;
2636 bool last_lvalue_read
= current_lvalue_read
;
2637 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2638 current_lvalue_read
= true;
2639 functioncall_traversing_visitor::visit_delete_statement (s
);
2640 current_lrvalue
= last_lrvalue
;
2641 current_lvalue_read
= last_lvalue_read
;
2645 varuse_collecting_visitor::side_effect_free ()
2647 return (written
.empty() && !embedded_seen
);
2652 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2654 // A looser notion of side-effect-freeness with respect to a given
2655 // list of variables.
2657 // That's useful because the written list may consist of local
2658 // variables of called functions. But visible side-effects only
2659 // occur if the client's locals, or any globals are written-to.
2661 set
<vardecl
*> intersection
;
2662 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2663 set_intersection (written
.begin(), written
.end(),
2664 vars
.begin(), vars
.end(),
2667 return (intersection
.empty() && !embedded_seen
);
2673 // ------------------------------------------------------------------------
2676 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2677 throwing_visitor::throwing_visitor (): msg (_("invalid element")) {}
2681 throwing_visitor::throwone (const token
* t
)
2683 throw SEMANTIC_ERROR (msg
, t
);
2687 throwing_visitor::visit_block (block
* s
)
2693 throwing_visitor::visit_try_block (try_block
* s
)
2700 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2706 throwing_visitor::visit_null_statement (null_statement
* s
)
2712 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2718 throwing_visitor::visit_if_statement (if_statement
* s
)
2724 throwing_visitor::visit_for_loop (for_loop
* s
)
2730 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2736 throwing_visitor::visit_return_statement (return_statement
* s
)
2742 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2748 throwing_visitor::visit_next_statement (next_statement
* s
)
2754 throwing_visitor::visit_break_statement (break_statement
* s
)
2760 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2766 throwing_visitor::visit_literal_string (literal_string
* e
)
2772 throwing_visitor::visit_literal_number (literal_number
* e
)
2778 throwing_visitor::visit_embedded_expr (embedded_expr
* e
)
2784 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2790 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2796 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2802 throwing_visitor::visit_post_crement (post_crement
* e
)
2809 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2815 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2821 throwing_visitor::visit_array_in (array_in
* e
)
2827 throwing_visitor::visit_regex_query (regex_query
* e
)
2833 throwing_visitor::visit_comparison (comparison
* e
)
2839 throwing_visitor::visit_concatenation (concatenation
* e
)
2845 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2851 throwing_visitor::visit_assignment (assignment
* e
)
2857 throwing_visitor::visit_symbol (symbol
* e
)
2863 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2869 throwing_visitor::visit_atvar_op (atvar_op
* e
)
2875 throwing_visitor::visit_cast_op (cast_op
* e
)
2881 throwing_visitor::visit_autocast_op (autocast_op
* e
)
2887 throwing_visitor::visit_defined_op (defined_op
* e
)
2893 throwing_visitor::visit_entry_op (entry_op
* e
)
2900 throwing_visitor::visit_perf_op (perf_op
* e
)
2907 throwing_visitor::visit_arrayindex (arrayindex
* e
)
2913 throwing_visitor::visit_functioncall (functioncall
* e
)
2919 throwing_visitor::visit_print_format (print_format
* e
)
2925 throwing_visitor::visit_stat_op (stat_op
* e
)
2931 throwing_visitor::visit_hist_op (hist_op
* e
)
2937 // ------------------------------------------------------------------------
2941 update_visitor::visit_block (block
* s
)
2943 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
2944 replace (s
->statements
[i
]);
2949 update_visitor::visit_try_block (try_block
* s
)
2951 replace (s
->try_block
);
2952 replace (s
->catch_error_var
);
2953 replace (s
->catch_block
);
2958 update_visitor::visit_embeddedcode (embeddedcode
* s
)
2964 update_visitor::visit_null_statement (null_statement
* s
)
2970 update_visitor::visit_expr_statement (expr_statement
* s
)
2977 update_visitor::visit_if_statement (if_statement
* s
)
2979 replace (s
->condition
);
2980 replace (s
->thenblock
);
2981 replace (s
->elseblock
);
2986 update_visitor::visit_for_loop (for_loop
* s
)
2996 update_visitor::visit_foreach_loop (foreach_loop
* s
)
2998 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
2999 replace (s
->indexes
[i
]);
3008 update_visitor::visit_return_statement (return_statement
* s
)
3015 update_visitor::visit_delete_statement (delete_statement
* s
)
3022 update_visitor::visit_next_statement (next_statement
* s
)
3028 update_visitor::visit_break_statement (break_statement
* s
)
3034 update_visitor::visit_continue_statement (continue_statement
* s
)
3040 update_visitor::visit_literal_string (literal_string
* e
)
3046 update_visitor::visit_literal_number (literal_number
* e
)
3052 update_visitor::visit_embedded_expr (embedded_expr
* e
)
3058 update_visitor::visit_binary_expression (binary_expression
* e
)
3066 update_visitor::visit_unary_expression (unary_expression
* e
)
3068 replace (e
->operand
);
3073 update_visitor::visit_pre_crement (pre_crement
* e
)
3075 replace (e
->operand
);
3080 update_visitor::visit_post_crement (post_crement
* e
)
3082 replace (e
->operand
);
3088 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3096 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3104 update_visitor::visit_array_in (array_in
* e
)
3106 replace (e
->operand
);
3111 update_visitor::visit_regex_query (regex_query
* e
)
3114 replace (e
->right
); // XXX: do we *need* to replace literal in RHS?
3119 update_visitor::visit_comparison (comparison
* e
)
3127 update_visitor::visit_concatenation (concatenation
* e
)
3135 update_visitor::visit_ternary_expression (ternary_expression
* e
)
3138 replace (e
->truevalue
);
3139 replace (e
->falsevalue
);
3144 update_visitor::visit_assignment (assignment
* e
)
3152 update_visitor::visit_symbol (symbol
* e
)
3158 update_visitor::visit_target_symbol (target_symbol
* e
)
3160 e
->visit_components (this);
3165 update_visitor::visit_cast_op (cast_op
* e
)
3167 replace (e
->operand
);
3168 e
->visit_components (this);
3173 update_visitor::visit_autocast_op (autocast_op
* e
)
3175 replace (e
->operand
);
3176 e
->visit_components (this);
3181 update_visitor::visit_atvar_op (atvar_op
* e
)
3183 e
->visit_components (this);
3188 update_visitor::visit_defined_op (defined_op
* e
)
3190 replace (e
->operand
);
3195 update_visitor::visit_entry_op (entry_op
* e
)
3197 replace (e
->operand
);
3202 update_visitor::visit_perf_op (perf_op
* e
)
3204 replace (e
->operand
);
3209 update_visitor::visit_arrayindex (arrayindex
* e
)
3212 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
3213 replace (e
->indexes
[i
]);
3218 update_visitor::visit_functioncall (functioncall
* e
)
3220 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3221 replace (e
->args
[i
]);
3226 update_visitor::visit_print_format (print_format
* e
)
3228 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3229 replace (e
->args
[i
]);
3235 update_visitor::visit_stat_op (stat_op
* e
)
3242 update_visitor::visit_hist_op (hist_op
* e
)
3249 // ------------------------------------------------------------------------
3253 deep_copy_visitor::visit_block (block
* s
)
3255 update_visitor::visit_block(new block(*s
));
3259 deep_copy_visitor::visit_try_block (try_block
* s
)
3261 update_visitor::visit_try_block(new try_block(*s
));
3265 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
3267 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
3271 deep_copy_visitor::visit_null_statement (null_statement
* s
)
3273 update_visitor::visit_null_statement(new null_statement(*s
));
3277 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
3279 update_visitor::visit_expr_statement(new expr_statement(*s
));
3283 deep_copy_visitor::visit_if_statement (if_statement
* s
)
3285 update_visitor::visit_if_statement(new if_statement(*s
));
3289 deep_copy_visitor::visit_for_loop (for_loop
* s
)
3291 update_visitor::visit_for_loop(new for_loop(*s
));
3295 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
3297 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
3301 deep_copy_visitor::visit_return_statement (return_statement
* s
)
3303 update_visitor::visit_return_statement(new return_statement(*s
));
3307 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
3309 update_visitor::visit_delete_statement(new delete_statement(*s
));
3313 deep_copy_visitor::visit_next_statement (next_statement
* s
)
3315 update_visitor::visit_next_statement(new next_statement(*s
));
3319 deep_copy_visitor::visit_break_statement (break_statement
* s
)
3321 update_visitor::visit_break_statement(new break_statement(*s
));
3325 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
3327 update_visitor::visit_continue_statement(new continue_statement(*s
));
3331 deep_copy_visitor::visit_literal_string (literal_string
* e
)
3333 update_visitor::visit_literal_string(new literal_string(*e
));
3337 deep_copy_visitor::visit_literal_number (literal_number
* e
)
3339 update_visitor::visit_literal_number(new literal_number(*e
));
3343 deep_copy_visitor::visit_embedded_expr (embedded_expr
* e
)
3345 update_visitor::visit_embedded_expr(new embedded_expr(*e
));
3349 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
3351 update_visitor::visit_binary_expression(new binary_expression(*e
));
3355 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
3357 update_visitor::visit_unary_expression(new unary_expression(*e
));
3361 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
3363 update_visitor::visit_pre_crement(new pre_crement(*e
));
3367 deep_copy_visitor::visit_post_crement (post_crement
* e
)
3369 update_visitor::visit_post_crement(new post_crement(*e
));
3374 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3376 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
3380 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3382 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
3386 deep_copy_visitor::visit_array_in (array_in
* e
)
3388 update_visitor::visit_array_in(new array_in(*e
));
3392 deep_copy_visitor::visit_regex_query (regex_query
* e
)
3394 update_visitor::visit_regex_query(new regex_query(*e
));
3398 deep_copy_visitor::visit_comparison (comparison
* e
)
3400 update_visitor::visit_comparison(new comparison(*e
));
3404 deep_copy_visitor::visit_concatenation (concatenation
* e
)
3406 update_visitor::visit_concatenation(new concatenation(*e
));
3410 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
3412 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
3416 deep_copy_visitor::visit_assignment (assignment
* e
)
3418 update_visitor::visit_assignment(new assignment(*e
));
3422 deep_copy_visitor::visit_symbol (symbol
* e
)
3424 symbol
* n
= new symbol(*e
);
3425 n
->referent
= NULL
; // don't copy!
3426 update_visitor::visit_symbol(n
);
3430 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
3432 target_symbol
* n
= new target_symbol(*e
);
3433 update_visitor::visit_target_symbol(n
);
3437 deep_copy_visitor::visit_cast_op (cast_op
* e
)
3439 update_visitor::visit_cast_op(new cast_op(*e
));
3443 deep_copy_visitor::visit_autocast_op (autocast_op
* e
)
3445 update_visitor::visit_autocast_op(new autocast_op(*e
));
3449 deep_copy_visitor::visit_atvar_op (atvar_op
* e
)
3451 update_visitor::visit_atvar_op(new atvar_op(*e
));
3455 deep_copy_visitor::visit_defined_op (defined_op
* e
)
3457 update_visitor::visit_defined_op(new defined_op(*e
));
3461 deep_copy_visitor::visit_entry_op (entry_op
* e
)
3463 update_visitor::visit_entry_op(new entry_op(*e
));
3467 deep_copy_visitor::visit_perf_op (perf_op
* e
)
3469 update_visitor::visit_perf_op(new perf_op(*e
));
3473 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
3475 update_visitor::visit_arrayindex(new arrayindex(*e
));
3479 deep_copy_visitor::visit_functioncall (functioncall
* e
)
3481 functioncall
* n
= new functioncall(*e
);
3482 n
->referent
= NULL
; // don't copy!
3483 update_visitor::visit_functioncall(n
);
3487 deep_copy_visitor::visit_print_format (print_format
* e
)
3489 update_visitor::visit_print_format(new print_format(*e
));
3493 deep_copy_visitor::visit_stat_op (stat_op
* e
)
3495 update_visitor::visit_stat_op(new stat_op(*e
));
3499 deep_copy_visitor::visit_hist_op (hist_op
* e
)
3501 update_visitor::visit_hist_op(new hist_op(*e
));
3504 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */