Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/entfile.pas
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ interface
ibmoduleoptions = 85;
ibunitimportsyms = 86;
iborderedsymbols = 87;
ibreexportunits = 88;

ibmainname = 90;
ibsymtableoptions = 91;
Expand Down Expand Up @@ -524,6 +525,7 @@ function entry_name(nr : byte) : string;
ibmoduleoptions: entry_name:='ibmoduleoptions';
ibunitimportsyms: entry_name:='ibunitimportsyms';
iborderedsymbols: entry_name:='iborderedsymbols';
ibreexportunits: entry_name:='ibreexportunits';

ibmainname: entry_name:='ibmainname';
ibsymtableoptions: entry_name:='ibsymtableoptions';
Expand Down
36 changes: 35 additions & 1 deletion compiler/fmodule.pas
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ tused_unit = class(tlinkedlistitem)
unitsym : tunitsym;
aliases : TCmdStrList; { additional spellings of this dependency }
dependent_added : boolean;
{ `uses export X` in the interface: the units using this one see X's
interface too (connect_loaded_units) }
reexport : boolean;
{ added by connect_loaded_units because a used unit re-exports it;
naming it in a uses clause as well is no duplicate }
via_reexport : boolean;
{ added by --project-uses: naming it in the uses clause too is no
duplicate, and it is not the first explicit unit }
via_project : boolean;
constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
destructor destroy; override;
procedure check_hints;
Expand Down Expand Up @@ -1705,6 +1714,28 @@ implementation
procedure tmodule.allunitsused;
var
pu : tused_unit;

{ A unit that passes others on ("uses export") is used when a name of
one of them is: "uses Lumi" for TForm is no unused unit. }
function reexportused(m: tmodule; depth: longint): boolean;
var
rp : tused_unit;
begin
result:=false;
if depth>32 then
exit;
rp:=tused_unit(m.used_units.first);
while assigned(rp) do
begin
if rp.in_interface and rp.reexport and
(rp.u.moduleid<length(unitmap)) and
(unitmap[rp.u.moduleid].u=rp.u) and
((unitmap[rp.u.moduleid].refs>0) or reexportused(rp.u,depth+1)) then
exit(true);
rp:=tused_unit(rp.next);
end;
end;

