next up previous
Next: arg_list()Iterator with Up: Use of Expressions Previous: replace()

Statement::iterate_expressions()

The iterate_expressions() member function of the Statement base class is a virtual function which returns an Iterator<Expression> which can be used to iterate through all the top-level expressions in a Statement. The top-level expressions of a Statement are the roots of the expression trees in the Abstract Syntax Tree. For example, an assignment statement has two top-level expressions - the left-hand side of the assignment and the right-hand side. So, AssignmentStmt::iterate_expressions() will produce an Iterator<Expression> which will have two iterations. A DoStmt::iterate_expressions() will produce an Iterator<Expression> with four iterations, one each for the loop index, the initial expression, the limit expression and the stride expression.

For example, to collect all symbols used in the executable statements in all the program:

    // Look at all of the statements and the expressions in them and 
    // collect the variables which are used.

    // pgm is a ProgramUnit object.

    for (Iterator<Statement> st_iter = pgm->stmts().iterator(); 
                             st_iter.valid(); 
                             ++st_iter) {
        Statement & st = st_iter.current();

        for (Iterator<Expression> e_iter = st.iterate_expressions();
                                  e_iter.valid(); 
                                  ++e_iter) {
            Expression &e = e_iter.current();

            // Find all symbol references in e and add to used.
            collect_all_symbols( e, used );
        }

Notice that the Iterator<Expression> is used in exactly the same way as the Iterator<Statement>.



Jay Hoeflinger
Mon Apr 21 11:52:18 CDT 1997