Deleted Added
sdiff udiff text old ( 195099 ) new ( 195341 )
full compact
1//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
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//===----------------------------------------------------------------------===//

--- 88 unchanged lines hidden (view full) ---

97}
98
99LangOptions::VisibilityMode
100CodeGenModule::getDeclVisibilityMode(const Decl *D) const {
101 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
102 if (VD->getStorageClass() == VarDecl::PrivateExtern)
103 return LangOptions::Hidden;
104
105 if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>(getContext())) {
106 switch (attr->getVisibility()) {
107 default: assert(0 && "Unknown visibility!");
108 case VisibilityAttr::DefaultVisibility:
109 return LangOptions::Default;
110 case VisibilityAttr::HiddenVisibility:
111 return LangOptions::Hidden;
112 case VisibilityAttr::ProtectedVisibility:
113 return LangOptions::Protected;

--- 124 unchanged lines hidden (view full) ---

238 llvm::GlobalValue::AppendingLinkage, Array,
239 "llvm.global.annotations", &TheModule);
240 gv->setSection("llvm.metadata");
241}
242
243static CodeGenModule::GVALinkage
244GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
245 const LangOptions &Features) {
246 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
247 // C++ member functions defined inside the class are always inline.
248 if (MD->isInline() || !MD->isOutOfLine())
249 return CodeGenModule::GVA_CXXInline;
250
251 return CodeGenModule::GVA_StrongExternal;
252 }
253
254 // "static" functions get internal linkage.
255 if (FD->getStorageClass() == FunctionDecl::Static)
256 return CodeGenModule::GVA_Internal;
257
258 if (!FD->isInline())
259 return CodeGenModule::GVA_StrongExternal;
260
261 // If the inline function explicitly has the GNU inline attribute on it, or if
262 // this is C89 mode, we use to GNU semantics.
263 if (!Features.C99 && !Features.CPlusPlus) {
264 // extern inline in GNU mode is like C99 inline.
265 if (FD->getStorageClass() == FunctionDecl::Extern)
266 return CodeGenModule::GVA_C99Inline;
267 // Normal inline is a strong symbol.
268 return CodeGenModule::GVA_StrongExternal;
269 } else if (FD->hasActiveGNUInlineAttribute(Context)) {
270 // GCC in C99 mode seems to use a different decision-making
271 // process for extern inline, which factors in previous
272 // declarations.
273 if (FD->isExternGNUInline(Context))
274 return CodeGenModule::GVA_C99Inline;
275 // Normal inline is a strong symbol.
276 return CodeGenModule::GVA_StrongExternal;
277 }
278
279 // The definition of inline changes based on the language. Note that we
280 // have already handled "static inline" above, with the GVA_Internal case.
281 if (Features.CPlusPlus) // inline and extern inline.
282 return CodeGenModule::GVA_CXXInline;
283
284 assert(Features.C99 && "Must be in C99 mode if not in C89 or C++ mode");

--- 8 unchanged lines hidden (view full) ---

