Indirect reference in grammar.h


I am looking into source code and in grammar.h file, few production rules include indirect reference to parser’s return type e.g. in ProgramUnit and ActionStmt.
How these productions are different from others by using indirect reference?



I think an explanation for this can be found in the following comment in parse-tree.h.

"// These are the unavoidable recursively-defined productions of Fortran.

// Some references to the representations of their parses require

// indirection. The Indirect<> pointer wrapper class is used to

// enforce ownership semantics and non-nullability."

Consider the definition of action-stmt (copied from given below. You can see that action-stmt is recursively defined in terms of itself through the if-stmt. We know that C/C++ does not support recursively defined types (structs, tuples, variants). But it is possible to have a reference or pointer to the same type. Hence the use of the indirection.

"R515 action-stmt →

allocate-stmt | assignment-stmt | backspace-stmt | call-stmt |

close-stmt | continue-stmt | cycle-stmt | deallocate-stmt |

endfile-stmt | error-stop-stmt | event-post-stmt | event-wait-stmt |

exit-stmt | fail-image-stmt | flush-stmt | form-team-stmt |

goto-stmt | if-stmt | inquire-stmt | lock-stmt | nullify-stmt |

open-stmt | pointer-assignment-stmt | print-stmt | read-stmt |

return-stmt | rewind-stmt | stop-stmt | sync-all-stmt |

sync-images-stmt | sync-memory-stmt | sync-team-stmt | unlock-stmt |

wait-stmt | where-stmt | write-stmt | computed-goto-stmt | forall-stmt

R1139 if-stmt → IF ( scalar-logical-expr ) action-stmt"