斜线和点的函数名和原型? [英] Slashes and dots in function names and prototypes?

查看:149
本文介绍了斜线和点的函数名和原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C和看的源代码树,我发现这一点:

<一个href=\"https://$c$c.google.com/p/go/source/browse/src/pkg/runtime/race.c\">https://$c$c.google.com/p/go/source/browse/src/pkg/runtime/race.c

  void运行/赛·读(INT32 GOID,void *的地址,无效* PC);
void运行/赛·写入(INT32 GOID,void *的地址,无效* PC);空虚
运行·raceinit(无效)
{
    // ...
}

什么是斜线和点(·)是什么意思?这是合法的C?


解决方案

  

重要更新:


  
  

最终的答案肯定是你有一个从拉斯考克斯 ,围棋的作者之一,golang螺母邮件列表。这就是说,我要离开我的一些早期以下的笔记,他们可能有助于了解一些事情。


  
  

另外,从阅读这个答案上面链接,我相信 / 伪斜杠现在可以转换为普通的 / 削减太(像middot被转换为点)在Go C编译器比我下面测试的一个新版本 - 但我没有时间来验证



该文件由围棋语言套房的内部C编译器,它起源于计划9 C编译器编译 (1)(2) 有一些差异(主要是扩展,据我所知)到C标准。

其中一个扩展是,它允许UTF-8字符的标识符。

现在,在Go语言套件的C编译器,middot字符(·)是一种特殊的方式处理,因为它被翻译到正规点(。)在目标文件,这是围棋PTED间$ P $语言套房的内部连接器作为命名空间的分隔符。


  

示例


  
  

有关下列文件 example.c (注意:它必须保存为UTF-8无BOM):


空·BAR1(){}
无效美孚·BAR2(){}
无效美孚/巴兹·bar3(){}


  
  

内部C编译器生成以下符号:


$去工具8C example.c
$去工具纳米example.8
 T.Bar1
 Ťfoo.bar2
 ŧ富/ baz.bar3

现在,请注意我已经给了·BAR1()资本 B 。这是
  因为这样,我可以把它定期去code可见 - 因为
  它被转换为完全一样的符号作为将导致从
  编译如下围棋code:


例如包
FUNC BAR1(){} //纳米显示:T.Bar1


现在,关于你的问题命名功能,这个故事进一步下降兔子洞。我有点不太肯定,如果我在这里,但我会尽量根据我所知道的解释。因此,如果它有 AFAIK 的末刚刚写好低于这个点的每个句子应该读

因此​​,为了更好地理解这个难题需要在未来缺少的部分,就是要多了解一下怪命名空间,围棋套件的连接器如何处理它。在命名空间是我们可能要调用一个空(因为一个程序员手段一个空字符串)命名空间,或者更好的,一个占位符命名空间。和链接器时看到的进口会是这样的:

 进口examp路径/要/包/例如
// ...
FUNC的main(){
    examp.B​​ar1()
}

那么所花费的 $ GOPATH / PKG /.../ example.a 库文件,并在进口替代阶段的飞行每个 路径/到/包/例子。所以,现在,在链接的程序,我们会看到这样的代码:


 ŧ路径/要/包/ example.Bar1

I'm new to C and looking at Go's source tree I found this:

https://code.google.com/p/go/source/browse/src/pkg/runtime/race.c

void runtime∕race·Read(int32 goid, void *addr, void *pc);
void runtime∕race·Write(int32 goid, void *addr, void *pc);

void
runtime·raceinit(void)
{
    // ...
}

What do the slashes and dots (·) mean? Is this valid C?

解决方案

IMPORTANT UPDATE:

The ultimate answer is certainly the one you got from Russ Cox, one of Go authors, on the golang-nuts mailing list. That said, I'm leaving some of my earlier notes below, they might help to understand some things.

Also, from reading this answer linked above, I believe the "pseudo-slash" may now be translated to regular / slash too (like the middot is translated to dot) in newer versions of Go C compiler than the one I've tested below - but I don't have time to verify.


The file is compiled by the Go Language Suite's internal C compiler, which originates in the Plan 9 C compiler(1)(2), and has some differences (mostly extensions, AFAIK) to the C standard.

One of the extensions is, that it allows UTF-8 characters in identifiers.

Now, in the Go Language Suite's C compiler, the middot character (·) is treated in a special way, as it is translated to a regular dot (.) in object files, which is interpreted by Go Language Suite's internal linker as namespace separator character.

Example

For the following file example.c (note: it must be saved as UTF-8 without BOM):

void ·Bar1() {}
void foo·bar2() {}
void foo∕baz·bar3() {}

the internal C compiler produces the following symbols:

$ go tool 8c example.c
$ go tool nm example.8
 T "".Bar1
 T foo.bar2
 T foo∕baz.bar3

Now, please note I've given the ·Bar1() a capital B. This is because that way, I can make it visible to regular Go code - because it is translated to the exact same symbol as would result from compiling the following Go code:

package example
func Bar1() {}  // nm will show:  T "".Bar1

Now, regarding the functions you named in the question, the story goes further down the rabbit hole. I'm a bit less sure if I'm right here, but I'll try to explain based on what I know. Thus, each sentence below this point should be read as if it had "AFAIK" written just at the end.

So, the next missing piece needed to better understand this puzzle, is to know something more about the strange "" namespace, and how the Go suite's linker handles it. The "" namespace is what we might want to call an "empty" (because "" for a programmer means "an empty string") namespace, or maybe better, a "placeholder" namespace. And when the linker sees an import going like this:

import examp "path/to/package/example"
//...
func main() {
    examp.Bar1()
}

then it takes the $GOPATH/pkg/.../example.a library file, and during import phase substitutes on the fly each "" with path/to/package/example. So now, in the linked program, we will see a symbol like this:

 T path/to/package/example.Bar1

这篇关于斜线和点的函数名和原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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