293/// FIXME: This is currently only done for aliases and functions, but not for
294/// variables (these details are set in EmitGlobalVarDefinition for variables).
295void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D,
296 llvm::GlobalValue *GV) {
297 GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features);
298
299 if (Linkage == GVA_Internal) {
300 GV->setLinkage(llvm::Function::InternalLinkage);
301 } else if (D->hasAttr<DLLExportAttr>(getContext())) {
302 GV->setLinkage(llvm::Function::DLLExportLinkage);
303 } else if (D->hasAttr<WeakAttr>(getContext())) {
304 GV->setLinkage(llvm::Function::WeakAnyLinkage);
305 } else if (Linkage == GVA_C99Inline) {
306 // In C99 mode, 'inline' functions are guaranteed to have a strong
307 // definition somewhere else, so we can use available_externally linkage.
308 GV->setLinkage(llvm::Function::AvailableExternallyLinkage);
309 } else if (Linkage == GVA_CXXInline) {
310 // In C++, the compiler has to emit a definition in every translation unit
311 // that references the function. We should use linkonce_odr because
312 // a) if all references in this translation unit are optimized away, we
313 // don't need to codegen it. b) if the function persists, it needs to be
314 // merged with other definitions. c) C++ has the ODR, so we know the
315 // definition is dependable.
316 GV->setLinkage(llvm::Function::LinkOnceODRLinkage);
317 } else {

--- 10 unchanged lines hidden (view full) ---

328 llvm::Function *F) {
329 AttributeListType AttributeList;
330 ConstructAttributeList(Info, D, AttributeList);
331
332 F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
333 AttributeList.size()));
334
335 // Set the appropriate calling convention for the Function.
336 if (D->hasAttr<FastCallAttr>(getContext()))
337 F->setCallingConv(llvm::CallingConv::X86_FastCall);
338
339 if (D->hasAttr<StdCallAttr>(getContext()))
340 F->setCallingConv(llvm::CallingConv::X86_StdCall);
341}
342
343void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
344 llvm::Function *F) {
345 if (!Features.Exceptions && !Features.ObjCNonFragileABI)
346 F->addFnAttr(llvm::Attribute::NoUnwind);
347
348 if (D->hasAttr<AlwaysInlineAttr>(getContext()))
349 F->addFnAttr(llvm::Attribute::AlwaysInline);
350
351 if (D->hasAttr<NoinlineAttr>(getContext()))
352 F->addFnAttr(llvm::Attribute::NoInline);
353}
354
355void CodeGenModule::SetCommonAttributes(const Decl *D,
356 llvm::GlobalValue *GV) {
357 setGlobalVisibility(GV, D);
358
359 if (D->hasAttr<UsedAttr>(getContext()))
360 AddUsedGlobal(GV);
361
362 if (const SectionAttr *SA = D->getAttr<SectionAttr>(getContext()))
363 GV->setSection(SA->getName());
364}
365
366void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
367 llvm::Function *F,
368 const CGFunctionInfo &FI) {
369 SetLLVMFunctionAttributes(D, FI, F);
370 SetLLVMFunctionAttributesForDefinition(D, F);

--- 7 unchanged lines hidden (view full) ---

378 llvm::Function *F,
379 bool IsIncompleteFunction) {
380 if (!IsIncompleteFunction)
381 SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
382
383 // Only a few attributes are set on declarations; these may later be
384 // overridden by a definition.
385
386 if (FD->hasAttr<DLLImportAttr>(getContext())) {
387 F->setLinkage(llvm::Function::DLLImportLinkage);
388 } else if (FD->hasAttr<WeakAttr>(getContext()) ||
389 FD->hasAttr<WeakImportAttr>(getContext())) {
390 // "extern_weak" is overloaded in LLVM; we probably should have
391 // separate linkage types for this.
392 F->setLinkage(llvm::Function::ExternalWeakLinkage);
393 } else {
394 F->setLinkage(llvm::Function::ExternalLinkage);
395 }
396
397 if (const SectionAttr *SA = FD->getAttr<SectionAttr>(getContext()))
398 F->setSection(SA->getName());
399}
400
401void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
402 assert(!GV->isDeclaration() &&
403 "Only globals with definition can force usage.");
404 LLVMUsed.push_back(GV);
405}

--- 97 unchanged lines hidden (view full) ---

503 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo)
504 };
505 return llvm::ConstantStruct::get(Fields, 4, false);
506}
507
508bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
509 // Never defer when EmitAllDecls is specified or the decl has
510 // attribute used.
511 if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>(getContext()))
512 return false;
513
514 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
515 // Constructors and destructors should never be deferred.
516 if (FD->hasAttr<ConstructorAttr>(getContext()) ||
517 FD->hasAttr<DestructorAttr>(getContext()))
518 return false;
519
520 GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features);
521
522 // static, static inline, always_inline, and extern inline functions can
523 // always be deferred. Normal inline functions can be deferred in C99/C++.
524 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
525 Linkage == GVA_CXXInline)

--- 7 unchanged lines hidden (view full) ---

533 return VD->getStorageClass() == VarDecl::Static;
534}
535
536void CodeGenModule::EmitGlobal(GlobalDecl GD) {
537 const ValueDecl *Global = GD.getDecl();
538
539 // If this is an alias definition (which otherwise looks like a declaration)
540 // emit it now.
541 if (Global->hasAttr<AliasAttr>(getContext()))
542 return EmitAliasDefinition(Global);
543
544 // Ignore declarations, they will be emitted on their first use.
545 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
546 // Forward declarations are emitted lazily on first use.
547 if (!FD->isThisDeclarationADefinition())
548 return;
549 } else {

--- 172 unchanged lines hidden (view full) ---

722 // FIXME: This code is overly simple and should be merged with other global
723 // handling.
724 GV->setConstant(D->getType().isConstant(Context));
725
726 // FIXME: Merge with other attribute handling code.
727 if (D->getStorageClass() == VarDecl::PrivateExtern)
728 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
729
730 if (D->hasAttr<WeakAttr>(getContext()) ||
731 D->hasAttr<WeakImportAttr>(getContext()))
732 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
733
734 GV->setThreadLocal(D->isThreadSpecified());
735 }
736
737 return Entry = GV;
738}
739

