如何编写 C++采用 C++ 的代码生成器代码作为输入?

发布于 2025-01-04 14:16:27 字数 347 浏览 1 评论 0原文

我们有一个 CORBA 实现,可以为我们自动生成 Java 和 C++ 存根。由于 CORBA 生成的代码很难使用,因此我们需要围绕 CORBA 代码编写包装器/帮助器。所以我们有一个两步代码生成过程(是的,我知道这很糟糕):

CORBA IDL ->烦人的 CORBA 生成的代码 ->有用的包装器/辅助函数

使用 Java 的反射,我可以检查 CORBA 生成的代码并使用它来生成其他代码。但是,因为 C++ 没有反射,所以我不确定如何在 C++ 端执行此操作。我应该使用 C++ 解析器吗? C++ 模板?

TLDR:如何使用生成的 C++ 代码作为输入来生成 C++ 代码?

We have a CORBA implementation that autogenerates Java and C++ stubs for us. Because the CORBA-generated code is difficult to work with, we need to write wrappers/helpers around the CORBA code. So we have a 2-step code generation process (yes, I know this is bad):

CORBA IDL -> annoying CORBA-generated code -> useful wrappers/helper functions

Using Java's reflection, I can inspect the CORBA-generated code and use that to generate additional code. However, because C++ doesn't have reflection, I am not sure how to do this on the C++ side. Should I use a C++ parser? C++ templates?

TLDR: How to generate C++ code using generated C++ code as input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泡沫很甜 2025-01-11 14:16:27

您是否考虑过退后一步,使用 IDL 作为自定义代码生成器的源代码?也许您有一些隐藏了重复、var、ptr 等内容的包装器代码。我们有一个基于 Ruby 的 CORBA IDL 编译器,当前可以生成 Ruby 和 C++ 代码。这可以通过客户生成器进行扩展,请参阅 https://www.remedy.nl 了解 RIDL 和 R2CORBA。

另一种选择是查看 IDL 到 C++11 语言映射,更多详细信息请参见 https://www.taox11。组织。这种新的语言映射更容易使用,并且使用标准类型和 STL 容器来处理。

Have you considered to take a step back and use the IDL as source for a custom code generator? Probably you have some wrapper code that hides things like duplicate, var, ptr, etc. We have a Ruby based CORBA IDL compiler that currently generates Ruby and C++ code. That could be extended with a customer generator, see https://www.remedy.nl for RIDL and R2CORBA.

Another option would be to check out the IDL to C++11 language mapping, more details on https://www.taox11.org. This new language mapping is much easier to use and uses standard types and STL containers to work with.

我家小可爱 2025-01-11 14:16:27

GCC XML 可以帮助恢复界面。

我正在使用它为 OpenGL 和 Horde3D 渲染引擎编写 Prolog 外部接口。

我感兴趣的接口仅限于 C,但 GCC XML 也可以处理 C++。

GCC XML 解析源代码接口并发出 XML AST。然后,使用 XML 库就可以相当轻松地提取请求的信息。细微差别是宏符号的丢失:据我所知,只有值在解析中幸存下来。举个例子,这里(部分)用于生成 FLI 的 Prolog 代码:

