How does the input file name get into gnat1?



In Frontend, we see Lib.Load.Load_Main_Source being called.
This is a parameterless procedure - so where does the file name come from?
The answer to this question requires a fair amount of digging in the GCC and GNAT source code:

The gcc compiler driver program determines which compiler proper to invoke and passes (a filtered version of) the command line with which it was called to the selected compiler, in our case gnat1.
The main program of gnat1 is written in C, see file main.c in the gcc subdirectory of the GCC source distribution,

int
main (int argc, char **argv)
{
  return toplev_main (argc, argv);
}
The input filename is passed from the main program's argv on to the function toplev_main (toplev.c) which calls the function decode_options (opts.c) which calls: After a few other initializations, toplev_main calls the function do_compile (also in toplev.c) which after further initializations executes this code:
  if (lang_dependent_init (main_input_filename))
    compile_file ();
The function lang_dependent_init (toplev.c) through some hooks and macros calls the function gnat_init (ada/gcc-interface/misc.c). Here, the contents of main_input_filename is copied to gnat_argv which is interfaced to Ada in the body of package Osint (see function Fill_Arg.) However, at least in the GCC version studied (gcc-4.6), Osint.Fill_Arg is subsequently mainly used for handling version and help switches but is not used for the actual unit name to compile.

Looking further, the function compile_file (toplev.c) calls lang_hooks.parse_file which again through some hooks and macros calls the function gnat_parse_file (ada/gcc-interface/misc.c). This function makes the transition from the C to the Ada world by calling

  _ada_gnat1drv ();
The Ada procedure Gnat1drv (ada/gnat1drv.adb) calls the procedure Scan_Compiler_Arguments from package Back_End. This procedure copies the input file name from save_argv (see above) using the procedure Add_File from package Osint. Add_File adds the file to the File_Names and File_Indexes in the private part of the Osint package spec. These variables are used by the function Next_Main_Source in package Osint.C which is called from Lib.Load.Load_Main_Source.