我将如何实现类似的Objective-C @恩在ANSI C code()编译器指令的东西吗? [英] How would I implement something similar to the Objective-C @encode() compiler directive in ANSI C?

查看:166
本文介绍了我将如何实现类似的Objective-C @恩在ANSI C code()编译器指令的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该@连接code指令返回一个const char *这是中传递的数据类型的各种元素的codeD类型说明符示例如下:

The @encode directive returns a const char * which is a coded type descriptor of the various elements of the datatype that was passed in. Example follows:

struct test
{ int ti ;
  char tc ;
} ;

printf( "%s", @encode(struct test) ) ;
// returns "{test=ic}"

我可以看到使用的sizeof()确定基本类型 - 如果它是一个完整的对象,我可以使用类的方法做反省

I could see using sizeof() to determine primitive types - and if it was a full object, I could use the class methods to do introspection.

但是,它是如何确定一个不透明的结构的每一个元素?

However, How does it determine each element of an opaque struct?

推荐答案

@Lothars答案可能是玩世不恭,但它是pretty接近标准,很遗憾。为了实现像 @恩code(),你需要为了提取类型信息一个完全成熟的解析器。好吧,至少不是微不足道的任何 @恩code()语句(即 @恩code(CHAR *))。现代编译器通常具有两个或三个主要组件:

@Lothars answer might be "cynical", but it's pretty close to the mark, unfortunately. In order to implement something like @encode(), you need a full blown parser in order to extract the the type information. Well, at least for anything other than "trivial" @encode() statements (i.e., @encode(char *)). Modern compilers generally have either two or three main components:


  • 前端。

  • 中间端(一些编译器)。

  • 的后端。

前端必须分析所有的源$ C ​​$ C,基本上在转换到内部,机器可用的形式源$ C ​​$ C文本。

The front end must parse all the source code and basically converts the source code text in to an internal, "machine useable" form.

后端转换到可执行code内部,机器可用的形式。

The back end translates the internal, "machine useable" form in to executable code.

编译器有一个中间到底通常这样做,因为有些需要:他们支持多个前端,可能是由完全不同的语言。另一个原因是为了简化优化:所有的优化过程在同一中间再presentation工作。在 GCC 编译器套件是一个三阶段的编译器的一个例子。 LLVM 可被视为一个中间和后端阶段的编译器:低级别的虚拟机是中间再presentation,所有的优化发生在这种形式。 LLVM 也能够保持它在这中间再presentation的权利,直到最后二线这允许链接时优化。在编译器是一个真正的前端的(有效)输出 LLVM 中间再presentation。

Compilers that have an "intermediate end" typically do so because of some need: they support multiple "front ends", possibly made up of completely different languages. Another reason is to simplify optimization: all the optimization passes work on the same intermediate representation. The gcc compiler suite is an example of a "three stage" compiler. llvm could be considered an "intermediate and back end" stage compiler: The "low level virtual machine" is the intermediate representation, and all the optimization takes place in this form. llvm also able to keep it in this intermediate representation right up until the last second- this allows for "link time optimization". The clang compiler is really a "front end" that (effectively) outputs llvm intermediate representation.

所以,如果你想 @恩code()功能添加到现有的编译器,你可能需要做它作为源源编译/ preprocessor。这是原来的Objective-C和C ++编译器是如何写上他们分析的输入源文本,并转换为纯C,然后将其以标准的C编译器送入。有几个方法可以做到这一点:

So, if you want to add @encode() functionality to an 'existing' compiler, you'd probably have to do it as a "source to source" 'compiler / preprocessor'. This was how the original Objective-C and C++ compilers were written- they parsed the input source text and converted it to "plain C" which was then fed in to the standard C compiler. There's a few ways to do this:

  • Use yacc and lex to put together a ANSI-C parser. You'll need a grammar- ANSI C grammar (Yacc) is a good start. Actually, to be clear, when I say yacc, I really mean bison and flex. And also, loosely, the other various yacc and lex like C-based tools: lemon, dparser, etc...
  • Use perl with Yapp or EYapp, which are pseudo-yacc clones in perl. Probably better for quickly prototyping an idea compared to C-based yacc and lex- it's perl after all: Regular expressions, associative arrays, no memory management, etc.
  • Build your parser with Antlr. I don't have any experience with this tool chain, but it's another "compiler compiler" tool that (seems) to be geared more towards java developers. There appears to be freely available C and Objective-C grammars available.

