C 中的反射支持 [英] Reflection support in C

查看:19
本文介绍了C 中的反射支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它不受支持,但我想知道它是否有任何技巧.有什么提示吗?

I know it is not supported, but I am wondering if there are any tricks around it. Any tips?

推荐答案

一般来说,反射是程序分析某些代码结构的一种手段.此分析用于更改代码的有效行为.

Reflection in general is a means for a program to analyze the structure of some code. This analysis is used to change the effective behavior of the code.

反射作为分析通常很弱;通常它只能提供对函数和字段名称的访问.这一弱点来自于语言实现者本质上不希望在运行时提供完整的源代码,以及从源代码中提取所需内容的适当分析例程.

Reflection as analysis is generally very weak; usually it can only provide access to function and field names. This weakness comes from the language implementers essentially not wanting to make the full source code available at runtime, along with the appropriate analysis routines to extract what one wants from the source code.

另一种方法是正面处理程序分析,通过使用强大的程序分析工具,例如,可以完全按照编译器的方式解析源文本的工具.(通常人们建议滥用编译器本身来做到这一点,但这通常是行不通的;编译器机器想成为一个编译器,而且很难将其用于其他目的).

Another approach is tackle program analysis head on, by using a strong program analysis tool, e.g., one that can parse the source text exactly the way the compiler does it. (Often people propose to abuse the compiler itself to do this, but that usually doesn't work; the compiler machinery wants to be a compiler and it is darn hard to bend it to other purposes).

需要的是一个工具:

  • 解析语言源文本
  • 构建代表程序每个细节的抽象语法树.(如果 AST 保留注释和源的其他详细信息会很有帮助代码布局,例如列号、文字基数值等)
  • 构建符号表,显示每个标识符的范围和含义
  • 可以从函数中提取控制流
  • 可以从代码中提取数据流
  • 可以为系统构建调用图
  • 可以判断每个指针指向什么
  • 允许使用上述事实构建自定义分析器
  • 可以根据此类自定义分析转换代码(通常通过修改代表解析代码的 AST)
  • 可以重新生成源文本(包括布局和注释)修订后的 AST.

使用这种机制,可以在任何需要的细节级别上进行分析,然后转换代码以实现运行时反射将实现的效果.有几个主要好处:

Using such machinery, one implements analysis at whatever level of detail is needed, and then transforms the code to achieve the effect that runtime reflection would accomplish. There are several major benefits:

  • 分析的详细程度或数量取决于目标(例如,它不是受限于运行时反射只能做什么)
  • 没有任何运行时开销来实现行为的反映变化
  • 所涉及的机制可以是通用的,适用于多种语言,而不是而不是仅限于特定语言实现提供的内容.
  • 这与 C/C++ 理念兼容,即您不用为不用的东西付费.如果你不需要反射,你就不需要这个机器.还有你的语言不需要内置弱反射的智力包袱.

请参阅我们的DMS 软件再造工具包,了解可以执行所有操作的系统以上适用于 C、Java 和 COBOL,大部分适用于 C++.

See our DMS Software Reengineering Toolkit for a system that can do all of the above for C, Java, and COBOL, and most of it for C++.

这篇关于C 中的反射支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