Improve code generation for function template specializations:
- Track implicit instantiations vs. the not-yet-supported explicit
specializations
- Give implicit instantiations of function templates (and member
functions of class templates) linkonce_odr linkage.
- Improve name mangling for function template specializations,
including the template arguments of the instantiation and the return
type of the function.
Note that our name-mangling is improved, but not correct: we still
don't mangle substitutions, although the manglings we produce can be
demangled.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74466 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index e25fe90..fa4d93c 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -584,7 +584,7 @@
if (FunctionTemplateSpecializationInfo *Info
= TemplateOrSpecialization
.dyn_cast<FunctionTemplateSpecializationInfo*>()) {
- return Info->Template;
+ return Info->Template.getPointer();
}
return 0;
}
@@ -610,7 +610,8 @@
Info = new (Context) FunctionTemplateSpecializationInfo;
Info->Function = this;
- Info->Template = Template;
+ Info->Template.setPointer(Template);
+ Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Info->TemplateArguments = TemplateArgs;
TemplateOrSpecialization = Info;
@@ -619,6 +620,26 @@
Template->getSpecializations().InsertNode(Info, InsertPos);
}
+bool FunctionDecl::isExplicitSpecialization() const {
+ // FIXME: check this property for explicit specializations of member
+ // functions of class templates.
+ FunctionTemplateSpecializationInfo *Info
+ = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
+ if (!Info)
+ return false;
+
+ return Info->isExplicitSpecialization();
+}
+
+void FunctionDecl::setExplicitSpecialization(bool ES) {
+ // FIXME: set this property for explicit specializations of member functions
+ // of class templates.
+ FunctionTemplateSpecializationInfo *Info
+ = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
+ if (Info)
+ Info->setExplicitSpecialization(ES);
+}
+
//===----------------------------------------------------------------------===//
// TagDecl Implementation
//===----------------------------------------------------------------------===//
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index acd90e2..7957a9f 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -243,12 +243,20 @@
static CodeGenModule::GVALinkage
GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
const LangOptions &Features) {
+ // The kind of external linkage this function will have, if it is not
+ // inline or static.
+ CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal;
+ if (Context.getLangOptions().CPlusPlus &&
+ (FD->getPrimaryTemplate() || FD->getInstantiatedFromMemberFunction()) &&
+ !FD->isExplicitSpecialization())
+ External = CodeGenModule::GVA_TemplateInstantiation;
+
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
// C++ member functions defined inside the class are always inline.
if (MD->isInline() || !MD->isOutOfLine())
return CodeGenModule::GVA_CXXInline;
- return CodeGenModule::GVA_StrongExternal;
+ return External;
}
// "static" functions get internal linkage.
@@ -256,7 +264,7 @@
return CodeGenModule::GVA_Internal;
if (!FD->isInline())
- return CodeGenModule::GVA_StrongExternal;
+ return External;
// If the inline function explicitly has the GNU inline attribute on it, or if
// this is C89 mode, we use to GNU semantics.
@@ -273,7 +281,7 @@
if (FD->isExternGNUInline(Context))
return CodeGenModule::GVA_C99Inline;
// Normal inline is a strong symbol.
- return CodeGenModule::GVA_StrongExternal;
+ return External;
}
// The definition of inline changes based on the language. Note that we
@@ -306,7 +314,7 @@
// In C99 mode, 'inline' functions are guaranteed to have a strong
// definition somewhere else, so we can use available_externally linkage.
GV->setLinkage(llvm::Function::AvailableExternallyLinkage);
- } else if (Linkage == GVA_CXXInline) {
+ } else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) {
// In C++, the compiler has to emit a definition in every translation unit
// that references the function. We should use linkonce_odr because
// a) if all references in this translation unit are optimized away, we
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index 4d50e89..ba9f1b2 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -373,7 +373,8 @@
GVA_Internal,
GVA_C99Inline,
GVA_CXXInline,
- GVA_StrongExternal
+ GVA_StrongExternal,
+ GVA_TemplateInstantiation
};
private:
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index b5ad5ac..8501835 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -170,7 +170,29 @@
void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
// <encoding> ::= <function name> <bare-function-type>
mangleName(FD);
- mangleBareFunctionType(FD->getType()->getAsFunctionType(), false);
+
+ // Whether the mangling of a function type includes the return type depends
+ // on the context and the nature of the function. The rules for deciding
+ // whether the return type is included are:
+ //
+ // 1. Template functions (names or types) have return types encoded, with
+ // the exceptions listed below.
+ // 2. Function types not appearing as part of a function name mangling,
+ // e.g. parameters, pointer types, etc., have return type encoded, with the
+ // exceptions listed below.
+ // 3. Non-template function names do not have return types encoded.
+ //
+ // The exceptions mentioned in (1) and (2) above, for which the return
+ // type is never included, are
+ // 1. Constructors.
+ // 2. Destructors.
+ // 3. Conversion operator functions, e.g. operator int.
+ bool MangleReturnType = false;
+ if (FD->getPrimaryTemplate() &&
+ !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
+ isa<CXXConversionDecl>(FD)))
+ MangleReturnType = true;
+ mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType);
}
static bool isStdNamespace(const DeclContext *DC) {
@@ -253,6 +275,12 @@
assert(false && "Can't mangle a using directive name!");
break;
}
+
+ if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
+ if (const TemplateArgumentList *TemplateArgs
+ = Function->getTemplateSpecializationArgs())
+ mangleTemplateArgumentList(*TemplateArgs);
+ }
}
void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {