File: fields.h

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (158 lines) | stat: -rw-r--r-- 3,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
struct HasThreeFields {
  int a = 1;
  int b = 2;
  int c = 3;
};

struct DerivedWithSameField : HasThreeFields {
  int a = 2;
};

struct DerivedWithOneField : HasThreeFields {
  int d = 4;
};

struct HasOneField {
  int e = 5;
};

struct DerivedFromAll : HasOneField, DerivedWithOneField {
  int f = 6;
};

struct OneField {
  int value = 42;
};

struct DerivedFromOneField : OneField {};

// Non trivial types:

struct __attribute__((swift_attr("import_unsafe"))) NonTrivial {
  NonTrivial() {}
  NonTrivial(const NonTrivial &) {}
  ~NonTrivial() {}
};

struct NonTrivialHasThreeFields : NonTrivial {
  int a = 1;
  int b = 2;
  int c = 3;
};

struct NonTrivialDerivedWithOneField : NonTrivialHasThreeFields {
  int d = 4;
};

struct __attribute__((swift_attr("import_unsafe"))) NonTrivialHasOneField {
  NonTrivialHasOneField() {}
  NonTrivialHasOneField(const NonTrivialHasOneField &other) : e(other.e) {}
  ~NonTrivialHasOneField() {}

  int e = 5;
};

struct __attribute__((swift_attr("import_unsafe"))) NonTrivialDerivedFromAll
    : NonTrivialHasOneField,
      NonTrivialDerivedWithOneField {
  int f = 6;
};

// Templates:

template<class T>
struct ClassTemplate {
  T value;
};

struct DerivedFromClassTemplate : ClassTemplate<int> {};

int &getCopyCounter() {
    static int copyCounter = 0;
    return copyCounter;
}

class CopyTrackedBaseClass {
public:
    CopyTrackedBaseClass(int x) : x(x) {}
    CopyTrackedBaseClass(const CopyTrackedBaseClass &other) : x(other.x) {
        ++getCopyCounter();
    }

    int x;
};

class CopyTrackedDerivedClass: public CopyTrackedBaseClass {
public:
    CopyTrackedDerivedClass(int x) : CopyTrackedBaseClass(x) {}
};

class NonEmptyBase {
public:
    int getY() const {
        return y;
    }
private:
    int y = 11;
};

class CopyTrackedDerivedDerivedClass: public NonEmptyBase, public CopyTrackedDerivedClass {
public:
    CopyTrackedDerivedDerivedClass(int x) : CopyTrackedDerivedClass(x) {}
};

// Types with virtual methods, make sure field offsets are right. rdar://126754931

struct HasOneFieldWithVirtualMethod {
  int a;
  virtual ~HasOneFieldWithVirtualMethod() {}
};

struct HasTwoFieldsWithVirtualMethod {
  bool b;
  bool c;
  virtual ~HasTwoFieldsWithVirtualMethod() = default;
};

struct InheritFromStructsWithVirtualMethod: HasOneFieldWithVirtualMethod, HasTwoFieldsWithVirtualMethod {
  int d;
  virtual ~InheritFromStructsWithVirtualMethod() = default;
};

// MARK: Types that pack their fields into tail padding of a base class.

struct BaseAlign8 {
  long long field8 = 123;
}; // sizeof=8, dsize=8, align=8

struct DerivedHasTailPadding : public BaseAlign8 {
  int field4 = 456;
}; // sizeof=16, dsize=12, align=8

struct DerivedUsesBaseTailPadding : public DerivedHasTailPadding {
  short field2 = 789;
}; // sizeof=16, dsize=14, align=8

// MARK: Types with an out-of-order inheritance.

struct BaseWithVirtualDestructor {
  int baseField = 123;

  virtual ~BaseWithVirtualDestructor() {}
};

struct DerivedWithVirtualDestructor : public BaseWithVirtualDestructor {
  int derivedField = 456;

  ~DerivedWithVirtualDestructor() override {}
};

struct DerivedOutOfOrder : public HasOneField,
                           public DerivedWithVirtualDestructor {
  // DerivedWithVirtualDestructor is the primary base class despite being the
  // second one the list.

  int leafField = 789;

  ~DerivedOutOfOrder() override {}
};