annotate ir/irclass.cpp @ 1230:e67c85d6e680

Completed interface implementation. Hopefully that's it for now..
author Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
date Thu, 16 Apr 2009 13:18:56 +0200
parents fafe7c8d6734
children 212ec2d9d176
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
1 #include "llvm/Constants.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
2 #include "llvm/DerivedTypes.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
3
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
4 #include "aggregate.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
5 #include "declaration.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
6 #include "mtype.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
7
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
8 #include "gen/irstate.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
9 #include "gen/logger.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
10 #include "gen/tollvm.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
11 #include "gen/llvmhelpers.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
12 #include "gen/utils.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
13 #include "gen/arrays.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
14
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
15 #include "ir/irstruct.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
16 #include "ir/irtypeclass.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
17
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
18 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
19
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
20 extern LLConstant* get_default_initializer(VarDeclaration* vd, Initializer* init);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
21 extern size_t add_zeros(std::vector<llvm::Constant*>& constants, size_t diff);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
22
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
23 extern LLConstant* DtoDefineClassInfo(ClassDeclaration* cd);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
24
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
25 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
26
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
27 LLGlobalVariable * IrStruct::getVtblSymbol()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
28 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
29 if (vtbl)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
30 return vtbl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
31
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
32 // create the initZ symbol
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
33 std::string initname("_D");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
34 initname.append(aggrdecl->mangle());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
35 initname.append("6__vtblZ");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
36
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
37 llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
38
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
39 const LLType* vtblTy = type->irtype->isClass()->getVtbl();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
40
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
41 vtbl = new llvm::GlobalVariable(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
42 vtblTy, true, _linkage, NULL, initname, gIR->module);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
43
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
44 return vtbl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
45 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
46
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
47 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
48
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
49 LLGlobalVariable * IrStruct::getClassInfoSymbol()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
50 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
51 if (classInfo)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
52 return classInfo;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
53
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
54 // create the initZ symbol
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
55 std::string initname("_D");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
56 initname.append(aggrdecl->mangle());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
57 if (aggrdecl->isInterfaceDeclaration())
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
58 initname.append("11__InterfaceZ");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
59 else
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
60 initname.append("7__ClassZ");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
61
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
62 llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
63
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
64 ClassDeclaration* cinfo = ClassDeclaration::classinfo;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
65 DtoType(cinfo->type);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
66 IrTypeClass* tc = cinfo->type->irtype->isClass();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
67 assert(tc && "invalid ClassInfo type");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
68
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
69 classInfo = new llvm::GlobalVariable(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
70 tc->getPA().get(), true, _linkage, NULL, initname, gIR->module);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
71
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
72 return classInfo;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
73 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
74
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
75 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
76
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
77 LLGlobalVariable * IrStruct::getInterfaceArraySymbol()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
78 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
79 if (classInterfacesArray)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
80 return classInterfacesArray;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
81
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
82 ClassDeclaration* cd = aggrdecl->isClassDeclaration();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
83
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
84 assert(cd->vtblInterfaces && cd->vtblInterfaces->dim > 0 &&
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
85 "should not create interface info array for class with no explicit "
1229
fafe7c8d6734 Now compiles again, and fixed type of the Interface[N] ClassInfo symbol.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
86 "interface implementations");
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
87
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
88 VarDeclarationIter idx(ClassDeclaration::classinfo->fields, 3);
1229
fafe7c8d6734 Now compiles again, and fixed type of the Interface[N] ClassInfo symbol.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
89 const llvm::Type* InterfaceTy = DtoType(idx->type->next);
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
90
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
91 // create Interface[N]
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
92 const llvm::ArrayType* array_type = llvm::ArrayType::get(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
93 InterfaceTy,
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
94 type->irtype->isClass()->getNumInterfaceVtbls());
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
95
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
96 // put it in a global
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
97 std::string name("_D");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
98 name.append(cd->mangle());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
99 name.append("16__interfaceInfosZ");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
100 classInterfacesArray = new llvm::GlobalVariable(array_type, true, DtoLinkage(cd), NULL, name, classInfo);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
101
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
102 return classInterfacesArray;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
103 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
104
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
105 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
106
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
107 LLConstant * IrStruct::getVtblInit()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
108 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
109 if (constVtbl)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
110 return constVtbl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
111
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
112 IF_LOG Logger::println("Building vtbl initializer");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
113 LOG_SCOPE;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
114
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
115 ClassDeclaration* cd = aggrdecl->isClassDeclaration();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
116 assert(cd && "not class");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
117
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
118 std::vector<llvm::Constant*> constants;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
119 constants.reserve(cd->vtbl.dim);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
120
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
121 // start with the classinfo
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
122 llvm::Constant* c = getClassInfoSymbol();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
123 c = DtoBitCast(c, DtoType(ClassDeclaration::classinfo->type));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
124 constants.push_back(c);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
125
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
126 // add virtual function pointers
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
127 size_t n = cd->vtbl.dim;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
128 for (size_t i = 1; i < n; i++)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
129 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
130 Dsymbol* dsym = (Dsymbol*)cd->vtbl.data[i];
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
131 assert(dsym && "null vtbl member");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
132
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
133 FuncDeclaration* fd = dsym->isFuncDeclaration();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
134 assert(fd && "vtbl entry not a function");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
135
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
136 if (fd->isAbstract() && !fd->fbody)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
137 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
138 c = getNullValue(DtoType(fd->type->pointerTo()));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
139 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
140 else
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
141 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
142 fd->codegen(Type::sir);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
143 assert(fd->ir.irFunc && "invalid vtbl function");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
144 c = fd->ir.irFunc->func;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
145 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
146 constants.push_back(c);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
147 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
148
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
149 // build the constant struct
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
150 constVtbl = llvm::ConstantStruct::get(constants, false);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
151
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
152 // sanity check
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
153 #if 0
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
154 IF_LOG Logger::cout() << "constVtbl type: " << *constVtbl->getType() << std::endl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
155 IF_LOG Logger::cout() << "vtbl type: " << *type->irtype->isClass()->getVtbl() << std::endl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
156 #endif
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
157
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
158 assert(constVtbl->getType() == type->irtype->isClass()->getVtbl() &&
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
159 "vtbl initializer type mismatch");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
160
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
161 return constVtbl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
162 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
163
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
164 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
165
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
166 LLConstant * IrStruct::getClassInfoInit()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
167 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
168 if (constClassInfo)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
169 return constClassInfo;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
170 constClassInfo = DtoDefineClassInfo(aggrdecl->isClassDeclaration());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
171 return constClassInfo;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
172 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
173
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
174 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
175
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
176 void IrStruct::addBaseClassInits(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
177 std::vector<llvm::Constant*>& constants,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
178 ClassDeclaration* base,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
179 size_t& offset,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
180 size_t& field_index)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
181 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
182 if (base->baseClass)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
183 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
184 addBaseClassInits(constants, base->baseClass, offset, field_index);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
185 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
186
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
187 ArrayIter<VarDeclaration> it(base->fields);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
188 for (; !it.done(); it.next())
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
189 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
190 VarDeclaration* vd = it.get();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
191
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
192 // skip if offset moved backwards
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
193 if (vd->offset < offset)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
194 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
195 IF_LOG Logger::println("Skipping field %s %s (+%u) for default", vd->type->toChars(), vd->toChars(), vd->offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
196 continue;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
197 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
198
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
199 IF_LOG Logger::println("Adding default field %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
200 LOG_SCOPE;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
201
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
202 // get next aligned offset for this type
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
203 size_t alignsize = vd->type->alignsize();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
204 size_t alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
205
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
206 // insert explicit padding?
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
207 if (alignedoffset < vd->offset)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
208 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
209 add_zeros(constants, vd->offset - alignedoffset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
210 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
211
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
212 // add default type
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
213 constants.push_back(get_default_initializer(vd, vd->init));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
214
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
215 // advance offset to right past this field
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
216 offset = vd->offset + vd->type->size();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
217 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
218
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
219 // has interface vtbls?
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
220 if (base->vtblInterfaces)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
221 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
222 // false when it's not okay to use functions from super classes
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
223 bool newinsts = (base == aggrdecl->isClassDeclaration());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
224
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
225 size_t inter_idx = interfacesWithVtbls.size();
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
226
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
227 ArrayIter<BaseClass> it2(*base->vtblInterfaces);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
228 for (; !it2.done(); it2.next())
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
229 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
230 BaseClass* b = it2.get();
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
231 constants.push_back(getInterfaceVtbl(b, newinsts, inter_idx));
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
232 offset += PTRSIZE;
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
233
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
234 // add to the interface list
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
235 interfacesWithVtbls.push_back(b);
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
236 inter_idx++;
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
237 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
238 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
239
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
240 // tail padding?
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
241 if (offset < base->structsize)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
242 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
243 add_zeros(constants, base->structsize - offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
244 offset = base->structsize;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
245 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
246 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
247
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
248 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
249
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
250 LLConstant * IrStruct::createClassDefaultInitializer()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
251 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
252 ClassDeclaration* cd = aggrdecl->isClassDeclaration();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
253 assert(cd && "invalid class aggregate");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
254
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
255 IF_LOG Logger::println("Building class default initializer %s @ %s", cd->toPrettyChars(), cd->locToChars());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
256 LOG_SCOPE;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
257 IF_LOG Logger::println("Instance size: %u", cd->structsize);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
258
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
259 // find the fields that contribute to the default initializer.
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
260 // these will define the default type.
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
261
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
262 std::vector<llvm::Constant*> constants;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
263 constants.reserve(32);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
264
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
265 // add vtbl
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
266 constants.push_back(getVtblSymbol());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
267 // add monitor
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
268 constants.push_back(getNullValue(DtoType(Type::tvoid->pointerTo())));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
269
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
270 // we start right after the vtbl and monitor
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
271 size_t offset = PTRSIZE * 2;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
272 size_t field_index = 2;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
273
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
274 // add data members recursively
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
275 addBaseClassInits(constants, cd, offset, field_index);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
276
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
277 // build the constant
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
278 llvm::Constant* definit = llvm::ConstantStruct::get(constants, false);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
279
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
280 // sanity check
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
281 assert(definit->getType() == type->irtype->getPA().get() && "class initializer type mismatch");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
282
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
283 return definit;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
284 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
285
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
286 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
287
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
288 llvm::GlobalVariable * IrStruct::getInterfaceVtbl(BaseClass * b, bool new_instance, size_t interfaces_index)
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
289 {
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
290 ClassGlobalMap::iterator it = interfaceVtblMap.find(b->base);
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
291 if (it != interfaceVtblMap.end())
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
292 return it->second;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
293
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
294 IF_LOG Logger::println("Building vtbl for implementation of interface %s in class %s",
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
295 b->base->toPrettyChars(), aggrdecl->toPrettyChars());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
296 LOG_SCOPE;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
297
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
298 ClassDeclaration* cd = aggrdecl->isClassDeclaration();
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
299 assert(cd && "not a class aggregate");
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
300
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
301 Array vtbl_array;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
302 b->fillVtbl(cd, &vtbl_array, new_instance);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
303
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
304 std::vector<llvm::Constant*> constants;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
305 constants.reserve(vtbl_array.dim);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
306
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
307 // start with the interface info
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
308 VarDeclarationIter interfaces_idx(ClassDeclaration::classinfo->fields, 3);
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
309 Type* first = interfaces_idx->type->next->pointerTo();
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
310
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
311 // index into the interfaces array
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
312 llvm::Constant* idxs[2] = {
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
313 DtoConstSize_t(0),
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
314 DtoConstSize_t(interfaces_index)
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
315 };
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
316
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
317 llvm::Constant* c = llvm::ConstantExpr::getGetElementPtr(
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
318 getInterfaceArraySymbol(), idxs, 2);
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
319
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
320 constants.push_back(c);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
321
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
322 // add virtual function pointers
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
323 size_t n = vtbl_array.dim;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
324 for (size_t i = 1; i < n; i++)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
325 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
326 Dsymbol* dsym = (Dsymbol*)vtbl_array.data[i];
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
327 assert(dsym && "null vtbl member");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
328
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
329 FuncDeclaration* fd = dsym->isFuncDeclaration();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
330 assert(fd && "vtbl entry not a function");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
331
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
332 assert(!(fd->isAbstract() && !fd->fbody) &&
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
333 "null symbol in interface implementation vtable");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
334
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
335 fd->codegen(Type::sir);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
336 assert(fd->ir.irFunc && "invalid vtbl function");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
337
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
338 constants.push_back(fd->ir.irFunc->func);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
339 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
340
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
341 // build the vtbl constant
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
342 llvm::Constant* vtbl_constant = llvm::ConstantStruct::get(constants, false);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
343
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
344 // create the global variable to hold it
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
345 llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
346
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
347 std::string mangle("_D");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
348 mangle.append(cd->mangle());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
349 mangle.append("11__interface");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
350 mangle.append(b->base->mangle());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
351 mangle.append("6__vtblZ");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
352
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
353 llvm::GlobalVariable* GV = new llvm::GlobalVariable(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
354 vtbl_constant->getType(),
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
355 true,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
356 _linkage,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
357 vtbl_constant,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
358 mangle,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
359 gIR->module
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
360 );
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
361
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
362 // insert into the vtbl map
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
363 interfaceVtblMap.insert(std::make_pair(b->base, GV));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
364
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
365 return GV;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
366 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
367
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
368 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
369
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
370 LLConstant * IrStruct::getClassInfoInterfaces()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
371 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
372 IF_LOG Logger::println("Building ClassInfo.interfaces");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
373 LOG_SCOPE;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
374
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
375 ClassDeclaration* cd = aggrdecl->isClassDeclaration();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
376 assert(cd);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
377
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
378 size_t n = interfacesWithVtbls.size();
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
379 assert(type->irtype->isClass()->getNumInterfaceVtbls() == n &&
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
380 "inconsistent number of interface vtables in this class");
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
381
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
382 if (n == 0)
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
383 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
384 VarDeclarationIter idx(ClassDeclaration::classinfo->fields, 3);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
385 return getNullValue(DtoType(idx->type));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
386 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
387
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
388 // Build array of:
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
389 //
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
390 // struct Interface
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
391 // {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
392 // ClassInfo classinfo;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
393 // void*[] vtbl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
394 // ptrdiff_t offset;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
395 // }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
396
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
397 LLSmallVector<LLConstant*, 6> constants;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
398 constants.reserve(cd->vtblInterfaces->dim);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
399
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
400 const LLType* classinfo_type = DtoType(ClassDeclaration::classinfo->type);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
401 const LLType* voidptrptr_type = DtoType(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
402 Type::tvoid->pointerTo()->pointerTo());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
403
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
404 const LLType* our_type = type->irtype->isClass()->getPA().get();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
405
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
406 for (size_t i = 0; i < n; ++i)
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
407 {
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
408 BaseClass* it = interfacesWithVtbls[i];
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
409
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
410 IF_LOG Logger::println("Adding interface %s", it->base->toPrettyChars());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
411
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
412 IrStruct* irinter = it->base->ir.irStruct;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
413 assert(irinter && "interface has null IrStruct");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
414 IrTypeClass* itc = irinter->type->irtype->isClass();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
415 assert(itc && "null interface IrTypeClass");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
416
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
417 // classinfo
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
418 LLConstant* ci = irinter->getClassInfoSymbol();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
419 ci = DtoBitCast(ci, classinfo_type);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
420
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
421 // vtbl
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
422 ClassGlobalMap::iterator itv = interfaceVtblMap.find(it->base);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
423 assert(itv != interfaceVtblMap.end() && "interface vtbl not found");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
424 LLConstant* vtb = itv->second;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
425 vtb = DtoBitCast(vtb, voidptrptr_type);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
426 vtb = DtoConstSlice(DtoConstSize_t(itc->getVtblSize()), vtb);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
427
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
428 // offset
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
429 LLConstant* off = DtoConstSize_t(it->offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
430
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
431 // create Interface struct
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
432 LLConstant* inits[3] = { ci, vtb, off };
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
433 LLConstant* entry = llvm::ConstantStruct::get(inits, 3);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
434 constants.push_back(entry);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
435 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
436
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
437 // create Interface[N]
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
438 const llvm::ArrayType* array_type = llvm::ArrayType::get(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
439 constants[0]->getType(),
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
440 n);
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
441
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
442 LLConstant* arr = llvm::ConstantArray::get(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
443 array_type,
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
444 &constants[0],
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
445 constants.size());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
446
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
447 // apply the initializer
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
448 classInterfacesArray->setInitializer(arr);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
449
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
450 LLConstant* idxs[2] = {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
451 DtoConstSize_t(0),
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
452 // only the interface explicitly implemented by this class
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
453 // (not super classes) should show in ClassInfo
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
454 DtoConstSize_t(n - cd->vtblInterfaces->dim)
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
455 };
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
456
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
457 // return as a slice
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
458 return DtoConstSlice(
1230
e67c85d6e680 Completed interface implementation. Hopefully that's it for now..
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1229
diff changeset
459 DtoConstSize_t(n),
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
460 llvm::ConstantExpr::getGetElementPtr(classInterfacesArray, idxs, 2));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
461 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
462
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
diff changeset
463 //////////////////////////////////////////////////////////////////////////////