From 81931eaba5dcd0727f33328bf4b0ff511b791990 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 31 Jul 2009 13:46:54 -0700 Subject: Refactor parser for target_symbol->components The components were being parsed for both target_symbols and cast_ops, so this change refactors that code into a single function. * parse.cxx (parser::parse_target_symbol_components): New. (parser::parse_symbol): Use the new function. --- parse.cxx | 72 ++++++++++++++++++++++++++------------------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) (limited to 'parse.cxx') diff --git a/parse.cxx b/parse.cxx index 35c78abe..29d97589 100644 --- a/parse.cxx +++ b/parse.cxx @@ -2346,27 +2346,8 @@ parser::parse_symbol () expect_unknown(tok_string, cop->module); } expect_op(")"); - while (true) - { - string c; - if (peek_op ("->")) - { - next(); - expect_ident_or_keyword (c); - cop->components.push_back - (make_pair (target_symbol::comp_struct_member, c)); - } - else if (peek_op ("[")) - { - next(); - expect_unknown (tok_number, c); - expect_op ("]"); - cop->components.push_back - (make_pair (target_symbol::comp_literal_array_index, c)); - } - else - break; - } + parse_target_symbol_components(cop); + // if there aren't any dereferences, then the cast is pointless if (cop->components.empty()) { @@ -2494,27 +2475,7 @@ parser::parse_symbol () target_symbol *tsym = new target_symbol; tsym->tok = t; tsym->base_name = name; - while (true) - { - string c; - if (peek_op ("->")) - { - next(); - expect_ident_or_keyword (c); - tsym->components.push_back - (make_pair (target_symbol::comp_struct_member, c)); - } - else if (peek_op ("[")) - { - next(); - expect_unknown (tok_number, c); - expect_op ("]"); - tsym->components.push_back - (make_pair (target_symbol::comp_literal_array_index, c)); - } - else - break; - } + parse_target_symbol_components(tsym); return tsym; } @@ -2604,4 +2565,31 @@ parser::parse_symbol () return sym; } + +void +parser::parse_target_symbol_components (target_symbol* e) +{ + while (true) + { + string c; + if (peek_op ("->")) + { + next(); + expect_ident_or_keyword (c); + e->components.push_back + (make_pair (target_symbol::comp_struct_member, c)); + } + else if (peek_op ("[")) + { + next(); + expect_unknown (tok_number, c); + expect_op ("]"); + e->components.push_back + (make_pair (target_symbol::comp_literal_array_index, c)); + } + else + break; + } +} + /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ -- cgit From c67847a0d05f8c7207513e79378fc8d84563e109 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 31 Jul 2009 17:00:09 -0700 Subject: Make a real type for target_symbol->components Now the dereferences on target_symbol and cast_op are tracked with a struct instead of just a generic pair. The first immediate benefit is that we can track the token for more exact error reporting. * staptree.h (target_symbol): Add a new component type. * staptree.cxx (target_symbol::component::print): New. (operator<<(ostream&, target_symbol::component&): New. (target_symbol::print): Adapt component printing. (cast_op::print): Ditto. * parse.cxx (parser::parse_target_symbol_components): Adapt to the new component construction. * dwflpp.cxx (dwflpp::find_struct_member): take the component as a parameter for a better token in error messages (dwflpp::translate_components): Adapt to the new component type. * tapsets.cxx (dwarf_var_expanding_visitor::visit_target_symbol): Don't overwrite the token in target_symbol saved errors. (tracepoint_var_expanding_visitor::visit_target_symbol_arg): Ditto. --- parse.cxx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'parse.cxx') diff --git a/parse.cxx b/parse.cxx index 29d97589..9d4a06a6 100644 --- a/parse.cxx +++ b/parse.cxx @@ -2571,21 +2571,20 @@ parser::parse_target_symbol_components (target_symbol* e) { while (true) { - string c; if (peek_op ("->")) { - next(); - expect_ident_or_keyword (c); - e->components.push_back - (make_pair (target_symbol::comp_struct_member, c)); + const token* t = next(); + string member; + expect_ident_or_keyword (member); + e->components.push_back (target_symbol::component(t, member)); } else if (peek_op ("[")) { - next(); - expect_unknown (tok_number, c); + const token* t = next(); + int64_t index; + expect_number (index); + e->components.push_back (target_symbol::component(t, index)); expect_op ("]"); - e->components.push_back - (make_pair (target_symbol::comp_literal_array_index, c)); } else break; -- cgit From 6fda2dff51c667a8c73545dd397b844108715310 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 3 Aug 2009 14:45:21 -0700 Subject: PR2049: support arbitrary $target-array indexing Rather than just numeric literals, we can now support arbitrary expressions for the index value. Note that loc2c won't allow this for noncontiguous arrays, as the access methods need to be statically computed, but for contiguous arrays and pointers-as-arrays it works just fine. * staptree.h (target_symbol::component): Add expression_array_index. * staptree.cxx (target_symbol::visit_components): New helper. (target_symbol::assert_no_components): Recognize new array type. (target_symbol::component::print): Print subexpressions. (traversing_visitor::visit_target_symbol, visit_cast_op): Visit the indexing components too. (varuse_collecting_visitor::visit_target_symbol): Ditto. (update_visitor::visit_target_symbol, visit_cast_op): Ditto. * elaborate.cxx (void_statement_reducer::visit_target_symbol): New. (void_statement_reducer::visit_cast_op): Save indexes too. * parse.cxx (parser::parse_target_symbol_components): Parse expressions. * tapsets.cxx (dwarf_var_expanding_visitor::visit_target_symbol): Pass expression-indexes as parameters (indexN) to the dwarf function. (dwarf_cast_expanding_visitor::visit_cast_op): Ditto. (tracepoint_var_expanding_visitor::visit_target_symbol_arg): Ditto. (sdt_var_expanding_visitor::visit_target_symbol): Visit the new @cast. * dwflpp.cxx (dwflpp::translate_components): Use THIS->indexN. * translate.cxx (c_unparser::visit_target_symbol): Correct error msg. * testsuite/systemtap.base/pointer_array.stp: Use a simple index. --- parse.cxx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'parse.cxx') diff --git a/parse.cxx b/parse.cxx index 9d4a06a6..41a13ca5 100644 --- a/parse.cxx +++ b/parse.cxx @@ -2581,9 +2581,12 @@ parser::parse_target_symbol_components (target_symbol* e) else if (peek_op ("[")) { const token* t = next(); - int64_t index; - expect_number (index); - e->components.push_back (target_symbol::component(t, index)); + expression* index = parse_expression(); + literal_number* ln = dynamic_cast(index); + if (ln) + e->components.push_back (target_symbol::component(t, ln->value)); + else + e->components.push_back (target_symbol::component(t, index)); expect_op ("]"); } else -- cgit