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 ()
56 arrayindex::arrayindex ():
62 functioncall::functioncall ():
68 symboldecl::symboldecl ():
75 symboldecl::~symboldecl ()
79 probe_point::probe_point (std::vector
<component
*> const & comps
,
81 components(comps
), tok(t
), optional (false), sufficient (false),
86 // NB: shallow-copy of compoonents & condition!
87 probe_point::probe_point (const probe_point
& pp
):
88 components(pp
.components
), tok(pp
.tok
), optional (pp
.optional
), sufficient (pp
.sufficient
),
89 condition (pp
.condition
)
94 probe_point::probe_point ():
95 tok (0), optional (false), sufficient (false), condition (0)
103 static unsigned last_probeidx
= 0;
104 this->name
= string ("probe_") + lex_cast(last_probeidx
++);
108 probe_point::component::component ():
114 probe_point::component::component (std::string
const & f
, literal
* a
):
121 arity (-1), maxsize(0), init(NULL
)
127 vardecl::set_arity (int a
)
132 if ((arity
!= a
&& arity
>= 0) || (a
== 0 && maxsize
> 0))
133 throw semantic_error ("inconsistent arity", tok
);
138 index_types
.resize (arity
);
139 for (int i
=0; i
<arity
; i
++)
140 index_types
[i
] = pe_unknown
;
145 vardecl::compatible_arity (int a
)
147 if (a
== 0 && maxsize
> 0)
149 if (arity
== -1 || a
== -1)
155 functiondecl::functiondecl ():
161 literal_number::literal_number (int64_t v
)
168 literal_string::literal_string (const string
& v
)
176 operator << (ostream
& o
, const exp_type
& e
)
180 case pe_unknown
: o
<< "unknown"; break;
181 case pe_long
: o
<< "long"; break;
182 case pe_string
: o
<< "string"; break;
183 case pe_stats
: o
<< "stats"; break;
184 default: o
<< "???"; break;
191 target_symbol::assert_no_components(const std::string
& tapset
)
193 if (components
.empty())
196 switch (components
[0].type
)
198 case target_symbol::comp_literal_array_index
:
199 case target_symbol::comp_expression_array_index
:
200 throw semantic_error(tapset
+ " variable '" + base_name
+
201 "' may not be used as array",
203 case target_symbol::comp_struct_member
:
204 throw semantic_error(tapset
+ " variable '" + base_name
+
205 "' may not be used as a structure",
208 throw semantic_error ("invalid use of " + tapset
+
209 " variable '" + base_name
+ "'",
215 // ------------------------------------------------------------------------
216 // parse tree printing
218 ostream
& operator << (ostream
& o
, const expression
& k
)
225 void literal_string::print (ostream
& o
) const
228 for (unsigned i
=0; i
<value
.size(); i
++)
229 if (value
[i
] == '"') // or other escapeworthy characters?
237 void literal_number::print (ostream
& o
) const
243 void binary_expression::print (ostream
& o
) const
245 o
<< "(" << *left
<< ") "
247 << " (" << *right
<< ")";
251 void unary_expression::print (ostream
& o
) const
253 o
<< op
<< '(' << *operand
<< ")";
256 void array_in::print (ostream
& o
) const
259 for (unsigned i
=0; i
<operand
->indexes
.size(); i
++)
261 if (i
> 0) o
<< ", ";
262 operand
->indexes
[i
]->print (o
);
265 operand
->base
->print_indexable (o
);
268 void post_crement::print (ostream
& o
) const
270 o
<< '(' << *operand
<< ")" << op
;
274 void ternary_expression::print (ostream
& o
) const
276 o
<< "(" << *cond
<< ")?("
277 << *truevalue
<< "):("
278 << *falsevalue
<< ")";
282 void symbol::print (ostream
& o
) const
288 void target_symbol::component::print (ostream
& o
) const
292 case comp_struct_member
:
295 case comp_literal_array_index
:
296 o
<< '[' << num_index
<< ']';
298 case comp_expression_array_index
:
299 o
<< '[' << *expr_index
<< ']';
305 std::ostream
& operator << (std::ostream
& o
, const target_symbol::component
& c
)
312 void target_symbol::print (ostream
& o
) const
317 for (unsigned i
= 0; i
< components
.size(); ++i
)
322 void cast_op::print (ostream
& o
) const
326 o
<< base_name
<< '(' << *operand
;
327 o
<< ", " << lex_cast_qstring (type
);
328 if (module
.length() > 0)
329 o
<< ", " << lex_cast_qstring (module
);
331 for (unsigned i
= 0; i
< components
.size(); ++i
)
336 void vardecl::print (ostream
& o
) const
340 o
<< "[" << maxsize
<< "]";
341 if (arity
> 0 || index_types
.size() > 0)
351 void vardecl::printsig (ostream
& o
) const
355 o
<< "[" << maxsize
<< "]";
357 if (index_types
.size() > 0)
360 for (unsigned i
=0; i
<index_types
.size(); i
++)
361 o
<< (i
>0 ? ", " : "") << index_types
[i
];
367 void functiondecl::print (ostream
& o
) const
369 o
<< "function " << name
<< " (";
370 for (unsigned i
=0; i
<formal_args
.size(); i
++)
371 o
<< (i
>0 ? ", " : "") << *formal_args
[i
];
377 void functiondecl::printsig (ostream
& o
) const
379 o
<< name
<< ":" << type
<< " (";
380 for (unsigned i
=0; i
<formal_args
.size(); i
++)
381 o
<< (i
>0 ? ", " : "")
384 << formal_args
[i
]->type
;
389 void arrayindex::print (ostream
& o
) const
391 base
->print_indexable (o
);
393 for (unsigned i
=0; i
<indexes
.size(); i
++)
394 o
<< (i
>0 ? ", " : "") << *indexes
[i
];
399 void functioncall::print (ostream
& o
) const
401 o
<< function
<< "(";
402 for (unsigned i
=0; i
<args
.size(); i
++)
403 o
<< (i
>0 ? ", " : "") << *args
[i
];
409 print_format::create(const token
*t
)
411 bool stream
, format
, delim
, newline
, _char
;
412 const char *n
= t
->content
.c_str();
415 format
= delim
= newline
= _char
= false;
417 if (strcmp(n
, "print_char") == 0)
427 if (0 != strncmp(n
, "print", 5))
444 if (*n
== 'l' && *(n
+1) == 'n')
455 print_format
*pf
= new print_format(stream
, format
, delim
, newline
, _char
);
462 print_format::components_to_string(vector
<format_component
> const & components
)
466 for (vector
<format_component
>::const_iterator i
= components
.begin();
467 i
!= components
.end(); ++i
)
470 assert (i
->type
!= conv_unspecified
);
472 if (i
->type
== conv_literal
)
474 assert(!i
->literal_string
.empty());
475 for (string::const_iterator j
= i
->literal_string
.begin();
476 j
!= i
->literal_string
.end(); ++j
)
478 // See also: c_unparser::visit_literal_string and lex_cast_qstring
490 if (i
->flags
& static_cast<unsigned long>(fmt_flag_zeropad
))
493 if (i
->flags
& static_cast<unsigned long>(fmt_flag_plus
))
496 if (i
->flags
& static_cast<unsigned long>(fmt_flag_space
))
499 if (i
->flags
& static_cast<unsigned long>(fmt_flag_left
))
502 if (i
->flags
& static_cast<unsigned long>(fmt_flag_special
))
505 if (i
->widthtype
== width_dynamic
)
507 else if (i
->widthtype
!= width_unspecified
&& i
->width
> 0)
510 if (i
->prectype
== prec_dynamic
)
512 else if (i
->prectype
!= prec_unspecified
&& i
->precision
> 0)
513 oss
<< '.' << i
->precision
;
525 case conv_signed_decimal
:
529 case conv_unsigned_decimal
:
533 case conv_unsigned_octal
:
537 case conv_unsigned_ptr
:
541 case conv_unsigned_uppercase_hex
:
545 case conv_unsigned_lowercase_hex
:
557 case conv_memory_hex
:
569 vector
<print_format::format_component
>
570 print_format::string_to_components(string
const & str
)
572 format_component curr
;
573 vector
<format_component
> res
;
577 string::const_iterator i
= str
.begin();
579 while (i
!= str
.end())
583 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
584 curr
.type
= conv_literal
;
585 curr
.literal_string
+= *i
;
589 else if (i
+1 == str
.end() || *(i
+1) == '%')
592 // *i == '%' and *(i+1) == '%'; append only one '%' to the literal string
593 assert (curr
.type
== conv_unspecified
|| curr
.type
== conv_literal
);
594 curr
.type
= conv_literal
;
595 curr
.literal_string
+= '%';
602 if (curr
.type
!= conv_unspecified
)
604 // Flush any component we were previously accumulating
605 assert (curr
.type
== conv_literal
);
615 // Now we are definitely parsing a conversion.
616 // Begin by parsing flags (which are optional).
621 curr
.flags
|= static_cast<unsigned long>(fmt_flag_zeropad
);
626 curr
.flags
|= static_cast<unsigned long>(fmt_flag_plus
);
631 curr
.flags
|= static_cast<unsigned long>(fmt_flag_left
);
636 curr
.flags
|= static_cast<unsigned long>(fmt_flag_space
);
641 curr
.flags
|= static_cast<unsigned long>(fmt_flag_special
);
652 // Parse optional width
655 curr
.widthtype
= width_dynamic
;
658 else if (isdigit(*i
))
660 curr
.widthtype
= width_static
;
665 curr
.width
+= (*i
- '0');
668 while (i
!= str
.end() && isdigit(*i
));
674 // Parse optional precision
682 curr
.prectype
= prec_dynamic
;
685 else if (isdigit(*i
))
687 curr
.prectype
= prec_static
;
691 curr
.precision
*= 10;
692 curr
.precision
+= (*i
- '0');
695 while (i
!= str
.end() && isdigit(*i
));
702 // Parse the actual conversion specifier (bcsmdioupxXn)
705 // Valid conversion types
707 curr
.type
= conv_binary
;
711 curr
.type
= conv_char
;
715 curr
.type
= conv_string
;
719 curr
.type
= conv_memory
;
723 curr
.type
= conv_memory_hex
;
728 curr
.type
= conv_signed_decimal
;
732 curr
.type
= conv_unsigned_octal
;
736 curr
.type
= conv_unsigned_decimal
;
740 curr
.type
= conv_unsigned_ptr
;
744 curr
.type
= conv_unsigned_uppercase_hex
;
748 curr
.type
= conv_unsigned_lowercase_hex
;
755 if (curr
.type
== conv_unspecified
)
756 throw parse_error("invalid or missing conversion specifier");
763 // If there's a remaining partly-composed conversion, fail.
764 if (!curr
.is_empty())
766 if (curr
.type
== conv_literal
)
769 throw parse_error("trailing incomplete print format conversion");
776 void print_format::print (ostream
& o
) const
778 o
<< tok
->content
<< "(";
779 if (print_with_format
)
780 o
<< lex_cast_qstring (raw_components
);
781 if (print_with_delim
)
782 o
<< lex_cast_qstring (delimiter
.literal_string
);
785 for (vector
<expression
*>::const_iterator i
= args
.begin();
786 i
!= args
.end(); ++i
)
788 if (i
!= args
.begin() || print_with_format
|| print_with_delim
)
795 void stat_op::print (ostream
& o
) const
825 hist_op::print (ostream
& o
) const
831 assert(params
.size() == 3);
834 for (size_t i
= 0; i
< params
.size(); ++i
)
836 o
<< ", " << params
[i
];
842 assert(params
.size() == 0);
850 ostream
& operator << (ostream
& o
, const statement
& k
)
857 void embeddedcode::print (ostream
&o
) const
864 void block::print (ostream
& o
) const
867 for (unsigned i
=0; i
<statements
.size(); i
++)
868 o
<< *statements
[i
] << endl
;
872 block::block (statement
* car
, statement
* cdr
)
874 statements
.push_back(car
);
875 statements
.push_back(cdr
);
876 this->tok
= car
->tok
;
881 void for_loop::print (ostream
& o
) const
884 if (init
) init
->print (o
);
888 if (incr
) incr
->print (o
);
894 void foreach_loop::print (ostream
& o
) const
897 for (unsigned i
=0; i
<indexes
.size(); i
++)
899 if (i
> 0) o
<< ", ";
900 indexes
[i
]->print (o
);
901 if (sort_direction
!= 0 && sort_column
== i
+1)
902 o
<< (sort_direction
> 0 ? "+" : "-");
905 base
->print_indexable (o
);
906 if (sort_direction
!= 0 && sort_column
== 0)
907 o
<< (sort_direction
> 0 ? "+" : "-");
918 void null_statement::print (ostream
& o
) const
924 void expr_statement::print (ostream
& o
) const
930 void return_statement::print (ostream
& o
) const
932 o
<< "return " << *value
;
936 void delete_statement::print (ostream
& o
) const
938 o
<< "delete " << *value
;
941 void next_statement::print (ostream
& o
) const
946 void break_statement::print (ostream
& o
) const
951 void continue_statement::print (ostream
& o
) const
956 void if_statement::print (ostream
& o
) const
958 o
<< "if (" << *condition
<< ") "
959 << *thenblock
<< endl
;
961 o
<< "else " << *elseblock
<< endl
;
965 void stapfile::print (ostream
& o
) const
967 o
<< "# file " << name
<< endl
;
969 for (unsigned i
=0; i
<embeds
.size(); i
++)
970 embeds
[i
]->print (o
);
972 for (unsigned i
=0; i
<globals
.size(); i
++)
975 globals
[i
]->print (o
);
979 for (unsigned i
=0; i
<aliases
.size(); i
++)
981 aliases
[i
]->print (o
);
985 for (unsigned i
=0; i
<probes
.size(); i
++)
987 probes
[i
]->print (o
);
991 for (unsigned j
= 0; j
< functions
.size(); j
++)
993 functions
[j
]->print (o
);
999 void probe::print (ostream
& o
) const
1007 void probe::printsig (ostream
& o
) const
1009 const probe_alias
*alias
= get_alias ();
1012 alias
->printsig (o
);
1016 for (unsigned i
=0; i
<locations
.size(); i
++)
1018 if (i
> 0) o
<< ",";
1019 locations
[i
]->print (o
);
1025 probe::collect_derivation_chain (std::vector
<probe
*> &probes_list
)
1027 probes_list
.push_back(this);
1031 void probe_point::print (ostream
& o
) const
1033 for (unsigned i
=0; i
<components
.size(); i
++)
1036 probe_point::component
* c
= components
[i
];
1039 o
<< "(" << *c
->arg
<< ")";
1043 else if (optional
) // sufficient implies optional
1046 o
<< " if (" << *condition
<< ")";
1049 string
probe_point::str ()
1052 for (unsigned i
=0; i
<components
.size(); i
++)
1055 probe_point::component
* c
= components
[i
];
1058 o
<< "(" << *c
->arg
<< ")";
1062 else if (optional
) // sufficient implies optional
1065 o
<< " if (" << *condition
<< ")";
1070 probe_alias::probe_alias(std::vector
<probe_point
*> const & aliases
):
1071 probe (), alias_names (aliases
)
1075 void probe_alias::printsig (ostream
& o
) const
1077 for (unsigned i
=0; i
<alias_names
.size(); i
++)
1079 o
<< (i
>0 ? " = " : "");
1080 alias_names
[i
]->print (o
);
1083 for (unsigned i
=0; i
<locations
.size(); i
++)
1085 if (i
> 0) o
<< ", ";
1086 locations
[i
]->print (o
);
1091 ostream
& operator << (ostream
& o
, const probe_point
& k
)
1098 ostream
& operator << (ostream
& o
, const symboldecl
& k
)
1106 // ------------------------------------------------------------------------
1111 block::visit (visitor
* u
)
1113 u
->visit_block (this);
1118 embeddedcode::visit (visitor
* u
)
1120 u
->visit_embeddedcode (this);
1125 for_loop::visit (visitor
* u
)
1127 u
->visit_for_loop (this);
1131 foreach_loop::visit (visitor
* u
)
1133 u
->visit_foreach_loop (this);
1137 null_statement::visit (visitor
* u
)
1139 u
->visit_null_statement (this);
1143 expr_statement::visit (visitor
* u
)
1145 u
->visit_expr_statement (this);
1149 return_statement::visit (visitor
* u
)
1151 u
->visit_return_statement (this);
1155 delete_statement::visit (visitor
* u
)
1157 u
->push_active_lvalue (this->value
);
1158 u
->visit_delete_statement (this);
1159 u
->pop_active_lvalue ();
1163 if_statement::visit (visitor
* u
)
1165 u
->visit_if_statement (this);
1169 next_statement::visit (visitor
* u
)
1171 u
->visit_next_statement (this);
1175 break_statement::visit (visitor
* u
)
1177 u
->visit_break_statement (this);
1181 continue_statement::visit (visitor
* u
)
1183 u
->visit_continue_statement (this);
1187 literal_string::visit(visitor
* u
)
1189 u
->visit_literal_string (this);
1193 literal_number::visit(visitor
* u
)
1195 u
->visit_literal_number (this);
1199 binary_expression::visit (visitor
* u
)
1201 u
->visit_binary_expression (this);
1205 unary_expression::visit (visitor
* u
)
1207 u
->visit_unary_expression (this);
1211 pre_crement::visit (visitor
* u
)
1213 u
->push_active_lvalue (this->operand
);
1214 u
->visit_pre_crement (this);
1215 u
->pop_active_lvalue ();
1219 post_crement::visit (visitor
* u
)
1221 u
->push_active_lvalue (this->operand
);
1222 u
->visit_post_crement (this);
1223 u
->pop_active_lvalue ();
1227 logical_or_expr::visit (visitor
* u
)
1229 u
->visit_logical_or_expr (this);
1233 logical_and_expr::visit (visitor
* u
)
1235 u
->visit_logical_and_expr (this);
1239 array_in::visit (visitor
* u
)
1241 u
->visit_array_in (this);
1245 comparison::visit (visitor
* u
)
1247 u
->visit_comparison (this);
1251 concatenation::visit (visitor
* u
)
1253 u
->visit_concatenation (this);
1257 ternary_expression::visit (visitor
* u
)
1259 u
->visit_ternary_expression (this);
1263 assignment::visit (visitor
* u
)
1265 u
->push_active_lvalue (this->left
);
1266 u
->visit_assignment (this);
1267 u
->pop_active_lvalue ();
1271 symbol::visit (visitor
* u
)
1273 u
->visit_symbol (this);
1277 target_symbol::visit (visitor
* u
)
1279 u
->visit_target_symbol(this);
1283 target_symbol::visit_components (visitor
* u
)
1285 for (unsigned i
= 0; i
< components
.size(); ++i
)
1286 if (components
[i
].type
== comp_expression_array_index
)
1287 components
[i
].expr_index
->visit (u
);
1291 target_symbol::visit_components (update_visitor
* u
)
1293 for (unsigned i
= 0; i
< components
.size(); ++i
)
1294 if (components
[i
].type
== comp_expression_array_index
)
1295 u
->replace (components
[i
].expr_index
);
1299 cast_op::visit (visitor
* u
)
1301 u
->visit_cast_op(this);
1305 arrayindex::visit (visitor
* u
)
1307 u
->visit_arrayindex (this);
1311 functioncall::visit (visitor
* u
)
1313 u
->visit_functioncall (this);
1317 print_format::visit (visitor
*u
)
1319 u
->visit_print_format (this);
1323 stat_op::visit (visitor
*u
)
1325 u
->visit_stat_op (this);
1329 hist_op::visit (visitor
*u
)
1331 u
->visit_hist_op (this);
1335 indexable::print_indexable (std::ostream
& o
) const
1338 const hist_op
*hist
;
1339 classify_const_indexable(this, sym
, hist
);
1350 indexable::visit_indexable (visitor
* u
)
1354 classify_indexable(this, sym
, hist
);
1366 indexable::is_symbol(symbol
*& sym_out
)
1373 indexable::is_hist_op(hist_op
*& hist_out
)
1380 indexable::is_const_symbol(const symbol
*& sym_out
) const
1387 indexable::is_const_hist_op(const hist_op
*& hist_out
) const
1394 symbol::is_symbol(symbol
*& sym_out
)
1401 symbol::is_const_symbol(const symbol
*& sym_out
) const
1408 symbol::get_tok() const
1414 hist_op::is_hist_op(hist_op
*& hist_out
)
1421 hist_op::is_const_hist_op(const hist_op
*& hist_out
) const
1428 hist_op::get_tok() const
1434 classify_indexable(indexable
* ix
,
1435 symbol
*& array_out
,
1436 hist_op
*& hist_out
)
1440 if (!(ix
->is_symbol (array_out
) || ix
->is_hist_op (hist_out
)))
1441 throw semantic_error("Expecting symbol or histogram operator", ix
->get_tok());
1442 if (ix
&& !(hist_out
|| array_out
))
1443 throw semantic_error("Failed to classify indexable", ix
->get_tok());
1447 classify_const_indexable(const indexable
* ix
,
1448 const symbol
*& array_out
,
1449 const hist_op
*& hist_out
)
1453 if (!(ix
->is_const_symbol(array_out
) || ix
->is_const_hist_op(hist_out
)))
1454 throw semantic_error("Expecting symbol or histogram operator", ix
->get_tok());
1457 // ------------------------------------------------------------------------
1460 visitor::is_active_lvalue(expression
*e
)
1462 for (unsigned i
= 0; i
< active_lvalues
.size(); ++i
)
1464 if (active_lvalues
[i
] == e
)
1471 visitor::push_active_lvalue(expression
*e
)
1473 active_lvalues
.push_back(e
);
1477 visitor::pop_active_lvalue()
1479 assert(!active_lvalues
.empty());
1480 active_lvalues
.pop_back();
1485 // ------------------------------------------------------------------------
1488 traversing_visitor::visit_block (block
* s
)
1490 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
1491 s
->statements
[i
]->visit (this);
1495 traversing_visitor::visit_embeddedcode (embeddedcode
*)
1500 traversing_visitor::visit_null_statement (null_statement
*)
1505 traversing_visitor::visit_expr_statement (expr_statement
* s
)
1507 s
->value
->visit (this);
1511 traversing_visitor::visit_if_statement (if_statement
* s
)
1513 s
->condition
->visit (this);
1514 s
->thenblock
->visit (this);
1516 s
->elseblock
->visit (this);
1520 traversing_visitor::visit_for_loop (for_loop
* s
)
1522 if (s
->init
) s
->init
->visit (this);
1523 s
->cond
->visit (this);
1524 if (s
->incr
) s
->incr
->visit (this);
1525 s
->block
->visit (this);
1529 traversing_visitor::visit_foreach_loop (foreach_loop
* s
)
1531 symbol
*array
= NULL
;
1532 hist_op
*hist
= NULL
;
1533 classify_indexable (s
->base
, array
, hist
);
1539 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1540 s
->indexes
[i
]->visit (this);
1543 s
->limit
->visit (this);
1545 s
->block
->visit (this);
1549 traversing_visitor::visit_return_statement (return_statement
* s
)
1551 s
->value
->visit (this);
1555 traversing_visitor::visit_delete_statement (delete_statement
* s
)
1557 s
->value
->visit (this);
1561 traversing_visitor::visit_next_statement (next_statement
*)
1566 traversing_visitor::visit_break_statement (break_statement
*)
1571 traversing_visitor::visit_continue_statement (continue_statement
*)
1576 traversing_visitor::visit_literal_string (literal_string
*)
1581 traversing_visitor::visit_literal_number (literal_number
*)
1586 traversing_visitor::visit_binary_expression (binary_expression
* e
)
1588 e
->left
->visit (this);
1589 e
->right
->visit (this);
1593 traversing_visitor::visit_unary_expression (unary_expression
* e
)
1595 e
->operand
->visit (this);
1599 traversing_visitor::visit_pre_crement (pre_crement
* e
)
1601 e
->operand
->visit (this);
1605 traversing_visitor::visit_post_crement (post_crement
* e
)
1607 e
->operand
->visit (this);
1612 traversing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
1614 e
->left
->visit (this);
1615 e
->right
->visit (this);
1619 traversing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
1621 e
->left
->visit (this);
1622 e
->right
->visit (this);
1626 traversing_visitor::visit_array_in (array_in
* e
)
1628 e
->operand
->visit (this);
1632 traversing_visitor::visit_comparison (comparison
* e
)
1634 e
->left
->visit (this);
1635 e
->right
->visit (this);
1639 traversing_visitor::visit_concatenation (concatenation
* e
)
1641 e
->left
->visit (this);
1642 e
->right
->visit (this);
1646 traversing_visitor::visit_ternary_expression (ternary_expression
* e
)
1648 e
->cond
->visit (this);
1649 e
->truevalue
->visit (this);
1650 e
->falsevalue
->visit (this);
1654 traversing_visitor::visit_assignment (assignment
* e
)
1656 e
->left
->visit (this);
1657 e
->right
->visit (this);
1661 traversing_visitor::visit_symbol (symbol
*)
1666 traversing_visitor::visit_target_symbol (target_symbol
* e
)
1668 e
->visit_components (this);
1672 traversing_visitor::visit_cast_op (cast_op
* e
)
1674 e
->operand
->visit (this);
1675 e
->visit_components (this);
1679 traversing_visitor::visit_arrayindex (arrayindex
* e
)
1681 for (unsigned i
=0; i
<e
->indexes
.size(); i
++)
1682 e
->indexes
[i
]->visit (this);
1684 symbol
*array
= NULL
;
1685 hist_op
*hist
= NULL
;
1686 classify_indexable(e
->base
, array
, hist
);
1688 return array
->visit(this);
1690 return hist
->visit(this);
1694 traversing_visitor::visit_functioncall (functioncall
* e
)
1696 for (unsigned i
=0; i
<e
->args
.size(); i
++)
1697 e
->args
[i
]->visit (this);
1701 traversing_visitor::visit_print_format (print_format
* e
)
1703 for (unsigned i
=0; i
<e
->args
.size(); i
++)
1704 e
->args
[i
]->visit (this);
1706 e
->hist
->visit(this);
1710 traversing_visitor::visit_stat_op (stat_op
* e
)
1712 e
->stat
->visit (this);
1716 traversing_visitor::visit_hist_op (hist_op
* e
)
1718 e
->stat
->visit (this);
1723 functioncall_traversing_visitor::visit_functioncall (functioncall
* e
)
1725 traversing_visitor::visit_functioncall (e
);
1727 // prevent infinite recursion
1728 if (traversed
.find (e
->referent
) == traversed
.end ())
1730 traversed
.insert (e
->referent
);
1732 functiondecl
* last_current_function
= current_function
;
1733 current_function
= e
->referent
;
1734 e
->referent
->body
->visit (this);
1735 current_function
= last_current_function
;
1741 varuse_collecting_visitor::visit_embeddedcode (embeddedcode
*s
)
1743 assert (current_function
); // only they get embedded code
1745 // Don't allow embedded C functions in unprivileged mode unless
1746 // they are tagged with /* unprivileged */
1747 if (session
.unprivileged
&& s
->code
.find ("/* unprivileged */") == string::npos
)
1748 throw semantic_error ("function may not be used when --unprivileged is specified",
1749 current_function
->tok
);
1751 // We want to elide embedded-C functions when possible. For
1752 // example, each $target variable access is expanded to an
1753 // embedded-C function call. Yet, for safety reasons, we should
1754 // presume that embedded-C functions have intentional side-effects.
1756 // To tell these two types of functions apart, we apply a
1757 // Kludge(tm): we look for a magic string within the function body.
1758 // $target variables as rvalues will have this; lvalues won't.
1759 // Also, explicit side-effect-free tapset functions will have this.
1761 if (s
->code
.find ("/* pure */") != string::npos
)
1764 embedded_seen
= true;
1768 varuse_collecting_visitor::visit_target_symbol (target_symbol
*e
)
1770 // Still-unresolved target symbol assignments get treated as
1771 // generating side-effects like embedded-C, to prevent premature
1772 // elision and later error message suppression (PR5516). rvalue use
1773 // of unresolved target symbols is OTOH not considered a side-effect.
1775 if (is_active_lvalue (e
))
1776 embedded_seen
= true;
1778 functioncall_traversing_visitor::visit_target_symbol (e
);
1782 varuse_collecting_visitor::visit_cast_op (cast_op
*e
)
1784 // As with target_symbols, unresolved cast assignments need to preserved
1785 // for later error handling.
1786 if (is_active_lvalue (e
))
1787 embedded_seen
= true;
1789 functioncall_traversing_visitor::visit_cast_op (e
);
1793 varuse_collecting_visitor::visit_print_format (print_format
* e
)
1795 // NB: Instead of being top-level statements, "print" and "printf"
1796 // are implemented as statement-expressions containing a
1797 // print_format. They have side-effects, but not via the
1798 // embedded-code detection method above.
1800 // But sprint and sprintf don't have side-effects.
1802 if (e
->print_to_stream
)
1803 embedded_seen
= true; // a proxy for "has unknown side-effects"
1805 functioncall_traversing_visitor::visit_print_format (e
);
1810 varuse_collecting_visitor::visit_assignment (assignment
*e
)
1812 if (e
->op
== "=" || e
->op
== "<<<") // pure writes
1814 expression
* last_lvalue
= current_lvalue
;
1815 current_lvalue
= e
->left
; // leave a mark for ::visit_symbol
1816 functioncall_traversing_visitor::visit_assignment (e
);
1817 current_lvalue
= last_lvalue
;
1819 else // read-modify-writes
1821 expression
* last_lrvalue
= current_lrvalue
;
1822 current_lrvalue
= e
->left
; // leave a mark for ::visit_symbol
1823 functioncall_traversing_visitor::visit_assignment (e
);
1824 current_lrvalue
= last_lrvalue
;
1829 varuse_collecting_visitor::visit_symbol (symbol
*e
)
1831 if (e
->referent
== 0)
1832 throw semantic_error ("symbol without referent", e
->tok
);
1834 // We could handle initialized globals by marking them as "written".
1835 // However, this current visitor may be called for a function or
1836 // probe body, from the point of view of which this global is
1837 // already initialized, so not written.
1839 if (e->referent->init)
1840 written.insert (e->referent);
1843 if (current_lvalue
== e
|| current_lrvalue
== e
)
1845 written
.insert (e
->referent
);
1846 // clog << "write ";
1848 if (current_lvalue
!= e
|| current_lrvalue
== e
)
1850 read
.insert (e
->referent
);
1853 // clog << *e->tok << endl;
1856 // NB: stat_op need not be overridden, since it will get to
1857 // visit_symbol and only as a possible rvalue.
1861 varuse_collecting_visitor::visit_arrayindex (arrayindex
*e
)
1863 // Hooking this callback is necessary because of the hacky
1864 // statistics representation. For the expression "i[4] = 5", the
1865 // incoming lvalue will point to this arrayindex. However, the
1866 // symbol corresponding to the "i[4]" is multiply inherited with
1867 // arrayindex. If the symbol base part of this object is not at
1868 // offset 0, then static_cast<symbol*>(e) may result in a different
1869 // address, and not match lvalue by number when we recurse that way.
1870 // So we explicitly override the incoming lvalue/lrvalue values to
1871 // point at the embedded objects' actual base addresses.
1873 expression
* last_lrvalue
= current_lrvalue
;
1874 expression
* last_lvalue
= current_lvalue
;
1876 symbol
*array
= NULL
;
1877 hist_op
*hist
= NULL
;
1878 classify_indexable(e
->base
, array
, hist
);
1882 if (current_lrvalue
== e
) current_lrvalue
= array
;
1883 if (current_lvalue
== e
) current_lvalue
= array
;
1884 functioncall_traversing_visitor::visit_arrayindex (e
);
1888 if (current_lrvalue
== e
) current_lrvalue
= hist
->stat
;
1889 if (current_lvalue
== e
) current_lvalue
= hist
->stat
;
1890 functioncall_traversing_visitor::visit_arrayindex (e
);
1893 current_lrvalue
= last_lrvalue
;
1894 current_lvalue
= last_lvalue
;
1899 varuse_collecting_visitor::visit_pre_crement (pre_crement
*e
)
1901 expression
* last_lrvalue
= current_lrvalue
;
1902 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
1903 functioncall_traversing_visitor::visit_pre_crement (e
);
1904 current_lrvalue
= last_lrvalue
;
1908 varuse_collecting_visitor::visit_post_crement (post_crement
*e
)
1910 expression
* last_lrvalue
= current_lrvalue
;
1911 current_lrvalue
= e
->operand
; // leave a mark for ::visit_symbol
1912 functioncall_traversing_visitor::visit_post_crement (e
);
1913 current_lrvalue
= last_lrvalue
;
1917 varuse_collecting_visitor::visit_foreach_loop (foreach_loop
* s
)
1919 // NB: we duplicate so don't bother call
1920 // functioncall_traversing_visitor::visit_foreach_loop (s);
1922 symbol
*array
= NULL
;
1923 hist_op
*hist
= NULL
;
1924 classify_indexable (s
->base
, array
, hist
);
1930 // If the collection is sorted, imply a "write" access to the
1931 // array in addition to the "read" one already noted above.
1932 if (s
->sort_direction
)
1934 symbol
*array
= NULL
;
1935 hist_op
*hist
= NULL
;
1936 classify_indexable (s
->base
, array
, hist
);
1937 if (array
) this->written
.insert (array
->referent
);
1938 // XXX: Can hist_op iterations be sorted?
1941 // NB: don't forget to visit the index expressions, which are lvalues.
1942 for (unsigned i
=0; i
<s
->indexes
.size(); i
++)
1944 expression
* last_lvalue
= current_lvalue
;
1945 current_lvalue
= s
->indexes
[i
]; // leave a mark for ::visit_symbol
1946 s
->indexes
[i
]->visit (this);
1947 current_lvalue
= last_lvalue
;
1951 s
->limit
->visit (this);
1953 s
->block
->visit (this);
1958 varuse_collecting_visitor::visit_delete_statement (delete_statement
* s
)
1960 // Ideally, this would be treated like an assignment: a plain write
1961 // to the underlying value ("lvalue"). XXX: However, the
1962 // optimization pass is not smart enough to remove an unneeded
1963 // "delete" yet, so we pose more like a *crement ("lrvalue"). This
1964 // should protect the underlying value from optimizional mischief.
1965 expression
* last_lrvalue
= current_lrvalue
;
1966 current_lrvalue
= s
->value
; // leave a mark for ::visit_symbol
1967 functioncall_traversing_visitor::visit_delete_statement (s
);
1968 current_lrvalue
= last_lrvalue
;
1972 varuse_collecting_visitor::side_effect_free ()
1974 return (written
.empty() && !embedded_seen
);
1979 varuse_collecting_visitor::side_effect_free_wrt (const set
<vardecl
*>& vars
)
1981 // A looser notion of side-effect-freeness with respect to a given
1982 // list of variables.
1984 // That's useful because the written list may consist of local
1985 // variables of called functions. But visible side-effects only
1986 // occur if the client's locals, or any globals are written-to.
1988 set
<vardecl
*> intersection
;
1989 insert_iterator
<set
<vardecl
*> > int_it (intersection
, intersection
.begin());
1990 set_intersection (written
.begin(), written
.end(),
1991 vars
.begin(), vars
.end(),
1994 return (intersection
.empty() && !embedded_seen
);
2000 // ------------------------------------------------------------------------
2003 throwing_visitor::throwing_visitor (const std::string
& m
): msg (m
) {}
2004 throwing_visitor::throwing_visitor (): msg ("invalid element") {}
2008 throwing_visitor::throwone (const token
* t
)
2010 throw semantic_error (msg
, t
);
2014 throwing_visitor::visit_block (block
* s
)
2020 throwing_visitor::visit_embeddedcode (embeddedcode
* s
)
2026 throwing_visitor::visit_null_statement (null_statement
* s
)
2032 throwing_visitor::visit_expr_statement (expr_statement
* s
)
2038 throwing_visitor::visit_if_statement (if_statement
* s
)
2044 throwing_visitor::visit_for_loop (for_loop
* s
)
2050 throwing_visitor::visit_foreach_loop (foreach_loop
* s
)
2056 throwing_visitor::visit_return_statement (return_statement
* s
)
2062 throwing_visitor::visit_delete_statement (delete_statement
* s
)
2068 throwing_visitor::visit_next_statement (next_statement
* s
)
2074 throwing_visitor::visit_break_statement (break_statement
* s
)
2080 throwing_visitor::visit_continue_statement (continue_statement
* s
)
2086 throwing_visitor::visit_literal_string (literal_string
* e
)
2092 throwing_visitor::visit_literal_number (literal_number
* e
)
2098 throwing_visitor::visit_binary_expression (binary_expression
* e
)
2104 throwing_visitor::visit_unary_expression (unary_expression
* e
)
2110 throwing_visitor::visit_pre_crement (pre_crement
* e
)
2116 throwing_visitor::visit_post_crement (post_crement
* e
)
2123 throwing_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2129 throwing_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2135 throwing_visitor::visit_array_in (array_in
* e
)
2141 throwing_visitor::visit_comparison (comparison
* e
)
2147 throwing_visitor::visit_concatenation (concatenation
* e
)
2153 throwing_visitor::visit_ternary_expression (ternary_expression
* e
)
2159 throwing_visitor::visit_assignment (assignment
* e
)
2165 throwing_visitor::visit_symbol (symbol
* e
)
2171 throwing_visitor::visit_target_symbol (target_symbol
* e
)
2177 throwing_visitor::visit_cast_op (cast_op
* e
)
2183 throwing_visitor::visit_arrayindex (arrayindex
* e
)
2189 throwing_visitor::visit_functioncall (functioncall
* e
)
2195 throwing_visitor::visit_print_format (print_format
* e
)
2201 throwing_visitor::visit_stat_op (stat_op
* e
)
2207 throwing_visitor::visit_hist_op (hist_op
* e
)
2213 // ------------------------------------------------------------------------
2217 update_visitor::visit_block (block
* s
)
2219 for (unsigned i
= 0; i
< s
->statements
.size(); ++i
)
2220 replace (s
->statements
[i
]);
2225 update_visitor::visit_embeddedcode (embeddedcode
* s
)
2231 update_visitor::visit_null_statement (null_statement
* s
)
2237 update_visitor::visit_expr_statement (expr_statement
* s
)
2244 update_visitor::visit_if_statement (if_statement
* s
)
2246 replace (s
->condition
);
2247 replace (s
->thenblock
);
2248 replace (s
->elseblock
);
2253 update_visitor::visit_for_loop (for_loop
* s
)
2263 update_visitor::visit_foreach_loop (foreach_loop
* s
)
2265 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
2266 replace (s
->indexes
[i
]);
2274 update_visitor::visit_return_statement (return_statement
* s
)
2281 update_visitor::visit_delete_statement (delete_statement
* s
)
2288 update_visitor::visit_next_statement (next_statement
* s
)
2294 update_visitor::visit_break_statement (break_statement
* s
)
2300 update_visitor::visit_continue_statement (continue_statement
* s
)
2306 update_visitor::visit_literal_string (literal_string
* e
)
2312 update_visitor::visit_literal_number (literal_number
* e
)
2318 update_visitor::visit_binary_expression (binary_expression
* e
)
2326 update_visitor::visit_unary_expression (unary_expression
* e
)
2328 replace (e
->operand
);
2333 update_visitor::visit_pre_crement (pre_crement
* e
)
2335 replace (e
->operand
);
2340 update_visitor::visit_post_crement (post_crement
* e
)
2342 replace (e
->operand
);
2348 update_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2356 update_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2364 update_visitor::visit_array_in (array_in
* e
)
2366 replace (e
->operand
);
2371 update_visitor::visit_comparison (comparison
* e
)
2379 update_visitor::visit_concatenation (concatenation
* e
)
2387 update_visitor::visit_ternary_expression (ternary_expression
* e
)
2390 replace (e
->truevalue
);
2391 replace (e
->falsevalue
);
2396 update_visitor::visit_assignment (assignment
* e
)
2404 update_visitor::visit_symbol (symbol
* e
)
2410 update_visitor::visit_target_symbol (target_symbol
* e
)
2412 e
->visit_components (this);
2417 update_visitor::visit_cast_op (cast_op
* e
)
2419 replace (e
->operand
);
2420 e
->visit_components (this);
2425 update_visitor::visit_arrayindex (arrayindex
* e
)
2428 for (unsigned i
= 0; i
< e
->indexes
.size(); ++i
)
2429 replace (e
->indexes
[i
]);
2434 update_visitor::visit_functioncall (functioncall
* e
)
2436 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
2437 replace (e
->args
[i
]);
2442 update_visitor::visit_print_format (print_format
* e
)
2444 for (unsigned i
= 0; i
< e
->args
.size(); ++i
)
2445 replace (e
->args
[i
]);
2451 update_visitor::visit_stat_op (stat_op
* e
)
2458 update_visitor::visit_hist_op (hist_op
* e
)
2464 template <> indexable
*
2465 update_visitor::require
<indexable
> (indexable
* src
, bool clearok
)
2467 indexable
*dst
= NULL
;
2470 symbol
*array_src
=NULL
;
2471 hist_op
*hist_src
=NULL
;
2473 classify_indexable(src
, array_src
, hist_src
);
2476 dst
= require (array_src
);
2478 dst
= require (hist_src
);
2479 assert(clearok
|| dst
);
2485 // ------------------------------------------------------------------------
2489 deep_copy_visitor::visit_block (block
* s
)
2491 update_visitor::visit_block(new block(*s
));
2495 deep_copy_visitor::visit_embeddedcode (embeddedcode
* s
)
2497 update_visitor::visit_embeddedcode(new embeddedcode(*s
));
2501 deep_copy_visitor::visit_null_statement (null_statement
* s
)
2503 update_visitor::visit_null_statement(new null_statement(*s
));
2507 deep_copy_visitor::visit_expr_statement (expr_statement
* s
)
2509 update_visitor::visit_expr_statement(new expr_statement(*s
));
2513 deep_copy_visitor::visit_if_statement (if_statement
* s
)
2515 update_visitor::visit_if_statement(new if_statement(*s
));
2519 deep_copy_visitor::visit_for_loop (for_loop
* s
)
2521 update_visitor::visit_for_loop(new for_loop(*s
));
2525 deep_copy_visitor::visit_foreach_loop (foreach_loop
* s
)
2527 update_visitor::visit_foreach_loop(new foreach_loop(*s
));
2531 deep_copy_visitor::visit_return_statement (return_statement
* s
)
2533 update_visitor::visit_return_statement(new return_statement(*s
));
2537 deep_copy_visitor::visit_delete_statement (delete_statement
* s
)
2539 update_visitor::visit_delete_statement(new delete_statement(*s
));
2543 deep_copy_visitor::visit_next_statement (next_statement
* s
)
2545 update_visitor::visit_next_statement(new next_statement(*s
));
2549 deep_copy_visitor::visit_break_statement (break_statement
* s
)
2551 update_visitor::visit_break_statement(new break_statement(*s
));
2555 deep_copy_visitor::visit_continue_statement (continue_statement
* s
)
2557 update_visitor::visit_continue_statement(new continue_statement(*s
));
2561 deep_copy_visitor::visit_literal_string (literal_string
* e
)
2563 update_visitor::visit_literal_string(new literal_string(*e
));
2567 deep_copy_visitor::visit_literal_number (literal_number
* e
)
2569 update_visitor::visit_literal_number(new literal_number(*e
));
2573 deep_copy_visitor::visit_binary_expression (binary_expression
* e
)
2575 update_visitor::visit_binary_expression(new binary_expression(*e
));
2579 deep_copy_visitor::visit_unary_expression (unary_expression
* e
)
2581 update_visitor::visit_unary_expression(new unary_expression(*e
));
2585 deep_copy_visitor::visit_pre_crement (pre_crement
* e
)
2587 update_visitor::visit_pre_crement(new pre_crement(*e
));
2591 deep_copy_visitor::visit_post_crement (post_crement
* e
)
2593 update_visitor::visit_post_crement(new post_crement(*e
));
2598 deep_copy_visitor::visit_logical_or_expr (logical_or_expr
* e
)
2600 update_visitor::visit_logical_or_expr(new logical_or_expr(*e
));
2604 deep_copy_visitor::visit_logical_and_expr (logical_and_expr
* e
)
2606 update_visitor::visit_logical_and_expr(new logical_and_expr(*e
));
2610 deep_copy_visitor::visit_array_in (array_in
* e
)
2612 update_visitor::visit_array_in(new array_in(*e
));
2616 deep_copy_visitor::visit_comparison (comparison
* e
)
2618 update_visitor::visit_comparison(new comparison(*e
));
2622 deep_copy_visitor::visit_concatenation (concatenation
* e
)
2624 update_visitor::visit_concatenation(new concatenation(*e
));
2628 deep_copy_visitor::visit_ternary_expression (ternary_expression
* e
)
2630 update_visitor::visit_ternary_expression(new ternary_expression(*e
));
2634 deep_copy_visitor::visit_assignment (assignment
* e
)
2636 update_visitor::visit_assignment(new assignment(*e
));
2640 deep_copy_visitor::visit_symbol (symbol
* e
)
2642 symbol
* n
= new symbol(*e
);
2643 n
->referent
= NULL
; // don't copy!
2644 update_visitor::visit_symbol(n
);
2648 deep_copy_visitor::visit_target_symbol (target_symbol
* e
)
2650 target_symbol
* n
= new target_symbol(*e
);
2651 n
->referent
= NULL
; // don't copy!
2652 update_visitor::visit_target_symbol(n
);
2656 deep_copy_visitor::visit_cast_op (cast_op
* e
)
2658 update_visitor::visit_cast_op(new cast_op(*e
));
2662 deep_copy_visitor::visit_arrayindex (arrayindex
* e
)
2664 update_visitor::visit_arrayindex(new arrayindex(*e
));
2668 deep_copy_visitor::visit_functioncall (functioncall
* e
)
2670 functioncall
* n
= new functioncall(*e
);
2671 n
->referent
= NULL
; // don't copy!
2672 update_visitor::visit_functioncall(n
);
2676 deep_copy_visitor::visit_print_format (print_format
* e
)
2678 update_visitor::visit_print_format(new print_format(*e
));
2682 deep_copy_visitor::visit_stat_op (stat_op
* e
)
2684 update_visitor::visit_stat_op(new stat_op(*e
));
2688 deep_copy_visitor::visit_hist_op (hist_op
* e
)
2690 update_visitor::visit_hist_op(new hist_op(*e
));
2693 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */