comparison gen/rttibuilder.cpp @ 1374:e630ff79e10d

Cleaned up TypeInfo generation, still need to do TypeInfo_Struct/Tuple. Eventually do ClassInfo and ModuleInfo as well using same interface.
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Sun, 17 May 2009 03:10:55 +0200
parents
children 63f4afd01036
comparison
equal deleted inserted replaced
1373:551b01341728 1374:e630ff79e10d
1 #include "gen/llvm.h"
2
3 #include "aggregate.h"
4 #include "mtype.h"
5
6 #include "gen/arrays.h"
7 #include "gen/irstate.h"
8 #include "gen/linkage.h"
9 #include "gen/llvmhelpers.h"
10 #include "gen/rttibuilder.h"
11 #include "gen/tollvm.h"
12
13 #include "ir/irstruct.h"
14
15 TypeInfoBuilder::TypeInfoBuilder(ClassDeclaration* base_class)
16 {
17 // make sure the base typeinfo class has been processed
18 base_class->codegen(Type::sir);
19
20 base = base_class;
21 basetype = (TypeClass*)base->type;
22
23 baseir = base->ir.irStruct;
24 assert(baseir && "no IrStruct for TypeInfo base class");
25
26 // just start with adding the vtbl
27 inits.push_back(baseir->getVtblSymbol());
28 // and monitor
29 push_null_vp();
30 }
31
32 void TypeInfoBuilder::push(llvm::Constant* C)
33 {
34 inits.push_back(C);
35 }
36
37 void TypeInfoBuilder::push_null_vp()
38 {
39 inits.push_back(getNullValue(getVoidPtrType()));
40 }
41
42 void TypeInfoBuilder::push_typeinfo(Type* t)
43 {
44 inits.push_back(DtoTypeInfoOf(t, true));
45 }
46
47 void TypeInfoBuilder::push_classinfo(ClassDeclaration* cd)
48 {
49 inits.push_back(cd->ir.irStruct->getClassInfoSymbol());
50 }
51
52 void TypeInfoBuilder::push_string(const char* str)
53 {
54 inits.push_back(DtoConstString(str));
55 }
56
57 void TypeInfoBuilder::push_null_void_array()
58 {
59 const llvm::Type* T = DtoType(Type::tvoid->arrayOf());
60 inits.push_back(getNullValue(T));
61 }
62
63 void TypeInfoBuilder::push_void_array(size_t dim, llvm::Constant* ptr)
64 {
65 inits.push_back(DtoConstSlice(
66 DtoConstSize_t(dim),
67 DtoBitCast(ptr, getVoidPtrType())));
68 }
69
70 void TypeInfoBuilder::push_void_array(llvm::Constant* CI, Type* valtype, Dsymbol* sym)
71 {
72 std::string initname(sym->mangle());
73 initname.append("13__defaultInitZ");
74
75 LLGlobalVariable* G = new llvm::GlobalVariable(
76 CI->getType(), true, TYPEINFO_LINKAGE_TYPE, CI, initname, gIR->module);
77 G->setAlignment(valtype->alignsize());
78
79 size_t dim = getTypePaddedSize(CI->getType());
80 push_void_array(dim, G);
81 }
82
83 void TypeInfoBuilder::finalize(IrGlobal* tid)
84 {
85 // create the inititalizer
86 LLConstant* tiInit = llvm::ConstantStruct::get(&inits[0], inits.size(), false);
87
88 // refine global type
89 llvm::cast<llvm::OpaqueType>(tid->type.get())->refineAbstractTypeTo(tiInit->getType());
90
91 // set the initializer
92 isaGlobalVar(tid->value)->setInitializer(tiInit);
93 }