First, let us consider the case where the current function contains function calls but no store to or load from non-local variables. Let us further assume that there is only one function call in the current function. In this case, any effects that the current function has will depend on any effects that the called function has. So the current function should inherit the class of the called function. With this fact, now we consider that the current function contains multiple function calls. Which class should be assigned to the current function? The effects of the current function will be the union of the effects of all the called functions. If one of the called function is unreusable, then the current function will have effects in the form of modification to non-local variables, and by definition the current function will also be unreusable. Similarly, if none of the called functions are unreusable but there is one or more which is pure, then the current function will have effects in the form of read from non-local variables, which means that it has to be pure. Only if there are no calls to unreusable or pure functions--in other words, all calls are to const functions--will the current function be const. So that is how we determine the class of the current function.
To determine the class of a called function, we apply the same method to it. This will present no problem if the call graph from the current function is in the form of a tree. In this case, the method will traverse the call graph to the functions at the leaves and propagate the results up the tree. However, cycles in the call graph present a problem, because the method described so far might potentially loop through these cycles infinitely when it cannot determine the class of the functions in the cycle. Nevertheless, in the case when all functions in the cycle do not contain any stores or loads, all functions in the cycle can be considered to be const, since there are no stores or loads in the whole cycle. We will consider cases of cycle with functions that may contain stores and loads in the next subsection.
|