blob: 091158daba759d61414ee8c1a45936893615aa39 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-module state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000014#include "CGDebugInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "CodeGenModule.h"
16#include "CodeGenFunction.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000017#include "CGCall.h"
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000018#include "CGObjCRuntime.h"
Douglas Gregor3556bc72009-02-13 00:10:09 +000019#include "Mangle.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000021#include "clang/AST/DeclObjC.h"
Chris Lattner825f6ea2008-11-04 16:51:42 +000022#include "clang/AST/DeclCXX.h"
Chris Lattnercf9c9d02007-12-02 07:19:18 +000023#include "clang/Basic/Diagnostic.h"
Nate Begeman8a704172008-04-19 04:17:09 +000024#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000025#include "clang/Basic/TargetInfo.h"
Nate Begemandc6262e2008-03-09 03:09:36 +000026#include "llvm/CallingConv.h"
Chris Lattnerab862cc2007-08-31 04:31:45 +000027#include "llvm/Module.h"
Chris Lattner4b009652007-07-25 00:24:17 +000028#include "llvm/Intrinsics.h"
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000029#include "llvm/Target/TargetData.h"
Chris Lattner4b009652007-07-25 00:24:17 +000030using namespace clang;
31using namespace CodeGen;
32
33
Chris Lattnerdb6be562007-11-28 05:34:05 +000034CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO,
Chris Lattner22595b82007-12-02 01:40:18 +000035 llvm::Module &M, const llvm::TargetData &TD,
Daniel Dunbar1be1df32008-08-11 21:35:06 +000036 Diagnostic &diags, bool GenerateDebugInfo)
Chris Lattner22595b82007-12-02 01:40:18 +000037 : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags),
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000038 Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0),
Mike Stump95e54802009-02-13 15:16:56 +000039 CFConstantStringClassRef(0), NSConcreteGlobalBlock(0),
Mike Stump0dffa462009-02-13 15:25:34 +000040 BlockDescriptorType(0), GenericBlockLiteralType(0) {
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000041
42 if (Features.ObjC1) {
Daniel Dunbar1be1df32008-08-11 21:35:06 +000043 if (Features.NeXTRuntime) {
Fariborz Jahaniand0374812009-01-22 23:02:58 +000044 Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this)
Fariborz Jahanian48543f52009-01-21 22:04:16 +000045 : CreateMacObjCRuntime(*this);
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000046 } else {
47 Runtime = CreateGNUObjCRuntime(*this);
48 }
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000049 }
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000050
51 // If debug info generation is enabled, create the CGDebugInfo object.
Mike Stumpef2c82f2009-02-13 18:36:05 +000052 DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0;
53
54 Block.GlobalUniqueCount = 0;
Chris Lattnercbfb5512008-03-01 08:45:05 +000055}
56
57CodeGenModule::~CodeGenModule() {
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000058 delete Runtime;
59 delete DebugInfo;
60}
61
62void CodeGenModule::Release() {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000063 EmitStatics();
Daniel Dunbar566a6502008-09-08 23:44:31 +000064 EmitAliases();
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000065 if (Runtime)
66 if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
67 AddGlobalCtor(ObjCInitFunction);
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +000068 EmitCtorList(GlobalCtors, "llvm.global_ctors");
69 EmitCtorList(GlobalDtors, "llvm.global_dtors");
Nate Begeman52da5c72008-04-18 23:43:57 +000070 EmitAnnotations();
Daniel Dunbar18c2ec62008-10-01 00:49:24 +000071 BindRuntimeFunctions();
Chris Lattnercbfb5512008-03-01 08:45:05 +000072}
Chris Lattner4b009652007-07-25 00:24:17 +000073
Daniel Dunbar18c2ec62008-10-01 00:49:24 +000074void CodeGenModule::BindRuntimeFunctions() {
75 // Deal with protecting runtime function names.
76 for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) {
77 llvm::Function *Fn = RuntimeFunctions[i].first;
78 const std::string &Name = RuntimeFunctions[i].second;
79
Daniel Dunbarfc1543e2008-11-19 06:15:35 +000080 // Discard unused runtime functions.
81 if (Fn->use_empty()) {
82 Fn->eraseFromParent();
83 continue;
84 }
85
Daniel Dunbar18c2ec62008-10-01 00:49:24 +000086 // See if there is a conflict against a function.
87 llvm::Function *Conflict = TheModule.getFunction(Name);
88 if (Conflict) {
89 // Decide which version to take. If the conflict is a definition
90 // we are forced to take that, otherwise assume the runtime
91 // knows best.
92 if (!Conflict->isDeclaration()) {
93 llvm::Value *Casted =
94 llvm::ConstantExpr::getBitCast(Conflict, Fn->getType());
95 Fn->replaceAllUsesWith(Casted);
96 Fn->eraseFromParent();
97 } else {
98 Fn->takeName(Conflict);
99 llvm::Value *Casted =
100 llvm::ConstantExpr::getBitCast(Fn, Conflict->getType());
101 Conflict->replaceAllUsesWith(Casted);
102 Conflict->eraseFromParent();
103 }
104 } else {
105 // FIXME: There still may be conflicts with aliases and
106 // variables.
107 Fn->setName(Name);
108 }
109 }
110}
111
Daniel Dunbar9503b782008-08-16 00:56:44 +0000112/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000113/// specified stmt yet.
Daniel Dunbar49bddf72008-09-04 03:43:08 +0000114void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
115 bool OmitOnError) {
116 if (OmitOnError && getDiags().hasErrorOccurred())
117 return;
Daniel Dunbar9503b782008-08-16 00:56:44 +0000118 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
Daniel Dunbarc5ba4912009-02-06 19:18:03 +0000119 "cannot compile this %0 yet");
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000120 std::string Msg = Type;
Chris Lattner6948ae62008-11-18 07:04:44 +0000121 getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
122 << Msg << S->getSourceRange();
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000123}
Chris Lattner0e4755d2007-12-02 06:27:33 +0000124
Daniel Dunbar9503b782008-08-16 00:56:44 +0000125/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattner806a5f52008-01-12 07:05:38 +0000126/// specified decl yet.
Daniel Dunbar49bddf72008-09-04 03:43:08 +0000127void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
128 bool OmitOnError) {
129 if (OmitOnError && getDiags().hasErrorOccurred())
130 return;
Daniel Dunbar9503b782008-08-16 00:56:44 +0000131 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
Daniel Dunbarc5ba4912009-02-06 19:18:03 +0000132 "cannot compile this %0 yet");
Chris Lattner806a5f52008-01-12 07:05:38 +0000133 std::string Msg = Type;
Chris Lattner6948ae62008-11-18 07:04:44 +0000134 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
Chris Lattner806a5f52008-01-12 07:05:38 +0000135}
136
Daniel Dunbar27250d32008-08-15 23:26:23 +0000137/// setGlobalVisibility - Set the visibility for the given LLVM
138/// GlobalValue according to the given clang AST visibility value.
139static void setGlobalVisibility(llvm::GlobalValue *GV,
140 VisibilityAttr::VisibilityTypes Vis) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000141 switch (Vis) {
142 default: assert(0 && "Unknown visibility!");
143 case VisibilityAttr::DefaultVisibility:
144 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
145 break;
146 case VisibilityAttr::HiddenVisibility:
147 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
148 break;
149 case VisibilityAttr::ProtectedVisibility:
150 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
151 break;
152 }
153}
154
Douglas Gregor3556bc72009-02-13 00:10:09 +0000155/// \brief Retrieves the mangled name for the given declaration.
156///
157/// If the given declaration requires a mangled name, returns an
158/// IdentifierInfo* containing the mangled name. Otherwise, returns
159/// the name of the declaration as an identifier.
160///
161/// FIXME: Returning an IdentifierInfo* here is a total hack. We
162/// really need some kind of string abstraction that either stores a
163/// mangled name or stores an IdentifierInfo*. This will require
164/// changes to the GlobalDeclMap, too.
165///
166/// FIXME: Performance here is going to be terribly until we start
167/// caching mangled names. However, we should fix the problem above
168/// first.
169IdentifierInfo *CodeGenModule::getMangledName(const NamedDecl *ND) const {
170 std::string Name;
171 llvm::raw_string_ostream Out(Name);
172 if (!mangleName(ND, Context, Out))
173 return ND->getIdentifier();
174
175 return &Context.Idents.get(Out.str());
176}
177
Chris Lattner753d2592008-03-14 17:18:18 +0000178/// AddGlobalCtor - Add a function to the list that will be called before
179/// main() runs.
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000180void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000181 // FIXME: Type coercion of void()* types.
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000182 GlobalCtors.push_back(std::make_pair(Ctor, Priority));
Chris Lattner753d2592008-03-14 17:18:18 +0000183}
184
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000185/// AddGlobalDtor - Add a function to the list that will be called
186/// when the module is unloaded.
187void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000188 // FIXME: Type coercion of void()* types.
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000189 GlobalDtors.push_back(std::make_pair(Dtor, Priority));
190}
191
192void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
193 // Ctor function type is void()*.
194 llvm::FunctionType* CtorFTy =
195 llvm::FunctionType::get(llvm::Type::VoidTy,
196 std::vector<const llvm::Type*>(),
197 false);
198 llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
199
200 // Get the type of a ctor entry, { i32, void ()* }.
Chris Lattnera18c12e2008-03-19 05:24:56 +0000201 llvm::StructType* CtorStructTy =
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000202 llvm::StructType::get(llvm::Type::Int32Ty,
203 llvm::PointerType::getUnqual(CtorFTy), NULL);
Chris Lattner753d2592008-03-14 17:18:18 +0000204
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000205 // Construct the constructor and destructor arrays.
206 std::vector<llvm::Constant*> Ctors;
207 for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
208 std::vector<llvm::Constant*> S;
209 S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false));
210 S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
211 Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
Chris Lattner753d2592008-03-14 17:18:18 +0000212 }
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000213
214 if (!Ctors.empty()) {
215 llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
216 new llvm::GlobalVariable(AT, false,
217 llvm::GlobalValue::AppendingLinkage,
218 llvm::ConstantArray::get(AT, Ctors),
219 GlobalName,
220 &TheModule);
221 }
Chris Lattner753d2592008-03-14 17:18:18 +0000222}
223
Nate Begeman52da5c72008-04-18 23:43:57 +0000224void CodeGenModule::EmitAnnotations() {
225 if (Annotations.empty())
226 return;
227
228 // Create a new global variable for the ConstantStruct in the Module.
229 llvm::Constant *Array =
230 llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
231 Annotations.size()),
232 Annotations);
233 llvm::GlobalValue *gv =
234 new llvm::GlobalVariable(Array->getType(), false,
235 llvm::GlobalValue::AppendingLinkage, Array,
236 "llvm.global.annotations", &TheModule);
237 gv->setSection("llvm.metadata");
238}
239
Daniel Dunbara8f02052008-09-08 21:33:45 +0000240static void SetGlobalValueAttributes(const Decl *D,
241 bool IsInternal,
242 bool IsInline,
Daniel Dunbar566a6502008-09-08 23:44:31 +0000243 llvm::GlobalValue *GV,
244 bool ForDefinition) {
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000245 // FIXME: Set up linkage and many other things. Note, this is a simple
Nuno Lopes78534382008-06-08 15:45:52 +0000246 // approximation of what we really want.
Daniel Dunbar566a6502008-09-08 23:44:31 +0000247 if (!ForDefinition) {
248 // Only a few attributes are set on declarations.
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000249 if (D->getAttr<DLLImportAttr>()) {
250 // The dllimport attribute is overridden by a subsequent declaration as
251 // dllexport.
Sebastian Redle299eb42009-01-05 20:53:53 +0000252 if (!D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000253 // dllimport attribute can be applied only to function decls, not to
254 // definitions.
255 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
256 if (!FD->getBody())
257 GV->setLinkage(llvm::Function::DLLImportLinkage);
258 } else
259 GV->setLinkage(llvm::Function::DLLImportLinkage);
Sebastian Redle299eb42009-01-05 20:53:53 +0000260 }
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000261 }
Daniel Dunbar566a6502008-09-08 23:44:31 +0000262 } else {
263 if (IsInternal) {
264 GV->setLinkage(llvm::Function::InternalLinkage);
265 } else {
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000266 if (D->getAttr<DLLExportAttr>()) {
267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
268 // The dllexport attribute is ignored for undefined symbols.
269 if (FD->getBody())
270 GV->setLinkage(llvm::Function::DLLExportLinkage);
271 } else
272 GV->setLinkage(llvm::Function::DLLExportLinkage);
273 } else if (D->getAttr<WeakAttr>() || IsInline)
Daniel Dunbar566a6502008-09-08 23:44:31 +0000274 GV->setLinkage(llvm::Function::WeakLinkage);
275 }
Daniel Dunbara8f02052008-09-08 21:33:45 +0000276 }
Nuno Lopes78534382008-06-08 15:45:52 +0000277
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000278 // FIXME: Figure out the relative priority of the attribute,
279 // -fvisibility, and private_extern.
Daniel Dunbara8f02052008-09-08 21:33:45 +0000280 if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
Daniel Dunbar27250d32008-08-15 23:26:23 +0000281 setGlobalVisibility(GV, attr->getVisibility());
Nuno Lopes78534382008-06-08 15:45:52 +0000282 // FIXME: else handle -fvisibility
Daniel Dunbarced89142008-08-06 00:03:29 +0000283
Daniel Dunbara8f02052008-09-08 21:33:45 +0000284 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Daniel Dunbarced89142008-08-06 00:03:29 +0000285 // Prefaced with special LLVM marker to indicate that the name
286 // should not be munged.
287 GV->setName("\01" + ALA->getLabel());
288 }
Daniel Dunbar97509722009-02-12 17:28:23 +0000289
290 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
291 GV->setSection(SA->getName());
Nuno Lopes78534382008-06-08 15:45:52 +0000292}
293
Devang Patela85a9ef2008-09-25 21:02:23 +0000294void CodeGenModule::SetFunctionAttributes(const Decl *D,
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000295 const CGFunctionInfo &Info,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000296 llvm::Function *F) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000297 AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000298 ConstructAttributeList(Info, D, AttributeList);
Eli Friedmanfa94dff2008-06-04 19:41:28 +0000299
Devang Patela85a9ef2008-09-25 21:02:23 +0000300 F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
301 AttributeList.size()));
Eli Friedman9be42212008-06-01 15:54:49 +0000302
303 // Set the appropriate calling convention for the Function.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000304 if (D->getAttr<FastCallAttr>())
Anton Korobeynikov0ef7c382008-11-11 20:21:14 +0000305 F->setCallingConv(llvm::CallingConv::X86_FastCall);
306
307 if (D->getAttr<StdCallAttr>())
308 F->setCallingConv(llvm::CallingConv::X86_StdCall);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000309}
310
311/// SetFunctionAttributesForDefinition - Set function attributes
312/// specific to a function definition.
Daniel Dunbar566a6502008-09-08 23:44:31 +0000313void CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D,
314 llvm::Function *F) {
315 if (isa<ObjCMethodDecl>(D)) {
316 SetGlobalValueAttributes(D, true, false, F, true);
317 } else {
318 const FunctionDecl *FD = cast<FunctionDecl>(D);
319 SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static,
320 FD->isInline(), F, true);
321 }
322
Daniel Dunbarf2787002008-09-04 23:41:35 +0000323 if (!Features.Exceptions)
Daniel Dunbar9575eb32008-09-27 07:16:42 +0000324 F->addFnAttr(llvm::Attribute::NoUnwind);
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000325
326 if (D->getAttr<AlwaysInlineAttr>())
327 F->addFnAttr(llvm::Attribute::AlwaysInline);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000328}
329
330void CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD,
331 llvm::Function *F) {
Daniel Dunbar34bda882009-02-02 23:23:47 +0000332 SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000333
Daniel Dunbar566a6502008-09-08 23:44:31 +0000334 SetFunctionAttributesForDefinition(MD, F);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000335}
336
337void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
338 llvm::Function *F) {
Daniel Dunbar34bda882009-02-02 23:23:47 +0000339 SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000340
Daniel Dunbar566a6502008-09-08 23:44:31 +0000341 SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static,
342 FD->isInline(), F, false);
343}
344
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000345
Daniel Dunbar566a6502008-09-08 23:44:31 +0000346void CodeGenModule::EmitAliases() {
347 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
348 const FunctionDecl *D = Aliases[i];
349 const AliasAttr *AA = D->getAttr<AliasAttr>();
350
351 // This is something of a hack, if the FunctionDecl got overridden
352 // then its attributes will be moved to the new declaration. In
353 // this case the current decl has no alias attribute, but we will
354 // eventually see it.
355 if (!AA)
356 continue;
357
358 const std::string& aliaseeName = AA->getAliasee();
359 llvm::Function *aliasee = getModule().getFunction(aliaseeName);
360 if (!aliasee) {
361 // FIXME: This isn't unsupported, this is just an error, which
362 // sema should catch, but...
363 ErrorUnsupported(D, "alias referencing a missing function");
364 continue;
365 }
366
367 llvm::GlobalValue *GA =
368 new llvm::GlobalAlias(aliasee->getType(),
369 llvm::Function::ExternalLinkage,
Douglas Gregor3556bc72009-02-13 00:10:09 +0000370 getMangledName(D)->getName(), aliasee,
371 &getModule());
Daniel Dunbar566a6502008-09-08 23:44:31 +0000372
Douglas Gregor3556bc72009-02-13 00:10:09 +0000373 llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
Daniel Dunbar566a6502008-09-08 23:44:31 +0000374 if (Entry) {
375 // If we created a dummy function for this then replace it.
376 GA->takeName(Entry);
377
378 llvm::Value *Casted =
379 llvm::ConstantExpr::getBitCast(GA, Entry->getType());
380 Entry->replaceAllUsesWith(Casted);
381 Entry->eraseFromParent();
382
383 Entry = GA;
384 }
385
386 // Alias should never be internal or inline.
387 SetGlobalValueAttributes(D, false, false, GA, true);
388 }
Eli Friedman9be42212008-06-01 15:54:49 +0000389}
390
Nate Begemanad320b62008-04-20 06:29:50 +0000391void CodeGenModule::EmitStatics() {
392 // Emit code for each used static decl encountered. Since a previously unused
393 // static decl may become used during the generation of code for a static
394 // function, iterate until no changes are made.
395 bool Changed;
396 do {
397 Changed = false;
Anders Carlsson2e427d52009-01-04 02:08:04 +0000398
399 for (std::list<const ValueDecl*>::iterator i = StaticDecls.begin(),
400 e = StaticDecls.end(); i != e; ) {
401 const ValueDecl *D = *i;
402
Eli Friedmana4d4e2f2008-05-27 04:58:01 +0000403 // Check if we have used a decl with the same name
404 // FIXME: The AST should have some sort of aggregate decls or
405 // global symbol map.
Daniel Dunbar566a6502008-09-08 23:44:31 +0000406 // FIXME: This is missing some important cases. For example, we
407 // need to check for uses in an alias and in a constructor.
Douglas Gregor3556bc72009-02-13 00:10:09 +0000408 if (!GlobalDeclMap.count(getMangledName(D))) {
Anders Carlsson2e427d52009-01-04 02:08:04 +0000409 i++;
Daniel Dunbarced89142008-08-06 00:03:29 +0000410 continue;
Anders Carlsson2e427d52009-01-04 02:08:04 +0000411 }
412
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000413 // Emit the definition.
414 EmitGlobalDefinition(D);
415
Nate Begemanad320b62008-04-20 06:29:50 +0000416 // Erase the used decl from the list.
Anders Carlsson2e427d52009-01-04 02:08:04 +0000417 i = StaticDecls.erase(i);
418
Nate Begemanad320b62008-04-20 06:29:50 +0000419 // Remember that we made a change.
420 Changed = true;
421 }
422 } while (Changed);
Chris Lattner4b009652007-07-25 00:24:17 +0000423}
424
Nate Begeman8a704172008-04-19 04:17:09 +0000425/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
426/// annotation information for a given GlobalValue. The annotation struct is
427/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000428/// GlobalValue being annotated. The second field is the constant string
Nate Begeman8a704172008-04-19 04:17:09 +0000429/// created from the AnnotateAttr's annotation. The third field is a constant
430/// string containing the name of the translation unit. The fourth field is
431/// the line number in the file of the annotated value declaration.
432///
433/// FIXME: this does not unique the annotation string constants, as llvm-gcc
434/// appears to.
435///
436llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
437 const AnnotateAttr *AA,
438 unsigned LineNo) {
439 llvm::Module *M = &getModule();
440
441 // get [N x i8] constants for the annotation string, and the filename string
442 // which are the 2nd and 3rd elements of the global annotation structure.
443 const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
444 llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true);
445 llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(),
446 true);
447
448 // Get the two global values corresponding to the ConstantArrays we just
449 // created to hold the bytes of the strings.
450 llvm::GlobalValue *annoGV =
451 new llvm::GlobalVariable(anno->getType(), false,
452 llvm::GlobalValue::InternalLinkage, anno,
453 GV->getName() + ".str", M);
454 // translation unit name string, emitted into the llvm.metadata section.
455 llvm::GlobalValue *unitGV =
456 new llvm::GlobalVariable(unit->getType(), false,
457 llvm::GlobalValue::InternalLinkage, unit, ".str", M);
458
459 // Create the ConstantStruct that is the global annotion.
460 llvm::Constant *Fields[4] = {
461 llvm::ConstantExpr::getBitCast(GV, SBP),
462 llvm::ConstantExpr::getBitCast(annoGV, SBP),
463 llvm::ConstantExpr::getBitCast(unitGV, SBP),
464 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo)
465 };
466 return llvm::ConstantStruct::get(Fields, 4, false);
467}
468
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000469void CodeGenModule::EmitGlobal(const ValueDecl *Global) {
470 bool isDef, isStatic;
471
472 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
Daniel Dunbar566a6502008-09-08 23:44:31 +0000473 // Aliases are deferred until code for everything else has been
474 // emitted.
475 if (FD->getAttr<AliasAttr>()) {
476 assert(!FD->isThisDeclarationADefinition() &&
477 "Function alias cannot have a definition!");
478 Aliases.push_back(FD);
479 return;
480 }
481
482 isDef = FD->isThisDeclarationADefinition();
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000483 isStatic = FD->getStorageClass() == FunctionDecl::Static;
484 } else if (const VarDecl *VD = cast<VarDecl>(Global)) {
485 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
486
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000487 isDef = !((VD->getStorageClass() == VarDecl::Extern ||
488 VD->getStorageClass() == VarDecl::PrivateExtern) &&
489 VD->getInit() == 0);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000490 isStatic = VD->getStorageClass() == VarDecl::Static;
491 } else {
492 assert(0 && "Invalid argument to EmitGlobal");
Nate Begemanad320b62008-04-20 06:29:50 +0000493 return;
494 }
495
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000496 // Forward declarations are emitted lazily on first use.
497 if (!isDef)
Chris Lattner4b009652007-07-25 00:24:17 +0000498 return;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000499
500 // If the global is a static, defer code generation until later so
501 // we can easily omit unused statics.
Daniel Dunbar9bae8652009-02-04 21:19:06 +0000502 if (isStatic && !Features.EmitAllDecls) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000503 StaticDecls.push_back(Global);
504 return;
505 }
506
507 // Otherwise emit the definition.
508 EmitGlobalDefinition(Global);
Nate Begemanad320b62008-04-20 06:29:50 +0000509}
510
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000511void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) {
512 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
513 EmitGlobalFunctionDefinition(FD);
514 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
515 EmitGlobalVarDefinition(VD);
516 } else {
517 assert(0 && "Invalid argument to EmitGlobalDefinition()");
518 }
519}
520
Daniel Dunbar2188c532008-07-30 16:32:24 +0000521 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) {
Eli Friedman43a0ce82008-05-30 19:50:47 +0000522 assert(D->hasGlobalStorage() && "Not a global variable");
523
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000524 QualType ASTTy = D->getType();
525 const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
Daniel Dunbar2188c532008-07-30 16:32:24 +0000526 const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000527
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000528 // Lookup the entry, lazily creating it if necessary.
Douglas Gregor3556bc72009-02-13 00:10:09 +0000529 llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000530 if (!Entry) {
531 llvm::GlobalVariable *GV =
532 new llvm::GlobalVariable(Ty, false,
533 llvm::GlobalValue::ExternalLinkage,
Douglas Gregor3556bc72009-02-13 00:10:09 +0000534 0, getMangledName(D)->getName(), &getModule(),
535 0, ASTTy.getAddressSpace());
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000536 Entry = GV;
537
538 // Handle things which are present even on external declarations.
539
540 // FIXME: This code is overly simple and should be merged with
541 // other global handling.
542
543 GV->setConstant(D->getType().isConstant(Context));
544
545 if (D->getStorageClass() == VarDecl::PrivateExtern)
546 setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility);
547 }
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000548
Daniel Dunbar2188c532008-07-30 16:32:24 +0000549 // Make sure the result is of the correct type.
550 return llvm::ConstantExpr::getBitCast(Entry, PTy);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000551}
552
553void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000554 llvm::Constant *Init = 0;
Eli Friedman43a0ce82008-05-30 19:50:47 +0000555 QualType ASTTy = D->getType();
556 const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy);
Eli Friedman43a0ce82008-05-30 19:50:47 +0000557
Chris Lattner4b009652007-07-25 00:24:17 +0000558 if (D->getInit() == 0) {
Eli Friedman7008e9a2008-05-30 20:39:54 +0000559 // This is a tentative definition; tentative definitions are
560 // implicitly initialized with { 0 }
561 const llvm::Type* InitTy;
562 if (ASTTy->isIncompleteArrayType()) {
563 // An incomplete array is normally [ TYPE x 0 ], but we need
564 // to fix it to [ TYPE x 1 ].
565 const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy);
566 InitTy = llvm::ArrayType::get(ATy->getElementType(), 1);
567 } else {
568 InitTy = VarTy;
569 }
570 Init = llvm::Constant::getNullValue(InitTy);
Eli Friedman43a0ce82008-05-30 19:50:47 +0000571 } else {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000572 Init = EmitConstantExpr(D->getInit());
Eli Friedman43a0ce82008-05-30 19:50:47 +0000573 }
574 const llvm::Type* InitType = Init->getType();
575
Douglas Gregor3556bc72009-02-13 00:10:09 +0000576 llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000577 llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry);
578
Eli Friedman43a0ce82008-05-30 19:50:47 +0000579 if (!GV) {
580 GV = new llvm::GlobalVariable(InitType, false,
581 llvm::GlobalValue::ExternalLinkage,
Douglas Gregor3556bc72009-02-13 00:10:09 +0000582 0, getMangledName(D)->getName(),
583 &getModule(), 0, ASTTy.getAddressSpace());
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000584 } else if (GV->getType() !=
585 llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) {
Eli Friedman43a0ce82008-05-30 19:50:47 +0000586 // We have a definition after a prototype with the wrong type.
587 // We must make a new GlobalVariable* and update everything that used OldGV
588 // (a declaration or tentative definition) with the new GlobalVariable*
589 // (which will be a definition).
590 //
591 // This happens if there is a prototype for a global (e.g. "extern int x[];")
592 // and then a definition of a different type (e.g. "int x[10];"). This also
593 // happens when an initializer has a different type from the type of the
594 // global (this happens with unions).
Eli Friedman7008e9a2008-05-30 20:39:54 +0000595 //
596 // FIXME: This also ends up happening if there's a definition followed by
597 // a tentative definition! (Although Sema rejects that construct
598 // at the moment.)
Eli Friedman43a0ce82008-05-30 19:50:47 +0000599
600 // Save the old global
601 llvm::GlobalVariable *OldGV = GV;
602
603 // Make a new global with the correct type
604 GV = new llvm::GlobalVariable(InitType, false,
605 llvm::GlobalValue::ExternalLinkage,
Douglas Gregor3556bc72009-02-13 00:10:09 +0000606 0, getMangledName(D)->getName(),
607 &getModule(), 0, ASTTy.getAddressSpace());
Eli Friedman43a0ce82008-05-30 19:50:47 +0000608 // Steal the name of the old global
609 GV->takeName(OldGV);
610
611 // Replace all uses of the old global with the new global
612 llvm::Constant *NewPtrForOldDecl =
613 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
614 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
Eli Friedman43a0ce82008-05-30 19:50:47 +0000615
616 // Erase the old global, since it is no longer used.
617 OldGV->eraseFromParent();
Chris Lattner4b009652007-07-25 00:24:17 +0000618 }
Devang Patel8b5f5302007-10-26 16:31:40 +0000619
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000620 Entry = GV;
Devang Patel8b5f5302007-10-26 16:31:40 +0000621
Nate Begeman8a704172008-04-19 04:17:09 +0000622 if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
623 SourceManager &SM = Context.getSourceManager();
624 AddAnnotation(EmitAnnotateAttr(GV, AA,
Chris Lattner18c8dc02009-01-16 07:36:28 +0000625 SM.getInstantiationLineNumber(D->getLocation())));
Nate Begeman8a704172008-04-19 04:17:09 +0000626 }
627
Chris Lattner4b009652007-07-25 00:24:17 +0000628 GV->setInitializer(Init);
Nuno Lopesa7bbf562008-09-01 11:33:04 +0000629 GV->setConstant(D->getType().isConstant(Context));
Chris Lattner402b3372008-03-03 03:28:21 +0000630
Eli Friedman7008e9a2008-05-30 20:39:54 +0000631 // FIXME: This is silly; getTypeAlign should just work for incomplete arrays
632 unsigned Align;
Chris Lattnera1923f62008-08-04 07:31:14 +0000633 if (const IncompleteArrayType* IAT =
634 Context.getAsIncompleteArrayType(D->getType()))
Eli Friedman7008e9a2008-05-30 20:39:54 +0000635 Align = Context.getTypeAlign(IAT->getElementType());
636 else
637 Align = Context.getTypeAlign(D->getType());
Eli Friedmanb232e992008-05-29 11:10:27 +0000638 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) {
639 Align = std::max(Align, AA->getAlignment());
640 }
641 GV->setAlignment(Align / 8);
642
Chris Lattner402b3372008-03-03 03:28:21 +0000643 if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
Daniel Dunbar27250d32008-08-15 23:26:23 +0000644 setGlobalVisibility(GV, attr->getVisibility());
Chris Lattner402b3372008-03-03 03:28:21 +0000645 // FIXME: else handle -fvisibility
Daniel Dunbarced89142008-08-06 00:03:29 +0000646
647 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
648 // Prefaced with special LLVM marker to indicate that the name
649 // should not be munged.
650 GV->setName("\01" + ALA->getLabel());
651 }
Chris Lattner4b009652007-07-25 00:24:17 +0000652
653 // Set the llvm linkage type as appropriate.
Chris Lattner25094a42008-05-04 01:44:26 +0000654 if (D->getStorageClass() == VarDecl::Static)
655 GV->setLinkage(llvm::Function::InternalLinkage);
656 else if (D->getAttr<DLLImportAttr>())
Chris Lattner402b3372008-03-03 03:28:21 +0000657 GV->setLinkage(llvm::Function::DLLImportLinkage);
658 else if (D->getAttr<DLLExportAttr>())
659 GV->setLinkage(llvm::Function::DLLExportLinkage);
Chris Lattner25094a42008-05-04 01:44:26 +0000660 else if (D->getAttr<WeakAttr>())
Chris Lattner402b3372008-03-03 03:28:21 +0000661 GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
Chris Lattner25094a42008-05-04 01:44:26 +0000662 else {
Chris Lattner402b3372008-03-03 03:28:21 +0000663 // FIXME: This isn't right. This should handle common linkage and other
664 // stuff.
665 switch (D->getStorageClass()) {
Chris Lattner25094a42008-05-04 01:44:26 +0000666 case VarDecl::Static: assert(0 && "This case handled above");
Chris Lattner402b3372008-03-03 03:28:21 +0000667 case VarDecl::Auto:
668 case VarDecl::Register:
669 assert(0 && "Can't have auto or register globals");
670 case VarDecl::None:
671 if (!D->getInit())
Eli Friedmana7f46332008-05-29 11:03:17 +0000672 GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
Anders Carlsson689bba82008-12-03 05:51:23 +0000673 else
674 GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
Chris Lattner402b3372008-03-03 03:28:21 +0000675 break;
676 case VarDecl::Extern:
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000677 // FIXME: common
678 break;
679
Chris Lattner402b3372008-03-03 03:28:21 +0000680 case VarDecl::PrivateExtern:
Daniel Dunbar3a80fc02009-01-13 02:25:00 +0000681 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
682 // FIXME: common
Chris Lattner402b3372008-03-03 03:28:21 +0000683 break;
Chris Lattner402b3372008-03-03 03:28:21 +0000684 }
Chris Lattner4b009652007-07-25 00:24:17 +0000685 }
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000686
Daniel Dunbar97509722009-02-12 17:28:23 +0000687 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
688 GV->setSection(SA->getName());
689
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000690 // Emit global variable debug information.
691 CGDebugInfo *DI = getDebugInfo();
692 if(DI) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000693 DI->setLocation(D->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000694 DI->EmitGlobalVariable(GV, D);
695 }
Chris Lattner4b009652007-07-25 00:24:17 +0000696}
697
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000698llvm::GlobalValue *
699CodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) {
Daniel Dunbar566a6502008-09-08 23:44:31 +0000700 const llvm::Type *Ty = getTypes().ConvertType(D->getType());
701 llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty),
702 llvm::Function::ExternalLinkage,
Douglas Gregor3556bc72009-02-13 00:10:09 +0000703 getMangledName(D)->getName(),
704 &getModule());
Daniel Dunbar566a6502008-09-08 23:44:31 +0000705 SetFunctionAttributes(D, F);
706 return F;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000707}
708
709llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) {
Daniel Dunbar2188c532008-07-30 16:32:24 +0000710 QualType ASTTy = D->getType();
711 const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
712 const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000713
714 // Lookup the entry, lazily creating it if necessary.
Douglas Gregor3556bc72009-02-13 00:10:09 +0000715 llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000716 if (!Entry)
717 Entry = EmitForwardFunctionDefinition(D);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000718
Daniel Dunbar2188c532008-07-30 16:32:24 +0000719 return llvm::ConstantExpr::getBitCast(Entry, PTy);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000720}
721
722void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) {
Douglas Gregor3556bc72009-02-13 00:10:09 +0000723 llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)];
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000724 if (!Entry) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000725 Entry = EmitForwardFunctionDefinition(D);
726 } else {
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000727 // If the types mismatch then we have to rewrite the definition.
728 const llvm::Type *Ty = getTypes().ConvertType(D->getType());
729 if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000730 // Otherwise, we have a definition after a prototype with the wrong type.
731 // F is the Function* for the one with the wrong type, we must make a new
732 // Function* and update everything that used F (a declaration) with the new
733 // Function* (which will be a definition).
734 //
735 // This happens if there is a prototype for a function (e.g. "int f()") and
736 // then a definition of a different type (e.g. "int f(int x)"). Start by
737 // making a new function of the correct type, RAUW, then steal the name.
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000738 llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D);
739 NewFn->takeName(Entry);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000740
741 // Replace uses of F with the Function we will endow with a body.
742 llvm::Constant *NewPtrForOldDecl =
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000743 llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
744 Entry->replaceAllUsesWith(NewPtrForOldDecl);
745
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000746 // Ok, delete the old function now, which is dead.
Daniel Dunbara31eaf72008-08-05 23:31:02 +0000747 assert(Entry->isDeclaration() && "Shouldn't replace non-declaration");
Daniel Dunbar566a6502008-09-08 23:44:31 +0000748 Entry->eraseFromParent();
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000749
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000750 Entry = NewFn;
751 }
752 }
753
Daniel Dunbar566a6502008-09-08 23:44:31 +0000754 llvm::Function *Fn = cast<llvm::Function>(Entry);
755 CodeGenFunction(*this).GenerateCode(D, Fn);
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000756
Daniel Dunbar566a6502008-09-08 23:44:31 +0000757 SetFunctionAttributesForDefinition(D, Fn);
758
759 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) {
760 AddGlobalCtor(Fn, CA->getPriority());
761 } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) {
762 AddGlobalDtor(Fn, DA->getPriority());
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000763 }
764}
765
Daniel Dunbar18c2ec62008-10-01 00:49:24 +0000766llvm::Function *
767CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
768 const std::string &Name) {
769 llvm::Function *Fn = llvm::Function::Create(FTy,
770 llvm::Function::ExternalLinkage,
771 "", &TheModule);
772 RuntimeFunctions.push_back(std::make_pair(Fn, Name));
773 return Fn;
774}
775
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000776void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
777 // Make sure that this type is translated.
778 Types.UpdateCompletedType(TD);
Chris Lattner1b22f8b2008-02-05 08:06:13 +0000779}
780
781
Chris Lattnerab862cc2007-08-31 04:31:45 +0000782/// getBuiltinLibFunction
783llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
Chris Lattner9f2d6892007-12-13 00:38:03 +0000784 if (BuiltinID > BuiltinFunctions.size())
785 BuiltinFunctions.resize(BuiltinID);
Chris Lattnerab862cc2007-08-31 04:31:45 +0000786
Chris Lattner9f2d6892007-12-13 00:38:03 +0000787 // Cache looked up functions. Since builtin id #0 is invalid we don't reserve
788 // a slot for it.
789 assert(BuiltinID && "Invalid Builtin ID");
790 llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
Chris Lattnerab862cc2007-08-31 04:31:45 +0000791 if (FunctionSlot)
792 return FunctionSlot;
793
794 assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
795
796 // Get the name, skip over the __builtin_ prefix.
797 const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
798
799 // Get the type for the builtin.
800 QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
801 const llvm::FunctionType *Ty =
802 cast<llvm::FunctionType>(getTypes().ConvertType(Type));
803
804 // FIXME: This has a serious problem with code like this:
805 // void abs() {}
806 // ... __builtin_abs(x);
807 // The two versions of abs will collide. The fix is for the builtin to win,
808 // and for the existing one to be turned into a constantexpr cast of the
809 // builtin. In the case where the existing one is a static function, it
810 // should just be renamed.
Chris Lattner02c60f52007-08-31 04:44:06 +0000811 if (llvm::Function *Existing = getModule().getFunction(Name)) {
812 if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
813 return FunctionSlot = Existing;
814 assert(Existing == 0 && "FIXME: Name collision");
815 }
Chris Lattnerab862cc2007-08-31 04:31:45 +0000816
817 // FIXME: param attributes for sext/zext etc.
Nate Begemanad320b62008-04-20 06:29:50 +0000818 return FunctionSlot =
819 llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name,
820 &getModule());
Chris Lattnerab862cc2007-08-31 04:31:45 +0000821}
822
Chris Lattner4b23f942007-12-18 00:25:38 +0000823llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
824 unsigned NumTys) {
825 return llvm::Intrinsic::getDeclaration(&getModule(),
826 (llvm::Intrinsic::ID)IID, Tys, NumTys);
827}
Chris Lattnerab862cc2007-08-31 04:31:45 +0000828
Chris Lattner4b009652007-07-25 00:24:17 +0000829llvm::Function *CodeGenModule::getMemCpyFn() {
830 if (MemCpyFn) return MemCpyFn;
Chris Lattnere73302f2008-11-21 16:43:15 +0000831 const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
832 return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000833}
Anders Carlsson36a04872007-08-21 00:21:21 +0000834
Eli Friedman8f08a252008-05-26 12:59:39 +0000835llvm::Function *CodeGenModule::getMemMoveFn() {
836 if (MemMoveFn) return MemMoveFn;
Chris Lattnere73302f2008-11-21 16:43:15 +0000837 const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
838 return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1);
Eli Friedman8f08a252008-05-26 12:59:39 +0000839}
840
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000841llvm::Function *CodeGenModule::getMemSetFn() {
842 if (MemSetFn) return MemSetFn;
Chris Lattnere73302f2008-11-21 16:43:15 +0000843 const llvm::Type *IntPtr = TheTargetData.getIntPtrType();
844 return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1);
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000845}
Chris Lattner4b23f942007-12-18 00:25:38 +0000846
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000847static void appendFieldAndPadding(CodeGenModule &CGM,
848 std::vector<llvm::Constant*>& Fields,
Douglas Gregor8acb7272008-12-11 16:49:14 +0000849 FieldDecl *FieldD, FieldDecl *NextFieldD,
850 llvm::Constant* Field,
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000851 RecordDecl* RD, const llvm::StructType *STy)
852{
853 // Append the field.
854 Fields.push_back(Field);
855
Douglas Gregor8acb7272008-12-11 16:49:14 +0000856 int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD);
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000857
858 int NextStructFieldNo;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000859 if (!NextFieldD) {
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000860 NextStructFieldNo = STy->getNumElements();
861 } else {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000862 NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD);
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000863 }
864
865 // Append padding
866 for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) {
867 llvm::Constant *C =
868 llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1));
869
870 Fields.push_back(C);
871 }
872}
873
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000874// We still need to work out the details of handling UTF-16.
875// See: <rdr://2996215>
Chris Lattnerab862cc2007-08-31 04:31:45 +0000876llvm::Constant *CodeGenModule::
877GetAddrOfConstantCFString(const std::string &str) {
Anders Carlsson36a04872007-08-21 00:21:21 +0000878 llvm::StringMapEntry<llvm::Constant *> &Entry =
879 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
880
881 if (Entry.getValue())
882 return Entry.getValue();
883
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000884 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
885 llvm::Constant *Zeros[] = { Zero, Zero };
Anders Carlsson36a04872007-08-21 00:21:21 +0000886
887 if (!CFConstantStringClassRef) {
888 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
889 Ty = llvm::ArrayType::get(Ty, 0);
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000890
891 // FIXME: This is fairly broken if
892 // __CFConstantStringClassReference is already defined, in that it
893 // will get renamed and the user will most likely see an opaque
894 // error message. This is a general issue with relying on
895 // particular names.
896 llvm::GlobalVariable *GV =
Anders Carlsson36a04872007-08-21 00:21:21 +0000897 new llvm::GlobalVariable(Ty, false,
898 llvm::GlobalVariable::ExternalLinkage, 0,
899 "__CFConstantStringClassReference",
900 &getModule());
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000901
902 // Decay array -> ptr
903 CFConstantStringClassRef =
904 llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
Anders Carlsson36a04872007-08-21 00:21:21 +0000905 }
906
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000907 QualType CFTy = getContext().getCFConstantStringType();
908 RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl();
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000909
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000910 const llvm::StructType *STy =
911 cast<llvm::StructType>(getTypes().ConvertType(CFTy));
912
913 std::vector<llvm::Constant*> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000914 RecordDecl::field_iterator Field = CFRD->field_begin();
915
Anders Carlsson36a04872007-08-21 00:21:21 +0000916 // Class pointer.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000917 FieldDecl *CurField = *Field++;
918 FieldDecl *NextField = *Field++;
919 appendFieldAndPadding(*this, Fields, CurField, NextField,
920 CFConstantStringClassRef, CFRD, STy);
Anders Carlsson36a04872007-08-21 00:21:21 +0000921
922 // Flags.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000923 CurField = NextField;
924 NextField = *Field++;
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000925 const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000926 appendFieldAndPadding(*this, Fields, CurField, NextField,
927 llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy);
Anders Carlsson36a04872007-08-21 00:21:21 +0000928
929 // String pointer.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000930 CurField = NextField;
931 NextField = *Field++;
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000932 llvm::Constant *C = llvm::ConstantArray::get(str);
Anders Carlsson36a04872007-08-21 00:21:21 +0000933 C = new llvm::GlobalVariable(C->getType(), true,
934 llvm::GlobalValue::InternalLinkage,
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000935 C, ".str", &getModule());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000936 appendFieldAndPadding(*this, Fields, CurField, NextField,
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000937 llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2),
938 CFRD, STy);
Anders Carlsson36a04872007-08-21 00:21:21 +0000939
940 // String length.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000941 CurField = NextField;
942 NextField = 0;
Anders Carlsson36a04872007-08-21 00:21:21 +0000943 Ty = getTypes().ConvertType(getContext().LongTy);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000944 appendFieldAndPadding(*this, Fields, CurField, NextField,
945 llvm::ConstantInt::get(Ty, str.length()), CFRD, STy);
Anders Carlsson36a04872007-08-21 00:21:21 +0000946
947 // The struct.
Anders Carlsson0c0d8952008-11-15 18:54:24 +0000948 C = llvm::ConstantStruct::get(STy, Fields);
Anders Carlsson9be009e2007-11-01 00:41:52 +0000949 llvm::GlobalVariable *GV =
950 new llvm::GlobalVariable(C->getType(), true,
951 llvm::GlobalVariable::InternalLinkage,
952 C, "", &getModule());
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000953
Anders Carlsson9be009e2007-11-01 00:41:52 +0000954 GV->setSection("__DATA,__cfstring");
955 Entry.setValue(GV);
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000956
Anders Carlsson9be009e2007-11-01 00:41:52 +0000957 return GV;
Anders Carlsson36a04872007-08-21 00:21:21 +0000958}
Chris Lattnerdb6be562007-11-28 05:34:05 +0000959
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000960/// GetStringForStringLiteral - Return the appropriate bytes for a
Daniel Dunbar3c670e12008-08-10 20:25:57 +0000961/// string literal, properly padded to match the literal type.
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000962std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
Daniel Dunbar952f4732008-08-29 17:28:43 +0000963 if (E->isWide()) {
964 ErrorUnsupported(E, "wide string");
965 return "FIXME";
966 }
967
Daniel Dunbar3c670e12008-08-10 20:25:57 +0000968 const char *StrData = E->getStrData();
969 unsigned Len = E->getByteLength();
970
971 const ConstantArrayType *CAT =
972 getContext().getAsConstantArrayType(E->getType());
973 assert(CAT && "String isn't pointer or array!");
974
975 // Resize the string to the right size
976 // FIXME: What about wchar_t strings?
977 std::string Str(StrData, StrData+Len);
978 uint64_t RealLen = CAT->getSize().getZExtValue();
979 Str.resize(RealLen, '\0');
980
981 return Str;
982}
983
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000984/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
985/// constant array for the given string literal.
986llvm::Constant *
987CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
988 // FIXME: This can be more efficient.
989 return GetAddrOfConstantString(GetStringForStringLiteral(S));
990}
991
Chris Lattnera6dcce32008-02-11 00:02:17 +0000992/// GenerateWritableString -- Creates storage for a string literal.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000993static llvm::Constant *GenerateStringLiteral(const std::string &str,
994 bool constant,
Daniel Dunbara70e1d72008-10-17 21:56:50 +0000995 CodeGenModule &CGM,
996 const char *GlobalName) {
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000997 // Create Constant for this string literal. Don't add a '\0'.
998 llvm::Constant *C = llvm::ConstantArray::get(str, false);
Chris Lattnerdb6be562007-11-28 05:34:05 +0000999
1000 // Create a global variable for this string
1001 C = new llvm::GlobalVariable(C->getType(), constant,
1002 llvm::GlobalValue::InternalLinkage,
Daniel Dunbara70e1d72008-10-17 21:56:50 +00001003 C,
1004 GlobalName ? GlobalName : ".str",
1005 &CGM.getModule());
Daniel Dunbar31fe9c32008-08-13 23:20:05 +00001006
Chris Lattnerdb6be562007-11-28 05:34:05 +00001007 return C;
1008}
1009
Daniel Dunbar31fe9c32008-08-13 23:20:05 +00001010/// GetAddrOfConstantString - Returns a pointer to a character array
1011/// containing the literal. This contents are exactly that of the
1012/// given string, i.e. it will not be null terminated automatically;
1013/// see GetAddrOfConstantCString. Note that whether the result is
1014/// actually a pointer to an LLVM constant depends on
1015/// Feature.WriteableStrings.
1016///
1017/// The result has pointer to array type.
Daniel Dunbara70e1d72008-10-17 21:56:50 +00001018llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
1019 const char *GlobalName) {
Chris Lattnerdb6be562007-11-28 05:34:05 +00001020 // Don't share any string literals if writable-strings is turned on.
1021 if (Features.WritableStrings)
Daniel Dunbara70e1d72008-10-17 21:56:50 +00001022 return GenerateStringLiteral(str, false, *this, GlobalName);
Chris Lattnerdb6be562007-11-28 05:34:05 +00001023
1024 llvm::StringMapEntry<llvm::Constant *> &Entry =
1025 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
1026
1027 if (Entry.getValue())
1028 return Entry.getValue();
1029
1030 // Create a global variable for this.
Daniel Dunbara70e1d72008-10-17 21:56:50 +00001031 llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
Chris Lattnerdb6be562007-11-28 05:34:05 +00001032 Entry.setValue(C);
1033 return C;
1034}
Daniel Dunbar31fe9c32008-08-13 23:20:05 +00001035
1036/// GetAddrOfConstantCString - Returns a pointer to a character
1037/// array containing the literal and a terminating '\-'
1038/// character. The result has pointer to array type.
Daniel Dunbara70e1d72008-10-17 21:56:50 +00001039llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
1040 const char *GlobalName){
Chris Lattnerb2176012008-12-09 19:10:54 +00001041 return GetAddrOfConstantString(str + '\0', GlobalName);
Daniel Dunbar31fe9c32008-08-13 23:20:05 +00001042}
Daniel Dunbar27250d32008-08-15 23:26:23 +00001043
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001044/// EmitObjCPropertyImplementations - Emit information for synthesized
1045/// properties for an implementation.
1046void CodeGenModule::EmitObjCPropertyImplementations(const
1047 ObjCImplementationDecl *D) {
1048 for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(),
1049 e = D->propimpl_end(); i != e; ++i) {
1050 ObjCPropertyImplDecl *PID = *i;
1051
1052 // Dynamic is just for type-checking.
1053 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1054 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1055
1056 // Determine which methods need to be implemented, some may have
1057 // been overridden. Note that ::isSynthesized is not the method
1058 // we want, that just indicates if the decl came from a
1059 // property. What we want to know is if the method is defined in
1060 // this implementation.
1061 if (!D->getInstanceMethod(PD->getGetterName()))
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +00001062 CodeGenFunction(*this).GenerateObjCGetter(
1063 const_cast<ObjCImplementationDecl *>(D), PID);
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001064 if (!PD->isReadOnly() &&
1065 !D->getInstanceMethod(PD->getSetterName()))
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +00001066 CodeGenFunction(*this).GenerateObjCSetter(
1067 const_cast<ObjCImplementationDecl *>(D), PID);
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001068 }
1069 }
1070}
1071
Daniel Dunbar27250d32008-08-15 23:26:23 +00001072/// EmitTopLevelDecl - Emit code for a single top level declaration.
1073void CodeGenModule::EmitTopLevelDecl(Decl *D) {
1074 // If an error has occurred, stop code generation, but continue
1075 // parsing and semantic analysis (to ensure all warnings and errors
1076 // are emitted).
1077 if (Diags.hasErrorOccurred())
1078 return;
1079
1080 switch (D->getKind()) {
1081 case Decl::Function:
1082 case Decl::Var:
1083 EmitGlobal(cast<ValueDecl>(D));
1084 break;
1085
1086 case Decl::Namespace:
Daniel Dunbar952f4732008-08-29 17:28:43 +00001087 ErrorUnsupported(D, "namespace");
Daniel Dunbar27250d32008-08-15 23:26:23 +00001088 break;
1089
1090 // Objective-C Decls
1091
1092 // Forward declarations, no (immediate) code generation.
1093 case Decl::ObjCClass:
1094 case Decl::ObjCCategory:
1095 case Decl::ObjCForwardProtocol:
1096 case Decl::ObjCInterface:
1097 break;
1098
1099 case Decl::ObjCProtocol:
1100 Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
1101 break;
1102
1103 case Decl::ObjCCategoryImpl:
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001104 // Categories have properties but don't support synthesize so we
1105 // can ignore them here.
1106
Daniel Dunbar27250d32008-08-15 23:26:23 +00001107 Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
1108 break;
1109
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001110 case Decl::ObjCImplementation: {
1111 ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1112 EmitObjCPropertyImplementations(OMD);
1113 Runtime->GenerateClass(OMD);
Daniel Dunbar27250d32008-08-15 23:26:23 +00001114 break;
Daniel Dunbar6b57d432008-08-26 08:29:31 +00001115 }
Daniel Dunbar27250d32008-08-15 23:26:23 +00001116 case Decl::ObjCMethod: {
1117 ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
1118 // If this is not a prototype, emit the body.
1119 if (OMD->getBody())
1120 CodeGenFunction(*this).GenerateObjCMethod(OMD);
1121 break;
1122 }
Daniel Dunbar27250d32008-08-15 23:26:23 +00001123 case Decl::ObjCCompatibleAlias:
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +00001124 // compatibility-alias is a directive and has no code gen.
Daniel Dunbar27250d32008-08-15 23:26:23 +00001125 break;
1126
1127 case Decl::LinkageSpec: {
1128 LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D);
1129 if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
Daniel Dunbar9503b782008-08-16 00:56:44 +00001130 ErrorUnsupported(LSD, "linkage spec");
Daniel Dunbar27250d32008-08-15 23:26:23 +00001131 // FIXME: implement C++ linkage, C linkage works mostly by C
1132 // language reuse already.
1133 break;
1134 }
1135
1136 case Decl::FileScopeAsm: {
1137 FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
1138 std::string AsmString(AD->getAsmString()->getStrData(),
1139 AD->getAsmString()->getByteLength());
1140
1141 const std::string &S = getModule().getModuleInlineAsm();
1142 if (S.empty())
1143 getModule().setModuleInlineAsm(AsmString);
1144 else
1145 getModule().setModuleInlineAsm(S + '\n' + AsmString);
1146 break;
1147 }
1148
1149 default:
1150 // Make sure we handled everything we should, every other kind is
1151 // a non-top-level decl. FIXME: Would be nice to have an
1152 // isTopLevelDeclKind function. Need to recode Decl::Kind to do
1153 // that easily.
1154 assert(isa<TypeDecl>(D) && "Unsupported decl kind");
1155 }
1156}