[fpc-devel] Proposal: Supporting initial references for weakexternal
Jeppe Johansen
jepjoh2 at es.aau.dk
Wed Jan 12 22:54:58 CET 2011
Den 12-01-2011 20:47, Jonas Maebe skrev:
>> The weakexternal directive already "defines" a symbol, it will exist at runtime, but it might be nil.
> What I meant is that there is a semantic difference between referencing a symbol that may or may not be defined somewhere else (regardless of whether a programmer or the linker creates the referenced symbol), and defining a new symbol. This is independent of whether a symbol is weak or not.
>
> Defining a weak symbol also exists, but is not yet supported by FPC. In fact, the example you gave would seem more logical to me as follows (assuming that FPC could define "weak" symbols using the "weak" modifier):
>
> procedure IRQHandler; weak;
> begin
> { default dummy implementation, the "NoHandler" from your example }
> end;
>
> procedure MyIRQHandler; public; alias: 'IRQHandler';
> begin
> { real implementation, overrides the weak definition of IRQHandler; if there
> is no such definition, the dummy one is used }
> end;
>
> (and of course, if we'd have a plain "weak" modifier, then "weakexternal;" could be "weak; external;", although this is annoying to still change because "weakexternal" support is already in released FPC versions)
>
>
> If you have no control over the original definition and it is not weak (maybe the reason you did that is because FPC does not yet support defining weak symbols), then we indeed need some way to define weak symbols whose "default value" is something else than nil, because I guess wrapping is not good enough (although it's not that bad, since it only results in an extra call in the case where the weak symbol is not replaced):
>
> procedure NoHandler;
> begin
> { dummy implementation }
> end;
>
> procedure MyIRQHandler; weak;
> begin
> { default behaviour: one extra call }
> NoHandler;
> end;
>
> { overriding implementation }
> procedure RealIRQHandler; public; alias: 'MyIRQHandler';
> begin
> { real code }
> end;
>
> I can't immediately think of a good syntax to directly define MyIRQHandler as a weak symbol with the address of NoHandler as default value. Maybe this, although I'm not 100% happy with it either:
>
> procedure MyIRQHandler; weak 'NoHandler';
Maybe something like instead? Requiring a symbol reference to a matching
implementation of a default value, like Paul suggested, would make the
solution more Pascal-like
procedure NoHandler;
begin
{ dummy implementation }
end;
{ Declaration NoHandler must exist and match }
procedure MyIRQHandler; weak NoHandler;
{ This will emit just a weak reference, the same as weakexternal; Not
guaranteed to be resolved after compiletime }
procedure MyIRQHandler2; weak;
This of course just introduces a new keyword weak, which was why I tried
to base it off the weakexternal keyword
More information about the fpc-devel
mailing list