Good Evening everyone.
Today I have made progress with reflecting over template instances.
TLDR. core.reflect can almost replace __traits
now and preliminary tests show that the reflection data-creation scales linearly.
Meaning 10 times as much code takes 10 times as much time to reflect.
The following code:
import core.reflect.reflect;
template TypeDef(string name, T)
{
struct TypeDef
{
T value;
alias value this;
}
}
alias myInt = TypeDef!("myInt", int);
pragma(msg, () {
auto n = nodeFromName("myInt", ReflectFlags.Everything);
string[immutable(ulong)] cmap;
return nodeToString(n, &cmap);
} ());
AliasDeclaration = {
internalPointer: unhandled,
serial: 599,
astTypeName: "AliasDeclaration",
name: "myInt",
attributes: [],
linkage: Default,
comment: "",
mangledName: "",
location: Location = {
filename: "testTemplateReflection.d",
line: 12,
column: 1,
},
parent: Module = {
internalPointer: unhandled,
serial: 580,
astTypeName: "Module",
_scope: Scope = {
internalPointer: unhandled,
serial: 0,
astTypeName: "Scope",
},
identifier: "testTemplateReflection",
parent: null,
members: [
DeclarationRef to 'myInt',
],
packages: [],
source_filename: "testTemplateReflection.d",
string_import_filenames: [],
declDefs: [],
},
storageClasses: [],
type: TypeStruct = {
internalPointer: unhandled,
serial: 31545,
astTypeName: "TypeStruct",
kind: "struct",
alignSize: 4,
size: 4,
identifier: "TypeDef!("myInt", int)",
_unqualified: null,
sym: StructDeclaration = {
internalPointer: unhandled,
serial: 31544,
astTypeName: "StructDeclaration",
name: "TypeDef",
attributes: [],
linkage: D,
comment: "",
mangledName: "22testTemplateReflection__T7TypeDefVAyaa5_6d79496e74TiZQBc",
location: Location = {
filename: "testTemplateReflection.d",
line: 5,
column: 5,
},
parent: TemplateInstance = {
internalPointer: unhandled,
serial: 597,
astTypeName: "TemplateInstance",
_scope: null,
identifier: "",
parent: Module = {
internalPointer: unhandled,
serial: 580,
astTypeName: "Module",
_scope: Scope = {
internalPointer: unhandled,
serial: 0,
astTypeName: "Scope",
},
identifier: "testTemplateReflection",
parent: null,
members: [
DeclarationRef to 'myInt',
],
packages: [],
source_filename: "testTemplateReflection.d",
string_import_filenames: [],
declDefs: [],
},
members: [
DeclarationRef to 'TypeDef',
],
name: "TypeDef",
decl: TemplateDeclaration = {
internalPointer: unhandled,
serial: 594,
astTypeName: "TemplateDeclaration",
name: "TypeDef",
attributes: [],
linkage: Default,
comment: "",
mangledName: "22testTemplateReflection7TypeDef",
location: Location = {
filename: "testTemplateReflection.d",
line: 3,
column: 1,
},
parent: Module = {
internalPointer: unhandled,
serial: 580,
astTypeName: "Module",
_scope: Scope = {
internalPointer: unhandled,
serial: 0,
astTypeName: "Scope",
},
identifier: "testTemplateReflection",
parent: null,
members: [
DeclarationRef to 'myInt',
],
packages: [],
source_filename: "testTemplateReflection.d",
string_import_filenames: [],
declDefs: [],
},
storageClasses: [],
isEponymous: true,
constraint: null,
},
},
storageClasses: [],
type: TypeRef to 'TypeDef!("myInt", int)',
fields: [
VariableDeclaration = {
internalPointer: unhandled,
serial: 31547,
astTypeName: "VarDeclaration",
name: "value",
attributes: [],
linkage: D,
comment: "",
mangledName: "_D22testTemplateReflection__T7TypeDefVAyaa5_6d79496e74TiZQBc5valuei",
location: Location = {
filename: "testTemplateReflection.d",
line: 7,
column: 11,
},
parent: DeclarationRef to 'TypeDef',
storageClasses: [
Field,
Ctorinit,
],
type: TypeBasic = {
internalPointer: unhandled,
serial: 138,
astTypeName: "TypeBasic",
kind: "int",
alignSize: 4,
size: 4,
identifier: "int",
_unqualified: null,
},
_init: null,
offset: unhandled,
},
],
members: [
DeclarationRef to 'value',
],
},
},
decl: null,
}
As you can see it is possible to look through the type alias myInt Into the template-instance TypeDef!("myInt", int) and through there into the eponymous struct Declaration.
I am aware that the produced tree is fairly big however I've thought that a more faithful representation of the Tree inside the compiler would be useful to more people than a cut down version which is tailored to specific use-cases that I know of.
As always I would be very happy about positive feedback or constructive criticism.
Have a very nice day/night/evening/morning.
Cheers,
Stefan