Download presentation
Presentation is loading. Please wait.
1
Base lookup for member Kei Hasegawa
2
Member offset at C language
struct S { ...; int m; ... }; void f(struct S s, struct S* ps) { ps->m = s.m; } t0 := s[offsetof(S, m)] t1 := ps + offsetof(S, m) *t1 := t0 offsetof(S, m) is constant determined at compile time
3
Member access operator at C language
Not necessary to lookup name after operator `.’ and `->’ while parsing Offset is calculated through internal expression of comiler: class record_type : public type { ... map<string, int> m_layout; // calculated at construction int offset(string name) const; // referene m_layout };
4
Tag is a scope at C++ Reference “Tag at C++ compiler” document
Change current scope to the tag for operator `.’ and ` ->’ Necessary to lookup the name after operator `.’ and `->’ Name except for data member Tag name Member function name (Not ordinary static member function) Enumerator (Not ordinary) Of cause, Necessary to suitably restore current scope after evaluating member access operator
5
Base class struct base { }; struct tag : scope {
flag_t m_flag; // private, protected, public and virtual tag* m_tag; }; struct tag : scope { ... vector<base*>* m_bases;
6
Base lookup for member If there doesn’t exist the name to lookup at current tag Lookup at base classes recursively Base class tag name is also compared struct B { ...; int n, m; ... }; struct D : B { ...; int m; ... }; // hide B::m D d; ... use `d.n’ use `d.B::m’ As the same, base lookup is performed at member access of member function
7
Path at derived graph Save bases when lookup `a’ at parsing.
offsetof(D, a) = offsetof(D,C)+ offsetof(C,B)+ offsetof(B,A)+ offsetof(A,a) struct A { ...; int a; ... }; struct B : ..., A, ... { ... }; struct C : ..., B, ... { ... }; struct D : ..., C, ... { void f(); }; ... D d; use `d.a’ void D::f(){ use `a’ } A B C D
8
Virtual base and lookup (1)
struct A { ...; int a; ... }; struct B1 : virtual A { ... }; struct B2 : virtual A { ... }; struct C : B1, B2 { ... }; struct D : C { ... }; struct E1 : virtual D { ... }; struct E2 : virtual D { ... }; struct F : E1, E2 { ... }; ... F* pf; .... use `pf->a’ There exists 4 paths of A::a from F. B1 B2 C D E1 E2 F
9
Virtual base and lookup (2)
vector<vector<base*> > choice; typedef pair<base*, const record_type*> route_t vector<route_t> route; Calculate `route’ from `choice’ Lookup at previous page choice: {F=>E1, E1 => D , D => C , C => B1, B1 => A } {F => E2, E2 => D , D => C , C => B1, B1 => A } {F => E1, E1 => D , D => C , C => B2, B2 => A } {F => E2, E2 => D , D => C , C => B2, B2 => A } route: { {0, D}, {D => C, 0}, {0, A} } offsetof(F, a) = offsetof(F, D) + offsetof(D,C) + offsetof(C, A) + offsetof(A, a) Note that offsetof(F, E1) and offsetof(C, B2) are not appeared.
10
record_type at C++ compiler
class record_type : public type { ... // Offset of base classes map<base*, int> m_base_offset; // Offset of `D’ at `F’ or offset of `C’ at `D’ map<const record_type*, int> m_virt_common_offset; };
11
F* pf; ...; use `pf->B1::a’
choice: {F => E1, E1 => D , D => C , C => B1, B1 => A } {F => E2, E2 => D , D => C , C => B1, B1 => A } route: OK : { {0, D}, {D => C, 0}, {0, A} } NG : { {0, D}, {D => C, 0}, {C => B1, 0}, {0, A} } Point : to eliminate {C=>B1, 0} from layout of `F’
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.