Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 02, 2013 Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
To access class fields in machine code generated by my JIT compiler, I need to be able to know the offset and size of the said fields. It seems that D requires a pointer to an instance of the class to make this work. This seems rather problematic for my usage. Is it safe to circumvent this by using a null pointer or could I potentially get an exception/segfault by doing this? E.g.: MyClass ptr = null; auto fSize = ptr.theField.sizeof; auto fOffs = ptr.theField.offsetof; Is it safe to assume that the offset is the same in every instance? I would assume so (why would the offsets change?) but I'd like to know if the language actually guarantees this. |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | Maxime Chevalier: > It seems that D requires a pointer to an instance of the class to make this work. I have related ER since a lot of time that has not received an answer in about three years: http://d.puremagic.com/issues/show_bug.cgi?id=3939 Bye, bearophile |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | On 3/2/13, Maxime Chevalier <maximechevalierb@gmail.com> wrote:
> To access class fields in machine code generated by my JIT compiler, I need to be able to know the offset and size of the said fields. It seems that D requires a pointer to an instance of the class to make this work.
Shouldn't the following work for you?
class C { int a; int[4] b; }
void main()
{
pragma(msg, C.a.sizeof);
pragma(msg, C.a.offsetof);
pragma(msg, C.b.sizeof);
pragma(msg, C.b.offsetof);
}
|
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Your example works but my use case doesn't and I have no idea why: class C { int a; } struct Foo { void foo(string className, string fieldName)() { mixin("auto sz = " ~ className ~ "." ~ fieldName ~ ".sizeof;"); writefln("sz: %s", sz); } } static this() { Foo f; f.foo!("C", "a"); } Produces: Error: this for a needs to be type C not type Foo Error: template instance Foo.foo!("C", "a") error instantiating |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | The problem persists without the mixin and the template: class C { int a; } struct Foo { void foo() { auto sz = C.a.sizeof; writefln("sz: %s", sz); } } static this() { Foo f; f.foo(); } Error: this for a needs to be type C not type Foo |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | On 3/2/2013 2:10 PM, Maxime Chevalier wrote: > The problem persists without the mixin and the template: It compiles without complaint for this code: -------------------------------------------- import std.stdio; class C { int a; } struct Foo { void foo() { auto sz = C.a.sizeof; writefln("sz: %s", sz); } } static this() { Foo f; f.foo(); } void main() { } ----------------------------------------- |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062. |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | On Saturday, 2 March 2013 at 22:25:24 UTC, Maxime Chevalier wrote: > I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062. Ditto: http://dpaste.dzfl.pl/8bf3cc73 |
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | On 3/2/2013 2:25 PM, Maxime Chevalier wrote:
> I'm guessing you use a bleeding edge version in which the bug has been fixed
> then. It doesn't work on DMD64 v2.062.
You're correct.
|
March 02, 2013 Re: Class Field Size/Offsets | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxime Chevalier | On Saturday, 2 March 2013 at 22:10:57 UTC, Maxime Chevalier wrote:
> The problem persists without the mixin and the template:
>
> class C { int a; }
>
> struct Foo
> {
> void foo()
> {
> auto sz = C.a.sizeof;
> writefln("sz: %s", sz);
> }
> }
>
> static this()
> {
> Foo f;
> f.foo();
> }
>
> Error: this for a needs to be type C not type Foo
For 2.062 you're going to have to move the .sizeof expression
outside of any methods which require 'this'. Make 'foo' static or
wrap the .sizeof expression inside of a template and it will
work. E.g.:
template getSizeOf(alias symb)
{
enum getSizeOf = symb.sizeof;
}
struct Foo
{
void foo()
{
auto sz = getSizeOf!(C.a);
writefln("sz: %s", sz);
}
}
It's just a bug which will be fixed in the next version.
|
Copyright © 1999-2021 by the D Language Foundation