begin
pu:=tused_unit(used_units.first);
while assigned(pu) do
Expand All @@ -1717,7 +1748,10 @@ implementation
this is for units with an initialization/finalization }
if (unitmap[pu.u.moduleid].refs=0) and
pu.in_uses and
((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
((pu.u.moduleflags * [mf_init,mf_finalize])=[]) and
{ passed on: used by the users of this unit }
not pu.reexport and
not reexportused(pu.u,0) then
CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
end;
pu:=tused_unit(pu.next);
Expand Down
52 changes: 52 additions & 0 deletions compiler/fppu.pas
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ tppumodule = class(tmodule)
procedure freederefunitimportsyms;
procedure writesourcefiles;
procedure writeusedunit(intf:boolean);
procedure writereexportunits;
procedure readreexportunits;
procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
procedure writederefmap;
procedure writederefdata;
Expand Down Expand Up @@ -1015,6 +1017,52 @@ implementation
end;


{ The interface units this unit passes on ("uses export"): their names, in
uses order. Part of the interface: it changes what the users of this
unit see, so it counts in the crc. Written only when there is one. }
procedure tppumodule.writereexportunits;
var
hp : tused_unit;
any : boolean;
begin
any:=false;
hp:=tused_unit(used_units.first);
while assigned(hp) do
begin
if hp.in_interface and hp.reexport then
begin
ppufile.putstring(hp.u.realmodulename^);
any:=true;
end;
hp:=tused_unit(hp.next);
end;
if any then
ppufile.writeentry(ibreexportunits);
end;


procedure tppumodule.readreexportunits;
var
hs : string;
hp : tused_unit;
begin
while not ppufile.endofentry do
begin
hs:=upper(ppufile.getstring);
hp:=tused_unit(used_units.first);
while assigned(hp) do
begin
if hp.u.modulename^=hs then
begin
hp.reexport:=true;
break;
end;
hp:=tused_unit(hp.next);
end;
end;
end;


procedure tppumodule.writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
var
hcontainer : tlinkcontainer;
Expand Down Expand Up @@ -1658,6 +1706,8 @@ implementation
{$ENDIF}
ibloadunit :
readloadunit;
ibreexportunits :
readreexportunits;
iblinkunitofiles :
readlinkcontainer(LinkUnitOFiles);
iblinkunitstaticlibs :
Expand Down Expand Up @@ -1808,6 +1858,7 @@ implementation

{ write interface uses }
writeusedunit(true);
writereexportunits;

{ write the objectfiles and libraries that come for this unit,
preserve the containers because they are still needed to load
Expand Down Expand Up @@ -1988,6 +2039,7 @@ implementation

{ the interface units affect the crc }
writeusedunit(true);
writereexportunits;

{ deref data of interface that affect the crc }
derefdata.reset;
Expand Down
4 changes: 4 additions & 0 deletions compiler/globals.pas
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ tpendingstate = record
{ ordered explicit program/library unit prefix required by
--required-first-unit }
requiredfirstunit : TIDString;
{ --project-uses=A,B: units every unit and program uses, as the first
of its uses clause (a project-wide uses) }
projectuses : ansistring;
// lowercased glob patterns from `--rttiexpose=` CLI flags;
// per-unit patterns from `{$rttiexpose}` directive live on tmodule
cli_rtti_expose_patterns : TCmdStrList;
Expand Down Expand Up @@ -1870,6 +1873,7 @@ tinitdoneentry=record
namespacelist:=TCmdStrList.Create;
pinnedunitfiles:=TLinkStrMap.Create;
requiredfirstunit:='';
projectuses:='';
cli_rtti_expose_patterns:=TCmdStrList.Create;
premodule_namespacelist:=TCmdStrList.Create;
current_namespacelist:=Nil;
Expand Down
4 changes: 4 additions & 0 deletions compiler/msg/errore.msg
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,10 @@ parser_e_align_bitalign_exclusive=03406_E_"align" and "bitalign" are mutually ex
parser_e_weak_threadlocal_property=03447_E_A weak field inside a class threadvar requires a property accessor method for RTTI
% The address of the field must be obtained for the calling thread.

parser_e_reexport_uses_in_implementation=03448_E_A used unit can be re-exported ("export $1") only in the interface uses clause
% "uses export" passes the used units on to the units that use this one, which see
% only its interface. In the implementation uses clause there is nothing to pass on.

parser_e_unsupported_weak_declaration=03446_E_[Weak] requires an instance field of a class or a COM interface type
% Weak storage is available for instance fields. Class references keep desktop non-ARC semantics.

Expand Down
14 changes: 14 additions & 0 deletions compiler/options.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,20 @@ procedure TOption.interpret_option(const opt:TCmdStr;ispara:boolean);
end;
exit;
end;
if opt.StartsWith('--project-uses=') then begin
{ units every unit and program of the project uses (lumi.json "uses") }
more:=Copy(opt,Length('--project-uses=')+1);
if (more='') or (more[1]=',') or (more[Length(more)]=',') or (Pos(',,',more)>0) then
IllegalPara(opt);
{ the options are read more than once: add each unit once }
if Pos(','+Upper(more)+',',','+Upper(projectuses)+',')=0 then
begin
if projectuses<>'' then
projectuses:=projectuses+',';
projectuses:=projectuses+more;
end;
exit;
end;
if opt.StartsWith('--required-first-unit=') then begin
more:=Upper(Copy(opt,Length('--required-first-unit=')+1));
if (more='') or (more[1]=',') or (more[Length(more)]=',') or
Expand Down
Loading