make_funcs(NameChange, Xml, FileName, Id) :-
    index_id(Xml, Indexed),

    findall(Name:Returns:ArgTypes,
        (xpath(Xml, //'Function'(@file = Id, @name = Name, @returns = ReturnsId), Function),
         typeid_indexed(Indexed, ReturnsId, Returns),
         findall(Arg:Type, (xpath(Function, //'Argument'(@name = Arg, @type = TypeId), _),
                    typeid_indexed(Indexed, TypeId, Type)), ArgTypes)
        ),
        AllFuncs),

    length(AllFuncs, LAllFuncs),
    writeln(FileName:LAllFuncs),

    fat('prolog/h3dplfi/~s.cpp', [FileName], Cpp),
    open(Cpp, write, Stream),
    maplist(\X^((X = K-A -> true ; K = X, A = []), format(Stream, K, A), nl(Stream)),
        ['#include "swi-uty.h"',
         '#include <~@>'-[call(NameChange, FileName)]
        ]),

    forall(member(F, AllFuncs), make_func(Stream, F)),
    close(Stream).

xpath(你猜对了)它是 SWI-Prolog 库,使分析变得更简单......

GCC XML could help in recovering the interface.

I'm using it to write a Prolog foreign interface for OpenGL and Horde3D rendering engine.

The interfaces I'm interested to are limited to C, but GCC XML handles C++ as well.

GCC XML parse source code interface and emits and XML AST. Then with an XML library it's fairly easy extract requested info. A nuance it's the lose of macro' symbols: AFAIK just the values survive to the parse. As an example, here (part of ) the Prolog code used to generate the FLI:

make_funcs(NameChange, Xml, FileName, Id) :-
    index_id(Xml, Indexed),

    findall(Name:Returns:ArgTypes,
        (xpath(Xml, //'Function'(@file = Id, @name = Name, @returns = ReturnsId), Function),
         typeid_indexed(Indexed, ReturnsId, Returns),
         findall(Arg:Type, (xpath(Function, //'Argument'(@name = Arg, @type = TypeId), _),
                    typeid_indexed(Indexed, TypeId, Type)), ArgTypes)
        ),
        AllFuncs),

    length(AllFuncs, LAllFuncs),
    writeln(FileName:LAllFuncs),

    fat('prolog/h3dplfi/~s.cpp', [FileName], Cpp),
    open(Cpp, write, Stream),
    maplist(\X^((X = K-A -> true ; K = X, A = []), format(Stream, K, A), nl(Stream)),
        ['#include "swi-uty.h"',
         '#include <~@>'-[call(NameChange, FileName)]
        ]),

    forall(member(F, AllFuncs), make_func(Stream, F)),
    close(Stream).

xpath (you guess it) it's the SWI-Prolog library that make analysis simpler...

羞稚 2025-01-11 14:16:27

如果想要可靠地处理 C++ 源代码,则需要一个能够理解 C++ 语法和语义、能够解析 C++ 代码、转换解析后的表示并重新生成有效的 C++ 代码(包括原始注释)的程序转换工具。这样的工具实际上通过在语言之外操作来提供任意元编程,因此它不受语言中内置的“反射”或“元编程”设施的限制。

我们的 DMS 软件重组工具包及其 C++ 前端 可以做到这一点。

它已用于许多 C++ 自动转换任务,这两个任务(意外地)都与基于 CORBA 的活动相关。第一个包括将专有分布式系统的接口重塑为 CORBA 兼容的方面。第二个是在 IDL 变化的情况下重塑了一个基于 CORBA 的大型应用程序;此类更改实际上会导致代码移动并导致签名更改。您可以在网站上找到描述第一项活动的技术论文;第二个是为一家主要国防承包商完成的。

If you want to reliably process C++ source code, you need a program transformation tool that understands C++ syntax and semantics, can parse C++ code, transform the parsed representation, and regenerate valid C++ code (including the original comments). Such a tool provides in effect arbitrary metaprogramming by operating outside the language, so it is not limited by the "reflection" or "metaprogramming" facilities built into the language.

Our DMS Software Reengineering Toolkit with its C++ Front End can do this.

It has been used on a number of C++ automated transformation tasks, both (accidentally) related to CORBA-based activities. The first included reshaping interfaces for a proprietary distributed system into CORBA-compatible facets. The second reshaped a large CORBA-based application in the face of IDL changes; such changes in effect cause the code to be moved around and causes signature changes. You can find technical papers at the web site that describe the first activity; the second was done for a major defense contractor.

粉红×色少女 2025-01-11 14:16:27

看一下 Clang 编译器,除了作为一个独立的编译器之外,它还可以用作在您所描述的情况下的图书馆。它将为您提供解析树,您可以在其上进行分析和转换

Take a look at Clang compiler, aside from being a standalone compiler it is also intended to be used as an library in situations like the one you describe. It will provide you with parse tree on which you could do your analysis and transformations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文