blob: 489c67bc6b74455be9a6f9a058197b02afaac2cd [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
86llvm::GlobalValue *
87CodeGenFunction::GenerateStaticCXXBlockVarDecl(const VarDecl &D)
88{
89 assert(!getContext().getLangOptions().ThreadsafeStatics &&
90 "thread safe statics are currently not supported!");
91 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
92
93 // FIXME: If the function is inline, the linkage should be weak.
94 llvm::GlobalValue::LinkageTypes linkage = llvm::GlobalValue::InternalLinkage;
95
96 // Create the guard variable.
97 llvm::GlobalValue *GuardV =
98 new llvm::GlobalVariable(llvm::Type::Int64Ty, false,
99 linkage,
100 llvm::Constant::getNullValue(llvm::Type::Int64Ty),
101 mangleGuardVariable(D),
102 &CGM.getModule());
103
104 // FIXME: Address space.
105 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
106
107 // Load the first byte of the guard variable.
108 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
109 "tmp");
110
111 // Compare it against 0.
112 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
113 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
114
Daniel Dunbar55e87422008-11-11 02:29:29 +0000115 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000116 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000117
118 // If the guard variable is 0, jump to the initializer code.
119 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
120
121 EmitBlock(InitBlock);
122
123 llvm::GlobalValue *GV =
124 new llvm::GlobalVariable(LTy, false,
125 llvm::GlobalValue::InternalLinkage,
126 llvm::Constant::getNullValue(LTy),
127 mangleVarDecl(D),
128 &CGM.getModule(), 0,
129 D.getType().getAddressSpace());
130
131 const Expr *Init = D.getInit();
132 if (!hasAggregateLLVMType(Init->getType())) {
133 llvm::Value *V = EmitScalarExpr(Init);
134 Builder.CreateStore(V, GV, D.getType().isVolatileQualified());
135 } else if (Init->getType()->isAnyComplexType()) {
136 EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified());
137 } else {
138 EmitAggExpr(Init, GV, D.getType().isVolatileQualified());
139 }
140
141 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1),
142 Builder.CreateBitCast(GuardV, PtrTy));
143
144 EmitBlock(EndBlock);
145 return GV;
146}
147