gpt4 book ai didi

Ada 详细说明根本没有发生

转载 作者:行者123 更新时间:2023-12-02 06:27:18 25 4
gpt4 key购买 nike

我有一个不寻常的情况,其中精化代码根本没有被执行。这不是细化顺序问题,而是细化所有问题。

问题是我不“使用”有问题的单元,但理论上它应该仍然可以访问,只要它的详细说明发生。

当然,我可以为有问题的单元添加一个无用的“with”,但在我的实际用例中,有大量单元我必须这样做。

我的问题是,在代码中,通过 pragmas,在 gpr 项目文件中,或者通过命令行开关,是否有任何方法可以强制编译器包含一个文件,即使它认为该文件不是引用?

这是一个最小的工作示例:

作为广告:

package As is
type A is tagged null record;
type Nothing is null record;
function Create (Ignored : not null access Nothing) return A;
function Image (From : A) return String;
end As;

作为.adb:

package body As is
function Create (Ignored : not null access Nothing) return A is
(null record);
function Image (From : A) return String is ("A");
end As;

finder.ads:

with Ada.Tags;

package Finder is
procedure Register (Name : String; Tag : Ada.Tags.Tag);
function Find (Name : String; Default : Ada.Tags.Tag) return Ada.Tags.Tag;
end Finder;

查找器.adb:

with Ada.Containers.Indefinite_Vectors;

package body Finder is
type Name_Tag (Size : Natural) is
record
Name : String (1 .. Size);
To : Ada.Tags.Tag;
end record;

package Name_Tag_Vectors is new Ada.Containers.Indefinite_Vectors (Positive, Name_Tag);
Name_Tags : Name_Tag_Vectors.Vector := Name_Tag_Vectors.Empty_Vector;

procedure Register (Name : String; Tag : Ada.Tags.Tag) is begin
Name_Tags.Append ((Name'Length, Name, Tag));
end Register;

function Find (Name : String; Default : Ada.Tags.Tag) return Ada.Tags.Tag is begin
for Tag of Name_Tags loop
if Tag.Name = Name then
return Tag.To;
end if;
end loop;

return Default;
end Find;
end Finder;

bs.ads:

with As;
package Bs is
type B is new As.A with null record;
function Create (Ignored : not null access As.Nothing) return B;
function Image (From : B) return String;
end Bs;

bs.adb:

with Finder;
package body Bs is
function Create (Ignored : not null access As.Nothing) return B is
(As.Create (Ignored) with null record);
function Image (From : B) return String is ("B");
begin
Finder.Register ("B", B'Tag);
end Bs;

测试.adb:

with As; use As;
-- with Bs; -- (uncommenting this line solves my problem, but what if I had the rest of the alphabet?)
with Finder;
with Ada.Tags.Generic_Dispatching_Constructor;
with Ada.Text_IO;

procedure Test is
function Constructor is new Ada.Tags.Generic_Dispatching_Constructor (
T => A,
Parameters => Nothing,
Constructor => Create);

Nada : aliased Nothing := (null record);
What : A'Class := Constructor (Finder.Find ("B", A'Tag), Nada'Access);
begin
Ada.Text_IO.Put_Line (What.Image);
end Test;

最佳答案

编译器认为您的包 Bs 没有被引用,因为它没有。你没有 with 子句,所以它不是你程序的一部分。

一个简单的例子:

a.广告

package A is
procedure Blah;
end A;

a.adb

with Ada.Text_IO;
package body A is
procedure Blah is begin null; end Blah;
begin
Ada.Text_IO.Put_Line("Elaborate A");
end A;

b.ads

package B is
procedure Blah;
end B;

b.adb

with Ada.Text_IO;
package body B is
procedure Blah is begin null; end Blah;
begin
Ada.Text_IO.Put_Line("Elaborate B");
end B;

主.adb

with Ada.Text_IO;
with A;
procedure Main is
begin
Ada.Text_IO.Put_Line("Main");
end Main;

当我运行 main 时,它会打印

Elaborate A
Main

它不打印 Elaborate B 因为那个包不是程序的一部分;它只是同一目录中的几个源文件。

显而易见的解决方案是添加 with 子句。

我不知道是否有不太明显的解决方案。如果有,它可能是特定于编译器的。但我不确定为什么编译器会有一项功能,可以让您将原本未使用的包合并到程序中。

关于Ada 详细说明根本没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54796066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com