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 operand
->indexes
[i
]->print (o
);
404 operand
->base
->print (o
);
407 void post_crement::print (ostream
& o
) const
409 o
<< '(' << *operand
<< ")" << op
;
413 void ternary_expression::print (ostream
& o
) const
415 o
<< "(" << *cond
<< ")?("
416 << *truevalue
<< "):("
417 << *falsevalue
<< ")";
421 void symbol::print (ostream
& o
) const
427 void target_symbol::component::print (ostream
& o
) const
431 case comp_pretty_print
:
432 case comp_struct_member
:
435 case comp_literal_array_index
:
436 o
<< '[' << num_index
<< ']';
438 case comp_expression_array_index
:
439 o
<< '[' << *expr_index
<< ']';
445 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
452 void target_symbol::print (ostream
& o
) const
457 for (unsigned i
= 0; i
< components
.size(); ++i
)
462 void atvar_op::print (ostream
& o
) const
466 o
<< name
<< "(\"" << target_name
<< "\")";
467 for (unsigned i
= 0; i
< components
.size(); ++i
)
472 void cast_op::print (ostream
& o
) const
476 o
<< name
<< '(' << *operand
;
477 o
<< ", " << lex_cast_qstring (type_name
);
478 if (module
.length() > 0)
479 o
<< ", " << lex_cast_qstring (module
);
481 for (unsigned i
= 0; i
< components
.size(); ++i
)
486 void autocast_op::print (ostream
& o
) const
490 o
<< '(' << *operand
<< ')';
491 for (unsigned i
= 0; i
< components
.size(); ++i
)
496 void defined_op::print (ostream
& o
) const
498 o
<< "@defined(" << *operand
<< ")";
502 void entry_op::print (ostream
& o
) const
504 o
<< "@entry(" << *operand
<< ")";
508 void perf_op::print (ostream
& o
) const
510 o
<< "@perf(" << *operand
<< ")";
514 void vardecl::print (ostream
& o
) const
520 o
<< "[" << maxsize
<< "]";
521 if (arity
> 0 || index_types
.size() > 0)
531 void vardecl::printsig (ostream
& o
) const
537 o
<< "[" << maxsize
<< "]";
539 if (index_types
.size() > 0)
542 for (unsigned i
=0; i
<index_types
.size(); i
++)
543 o
<< (i
>0 ? ", " : "") << index_types
[i
];
549 void functiondecl::print (ostream
& o
) const
551 o
<< "function " << name
<< " (";
552 for (unsigned i
=0; i
<formal_args
.size(); i
++)
553 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
559 void functiondecl::printsig (ostream
& o
) const
561 o
<< name
<< ":" << type
<< " (";
562 for (unsigned i
=0; i
<formal_args
.size(); i
++)
563 o
<< (i
>0 ? ", " : "")
566 << formal_args
[i
]->type
;
570 struct embedded_tags_visitor
: public traversing_visitor
572 map
<string
, bool> tags
;
574 embedded_tags_visitor(bool all_tags
)
576 tags
["/* guru */"] = false;
577 tags
["/* unprivileged */"] = false;
578 tags
["/* myproc-unprivileged */"] = false;
581 tags
["/* pure */"] = false;
582 tags
["/* unmangled */"] = false;
586 void find_tags_in_code (const string
& s
)
588 map
<string
, bool>::iterator tag
;
589 for (tag
= tags
.begin(); tag
!= tags
.end(); ++tag
)
591 tag
->second
= s
.find(tag
->first
) != string::npos
;
594 void visit_embeddedcode (embeddedcode
*s
)
596 find_tags_in_code(s
->code
);
599 void visit_embedded_expr (embedded_expr
*e
)
601 find_tags_in_code(e
->code
);
605 void functiondecl::printsigtags (ostream
& o
, bool all_tags
) const
609 // Visit the function's body to see if there's any embedded_code or
610 // embeddedexpr that have special tags (e.g. /* guru */)
611 embedded_tags_visitor
etv(all_tags
);
612 this->body
->visit(&etv
);
614 map
<string
, bool>::const_iterator tag
;
615 for (tag
= etv
.tags
.begin(); tag
!= etv
.tags
.end(); ++tag
)
617 o
<< " " << tag
->first
;
620 void arrayindex::print (ostream
& o
) const
624 for (unsigned i
=0; i
<indexes
.size(); i
++)
625 o
<< (i
>0 ? ", " : "") << *indexes
[i
];
630 void functioncall::print (ostream
& o
) const
632 o
<< function
<< "(";
633 for (unsigned i
=0; i
<args
.size(); i
++)
634 o
<< (i
>0 ? ", " : "") << *args
[i
];
640 print_format::create(const token
*t
, const char *n
)
642 bool stream
, format
, delim
, newline
, _char
;
643 if (n
== NULL
) n
= t
->content
.c_str();
647 format
= delim
= newline
= _char
= false;
649 if (strcmp(n
, "print_char") == 0)
659 if (0 != strncmp(n
, "print", 5))
676 if (*n
== 'l' && *(n
+1) == 'n')
687 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
, o
);
694 print_format::components_to_string(vector
<format_component
> const & components
)
698 for (vector
<format_component
>::const_iterator i
= components
.begin();
699 i
!= components
.end(); ++i
)
702 assert (i
->type
!= conv_unspecified
);
704 if (i
->type
== conv_literal
)
706 assert(!i
->literal_string
.empty());
707 for (string::const_iterator j
= i
->literal_string
.begin();
708 j
!= i
->literal_string
.end(); ++j
)
710 // See also: c_unparser::visit_literal_string and lex_cast_qstring
722 if (i
->test_flag (fmt_flag_zeropad
))
725 if (i
->test_flag (fmt_flag_plus
))
728 if (i
->test_flag (fmt_flag_space
))
731 if (i
->test_flag (fmt_flag_left
))
734 if (i
->test_flag (fmt_flag_special
))
737 if (i
->widthtype
== width_dynamic
)
739 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
742 if (i
->prectype
== prec_dynamic
)
744 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
745 oss
<< '.' << i
->precision
;
760 if (i
->test_flag (fmt_flag_large
))
765 else if (i
->base
== 8)
767 else if (i
->test_flag (fmt_flag_sign
))
785 case conv_memory_hex
:
797 vector
<print_format::format_component
>
798 print_format::string_to_components(string
const & str
)
800 format_component curr
;
801 vector
<format_component
> res
;
805 string::const_iterator i
= str
.begin();
807 while (i
!= str
.end())
811 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
812 curr
.type
= conv_literal
;
813 curr
.literal_string
+= *i
;
817 else if (i
+1 == str
.end() || *(i
+1) == '%')
820 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
821 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
822 curr
.type
= conv_literal
;
823 curr
.literal_string
+= '%';
830 if (curr
.type
!= conv_unspecified
)
832 // Flush any component we were previously accumulating
833 assert (curr
.type
== conv_literal
);
843 // Now we are definitely parsing a conversion.
844 // Begin by parsing flags (which are optional).
849 curr
.set_flag (fmt_flag_zeropad
);
854 curr
.set_flag (fmt_flag_plus
);
859 curr
.set_flag (fmt_flag_left
);
864 curr
.set_flag (fmt_flag_space
);
869 curr
.set_flag (fmt_flag_special
);
880 // Parse optional width
883 curr
.widthtype
= width_dynamic
;
886 else if (isdigit(*i
))
888 curr
.widthtype
= width_static
;
893 curr
.width
+= (*i
- '0');
896 while (i
!= str
.end() && isdigit(*i
));
902 // Parse optional precision
910 curr
.prectype
= prec_dynamic
;
913 else if (isdigit(*i
))
915 curr
.prectype
= prec_static
;
919 curr
.precision
*= 10;
920 curr
.precision
+= (*i
- '0');
923 while (i
!= str
.end() && isdigit(*i
));
930 // Parse the type modifier
941 // Parse the actual conversion specifier (bcsmdioupxXn)
944 // Valid conversion types
946 curr
.type
= conv_binary
;
950 curr
.type
= conv_char
;
954 curr
.type
= conv_string
;
958 curr
.type
= conv_memory
;
962 curr
.type
= conv_memory_hex
;
967 curr
.set_flag (fmt_flag_sign
);
969 curr
.type
= conv_number
;
974 curr
.type
= conv_number
;
979 curr
.set_flag (fmt_flag_large
);
981 curr
.type
= conv_number
;
986 // Since stap 1.3, %p == %#x.
987 curr
.set_flag (fmt_flag_special
);
988 curr
.type
= conv_pointer
;
990 // Oddness for stap < 1.3 is handled in translation
997 if (curr
.type
== conv_unspecified
)
998 throw PARSE_ERROR(_("invalid or missing conversion specifier"));
1001 res
.push_back(curr
);
1005 // If there's a remaining partly-composed conversion, fail.
1006 if (!curr
.is_empty())
1008 if (curr
.type
== conv_literal
)
1009 res
.push_back(curr
);
1011 throw PARSE_ERROR(_("trailing incomplete print format conversion"));
1018 void print_format::print (ostream
& o
) const
1020 o
<< print_format_type
<< "(";
1021 if (print_with_format
)
1022 o
<< lex_cast_qstring (raw_components
);
1023 if (print_with_delim
)
1024 o
<< lex_cast_qstring (delimiter
.literal_string
);
1027 for (vector
<expression
*>::const_iterator i
= args
.begin();
1028 i
!= args
.end(); ++i
)
1030 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
1037 void stat_op::print (ostream
& o
) const
1063 assert (0); // should not happen, as sc_none is only used in foreach sorts
1071 hist_op::print (ostream
& o
) const
1077 assert(params
.size() == 3);
1078 o
<< "hist_linear(";
1080 for (size_t i
= 0; i
< params
.size(); ++i
)
1082 o
<< ", " << params
[i
];
1088 assert(params
.size() == 0);
1096 ostream
& operator << (ostream
& o
, const statement
& k
)
1103 void embeddedcode::print (ostream
&o
) const
1110 void block::print (ostream
& o
) const
1113 for (unsigned i
=0; i
<statements
.size(); i
++)
1114 o
<< *statements
[i
] << endl
;
1118 block::block (statement
* car
, statement
* cdr
)
1120 statements
.push_back(car
);
1121 statements
.push_back(cdr
);
1122 this->tok
= car
->tok
;
1127 void try_block::print (ostream
& o
) const
1129 o
<< "try {" << endl
;
1130 if (try_block
) o
<< *try_block
<< endl
;
1132 if (catch_error_var
) o
<< "(" << *catch_error_var
<< ") ";
1134 if (catch_block
) o
<< *catch_block
<< endl
;
1139 void for_loop::print (ostream
& o
) const
1142 if (init
) init
->print (o
);
1146 if (incr
) incr
->print (o
);
1152 void foreach_loop::print (ostream
& o
) const
1161 for (unsigned i
=0; i
<indexes
.size(); i
++)
1163 if (i
> 0) o
<< ", ";
1164 indexes
[i
]->print (o
);
1165 if (sort_direction
!= 0 && sort_column
== i
+1)
1166 o
<< (sort_direction
> 0 ? "+" : "-");
1170 if (!array_slice
.empty())
1173 for (unsigned i
=0; i
<array_slice
.size(); i
++)
1175 if (i
> 0) o
<< ", ";
1176 array_slice
[i
]->print (o
);
1180 if (sort_direction
!= 0 && sort_column
== 0)
1184 case sc_count
: o
<< " @count"; break;
1185 case sc_average
: o
<< " @avg"; break;
1186 case sc_min
: o
<< " @min"; break;
1187 case sc_max
: o
<< " @max"; break;
1188 case sc_sum
: o
<< " @sum"; break;
1194 o
<< (sort_direction
> 0 ? "+" : "-");
1206 void null_statement::print (ostream
& o
) const
1212 void expr_statement::print (ostream
& o
) const
1218 void return_statement::print (ostream
& o
) const
1220 o
<< "return " << *value
;
1224 void delete_statement::print (ostream
& o
) const
1226 o
<< "delete " << *value
;
1229 void next_statement::print (ostream
& o
) const
1234 void break_statement::print (ostream
& o
) const
1239 void continue_statement::print (ostream
& o
) const
1244 void if_statement::print (ostream
& o
) const
1246 o
<< "if (" << *condition
<< ") "
1247 << *thenblock
<< endl
;
1249 o
<< "else " << *elseblock
<< endl
;
1253 void stapfile::print (ostream
& o
) const
1255 o
<< "# file " << name
<< endl
;
1257 for (unsigned i
=0; i
<embeds
.size(); i
++)
1258 embeds
[i
]->print (o
);
1260 for (unsigned i
=0; i
<globals
.size(); i
++)
1263 globals
[i
]->print (o
);
1267 for (unsigned i
=0; i
<aliases
.size(); i
++)
1269 aliases
[i
]->print (o
);
1273 for (unsigned i
=0; i
<probes
.size(); i
++)
1275 probes
[i
]->print (o
);
1279 for (unsigned j
= 0; j
< functions
.size(); j
++)
1281 functions
[j
]->print (o
);
1287 void probe::print (ostream
& o
) const
1295 void probe::printsig (ostream
& o
) const
1297 const probe_alias
*alias
= get_alias ();
1300 alias
->printsig (o
);
1304 for (unsigned i
=0; i
<locations
.size(); i
++)
1306 if (i
> 0) o
<< ",";
1307 locations
[i
]->print (o
);
1313 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
) const
1315 probes_list
.push_back(const_cast<probe
*>(this));
1317 base
->collect_derivation_chain(probes_list
);
1322 probe::collect_derivation_pp_chain (std::vector
<probe_point
*> &pp_list
) const
1324 pp_list
.push_back(const_cast<probe_point
*>(this->locations
[0]));
1326 base
->collect_derivation_pp_chain(pp_list
);
1331 void probe_point::print (ostream
& o
, bool print_extras
) const
1333 for (unsigned i
=0; i
<components
.size(); i
++)
1336 probe_point::component
* c
= components
[i
];
1339 // We might like to excise this weird 0 pointer, just in
1340 // case some recursion during the error-handling path, but
1341 // can't, because what we have here is a const *this.
1342 static int been_here
= 0;
1343 if (been_here
== 0) {
1345 throw SEMANTIC_ERROR (_("internal error: missing probe point component"));
1347 continue; // ... sad panda decides to skip the bad boy
1351 o
<< "(" << *c
->arg
<< ")";
1357 else if (optional
) // sufficient implies optional
1360 o
<< " if (" << *condition
<< ")";
1363 string
probe_point::str (bool print_extras
) const
1366 print(o
, print_extras
);
1371 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1372 probe (), alias_names (aliases
), epilogue_style(false)
1376 void probe_alias::printsig (ostream
& o
) const
1378 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1380 o
<< (i
>0 ? " = " : "");
1381 alias_names
[i
]->print (o
);
1383 o
<< (epilogue_style
? " += " : " = ");
1384 for (unsigned i
=0; i
<locations
.size(); i
++)
1386 if (i
> 0) o
<< ", ";
1387 locations
[i
]->print (o
);
1392 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1399 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1407 // ------------------------------------------------------------------------
1412 block::visit (visitor
* u
)
1414 u
->visit_block (this);
1419 try_block::visit (visitor
* u
)
1421 u
->visit_try_block (this);
1426 embeddedcode::visit (visitor
* u
)
1428 u
->visit_embeddedcode (this);
1433 for_loop::visit (visitor
* u
)
1435 u
->visit_for_loop (this);
1439 foreach_loop::visit (visitor
* u
)
1441 u
->visit_foreach_loop (this);
1445 null_statement::visit (visitor
* u
)
1447 u
->visit_null_statement (this);
1451 expr_statement::visit (visitor
* u
)
1453 u
->visit_expr_statement (this);
1457 return_statement::visit (visitor
* u
)
1459 u
->visit_return_statement (this);
1463 delete_statement::visit (visitor
* u
)
1465 u
->push_active_lvalue (this->value
);
1466 u
->visit_delete_statement (this);
1467 u
->pop_active_lvalue ();
1471 if_statement::visit (visitor
* u
)
1473 u
->visit_if_statement (this);
1477 next_statement::visit (visitor
* u
)
1479 u
->visit_next_statement (this);
1483 break_statement::visit (visitor
* u
)
1485 u
->visit_break_statement (this);
1489 continue_statement::visit (visitor
* u
)
1491 u
->visit_continue_statement (this);
1495 literal_string::visit(visitor
* u
)
1497 u
->visit_literal_string (this);
1501 literal_number::visit(visitor
* u
)
1503 u
->visit_literal_number (this);
1507 binary_expression::visit (visitor
* u
)
1509 u
->visit_binary_expression (this);
1513 embedded_expr::visit (visitor
* u
)
1515 u
->visit_embedded_expr (this);
1519 unary_expression::visit (visitor
* u
)
1521 u
->visit_unary_expression (this);
1525 pre_crement::visit (visitor
* u
)
1527 u
->push_active_lvalue (this->operand
);
1528 u
->visit_pre_crement (this);
1529 u
->pop_active_lvalue ();
1533 post_crement::visit (visitor
* u
)
1535 u
->push_active_lvalue (this->operand
);
1536 u
->visit_post_crement (this);
1537 u
->pop_active_lvalue ();
1541 logical_or_expr::visit (visitor
* u
)
1543 u
->visit_logical_or_expr (this);
1547 logical_and_expr::visit (visitor
* u
)
1549 u
->visit_logical_and_expr (this);
1553 array_in::visit (visitor
* u
)
1555 u
->visit_array_in (this);
1559 regex_query::visit (visitor
* u
)
1561 u
->visit_regex_query (this);
1565 comparison::visit (visitor
* u
)
1567 u
->visit_comparison (this);
1571 concatenation::visit (visitor
* u
)
1573 u
->visit_concatenation (this);
1577 ternary_expression::visit (visitor
* u
)
1579 u
->visit_ternary_expression (this);
1583 assignment::visit (visitor
* u
)
1585 u
->push_active_lvalue (this->left
);
1586 u
->visit_assignment (this);
1587 u
->pop_active_lvalue ();
1591 symbol::visit (visitor
* u
)
1593 u
->visit_symbol (this);
1597 target_symbol::visit (visitor
* u
)
1599 u
->visit_target_symbol(this);
1603 target_symbol::visit_components (visitor
* u
)
1605 for (unsigned i
= 0; i
< components
.size(); ++i
)
1606 if (components
[i
].type
== comp_expression_array_index
)
1607 components
[i
].expr_index
->visit (u
);
1611 target_symbol::visit_components (update_visitor
* u
)
1613 for (unsigned i
= 0; i
< components
.size(); ++i
)
1614 if (components
[i
].type
== comp_expression_array_index
)
1615 u
->replace (components
[i
].expr_index
);
1619 cast_op::visit (visitor
* u
)
1621 u
->visit_cast_op(this);
1626 autocast_op::visit (visitor
* u
)
1628 u
->visit_autocast_op(this);
1633 atvar_op::visit (visitor
* u
)
1635 u
->visit_atvar_op(this);
1640 defined_op::visit (visitor
* u
)
1642 u
->visit_defined_op(this);
1647 entry_op::visit (visitor
* u
)
1649 u
->visit_entry_op(this);
1654 perf_op::visit (visitor
* u
)
1656 u
->visit_perf_op(this);
1661 arrayindex::visit (visitor
* u
)
1663 u
->visit_arrayindex (this);
1667 functioncall::visit (visitor
* u
)
1669 u
->visit_functioncall (this);
1673 print_format::visit (visitor
*u
)
1675 u
->visit_print_format (this);
1679 stat_op::visit (visitor
*u
)
1681 u
->visit_stat_op (this);
1685 hist_op::visit (visitor
*u
)
1687 u
->visit_hist_op (this);
1692 indexable::is_symbol(symbol
*& sym_out
)
1699 indexable::is_hist_op(hist_op
*& hist_out
)
1706 symbol::is_symbol(symbol
*& sym_out
)
1713 hist_op::is_hist_op(hist_op
*& hist_out
)
1720 classify_indexable(indexable
* ix
,
1721 symbol
*& array_out
,
1722 hist_op
*& hist_out
)
1727 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1728 throw SEMANTIC_ERROR(_("Expecting symbol or histogram operator"), ix
->tok
);
1729 if (!(hist_out
|| array_out
))
1730 throw SEMANTIC_ERROR(_("Failed to classify indexable"), ix
->tok
);
1734 // ------------------------------------------------------------------------
1737 visitor::is_active_lvalue(expression
*e
)
1739 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1741 if (active_lvalues
[i
] == e
)
1748 visitor::push_active_lvalue(expression
*e
)
1750 active_lvalues
.push_back(e
);
1754 visitor::pop_active_lvalue()
1756 assert(!active_lvalues
.empty());
1757 active_lvalues
.pop_back();
1762 // ------------------------------------------------------------------------
1765 traversing_visitor::visit_block (block
* s
)
1767 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1768 s
->statements
[i
]->visit (this);
1772 traversing_visitor::visit_try_block (try_block
* s
)
1775 s
->try_block
->visit (this);
1776 if (s
->catch_error_var
)
1777 s
->catch_error_var
->visit (this);
1779 s
->catch_block
->visit (this);
1783 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1788 traversing_visitor::visit_null_statement (null_statement
*)
1793 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1795 s
->value
->visit (this);
1799 traversing_visitor::visit_if_statement (if_statement
* s
)
1801 s
->condition
->visit (this);
1802 s
->thenblock
->visit (this);
1804 s
->elseblock
->visit (this);
1808 traversing_visitor::visit_for_loop (for_loop
* s
)
1810 if (s
->init
) s
->init
->visit (this);
1811 s
->cond
->visit (this);
1812 if (s
->incr
) s
->incr
->visit (this);
1813 s
->block
->visit (this);
1817 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1819 s
->base
->visit(this);
1821 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1822 s
->indexes
[i
]->visit (this);
1825 s
->value
->visit (this);
1828 s
->limit
->visit (this);
1830 s
->block
->visit (this);
1834 traversing_visitor::visit_return_statement (return_statement
* s
)
1836 s
->value
->visit (this);
1840 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1842 s
->value
->visit (this);
1846 traversing_visitor::visit_next_statement (next_statement
*)
1851 traversing_visitor::visit_break_statement (break_statement
*)
1856 traversing_visitor::visit_continue_statement (continue_statement
*)
1861 traversing_visitor::visit_literal_string (literal_string
*)
1866 traversing_visitor::visit_literal_number (literal_number
*)
1871 traversing_visitor::visit_embedded_expr (embedded_expr
*)
1876 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1878 e
->left
->visit (this);
1879 e
->right
->visit (this);
1883 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1885 e
->operand
->visit (this);
1889 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1891 e
->operand
->visit (this);
1895 traversing_visitor::visit_post_crement (post_crement
* e
)
1897 e
->operand
->visit (this);
1902 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1904 e
->left
->visit (this);
1905 e
->right
->visit (this);
1909 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1911 e
->left
->visit (this);
1912 e
->right
->visit (this);
1916 traversing_visitor::visit_array_in (array_in
* e
)
1918 e
->operand
->visit (this);
1922 traversing_visitor::visit_regex_query (regex_query
* e
)
1924 e
->left
->visit (this);
1925 e
->right
->visit (this);
1929 traversing_visitor::visit_comparison (comparison
* e
)
1931 e
->left
->visit (this);
1932 e
->right
->visit (this);
1936 traversing_visitor::visit_concatenation (concatenation
* e
)
1938 e
->left
->visit (this);
1939 e
->right
->visit (this);
1943 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1945 e
->cond
->visit (this);
1946 e
->truevalue
->visit (this);
1947 e
->falsevalue
->visit (this);
1951 traversing_visitor::visit_assignment (assignment
* e
)
1953 e
->left
->visit (this);
1954 e
->right
->visit (this);
1958 traversing_visitor::visit_symbol (symbol
*)
1963 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1965 e
->visit_components (this);
1969 traversing_visitor::visit_cast_op (cast_op
* e
)
1971 e
->operand
->visit (this);
1972 e
->visit_components (this);
1976 traversing_visitor::visit_autocast_op (autocast_op
* e
)
1978 e
->operand
->visit (this);
1979 e
->visit_components (this);
1983 traversing_visitor::visit_atvar_op (atvar_op
* e
)
1985 e
->visit_components (this);
1989 traversing_visitor::visit_defined_op (defined_op
* e
)
1991 e
->operand
->visit (this);
1995 traversing_visitor::visit_entry_op (entry_op
* e
)
1997 e
->operand
->visit (this);
2002 traversing_visitor::visit_perf_op (perf_op
* e
)
2004 e
->operand
->visit (this);
2009 traversing_visitor::visit_arrayindex (arrayindex
* e
)
2011 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
2012 e
->indexes
[i
]->visit (this);
2014 e
->base
->visit(this);
2018 traversing_visitor::visit_functioncall (functioncall
* e
)
2020 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2021 e
->args
[i
]->visit (this);
2025 traversing_visitor::visit_print_format (print_format
* e
)
2027 for (unsigned i
=0; i
<e
->args
.size(); i
++)
2028 e
->args
[i
]->visit (this);
2030 e
->hist
->visit(this);
2034 traversing_visitor::visit_stat_op (stat_op
* e
)
2036 e
->stat
->visit (this);
2040 traversing_visitor::visit_hist_op (hist_op
* e
)
2042 e
->stat
->visit (this);
2047 expression_visitor::visit_literal_string (literal_string
* e
)
2049 traversing_visitor::visit_literal_string (e
);
2050 visit_expression (e
);
2054 expression_visitor::visit_literal_number (literal_number
* e
)
2056 traversing_visitor::visit_literal_number (e
);
2057 visit_expression (e
);
2061 expression_visitor::visit_embedded_expr (embedded_expr
* e
)
2063 traversing_visitor::visit_embedded_expr (e
);
2064 visit_expression (e
);
2068 expression_visitor::visit_binary_expression (binary_expression
* e
)
2070 traversing_visitor::visit_binary_expression (e
);
2071 visit_expression (e
);
2075 expression_visitor::visit_unary_expression (unary_expression
* e
)
2077 traversing_visitor::visit_unary_expression (e
);
2078 visit_expression (e
);
2082 expression_visitor::visit_pre_crement (pre_crement
* e
)
2084 traversing_visitor::visit_pre_crement (e
);
2085 visit_expression (e
);
2089 expression_visitor::visit_post_crement (post_crement
* e
)
2091 traversing_visitor::visit_post_crement (e
);
2092 visit_expression (e
);
2096 expression_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2098 traversing_visitor::visit_logical_or_expr (e
);
2099 visit_expression (e
);
2103 expression_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2105 traversing_visitor::visit_logical_and_expr (e
);
2106 visit_expression (e
);
2110 expression_visitor::visit_array_in (array_in
* e
)
2112 traversing_visitor::visit_array_in (e
);
2113 visit_expression (e
);
2117 expression_visitor::visit_regex_query (regex_query
* e
)
2119 traversing_visitor::visit_regex_query (e
);
2120 visit_expression (e
);
2124 expression_visitor::visit_comparison (comparison
* e
)
2126 traversing_visitor::visit_comparison (e
);
2127 visit_expression (e
);
2131 expression_visitor::visit_concatenation (concatenation
* e
)
2133 traversing_visitor::visit_concatenation (e
);
2134 visit_expression (e
);
2138 expression_visitor::visit_ternary_expression (ternary_expression
* e
)
2140 traversing_visitor::visit_ternary_expression (e
);
2141 visit_expression (e
);
2145 expression_visitor::visit_assignment (assignment
* e
)
2147 traversing_visitor::visit_assignment (e
);
2148 visit_expression (e
);
2152 expression_visitor::visit_symbol (symbol
* e
)
2154 traversing_visitor::visit_symbol (e
);
2155 visit_expression (e
);
2159 expression_visitor::visit_target_symbol (target_symbol
* e
)
2161 traversing_visitor::visit_target_symbol (e
);
2162 visit_expression (e
);
2166 expression_visitor::visit_arrayindex (arrayindex
* e
)
2168 traversing_visitor::visit_arrayindex (e
);
2169 visit_expression (e
);
2173 expression_visitor::visit_functioncall (functioncall
* e
)
2175 traversing_visitor::visit_functioncall (e
);
2176 visit_expression (e
);
2180 expression_visitor::visit_print_format (print_format
* e
)
2182 traversing_visitor::visit_print_format (e
);
2183 visit_expression (e
);
2187 expression_visitor::visit_stat_op (stat_op
* e
)
2189 traversing_visitor::visit_stat_op (e
);
2190 visit_expression (e
);
2194 expression_visitor::visit_hist_op (hist_op
* e
)
2196 traversing_visitor::visit_hist_op (e
);
2197 visit_expression (e
);
2201 expression_visitor::visit_cast_op (cast_op
* e
)
2203 traversing_visitor::visit_cast_op (e
);
2204 visit_expression (e
);
2208 expression_visitor::visit_autocast_op (autocast_op
* e
)
2210 traversing_visitor::visit_autocast_op (e
);
2211 visit_expression (e
);
2215 expression_visitor::visit_atvar_op (atvar_op
* e
)
2217 traversing_visitor::visit_atvar_op (e
);
2218 visit_expression (e
);
2222 expression_visitor::visit_defined_op (defined_op
* e
)
2224 traversing_visitor::visit_defined_op (e
);
2225 visit_expression (e
);
2229 expression_visitor::visit_entry_op (entry_op
* e
)
2231 traversing_visitor::visit_entry_op (e
);
2232 visit_expression (e
);
2236 expression_visitor::visit_perf_op (perf_op
* e
)
2238 traversing_visitor::visit_perf_op (e
);
2239 visit_expression (e
);
2244 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
2246 traversing_visitor::visit_functioncall (e
);
2248 // prevent infinite recursion
2249 if (nested
.find (e
->referent
) == nested
.end ())
2251 if (seen
.find(e
->referent
) == seen
.end())
2252 seen
.insert (e
->referent
);
2253 nested
.insert (e
->referent
);
2255 functiondecl
* last_current_function
= current_function
;
2256 current_function
= e
->referent
;
2257 e
->referent
->body
->visit (this);
2258 current_function
= last_current_function
;
2259 nested
.erase (e
->referent
);
2261 else { this->note_recursive_functioncall(e
); }
2265 functioncall_traversing_visitor::note_recursive_functioncall (functioncall
* e
)
2270 varuse_collecting_visitor::visit_try_block (try_block
*s
)
2273 s
->try_block
->visit (this);
2274 if (s
->catch_error_var
)
2275 written
.insert (s
->catch_error_var
->referent
);
2277 s
->catch_block
->visit (this);
2279 // NB: don't functioncall_traversing_visitor::visit_try_block (s);
2280 // since that would count s->catch_error_var as a read also.
2285 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
2287 assert (current_function
); // only they get embedded code
2289 // Don't allow embedded C functions in unprivileged mode unless
2290 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2291 // or we're in a usermode runtime.
2292 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2293 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2294 ! session
.runtime_usermode_p () &&
2295 s
->code
.find ("/* unprivileged */") == string::npos
&&
2296 s
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2297 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
2298 pr_name (session
.privilege
)),
2299 current_function
->tok
);
2301 // Don't allow /* guru */ functions unless -g is active.
2302 if (!session
.guru_mode
&& s
->code
.find ("/* guru */") != string::npos
)
2303 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
2304 current_function
->tok
);
2306 // PR14524: Support old-style THIS->local syntax on per-function basis.
2307 if (s
->code
.find ("/* unmangled */") != string::npos
)
2308 current_function
->mangle_oldstyle
= true;
2310 // We want to elide embedded-C functions when possible. For
2311 // example, each $target variable access is expanded to an
2312 // embedded-C function call. Yet, for safety reasons, we should
2313 // presume that embedded-C functions have intentional side-effects.
2315 // To tell these two types of functions apart, we apply a
2316 // Kludge(tm): we look for a magic string within the function body.
2317 // $target variables as rvalues will have this; lvalues won't.
2318 // Also, explicit side-effect-free tapset functions will have this.
2320 if (s
->code
.find ("/* pure */") != string::npos
)
2323 embedded_seen
= true;
2327 // About the same case as above.
2329 varuse_collecting_visitor::visit_embedded_expr (embedded_expr
*e
)
2331 // Don't allow embedded C expressions in unprivileged mode unless
2332 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
2333 // or we're in a usermode runtime.
2334 if (! pr_contains (session
.privilege
, pr_stapdev
) &&
2335 ! pr_contains (session
.privilege
, pr_stapsys
) &&
2336 ! session
.runtime_usermode_p () &&
2337 e
->code
.find ("/* unprivileged */") == string::npos
&&
2338 e
->code
.find ("/* myproc-unprivileged */") == string::npos
)
2339 throw SEMANTIC_ERROR (_F("embedded expression may not be used when --privilege=%s is specified",
2340 pr_name (session
.privilege
)),
2343 // Don't allow /* guru */ functions unless -g is active.
2344 if (!session
.guru_mode
&& e
->code
.find ("/* guru */") != string::npos
)
2345 throw SEMANTIC_ERROR (_("embedded expression may not be used unless -g is specified"),
2348 // We want to elide embedded-C functions when possible. For
2349 // example, each $target variable access is expanded to an
2350 // embedded-C function call. Yet, for safety reasons, we should
2351 // presume that embedded-C functions have intentional side-effects.
2353 // To tell these two types of functions apart, we apply a
2354 // Kludge(tm): we look for a magic string within the function body.
2355 // $target variables as rvalues will have this; lvalues won't.
2356 // Also, explicit side-effect-free tapset functions will have this.
2358 if (e
->code
.find ("/* pure */") != string::npos
)
2361 embedded_seen
= true;
2366 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
2368 // Still-unresolved target symbol assignments get treated as
2369 // generating side-effects like embedded-C, to prevent premature
2370 // elision and later error message suppression (PR5516). rvalue use
2371 // of unresolved target symbols is OTOH not considered a side-effect.
2373 if (is_active_lvalue (e
))
2374 embedded_seen
= true;
2376 functioncall_traversing_visitor::visit_target_symbol (e
);
2381 varuse_collecting_visitor::visit_atvar_op (atvar_op
*e
)
2383 // Similar to visit_target_symbol
2385 if (is_active_lvalue (e
))
2386 embedded_seen
= true;
2388 functioncall_traversing_visitor::visit_atvar_op (e
);
2393 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
2395 // As with target_symbols, unresolved cast assignments need to preserved
2396 // for later error handling.
2397 if (is_active_lvalue (e
))
2398 embedded_seen
= true;
2400 functioncall_traversing_visitor::visit_cast_op (e
);
2404 varuse_collecting_visitor::visit_autocast_op (autocast_op
*e
)
2406 // As with target_symbols, unresolved cast assignments need to preserved
2407 // for later error handling.
2408 if (is_active_lvalue (e
))
2409 embedded_seen
= true;
2411 functioncall_traversing_visitor::visit_autocast_op (e
);
2415 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
2418 functioncall_traversing_visitor::visit_defined_op (e
);
2422 varuse_collecting_visitor::visit_entry_op (entry_op
*e
)
2425 functioncall_traversing_visitor::visit_entry_op (e
);
2430 varuse_collecting_visitor::visit_perf_op (perf_op
*e
)
2432 functioncall_traversing_visitor::visit_perf_op (e
);
2437 varuse_collecting_visitor::visit_print_format (print_format
* e
)
2439 // NB: Instead of being top-level statements, "print" and "printf"
2440 // are implemented as statement-expressions containing a
2441 // print_format. They have side-effects, but not via the
2442 // embedded-code detection method above.
2444 // But sprint and sprintf don't have side-effects.
2446 bool last_lvalue_read
= current_lvalue_read
;
2447 current_lvalue_read
= true;
2448 if (e
->print_to_stream
)
2449 embedded_seen
= true; // a proxy for "has unknown side-effects"
2451 functioncall_traversing_visitor::visit_print_format (e
);
2452 current_lvalue_read
= last_lvalue_read
;
2457 varuse_collecting_visitor::visit_assignment (assignment
*e
)
2459 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
2461 expression
* last_lvalue
= current_lvalue
;
2462 bool last_lvalue_read
= current_lvalue_read
;
2463 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
2464 current_lvalue_read
= true;
2465 functioncall_traversing_visitor::visit_assignment (e
);
2466 current_lvalue
= last_lvalue
;
2467 current_lvalue_read
= last_lvalue_read
;
2469 else // read-modify-writes
2471 expression
* last_lrvalue
= current_lrvalue
;
2472 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
2473 functioncall_traversing_visitor::visit_assignment (e
);
2474 current_lrvalue
= last_lrvalue
;
2479 varuse_collecting_visitor::visit_symbol (symbol
*e
)
2481 if (e
->referent
== 0 && (e
->tok
->type
!= tok_operator
|| e
->name
!= "*"))
2482 throw SEMANTIC_ERROR (_("symbol without referent"), e
->tok
);
2484 // We could handle initialized globals by marking them as "written".
2485 // However, this current visitor may be called for a function or
2486 // probe body, from the point of view of which this global is
2487 // already initialized, so not written.
2489 if (e->referent->init)
2490 written.insert (e->referent);
2493 if (current_lvalue
== e
|| current_lrvalue
== e
)
2495 written
.insert (e
->referent
);
2497 if (current_lvalue
!= e
|| current_lrvalue
== e
)
2499 read
.insert (e
->referent
);
2502 if (current_lrvalue
== e
)
2504 if (current_lvalue_read
)
2505 used
.insert (e
->referent
);
2507 else if (current_lvalue
!= e
)
2508 used
.insert (e
->referent
);
2511 // NB: stat_op need not be overridden, since it will get to
2512 // visit_symbol and only as a possible rvalue.
2516 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
2518 // Hooking this callback is necessary because of the hacky
2519 // statistics representation. For the expression "i[4] = 5", the
2520 // incoming lvalue will point to this arrayindex. However, the
2521 // symbol corresponding to the "i[4]" is multiply inherited with
2522 // arrayindex. If the symbol base part of this object is not at
2523 // offset 0, then static_cast<symbol*>(e) may result in a different
2524 // address, and not match lvalue by number when we recurse that way.
2525 // So we explicitly override the incoming lvalue/lrvalue values to
2526 // point at the embedded objects' actual base addresses.
2528 expression
* last_lrvalue
= current_lrvalue
;
2529 expression
* last_lvalue
= current_lvalue
;
2531 symbol
*array
= NULL
;
2532 hist_op
*hist
= NULL
;
2533 classify_indexable(e
->base
, array
, hist
);
2534 expression
*value
= array
?: hist
->stat
;
2536 if (current_lrvalue
== e
) current_lrvalue
= value
;
2537 if (current_lvalue
== e
) current_lvalue
= value
;
2538 functioncall_traversing_visitor::visit_arrayindex (e
);
2540 current_lrvalue
= last_lrvalue
;
2541 current_lvalue
= last_lvalue
;
2546 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
2548 expression
* last_lrvalue
= current_lrvalue
;
2549 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2550 functioncall_traversing_visitor::visit_pre_crement (e
);
2551 current_lrvalue
= last_lrvalue
;
2555 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
2557 expression
* last_lrvalue
= current_lrvalue
;
2558 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
2559 functioncall_traversing_visitor::visit_post_crement (e
);
2560 current_lrvalue
= last_lrvalue
;
2564 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
2566 // NB: we duplicate so don't bother call
2567 // functioncall_traversing_visitor::visit_foreach_loop (s);
2569 s
->base
->visit(this);
2571 // If the collection is sorted, imply a "write" access to the
2572 // array in addition to the "read" one already noted above.
2573 if (s
->sort_direction
)
2575 symbol
*array
= NULL
;
2576 hist_op
*hist
= NULL
;
2577 classify_indexable (s
->base
, array
, hist
);
2578 if (array
) this->written
.insert (array
->referent
);
2579 // XXX: Can hist_op iterations be sorted?
2582 // NB: don't forget to visit the index expressions, which are lvalues.
2583 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
2585 expression
* last_lvalue
= current_lvalue
;
2586 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
2587 s
->indexes
[i
]->visit (this);
2588 current_lvalue
= last_lvalue
;
2591 // visit the additional specified array slice
2592 for (unsigned i
=0; i
<s
->array_slice
.size(); i
++)
2594 if (s
->array_slice
[i
]->tok
->type
!= tok_operator
|| s
->array_slice
[i
]->tok
->content
!= "*")
2596 expression
* last_lvalue
= current_lvalue
;
2597 current_lvalue
= s
->array_slice
[i
]; // leave a mark for ::visit_symbol
2598 s
->array_slice
[i
]->visit (this);
2599 current_lvalue
= last_lvalue
;
2603 // The value is an lvalue too
2606 expression
* last_lvalue
= current_lvalue
;
2607 current_lvalue
= s
->value
; // leave a mark for ::visit_symbol
2608 s
->value
->visit (this);
2609 current_lvalue
= last_lvalue
;
2613 s
->limit
->visit (this);
2615 s
->block
->visit (this);
2620 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2622 // Ideally, this would be treated like an assignment: a plain write
2623 // to the underlying value ("lvalue"). XXX: However, the
2624 // optimization pass is not smart enough to remove an unneeded
2625 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2626 // should protect the underlying value from optimizional mischief.
2627 expression
* last_lrvalue
= current_lrvalue
;
2628 bool last_lvalue_read
= current_lvalue_read
;
2629 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2630 current_lvalue_read
= true;
2631 functioncall_traversing_visitor::visit_delete_statement (s
);
2632 current_lrvalue
= last_lrvalue
;
2633 current_lvalue_read
= last_lvalue_read
;
2637 varuse_collecting_visitor::side_effect_free ()
2639 return (written
.empty() && !embedded_seen
);
2644 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2646 // A looser notion of side-effect-freeness with respect to a given
2647 // list of variables.
2649 // That's useful because the written list may consist of local
2650 // variables of called functions. But visible side-effects only
2651 // occur if the client's locals, or any globals are written-to.
2653 set
<vardecl
*> intersection
;
2654 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2655 set_intersection (written
.begin(), written
.end(),
2656 vars
.begin(), vars
.end(),
2659 return (intersection
.empty() && !embedded_seen
);
2665 // ------------------------------------------------------------------------
2668 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2669 throwing_visitor::throwing_visitor (): msg (_("invalid element")) {}
2673 throwing_visitor::throwone (const token
* t
)
2675 throw SEMANTIC_ERROR (msg
, t
);
2679 throwing_visitor::visit_block (block
* s
)
2685 throwing_visitor::visit_try_block (try_block
* s
)
2692 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2698 throwing_visitor::visit_null_statement (null_statement
* s
)
2704 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2710 throwing_visitor::visit_if_statement (if_statement
* s
)
2716 throwing_visitor::visit_for_loop (for_loop
* s
)
2722 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2728 throwing_visitor::visit_return_statement (return_statement
* s
)
2734 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2740 throwing_visitor::visit_next_statement (next_statement
* s
)
2746 throwing_visitor::visit_break_statement (break_statement
* s
)
2752 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2758 throwing_visitor::visit_literal_string (literal_string
* e
)
2764 throwing_visitor::visit_literal_number (literal_number
* e
)
2770 throwing_visitor::visit_embedded_expr (embedded_expr
* e
)
2776 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2782 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2788 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2794 throwing_visitor::visit_post_crement (post_crement
* e
)
2801 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2807 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2813 throwing_visitor::visit_array_in (array_in
* e
)
2819 throwing_visitor::visit_regex_query (regex_query
* e
)
2825 throwing_visitor::visit_comparison (comparison
* e
)
2831 throwing_visitor::visit_concatenation (concatenation
* e
)
2837 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2843 throwing_visitor::visit_assignment (assignment
* e
)
2849 throwing_visitor::visit_symbol (symbol
* e
)
2855 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2861 throwing_visitor::visit_atvar_op (atvar_op
* e
)
2867 throwing_visitor::visit_cast_op (cast_op
* e
)
2873 throwing_visitor::visit_autocast_op (autocast_op
* e
)
2879 throwing_visitor::visit_defined_op (defined_op
* e
)
2885 throwing_visitor::visit_entry_op (entry_op
* e
)
2892 throwing_visitor::visit_perf_op (perf_op
* e
)
2899 throwing_visitor::visit_arrayindex (arrayindex
* e
)
2905 throwing_visitor::visit_functioncall (functioncall
* e
)
2911 throwing_visitor::visit_print_format (print_format
* e
)
2917 throwing_visitor::visit_stat_op (stat_op
* e
)
2923 throwing_visitor::visit_hist_op (hist_op
* e
)
2929 // ------------------------------------------------------------------------
2933 update_visitor::visit_block (block
* s
)
2935 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
2936 replace (s
->statements
[i
]);
2941 update_visitor::visit_try_block (try_block
* s
)
2943 replace (s
->try_block
);
2944 replace (s
->catch_error_var
);
2945 replace (s
->catch_block
);
2950 update_visitor::visit_embeddedcode (embeddedcode
* s
)
2956 update_visitor::visit_null_statement (null_statement
* s
)
2962 update_visitor::visit_expr_statement (expr_statement
* s
)
2969 update_visitor::visit_if_statement (if_statement
* s
)
2971 replace (s
->condition
);
2972 replace (s
->thenblock
);
2973 replace (s
->elseblock
);
2978 update_visitor::visit_for_loop (for_loop
* s
)
2988 update_visitor::visit_foreach_loop (foreach_loop
* s
)
2990 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
2991 replace (s
->indexes
[i
]);
3000 update_visitor::visit_return_statement (return_statement
* s
)
3007 update_visitor::visit_delete_statement (delete_statement
* s
)
3014 update_visitor::visit_next_statement (next_statement
* s
)
3020 update_visitor::visit_break_statement (break_statement
* s
)
3026 update_visitor::visit_continue_statement (continue_statement
* s
)
3032 update_visitor::visit_literal_string (literal_string
* e
)
3038 update_visitor::visit_literal_number (literal_number
* e
)
3044 update_visitor::visit_embedded_expr (embedded_expr
* e
)
3050 update_visitor::visit_binary_expression (binary_expression
* e
)
3058 update_visitor::visit_unary_expression (unary_expression
* e
)
3060 replace (e
->operand
);
3065 update_visitor::visit_pre_crement (pre_crement
* e
)
3067 replace (e
->operand
);
3072 update_visitor::visit_post_crement (post_crement
* e
)
3074 replace (e
->operand
);
3080 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3088 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3096 update_visitor::visit_array_in (array_in
* e
)
3098 replace (e
->operand
);
3103 update_visitor::visit_regex_query (regex_query
* e
)
3106 replace (e
->right
); // XXX: do we *need* to replace literal in RHS?
3111 update_visitor::visit_comparison (comparison
* e
)
3119 update_visitor::visit_concatenation (concatenation
* e
)
3127 update_visitor::visit_ternary_expression (ternary_expression
* e
)
3130 replace (e
->truevalue
);
3131 replace (e
->falsevalue
);
3136 update_visitor::visit_assignment (assignment
* e
)
3144 update_visitor::visit_symbol (symbol
* e
)
3150 update_visitor::visit_target_symbol (target_symbol
* e
)
3152 e
->visit_components (this);
3157 update_visitor::visit_cast_op (cast_op
* e
)
3159 replace (e
->operand
);
3160 e
->visit_components (this);
3165 update_visitor::visit_autocast_op (autocast_op
* e
)
3167 replace (e
->operand
);
3168 e
->visit_components (this);
3173 update_visitor::visit_atvar_op (atvar_op
* e
)
3175 e
->visit_components (this);
3180 update_visitor::visit_defined_op (defined_op
* e
)
3182 replace (e
->operand
);
3187 update_visitor::visit_entry_op (entry_op
* e
)
3189 replace (e
->operand
);
3194 update_visitor::visit_perf_op (perf_op
* e
)
3196 replace (e
->operand
);
3201 update_visitor::visit_arrayindex (arrayindex
* e
)
3204 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
3205 replace (e
->indexes
[i
]);
3210 update_visitor::visit_functioncall (functioncall
* e
)
3212 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3213 replace (e
->args
[i
]);
3218 update_visitor::visit_print_format (print_format
* e
)
3220 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
3221 replace (e
->args
[i
]);
3227 update_visitor::visit_stat_op (stat_op
* e
)
3234 update_visitor::visit_hist_op (hist_op
* e
)
3241 // ------------------------------------------------------------------------
3245 deep_copy_visitor::visit_block (block
* s
)
3247 update_visitor::visit_block(new block(*s
));
3251 deep_copy_visitor::visit_try_block (try_block
* s
)
3253 update_visitor::visit_try_block(new try_block(*s
));
3257 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
3259 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
3263 deep_copy_visitor::visit_null_statement (null_statement
* s
)
3265 update_visitor::visit_null_statement(new null_statement(*s
));
3269 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
3271 update_visitor::visit_expr_statement(new expr_statement(*s
));
3275 deep_copy_visitor::visit_if_statement (if_statement
* s
)
3277 update_visitor::visit_if_statement(new if_statement(*s
));
3281 deep_copy_visitor::visit_for_loop (for_loop
* s
)
3283 update_visitor::visit_for_loop(new for_loop(*s
));
3287 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
3289 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
3293 deep_copy_visitor::visit_return_statement (return_statement
* s
)
3295 update_visitor::visit_return_statement(new return_statement(*s
));
3299 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
3301 update_visitor::visit_delete_statement(new delete_statement(*s
));
3305 deep_copy_visitor::visit_next_statement (next_statement
* s
)
3307 update_visitor::visit_next_statement(new next_statement(*s
));
3311 deep_copy_visitor::visit_break_statement (break_statement
* s
)
3313 update_visitor::visit_break_statement(new break_statement(*s
));
3317 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
3319 update_visitor::visit_continue_statement(new continue_statement(*s
));
3323 deep_copy_visitor::visit_literal_string (literal_string
* e
)
3325 update_visitor::visit_literal_string(new literal_string(*e
));
3329 deep_copy_visitor::visit_literal_number (literal_number
* e
)
3331 update_visitor::visit_literal_number(new literal_number(*e
));
3335 deep_copy_visitor::visit_embedded_expr (embedded_expr
* e
)
3337 update_visitor::visit_embedded_expr(new embedded_expr(*e
));
3341 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
3343 update_visitor::visit_binary_expression(new binary_expression(*e
));
3347 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
3349 update_visitor::visit_unary_expression(new unary_expression(*e
));
3353 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
3355 update_visitor::visit_pre_crement(new pre_crement(*e
));
3359 deep_copy_visitor::visit_post_crement (post_crement
* e
)
3361 update_visitor::visit_post_crement(new post_crement(*e
));
3366 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
3368 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
3372 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
3374 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
3378 deep_copy_visitor::visit_array_in (array_in
* e
)
3380 update_visitor::visit_array_in(new array_in(*e
));
3384 deep_copy_visitor::visit_regex_query (regex_query
* e
)
3386 update_visitor::visit_regex_query(new regex_query(*e
));
3390 deep_copy_visitor::visit_comparison (comparison
* e
)
3392 update_visitor::visit_comparison(new comparison(*e
));
3396 deep_copy_visitor::visit_concatenation (concatenation
* e
)
3398 update_visitor::visit_concatenation(new concatenation(*e
));
3402 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
3404 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
3408 deep_copy_visitor::visit_assignment (assignment
* e
)
3410 update_visitor::visit_assignment(new assignment(*e
));
3414 deep_copy_visitor::visit_symbol (symbol
* e
)
3416 symbol
* n
= new symbol(*e
);
3417 n
->referent
= NULL
; // don't copy!
3418 update_visitor::visit_symbol(n
);
3422 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
3424 target_symbol
* n
= new target_symbol(*e
);
3425 update_visitor::visit_target_symbol(n
);
3429 deep_copy_visitor::visit_cast_op (cast_op
* e
)
3431 update_visitor::visit_cast_op(new cast_op(*e
));
3435 deep_copy_visitor::visit_autocast_op (autocast_op
* e
)
3437 update_visitor::visit_autocast_op(new autocast_op(*e
));
3441 deep_copy_visitor::visit_atvar_op (atvar_op
* e
)
3443 update_visitor::visit_atvar_op(new atvar_op(*e
));
3447 deep_copy_visitor::visit_defined_op (defined_op
* e
)
3449 update_visitor::visit_defined_op(new defined_op(*e
));
3453 deep_copy_visitor::visit_entry_op (entry_op
* e
)
3455 update_visitor::visit_entry_op(new entry_op(*e
));
3459 deep_copy_visitor::visit_perf_op (perf_op
* e
)
3461 update_visitor::visit_perf_op(new perf_op(*e
));
3465 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
3467 update_visitor::visit_arrayindex(new arrayindex(*e
));
3471 deep_copy_visitor::visit_functioncall (functioncall
* e
)
3473 functioncall
* n
= new functioncall(*e
);
3474 n
->referent
= NULL
; // don't copy!
3475 update_visitor::visit_functioncall(n
);
3479 deep_copy_visitor::visit_print_format (print_format
* e
)
3481 update_visitor::visit_print_format(new print_format(*e
));
3485 deep_copy_visitor::visit_stat_op (stat_op
* e
)
3487 update_visitor::visit_stat_op(new stat_op(*e
));
3491 deep_copy_visitor::visit_hist_op (hist_op
* e
)
3493 update_visitor::visit_hist_op(new hist_op(*e
));
3496 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */