Skip to content

Commit 73af5c6

Browse files
committed
MS: Mangle rvalue references and nullptr_t, and produce back-references when
appropriate. Patch by João Matos! llvm-svn: 158895
1 parent 8897331 commit 73af5c6

File tree

4 files changed

+100
-15
lines changed

4 files changed

+100
-15
lines changed

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class MicrosoftCXXNameMangler {
3131
MangleContext &Context;
3232
raw_ostream &Out;
3333

34+
typedef llvm::DenseMap<const IdentifierInfo*, unsigned> BackRefMap;
35+
BackRefMap BackReferences;
36+
3437
ASTContext &getASTContext() const { return Context.getASTContext(); }
3538

3639
public:
@@ -641,7 +644,16 @@ void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
641644

642645
void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
643646
// <source name> ::= <identifier> @
644-
Out << II->getName() << '@';
647+
BackRefMap::iterator Found = BackReferences.find(II);
648+
if (Found == BackReferences.end()) {
649+
Out << II->getName() << '@';
650+
if (BackReferences.size() < 10) {
651+
size_t Size = BackReferences.size();
652+
BackReferences[II] = Size;
653+
}
654+
} else {
655+
Out << Found->second;
656+
}
645657
}
646658

647659
void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
@@ -938,17 +950,19 @@ void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T,
938950
case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
939951
case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
940952
case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
953+
954+
case BuiltinType::NullPtr: Out << "$$T"; break;
941955

942956
case BuiltinType::Char16:
943957
case BuiltinType::Char32:
944-
case BuiltinType::Half:
945-
case BuiltinType::NullPtr: {
958+
case BuiltinType::Half: {
946959
DiagnosticsEngine &Diags = Context.getDiags();
947960
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
948961
"cannot mangle this built-in %0 type yet");
949962
Diags.Report(Range.getBegin(), DiagID)
950963
<< T->getName(Context.getASTContext().getPrintingPolicy())
951964
<< Range;
965+
break;
952966
}
953967
}
954968
}
@@ -986,29 +1000,45 @@ void MicrosoftCXXNameMangler::mangleType(const FunctionType *T,
9861000
// ::= @ # structors (they have no declared return type)
9871001
if (IsStructor)
9881002
Out << '@';
989-
else
1003+
else {
1004+
QualType Result = Proto->getResultType();
1005+
const Type* RT = Result.getTypePtr();
1006+
if(isa<TagType>(RT) && !RT->isAnyPointerType() && !RT->isReferenceType())
1007+
Out << "?A";
9901008
// FIXME: Get the source range for the result type. Or, better yet,
9911009
// implement the unimplemented stuff so we don't need accurate source
9921010
// location info anymore :).
993-
mangleType(Proto->getResultType(), SourceRange());
1011+
mangleType(Result, SourceRange());
1012+
}
9941013

9951014
// <argument-list> ::= X # void
9961015
// ::= <type>+ @
9971016
// ::= <type>* Z # varargs
9981017
if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
9991018
Out << 'X';
10001019
} else {
1020+
typedef llvm::DenseMap<void*, unsigned> BackRef;
1021+
BackRef BackReferences;
10011022
if (D) {
10021023
// If we got a decl, use the type-as-written to make sure arrays
10031024
// get mangled right. Note that we can't rely on the TSI
10041025
// existing if (for example) the parameter was synthesized.
10051026
for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
10061027
ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
1007-
if (TypeSourceInfo *typeAsWritten = (*Parm)->getTypeSourceInfo())
1008-
mangleType(typeAsWritten->getType(),
1009-
typeAsWritten->getTypeLoc().getSourceRange());
1010-
else
1011-
mangleType((*Parm)->getType(), SourceRange());
1028+
TypeSourceInfo *TSI = (*Parm)->getTypeSourceInfo();
1029+
QualType Type = TSI ? TSI->getType() : (*Parm)->getType();
1030+
CanQualType Canonical = getASTContext().getCanonicalType(Type);
1031+
void *TypePtr = Canonical.getAsOpaquePtr();
1032+
BackRef::iterator Found = BackReferences.find(TypePtr);
1033+
if (Found == BackReferences.end()) {
1034+
mangleType(Type, (*Parm)->getSourceRange());
1035+
if (BackReferences.size() < 10 && (Canonical->getTypeClass() != Type::Builtin)) {
1036+
size_t Size = BackReferences.size();
1037+
BackReferences[TypePtr] = Size;
1038+
}
1039+
} else {
1040+
Out << Found->second;
1041+
}
10121042
}
10131043
} else {
10141044
for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
@@ -1316,13 +1346,16 @@ void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
13161346
mangleType(PointeeTy, Range);
13171347
}
13181348

