4

I wrote a compiler which is already working decently. It checks that all non-void methods do have at least one return statement, but it does not check whether all code paths within a non-void method return a value. So, for example, if I have the following code:

int function(bool a) 
{
     if(a){
         return 5;
     }
}

It will compile "fine" and if a is false, control will drop off the function and keep executing whatever is defined under the function.

Is there some algorithm I can use to perform this check so that I always know if a method is always returning a value? Or will I need to reinvent the wheel?

Alex Terreaux
  • 1,881
  • 5
  • 23
  • 39
  • For quite a few compiler tricks there are already algorithms available, including recommended internal representation etc. See e.g. http://en.wikipedia.org/wiki/Data-flow_analysis Reading commented source code of an existing compiler for the same language (looks like `C` in your case, so pick a http://stackoverflow.com/questions/584714/is-there-an-interpreter-for-c) is probably the best way to [self-answer](http://stackoverflow.com/help/self-answer) your otherwise unclear and broad question – xmojmr Nov 05 '14 at 16:57

1 Answers1

1

Functions cannot "fall off the end" and start executing code outside of their scope, regardless of whether they return a value or not. It's common to allow the final return statement to be omitted for functions which do not return a result (or in some undisciplined languages even for functions which do return a result), but the function needs to return in all cases.

Given that, the simplest way to produce error messages for non-void functions which fall of the end is:

  • The parser inserts a return statement at the end of the function body.

  • Dead code elimination is used to remove the inserted return statement in case it is unnecessary.

  • Typechecking is used to verify that all return statements, included the inserted one if it hasn't been deleted, are consistent with the return type of the function. (That is, since the generated return statement has no value, it can only be consistent with a void function.)

In this scenario, you need to delete unnecessary return statements before typechecking, or you'll end up with a lot of incorrect error messages. That requires some control flow analysis.

If that's too much work, you can issue the error message at run-time by compiling the no-value return statement as an error operation in the case that the function returns a value.

rici
  • 234,347
  • 28
  • 237
  • 341
  • While that does indeed fix the "fall off the end" issue, I don't think that was the question. The question, as I understood it, was how to produce a compile time error if not all of the paths return a value. – sepp2k Nov 05 '14 at 16:25
  • I do what you suggest for functions that don't return a value. But can't do the same for those that do, because I can't simply guess what the return value will be. Obviusly functions falling off the end shouldn't happen. What I was asking is how to detect this situation so that the compiler reports an error and halts compilation. – Alex Terreaux Nov 05 '14 at 16:26
  • @sepp2k Exactly, that is what I meant. – Alex Terreaux Nov 05 '14 at 16:27
  • The fall off the end issue is fixed the way @rici suggests. – Alex Terreaux Nov 05 '14 at 16:27
  • @sepp2k: I edited the answer to make it more clear that I was trying to answer the original question. Although I don't think that it is different from the previous answer, aside from some cosmetics. – rici Nov 05 '14 at 16:59
  • Thanks, now it makes more sense to me. – Alex Terreaux Nov 05 '14 at 17:08