blob: f43130a12b2d10fa4f5c2ae2719c842dc52f7552 [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
14// We might split this into multiple files if it gets too unwieldy
15
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000020#include "clang/AST/DeclObjC.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000022using namespace clang;
23using namespace CodeGen;
24
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025
26// FIXME: Name mangling should be moved to a separate class.
27
28static void mangleDeclContextInternal(const DeclContext *D, std::string &S)
29{
Anders Carlsson86e96442008-08-23 19:42:54 +000030 // FIXME: Should ObjcMethodDecl have the TranslationUnitDecl as its parent?
Eli Friedmane8e32052008-12-16 20:06:41 +000031 assert((!D->getParent() || isa<TranslationUnitDecl>(D->getParent())) &&
Anders Carlssone1b29ef2008-08-22 16:00:37 +000032 "Only one level of decl context mangling is currently supported!");
33
Anders Carlsson86e96442008-08-23 19:42:54 +000034 if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +000035 S += llvm::utostr(FD->getIdentifier()->getLength());
Anders Carlssone1b29ef2008-08-22 16:00:37 +000036 S += FD->getIdentifier()->getName();
37
38 if (FD->param_size() == 0)
39 S += 'v';
40 else
41 assert(0 && "mangling of types not supported yet!");
Anders Carlsson86e96442008-08-23 19:42:54 +000042 } else if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
43
44 // FIXME: This should really use GetNameForMethod from CGObjCMac.
45 std::string Name;
Douglas Gregorf8d49f62009-01-09 17:18:27 +000046 Name += MD->isInstanceMethod() ? '-' : '+';
Anders Carlsson86e96442008-08-23 19:42:54 +000047 Name += '[';
Chris Lattner077bf5e2008-11-24 03:33:13 +000048 Name += MD->getClassInterface()->getNameAsString();
Anders Carlsson86e96442008-08-23 19:42:54 +000049 Name += ' ';
Chris Lattner077bf5e2008-11-24 03:33:13 +000050 Name += MD->getSelector().getAsString();
Anders Carlsson86e96442008-08-23 19:42:54 +000051 Name += ']';
Chris Lattner077bf5e2008-11-24 03:33:13 +000052 S += llvm::utostr(Name.length());
Anders Carlsson86e96442008-08-23 19:42:54 +000053 S += Name;
Anders Carlssone1b29ef2008-08-22 16:00:37 +000054 } else
55 assert(0 && "Unsupported decl type!");
56}
57
58static void mangleVarDeclInternal(const VarDecl &D, std::string &S)
59{
60 S += 'Z';
61 mangleDeclContextInternal(D.getDeclContext(), S);
62 S += 'E';
63
Chris Lattner077bf5e2008-11-24 03:33:13 +000064 S += llvm::utostr(D.getIdentifier()->getLength());
Anders Carlssone1b29ef2008-08-22 16:00:37 +000065 S += D.getIdentifier()->getName();
66}
67
68static std::string mangleVarDecl(const VarDecl& D)
69{
70 std::string S = "_Z";
71
72 mangleVarDeclInternal(D, S);
73
74 return S;
75}
76
77static std::string mangleGuardVariable(const VarDecl& D)
78{
79 std::string S = "_ZGV";
80
81 mangleVarDeclInternal(D, S);
82
83 return S;
84}
85
Daniel Dunbar0096acf2009-02-25 19:24:29 +000086void
87CodeGenFunction::GenerateStaticCXXBlockVarDeclInit(const VarDecl &D,
88 llvm::GlobalVariable *GV) {
89 // FIXME: This should use __cxa_guard_{acquire,release}?
90
Anders Carlssone1b29ef2008-08-22 16:00:37 +000091 assert(!getContext().getLangOptions().ThreadsafeStatics &&
92 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +000093
Anders Carlssone1b29ef2008-08-22 16:00:37 +000094 // Create the guard variable.
95 llvm::GlobalValue *GuardV =
96 new llvm::GlobalVariable(llvm::Type::Int64Ty, false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +000097 GV->getLinkage(),
Anders Carlssone1b29ef2008-08-22 16:00:37 +000098 llvm::Constant::getNullValue(llvm::Type::Int64Ty),
99 mangleGuardVariable(D),
100 &CGM.getModule());
101
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000102 // Load the first byte of the guard variable.
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000103 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000104 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
105 "tmp");
106
107 // Compare it against 0.
108 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
109 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
110
Daniel Dunbar55e87422008-11-11 02:29:29 +0000111 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000112 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000113
114 // If the guard variable is 0, jump to the initializer code.
115 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
116
117 EmitBlock(InitBlock);
118
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000119 // Patch the name. FIXME: We shouldn't need to do this.
120 GV->setName(mangleVarDecl(D));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000121
122 const Expr *Init = D.getInit();
123 if (!hasAggregateLLVMType(Init->getType())) {
124 llvm::Value *V = EmitScalarExpr(Init);
125 Builder.CreateStore(V, GV, D.getType().isVolatileQualified());
126 } else if (Init->getType()->isAnyComplexType()) {
127 EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified());
128 } else {
129 EmitAggExpr(Init, GV, D.getType().isVolatileQualified());
130 }
131
132 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1),
133 Builder.CreateBitCast(GuardV, PtrTy));
134
135 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000136}
137