1349+
// <type> ::= <r-value-reference-type>
1350+
// <r-value-reference-type> ::= $$Q <cvr-qualifiers> <type>
13191351
void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
13201352
SourceRange Range) {
1321-
DiagnosticsEngine &Diags = Context.getDiags();
1322-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1323-
"cannot mangle this r-value reference type yet");
1324-
Diags.Report(Range.getBegin(), DiagID)
1325-
<< Range;
1353+
Out << "$$Q";
1354+
QualType PointeeTy = T->getPointeeType();
1355+
if (!PointeeTy.hasQualifiers())
1356+
// Lack of qualifiers is mangled as 'A'.
1357+
Out << 'A';
1358+
mangleType(PointeeTy, Range);
13261359
}
13271360

13281361
void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 -fms-extensions -fblocks -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
2+
3+
void f1(const char* a, const char* b) {}
4+
// CHECK: "\01?f1@@YAXPBD0@Z"
5+
6+
void f2(const char* a, char* b) {}
7+
// CHECK: "\01?f2@@YAXPBDPAD@Z"
8+
9+
void f3(int a, const char* b, const char* c) {}
10+
// CHECK: "\01?f3@@YAXHPBD0@Z"
11+
12+
const char *f4(const char* a, const char* b) {}
13+
// CHECK: "\01?f4@@YAPBDPBD0@Z"
14+
15+
// FIXME: tests for more than 10 types?
16+
17+
struct S {};
18+
19+
void g4(const char* a, struct S* b, const char *c, struct S* d) {}
20+
// CHECK: "\01?g4@@YAXPBDPAUS@@01@Z"
21+
22+
typedef void (*VoidFunc)();
23+
24+
void foo_ptr(const char* a, const char* b, VoidFunc c, VoidFunc d) {}
25+
// CHECK: @"\01?foo_ptr@@YAXPBD0P6AXXZ1@Z"
26+
27+
// Make sure that different aliases of built-in types end up mangled as the
28+
// built-ins.
29+
typedef unsigned int uintptr_t;
30+
typedef unsigned int size_t;
31+
void *h(size_t a, uintptr_t b) {}
32+
// CHECK: "\01?h@@YAPAXII@Z"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -std=c++11 -fms-extensions -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
2+
3+
// CHECK: "\01?LRef@@YAXAAH@Z"
4+
void LRef(int& a) { }
5+
6+
// CHECK: "\01?RRef@@YAH$$QAH@Z"
7+
int RRef(int&& a) { return a; }
8+
9+
// CHECK: "\01?Null@@YAX$$T@Z"
10+
namespace std { typedef decltype(__nullptr) nullptr_t; }
11+
void Null(std::nullptr_t) {}

clang/test/CodeGenCXX/mangle-ms.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,12 @@ typedef double RGB[3];
132132
RGB color1;
133133
extern const RGB color2 = {};
134134
extern RGB const ((color3)[5]) = {};
135+
136+
// PR12603
137+
enum E {};
138+
// CHECK: "\01?fooE@@YA?AW4E@@XZ"
139+
E fooE() { return E(); }
140+
141+
class X {};
142+
// CHECK: "\01?fooX@@YA?AVX@@XZ"
143+
X fooX() { return X(); }

0 commit comments

Comments
 (0)