0

我正在研究一种使用 C++ 作为目标语言的编程语言。我遇到了一个异常奇怪的回溯。

#1  0x08048d09 in factorial (n=0x8052160) at ir.cpp:35
35      shore::builtin__int * __return = NULL;
(gdb) bt
#0  shore::builtin__int::__mul__ (this=0x8052160, other=0x8052288) at /home/alex/projects/shore/shore/runtime/int.h:36
#1  0x08048d09 in factorial (n=0x8052160) at ir.cpp:35
#2  0x08048cfa in factorial (n=0x80520b8) at ir.cpp:35
#3  0x08048cfa in factorial (n=0x8052018) at ir.cpp:35
#4  0x08048d6f in main () at ir.cpp:43

具体来说,声明返回类型似乎会以某种方式触发 builtin__int 上的 __mul方法被调用,我不知道为什么。builtin__int 看起来像:

#ifndef _SHORE_INT_H
#define _SHORE_INT_H

#include "gc.h"


namespace shore {
    class builtin__int : public shore::Object {
        public:
            // Some day this will be arbitrary percision, but not today.
            long long value;

            static builtin__int* new_instance(long long value_) {
                builtin__int* val = new builtin__int(value_);
                shore::GC::register_object(val);
                return val;
            }

            builtin__int(long long value_) {
                this->value = value_;
            }

            builtin__bool* __eq__(builtin__int* other) {
                return builtin__bool::new_instance(this->value == other->value);
            }

            builtin__int* __add__(builtin__int* other) {
                return builtin__int::new_instance(this->value + other->value);
            }

            builtin__int* __sub__(builtin__int* other) {
                return builtin__int::new_instance(this->value - other->value);
            }

            builtin__int* __mul__(builtin__int* other) {
                return builtin__int::new_instance(this->value * other->value);
            }
    };
}
#endif

关于到底是什么迫使 C++ 调用mul方法的任何想法?

编辑:添加了 ir.cpp 的来源

#include "builtins.h"
#include "frame.h"
#include "object.h"
#include "state.h"
std::vector < shore::Frame * >shore::State::frames;
shore::GCSet shore::GC::allocated_objects;
class
    factorial__frame:
    public
    shore::Frame {
  public:
    shore::builtin__int *
    n;
    shore::GCSet
    __get_sub_objects() {
    shore::GCSet s;
    s.
    insert(this->n);
    return
        s;
}};
class
    main__frame:
    public
    shore::Frame {
  public:
    shore::GCSet
    __get_sub_objects() {
    shore::GCSet s;
    return
        s;
}};
shore::builtin__int * factorial(shore::builtin__int * n)
{
    shore::builtin__int * __return = NULL;
    factorial__frame frame;
    shore::State::frames.push_back(&frame);
    frame.n = NULL;
    frame.n = n;
    if (((frame.n)->__eq__(shore::builtin__int::new_instance(0)))->value) {
    __return = shore::builtin__int::new_instance(1);
    shore::GC::collect();
    shore::State::frames.pop_back();
    return __return;
    }
    __return =
    (frame.n)->
    __mul__(factorial
        ((frame.n)->
         __sub__(shore::builtin__int::new_instance(1))));
    shore::GC::collect();
    shore::State::frames.pop_back();
    return __return;
}
int
main()
{
    main__frame     frame;
    shore::State::frames.push_back(&frame);
    builtin__print(factorial(shore::builtin__int::new_instance(3)));
    shore::State::frames.pop_back();
}
4

3 回答 3

5

带有双下划线的标识符名称是保留的。您可能会与编译器生成的名称发生冲突。

于 2009-12-01T20:00:36.357 回答
5

有点猜测:行中的初始化shore::builtin__int * __return = NULL;什么都不做,因为它总是被覆盖。编译器完全有权(a)__return通过确实调用的语句将其重新排序到分配的位置,__mul__然后(b)完全删除代码。但也许它在调试信息中留下了源代码行,链接器或 gdb 最终认为调用指令属于附近几个源代码行中的错误一个。

除非您也可以看到反汇编,否则永远不要相信源代码调试。编译语言 - 呸,骗子。等等。

于 2009-12-01T20:11:05.423 回答
1

在我看来翻译器失败了,并且不知何故 gcc(或其他)认为shore::builtin__int 是某种值,并且您试图将其乘以 __return,而不是将值 __return 声明为类型shore: :builtin__int *...

显然,如果这个东西正在编译,并给你运行时错误,那么乘法会给你的任何类型都是有效的 LHS ......

于 2009-12-01T20:05:20.077 回答