annotate gen/classes.cpp @ 169:2df270e1ba59 trunk

[svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles. Commented some of the *very* verbose logging for -vv option.
author lindquist
date Tue, 06 May 2008 03:07:21 +0200
parents a8cd9bc1021a
children db9890b3fb64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1 #include <sstream>
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
2 #include "gen/llvm.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
3
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
4 #include "mtype.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
5 #include "aggregate.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
6 #include "init.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
7 #include "declaration.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
8
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
9 #include "gen/irstate.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
10 #include "gen/tollvm.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
11 #include "gen/arrays.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
12 #include "gen/logger.h"
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
13 #include "gen/classes.h"
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
14 #include "gen/structs.h"
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
15 #include "gen/functions.h"
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
16 #include "gen/runtime.h"
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
17 #include "gen/dvalue.h"
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
18
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
19 #include "ir/irstruct.h"
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
20
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
21 //////////////////////////////////////////////////////////////////////////////////////////
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
22
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
23 static void LLVM_AddBaseClassInterfaces(ClassDeclaration* target, BaseClasses* bcs)
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
24 {
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
25 // add base class data members first
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
26 for (int j=0; j<bcs->dim; j++)
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
27 {
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
28 BaseClass* bc = (BaseClass*)(bcs->data[j]);
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
29
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
30 // base *classes* might add more interfaces?
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
31 DtoResolveClass(bc->base);
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
32 LLVM_AddBaseClassInterfaces(target, &bc->base->baseclasses);
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
33
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
34 // resolve interfaces while we're at it
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
35 if (bc->base->isInterfaceDeclaration())
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
36 {
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
37 // don't add twice
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
38 if (gIR->irDsymbol[target].irStruct->interfaceMap.find(bc->base) == gIR->irDsymbol[target].irStruct->interfaceMap.end())
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
39 {
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
40 Logger::println("adding interface '%s'", bc->base->toPrettyChars());
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
41 IrInterface* iri = new IrInterface(bc, NULL);
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
42
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
43 // add to map
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
44 gIR->irDsymbol[target].irStruct->interfaceMap.insert(std::make_pair(bc->base, iri));
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
45 // add to ordered list
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
46 gIR->irDsymbol[target].irStruct->interfaceVec.push_back(iri);
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
47
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
48 // Fill in vtbl[]
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
49 if (!target->isAbstract()) {
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
50 bc->fillVtbl(target, &bc->vtbl, 0);
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
51 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
52 }
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
53 }
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
54 }
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
55 }
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
56
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
57 //////////////////////////////////////////////////////////////////////////////////////////
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
58
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
59 static void LLVM_AddBaseClassData(BaseClasses* bcs)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
60 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
61 // add base class data members first
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
62 for (int j=0; j<bcs->dim; j++)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
63 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
64 BaseClass* bc = (BaseClass*)(bcs->data[j]);
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
65
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
66 // interfaces never add data fields
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
67 if (bc->base->isInterfaceDeclaration())
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
68 continue;
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
69
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
70 // recursively add baseclass data
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
71 LLVM_AddBaseClassData(&bc->base->baseclasses);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
72
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
73 Array* arr = &bc->base->fields;
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
74 if (arr->dim == 0)
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
75 continue;
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
76
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
77 Logger::println("Adding base class members of %s", bc->base->toChars());
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
78 LOG_SCOPE;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
79
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
80 for (int k=0; k < arr->dim; k++) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
81 VarDeclaration* v = (VarDeclaration*)(arr->data[k]);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
82 v->toObjFile();
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
83 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
84 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
85 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
86
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
87 //////////////////////////////////////////////////////////////////////////////////////////
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
88
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
89 void DtoResolveClass(ClassDeclaration* cd)
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
90 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
91 if (gIR->irDsymbol[cd].resolved) return;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
92 gIR->irDsymbol[cd].resolved = true;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
93
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
94 Logger::println("DtoResolveClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars());
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
95 LOG_SCOPE;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
96
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
97 // get the TypeClass
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
98 assert(cd->type->ty == Tclass);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
99 TypeClass* ts = (TypeClass*)cd->type;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
100
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
101 // make sure the IrStruct is created
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
102 IrStruct* irstruct = gIR->irDsymbol[cd].irStruct;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
103 if (!irstruct) {
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
104 irstruct = new IrStruct(ts);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
105 gIR->irDsymbol[cd].irStruct = irstruct;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
106 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
107
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
108 // resolve the base class
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
109 if (cd->baseClass) {
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
110 DtoResolveClass(cd->baseClass);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
111 }
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
112
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
113 // resolve interface vtables
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
114 /*if (cd->vtblInterfaces) {
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
115 Logger::println("Vtbl interfaces for '%s'", cd->toPrettyChars());
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
116 LOG_SCOPE;
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
117 for (int i=0; i < cd->vtblInterfaces->dim; i++) {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
118 BaseClass *b = (BaseClass *)cd->vtblInterfaces->data[i];
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
119 ClassDeclaration *id = b->base;
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
120 Logger::println("Vtbl interface: '%s'", id->toPrettyChars());
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
121 DtoResolveClass(id);
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
122 // Fill in vtbl[]
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
123 b->fillVtbl(cd, &b->vtbl, 1);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
124 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
125 }*/
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
126
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
127 gIR->structs.push_back(irstruct);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
128 gIR->classes.push_back(cd);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
129
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
130 // add vtable
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
131 gIR->irType[ts].vtblType = new llvm::PATypeHolder(llvm::OpaqueType::get());
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
132 const llvm::Type* vtabty = getPtrToType(gIR->irType[ts].vtblType->get());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
133
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
134 std::vector<const llvm::Type*> fieldtypes;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
135 fieldtypes.push_back(vtabty);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
136
115
5ba6d286c941 [svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents: 114
diff changeset
137 // add monitor
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
138 fieldtypes.push_back(getPtrToType(llvm::Type::Int8Ty));
115
5ba6d286c941 [svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents: 114
diff changeset
139
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
140 // add base class data fields first
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
141 LLVM_AddBaseClassData(&cd->baseclasses);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
142
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
143 // then add own members
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
144 for (int k=0; k < cd->members->dim; k++) {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
145 Dsymbol* dsym = (Dsymbol*)(cd->members->data[k]);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
146 dsym->toObjFile();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
147 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
148
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
149 // resolve class data fields (possibly unions)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
150 Logger::println("doing class fields");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
151
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
152 if (irstruct->offsets.empty())
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
153 {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
154 Logger::println("has no fields");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
155 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
156 else
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
157 {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
158 Logger::println("has fields");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
159 unsigned prevsize = (unsigned)-1;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
160 unsigned lastoffset = (unsigned)-1;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
161 const llvm::Type* fieldtype = NULL;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
162 VarDeclaration* fieldinit = NULL;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
163 size_t fieldpad = 0;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
164 int idx = 0;
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
165 for (IrStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) {
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
166 // first iteration
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
167 if (lastoffset == (unsigned)-1) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
168 lastoffset = i->first;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
169 fieldtype = i->second.type;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
170 fieldinit = i->second.var;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
171 prevsize = getABITypeSize(fieldtype);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
172 gIR->irDsymbol[i->second.var].irField->index = idx;
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
173 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
174 // colliding offset?
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
175 else if (lastoffset == i->first) {
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
176 size_t s = getABITypeSize(i->second.type);
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
177 if (s > prevsize) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
178 fieldpad += s - prevsize;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
179 prevsize = s;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
180 }
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
181 gIR->irDsymbol[cd].irStruct->hasUnions = true;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
182 gIR->irDsymbol[i->second.var].irField->index = idx;
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
183 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
184 // intersecting offset?
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
185 else if (i->first < (lastoffset + prevsize)) {
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
186 size_t s = getABITypeSize(i->second.type);
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
187 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
188 gIR->irDsymbol[cd].irStruct->hasUnions = true;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
189 gIR->irDsymbol[i->second.var].irField->index = idx;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
190 gIR->irDsymbol[i->second.var].irField->indexOffset = (i->first - lastoffset) / s;
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
191 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
192 // fresh offset
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
193 else {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
194 // commit the field
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
195 fieldtypes.push_back(fieldtype);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
196 irstruct->defaultFields.push_back(fieldinit);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
197 if (fieldpad) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
198 fieldtypes.push_back(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad));
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
199 irstruct->defaultFields.push_back(NULL);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
200 idx++;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
201 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
202
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
203 idx++;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
204
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
205 // start new
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
206 lastoffset = i->first;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
207 fieldtype = i->second.type;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
208 fieldinit = i->second.var;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
209 prevsize = getABITypeSize(fieldtype);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
210 gIR->irDsymbol[i->second.var].irField->index = idx;
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
211 fieldpad = 0;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
212 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
213 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
214 fieldtypes.push_back(fieldtype);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
215 irstruct->defaultFields.push_back(fieldinit);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
216 if (fieldpad) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
217 fieldtypes.push_back(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad));
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
218 irstruct->defaultFields.push_back(NULL);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
219 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
220 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
221
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
222 // populate interface map
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
223 {
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
224 Logger::println("Adding interfaces to '%s'", cd->toPrettyChars());
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
225 LOG_SCOPE;
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
226 LLVM_AddBaseClassInterfaces(cd, &cd->baseclasses);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
227 Logger::println("%d interfaces added", gIR->irDsymbol[cd].irStruct->interfaceVec.size());
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
228 assert(gIR->irDsymbol[cd].irStruct->interfaceVec.size() == gIR->irDsymbol[cd].irStruct->interfaceMap.size());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
229 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
230
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
231 // add interface vtables at the end
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
232 int interIdx = (int)fieldtypes.size();
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
233 for (IrStruct::InterfaceVectorIter i=irstruct->interfaceVec.begin(); i!=irstruct->interfaceVec.end(); ++i)
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
234 {
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
235 IrInterface* iri = *i;
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
236 ClassDeclaration* id = iri->decl;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
237
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
238 // set vtbl type
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
239 TypeClass* itc = (TypeClass*)id->type;
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
240 const llvm::Type* ivtblTy = getPtrToType(gIR->irType[itc].vtblType->get());
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
241 fieldtypes.push_back(ivtblTy);
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
242
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
243 // fix the interface vtable type
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
244 iri->vtblTy = isaStruct(gIR->irType[itc].vtblType->get());
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
245
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
246 // set index
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
247 iri->index = interIdx++;
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
248 }
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
249 Logger::println("%d interface vtables added", gIR->irDsymbol[cd].irStruct->interfaceVec.size());
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
250 assert(gIR->irDsymbol[cd].irStruct->interfaceVec.size() == gIR->irDsymbol[cd].irStruct->interfaceMap.size());
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
251
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
252 // create type
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
253 const llvm::StructType* structtype = llvm::StructType::get(fieldtypes);
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
254
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
255 // refine abstract types for stuff like: class C {C next;}
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
256 assert(irstruct->recty != 0);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
257 llvm::PATypeHolder& spa = irstruct->recty;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
258 llvm::cast<llvm::OpaqueType>(spa.get())->refineAbstractTypeTo(structtype);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
259 structtype = isaStruct(spa.get());
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
260
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
261 // make it official
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
262 if (!gIR->irType[ts].type)
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
263 gIR->irType[ts].type = new llvm::PATypeHolder(structtype);
103
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
264 else
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
265 *gIR->irType[ts].type = structtype;
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
266 spa = *gIR->irType[ts].type;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
267
123
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
268 // name the type
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
269 gIR->module->addTypeName(cd->mangle(), gIR->irType[ts].type->get());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
270
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
271 // get interface info type
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
272 const llvm::StructType* infoTy = DtoInterfaceInfoType();
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
273
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
274 // create vtable type
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
275 llvm::GlobalVariable* svtblVar = 0;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
276 std::vector<const llvm::Type*> sinits_ty;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
277
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
278 for (int k=0; k < cd->vtbl.dim; k++)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
279 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
280 Dsymbol* dsym = (Dsymbol*)cd->vtbl.data[k];
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
281 assert(dsym);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
282 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n';
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
283
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
284 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) {
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
285 DtoResolveFunction(fd);
117
56a21f3e5d3e [svn r121] Finished ModuleInfo implementation.
lindquist
parents: 115
diff changeset
286 //assert(fd->type->ty == Tfunction);
56a21f3e5d3e [svn r121] Finished ModuleInfo implementation.
lindquist
parents: 115
diff changeset
287 //TypeFunction* tf = (TypeFunction*)fd->type;
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
288 //const llvm::Type* fpty = getPtrToType(gIR->irType[tf].type->get());
117
56a21f3e5d3e [svn r121] Finished ModuleInfo implementation.
lindquist
parents: 115
diff changeset
289 const llvm::FunctionType* vfty = DtoBaseFunctionType(fd);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
290 const llvm::Type* vfpty = getPtrToType(vfty);
117
56a21f3e5d3e [svn r121] Finished ModuleInfo implementation.
lindquist
parents: 115
diff changeset
291 sinits_ty.push_back(vfpty);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
292 }
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
293 else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
294 Logger::println("*** ClassDeclaration in vtable: %s", cd2->toChars());
103
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
295 const llvm::Type* cinfoty;
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
296 if (cd->isInterfaceDeclaration()) {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
297 cinfoty = infoTy;
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
298 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
299 else if (cd != ClassDeclaration::classinfo) {
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
300 cinfoty = gIR->irType[ClassDeclaration::classinfo->type].type->get();
103
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
301 }
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
302 else {
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
303 // this is the ClassInfo class, the type is this type
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
304 cinfoty = gIR->irType[ts].type->get();
103
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
305 }
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
306 const llvm::Type* cty = getPtrToType(cinfoty);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
307 sinits_ty.push_back(cty);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
308 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
309 else
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
310 assert(0);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
311 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
312
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
313 assert(!sinits_ty.empty());
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
314 const llvm::StructType* svtbl_ty = llvm::StructType::get(sinits_ty);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
315
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
316 std::string styname(cd->mangle());
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
317 styname.append("__vtblType");
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
318 gIR->module->addTypeName(styname, svtbl_ty);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
319
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
320 // refine for final vtable type
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
321 llvm::cast<llvm::OpaqueType>(gIR->irType[ts].vtblType->get())->refineAbstractTypeTo(svtbl_ty);
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
322
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
323 gIR->classes.pop_back();
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
324 gIR->structs.pop_back();
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
325
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
326 gIR->declareList.push_back(cd);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
327 }
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
328
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
329 //////////////////////////////////////////////////////////////////////////////////////////
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
330
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
331 void DtoDeclareClass(ClassDeclaration* cd)
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
332 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
333 if (gIR->irDsymbol[cd].declared) return;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
334 gIR->irDsymbol[cd].declared = true;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
335
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
336 Logger::println("DtoDeclareClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars());
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
337 LOG_SCOPE;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
338
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
339 assert(cd->type->ty == Tclass);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
340 TypeClass* ts = (TypeClass*)cd->type;
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
341
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
342 assert(gIR->irDsymbol[cd].irStruct);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
343 IrStruct* irstruct = gIR->irDsymbol[cd].irStruct;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
344
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
345 gIR->structs.push_back(irstruct);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
346 gIR->classes.push_back(cd);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
347
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
348 bool needs_definition = false;
147
0636f6269dfd [svn r152] Relates to ticket #34. Always emit class definition if it is a template instance. Linkage still needs to be fixed.
ChristianK
parents: 142
diff changeset
349 if (cd->getModule() == gIR->dmodule || DtoIsTemplateInstance(cd)) {
123
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
350 needs_definition = true;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
351 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
352
149
4c577c2b7229 [svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents: 147
diff changeset
353 llvm::GlobalValue::LinkageTypes _linkage = DtoLinkage(cd);
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
354
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
355 // interfaces have no static initializer
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
356 // same goes for abstract classes
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
357 if (!cd->isInterfaceDeclaration() && !cd->isAbstract()) {
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
358 // vtable
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
359 std::string varname("_D");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
360 varname.append(cd->mangle());
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
361 varname.append("6__vtblZ");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
362
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
363 const llvm::StructType* svtbl_ty = isaStruct(gIR->irType[ts].vtblType->get());
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
364 gIR->irDsymbol[cd].irStruct->vtbl = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module);
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
365 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
366
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
367 // get interface info type
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
368 const llvm::StructType* infoTy = DtoInterfaceInfoType();
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
369
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
370 // interface info array
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
371 if (!gIR->irDsymbol[cd].irStruct->interfaceVec.empty()) {
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
372 // symbol name
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
373 std::string nam = "_D";
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
374 nam.append(cd->mangle());
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
375 nam.append("16__interfaceInfosZ");
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
376 // resolve array type
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
377 const llvm::ArrayType* arrTy = llvm::ArrayType::get(infoTy, gIR->irDsymbol[cd].irStruct->interfaceVec.size());
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
378 // declare global
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
379 irstruct->interfaceInfosTy = arrTy;
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
380 irstruct->interfaceInfos = new llvm::GlobalVariable(arrTy, true, _linkage, NULL, nam, gIR->module);
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
381 }
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
382
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
383 // interfaces have no static initializer
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
384 // same goes for abstract classes
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
385 if (!cd->isInterfaceDeclaration() && !cd->isAbstract()) {
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
386 // interface vtables
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
387 unsigned idx = 0;
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
388 for (IrStruct::InterfaceVectorIter i=irstruct->interfaceVec.begin(); i!=irstruct->interfaceVec.end(); ++i)
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
389 {
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
390 IrInterface* iri = *i;
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
391 ClassDeclaration* id = iri->decl;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
392
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
393 std::string nam("_D");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
394 nam.append(cd->mangle());
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
395 nam.append("11__interface");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
396 nam.append(id->mangle());
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
397 nam.append("6__vtblZ");
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
398
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
399 assert(iri->vtblTy);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
400 iri->vtbl = new llvm::GlobalVariable(iri->vtblTy, true, _linkage, 0, nam, gIR->module);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
401 llvm::Constant* idxs[2] = {DtoConstUint(0), DtoConstUint(idx)};
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
402 iri->info = llvm::ConstantExpr::getGetElementPtr(irstruct->interfaceInfos, idxs, 2);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
403 idx++;
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
404 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
405
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
406 // init
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
407 std::string initname("_D");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
408 initname.append(cd->mangle());
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
409 initname.append("6__initZ");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
410
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
411 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(gIR->irType[ts].type->get(), true, _linkage, NULL, initname, gIR->module);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
412 gIR->irDsymbol[cd].irStruct->init = initvar;
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
413 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
414
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
415 gIR->classes.pop_back();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
416 gIR->structs.pop_back();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
417
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
418 gIR->constInitList.push_back(cd);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
419 if (needs_definition)
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
420 gIR->defineList.push_back(cd);
103
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
421
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
422 // classinfo
855adfdb8d38 [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents: 102
diff changeset
423 DtoDeclareClassInfo(cd);
106
5b5194b25f33 [svn r110] Fixed typeinfo for classes.
lindquist
parents: 103
diff changeset
424
5b5194b25f33 [svn r110] Fixed typeinfo for classes.
lindquist
parents: 103
diff changeset
425 // typeinfo
123
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
426 if (needs_definition)
106
5b5194b25f33 [svn r110] Fixed typeinfo for classes.
lindquist
parents: 103
diff changeset
427 cd->type->getTypeInfo(NULL);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
428 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
429
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
430 //////////////////////////////////////////////////////////////////////////////////////////
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
431
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
432 void DtoConstInitClass(ClassDeclaration* cd)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
433 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
434 if (gIR->irDsymbol[cd].initialized) return;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
435 gIR->irDsymbol[cd].initialized = true;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
436
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
437 if (cd->isInterfaceDeclaration())
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
438 return; // nothing to do
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
439
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
440 Logger::println("DtoConstInitClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
441 LOG_SCOPE;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
442
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
443 IrStruct* irstruct = gIR->irDsymbol[cd].irStruct;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
444 gIR->structs.push_back(irstruct);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
445 gIR->classes.push_back(cd);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
446
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
447 // get the struct (class) type
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
448 assert(cd->type->ty == Tclass);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
449 TypeClass* ts = (TypeClass*)cd->type;
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
450 const llvm::StructType* structtype = isaStruct(gIR->irType[ts].type->get());
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
451 const llvm::StructType* vtbltype = isaStruct(gIR->irType[ts].vtblType->get());
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
452
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
453 // make sure each offset knows its default initializer
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
454 for (IrStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i)
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
455 {
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
456 IrStruct::Offset* so = &i->second;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
457 llvm::Constant* finit = DtoConstFieldInitializer(so->var->type, so->var->init);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
458 so->init = finit;
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
459 gIR->irDsymbol[so->var].irField->constInit = finit;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
460 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
461
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
462 // fill out fieldtypes/inits
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
463 std::vector<llvm::Constant*> fieldinits;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
464
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
465 // first field is always the vtable
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
466 if (cd->isAbstract())
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
467 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
468 fieldinits.push_back(
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
469 llvm::ConstantPointerNull::get(
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
470 getPtrToType(
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
471 gIR->irType[ts].vtblType->get()
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
472 )
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
473 )
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
474 );
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
475 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
476 else
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
477 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
478 assert(gIR->irDsymbol[cd].irStruct->vtbl != 0);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
479 fieldinits.push_back(gIR->irDsymbol[cd].irStruct->vtbl);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
480 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
481
115
5ba6d286c941 [svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents: 114
diff changeset
482 // then comes monitor
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
483 fieldinits.push_back(llvm::ConstantPointerNull::get(getPtrToType(llvm::Type::Int8Ty)));
115
5ba6d286c941 [svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents: 114
diff changeset
484
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
485 // go through the field inits and build the default initializer
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
486 size_t nfi = irstruct->defaultFields.size();
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
487 for (size_t i=0; i<nfi; ++i) {
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
488 llvm::Constant* c;
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
489 if (irstruct->defaultFields[i]) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
490 c = gIR->irDsymbol[irstruct->defaultFields[i]].irField->constInit;
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
491 assert(c);
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
492 }
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
493 else {
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
494 const llvm::ArrayType* arrty = isaArray(structtype->getElementType(i+2));
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
495 assert(arrty);
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
496 std::vector<llvm::Constant*> vals(arrty->getNumElements(), llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false));
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
497 c = llvm::ConstantArray::get(arrty, vals);
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
498 }
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
499 fieldinits.push_back(c);
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
500 }
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
501
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
502 // last comes interface vtables
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
503 const llvm::StructType* infoTy = DtoInterfaceInfoType();
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
504 for (IrStruct::InterfaceVectorIter i=irstruct->interfaceVec.begin(); i!=irstruct->interfaceVec.end(); ++i)
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
505 {
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
506 IrInterface* iri = *i;
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
507 iri->infoTy = infoTy;
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
508
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
509 if (cd->isAbstract())
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
510 {
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
511 fieldinits.push_back(llvm::Constant::getNullValue(structtype->getElementType(iri->index)));
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
512 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
513 else
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
514 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
515 assert(iri->vtbl);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
516 fieldinits.push_back(iri->vtbl);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
517 }
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
518 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
519
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
520 // generate initializer
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
521 #if 0
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
522 //Logger::cout() << cd->toPrettyChars() << " | " << *structtype << '\n';
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
523 assert(fieldinits.size() == structtype->getNumElements());
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
524 for(size_t i=0; i<structtype->getNumElements(); ++i) {
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
525 Logger::cout() << "s#" << i << " = " << *structtype->getElementType(i) << '\n';
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
526 Logger::cout() << "i#" << i << " = " << *fieldinits[i] << '\n';
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
527 assert(fieldinits[i]->getType() == structtype->getElementType(i));
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
528 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
529 #endif
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
530
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
531 #if 0
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
532 for(size_t i=0; i<fieldinits.size(); ++i) {
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
533 Logger::cout() << "i#" << i << " = " << *fieldinits[i]->getType() << '\n';
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
534 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
535 #endif
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
536
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
537 llvm::Constant* _init = llvm::ConstantStruct::get(structtype, fieldinits);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
538 assert(_init);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
539 gIR->irDsymbol[cd].irStruct->constInit = _init;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
540
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
541 // abstract classes have no static vtable
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
542 // neither do interfaces (on their own, the implementing class supplies the vtable)
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
543 if (!cd->isInterfaceDeclaration() && !cd->isAbstract())
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
544 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
545 // generate vtable initializer
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
546 std::vector<llvm::Constant*> sinits;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
547
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
548 for (int k=0; k < cd->vtbl.dim; k++)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
549 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
550 Dsymbol* dsym = (Dsymbol*)cd->vtbl.data[k];
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
551 assert(dsym);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
552 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n';
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
553
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
554 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
555 DtoForceDeclareDsymbol(fd);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
556 assert(gIR->irDsymbol[fd].irFunc->func);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
557 llvm::Constant* c = llvm::cast<llvm::Constant>(gIR->irDsymbol[fd].irFunc->func);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
558 // cast if necessary (overridden method)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
559 if (c->getType() != vtbltype->getElementType(k))
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
560 c = llvm::ConstantExpr::getBitCast(c, vtbltype->getElementType(k));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
561 sinits.push_back(c);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
562 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
563 else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
564 assert(gIR->irDsymbol[cd].irStruct->classInfo);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
565 llvm::Constant* c = gIR->irDsymbol[cd].irStruct->classInfo;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
566 sinits.push_back(c);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
567 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
568 else
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
569 assert(0);
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
570 }
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
571
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
572 const llvm::StructType* svtbl_ty = isaStruct(gIR->irType[ts].vtblType->get());
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
573
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
574 #if 0
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
575 for (size_t i=0; i< sinits.size(); ++i)
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
576 {
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
577 Logger::cout() << "field[" << i << "] = " << *svtbl_ty->getElementType(i) << '\n';
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
578 Logger::cout() << "init [" << i << "] = " << *sinits[i]->getType() << '\n';
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
579 assert(svtbl_ty->getElementType(i) == sinits[i]->getType());
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
580 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
581 #endif
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
582
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
583 llvm::Constant* cvtblInit = llvm::ConstantStruct::get(svtbl_ty, sinits);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
584 gIR->irDsymbol[cd].irStruct->constVtbl = llvm::cast<llvm::ConstantStruct>(cvtblInit);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
585
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
586 // create interface vtable const initalizers
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
587 for (IrStruct::InterfaceVectorIter i=irstruct->interfaceVec.begin(); i!=irstruct->interfaceVec.end(); ++i)
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
588 {
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
589 IrInterface* iri = *i;
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
590 BaseClass* b = iri->base;
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
591
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
592 ClassDeclaration* id = iri->decl;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
593 assert(id->type->ty == Tclass);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
594 TypeClass* its = (TypeClass*)id->type;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
595
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
596 const llvm::StructType* ivtbl_ty = isaStruct(gIR->irType[its].vtblType->get());
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
597
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
598 // generate interface info initializer
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
599 std::vector<llvm::Constant*> infoInits;
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
600
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
601 // classinfo
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
602 assert(gIR->irDsymbol[id].irStruct->classInfo);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
603 llvm::Constant* c = gIR->irDsymbol[id].irStruct->classInfo;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
604 infoInits.push_back(c);
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
605
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
606 // vtbl
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
607 const llvm::Type* byteptrptrty = getPtrToType(getPtrToType(llvm::Type::Int8Ty));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
608 c = llvm::ConstantExpr::getBitCast(iri->vtbl, byteptrptrty);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
609 c = DtoConstSlice(DtoConstSize_t(b->vtbl.dim), c);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
610 infoInits.push_back(c);
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
611
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
612 // offset
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
613 // generate target independent offset with constGEP
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
614 /*llvm::Value* cidx = DtoConstInt(iri->index);
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
615 Logger::cout() << "offset to interface in class type: " << *gIR->irType[cd->type].type->get() << '\n';
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
616 size_t ioff = gTargetData->getIndexedOffset(gIR->irType[cd->type].type->get(), &cidx, 1);
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
617 infoInits.push_back(DtoConstUint(ioff));*/
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
618 assert(iri->index >= 0);
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
619 size_t ioff = gTargetData->getStructLayout(isaStruct(gIR->irType[cd->type].type->get()))->getElementOffset(iri->index);
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
620 infoInits.push_back(DtoConstUint(ioff));
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
621
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
622 // create interface info initializer constant
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
623 iri->infoInit = llvm::cast<llvm::ConstantStruct>(llvm::ConstantStruct::get(iri->infoTy, infoInits));
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
624
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
625 // generate vtable initializer
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
626 std::vector<llvm::Constant*> iinits;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
627
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
628 // add interface info
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
629 iinits.push_back(iri->info);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
630
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
631 for (int k=1; k < b->vtbl.dim; k++)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
632 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
633 Logger::println("interface vtbl const init nr. %d", k);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
634 Dsymbol* dsym = (Dsymbol*)b->vtbl.data[k];
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
635 assert(dsym);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
636 FuncDeclaration* fd = dsym->isFuncDeclaration();
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
637 assert(fd);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
638 DtoForceDeclareDsymbol(fd);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
639 assert(gIR->irDsymbol[fd].irFunc->func);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
640 llvm::Constant* c = llvm::cast<llvm::Constant>(gIR->irDsymbol[fd].irFunc->func);
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
641
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
642 // we have to bitcast, as the type created in ResolveClass expects a different this type
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
643 c = llvm::ConstantExpr::getBitCast(c, iri->vtblTy->getContainedType(k));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
644 iinits.push_back(c);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
645 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
646
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
647 #if 0
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
648 for (size_t x=0; x< iinits.size(); ++x)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
649 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
650 Logger::cout() << "field[" << x << "] = " << *ivtbl_ty->getElementType(x) << "\n\n";
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
651 Logger::cout() << "init [" << x << "] = " << *iinits[x] << "\n\n";
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
652 assert(ivtbl_ty->getElementType(x) == iinits[x]->getType());
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
653 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
654 #endif
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
655
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
656 llvm::Constant* civtblInit = llvm::ConstantStruct::get(ivtbl_ty, iinits);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
657 iri->vtblInit = llvm::cast<llvm::ConstantStruct>(civtblInit);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
658 }
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
659 }
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
660 // we always generate interfaceinfos as best we can
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
661 /*else
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
662 {
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
663 for (IrStruct::InterfaceIter i=irstruct->interfaces.begin(); i!=irstruct->interfaces.end(); ++i)
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
664 {
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
665 ClassDeclaration* id = i->first;
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
666 assert(id->type->ty == Tclass);
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
667 TypeClass* its = (TypeClass*)id->type;
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
668
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
669 IrInterface* iri = i->second;
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
670 BaseClass* b = iri->base;
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
671
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
672 // generate interface info initializer
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
673 std::vector<llvm::Constant*> infoInits;
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
674
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
675 // classinfo
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
676 assert(gIR->irDsymbol[id].irStruct->classInfo);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
677 llvm::Constant* c = gIR->irDsymbol[id].irStruct->classInfo;
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
678 infoInits.push_back(c);
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
679
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
680 // vtbl
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
681 const llvm::Type* byteptrptrty = getPtrToType(getPtrToType(llvm::Type::Int8Ty));
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
682 c = DtoConstSlice(DtoConstSize_t(0), getNullPtr(byteptrptrty));
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
683 infoInits.push_back(c);
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
684
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
685 // offset
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
686 infoInits.push_back(DtoConstInt(0));
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
687
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
688 // create interface info initializer constant
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
689 iri->infoInit = llvm::cast<llvm::ConstantStruct>(llvm::ConstantStruct::get(iri->infoTy, infoInits));
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
690 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
691 }*/
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
692
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
693 gIR->classes.pop_back();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
694 gIR->structs.pop_back();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
695 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
696
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
697 //////////////////////////////////////////////////////////////////////////////////////////
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
698
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
699 void DtoDefineClass(ClassDeclaration* cd)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
700 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
701 if (gIR->irDsymbol[cd].defined) return;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
702 gIR->irDsymbol[cd].defined = true;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
703
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
704 Logger::println("DtoDefineClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
705 LOG_SCOPE;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
706
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
707 // get the struct (class) type
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
708 assert(cd->type->ty == Tclass);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
709 TypeClass* ts = (TypeClass*)cd->type;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
710
147
0636f6269dfd [svn r152] Relates to ticket #34. Always emit class definition if it is a template instance. Linkage still needs to be fixed.
ChristianK
parents: 142
diff changeset
711 if (cd->getModule() == gIR->dmodule || DtoIsTemplateInstance(cd)) {
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
712 // interfaces don't have initializers
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
713 // neither do abstract classes
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
714 if (!cd->isInterfaceDeclaration() && !cd->isAbstract())
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
715 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
716 gIR->irDsymbol[cd].irStruct->init->setInitializer(gIR->irDsymbol[cd].irStruct->constInit);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
717 gIR->irDsymbol[cd].irStruct->vtbl->setInitializer(gIR->irDsymbol[cd].irStruct->constVtbl);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
718
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
719 // initialize interface vtables
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
720 IrStruct* irstruct = gIR->irDsymbol[cd].irStruct;
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
721 std::vector<llvm::Constant*> infoInits;
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
722 for (IrStruct::InterfaceVectorIter i=irstruct->interfaceVec.begin(); i!=irstruct->interfaceVec.end(); ++i)
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
723 {
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
724 IrInterface* iri = *i;
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
725 iri->vtbl->setInitializer(iri->vtblInit);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
726 infoInits.push_back(iri->infoInit);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
727 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
728 // initialize interface info array
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
729 if (!infoInits.empty())
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
730 {
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
731 llvm::Constant* arrInit = llvm::ConstantArray::get(irstruct->interfaceInfosTy, infoInits);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
732 irstruct->interfaceInfos->setInitializer(arrInit);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
733 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
734 }
123
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
735
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
736 // generate classinfo
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
737 DtoDefineClassInfo(cd);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
738 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
739 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
740
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
741 //////////////////////////////////////////////////////////////////////////////////////////
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
742
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
743 DValue* DtoNewClass(TypeClass* tc, NewExp* newexp)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
744 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
745 // resolve type
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
746 DtoForceDeclareDsymbol(tc->sym);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
747
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
748 // allocate
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
749 llvm::Value* mem;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
750 if (newexp->onstack)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
751 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
752 mem = new llvm::AllocaInst(DtoType(tc)->getContainedType(0), "newclass_alloca", gIR->topallocapoint());
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
753 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
754 else
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
755 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
756 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_newclass");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
757 std::vector<llvm::Value*> args;
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
758 args.push_back(gIR->irDsymbol[tc->sym].irStruct->classInfo);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
759 mem = gIR->ir->CreateCall(fn, args.begin(), args.end(), "newclass_gc_alloc");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
760 mem = DtoBitCast(mem, DtoType(tc), "newclass_gc");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
761 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
762
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
763 // init
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
764 DtoInitClass(tc, mem);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
765
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
766 // init inner-class outer reference
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
767 if (newexp->thisexp)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
768 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
769 Logger::println("Resolving outer class");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
770 LOG_SCOPE;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
771 DValue* thisval = newexp->thisexp->toElem(gIR);
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
772 size_t idx = 2 + gIR->irDsymbol[tc->sym->vthis].irField->index;
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
773 llvm::Value* src = thisval->getRVal();
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
774 llvm::Value* dst = DtoGEPi(mem,0,idx,"tmp");
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
775 Logger::cout() << "dst: " << *dst << "\nsrc: " << *src << '\n';
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
776 DtoStore(src, dst);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
777 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
778 // set the context for nested classes
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
779 else if (tc->sym->isNested())
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
780 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
781 Logger::println("Resolving nested context");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
782 LOG_SCOPE;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
783 size_t idx = 2;
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
784 //idx += gIR->irDsymbol[tc->sym].irStruct->interfaces.size();
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
785 llvm::Value* nest = gIR->irDsymbol[gIR->func()->decl].irFunc->nestedVar;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
786 if (!nest)
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
787 nest = gIR->irDsymbol[gIR->func()->decl].irFunc->thisVar;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
788 assert(nest);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
789 llvm::Value* gep = DtoGEPi(mem,0,idx,"tmp");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
790 nest = DtoBitCast(nest, gep->getType()->getContainedType(0));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
791 DtoStore(nest, gep);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
792 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
793
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
794 // call constructor
160
b77664331d06 [svn r176] Fixed a bug with class constructors.
lindquist
parents: 157
diff changeset
795 if (newexp->member)
b77664331d06 [svn r176] Fixed a bug with class constructors.
lindquist
parents: 157
diff changeset
796 {
b77664331d06 [svn r176] Fixed a bug with class constructors.
lindquist
parents: 157
diff changeset
797 assert(newexp->arguments != NULL);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
798 return DtoCallClassCtor(tc, newexp->member, newexp->arguments, mem);
160
b77664331d06 [svn r176] Fixed a bug with class constructors.
lindquist
parents: 157
diff changeset
799 }
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
800
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
801 // return default constructed class
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
802 return new DImValue(tc, mem, false);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
803 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
804
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
805 //////////////////////////////////////////////////////////////////////////////////////////
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
806
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
807 void DtoInitClass(TypeClass* tc, llvm::Value* dst)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
808 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
809 size_t presz = 2*getABITypeSize(DtoSize_t());
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
810 uint64_t n = getABITypeSize(gIR->irType[tc].type->get()) - presz;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
811
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
812 // set vtable field seperately, this might give better optimization
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
813 assert(gIR->irDsymbol[tc->sym].irStruct->vtbl);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
814 DtoStore(gIR->irDsymbol[tc->sym].irStruct->vtbl, DtoGEPi(dst,0,0,"vtbl"));
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
815
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
816 // monitor always defaults to zero
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
817 llvm::Value* tmp = DtoGEPi(dst,0,1,"monitor");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
818 DtoStore(llvm::Constant::getNullValue(tmp->getType()->getContainedType(0)), tmp);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
819
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
820 // done?
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
821 if (n == 0)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
822 return;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
823
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
824 // copy the rest from the static initializer
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
825 assert(gIR->irDsymbol[tc->sym].irStruct->init);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
826 assert(dst->getType() == gIR->irDsymbol[tc->sym].irStruct->init->getType());
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
827
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
828 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
829
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
830 llvm::Value* dstarr = DtoGEPi(dst,0,2,"tmp");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
831 dstarr = DtoBitCast(dstarr, arrty);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
832
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
833 llvm::Value* srcarr = DtoGEPi(gIR->irDsymbol[tc->sym].irStruct->init,0,2,"tmp");
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
834 srcarr = DtoBitCast(srcarr, arrty);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
835
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
836 llvm::Function* fn = LLVM_DeclareMemCpy32();
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
837 std::vector<llvm::Value*> llargs;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
838 llargs.resize(4);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
839 llargs[0] = dstarr;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
840 llargs[1] = srcarr;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
841 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
842 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
843
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
844 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
845 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
846
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
847 //////////////////////////////////////////////////////////////////////////////////////////
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
848
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
849 DValue* DtoCallClassCtor(TypeClass* type, CtorDeclaration* ctor, Array* arguments, llvm::Value* mem)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
850 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
851 Logger::println("Calling constructor");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
852 LOG_SCOPE;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
853
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
854 assert(ctor);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
855 DtoForceDeclareDsymbol(ctor);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
856 llvm::Function* fn = gIR->irDsymbol[ctor].irFunc->func;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
857 TypeFunction* tf = (TypeFunction*)DtoDType(ctor->type);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
858
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
859 std::vector<llvm::Value*> ctorargs;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
860 ctorargs.push_back(mem);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
861 for (size_t i=0; i<arguments->dim; ++i)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
862 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
863 Expression* ex = (Expression*)arguments->data[i];
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
864 Argument* fnarg = Argument::getNth(tf->parameters, i);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
865 DValue* argval = DtoArgument(fnarg, ex);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
866 llvm::Value* a = argval->getRVal();
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
867 const llvm::Type* aty = fn->getFunctionType()->getParamType(i+1);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
868 if (a->getType() != aty)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
869 a = DtoBitCast(a, aty);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
870 ctorargs.push_back(a);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
871 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
872 llvm::CallInst* call = new llvm::CallInst(fn, ctorargs.begin(), ctorargs.end(), "tmp", gIR->scopebb());
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
873 call->setCallingConv(DtoCallingConv(LINKd));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
874
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
875 return new DImValue(type, call, false);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
876 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
877
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
878 //////////////////////////////////////////////////////////////////////////////////////////
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
879
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
880 void DtoCallClassDtors(TypeClass* tc, llvm::Value* instance)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
881 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
882 Array* arr = &tc->sym->dtors;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
883 for (size_t i=0; i<arr->dim; i++)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
884 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
885 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i];
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
886 assert(gIR->irDsymbol[fd].irFunc->func);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
887 new llvm::CallInst(gIR->irDsymbol[fd].irFunc->func, instance, "", gIR->scopebb());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
888 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
889 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
890
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
891 //////////////////////////////////////////////////////////////////////////////////////////
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
892
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
893 DValue* DtoCastClass(DValue* val, Type* _to)
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
894 {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
895 Logger::println("DtoCastClass(%s, %s)", val->getType()->toChars(), _to->toChars());
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
896 LOG_SCOPE;
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
897
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
898 Type* to = DtoDType(_to);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
899 if (to->ty == Tpointer) {
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
900 const llvm::Type* tolltype = DtoType(_to);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
901 llvm::Value* rval = DtoBitCast(val->getRVal(), tolltype);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
902 return new DImValue(_to, rval);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
903 }
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
904
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
905 assert(to->ty == Tclass);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
906 TypeClass* tc = (TypeClass*)to;
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
907
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
908 Type* from = DtoDType(val->getType());
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
909 TypeClass* fc = (TypeClass*)from;
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
910
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
911 if (tc->sym->isInterfaceDeclaration()) {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
912 Logger::println("to interface");
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
913 if (fc->sym->isInterfaceDeclaration()) {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
914 Logger::println("from interface");
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
915 return DtoDynamicCastInterface(val, _to);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
916 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
917 else {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
918 Logger::println("from object");
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
919 return DtoDynamicCastObject(val, _to);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
920 }
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
921 }
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
922 else {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
923 Logger::println("to object");
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
924 int poffset;
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
925 if (fc->sym->isInterfaceDeclaration()) {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
926 Logger::println("interface cast");
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
927 return DtoCastInterfaceToObject(val, _to);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
928 }
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
929 else if (!tc->sym->isInterfaceDeclaration() && tc->sym->isBaseOf(fc->sym,NULL)) {
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
930 Logger::println("static down cast)");
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
931 const llvm::Type* tolltype = DtoType(_to);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
932 llvm::Value* rval = DtoBitCast(val->getRVal(), tolltype);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
933 return new DImValue(_to, rval);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
934 }
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
935 else {
138
aeddd4d533b3 [svn r142] minor fix to dynamic casts.
lindquist
parents: 137
diff changeset
936 Logger::println("dynamic up cast");
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
937 return DtoDynamicCastObject(val, _to);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
938 }
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
939 }
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
940 }
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
941
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
942 //////////////////////////////////////////////////////////////////////////////////////////
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
943
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
944 DValue* DtoDynamicCastObject(DValue* val, Type* _to)
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
945 {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
946 // call:
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
947 // Object _d_dynamic_cast(Object o, ClassInfo c)
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
948
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
949 DtoForceDeclareDsymbol(ClassDeclaration::object);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
950 DtoForceDeclareDsymbol(ClassDeclaration::classinfo);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
951
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
952 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_d_dynamic_cast");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
953 const llvm::FunctionType* funcTy = func->getFunctionType();
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
954
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
955 std::vector<llvm::Value*> args;
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
956
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
957 // Object o
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
958 llvm::Value* tmp = val->getRVal();
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
959 tmp = DtoBitCast(tmp, funcTy->getParamType(0));
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
960 args.push_back(tmp);
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
961 assert(funcTy->getParamType(0) == tmp->getType());
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
962
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
963 // ClassInfo c
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
964 TypeClass* to = (TypeClass*)DtoDType(_to);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
965 DtoForceDeclareDsymbol(to->sym);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
966 assert(gIR->irDsymbol[to->sym].irStruct->classInfo);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
967 tmp = gIR->irDsymbol[to->sym].irStruct->classInfo;
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
968 // unfortunately this is needed as the implementation of object differs somehow from the declaration
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
969 // this could happen in user code as well :/
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
970 tmp = DtoBitCast(tmp, funcTy->getParamType(1));
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
971 args.push_back(tmp);
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
972 assert(funcTy->getParamType(1) == tmp->getType());
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
973
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
974 // call it
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
975 llvm::Value* ret = gIR->ir->CreateCall(func, args.begin(), args.end(), "tmp");
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
976
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
977 // cast return value
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
978 ret = DtoBitCast(ret, DtoType(_to));
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
979
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
980 return new DImValue(_to, ret);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
981 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
982
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
983 //////////////////////////////////////////////////////////////////////////////////////////
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
984
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
985 DValue* DtoCastInterfaceToObject(DValue* val, Type* to)
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
986 {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
987 // call:
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
988 // Object _d_toObject(void* p)
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
989
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
990 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_d_toObject");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
991 const llvm::FunctionType* funcTy = func->getFunctionType();
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
992
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
993 // void* p
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
994 llvm::Value* tmp = val->getRVal();
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
995 tmp = DtoBitCast(tmp, funcTy->getParamType(0));
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
996
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
997 // call it
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
998 llvm::Value* ret = gIR->ir->CreateCall(func, tmp, "tmp");
114
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
999
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1000 // cast return value
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1001 if (to != NULL)
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1002 ret = DtoBitCast(ret, DtoType(to));
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1003 else
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1004 to = ClassDeclaration::object->type;
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1005
5880c12dba83 [svn r118] Fixed dynamic casts.
lindquist
parents: 113
diff changeset
1006 return new DImValue(to, ret);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1007 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1008
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1009 //////////////////////////////////////////////////////////////////////////////////////////
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1010
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1011 DValue* DtoDynamicCastInterface(DValue* val, Type* _to)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1012 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1013 // call:
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1014 // Object _d_interface_cast(void* p, ClassInfo c)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1015
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1016 DtoForceDeclareDsymbol(ClassDeclaration::object);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1017 DtoForceDeclareDsymbol(ClassDeclaration::classinfo);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1018
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1019 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_d_interface_cast");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1020 const llvm::FunctionType* funcTy = func->getFunctionType();
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1021
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1022 std::vector<llvm::Value*> args;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1023
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1024 // void* p
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1025 llvm::Value* tmp = val->getRVal();
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1026 tmp = DtoBitCast(tmp, funcTy->getParamType(0));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1027 args.push_back(tmp);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1028
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1029 // ClassInfo c
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1030 TypeClass* to = (TypeClass*)DtoDType(_to);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1031 DtoForceDeclareDsymbol(to->sym);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1032 assert(gIR->irDsymbol[to->sym].irStruct->classInfo);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1033 tmp = gIR->irDsymbol[to->sym].irStruct->classInfo;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1034 // unfortunately this is needed as the implementation of object differs somehow from the declaration
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1035 // this could happen in user code as well :/
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1036 tmp = DtoBitCast(tmp, funcTy->getParamType(1));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1037 args.push_back(tmp);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1038
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1039 // call it
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1040 llvm::Value* ret = gIR->ir->CreateCall(func, args.begin(), args.end(), "tmp");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1041
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1042 // cast return value
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1043 ret = DtoBitCast(ret, DtoType(_to));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1044
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1045 return new DImValue(_to, ret);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1046 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1047
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1048 //////////////////////////////////////////////////////////////////////////////////////////
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1049
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1050 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1051 {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1052 // start at the bottom of the inheritance chain
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1053 if (cd->baseClass != 0) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1054 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1055 if (o != (unsigned)-1)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1056 return o;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1057 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1058
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1059 // check this class
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1060 unsigned i;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1061 for (i=0; i<cd->fields.dim; ++i) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1062 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i];
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1063 if (os == vd->offset)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1064 return i+idx;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1065 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1066 idx += i;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1067
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1068 return (unsigned)-1;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1069 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1070
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1071 //////////////////////////////////////////////////////////////////////////////////////////
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1072
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1073 void ClassDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1074 {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1075 unsigned idx = 0;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1076 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1077 assert(r != (unsigned)-1 && "Offset not found in any aggregate field");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1078 // vtable is 0, monitor is 1
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1079 r += 2;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1080 // interface offset further
142
a123dca8349b [svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
lindquist
parents: 138
diff changeset
1081 //r += vtblInterfaces->dim;
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1082 // the final index was not pushed
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1083 result.push_back(r);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1084 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1085
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1086 //////////////////////////////////////////////////////////////////////////////////////////
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1087
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1088 llvm::Value* DtoIndexClass(llvm::Value* ptr, ClassDeclaration* cd, Type* t, unsigned os, std::vector<unsigned>& idxs)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1089 {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1090 Logger::println("checking for offset %u type %s:", os, t->toChars());
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1091 LOG_SCOPE;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1092
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1093 if (idxs.empty())
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1094 idxs.push_back(0);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1095
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1096 const llvm::Type* st = DtoType(cd->type);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1097 if (ptr->getType() != st) {
163
a8cd9bc1021a [svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents: 160
diff changeset
1098 //assert(gIR->irDsymbol[cd].irStruct->hasUnions);
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1099 ptr = gIR->ir->CreateBitCast(ptr, st, "tmp");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1100 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1101
163
a8cd9bc1021a [svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents: 160
diff changeset
1102 const llvm::Type* llt = getPtrToType(DtoType(t));
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
1103 unsigned dataoffset = 2;
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1104
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1105 IrStruct* irstruct = gIR->irDsymbol[cd].irStruct;
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents: 133
diff changeset
1106 for (IrStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) {
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1107 //for (unsigned i=0; i<cd->fields.dim; ++i) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1108 //VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i];
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1109 VarDeclaration* vd = i->second.var;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1110 assert(vd);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1111 Type* vdtype = DtoDType(vd->type);
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
1112 //Logger::println("found %u type %s", vd->offset, vdtype->toChars());
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1113 assert(gIR->irDsymbol[vd].irField->index >= 0);
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1114 if (os == vd->offset && vdtype == t) {
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
1115 Logger::println("found %s %s", vdtype->toChars(), vd->toChars());
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1116 idxs.push_back(gIR->irDsymbol[vd].irField->index + dataoffset);
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
1117 //Logger::cout() << "indexing: " << *ptr << '\n';
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1118 ptr = DtoGEP(ptr, idxs, "tmp");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1119 if (ptr->getType() != llt)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1120 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp");
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
1121 //Logger::cout() << "indexing: " << *ptr << '\n';
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1122 if (gIR->irDsymbol[vd].irField->indexOffset)
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1123 ptr = new llvm::GetElementPtrInst(ptr, DtoConstUint(gIR->irDsymbol[vd].irField->indexOffset), "tmp", gIR->scopebb());
169
2df270e1ba59 [svn r185] Fixed broken nested classes with data members, did DMD change the class layout? tango.text.Regex now compiles.
lindquist
parents: 163
diff changeset
1124 //Logger::cout() << "indexing: " << *ptr << '\n';
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1125 return ptr;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1126 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1127 else if (vdtype->ty == Tstruct && (vd->offset + vdtype->size()) > os) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1128 TypeStruct* ts = (TypeStruct*)vdtype;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1129 StructDeclaration* ssd = ts->sym;
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1130 idxs.push_back(gIR->irDsymbol[vd].irField->index + dataoffset);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1131 if (gIR->irDsymbol[vd].irField->indexOffset) {
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1132 Logger::println("has union field offset");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1133 ptr = DtoGEP(ptr, idxs, "tmp");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1134 if (ptr->getType() != llt)
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1135 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp");
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1136 ptr = new llvm::GetElementPtrInst(ptr, DtoConstUint(gIR->irDsymbol[vd].irField->indexOffset), "tmp", gIR->scopebb());
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1137 std::vector<unsigned> tmp;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1138 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1139 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1140 else {
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1141 const llvm::Type* sty = getPtrToType(DtoType(vd->type));
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1142 if (ptr->getType() != sty) {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1143 ptr = gIR->ir->CreateBitCast(ptr, sty, "tmp");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1144 std::vector<unsigned> tmp;
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1145 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1146 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1147 else {
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1148 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, idxs);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1149 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1150 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1151 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1152 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1153
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1154 assert(0);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1155
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1156 size_t llt_sz = getABITypeSize(llt->getContainedType(0));
132
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1157 assert(os % llt_sz == 0);
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1158 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp");
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1159 return new llvm::GetElementPtrInst(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb());
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1160 }
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1161
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1162 //////////////////////////////////////////////////////////////////////////////////////////
1700239cab2e [svn r136] MAJOR UNSTABLE UPDATE!!!
lindquist
parents: 123
diff changeset
1163
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1164 llvm::Value* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl)
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1165 {
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1166 assert(fdecl->isVirtual());//fdecl->isAbstract() || (!fdecl->isFinal() && fdecl->isVirtual()));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1167 assert(fdecl->vtblIndex > 0);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1168 assert(DtoDType(inst->getType())->ty == Tclass);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1169
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1170 llvm::Value* vthis = inst->getRVal();
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1171 //Logger::cout() << "vthis: " << *vthis << '\n';
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1172
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1173 llvm::Value* funcval;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1174 funcval = DtoGEPi(vthis, 0, 0, "tmp");
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1175 funcval = DtoLoad(funcval);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1176 funcval = DtoGEPi(funcval, 0, fdecl->vtblIndex, fdecl->toPrettyChars());
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1177 funcval = DtoLoad(funcval);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1178
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1179 //assert(funcval->getType() == DtoType(fdecl->type));
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1180 //cc = DtoCallingConv(fdecl->linkage);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1181
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1182 return funcval;
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1183 }
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1184
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1185 //////////////////////////////////////////////////////////////////////////////////////////
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1186
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1187 void DtoDeclareClassInfo(ClassDeclaration* cd)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1188 {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1189 if (gIR->irDsymbol[cd].irStruct->classDeclared) return;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1190 gIR->irDsymbol[cd].irStruct->classDeclared = true;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1191
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1192 Logger::println("DtoDeclareClassInfo(%s)", cd->toChars());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1193 LOG_SCOPE;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1194
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1195 ClassDeclaration* cinfo = ClassDeclaration::classinfo;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1196 DtoResolveClass(cinfo);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1197
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1198 std::string gname("_D");
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1199 gname.append(cd->mangle());
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1200 if (!cd->isInterfaceDeclaration())
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1201 gname.append("7__ClassZ");
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1202 else
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1203 gname.append("11__InterfaceZ");
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1204
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
1205 const llvm::Type* st = gIR->irType[cinfo->type].type->get();
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1206
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1207 gIR->irDsymbol[cd].irStruct->classInfo = new llvm::GlobalVariable(st, true, DtoLinkage(cd), NULL, gname, gIR->module);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1208 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1209
110
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1210 static llvm::Constant* build_offti_entry(VarDeclaration* vd)
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1211 {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1212 std::vector<const llvm::Type*> types;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1213 std::vector<llvm::Constant*> inits;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1214
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1215 types.push_back(DtoSize_t());
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1216
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1217 size_t offset = vd->offset; // TODO might not be the true offset
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1218 // dmd only accounts for the vtable, not classinfo or monitor
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1219 if (global.params.is64bit)
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1220 offset += 8;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1221 else
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1222 offset += 4;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1223 inits.push_back(DtoConstSize_t(offset));
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1224
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1225 vd->type->getTypeInfo(NULL);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1226 assert(vd->type->vtinfo);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1227 DtoForceDeclareDsymbol(vd->type->vtinfo);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1228 llvm::Constant* c = isaConstant(gIR->irDsymbol[vd->type->vtinfo].getIrValue());
110
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1229
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
1230 const llvm::Type* tiTy = getPtrToType(gIR->irType[Type::typeinfo->type].type->get());
123
7f9a0a58394b [svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents: 121
diff changeset
1231 //Logger::cout() << "tiTy = " << *tiTy << '\n';
110
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1232
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1233 types.push_back(tiTy);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1234 inits.push_back(llvm::ConstantExpr::getBitCast(c, tiTy));
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1235
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1236 const llvm::StructType* sTy = llvm::StructType::get(types);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1237 return llvm::ConstantStruct::get(sTy, inits);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1238 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1239
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1240 static llvm::Constant* build_offti_array(ClassDeclaration* cd, llvm::Constant* init)
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1241 {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1242 const llvm::StructType* initTy = isaStruct(init->getType());
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1243 assert(initTy);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1244
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1245 std::vector<llvm::Constant*> arrayInits;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1246 for (ClassDeclaration *cd2 = cd; cd2; cd2 = cd2->baseClass)
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1247 {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1248 if (cd2->members)
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1249 {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1250 for (size_t i = 0; i < cd2->members->dim; i++)
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1251 {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1252 Dsymbol *sm = (Dsymbol *)cd2->members->data[i];
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1253 if (VarDeclaration* vd = sm->isVarDeclaration()) // is this enough?
110
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1254 {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1255 llvm::Constant* c = build_offti_entry(vd);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1256 assert(c);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1257 arrayInits.push_back(c);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1258 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1259 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1260 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1261 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1262
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1263 size_t ninits = arrayInits.size();
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1264 llvm::Constant* size = DtoConstSize_t(ninits);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1265 llvm::Constant* ptr;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1266
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1267 if (ninits > 0) {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1268 // OffsetTypeInfo type
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1269 std::vector<const llvm::Type*> elemtypes;
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1270 elemtypes.push_back(DtoSize_t());
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
1271 const llvm::Type* tiTy = getPtrToType(gIR->irType[Type::typeinfo->type].type->get());
110
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1272 elemtypes.push_back(tiTy);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1273 const llvm::StructType* sTy = llvm::StructType::get(elemtypes);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1274
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1275 // array type
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1276 const llvm::ArrayType* arrTy = llvm::ArrayType::get(sTy, ninits);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1277 llvm::Constant* arrInit = llvm::ConstantArray::get(arrTy, arrayInits);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1278
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1279 std::string name(cd->type->vtinfo->toChars());
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1280 name.append("__OffsetTypeInfos");
149
4c577c2b7229 [svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents: 147
diff changeset
1281
4c577c2b7229 [svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents: 147
diff changeset
1282 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(arrTy,true,DtoInternalLinkage(cd),arrInit,name,gIR->module);
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1283 ptr = llvm::ConstantExpr::getBitCast(gvar, getPtrToType(sTy));
110
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1284 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1285 else {
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1286 ptr = llvm::ConstantPointerNull::get(isaPointer(initTy->getElementType(1)));
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1287 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1288
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1289 return DtoConstSlice(size, ptr);
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1290 }
e8da7856a260 [svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents: 106
diff changeset
1291
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1292 static llvm::Constant* build_class_dtor(ClassDeclaration* cd)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1293 {
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1294 // construct the function
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1295 std::vector<const llvm::Type*> paramTypes;
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 156
diff changeset
1296 paramTypes.push_back(getPtrToType(gIR->irType[cd->type].type->get()));
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1297
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1298 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy, paramTypes, false);
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1299
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1300 if (cd->dtors.dim == 0) {
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1301 return llvm::ConstantPointerNull::get(getPtrToType(llvm::Type::Int8Ty));
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1302 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1303 else if (cd->dtors.dim == 1) {
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1304 DtorDeclaration *d = (DtorDeclaration *)cd->dtors.data[0];
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1305 DtoForceDeclareDsymbol(d);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1306 assert(gIR->irDsymbol[d].irFunc->func);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1307 return llvm::ConstantExpr::getBitCast(isaConstant(gIR->irDsymbol[d].irFunc->func), getPtrToType(llvm::Type::Int8Ty));
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1308 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1309
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1310 std::string gname("_D");
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1311 gname.append(cd->mangle());
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1312 gname.append("12__destructorMFZv");
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1313
149
4c577c2b7229 [svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents: 147
diff changeset
1314 llvm::Function* func = new llvm::Function(fnTy, DtoInternalLinkage(cd), gname, gIR->module);
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1315 llvm::Value* thisptr = func->arg_begin();
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1316 thisptr->setName("this");
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1317
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1318 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", func);
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1319 LLVMBuilder builder(bb);
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1320
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1321 for (size_t i = 0; i < cd->dtors.dim; i++)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1322 {
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1323 DtorDeclaration *d = (DtorDeclaration *)cd->dtors.data[i];
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1324 DtoForceDeclareDsymbol(d);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1325 assert(gIR->irDsymbol[d].irFunc->func);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1326 builder.CreateCall(gIR->irDsymbol[d].irFunc->func, thisptr);
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1327 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1328 builder.CreateRetVoid();
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1329
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1330 return llvm::ConstantExpr::getBitCast(func, getPtrToType(llvm::Type::Int8Ty));
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1331 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1332
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1333 static uint build_classinfo_flags(ClassDeclaration* cd)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1334 {
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1335 // adapted from original dmd code
112
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1336 uint flags = 0;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1337 //flags |= isCOMclass(); // IUnknown
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1338 bool hasOffTi = false;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1339 if (cd->ctor) flags |= 8;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1340 for (ClassDeclaration *cd2 = cd; cd2; cd2 = cd2->baseClass)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1341 {
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1342 if (cd2->members)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1343 {
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1344 for (size_t i = 0; i < cd2->members->dim; i++)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1345 {
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1346 Dsymbol *sm = (Dsymbol *)cd2->members->data[i];
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1347 if (sm->isVarDeclaration()) // is this enough?
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1348 hasOffTi = true;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1349 //printf("sm = %s %s\n", sm->kind(), sm->toChars());
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1350 if (sm->hasPointers())
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1351 goto L2;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1352 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1353 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1354 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1355 flags |= 2; // no pointers
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1356 L2:
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1357 if (hasOffTi)
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1358 flags |= 4;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1359 return flags;
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1360 }
368547b1cbe6 [svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents: 111
diff changeset
1361
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1362 void DtoDefineClassInfo(ClassDeclaration* cd)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1363 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1364 // The layout is:
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1365 // {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1366 // void **vptr;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1367 // monitor_t monitor;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1368 // byte[] initializer; // static initialization data
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1369 // char[] name; // class name
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1370 // void *[] vtbl;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1371 // Interface[] interfaces;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1372 // ClassInfo *base; // base class
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1373 // void *destructor;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1374 // void *invariant; // class invariant
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1375 // uint flags;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1376 // void *deallocator;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1377 // OffsetTypeInfo[] offTi;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1378 // void *defaultConstructor;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1379 // }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1380
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1381 if (gIR->irDsymbol[cd].irStruct->classDefined) return;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1382 gIR->irDsymbol[cd].irStruct->classDefined = true;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1383
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1384 Logger::println("DtoDefineClassInfo(%s)", cd->toChars());
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1385 LOG_SCOPE;
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1386
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1387 assert(cd->type->ty == Tclass);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1388 assert(gIR->irDsymbol[cd].irStruct->classInfo);
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1389
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1390 TypeClass* cdty = (TypeClass*)cd->type;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1391 if (!cd->isInterfaceDeclaration() && !cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1392 assert(gIR->irDsymbol[cd].irStruct->init);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1393 assert(gIR->irDsymbol[cd].irStruct->constInit);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1394 assert(gIR->irDsymbol[cd].irStruct->vtbl);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1395 assert(gIR->irDsymbol[cd].irStruct->constVtbl);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1396 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1397
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1398 // holds the list of initializers for llvm
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1399 std::vector<llvm::Constant*> inits;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1400
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1401 ClassDeclaration* cinfo = ClassDeclaration::classinfo;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1402 DtoForceConstInitDsymbol(cinfo);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1403 assert(gIR->irDsymbol[cinfo].irStruct->constInit);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1404
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1405 llvm::Constant* c;
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1406
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1407 // own vtable
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1408 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(0);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1409 assert(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1410 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1411
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1412 // monitor
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1413 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(1);
115
5ba6d286c941 [svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents: 114
diff changeset
1414 inits.push_back(c);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1415
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1416 // byte[] init
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1417 const llvm::Type* byteptrty = getPtrToType(llvm::Type::Int8Ty);
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1418 if (cd->isInterfaceDeclaration() || cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1419 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(2);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1420 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1421 else {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1422 c = llvm::ConstantExpr::getBitCast(gIR->irDsymbol[cd].irStruct->init, byteptrty);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1423 assert(!gIR->irDsymbol[cd].irStruct->constInit->getType()->isAbstract());
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1424 size_t initsz = getABITypeSize(gIR->irDsymbol[cd].irStruct->constInit->getType());
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1425 c = DtoConstSlice(DtoConstSize_t(initsz), c);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1426 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1427 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1428
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1429 // class name
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1430 // from dmd
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1431 char *name = cd->ident->toChars();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1432 size_t namelen = strlen(name);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1433 if (!(namelen > 9 && memcmp(name, "TypeInfo_", 9) == 0))
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1434 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1435 name = cd->toPrettyChars();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1436 namelen = strlen(name);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1437 }
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1438 c = DtoConstString(name);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1439 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1440
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1441 // vtbl array
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1442 if (cd->isInterfaceDeclaration() || cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1443 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(4);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1444 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1445 else {
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1446 const llvm::Type* byteptrptrty = getPtrToType(byteptrty);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1447 assert(!gIR->irDsymbol[cd].irStruct->vtbl->getType()->isAbstract());
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1448 c = llvm::ConstantExpr::getBitCast(gIR->irDsymbol[cd].irStruct->vtbl, byteptrptrty);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1449 assert(!gIR->irDsymbol[cd].irStruct->constVtbl->getType()->isAbstract());
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1450 size_t vtblsz = gIR->irDsymbol[cd].irStruct->constVtbl->getType()->getNumElements();
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1451 c = DtoConstSlice(DtoConstSize_t(vtblsz), c);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1452 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1453 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1454
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1455 // interfaces array
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1456 IrStruct* irstruct = gIR->irDsymbol[cd].irStruct;
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1457 if (cd->isInterfaceDeclaration() || !irstruct->interfaceInfos || cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1458 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(5);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1459 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1460 else {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1461 const llvm::Type* t = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(5)->getType()->getContainedType(1);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1462 c = llvm::ConstantExpr::getBitCast(irstruct->interfaceInfos, t);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1463 size_t iisz = irstruct->interfaceInfosTy->getNumElements();
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1464 c = DtoConstSlice(DtoConstSize_t(iisz), c);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1465 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1466 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1467
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1468 // base classinfo
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1469 if (cd->baseClass && !cd->isInterfaceDeclaration() && !cd->isAbstract()) {
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1470 DtoDeclareClassInfo(cd->baseClass);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1471 c = gIR->irDsymbol[cd->baseClass].irStruct->classInfo;
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1472 assert(c);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1473 inits.push_back(c);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1474 }
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1475 else {
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1476 // null
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1477 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(6);
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1478 inits.push_back(c);
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1479 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1480
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1481 // destructor
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1482 if (cd->isInterfaceDeclaration() || cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1483 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(7);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1484 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1485 else {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1486 c = build_class_dtor(cd);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1487 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1488 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1489
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1490 // invariant
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1491 // TODO
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1492 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(8);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1493 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1494
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1495 // uint flags
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1496 if (cd->isInterfaceDeclaration() || cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1497 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(9);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1498 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1499 else {
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1500 uint flags = build_classinfo_flags(cd);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1501 c = DtoConstUint(flags);
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1502 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1503 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1504
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1505 // allocator
102
027b8d8b71ec [svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents: 100
diff changeset
1506 // TODO
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1507 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(10);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1508 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1509
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1510 // offset typeinfo
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1511 if (cd->isInterfaceDeclaration() || cd->isAbstract()) {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1512 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(11);
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1513 }
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1514 else {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1515 c = build_offti_array(cd, gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(11));
113
27b9f749d9fe [svn r117] Initial working implementation of interfaces.
lindquist
parents: 112
diff changeset
1516 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1517 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1518
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1519 // default constructor
133
44a95ac7368a [svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents: 132
diff changeset
1520 if (cd->defaultCtor && !cd->isInterfaceDeclaration() && !cd->isAbstract()) {
111
a7ae554ce4f4 [svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents: 110
diff changeset
1521 DtoForceDeclareDsymbol(cd->defaultCtor);
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1522 c = isaConstant(gIR->irDsymbol[cd->defaultCtor].irFunc->func);
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1523 const llvm::Type* toTy = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(12)->getType();
117
56a21f3e5d3e [svn r121] Finished ModuleInfo implementation.
lindquist
parents: 115
diff changeset
1524 c = llvm::ConstantExpr::getBitCast(c, toTy);
111
a7ae554ce4f4 [svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents: 110
diff changeset
1525 }
a7ae554ce4f4 [svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents: 110
diff changeset
1526 else {
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1527 c = gIR->irDsymbol[cinfo].irStruct->constInit->getOperand(12);
111
a7ae554ce4f4 [svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents: 110
diff changeset
1528 }
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1529 inits.push_back(c);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1530
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1531 /*size_t n = inits.size();
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1532 for (size_t i=0; i<n; ++i)
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1533 {
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1534 Logger::cout() << "inits[" << i << "]: " << *inits[i] << '\n';
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1535 }*/
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1536
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1537 // build the initializer
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1538 const llvm::StructType* st = isaStruct(gIR->irDsymbol[cinfo].irStruct->constInit->getType());
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1539 llvm::Constant* finalinit = llvm::ConstantStruct::get(st, inits);
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1540 //Logger::cout() << "built the classinfo initializer:\n" << *finalinit <<'\n';
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1541
156
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1542 gIR->irDsymbol[cd].irStruct->constClassInfo = finalinit;
ccd07d9f2ce9 [svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
ChristianK
parents: 155
diff changeset
1543 gIR->irDsymbol[cd].irStruct->classInfo->setInitializer(finalinit);
100
5071469303d4 [svn r104] TONS OF FIXES.
lindquist
parents:
diff changeset
1544 }