1 // parse tree functions
2 // Copyright (C) 2005-2009 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
27 expression::expression ():
28 type (pe_unknown
), tok (0)
33 expression::~expression ()
38 statement::statement ():
44 statement::~statement ()
55 arrayindex::arrayindex ():
61 functioncall::functioncall ():
67 symboldecl::symboldecl ():
74 symboldecl::~symboldecl ()
78 probe_point::probe_point (std::vector
<component
*> const & comps
,
80 components(comps
), tok(t
), optional (false), sufficient (false),
85 // NB: shallow-copy of compoonents & condition!
86 probe_point::probe_point (const probe_point
& pp
):
87 components(pp
.components
), tok(pp
.tok
), optional (pp
.optional
), sufficient (pp
.sufficient
),
88 condition (pp
.condition
)
93 probe_point::probe_point ():
94 tok (0), optional (false), sufficient (false), condition (0)
99 unsigned probe::last_probeidx
= 0;
103 this->name
= string ("probe_") + lex_cast
<string
>(last_probeidx
++);
107 probe_point::component::component ():
113 probe_point::component::component (std::string
const & f
, literal
* a
):
120 arity (-1), maxsize(0), init(NULL
)
126 vardecl::set_arity (int a
)
131 if ((arity
!= a
&& arity
>= 0) || (a
== 0 && maxsize
> 0))
132 throw semantic_error ("inconsistent arity", tok
);
137 index_types
.resize (arity
);
138 for (int i
=0; i
<arity
; i
++)
139 index_types
[i
] = pe_unknown
;
144 vardecl::compatible_arity (int a
)
146 if (a
== 0 && maxsize
> 0)
148 if (arity
== -1 || a
== -1)
154 functiondecl::functiondecl ():
160 literal_number::literal_number (int64_t v
)
167 literal_string::literal_string (const string
& v
)
175 operator << (ostream
& o
, const exp_type
& e
)
179 case pe_unknown
: o
<< "unknown"; break;
180 case pe_long
: o
<< "long"; break;
181 case pe_string
: o
<< "string"; break;
182 case pe_stats
: o
<< "stats"; break;
183 default: o
<< "???"; break;
190 target_symbol::assert_no_components(const std::string
& tapset
)
192 if (components
.empty())
195 switch (components
[0].type
)
197 case target_symbol::comp_literal_array_index
:
198 case target_symbol::comp_expression_array_index
:
199 throw semantic_error(tapset
+ " variable '" + base_name
+
200 "' may not be used as array",
202 case target_symbol::comp_struct_member
:
203 throw semantic_error(tapset
+ " variable '" + base_name
+
204 "' may not be used as a structure",
207 throw semantic_error ("invalid use of " + tapset
+
208 " variable '" + base_name
+ "'",
214 // ------------------------------------------------------------------------
215 // parse tree printing
217 ostream
& operator << (ostream
& o
, const expression
& k
)
224 void literal_string::print (ostream
& o
) const
227 for (unsigned i
=0; i
<value
.size(); i
++)
228 if (value
[i
] == '"') // or other escapeworthy characters?
236 void literal_number::print (ostream
& o
) const
242 void binary_expression::print (ostream
& o
) const
244 o
<< "(" << *left
<< ") "
246 << " (" << *right
<< ")";
250 void unary_expression::print (ostream
& o
) const
252 o
<< op
<< '(' << *operand
<< ")";
255 void array_in::print (ostream
& o
) const
258 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
260 if (i
> 0) o
<< ", ";
261 operand
->indexes
[i
]->print (o
);
264 operand
->base
->print_indexable (o
);
267 void post_crement::print (ostream
& o
) const
269 o
<< '(' << *operand
<< ")" << op
;
273 void ternary_expression::print (ostream
& o
) const
275 o
<< "(" << *cond
<< ")?("
276 << *truevalue
<< "):("
277 << *falsevalue
<< ")";
281 void symbol::print (ostream
& o
) const
287 void target_symbol::component::print (ostream
& o
) const
291 case comp_struct_member
:
294 case comp_literal_array_index
:
295 o
<< '[' << num_index
<< ']';
297 case comp_expression_array_index
:
298 o
<< '[' << *expr_index
<< ']';
304 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
311 void target_symbol::print (ostream
& o
) const
316 for (unsigned i
= 0; i
< components
.size(); ++i
)
321 void cast_op::print (ostream
& o
) const
325 o
<< base_name
<< '(' << *operand
;
326 o
<< ", " << lex_cast_qstring (type
);
327 if (module
.length() > 0)
328 o
<< ", " << lex_cast_qstring (module
);
330 for (unsigned i
= 0; i
< components
.size(); ++i
)
335 void vardecl::print (ostream
& o
) const
339 o
<< "[" << maxsize
<< "]";
340 if (arity
> 0 || index_types
.size() > 0)
350 void vardecl::printsig (ostream
& o
) const
354 o
<< "[" << maxsize
<< "]";
356 if (index_types
.size() > 0)
359 for (unsigned i
=0; i
<index_types
.size(); i
++)
360 o
<< (i
>0 ? ", " : "") << index_types
[i
];
366 void functiondecl::print (ostream
& o
) const
368 o
<< "function " << name
<< " (";
369 for (unsigned i
=0; i
<formal_args
.size(); i
++)
370 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
376 void functiondecl::printsig (ostream
& o
) const
378 o
<< name
<< ":" << type
<< " (";
379 for (unsigned i
=0; i
<formal_args
.size(); i
++)
380 o
<< (i
>0 ? ", " : "")
383 << formal_args
[i
]->type
;
388 void arrayindex::print (ostream
& o
) const
390 base
->print_indexable (o
);
392 for (unsigned i
=0; i
<indexes
.size(); i
++)
393 o
<< (i
>0 ? ", " : "") << *indexes
[i
];
398 void functioncall::print (ostream
& o
) const
400 o
<< function
<< "(";
401 for (unsigned i
=0; i
<args
.size(); i
++)
402 o
<< (i
>0 ? ", " : "") << *args
[i
];
408 print_format::parse_print(const std::string
&name
,
409 bool &stream
, bool &format
, bool &delim
, bool &newline
, bool &_char
)
411 const char *n
= name
.c_str();
414 format
= delim
= newline
= _char
= false;
416 if (strcmp(n
, "print_char") == 0)
428 if (0 != strncmp(n
, "print", 5))
445 if (*n
== 'l' && *(n
+1) == 'n')
457 print_format::components_to_string(vector
<format_component
> const & components
)
461 for (vector
<format_component
>::const_iterator i
= components
.begin();
462 i
!= components
.end(); ++i
)
465 assert (i
->type
!= conv_unspecified
);
467 if (i
->type
== conv_literal
)
469 assert(!i
->literal_string
.empty());
470 for (string::const_iterator j
= i
->literal_string
.begin();
471 j
!= i
->literal_string
.end(); ++j
)
473 // See also: c_unparser::visit_literal_string and lex_cast_qstring
485 if (i
->flags
& static_cast<unsigned long>(fmt_flag_zeropad
))
488 if (i
->flags
& static_cast<unsigned long>(fmt_flag_plus
))
491 if (i
->flags
& static_cast<unsigned long>(fmt_flag_space
))
494 if (i
->flags
& static_cast<unsigned long>(fmt_flag_left
))
497 if (i
->flags
& static_cast<unsigned long>(fmt_flag_special
))
500 if (i
->widthtype
== width_dynamic
)
502 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
505 if (i
->prectype
== prec_dynamic
)
507 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
508 oss
<< '.' << i
->precision
;
520 case conv_signed_decimal
:
524 case conv_unsigned_decimal
:
528 case conv_unsigned_octal
:
532 case conv_unsigned_ptr
:
536 case conv_unsigned_uppercase_hex
:
540 case conv_unsigned_lowercase_hex
:
552 case conv_memory_hex
:
564 vector
<print_format::format_component
>
565 print_format::string_to_components(string
const & str
)
567 format_component curr
;
568 vector
<format_component
> res
;
572 string::const_iterator i
= str
.begin();
574 while (i
!= str
.end())
578 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
579 curr
.type
= conv_literal
;
580 curr
.literal_string
+= *i
;
584 else if (i
+1 == str
.end() || *(i
+1) == '%')
587 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
588 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
589 curr
.type
= conv_literal
;
590 curr
.literal_string
+= '%';
597 if (curr
.type
!= conv_unspecified
)
599 // Flush any component we were previously accumulating
600 assert (curr
.type
== conv_literal
);
610 // Now we are definitely parsing a conversion.
611 // Begin by parsing flags (which are optional).
616 curr
.flags
|= static_cast<unsigned long>(fmt_flag_zeropad
);
621 curr
.flags
|= static_cast<unsigned long>(fmt_flag_plus
);
626 curr
.flags
|= static_cast<unsigned long>(fmt_flag_left
);
631 curr
.flags
|= static_cast<unsigned long>(fmt_flag_space
);
636 curr
.flags
|= static_cast<unsigned long>(fmt_flag_special
);
647 // Parse optional width
650 curr
.widthtype
= width_dynamic
;
653 else if (isdigit(*i
))
655 curr
.widthtype
= width_static
;
660 curr
.width
+= (*i
- '0');
663 while (i
!= str
.end() && isdigit(*i
));
669 // Parse optional precision
677 curr
.prectype
= prec_dynamic
;
680 else if (isdigit(*i
))
682 curr
.prectype
= prec_static
;
686 curr
.precision
*= 10;
687 curr
.precision
+= (*i
- '0');
690 while (i
!= str
.end() && isdigit(*i
));
697 // Parse the actual conversion specifier (bcsmdioupxXn)
700 // Valid conversion types
702 curr
.type
= conv_binary
;
706 curr
.type
= conv_char
;
710 curr
.type
= conv_string
;
714 curr
.type
= conv_memory
;
718 curr
.type
= conv_memory_hex
;
723 curr
.type
= conv_signed_decimal
;
727 curr
.type
= conv_unsigned_octal
;
731 curr
.type
= conv_unsigned_decimal
;
735 curr
.type
= conv_unsigned_ptr
;
739 curr
.type
= conv_unsigned_uppercase_hex
;
743 curr
.type
= conv_unsigned_lowercase_hex
;
750 if (curr
.type
== conv_unspecified
)
751 throw parse_error("invalid or missing conversion specifier");
758 // If there's a remaining partly-composed conversion, fail.
759 if (!curr
.is_empty())
761 if (curr
.type
== conv_literal
)
764 throw parse_error("trailing incomplete print format conversion");
771 void print_format::print (ostream
& o
) const
773 o
<< tok
->content
<< "(";
774 if (print_with_format
)
775 o
<< lex_cast_qstring (raw_components
);
776 if (print_with_delim
)
777 o
<< lex_cast_qstring (delimiter
.literal_string
);
780 for (vector
<expression
*>::const_iterator i
= args
.begin();
781 i
!= args
.end(); ++i
)
783 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
790 void stat_op::print (ostream
& o
) const
820 hist_op::print (ostream
& o
) const
826 assert(params
.size() == 3);
829 for (size_t i
= 0; i
< params
.size(); ++i
)
831 o
<< ", " << params
[i
];
837 assert(params
.size() == 0);
845 ostream
& operator << (ostream
& o
, const statement
& k
)
852 void embeddedcode::print (ostream
&o
) const
859 void block::print (ostream
& o
) const
862 for (unsigned i
=0; i
<statements
.size(); i
++)
863 o
<< *statements
[i
] << endl
;
867 block::block (statement
* car
, statement
* cdr
)
869 statements
.push_back(car
);
870 statements
.push_back(cdr
);
871 this->tok
= car
->tok
;
876 void for_loop::print (ostream
& o
) const
879 if (init
) init
->print (o
);
883 if (incr
) incr
->print (o
);
889 void foreach_loop::print (ostream
& o
) const
892 for (unsigned i
=0; i
<indexes
.size(); i
++)
894 if (i
> 0) o
<< ", ";
895 indexes
[i
]->print (o
);
896 if (sort_direction
!= 0 && sort_column
== i
+1)
897 o
<< (sort_direction
> 0 ? "+" : "-");
900 base
->print_indexable (o
);
901 if (sort_direction
!= 0 && sort_column
== 0)
902 o
<< (sort_direction
> 0 ? "+" : "-");
913 void null_statement::print (ostream
& o
) const
919 void expr_statement::print (ostream
& o
) const
925 void return_statement::print (ostream
& o
) const
927 o
<< "return " << *value
;
931 void delete_statement::print (ostream
& o
) const
933 o
<< "delete " << *value
;
936 void next_statement::print (ostream
& o
) const
941 void break_statement::print (ostream
& o
) const
946 void continue_statement::print (ostream
& o
) const
951 void if_statement::print (ostream
& o
) const
953 o
<< "if (" << *condition
<< ") "
954 << *thenblock
<< endl
;
956 o
<< "else " << *elseblock
<< endl
;
960 void stapfile::print (ostream
& o
) const
962 o
<< "# file " << name
<< endl
;
964 for (unsigned i
=0; i
<embeds
.size(); i
++)
965 embeds
[i
]->print (o
);
967 for (unsigned i
=0; i
<globals
.size(); i
++)
970 globals
[i
]->print (o
);
974 for (unsigned i
=0; i
<aliases
.size(); i
++)
976 aliases
[i
]->print (o
);
980 for (unsigned i
=0; i
<probes
.size(); i
++)
982 probes
[i
]->print (o
);
986 for (unsigned j
= 0; j
< functions
.size(); j
++)
988 functions
[j
]->print (o
);
994 void probe::print (ostream
& o
) const
1002 void probe::printsig (ostream
& o
) const
1004 const probe_alias
*alias
= get_alias ();
1007 alias
->printsig (o
);
1011 for (unsigned i
=0; i
<locations
.size(); i
++)
1013 if (i
> 0) o
<< ",";
1014 locations
[i
]->print (o
);
1020 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
)
1022 probes_list
.push_back(this);
1026 void probe_point::print (ostream
& o
) const
1028 for (unsigned i
=0; i
<components
.size(); i
++)
1031 probe_point::component
* c
= components
[i
];
1034 o
<< "(" << *c
->arg
<< ")";
1038 else if (optional
) // sufficient implies optional
1041 o
<< " if (" << *condition
<< ")";
1044 string
probe_point::str ()
1047 for (unsigned i
=0; i
<components
.size(); i
++)
1050 probe_point::component
* c
= components
[i
];
1053 o
<< "(" << *c
->arg
<< ")";
1057 else if (optional
) // sufficient implies optional
1060 o
<< " if (" << *condition
<< ")";
1065 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1066 probe (), alias_names (aliases
)
1070 void probe_alias::printsig (ostream
& o
) const
1072 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1074 o
<< (i
>0 ? " = " : "");
1075 alias_names
[i
]->print (o
);
1078 for (unsigned i
=0; i
<locations
.size(); i
++)
1080 if (i
> 0) o
<< ", ";
1081 locations
[i
]->print (o
);
1086 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1093 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1101 // ------------------------------------------------------------------------
1106 block::visit (visitor
* u
)
1108 u
->visit_block (this);
1113 embeddedcode::visit (visitor
* u
)
1115 u
->visit_embeddedcode (this);
1120 for_loop::visit (visitor
* u
)
1122 u
->visit_for_loop (this);
1126 foreach_loop::visit (visitor
* u
)
1128 u
->visit_foreach_loop (this);
1132 null_statement::visit (visitor
* u
)
1134 u
->visit_null_statement (this);
1138 expr_statement::visit (visitor
* u
)
1140 u
->visit_expr_statement (this);
1144 return_statement::visit (visitor
* u
)
1146 u
->visit_return_statement (this);
1150 delete_statement::visit (visitor
* u
)
1152 u
->push_active_lvalue (this->value
);
1153 u
->visit_delete_statement (this);
1154 u
->pop_active_lvalue ();
1158 if_statement::visit (visitor
* u
)
1160 u
->visit_if_statement (this);
1164 next_statement::visit (visitor
* u
)
1166 u
->visit_next_statement (this);
1170 break_statement::visit (visitor
* u
)
1172 u
->visit_break_statement (this);
1176 continue_statement::visit (visitor
* u
)
1178 u
->visit_continue_statement (this);
1182 literal_string::visit(visitor
* u
)
1184 u
->visit_literal_string (this);
1188 literal_number::visit(visitor
* u
)
1190 u
->visit_literal_number (this);
1194 binary_expression::visit (visitor
* u
)
1196 u
->visit_binary_expression (this);
1200 unary_expression::visit (visitor
* u
)
1202 u
->visit_unary_expression (this);
1206 pre_crement::visit (visitor
* u
)
1208 u
->push_active_lvalue (this->operand
);
1209 u
->visit_pre_crement (this);
1210 u
->pop_active_lvalue ();
1214 post_crement::visit (visitor
* u
)
1216 u
->push_active_lvalue (this->operand
);
1217 u
->visit_post_crement (this);
1218 u
->pop_active_lvalue ();
1222 logical_or_expr::visit (visitor
* u
)
1224 u
->visit_logical_or_expr (this);
1228 logical_and_expr::visit (visitor
* u
)
1230 u
->visit_logical_and_expr (this);
1234 array_in::visit (visitor
* u
)
1236 u
->visit_array_in (this);
1240 comparison::visit (visitor
* u
)
1242 u
->visit_comparison (this);
1246 concatenation::visit (visitor
* u
)
1248 u
->visit_concatenation (this);
1252 ternary_expression::visit (visitor
* u
)
1254 u
->visit_ternary_expression (this);
1258 assignment::visit (visitor
* u
)
1260 u
->push_active_lvalue (this->left
);
1261 u
->visit_assignment (this);
1262 u
->pop_active_lvalue ();
1266 symbol::visit (visitor
* u
)
1268 u
->visit_symbol (this);
1272 target_symbol::visit (visitor
* u
)
1274 u
->visit_target_symbol(this);
1278 target_symbol::visit_components (visitor
* u
)
1280 for (unsigned i
= 0; i
< components
.size(); ++i
)
1281 if (components
[i
].type
== comp_expression_array_index
)
1282 components
[i
].expr_index
->visit (u
);
1286 target_symbol::visit_components (update_visitor
* u
)
1288 for (unsigned i
= 0; i
< components
.size(); ++i
)
1289 if (components
[i
].type
== comp_expression_array_index
)
1290 components
[i
].expr_index
= u
->require (components
[i
].expr_index
);
1294 cast_op::visit (visitor
* u
)
1296 u
->visit_cast_op(this);
1300 arrayindex::visit (visitor
* u
)
1302 u
->visit_arrayindex (this);
1306 functioncall::visit (visitor
* u
)
1308 u
->visit_functioncall (this);
1312 print_format::visit (visitor
*u
)
1314 u
->visit_print_format (this);
1318 stat_op::visit (visitor
*u
)
1320 u
->visit_stat_op (this);
1324 hist_op::visit (visitor
*u
)
1326 u
->visit_hist_op (this);
1330 indexable::print_indexable (std::ostream
& o
) const
1333 const hist_op
*hist
;
1334 classify_const_indexable(this, sym
, hist
);
1345 indexable::visit_indexable (visitor
* u
)
1349 classify_indexable(this, sym
, hist
);
1361 indexable::is_symbol(symbol
*& sym_out
)
1368 indexable::is_hist_op(hist_op
*& hist_out
)
1375 indexable::is_const_symbol(const symbol
*& sym_out
) const
1382 indexable::is_const_hist_op(const hist_op
*& hist_out
) const
1389 symbol::is_symbol(symbol
*& sym_out
)
1396 symbol::is_const_symbol(const symbol
*& sym_out
) const
1403 symbol::get_tok() const
1409 hist_op::is_hist_op(hist_op
*& hist_out
)
1416 hist_op::is_const_hist_op(const hist_op
*& hist_out
) const
1423 hist_op::get_tok() const
1429 classify_indexable(indexable
* ix
,
1430 symbol
*& array_out
,
1431 hist_op
*& hist_out
)
1435 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1436 throw semantic_error("Expecting symbol or histogram operator", ix
->get_tok());
1437 if (ix
&& !(hist_out
|| array_out
))
1438 throw semantic_error("Failed to classify indexable", ix
->get_tok());
1442 classify_const_indexable(const indexable
* ix
,
1443 const symbol
*& array_out
,
1444 const hist_op
*& hist_out
)
1448 if (!(ix
->is_const_symbol(array_out
) || ix
->is_const_hist_op(hist_out
)))
1449 throw semantic_error("Expecting symbol or histogram operator", ix
->get_tok());
1452 // ------------------------------------------------------------------------
1455 visitor::is_active_lvalue(expression
*e
)
1457 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1459 if (active_lvalues
[i
] == e
)
1466 visitor::push_active_lvalue(expression
*e
)
1468 active_lvalues
.push_back(e
);
1472 visitor::pop_active_lvalue()
1474 assert(!active_lvalues
.empty());
1475 active_lvalues
.pop_back();
1480 // ------------------------------------------------------------------------
1483 traversing_visitor::visit_block (block
* s
)
1485 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1486 s
->statements
[i
]->visit (this);
1490 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1495 traversing_visitor::visit_null_statement (null_statement
*)
1500 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1502 s
->value
->visit (this);
1506 traversing_visitor::visit_if_statement (if_statement
* s
)
1508 s
->condition
->visit (this);
1509 s
->thenblock
->visit (this);
1511 s
->elseblock
->visit (this);
1515 traversing_visitor::visit_for_loop (for_loop
* s
)
1517 if (s
->init
) s
->init
->visit (this);
1518 s
->cond
->visit (this);
1519 if (s
->incr
) s
->incr
->visit (this);
1520 s
->block
->visit (this);
1524 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1526 symbol
*array
= NULL
;
1527 hist_op
*hist
= NULL
;
1528 classify_indexable (s
->base
, array
, hist
);
1534 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1535 s
->indexes
[i
]->visit (this);
1538 s
->limit
->visit (this);
1540 s
->block
->visit (this);
1544 traversing_visitor::visit_return_statement (return_statement
* s
)
1546 s
->value
->visit (this);
1550 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1552 s
->value
->visit (this);
1556 traversing_visitor::visit_next_statement (next_statement
*)
1561 traversing_visitor::visit_break_statement (break_statement
*)
1566 traversing_visitor::visit_continue_statement (continue_statement
*)
1571 traversing_visitor::visit_literal_string (literal_string
*)
1576 traversing_visitor::visit_literal_number (literal_number
*)
1581 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1583 e
->left
->visit (this);
1584 e
->right
->visit (this);
1588 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1590 e
->operand
->visit (this);
1594 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1596 e
->operand
->visit (this);
1600 traversing_visitor::visit_post_crement (post_crement
* e
)
1602 e
->operand
->visit (this);
1607 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1609 e
->left
->visit (this);
1610 e
->right
->visit (this);
1614 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1616 e
->left
->visit (this);
1617 e
->right
->visit (this);
1621 traversing_visitor::visit_array_in (array_in
* e
)
1623 e
->operand
->visit (this);
1627 traversing_visitor::visit_comparison (comparison
* e
)
1629 e
->left
->visit (this);
1630 e
->right
->visit (this);
1634 traversing_visitor::visit_concatenation (concatenation
* e
)
1636 e
->left
->visit (this);
1637 e
->right
->visit (this);
1641 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1643 e
->cond
->visit (this);
1644 e
->truevalue
->visit (this);
1645 e
->falsevalue
->visit (this);
1649 traversing_visitor::visit_assignment (assignment
* e
)
1651 e
->left
->visit (this);
1652 e
->right
->visit (this);
1656 traversing_visitor::visit_symbol (symbol
*)
1661 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1663 e
->visit_components (this);
1667 traversing_visitor::visit_cast_op (cast_op
* e
)
1669 e
->operand
->visit (this);
1670 e
->visit_components (this);
1674 traversing_visitor::visit_arrayindex (arrayindex
* e
)
1676 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
1677 e
->indexes
[i
]->visit (this);
1679 symbol
*array
= NULL
;
1680 hist_op
*hist
= NULL
;
1681 classify_indexable(e
->base
, array
, hist
);
1683 return array
->visit(this);
1685 return hist
->visit(this);
1689 traversing_visitor::visit_functioncall (functioncall
* e
)
1691 for (unsigned i
=0; i
<e
->args
.size(); i
++)
1692 e
->args
[i
]->visit (this);
1696 traversing_visitor::visit_print_format (print_format
* e
)
1698 for (unsigned i
=0; i
<e
->args
.size(); i
++)
1699 e
->args
[i
]->visit (this);
1701 e
->hist
->visit(this);
1705 traversing_visitor::visit_stat_op (stat_op
* e
)
1707 e
->stat
->visit (this);
1711 traversing_visitor::visit_hist_op (hist_op
* e
)
1713 e
->stat
->visit (this);
1718 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
1720 traversing_visitor::visit_functioncall (e
);
1722 // prevent infinite recursion
1723 if (traversed
.find (e
->referent
) == traversed
.end ())
1725 traversed
.insert (e
->referent
);
1727 functiondecl
* last_current_function
= current_function
;
1728 current_function
= e
->referent
;
1729 e
->referent
->body
->visit (this);
1730 current_function
= last_current_function
;
1736 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
1738 // We want to elide embedded-C functions when possible. For
1739 // example, each $target variable access is expanded to an
1740 // embedded-C function call. Yet, for safety reasons, we should
1741 // presume that embedded-C functions have intentional side-effects.
1743 // To tell these two types of functions apart, we apply a
1744 // Kludge(tm): we look for a magic string within the function body.
1745 // $target variables as rvalues will have this; lvalues won't.
1746 // Also, explicit side-effect-free tapset functions will have this.
1748 assert (current_function
); // only they get embedded code
1749 if (s
->code
.find ("/* pure */") != string::npos
)
1752 embedded_seen
= true;
1756 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
1758 // Still-unresolved target symbol assignments get treated as
1759 // generating side-effects like embedded-C, to prevent premature
1760 // elision and later error message suppression (PR5516). rvalue use
1761 // of unresolved target symbols is OTOH not considered a side-effect.
1763 if (is_active_lvalue (e
))
1764 embedded_seen
= true;
1766 functioncall_traversing_visitor::visit_target_symbol (e
);
1770 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
1772 // As with target_symbols, unresolved cast assignments need to preserved
1773 // for later error handling.
1774 if (is_active_lvalue (e
))
1775 embedded_seen
= true;
1777 functioncall_traversing_visitor::visit_cast_op (e
);
1781 varuse_collecting_visitor::visit_print_format (print_format
* e
)
1783 // NB: Instead of being top-level statements, "print" and "printf"
1784 // are implemented as statement-expressions containing a
1785 // print_format. They have side-effects, but not via the
1786 // embedded-code detection method above.
1788 // But sprint and sprintf don't have side-effects.
1790 if (e
->print_to_stream
)
1791 embedded_seen
= true; // a proxy for "has unknown side-effects"
1793 functioncall_traversing_visitor::visit_print_format (e
);
1798 varuse_collecting_visitor::visit_assignment (assignment
*e
)
1800 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
1802 expression
* last_lvalue
= current_lvalue
;
1803 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
1804 functioncall_traversing_visitor::visit_assignment (e
);
1805 current_lvalue
= last_lvalue
;
1807 else // read-modify-writes
1809 expression
* last_lrvalue
= current_lrvalue
;
1810 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
1811 functioncall_traversing_visitor::visit_assignment (e
);
1812 current_lrvalue
= last_lrvalue
;
1817 varuse_collecting_visitor::visit_symbol (symbol
*e
)
1819 if (e
->referent
== 0)
1820 throw semantic_error ("symbol without referent", e
->tok
);
1822 // We could handle initialized globals by marking them as "written".
1823 // However, this current visitor may be called for a function or
1824 // probe body, from the point of view of which this global is
1825 // already initialized, so not written.
1827 if (e->referent->init)
1828 written.insert (e->referent);
1831 if (current_lvalue
== e
|| current_lrvalue
== e
)
1833 written
.insert (e
->referent
);
1834 // clog << "write ";
1836 if (current_lvalue
!= e
|| current_lrvalue
== e
)
1838 read
.insert (e
->referent
);
1841 // clog << *e->tok << endl;
1844 // NB: stat_op need not be overridden, since it will get to
1845 // visit_symbol and only as a possible rvalue.
1849 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
1851 // Hooking this callback is necessary because of the hacky
1852 // statistics representation. For the expression "i[4] = 5", the
1853 // incoming lvalue will point to this arrayindex. However, the
1854 // symbol corresponding to the "i[4]" is multiply inherited with
1855 // arrayindex. If the symbol base part of this object is not at
1856 // offset 0, then static_cast<symbol*>(e) may result in a different
1857 // address, and not match lvalue by number when we recurse that way.
1858 // So we explicitly override the incoming lvalue/lrvalue values to
1859 // point at the embedded objects' actual base addresses.
1861 expression
* last_lrvalue
= current_lrvalue
;
1862 expression
* last_lvalue
= current_lvalue
;
1864 symbol
*array
= NULL
;
1865 hist_op
*hist
= NULL
;
1866 classify_indexable(e
->base
, array
, hist
);
1870 if (current_lrvalue
== e
) current_lrvalue
= array
;
1871 if (current_lvalue
== e
) current_lvalue
= array
;
1872 functioncall_traversing_visitor::visit_arrayindex (e
);
1876 if (current_lrvalue
== e
) current_lrvalue
= hist
->stat
;
1877 if (current_lvalue
== e
) current_lvalue
= hist
->stat
;
1878 functioncall_traversing_visitor::visit_arrayindex (e
);
1881 current_lrvalue
= last_lrvalue
;
1882 current_lvalue
= last_lvalue
;
1887 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
1889 expression
* last_lrvalue
= current_lrvalue
;
1890 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
1891 functioncall_traversing_visitor::visit_pre_crement (e
);
1892 current_lrvalue
= last_lrvalue
;
1896 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
1898 expression
* last_lrvalue
= current_lrvalue
;
1899 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
1900 functioncall_traversing_visitor::visit_post_crement (e
);
1901 current_lrvalue
= last_lrvalue
;
1905 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
1907 // NB: we duplicate so don't bother call
1908 // functioncall_traversing_visitor::visit_foreach_loop (s);
1910 symbol
*array
= NULL
;
1911 hist_op
*hist
= NULL
;
1912 classify_indexable (s
->base
, array
, hist
);
1918 // If the collection is sorted, imply a "write" access to the
1919 // array in addition to the "read" one already noted above.
1920 if (s
->sort_direction
)
1922 symbol
*array
= NULL
;
1923 hist_op
*hist
= NULL
;
1924 classify_indexable (s
->base
, array
, hist
);
1925 if (array
) this->written
.insert (array
->referent
);
1926 // XXX: Can hist_op iterations be sorted?
1929 // NB: don't forget to visit the index expressions, which are lvalues.
1930 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1932 expression
* last_lvalue
= current_lvalue
;
1933 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
1934 s
->indexes
[i
]->visit (this);
1935 current_lvalue
= last_lvalue
;
1939 s
->limit
->visit (this);
1941 s
->block
->visit (this);
1946 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
1948 // Ideally, this would be treated like an assignment: a plain write
1949 // to the underlying value ("lvalue"). XXX: However, the
1950 // optimization pass is not smart enough to remove an unneeded
1951 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
1952 // should protect the underlying value from optimizional mischief.
1953 expression
* last_lrvalue
= current_lrvalue
;
1954 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
1955 functioncall_traversing_visitor::visit_delete_statement (s
);
1956 current_lrvalue
= last_lrvalue
;
1960 varuse_collecting_visitor::side_effect_free ()
1962 return (written
.empty() && !embedded_seen
);
1967 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
1969 // A looser notion of side-effect-freeness with respect to a given
1970 // list of variables.
1972 // That's useful because the written list may consist of local
1973 // variables of called functions. But visible side-effects only
1974 // occur if the client's locals, or any globals are written-to.
1976 set
<vardecl
*> intersection
;
1977 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
1978 set_intersection (written
.begin(), written
.end(),
1979 vars
.begin(), vars
.end(),
1982 return (intersection
.empty() && !embedded_seen
);
1988 // ------------------------------------------------------------------------
1991 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
1992 throwing_visitor::throwing_visitor (): msg ("invalid element") {}
1996 throwing_visitor::throwone (const token
* t
)
1998 throw semantic_error (msg
, t
);
2002 throwing_visitor::visit_block (block
* s
)
2008 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2014 throwing_visitor::visit_null_statement (null_statement
* s
)
2020 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2026 throwing_visitor::visit_if_statement (if_statement
* s
)
2032 throwing_visitor::visit_for_loop (for_loop
* s
)
2038 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2044 throwing_visitor::visit_return_statement (return_statement
* s
)
2050 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2056 throwing_visitor::visit_next_statement (next_statement
* s
)
2062 throwing_visitor::visit_break_statement (break_statement
* s
)
2068 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2074 throwing_visitor::visit_literal_string (literal_string
* e
)
2080 throwing_visitor::visit_literal_number (literal_number
* e
)
2086 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2092 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2098 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2104 throwing_visitor::visit_post_crement (post_crement
* e
)
2111 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2117 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2123 throwing_visitor::visit_array_in (array_in
* e
)
2129 throwing_visitor::visit_comparison (comparison
* e
)
2135 throwing_visitor::visit_concatenation (concatenation
* e
)
2141 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2147 throwing_visitor::visit_assignment (assignment
* e
)
2153 throwing_visitor::visit_symbol (symbol
* e
)
2159 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2165 throwing_visitor::visit_cast_op (cast_op
* e
)
2171 throwing_visitor::visit_arrayindex (arrayindex
* e
)
2177 throwing_visitor::visit_functioncall (functioncall
* e
)
2183 throwing_visitor::visit_print_format (print_format
* e
)
2189 throwing_visitor::visit_stat_op (stat_op
* e
)
2195 throwing_visitor::visit_hist_op (hist_op
* e
)
2201 // ------------------------------------------------------------------------
2205 update_visitor::visit_block (block
* s
)
2207 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
2208 s
->statements
[i
] = require (s
->statements
[i
]);
2213 update_visitor::visit_embeddedcode (embeddedcode
* s
)
2219 update_visitor::visit_null_statement (null_statement
* s
)
2225 update_visitor::visit_expr_statement (expr_statement
* s
)
2227 s
->value
= require (s
->value
);
2232 update_visitor::visit_if_statement (if_statement
* s
)
2234 s
->condition
= require (s
->condition
);
2235 s
->thenblock
= require (s
->thenblock
);
2236 s
->elseblock
= require (s
->elseblock
);
2241 update_visitor::visit_for_loop (for_loop
* s
)
2243 s
->init
= require (s
->init
);
2244 s
->cond
= require (s
->cond
);
2245 s
->incr
= require (s
->incr
);
2246 s
->block
= require (s
->block
);
2251 update_visitor::visit_foreach_loop (foreach_loop
* s
)
2253 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
2254 s
->indexes
[i
] = require (s
->indexes
[i
]);
2255 s
->base
= require (s
->base
);
2256 s
->limit
= require (s
->limit
);
2257 s
->block
= require (s
->block
);
2262 update_visitor::visit_return_statement (return_statement
* s
)
2264 s
->value
= require (s
->value
);
2269 update_visitor::visit_delete_statement (delete_statement
* s
)
2271 s
->value
= require (s
->value
);
2276 update_visitor::visit_next_statement (next_statement
* s
)
2282 update_visitor::visit_break_statement (break_statement
* s
)
2288 update_visitor::visit_continue_statement (continue_statement
* s
)
2294 update_visitor::visit_literal_string (literal_string
* e
)
2300 update_visitor::visit_literal_number (literal_number
* e
)
2306 update_visitor::visit_binary_expression (binary_expression
* e
)
2308 e
->left
= require (e
->left
);
2309 e
->right
= require (e
->right
);
2314 update_visitor::visit_unary_expression (unary_expression
* e
)
2316 e
->operand
= require (e
->operand
);
2321 update_visitor::visit_pre_crement (pre_crement
* e
)
2323 e
->operand
= require (e
->operand
);
2328 update_visitor::visit_post_crement (post_crement
* e
)
2330 e
->operand
= require (e
->operand
);
2336 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2338 e
->left
= require (e
->left
);
2339 e
->right
= require (e
->right
);
2344 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2346 e
->left
= require (e
->left
);
2347 e
->right
= require (e
->right
);
2352 update_visitor::visit_array_in (array_in
* e
)
2354 e
->operand
= require (e
->operand
);
2359 update_visitor::visit_comparison (comparison
* e
)
2361 e
->left
= require (e
->left
);
2362 e
->right
= require (e
->right
);
2367 update_visitor::visit_concatenation (concatenation
* e
)
2369 e
->left
= require (e
->left
);
2370 e
->right
= require (e
->right
);
2375 update_visitor::visit_ternary_expression (ternary_expression
* e
)
2377 e
->cond
= require (e
->cond
);
2378 e
->truevalue
= require (e
->truevalue
);
2379 e
->falsevalue
= require (e
->falsevalue
);
2384 update_visitor::visit_assignment (assignment
* e
)
2386 e
->left
= require (e
->left
);
2387 e
->right
= require (e
->right
);
2392 update_visitor::visit_symbol (symbol
* e
)
2398 update_visitor::visit_target_symbol (target_symbol
* e
)
2400 e
->visit_components (this);
2405 update_visitor::visit_cast_op (cast_op
* e
)
2407 e
->operand
= require (e
->operand
);
2408 e
->visit_components (this);
2413 update_visitor::visit_arrayindex (arrayindex
* e
)
2415 e
->base
= require (e
->base
);
2416 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
2417 e
->indexes
[i
] = require (e
->indexes
[i
]);
2422 update_visitor::visit_functioncall (functioncall
* e
)
2424 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
2425 e
->args
[i
] = require (e
->args
[i
]);
2430 update_visitor::visit_print_format (print_format
* e
)
2432 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
2433 e
->args
[i
] = require (e
->args
[i
]);
2434 e
->hist
= require (e
->hist
);
2439 update_visitor::visit_stat_op (stat_op
* e
)
2441 e
->stat
= require (e
->stat
);
2446 update_visitor::visit_hist_op (hist_op
* e
)
2448 e
->stat
= require (e
->stat
);
2452 template <> indexable
*
2453 update_visitor::require
<indexable
*> (indexable
* src
, bool clearok
)
2455 indexable
*dst
= NULL
;
2458 symbol
*array_src
=NULL
;
2459 hist_op
*hist_src
=NULL
;
2461 classify_indexable(src
, array_src
, hist_src
);
2464 dst
= require (array_src
);
2466 dst
= require (hist_src
);
2467 assert(clearok
|| dst
);
2473 // ------------------------------------------------------------------------
2477 deep_copy_visitor::visit_block (block
* s
)
2479 update_visitor::visit_block(new block(*s
));
2483 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
2485 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
2489 deep_copy_visitor::visit_null_statement (null_statement
* s
)
2491 update_visitor::visit_null_statement(new null_statement(*s
));
2495 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
2497 update_visitor::visit_expr_statement(new expr_statement(*s
));
2501 deep_copy_visitor::visit_if_statement (if_statement
* s
)
2503 update_visitor::visit_if_statement(new if_statement(*s
));
2507 deep_copy_visitor::visit_for_loop (for_loop
* s
)
2509 update_visitor::visit_for_loop(new for_loop(*s
));
2513 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
2515 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
2519 deep_copy_visitor::visit_return_statement (return_statement
* s
)
2521 update_visitor::visit_return_statement(new return_statement(*s
));
2525 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
2527 update_visitor::visit_delete_statement(new delete_statement(*s
));
2531 deep_copy_visitor::visit_next_statement (next_statement
* s
)
2533 update_visitor::visit_next_statement(new next_statement(*s
));
2537 deep_copy_visitor::visit_break_statement (break_statement
* s
)
2539 update_visitor::visit_break_statement(new break_statement(*s
));
2543 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
2545 update_visitor::visit_continue_statement(new continue_statement(*s
));
2549 deep_copy_visitor::visit_literal_string (literal_string
* e
)
2551 update_visitor::visit_literal_string(new literal_string(*e
));
2555 deep_copy_visitor::visit_literal_number (literal_number
* e
)
2557 update_visitor::visit_literal_number(new literal_number(*e
));
2561 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
2563 update_visitor::visit_binary_expression(new binary_expression(*e
));
2567 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
2569 update_visitor::visit_unary_expression(new unary_expression(*e
));
2573 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
2575 update_visitor::visit_pre_crement(new pre_crement(*e
));
2579 deep_copy_visitor::visit_post_crement (post_crement
* e
)
2581 update_visitor::visit_post_crement(new post_crement(*e
));
2586 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2588 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
2592 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2594 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
2598 deep_copy_visitor::visit_array_in (array_in
* e
)
2600 update_visitor::visit_array_in(new array_in(*e
));
2604 deep_copy_visitor::visit_comparison (comparison
* e
)
2606 update_visitor::visit_comparison(new comparison(*e
));
2610 deep_copy_visitor::visit_concatenation (concatenation
* e
)
2612 update_visitor::visit_concatenation(new concatenation(*e
));
2616 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
2618 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
2622 deep_copy_visitor::visit_assignment (assignment
* e
)
2624 update_visitor::visit_assignment(new assignment(*e
));
2628 deep_copy_visitor::visit_symbol (symbol
* e
)
2630 symbol
* n
= new symbol(*e
);
2631 n
->referent
= NULL
; // don't copy!
2632 update_visitor::visit_symbol(n
);
2636 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
2638 target_symbol
* n
= new target_symbol(*e
);
2639 n
->referent
= NULL
; // don't copy!
2640 update_visitor::visit_target_symbol(n
);
2644 deep_copy_visitor::visit_cast_op (cast_op
* e
)
2646 update_visitor::visit_cast_op(new cast_op(*e
));
2650 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
2652 update_visitor::visit_arrayindex(new arrayindex(*e
));
2656 deep_copy_visitor::visit_functioncall (functioncall
* e
)
2658 functioncall
* n
= new functioncall(*e
);
2659 n
->referent
= NULL
; // don't copy!
2660 update_visitor::visit_functioncall(n
);
2664 deep_copy_visitor::visit_print_format (print_format
* e
)
2666 update_visitor::visit_print_format(new print_format(*e
));
2670 deep_copy_visitor::visit_stat_op (stat_op
* e
)
2672 update_visitor::visit_stat_op(new stat_op(*e
));
2676 deep_copy_visitor::visit_hist_op (hist_op
* e
)
2678 update_visitor::visit_hist_op(new hist_op(*e
));
2681 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */