Merge pull request #6745 from yebblies/cppmanglert

Let Target determine how to mangle extern(C++) symbols
merged-on-behalf-of: Iain Buclaw <ibuclaw@gdcproject.org>
This commit is contained in:
The Dlang Bot 2017-05-06 19:45:18 +02:00 committed by GitHub
commit 2cc58d9b0e
4 changed files with 1697 additions and 1682 deletions

View file

@ -37,13 +37,12 @@ import ddmd.visitor;
* ABI has no concept of. These affect every D mangled name, * ABI has no concept of. These affect every D mangled name,
* so nothing would be compatible anyway. * so nothing would be compatible anyway.
*/ */
static if (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS)
{ /*
/*
* Follows Itanium C++ ABI 1.86 * Follows Itanium C++ ABI 1.86
*/ */
extern (C++) final class CppMangleVisitor : Visitor extern (C++) final class CppMangleVisitor : Visitor
{ {
alias visit = super.visit; alias visit = super.visit;
Objects components; Objects components;
OutBuffer buf; OutBuffer buf;
@ -530,7 +529,7 @@ static if (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TAR
buf.writeByte('v'); // encode ( ) parameters buf.writeByte('v'); // encode ( ) parameters
} }
public: public:
extern (D) this() extern (D) this()
{ {
this.components_on = true; this.components_on = true;
@ -920,30 +919,28 @@ static if (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TAR
cpp_mangle_name(s, false); cpp_mangle_name(s, false);
return buf.extractString(); return buf.extractString();
} }
} }
extern (C++) const(char)* toCppMangle(Dsymbol s) extern (C++) const(char)* toCppMangleItanium(Dsymbol s)
{ {
//printf("toCppMangle(%s)\n", s.toChars()); //printf("toCppMangleItanium(%s)\n", s.toChars());
scope CppMangleVisitor v = new CppMangleVisitor(); scope CppMangleVisitor v = new CppMangleVisitor();
return v.mangleOf(s); return v.mangleOf(s);
} }
extern (C++) const(char)* cppTypeInfoMangle(Dsymbol s) extern (C++) const(char)* cppTypeInfoMangleItanium(Dsymbol s)
{ {
//printf("cppTypeInfoMangle(%s)\n", s.toChars()); //printf("cppTypeInfoMangle(%s)\n", s.toChars());
scope CppMangleVisitor v = new CppMangleVisitor(); scope CppMangleVisitor v = new CppMangleVisitor();
return v.mangle_typeinfo(s); return v.mangle_typeinfo(s);
}
} }
else static if (TARGET_WINDOS)
{
// Windows DMC and Microsoft Visual C++ mangling
enum VC_SAVED_TYPE_CNT = 10u;
enum VC_SAVED_IDENT_CNT = 10u;
extern (C++) final class VisualCPPMangler : Visitor // Windows DMC and Microsoft Visual C++ mangling
{ enum VC_SAVED_TYPE_CNT = 10u;
enum VC_SAVED_IDENT_CNT = 10u;
extern (C++) final class VisualCPPMangler : Visitor
{
alias visit = super.visit; alias visit = super.visit;
const(char)*[VC_SAVED_IDENT_CNT] saved_idents; const(char)*[VC_SAVED_IDENT_CNT] saved_idents;
Type[VC_SAVED_TYPE_CNT] saved_types; Type[VC_SAVED_TYPE_CNT] saved_types;
@ -978,7 +975,7 @@ else static if (TARGET_WINDOS)
memcpy(&saved_types, &rvl.saved_types, Type.sizeof * VC_SAVED_TYPE_CNT); memcpy(&saved_types, &rvl.saved_types, Type.sizeof * VC_SAVED_TYPE_CNT);
} }
public: public:
extern (D) this(bool isdmc) extern (D) this(bool isdmc)
{ {
if (isdmc) if (isdmc)
@ -1376,7 +1373,7 @@ else static if (TARGET_WINDOS)
return buf.extractString(); return buf.extractString();
} }
private: private:
void mangleFunction(FuncDeclaration d) void mangleFunction(FuncDeclaration d)
{ {
// <function mangle> ? <qualified name> <flags> <return type> <arg list> // <function mangle> ? <qualified name> <flags> <return type> <arg list>
@ -1960,21 +1957,16 @@ else static if (TARGET_WINDOS)
memcpy(&saved_types, &tmp.saved_types, Type.sizeof * VC_SAVED_TYPE_CNT); memcpy(&saved_types, &tmp.saved_types, Type.sizeof * VC_SAVED_TYPE_CNT);
return ret; return ret;
} }
} }
extern (C++) const(char)* toCppMangle(Dsymbol s) extern (C++) const(char)* toCppMangleMSVC(Dsymbol s)
{ {
scope VisualCPPMangler v = new VisualCPPMangler(!global.params.mscoff); scope VisualCPPMangler v = new VisualCPPMangler(!global.params.mscoff);
return v.mangleOf(s); return v.mangleOf(s);
} }
extern (C++) const(char)* cppTypeInfoMangle(Dsymbol s) extern (C++) const(char)* cppTypeInfoMangleMSVC(Dsymbol s)
{ {
//printf("cppTypeInfoMangle(%s)\n", s.toChars()); //printf("cppTypeInfoMangle(%s)\n", s.toChars());
assert(0); assert(0);
}
}
else
{
static assert(0, "fix this");
} }

View file

@ -28,6 +28,7 @@ import ddmd.id;
import ddmd.mtype; import ddmd.mtype;
import ddmd.root.ctfloat; import ddmd.root.ctfloat;
import ddmd.root.outbuffer; import ddmd.root.outbuffer;
import ddmd.target;
import ddmd.utf; import ddmd.utf;
import ddmd.visitor; import ddmd.visitor;
@ -451,7 +452,7 @@ public:
buf.writestring(d.ident.toChars()); buf.writestring(d.ident.toChars());
return; return;
case LINKcpp: case LINKcpp:
buf.writestring(toCppMangle(d)); buf.writestring(Target.toCppMangle(d));
return; return;
case LINKdefault: case LINKdefault:
d.error("forward declaration"); d.error("forward declaration");

View file

@ -10,7 +10,10 @@
module ddmd.target; module ddmd.target;
import ddmd.cppmangle;
import ddmd.dclass;
import ddmd.dmodule; import ddmd.dmodule;
import ddmd.dsymbol;
import ddmd.expression; import ddmd.expression;
import ddmd.globals; import ddmd.globals;
import ddmd.identifier; import ddmd.identifier;
@ -429,6 +432,25 @@ struct Target
break; break;
} }
} }
extern (C++) static const(char)* toCppMangle(Dsymbol s)
{
static if (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS)
return toCppMangleItanium(s);
else static if (TARGET_WINDOS)
return toCppMangleMSVC(s);
else
static assert(0, "fix this");
}
extern (C++) static const(char)* cppTypeInfoMangle(ClassDeclaration cd)
{
static if (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS)
return cppTypeInfoMangleItanium(cd);
else static if (TARGET_WINDOS)
return cppTypeInfoMangleMSVC(cd);
else
static assert(0, "fix this");
}
} }
/****************************** /******************************

View file

@ -777,7 +777,7 @@ Symbol* toSymbolCpp(ClassDeclaration cd)
*/ */
Symbol *toSymbolCppTypeInfo(ClassDeclaration cd) Symbol *toSymbolCppTypeInfo(ClassDeclaration cd)
{ {
const id = cppTypeInfoMangle(cd); const id = Target.cppTypeInfoMangle(cd);
auto s = symbol_calloc(id, cast(uint)strlen(id)); auto s = symbol_calloc(id, cast(uint)strlen(id));
s.Sclass = SCextern; s.Sclass = SCextern;
s.Sfl = FLextern; // C++ code will provide the definition s.Sfl = FLextern; // C++ code will provide the definition