comparison gen/llvmhelpers.cpp @ 433:b5f55f471e0b

Move DeclarationExp code into a helper function so it can call itself for template mixin members.
author Christian Kamm <kamm incasoftware de>
date Wed, 30 Jul 2008 09:21:06 +0200
parents c8d98ccad0cc
children 74101be2a553
comparison
equal deleted inserted replaced
432:ecf70fe065b9 433:b5f55f471e0b
3 3
4 #include "mars.h" 4 #include "mars.h"
5 #include "init.h" 5 #include "init.h"
6 #include "id.h" 6 #include "id.h"
7 #include "expression.h" 7 #include "expression.h"
8 #include "template.h"
8 9
9 #include "gen/tollvm.h" 10 #include "gen/tollvm.h"
10 #include "gen/llvmhelpers.h" 11 #include "gen/llvmhelpers.h"
11 #include "gen/irstate.h" 12 #include "gen/irstate.h"
12 #include "gen/runtime.h" 13 #include "gen/runtime.h"
1205 DtoDefineDsymbol(dsym); 1206 DtoDefineDsymbol(dsym);
1206 } 1207 }
1207 1208
1208 /****************************************************************************************/ 1209 /****************************************************************************************/
1209 /*//////////////////////////////////////////////////////////////////////////////////////// 1210 /*////////////////////////////////////////////////////////////////////////////////////////
1211 // DECLARATION EXP HELPER
1212 ////////////////////////////////////////////////////////////////////////////////////////*/
1213 DValue* DtoDeclarationExp(Dsymbol* declaration)
1214 {
1215 Logger::print("DtoDeclarationExp: %s\n", declaration->toChars());
1216 LOG_SCOPE;
1217
1218 // variable declaration
1219 if (VarDeclaration* vd = declaration->isVarDeclaration())
1220 {
1221 Logger::println("VarDeclaration");
1222
1223 // static
1224 if (vd->isDataseg())
1225 {
1226 vd->toObjFile(0); // TODO: multiobj
1227 }
1228 else
1229 {
1230 if (global.params.llvmAnnotate)
1231 DtoAnnotation(declaration->toChars());
1232
1233 Logger::println("vdtype = %s", vd->type->toChars());
1234
1235 // referenced by nested delegate?
1236 if (vd->nestedref) {
1237 Logger::println("has nestedref set");
1238 assert(vd->ir.irLocal);
1239 vd->ir.irLocal->value = gIR->func()->decl->ir.irFunc->nestedVar;
1240 assert(vd->ir.irLocal->value);
1241 assert(vd->ir.irLocal->nestedIndex >= 0);
1242 }
1243 // normal stack variable, allocate storage on the stack if it has not already been done
1244 else if(!vd->ir.irLocal) {
1245 const LLType* lltype = DtoType(vd->type);
1246
1247 llvm::Value* allocainst;
1248 if(gTargetData->getTypeSizeInBits(lltype) == 0)
1249 allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype));
1250 else
1251 allocainst = new llvm::AllocaInst(lltype, vd->toChars(), gIR->topallocapoint());
1252
1253 //allocainst->setAlignment(vd->type->alignsize()); // TODO
1254 vd->ir.irLocal = new IrLocal(vd);
1255 vd->ir.irLocal->value = allocainst;
1256
1257 if (global.params.symdebug)
1258 {
1259 DtoDwarfLocalVariable(allocainst, vd);
1260 }
1261 }
1262
1263 Logger::cout() << "llvm value for decl: " << *vd->ir.irLocal->value << '\n';
1264 DValue* ie = DtoInitializer(vd->init);
1265 }
1266
1267 return new DVarValue(vd, vd->ir.getIrValue(), true);
1268 }
1269 // struct declaration
1270 else if (StructDeclaration* s = declaration->isStructDeclaration())
1271 {
1272 Logger::println("StructDeclaration");
1273 DtoForceConstInitDsymbol(s);
1274 }
1275 // function declaration
1276 else if (FuncDeclaration* f = declaration->isFuncDeclaration())
1277 {
1278 Logger::println("FuncDeclaration");
1279 DtoForceDeclareDsymbol(f);
1280 }
1281 // alias declaration
1282 else if (AliasDeclaration* a = declaration->isAliasDeclaration())
1283 {
1284 Logger::println("AliasDeclaration - no work");
1285 // do nothing
1286 }
1287 // enum
1288 else if (EnumDeclaration* e = declaration->isEnumDeclaration())
1289 {
1290 Logger::println("EnumDeclaration - no work");
1291 // do nothing
1292 }
1293 // class
1294 else if (ClassDeclaration* e = declaration->isClassDeclaration())
1295 {
1296 Logger::println("ClassDeclaration");
1297 DtoForceConstInitDsymbol(e);
1298 }
1299 // typedef
1300 else if (TypedefDeclaration* tdef = declaration->isTypedefDeclaration())
1301 {
1302 Logger::println("TypedefDeclaration");
1303 DtoTypeInfoOf(tdef->type, false);
1304 }
1305 // attribute declaration
1306 else if (AttribDeclaration* a = declaration->isAttribDeclaration())
1307 {
1308 Logger::println("AttribDeclaration");
1309 for (int i=0; i < a->decl->dim; ++i)
1310 {
1311 DtoForceDeclareDsymbol((Dsymbol*)a->decl->data[i]);
1312 }
1313 }
1314 // mixin declaration
1315 else if (TemplateMixin* m = declaration->isTemplateMixin())
1316 {
1317 Logger::println("TemplateMixin");
1318 for (int i=0; i < m->members->dim; ++i)
1319 {
1320 Dsymbol* mdsym = (Dsymbol*)m->members->data[i];
1321 DtoDeclarationExp(mdsym);
1322 }
1323 }
1324 // unsupported declaration
1325 else
1326 {
1327 error(declaration->loc, "Unimplemented Declaration type for DeclarationExp. kind: %s", declaration->kind());
1328 assert(0);
1329 }
1330 return NULL;
1331 }
1332
1333
1334 /****************************************************************************************/
1335 /*////////////////////////////////////////////////////////////////////////////////////////
1210 // INITIALIZER HELPERS 1336 // INITIALIZER HELPERS
1211 ////////////////////////////////////////////////////////////////////////////////////////*/ 1337 ////////////////////////////////////////////////////////////////////////////////////////*/
1212 1338
1213 LLConstant* DtoConstInitializer(Type* type, Initializer* init) 1339 LLConstant* DtoConstInitializer(Type* type, Initializer* init)
1214 { 1340 {