--- 103 unchanged lines hidden (view full) ---

843 llvm::Constant *NewPtrForOldDecl =
844 llvm::ConstantExpr::getBitCast(GV, Entry->getType());
845 Entry->replaceAllUsesWith(NewPtrForOldDecl);
846
847 // Erase the old global, since it is no longer used.
848 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
849 }
850
851 if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>(getContext())) {
852 SourceManager &SM = Context.getSourceManager();
853 AddAnnotation(EmitAnnotateAttr(GV, AA,
854 SM.getInstantiationLineNumber(D->getLocation())));
855 }
856
857 GV->setInitializer(Init);
858 GV->setConstant(D->getType().isConstant(Context));
859 GV->setAlignment(getContext().getDeclAlignInBytes(D));
860
861 // Set the llvm linkage type as appropriate.
862 if (D->getStorageClass() == VarDecl::Static)
863 GV->setLinkage(llvm::Function::InternalLinkage);
864 else if (D->hasAttr<DLLImportAttr>(getContext()))
865 GV->setLinkage(llvm::Function::DLLImportLinkage);
866 else if (D->hasAttr<DLLExportAttr>(getContext()))
867 GV->setLinkage(llvm::Function::DLLExportLinkage);
868 else if (D->hasAttr<WeakAttr>(getContext()))
869 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
870 else if (!CompileOpts.NoCommon &&
871 (!D->hasExternalStorage() && !D->getInit()))
872 GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
873 else
874 GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
875
876 SetCommonAttributes(D, GV);

--- 146 unchanged lines hidden (view full) ---

1023
1024 llvm::Function *Fn = cast<llvm::Function>(Entry);
1025
1026 CodeGenFunction(*this).GenerateCode(D, Fn);
1027
1028 SetFunctionDefinitionAttributes(D, Fn);
1029 SetLLVMFunctionAttributesForDefinition(D, Fn);
1030
1031 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>(getContext()))
1032 AddGlobalCtor(Fn, CA->getPriority());
1033 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>(getContext()))
1034 AddGlobalDtor(Fn, DA->getPriority());
1035}
1036
1037void CodeGenModule::EmitAliasDefinition(const ValueDecl *D) {
1038 const AliasAttr *AA = D->getAttr<AliasAttr>(getContext());
1039 assert(AA && "Not an alias?");
1040
1041 const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
1042
1043 // Unique the name through the identifier table.
1044 const char *AliaseeName = AA->getAliasee().c_str();
1045 AliaseeName = getContext().Idents.get(AliaseeName).getName();
1046

--- 39 unchanged lines hidden (view full) ---

1086
1087 // Now we know that there is no conflict, set the name.
1088 Entry = GA;
1089 GA->setName(MangledName);
1090
1091 // Set attributes which are particular to an alias; this is a
1092 // specialization of the attributes which may be set on a global
1093 // variable/function.
1094 if (D->hasAttr<DLLExportAttr>(getContext())) {
1095 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1096 // The dllexport attribute is ignored for undefined symbols.
1097 if (FD->getBody(getContext()))
1098 GA->setLinkage(llvm::Function::DLLExportLinkage);
1099 } else {
1100 GA->setLinkage(llvm::Function::DLLExportLinkage);
1101 }
1102 } else if (D->hasAttr<WeakAttr>(getContext()) ||
1103 D->hasAttr<WeakImportAttr>(getContext())) {
1104 GA->setLinkage(llvm::Function::WeakAnyLinkage);
1105 }
1106
1107 SetCommonAttributes(D, GA);
1108}
1109
1110/// getBuiltinLibFunction - Given a builtin id for a function like
1111/// "__builtin_fabsf", return a Function* for "fabsf".

--- 137 unchanged lines hidden (view full) ---

