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
28 expression::expression ():
29 type (pe_unknown
), tok (0)
34 expression::~expression ()
39 statement::statement ():
45 statement::statement (const token
* tok
):
51 null_statement::null_statement (const token
* tok
):
57 statement::~statement ()
68 arrayindex::arrayindex ():
74 functioncall::functioncall ():
80 symboldecl::symboldecl ():
87 symboldecl::~symboldecl ()
91 probe_point::probe_point (std::vector
<component
*> const & comps
):
92 components(comps
), optional (false), sufficient (false),
97 // NB: shallow-copy of compoonents & condition!
98 probe_point::probe_point (const probe_point
& pp
):
99 components(pp
.components
), optional (pp
.optional
), sufficient (pp
.sufficient
),
100 condition (pp
.condition
)
105 probe_point::probe_point ():
106 optional (false), sufficient (false), condition (0)
114 static unsigned last_probeidx
= 0;
115 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
119 probe_point::component::component ():
125 probe_point::component::component (std::string
const & f
, literal
* a
):
132 arity (-1), maxsize(0), init(NULL
)
138 vardecl::set_arity (int a
)
143 if ((arity
!= a
&& arity
>= 0) || (a
== 0 && maxsize
> 0))
144 throw semantic_error ("inconsistent arity", tok
);
149 index_types
.resize (arity
);
150 for (int i
=0; i
<arity
; i
++)
151 index_types
[i
] = pe_unknown
;
156 vardecl::compatible_arity (int a
)
158 if (a
== 0 && maxsize
> 0)
160 if (arity
== -1 || a
== -1)
166 functiondecl::functiondecl ():
167 body (0), synthetic (false)
172 literal_number::literal_number (int64_t v
)
179 literal_string::literal_string (const string
& v
)
187 operator << (ostream
& o
, const exp_type
& e
)
191 case pe_unknown
: o
<< "unknown"; break;
192 case pe_long
: o
<< "long"; break;
193 case pe_string
: o
<< "string"; break;
194 case pe_stats
: o
<< "stats"; break;
195 default: o
<< "???"; break;
202 target_symbol::assert_no_components(const std::string
& tapset
)
204 if (components
.empty())
207 switch (components
[0].type
)
209 case target_symbol::comp_literal_array_index
:
210 case target_symbol::comp_expression_array_index
:
211 throw semantic_error(tapset
+ " variable '" + base_name
+
212 "' may not be used as array",
214 case target_symbol::comp_struct_member
:
215 throw semantic_error(tapset
+ " variable '" + base_name
+
216 "' may not be used as a structure",
219 throw semantic_error ("invalid use of " + tapset
+
220 " variable '" + base_name
+ "'",
226 void target_symbol::chain (semantic_error
*e
)
228 assert (e
->chain
== 0);
229 e
->chain
= this->saved_conversion_error
;
230 this->saved_conversion_error
= e
;
234 // ------------------------------------------------------------------------
235 // parse tree printing
237 ostream
& operator << (ostream
& o
, const expression
& k
)
244 void literal_string::print (ostream
& o
) const
247 for (unsigned i
=0; i
<value
.size(); i
++)
248 if (value
[i
] == '"') // or other escapeworthy characters?
256 void literal_number::print (ostream
& o
) const
262 void binary_expression::print (ostream
& o
) const
264 o
<< "(" << *left
<< ") "
266 << " (" << *right
<< ")";
270 void unary_expression::print (ostream
& o
) const
272 o
<< op
<< '(' << *operand
<< ")";
275 void array_in::print (ostream
& o
) const
278 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
280 if (i
> 0) o
<< ", ";
281 operand
->indexes
[i
]->print (o
);
284 operand
->base
->print_indexable (o
);
287 void post_crement::print (ostream
& o
) const
289 o
<< '(' << *operand
<< ")" << op
;
293 void ternary_expression::print (ostream
& o
) const
295 o
<< "(" << *cond
<< ")?("
296 << *truevalue
<< "):("
297 << *falsevalue
<< ")";
301 void symbol::print (ostream
& o
) const
307 void target_symbol::component::print (ostream
& o
) const
311 case comp_struct_member
:
314 case comp_literal_array_index
:
315 o
<< '[' << num_index
<< ']';
317 case comp_expression_array_index
:
318 o
<< '[' << *expr_index
<< ']';
324 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
331 void target_symbol::print (ostream
& o
) const
336 for (unsigned i
= 0; i
< components
.size(); ++i
)
341 void cast_op::print (ostream
& o
) const
345 o
<< base_name
<< '(' << *operand
;
346 o
<< ", " << lex_cast_qstring (type
);
347 if (module
.length() > 0)
348 o
<< ", " << lex_cast_qstring (module
);
350 for (unsigned i
= 0; i
< components
.size(); ++i
)
355 void defined_op::print (ostream
& o
) const
357 o
<< "@defined(" << *operand
<< ")";
361 void vardecl::print (ostream
& o
) const
365 o
<< "[" << maxsize
<< "]";
366 if (arity
> 0 || index_types
.size() > 0)
376 void vardecl::printsig (ostream
& o
) const
380 o
<< "[" << maxsize
<< "]";
382 if (index_types
.size() > 0)
385 for (unsigned i
=0; i
<index_types
.size(); i
++)
386 o
<< (i
>0 ? ", " : "") << index_types
[i
];
392 void functiondecl::print (ostream
& o
) const
394 o
<< "function " << name
<< " (";
395 for (unsigned i
=0; i
<formal_args
.size(); i
++)
396 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
402 void functiondecl::printsig (ostream
& o
) const
404 o
<< name
<< ":" << type
<< " (";
405 for (unsigned i
=0; i
<formal_args
.size(); i
++)
406 o
<< (i
>0 ? ", " : "")
409 << formal_args
[i
]->type
;
414 void arrayindex::print (ostream
& o
) const
416 base
->print_indexable (o
);
418 for (unsigned i
=0; i
<indexes
.size(); i
++)
419 o
<< (i
>0 ? ", " : "") << *indexes
[i
];
424 void functioncall::print (ostream
& o
) const
426 o
<< function
<< "(";
427 for (unsigned i
=0; i
<args
.size(); i
++)
428 o
<< (i
>0 ? ", " : "") << *args
[i
];
434 print_format::create(const token
*t
)
436 bool stream
, format
, delim
, newline
, _char
;
437 const char *n
= t
->content
.c_str();
440 format
= delim
= newline
= _char
= false;
442 if (strcmp(n
, "print_char") == 0)
452 if (0 != strncmp(n
, "print", 5))
469 if (*n
== 'l' && *(n
+1) == 'n')
480 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
);
487 print_format::components_to_string(vector
<format_component
> const & components
)
491 for (vector
<format_component
>::const_iterator i
= components
.begin();
492 i
!= components
.end(); ++i
)
495 assert (i
->type
!= conv_unspecified
);
497 if (i
->type
== conv_literal
)
499 assert(!i
->literal_string
.empty());
500 for (string::const_iterator j
= i
->literal_string
.begin();
501 j
!= i
->literal_string
.end(); ++j
)
503 // See also: c_unparser::visit_literal_string and lex_cast_qstring
515 if (i
->flags
& static_cast<unsigned long>(fmt_flag_zeropad
))
518 if (i
->flags
& static_cast<unsigned long>(fmt_flag_plus
))
521 if (i
->flags
& static_cast<unsigned long>(fmt_flag_space
))
524 if (i
->flags
& static_cast<unsigned long>(fmt_flag_left
))
527 if (i
->flags
& static_cast<unsigned long>(fmt_flag_special
))
530 if (i
->widthtype
== width_dynamic
)
532 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
535 if (i
->prectype
== prec_dynamic
)
537 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
538 oss
<< '.' << i
->precision
;
550 case conv_signed_decimal
:
554 case conv_unsigned_decimal
:
558 case conv_unsigned_octal
:
562 case conv_unsigned_ptr
:
566 case conv_unsigned_uppercase_hex
:
570 case conv_unsigned_lowercase_hex
:
582 case conv_memory_hex
:
594 vector
<print_format::format_component
>
595 print_format::string_to_components(string
const & str
)
597 format_component curr
;
598 vector
<format_component
> res
;
602 string::const_iterator i
= str
.begin();
604 while (i
!= str
.end())
608 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
609 curr
.type
= conv_literal
;
610 curr
.literal_string
+= *i
;
614 else if (i
+1 == str
.end() || *(i
+1) == '%')
617 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
618 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
619 curr
.type
= conv_literal
;
620 curr
.literal_string
+= '%';
627 if (curr
.type
!= conv_unspecified
)
629 // Flush any component we were previously accumulating
630 assert (curr
.type
== conv_literal
);
640 // Now we are definitely parsing a conversion.
641 // Begin by parsing flags (which are optional).
646 curr
.flags
|= static_cast<unsigned long>(fmt_flag_zeropad
);
651 curr
.flags
|= static_cast<unsigned long>(fmt_flag_plus
);
656 curr
.flags
|= static_cast<unsigned long>(fmt_flag_left
);
661 curr
.flags
|= static_cast<unsigned long>(fmt_flag_space
);
666 curr
.flags
|= static_cast<unsigned long>(fmt_flag_special
);
677 // Parse optional width
680 curr
.widthtype
= width_dynamic
;
683 else if (isdigit(*i
))
685 curr
.widthtype
= width_static
;
690 curr
.width
+= (*i
- '0');
693 while (i
!= str
.end() && isdigit(*i
));
699 // Parse optional precision
707 curr
.prectype
= prec_dynamic
;
710 else if (isdigit(*i
))
712 curr
.prectype
= prec_static
;
716 curr
.precision
*= 10;
717 curr
.precision
+= (*i
- '0');
720 while (i
!= str
.end() && isdigit(*i
));
727 // Parse the actual conversion specifier (bcsmdioupxXn)
730 // Valid conversion types
732 curr
.type
= conv_binary
;
736 curr
.type
= conv_char
;
740 curr
.type
= conv_string
;
744 curr
.type
= conv_memory
;
748 curr
.type
= conv_memory_hex
;
753 curr
.type
= conv_signed_decimal
;
757 curr
.type
= conv_unsigned_octal
;
761 curr
.type
= conv_unsigned_decimal
;
765 curr
.type
= conv_unsigned_ptr
;
769 curr
.type
= conv_unsigned_uppercase_hex
;
773 curr
.type
= conv_unsigned_lowercase_hex
;
780 if (curr
.type
== conv_unspecified
)
781 throw parse_error("invalid or missing conversion specifier");
788 // If there's a remaining partly-composed conversion, fail.
789 if (!curr
.is_empty())
791 if (curr
.type
== conv_literal
)
794 throw parse_error("trailing incomplete print format conversion");
801 void print_format::print (ostream
& o
) const
803 o
<< tok
->content
<< "(";
804 if (print_with_format
)
805 o
<< lex_cast_qstring (raw_components
);
806 if (print_with_delim
)
807 o
<< lex_cast_qstring (delimiter
.literal_string
);
810 for (vector
<expression
*>::const_iterator i
= args
.begin();
811 i
!= args
.end(); ++i
)
813 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
820 void stat_op::print (ostream
& o
) const
850 hist_op::print (ostream
& o
) const
856 assert(params
.size() == 3);
859 for (size_t i
= 0; i
< params
.size(); ++i
)
861 o
<< ", " << params
[i
];
867 assert(params
.size() == 0);
875 ostream
& operator << (ostream
& o
, const statement
& k
)
882 void embeddedcode::print (ostream
&o
) const
889 void block::print (ostream
& o
) const
892 for (unsigned i
=0; i
<statements
.size(); i
++)
893 o
<< *statements
[i
] << endl
;
897 block::block (statement
* car
, statement
* cdr
)
899 statements
.push_back(car
);
900 statements
.push_back(cdr
);
901 this->tok
= car
->tok
;
906 void for_loop::print (ostream
& o
) const
909 if (init
) init
->print (o
);
913 if (incr
) incr
->print (o
);
919 void foreach_loop::print (ostream
& o
) const
922 for (unsigned i
=0; i
<indexes
.size(); i
++)
924 if (i
> 0) o
<< ", ";
925 indexes
[i
]->print (o
);
926 if (sort_direction
!= 0 && sort_column
== i
+1)
927 o
<< (sort_direction
> 0 ? "+" : "-");
930 base
->print_indexable (o
);
931 if (sort_direction
!= 0 && sort_column
== 0)
932 o
<< (sort_direction
> 0 ? "+" : "-");
943 void null_statement::print (ostream
& o
) const
949 void expr_statement::print (ostream
& o
) const
955 void return_statement::print (ostream
& o
) const
957 o
<< "return " << *value
;
961 void delete_statement::print (ostream
& o
) const
963 o
<< "delete " << *value
;
966 void next_statement::print (ostream
& o
) const
971 void break_statement::print (ostream
& o
) const
976 void continue_statement::print (ostream
& o
) const
981 void if_statement::print (ostream
& o
) const
983 o
<< "if (" << *condition
<< ") "
984 << *thenblock
<< endl
;
986 o
<< "else " << *elseblock
<< endl
;
990 void stapfile::print (ostream
& o
) const
992 o
<< "# file " << name
<< endl
;
994 for (unsigned i
=0; i
<embeds
.size(); i
++)
995 embeds
[i
]->print (o
);
997 for (unsigned i
=0; i
<globals
.size(); i
++)
1000 globals
[i
]->print (o
);
1004 for (unsigned i
=0; i
<aliases
.size(); i
++)
1006 aliases
[i
]->print (o
);
1010 for (unsigned i
=0; i
<probes
.size(); i
++)
1012 probes
[i
]->print (o
);
1016 for (unsigned j
= 0; j
< functions
.size(); j
++)
1018 functions
[j
]->print (o
);
1024 void probe::print (ostream
& o
) const
1032 void probe::printsig (ostream
& o
) const
1034 const probe_alias
*alias
= get_alias ();
1037 alias
->printsig (o
);
1041 for (unsigned i
=0; i
<locations
.size(); i
++)
1043 if (i
> 0) o
<< ",";
1044 locations
[i
]->print (o
);
1050 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
)
1052 probes_list
.push_back(this);
1056 void probe_point::print (ostream
& o
) const
1058 for (unsigned i
=0; i
<components
.size(); i
++)
1061 probe_point::component
* c
= components
[i
];
1064 o
<< "(" << *c
->arg
<< ")";
1068 else if (optional
) // sufficient implies optional
1071 o
<< " if (" << *condition
<< ")";
1074 string
probe_point::str ()
1077 for (unsigned i
=0; i
<components
.size(); i
++)
1080 probe_point::component
* c
= components
[i
];
1083 o
<< "(" << *c
->arg
<< ")";
1087 else if (optional
) // sufficient implies optional
1090 o
<< " if (" << *condition
<< ")";
1095 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1096 probe (), alias_names (aliases
)
1100 void probe_alias::printsig (ostream
& o
) const
1102 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1104 o
<< (i
>0 ? " = " : "");
1105 alias_names
[i
]->print (o
);
1108 for (unsigned i
=0; i
<locations
.size(); i
++)
1110 if (i
> 0) o
<< ", ";
1111 locations
[i
]->print (o
);
1116 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1123 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1131 // ------------------------------------------------------------------------
1136 block::visit (visitor
* u
)
1138 u
->visit_block (this);
1143 embeddedcode::visit (visitor
* u
)
1145 u
->visit_embeddedcode (this);
1150 for_loop::visit (visitor
* u
)
1152 u
->visit_for_loop (this);
1156 foreach_loop::visit (visitor
* u
)
1158 u
->visit_foreach_loop (this);
1162 null_statement::visit (visitor
* u
)
1164 u
->visit_null_statement (this);
1168 expr_statement::visit (visitor
* u
)
1170 u
->visit_expr_statement (this);
1174 return_statement::visit (visitor
* u
)
1176 u
->visit_return_statement (this);
1180 delete_statement::visit (visitor
* u
)
1182 u
->push_active_lvalue (this->value
);
1183 u
->visit_delete_statement (this);
1184 u
->pop_active_lvalue ();
1188 if_statement::visit (visitor
* u
)
1190 u
->visit_if_statement (this);
1194 next_statement::visit (visitor
* u
)
1196 u
->visit_next_statement (this);
1200 break_statement::visit (visitor
* u
)
1202 u
->visit_break_statement (this);
1206 continue_statement::visit (visitor
* u
)
1208 u
->visit_continue_statement (this);
1212 literal_string::visit(visitor
* u
)
1214 u
->visit_literal_string (this);
1218 literal_number::visit(visitor
* u
)
1220 u
->visit_literal_number (this);
1224 binary_expression::visit (visitor
* u
)
1226 u
->visit_binary_expression (this);
1230 unary_expression::visit (visitor
* u
)
1232 u
->visit_unary_expression (this);
1236 pre_crement::visit (visitor
* u
)
1238 u
->push_active_lvalue (this->operand
);
1239 u
->visit_pre_crement (this);
1240 u
->pop_active_lvalue ();
1244 post_crement::visit (visitor
* u
)
1246 u
->push_active_lvalue (this->operand
);
1247 u
->visit_post_crement (this);
1248 u
->pop_active_lvalue ();
1252 logical_or_expr::visit (visitor
* u
)
1254 u
->visit_logical_or_expr (this);
1258 logical_and_expr::visit (visitor
* u
)
1260 u
->visit_logical_and_expr (this);
1264 array_in::visit (visitor
* u
)
1266 u
->visit_array_in (this);
1270 comparison::visit (visitor
* u
)
1272 u
->visit_comparison (this);
1276 concatenation::visit (visitor
* u
)
1278 u
->visit_concatenation (this);
1282 ternary_expression::visit (visitor
* u
)
1284 u
->visit_ternary_expression (this);
1288 assignment::visit (visitor
* u
)
1290 u
->push_active_lvalue (this->left
);
1291 u
->visit_assignment (this);
1292 u
->pop_active_lvalue ();
1296 symbol::visit (visitor
* u
)
1298 u
->visit_symbol (this);
1302 target_symbol::visit (visitor
* u
)
1304 u
->visit_target_symbol(this);
1308 target_symbol::visit_components (visitor
* u
)
1310 for (unsigned i
= 0; i
< components
.size(); ++i
)
1311 if (components
[i
].type
== comp_expression_array_index
)
1312 components
[i
].expr_index
->visit (u
);
1316 target_symbol::visit_components (update_visitor
* u
)
1318 for (unsigned i
= 0; i
< components
.size(); ++i
)
1319 if (components
[i
].type
== comp_expression_array_index
)
1320 u
->replace (components
[i
].expr_index
);
1324 cast_op::visit (visitor
* u
)
1326 u
->visit_cast_op(this);
1331 defined_op::visit (visitor
* u
)
1333 u
->visit_defined_op(this);
1338 arrayindex::visit (visitor
* u
)
1340 u
->visit_arrayindex (this);
1344 functioncall::visit (visitor
* u
)
1346 u
->visit_functioncall (this);
1350 print_format::visit (visitor
*u
)
1352 u
->visit_print_format (this);
1356 stat_op::visit (visitor
*u
)
1358 u
->visit_stat_op (this);
1362 hist_op::visit (visitor
*u
)
1364 u
->visit_hist_op (this);
1368 indexable::print_indexable (std::ostream
& o
) const
1371 const hist_op
*hist
;
1372 classify_const_indexable(this, sym
, hist
);
1383 indexable::visit_indexable (visitor
* u
)
1387 classify_indexable(this, sym
, hist
);
1399 indexable::is_symbol(symbol
*& sym_out
)
1406 indexable::is_hist_op(hist_op
*& hist_out
)
1413 indexable::is_const_symbol(const symbol
*& sym_out
) const
1420 indexable::is_const_hist_op(const hist_op
*& hist_out
) const
1427 symbol::is_symbol(symbol
*& sym_out
)
1434 symbol::is_const_symbol(const symbol
*& sym_out
) const
1441 symbol::get_tok() const
1447 hist_op::is_hist_op(hist_op
*& hist_out
)
1454 hist_op::is_const_hist_op(const hist_op
*& hist_out
) const
1461 hist_op::get_tok() const
1467 classify_indexable(indexable
* ix
,
1468 symbol
*& array_out
,
1469 hist_op
*& hist_out
)
1473 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1474 throw semantic_error("Expecting symbol or histogram operator", ix
->get_tok());
1475 if (ix
&& !(hist_out
|| array_out
))
1476 throw semantic_error("Failed to classify indexable", ix
->get_tok());
1480 classify_const_indexable(const indexable
* ix
,
1481 const symbol
*& array_out
,
1482 const hist_op
*& hist_out
)
1486 if (!(ix
->is_const_symbol(array_out
) || ix
->is_const_hist_op(hist_out
)))
1487 throw semantic_error("Expecting symbol or histogram operator", ix
->get_tok());
1490 // ------------------------------------------------------------------------
1493 visitor::is_active_lvalue(expression
*e
)
1495 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1497 if (active_lvalues
[i
] == e
)
1504 visitor::push_active_lvalue(expression
*e
)
1506 active_lvalues
.push_back(e
);
1510 visitor::pop_active_lvalue()
1512 assert(!active_lvalues
.empty());
1513 active_lvalues
.pop_back();
1518 // ------------------------------------------------------------------------
1521 traversing_visitor::visit_block (block
* s
)
1523 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1524 s
->statements
[i
]->visit (this);
1528 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1533 traversing_visitor::visit_null_statement (null_statement
*)
1538 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1540 s
->value
->visit (this);
1544 traversing_visitor::visit_if_statement (if_statement
* s
)
1546 s
->condition
->visit (this);
1547 s
->thenblock
->visit (this);
1549 s
->elseblock
->visit (this);
1553 traversing_visitor::visit_for_loop (for_loop
* s
)
1555 if (s
->init
) s
->init
->visit (this);
1556 s
->cond
->visit (this);
1557 if (s
->incr
) s
->incr
->visit (this);
1558 s
->block
->visit (this);
1562 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1564 symbol
*array
= NULL
;
1565 hist_op
*hist
= NULL
;
1566 classify_indexable (s
->base
, array
, hist
);
1572 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1573 s
->indexes
[i
]->visit (this);
1576 s
->limit
->visit (this);
1578 s
->block
->visit (this);
1582 traversing_visitor::visit_return_statement (return_statement
* s
)
1584 s
->value
->visit (this);
1588 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1590 s
->value
->visit (this);
1594 traversing_visitor::visit_next_statement (next_statement
*)
1599 traversing_visitor::visit_break_statement (break_statement
*)
1604 traversing_visitor::visit_continue_statement (continue_statement
*)
1609 traversing_visitor::visit_literal_string (literal_string
*)
1614 traversing_visitor::visit_literal_number (literal_number
*)
1619 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1621 e
->left
->visit (this);
1622 e
->right
->visit (this);
1626 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1628 e
->operand
->visit (this);
1632 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1634 e
->operand
->visit (this);
1638 traversing_visitor::visit_post_crement (post_crement
* e
)
1640 e
->operand
->visit (this);
1645 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1647 e
->left
->visit (this);
1648 e
->right
->visit (this);
1652 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1654 e
->left
->visit (this);
1655 e
->right
->visit (this);
1659 traversing_visitor::visit_array_in (array_in
* e
)
1661 e
->operand
->visit (this);
1665 traversing_visitor::visit_comparison (comparison
* e
)
1667 e
->left
->visit (this);
1668 e
->right
->visit (this);
1672 traversing_visitor::visit_concatenation (concatenation
* e
)
1674 e
->left
->visit (this);
1675 e
->right
->visit (this);
1679 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1681 e
->cond
->visit (this);
1682 e
->truevalue
->visit (this);
1683 e
->falsevalue
->visit (this);
1687 traversing_visitor::visit_assignment (assignment
* e
)
1689 e
->left
->visit (this);
1690 e
->right
->visit (this);
1694 traversing_visitor::visit_symbol (symbol
*)
1699 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1701 e
->visit_components (this);
1705 traversing_visitor::visit_cast_op (cast_op
* e
)
1707 e
->operand
->visit (this);
1708 e
->visit_components (this);
1712 traversing_visitor::visit_defined_op (defined_op
* e
)
1714 e
->operand
->visit (this);
1719 traversing_visitor::visit_arrayindex (arrayindex
* e
)
1721 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
1722 e
->indexes
[i
]->visit (this);
1724 symbol
*array
= NULL
;
1725 hist_op
*hist
= NULL
;
1726 classify_indexable(e
->base
, array
, hist
);
1728 return array
->visit(this);
1730 return hist
->visit(this);
1734 traversing_visitor::visit_functioncall (functioncall
* e
)
1736 for (unsigned i
=0; i
<e
->args
.size(); i
++)
1737 e
->args
[i
]->visit (this);
1741 traversing_visitor::visit_print_format (print_format
* e
)
1743 for (unsigned i
=0; i
<e
->args
.size(); i
++)
1744 e
->args
[i
]->visit (this);
1746 e
->hist
->visit(this);
1750 traversing_visitor::visit_stat_op (stat_op
* e
)
1752 e
->stat
->visit (this);
1756 traversing_visitor::visit_hist_op (hist_op
* e
)
1758 e
->stat
->visit (this);
1763 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
1765 traversing_visitor::visit_functioncall (e
);
1767 // prevent infinite recursion
1768 if (traversed
.find (e
->referent
) == traversed
.end ())
1770 traversed
.insert (e
->referent
);
1772 functiondecl
* last_current_function
= current_function
;
1773 current_function
= e
->referent
;
1774 e
->referent
->body
->visit (this);
1775 current_function
= last_current_function
;
1781 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
1783 assert (current_function
); // only they get embedded code
1785 // Don't allow embedded C functions in unprivileged mode unless
1786 // they are tagged with /* unprivileged */
1787 if (session
.unprivileged
&& s
->code
.find ("/* unprivileged */") == string::npos
)
1788 throw semantic_error ("function may not be used when --unprivileged is specified",
1789 current_function
->tok
);
1791 // We want to elide embedded-C functions when possible. For
1792 // example, each $target variable access is expanded to an
1793 // embedded-C function call. Yet, for safety reasons, we should
1794 // presume that embedded-C functions have intentional side-effects.
1796 // To tell these two types of functions apart, we apply a
1797 // Kludge(tm): we look for a magic string within the function body.
1798 // $target variables as rvalues will have this; lvalues won't.
1799 // Also, explicit side-effect-free tapset functions will have this.
1801 if (s
->code
.find ("/* pure */") != string::npos
)
1804 embedded_seen
= true;
1808 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
1810 // Still-unresolved target symbol assignments get treated as
1811 // generating side-effects like embedded-C, to prevent premature
1812 // elision and later error message suppression (PR5516). rvalue use
1813 // of unresolved target symbols is OTOH not considered a side-effect.
1815 if (is_active_lvalue (e
))
1816 embedded_seen
= true;
1818 functioncall_traversing_visitor::visit_target_symbol (e
);
1822 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
1824 // As with target_symbols, unresolved cast assignments need to preserved
1825 // for later error handling.
1826 if (is_active_lvalue (e
))
1827 embedded_seen
= true;
1829 functioncall_traversing_visitor::visit_cast_op (e
);
1833 varuse_collecting_visitor::visit_defined_op (defined_op
*e
)
1836 functioncall_traversing_visitor::visit_defined_op (e
);
1841 varuse_collecting_visitor::visit_print_format (print_format
* e
)
1843 // NB: Instead of being top-level statements, "print" and "printf"
1844 // are implemented as statement-expressions containing a
1845 // print_format. They have side-effects, but not via the
1846 // embedded-code detection method above.
1848 // But sprint and sprintf don't have side-effects.
1850 if (e
->print_to_stream
)
1851 embedded_seen
= true; // a proxy for "has unknown side-effects"
1853 functioncall_traversing_visitor::visit_print_format (e
);
1858 varuse_collecting_visitor::visit_assignment (assignment
*e
)
1860 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
1862 expression
* last_lvalue
= current_lvalue
;
1863 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
1864 functioncall_traversing_visitor::visit_assignment (e
);
1865 current_lvalue
= last_lvalue
;
1867 else // read-modify-writes
1869 expression
* last_lrvalue
= current_lrvalue
;
1870 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
1871 functioncall_traversing_visitor::visit_assignment (e
);
1872 current_lrvalue
= last_lrvalue
;
1877 varuse_collecting_visitor::visit_symbol (symbol
*e
)
1879 if (e
->referent
== 0)
1880 throw semantic_error ("symbol without referent", e
->tok
);
1882 // We could handle initialized globals by marking them as "written".
1883 // However, this current visitor may be called for a function or
1884 // probe body, from the point of view of which this global is
1885 // already initialized, so not written.
1887 if (e->referent->init)
1888 written.insert (e->referent);
1891 if (current_lvalue
== e
|| current_lrvalue
== e
)
1893 written
.insert (e
->referent
);
1894 // clog << "write ";
1896 if (current_lvalue
!= e
|| current_lrvalue
== e
)
1898 read
.insert (e
->referent
);
1901 // clog << *e->tok << endl;
1904 // NB: stat_op need not be overridden, since it will get to
1905 // visit_symbol and only as a possible rvalue.
1909 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
1911 // Hooking this callback is necessary because of the hacky
1912 // statistics representation. For the expression "i[4] = 5", the
1913 // incoming lvalue will point to this arrayindex. However, the
1914 // symbol corresponding to the "i[4]" is multiply inherited with
1915 // arrayindex. If the symbol base part of this object is not at
1916 // offset 0, then static_cast<symbol*>(e) may result in a different
1917 // address, and not match lvalue by number when we recurse that way.
1918 // So we explicitly override the incoming lvalue/lrvalue values to
1919 // point at the embedded objects' actual base addresses.
1921 expression
* last_lrvalue
= current_lrvalue
;
1922 expression
* last_lvalue
= current_lvalue
;
1924 symbol
*array
= NULL
;
1925 hist_op
*hist
= NULL
;
1926 classify_indexable(e
->base
, array
, hist
);
1930 if (current_lrvalue
== e
) current_lrvalue
= array
;
1931 if (current_lvalue
== e
) current_lvalue
= array
;
1932 functioncall_traversing_visitor::visit_arrayindex (e
);
1936 if (current_lrvalue
== e
) current_lrvalue
= hist
->stat
;
1937 if (current_lvalue
== e
) current_lvalue
= hist
->stat
;
1938 functioncall_traversing_visitor::visit_arrayindex (e
);
1941 current_lrvalue
= last_lrvalue
;
1942 current_lvalue
= last_lvalue
;
1947 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
1949 // PR6954: regard as pure writes
1950 expression
* last_lvalue
= current_lvalue
;
1951 current_lvalue
= e
->operand
; // leave a mark for ::visit_symbol
1952 functioncall_traversing_visitor::visit_pre_crement (e
);
1953 current_lvalue
= last_lvalue
;
1957 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
1959 // PR6954: regard as pure writes
1960 expression
* last_lvalue
= current_lvalue
;
1961 current_lvalue
= e
->operand
; // leave a mark for ::visit_symbol
1962 functioncall_traversing_visitor::visit_post_crement (e
);
1963 current_lvalue
= last_lvalue
;
1967 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
1969 // NB: we duplicate so don't bother call
1970 // functioncall_traversing_visitor::visit_foreach_loop (s);
1972 symbol
*array
= NULL
;
1973 hist_op
*hist
= NULL
;
1974 classify_indexable (s
->base
, array
, hist
);
1980 // If the collection is sorted, imply a "write" access to the
1981 // array in addition to the "read" one already noted above.
1982 if (s
->sort_direction
)
1984 symbol
*array
= NULL
;
1985 hist_op
*hist
= NULL
;
1986 classify_indexable (s
->base
, array
, hist
);
1987 if (array
) this->written
.insert (array
->referent
);
1988 // XXX: Can hist_op iterations be sorted?
1991 // NB: don't forget to visit the index expressions, which are lvalues.
1992 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1994 expression
* last_lvalue
= current_lvalue
;
1995 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
1996 s
->indexes
[i
]->visit (this);
1997 current_lvalue
= last_lvalue
;
2001 s
->limit
->visit (this);
2003 s
->block
->visit (this);
2008 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
2010 // Ideally, this would be treated like an assignment: a plain write
2011 // to the underlying value ("lvalue"). XXX: However, the
2012 // optimization pass is not smart enough to remove an unneeded
2013 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
2014 // should protect the underlying value from optimizional mischief.
2015 expression
* last_lrvalue
= current_lrvalue
;
2016 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
2017 functioncall_traversing_visitor::visit_delete_statement (s
);
2018 current_lrvalue
= last_lrvalue
;
2022 varuse_collecting_visitor::side_effect_free ()
2024 return (written
.empty() && !embedded_seen
);
2029 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
2031 // A looser notion of side-effect-freeness with respect to a given
2032 // list of variables.
2034 // That's useful because the written list may consist of local
2035 // variables of called functions. But visible side-effects only
2036 // occur if the client's locals, or any globals are written-to.
2038 set
<vardecl
*> intersection
;
2039 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
2040 set_intersection (written
.begin(), written
.end(),
2041 vars
.begin(), vars
.end(),
2044 return (intersection
.empty() && !embedded_seen
);
2050 // ------------------------------------------------------------------------
2053 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2054 throwing_visitor::throwing_visitor (): msg ("invalid element") {}
2058 throwing_visitor::throwone (const token
* t
)
2060 throw semantic_error (msg
, t
);
2064 throwing_visitor::visit_block (block
* s
)
2070 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2076 throwing_visitor::visit_null_statement (null_statement
* s
)
2082 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2088 throwing_visitor::visit_if_statement (if_statement
* s
)
2094 throwing_visitor::visit_for_loop (for_loop
* s
)
2100 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2106 throwing_visitor::visit_return_statement (return_statement
* s
)
2112 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2118 throwing_visitor::visit_next_statement (next_statement
* s
)
2124 throwing_visitor::visit_break_statement (break_statement
* s
)
2130 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2136 throwing_visitor::visit_literal_string (literal_string
* e
)
2142 throwing_visitor::visit_literal_number (literal_number
* e
)
2148 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2154 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2160 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2166 throwing_visitor::visit_post_crement (post_crement
* e
)
2173 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2179 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2185 throwing_visitor::visit_array_in (array_in
* e
)
2191 throwing_visitor::visit_comparison (comparison
* e
)
2197 throwing_visitor::visit_concatenation (concatenation
* e
)
2203 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2209 throwing_visitor::visit_assignment (assignment
* e
)
2215 throwing_visitor::visit_symbol (symbol
* e
)
2221 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2227 throwing_visitor::visit_cast_op (cast_op
* e
)
2233 throwing_visitor::visit_defined_op (defined_op
* e
)
2240 throwing_visitor::visit_arrayindex (arrayindex
* e
)
2246 throwing_visitor::visit_functioncall (functioncall
* e
)
2252 throwing_visitor::visit_print_format (print_format
* e
)
2258 throwing_visitor::visit_stat_op (stat_op
* e
)
2264 throwing_visitor::visit_hist_op (hist_op
* e
)
2270 // ------------------------------------------------------------------------
2274 update_visitor::visit_block (block
* s
)
2276 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
2277 replace (s
->statements
[i
]);
2282 update_visitor::visit_embeddedcode (embeddedcode
* s
)
2288 update_visitor::visit_null_statement (null_statement
* s
)
2294 update_visitor::visit_expr_statement (expr_statement
* s
)
2301 update_visitor::visit_if_statement (if_statement
* s
)
2303 replace (s
->condition
);
2304 replace (s
->thenblock
);
2305 replace (s
->elseblock
);
2310 update_visitor::visit_for_loop (for_loop
* s
)
2320 update_visitor::visit_foreach_loop (foreach_loop
* s
)
2322 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
2323 replace (s
->indexes
[i
]);
2331 update_visitor::visit_return_statement (return_statement
* s
)
2338 update_visitor::visit_delete_statement (delete_statement
* s
)
2345 update_visitor::visit_next_statement (next_statement
* s
)
2351 update_visitor::visit_break_statement (break_statement
* s
)
2357 update_visitor::visit_continue_statement (continue_statement
* s
)
2363 update_visitor::visit_literal_string (literal_string
* e
)
2369 update_visitor::visit_literal_number (literal_number
* e
)
2375 update_visitor::visit_binary_expression (binary_expression
* e
)
2383 update_visitor::visit_unary_expression (unary_expression
* e
)
2385 replace (e
->operand
);
2390 update_visitor::visit_pre_crement (pre_crement
* e
)
2392 replace (e
->operand
);
2397 update_visitor::visit_post_crement (post_crement
* e
)
2399 replace (e
->operand
);
2405 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2413 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2421 update_visitor::visit_array_in (array_in
* e
)
2423 replace (e
->operand
);
2428 update_visitor::visit_comparison (comparison
* e
)
2436 update_visitor::visit_concatenation (concatenation
* e
)
2444 update_visitor::visit_ternary_expression (ternary_expression
* e
)
2447 replace (e
->truevalue
);
2448 replace (e
->falsevalue
);
2453 update_visitor::visit_assignment (assignment
* e
)
2461 update_visitor::visit_symbol (symbol
* e
)
2467 update_visitor::visit_target_symbol (target_symbol
* e
)
2469 e
->visit_components (this);
2474 update_visitor::visit_cast_op (cast_op
* e
)
2476 replace (e
->operand
);
2477 e
->visit_components (this);
2482 update_visitor::visit_defined_op (defined_op
* e
)
2484 replace (e
->operand
);
2489 update_visitor::visit_arrayindex (arrayindex
* e
)
2492 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
2493 replace (e
->indexes
[i
]);
2498 update_visitor::visit_functioncall (functioncall
* e
)
2500 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
2501 replace (e
->args
[i
]);
2506 update_visitor::visit_print_format (print_format
* e
)
2508 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
2509 replace (e
->args
[i
]);
2515 update_visitor::visit_stat_op (stat_op
* e
)
2522 update_visitor::visit_hist_op (hist_op
* e
)
2528 template <> indexable
*
2529 update_visitor::require
<indexable
> (indexable
* src
, bool clearok
)
2531 indexable
*dst
= NULL
;
2534 symbol
*array_src
=NULL
;
2535 hist_op
*hist_src
=NULL
;
2537 classify_indexable(src
, array_src
, hist_src
);
2540 dst
= require (array_src
);
2542 dst
= require (hist_src
);
2543 assert(clearok
|| dst
);
2549 // ------------------------------------------------------------------------
2553 deep_copy_visitor::visit_block (block
* s
)
2555 update_visitor::visit_block(new block(*s
));
2559 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
2561 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
2565 deep_copy_visitor::visit_null_statement (null_statement
* s
)
2567 update_visitor::visit_null_statement(new null_statement(*s
));
2571 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
2573 update_visitor::visit_expr_statement(new expr_statement(*s
));
2577 deep_copy_visitor::visit_if_statement (if_statement
* s
)
2579 update_visitor::visit_if_statement(new if_statement(*s
));
2583 deep_copy_visitor::visit_for_loop (for_loop
* s
)
2585 update_visitor::visit_for_loop(new for_loop(*s
));
2589 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
2591 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
2595 deep_copy_visitor::visit_return_statement (return_statement
* s
)
2597 update_visitor::visit_return_statement(new return_statement(*s
));
2601 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
2603 update_visitor::visit_delete_statement(new delete_statement(*s
));
2607 deep_copy_visitor::visit_next_statement (next_statement
* s
)
2609 update_visitor::visit_next_statement(new next_statement(*s
));
2613 deep_copy_visitor::visit_break_statement (break_statement
* s
)
2615 update_visitor::visit_break_statement(new break_statement(*s
));
2619 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
2621 update_visitor::visit_continue_statement(new continue_statement(*s
));
2625 deep_copy_visitor::visit_literal_string (literal_string
* e
)
2627 update_visitor::visit_literal_string(new literal_string(*e
));
2631 deep_copy_visitor::visit_literal_number (literal_number
* e
)
2633 update_visitor::visit_literal_number(new literal_number(*e
));
2637 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
2639 update_visitor::visit_binary_expression(new binary_expression(*e
));
2643 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
2645 update_visitor::visit_unary_expression(new unary_expression(*e
));
2649 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
2651 update_visitor::visit_pre_crement(new pre_crement(*e
));
2655 deep_copy_visitor::visit_post_crement (post_crement
* e
)
2657 update_visitor::visit_post_crement(new post_crement(*e
));
2662 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2664 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
2668 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2670 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
2674 deep_copy_visitor::visit_array_in (array_in
* e
)
2676 update_visitor::visit_array_in(new array_in(*e
));
2680 deep_copy_visitor::visit_comparison (comparison
* e
)
2682 update_visitor::visit_comparison(new comparison(*e
));
2686 deep_copy_visitor::visit_concatenation (concatenation
* e
)
2688 update_visitor::visit_concatenation(new concatenation(*e
));
2692 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
2694 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
2698 deep_copy_visitor::visit_assignment (assignment
* e
)
2700 update_visitor::visit_assignment(new assignment(*e
));
2704 deep_copy_visitor::visit_symbol (symbol
* e
)
2706 symbol
* n
= new symbol(*e
);
2707 n
->referent
= NULL
; // don't copy!
2708 update_visitor::visit_symbol(n
);
2712 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
2714 target_symbol
* n
= new target_symbol(*e
);
2715 n
->referent
= NULL
; // don't copy!
2716 update_visitor::visit_target_symbol(n
);
2720 deep_copy_visitor::visit_cast_op (cast_op
* e
)
2722 update_visitor::visit_cast_op(new cast_op(*e
));
2726 deep_copy_visitor::visit_defined_op (defined_op
* e
)
2728 update_visitor::visit_defined_op(new defined_op(*e
));
2732 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
2734 update_visitor::visit_arrayindex(new arrayindex(*e
));
2738 deep_copy_visitor::visit_functioncall (functioncall
* e
)
2740 functioncall
* n
= new functioncall(*e
);
2741 n
->referent
= NULL
; // don't copy!
2742 update_visitor::visit_functioncall(n
);
2746 deep_copy_visitor::visit_print_format (print_format
* e
)
2748 update_visitor::visit_print_format(new print_format(*e
));
2752 deep_copy_visitor::visit_stat_op (stat_op
* e
)
2754 update_visitor::visit_stat_op(new stat_op(*e
));
2758 deep_copy_visitor::visit_hist_op (hist_op
* e
)
2760 update_visitor::visit_hist_op(new hist_op(*e
));
2763 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */