Skip to content

Commit

Permalink
add support for Object.RTTypeid template
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jul 29, 2020
1 parent 523feaf commit 18b5cd0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/dmd/dsymbolsem.d
Expand Up @@ -2710,6 +2710,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
{
if (tempdecl.ident == Id.RTInfo)
Type.rtinfo = tempdecl;

if (tempdecl.ident == Id.RTTypeid)
Type.rttypeid = tempdecl;
}

/* Remember Scope for later instantiations, but make
Expand Down
1 change: 1 addition & 0 deletions src/dmd/id.d
Expand Up @@ -107,6 +107,7 @@ immutable Msgtable[] msgtable =
{ "outer" },
{ "Exception" },
{ "RTInfo" },
{ "RTTypeid" },
{ "Throwable" },
{ "Error" },
{ "withSym", "__withSym" },
Expand Down
1 change: 1 addition & 0 deletions src/dmd/mtype.d
Expand Up @@ -473,6 +473,7 @@ extern (C++) abstract class Type : ASTNode
extern (C++) __gshared ClassDeclaration typeinfowild;

extern (C++) __gshared TemplateDeclaration rtinfo;
extern (C++) __gshared TemplateDeclaration rttypeid;

extern (C++) __gshared Type[TMAX] basic;

Expand Down
57 changes: 47 additions & 10 deletions src/dmd/typinf.d
Expand Up @@ -11,15 +11,21 @@

module dmd.typinf;

import dmd.arraytypes;
import dmd.declaration;
import dmd.dmodule;
import dmd.dscope;
import dmd.dclass;
import dmd.dstruct;
import dmd.dsymbolsem;
import dmd.dtemplate;
import dmd.errors;
import dmd.expressionsem;
import dmd.globals;
import dmd.gluelayer;
import dmd.mtype;
import dmd.semantic2;
import dmd.semantic3;
import dmd.visitor;
import core.stdc.stdio;

Expand Down Expand Up @@ -56,16 +62,47 @@ void genTypeInfo(Loc loc, Type torig, Scope* sc)
Type t = torig.merge2(); // do this since not all Type's are merge'd
if (!t.vtinfo)
{
if (t.isShared()) // does both 'shared' and 'shared const'
t.vtinfo = TypeInfoSharedDeclaration.create(t);
else if (t.isConst())
t.vtinfo = TypeInfoConstDeclaration.create(t);
else if (t.isImmutable())
t.vtinfo = TypeInfoInvariantDeclaration.create(t);
else if (t.isWild())
t.vtinfo = TypeInfoWildDeclaration.create(t);
else
t.vtinfo = getTypeInfoDeclaration(t);
if (Type.rttypeid)
{
// Evaluate: RTTypeid!t
auto tiargs = new Objects();
tiargs.push(t);
auto ti = new TemplateInstance(loc, Type.rttypeid, tiargs);

Scope* sc3 = ti.tempdecl._scope.startCTFE();
sc3.tinst = sc.tinst;
sc3.minst = sc.minst;
ti.dsymbolSemantic(sc3);
ti.semantic2(sc3);
ti.semantic3(sc3);
auto e = symbolToExp(ti.toAlias(), Loc.initial, sc3, false);

sc3.endCTFE();

e = e.ctfeInterpret();
t.vtinfo = cast(TypeInfoDeclaration) e.isVarExp();
/* Save this for when RTTypeid is fully operational
if (!t.vtinfo)
{
.error(loc, "`object.RTTypeid!T` did not return a TypeInfo variable");
fatal();
}
*/
}

if (!t.vtinfo)
{
if (t.isShared()) // does both 'shared' and 'shared const'
t.vtinfo = TypeInfoSharedDeclaration.create(t);
else if (t.isConst())
t.vtinfo = TypeInfoConstDeclaration.create(t);
else if (t.isImmutable())
t.vtinfo = TypeInfoInvariantDeclaration.create(t);
else if (t.isWild())
t.vtinfo = TypeInfoWildDeclaration.create(t);
else
t.vtinfo = getTypeInfoDeclaration(t);
}
assert(t.vtinfo);

/* If this has a custom implementation in std/typeinfo, then
Expand Down

0 comments on commit 18b5cd0

Please sign in to comment.