
D front-end changes: - Import dmd v2.103.0-beta.1. - Using `alias this' for classes has been deprecated. - The feature `-fpreview=dip25` is now enabled by default. - The compile-time traits `isVirtualFunction' and `getVirtualFunctions' have been deprecated. D runtime changes: - Import druntime v2.103.0-beta.1. Phobos changes: - Import phobos v2.103.0-beta.1. - Updated unicode grapheme walking updated to conform to Unicode version 15. - Improved friendliness of error messages when instantiating `std.algorithm.iteration.joiner' and `std.algorithm.sorting.sort' with wrong inputs. gcc/d/ChangeLog: * dmd/MERGE: Merge upstream dmd 4ca4140e58. * dmd/VERSION: Bump version to v2.103.0-beta.1. * Make-lang.in (D_FRONTEND_OBJS): Add d/errorsink.o. * d-ctfloat.cc (CTFloat::sprint): Update signature for new front-end interface. * d-frontend.cc (getTypeInfoType): Likewise. * d-lang.cc (d_handle_option): Remove handling of -fpreview=dip25 and -frevert=dip25. (d_post_options): Remove enabling of sealed references language feature when scoped pointers is enabled. * d-tree.h (create_typeinfo): Update signature. * decl.cc (DeclVisitor::finish_vtable): Update for new front-end interface. (DeclVisitor::visit (VarDeclaration *)): Likewise. (DeclVisitor::visit (FuncDeclaration *)): Check skipCodegen to see if front-end explicitly requested not to generate code. * expr.cc (ExprVisitor::visit (NewExp *)): Update for new front-end interface. * lang.opt (fpreview=dip25): Remove. (frevert=dip25): Remove. * modules.cc (layout_moduleinfo_fields): Update for new front-end interface. (layout_moduleinfo): Likewise. * runtime.def (NEWCLASS): Remove. * toir.cc (IRVisitor::visit (IfStatement *)): Don't generate IR for if statement list when condition is `__ctfe'. * typeinfo.cc (create_typeinfo): Add generate parameter. * types.cc (layout_aggregate_members): Update for new front-end interface. libphobos/ChangeLog: * libdruntime/MERGE: Merge upstream druntime 4ca4140e58. * libdruntime/Makefile.am (DRUNTIME_DSOURCES): Add core/factory.d. * libdruntime/Makefile.in: Regenerate. * src/MERGE: Merge upstream phobos 454dff14d. * testsuite/libphobos.hash/test_hash.d: Update test. * testsuite/libphobos.shared/finalize.d: Update test. * libdruntime/core/factory.d: New file. gcc/testsuite/ChangeLog: * gdc.dg/torture/simd23084.d: New test. * gdc.dg/torture/simd23085.d: New test. * gdc.dg/torture/simd23218.d: New test.
68 lines
1.7 KiB
D
68 lines
1.7 KiB
D
/* Create classes from their modules and names.
|
|
*
|
|
* Copyright: Copyright (C) D Language Foundation 2023
|
|
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
|
|
* Authors: Walter Bright, Steven Schveighoffer
|
|
* Source: $(DRUNTIMESRC core/_factory.d)
|
|
*/
|
|
|
|
module core.factory;
|
|
|
|
/**
|
|
* Create instance of class specified by the module symbol and a string
|
|
* representing the name of the class.
|
|
* The class must either have no constructors or have
|
|
* a default constructor.
|
|
* Params:
|
|
* mod = symbol representing the module that the class is in
|
|
* classname = string representing the name of the class
|
|
* Returns:
|
|
* null if failed
|
|
* Example:
|
|
* ---
|
|
* module foo.bar;
|
|
*
|
|
* class C
|
|
* {
|
|
* this() { x = 10; }
|
|
* int x;
|
|
* }
|
|
*
|
|
* void main()
|
|
* {
|
|
* auto c = cast(C)factory!(foo.bar)("C");
|
|
* assert(c !is null && c.x == 10);
|
|
* }
|
|
* ---
|
|
*/
|
|
Object factory(alias mod)(string classname)
|
|
{
|
|
foreach(cl; _getModuleClasses!mod)
|
|
{
|
|
if (cl.stringof == classname)
|
|
return cl.classinfo.create();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@system unittest
|
|
{
|
|
Object valid_obj = factory!object("Object");
|
|
Object invalid_obj = factory!object("__this_class_doesnt_exist__");
|
|
|
|
assert(valid_obj !is null);
|
|
assert(invalid_obj is null);
|
|
}
|
|
|
|
/**************************************
|
|
* Retrieve as a tuple all the types of the top level classes in the module mod.
|
|
*/
|
|
private template _getModuleClasses(alias mod) {
|
|
alias result = _AliasSeq!();
|
|
static foreach(m; __traits(allMembers, mod))
|
|
static if(is(__traits(getMember, mod, m) == class))
|
|
result = _AliasSeq!(result, __traits(getMember, mod, m));
|
|
alias _getModuleClasses = result;
|
|
}
|
|
|
|
private template _AliasSeq(TList...) { alias _AliasSeq = TList; }
|