注意:我有使用任何这些工具做这样添加任何没有亲身经历 @恩code(),但我怀疑他们将是一个很大的帮助。

Note: I have no personal experience using any of these tools to do anything like adding @encode(), but I suspect they would be a big help.


  • CIL - 不使用此工具的个人经验,而是专为解析C ++源$ C ​​$ C,然后做的东西吧。从我可以从文档收集,这个工具应该允许你提取类型的信息你需要。

  • 稀疏 - 值得看的,但不知道

  • - 还没有使用它用于此目的,但据称的目标之一是使之轻松容易被破解的只是这样的东西。特别是(并再次,没有亲身经历)在做所有的解析繁重,让你专注于有趣的一部分,在这种情况下,将提取的上下文和语法敏感类型的信息,然后将其转换中以一个普通的C字符串。

  • GCC插件 - 插件是一个gcc 4.5(这是目前的α/β版本编译器)功能和可能让您轻松地勾在编译器中提取你需要的类型信息。如果插件架构允许这种事情不知道。

  • CIL - No personal experience with this tool, but designed for parsing C source code and then "doing stuff" with it. From what I can glean from the docs, this tool should allow you to extract the type information you'd need.
  • Sparse - Worth looking at, but not sure.
  • clang - Haven't used it for this purpose, but allegedly one of the goals was to make it "easily hackable" for just this sort of stuff. Particularly (and again, no personal experience) in doing the "heavy lifting" of all the parsing, letting you concentrate on the "interesting" part, which in this case would be extracting context and syntax sensitive type information, and then convert that in to a plain C string.
  • gcc Plugins - Plugins are a gcc 4.5 (which is the current alpha/beta version of the compiler) feature and "might" allow you to easily hook in to the compiler to extract the type information you'd need. No idea if the plugin architecture allows for this kind of thing.

  • Coccinelle的 - 书签这个最近在看后来的。这个可能能够做到你想要什么,可能能够与出很多努力做到这一点。

  • METAC - 书签这个最近太。不知道如何用这将是。

  • mygcc - 可能做你想做的。这是一个有趣的想法,但它并不直接适用于你想要什么。从网页:Mygcc允许程序员添加自己的支票考虑到语法,控制流和数据流信息

  • Coccinelle - Bookmarked this recently to "look at later". This "might" be able to do what you want, and "might" be able to do it with out much effort.
  • MetaC - Bookmarked this one recently too. No idea how useful this would be.
  • mygcc - "Might" do what you want. It's an interesting idea, but it's not directly applicable to what you want. From the web page: "Mygcc allows programmers to add their own checks that take into account syntax, control flow, and data flow information."
  • CocoaDev Objective-C Parsing - Worth looking at. Has some links to lexers and grammars.

@Lothar使得他的意见好点。我实际上意在包括 LCC ,但它看起来像它得到了前进的道路上丢失了。

@Lothar makes a good point in his comment. I had actually intended to include lcc, but it looks like it got lost along the way.


  • LCC - 的 LCC C编译器。这是一个C编译器是特别小的,至少在源$ C ​​$ C尺寸的条款。它还有一本书,我强烈建议。

  • TCC - 的台泥 C编译器。不太教学为 LCC ,但绝对还是值得期待的。

  • POC - 的 POC 的Objective-C编译器。这是一个源源的Objective-C编译器。它分析的Objective-C源$ C ​​$ c和发射的C源$ C ​​$ C,它然后传递到 GCC (当然,一般 GCC )。有一些Objective-C的扩展/不可用在 GCC 功能。绝对值得看。

  • lcc - The lcc C compiler. This is a C compiler that is particularly small, at least in terms of source code size. It also has a book, which I highly recommend.
  • tcc - The tcc C compiler. Not quite as pedagogical as lcc, but definitely still worth looking at.
  • poc - The poc Objective-C compiler. This is a "source to source" Objective-C compiler. It parses the Objective-C source code and emits C source code, which it then passes to gcc (well, usually gcc). Has a number of Objective-C extensions / features that aren't available in gcc. Definitely worth looking at.

这篇关于我将如何实现类似的Objective-C @恩在ANSI C code()编译器指令的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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