Ale*_*nor 0 c++ static-methods operator-overloading
我正在研究一种使用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
Run Code Online (Sandbox Code Playgroud)
具体来说,似乎声明返回的类型以某种方式触发了要调用的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
Run Code Online (Sandbox Code Playgroud)
关于什么是迫使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();
}
Run Code Online (Sandbox Code Playgroud)
有点猜测:行shore::builtin__int * __return = NULL;中的初始化什么都不做,因为它总是被覆盖.编译器完全有权(a)__return通过调用的语句将其重新排序到指定的位置,__mul__然后(b)完全删除代码.但也许它在调试信息中留下了源代码行,并且链接器或gdb最终认为调用指令属于附近几条源代码行中的错误代码.
除非您也能看到反汇编,否则永远不要相信源代码调试.编译语言 - 呸,欺骗.等等.