The compiler is not involved in any of this. The IDE is handling everything instead.
When you double-click on a control at design-time, the Form Designer knows which control is being clicked on, as it is a live object in memory.
The Form Designer uses the control's RTTI and registered TComponentEditor
implementation (the VCL provides a default implementation if a user-defined implementation is not registered) to determine which event for that class type is the default event (in the case of TButton
, that is the OnClick
event), then uses the RTTI to check if that event already has a handler assigned to it.
If a handler is not assigned yet, the Form Designer uses the RTTI to read the control's Name
property and the event's declared name, concatenates them together (dropping the On
portion of the event name), and looks for a procedure of that name in the source code of the control's Owner
. If that procedure is not found, it is created at that time.
Once the Form Designer finds the procedure, it uses the RTTI to validate that the procedure matches the signature of the event, then assigns the procedure as the new event handler if needed before then finally jumping to the procedure implementation in the Code Editor.
If you click on the control's event in the Object Inspector and rename the handler, the corresponding procedure in the source code is renamed to match the new name, and any other events, even in other components, that were linked to that same procedure are updated via their RTTI to match the new name as well.
When compiling the project, the IDE first utilizes RTTI and registered component streaming routines to create a .DFM file that contains all of the various component property/event values. Then it invokes the compiler, which compiles the source code and links in the .DFM file as a binary resource into the final executable.
At runtime, the RTL parses the DFM resource, using RTTI and registered custom component streaming routines, to locate the various components and hook up their property/event values as needed.