1249
1250 QualType CFTy = getContext().getCFConstantStringType();
1251 RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl();
1252
1253 const llvm::StructType *STy =
1254 cast<llvm::StructType>(getTypes().ConvertType(CFTy));
1255
1256 std::vector<llvm::Constant*> Fields;
1257 RecordDecl::field_iterator Field = CFRD->field_begin(getContext());
1258
1259 // Class pointer.
1260 FieldDecl *CurField = *Field++;
1261 FieldDecl *NextField = *Field++;
1262 appendFieldAndPadding(*this, Fields, CurField, NextField,
1263 CFConstantStringClassRef, CFRD, STy);
1264
1265 // Flags.

--- 153 unchanged lines hidden (view full) ---

1419 return GetAddrOfConstantString(str + '\0', GlobalName);
1420}
1421
1422/// EmitObjCPropertyImplementations - Emit information for synthesized
1423/// properties for an implementation.
1424void CodeGenModule::EmitObjCPropertyImplementations(const
1425 ObjCImplementationDecl *D) {
1426 for (ObjCImplementationDecl::propimpl_iterator
1427 i = D->propimpl_begin(getContext()),
1428 e = D->propimpl_end(getContext()); i != e; ++i) {
1429 ObjCPropertyImplDecl *PID = *i;
1430
1431 // Dynamic is just for type-checking.
1432 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1433 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1434
1435 // Determine which methods need to be implemented, some may have
1436 // been overridden. Note that ::isSynthesized is not the method
1437 // we want, that just indicates if the decl came from a
1438 // property. What we want to know is if the method is defined in
1439 // this implementation.
1440 if (!D->getInstanceMethod(getContext(), PD->getGetterName()))
1441 CodeGenFunction(*this).GenerateObjCGetter(
1442 const_cast<ObjCImplementationDecl *>(D), PID);
1443 if (!PD->isReadOnly() &&
1444 !D->getInstanceMethod(getContext(), PD->getSetterName()))
1445 CodeGenFunction(*this).GenerateObjCSetter(
1446 const_cast<ObjCImplementationDecl *>(D), PID);
1447 }
1448 }
1449}
1450
1451/// EmitNamespace - Emit all declarations in a namespace.
1452void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
1453 for (RecordDecl::decl_iterator I = ND->decls_begin(getContext()),
1454 E = ND->decls_end(getContext());
1455 I != E; ++I)
1456 EmitTopLevelDecl(*I);
1457}
1458
1459// EmitLinkageSpec - Emit all declarations in a linkage spec.
1460void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
1461 if (LSD->getLanguage() != LinkageSpecDecl::lang_c) {
1462 ErrorUnsupported(LSD, "linkage spec");
1463 return;
1464 }
1465
1466 for (RecordDecl::decl_iterator I = LSD->decls_begin(getContext()),
1467 E = LSD->decls_end(getContext());
1468 I != E; ++I)
1469 EmitTopLevelDecl(*I);
1470}
1471
1472/// EmitTopLevelDecl - Emit code for a single top level declaration.
1473void CodeGenModule::EmitTopLevelDecl(Decl *D) {
1474 // If an error has occurred, stop code generation, but continue
1475 // parsing and semantic analysis (to ensure all warnings and errors
1476 // are emitted).
1477 if (Diags.hasErrorOccurred())
1478 return;
1479
1480 switch (D->getKind()) {
1481 case Decl::CXXMethod:
1482 case Decl::Function:
1483 case Decl::Var:
1484 EmitGlobal(GlobalDecl(cast<ValueDecl>(D)));
1485 break;
1486
1487 // C++ Decls
1488 case Decl::Namespace:
1489 EmitNamespace(cast<NamespaceDecl>(D));
1490 break;
1491 // No code generation needed.
1492 case Decl::Using:
1493 break;
1494 case Decl::CXXConstructor:
1495 EmitCXXConstructors(cast<CXXConstructorDecl>(D));
1496 break;
1497 case Decl::CXXDestructor:
1498 EmitCXXDestructors(cast<CXXDestructorDecl>(D));
1499 break;
1500

--- 24 unchanged lines hidden (view full) ---

1525 ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
1526 EmitObjCPropertyImplementations(OMD);
1527 Runtime->GenerateClass(OMD);
1528 break;
1529 }
1530 case Decl::ObjCMethod: {
1531 ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
1532 // If this is not a prototype, emit the body.
1533 if (OMD->getBody(getContext()))
1534 CodeGenFunction(*this).GenerateObjCMethod(OMD);
1535 break;
1536 }
1537 case Decl::ObjCCompatibleAlias:
1538 // compatibility-alias is a directive and has no code gen.
1539 break;
1540
1541 case Decl::LinkageSpec:

--- 23 unchanged lines hidden ---