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 16, 2022
1 parent a0faedf commit 71ab36b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dmd/dsymbolsem.d
Expand Up @@ -2533,6 +2533,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 compiler/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 compiler/src/dmd/mtype.d
Expand Up @@ -422,6 +422,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 compiler/src/dmd/typinf.d
Expand Up @@ -12,16 +12,22 @@
module dmd.typinf;

import dmd.astenums;
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.expression;
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 @@ -62,16 +68,47 @@ extern (C++) void genTypeInfo(Expression e, const ref Loc loc, Type torig, Scope
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 ex = symbolToExp(ti.toAlias(), Loc.initial, sc3, false);

sc3.endCTFE();

ex = ex.ctfeInterpret();
t.vtinfo = cast(TypeInfoDeclaration) ex.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);

// ClassInfos are generated as part of ClassDeclaration codegen
Expand Down

0 comments on commit 71ab36b

Please sign in to comment.