1 /*******************************************************************************
2 3 Utilities to get string representations of symbols.
4 5 NB: because this module is often used as purely compile-time dependency it
6 used built-in asserts instead of `ocean.core.Test` to reduce amount of
7 cyclic imports. `ocean.meta` modules in general are not supposed to
8 import anything outside of `ocean.meta`.
9 10 Copyright:
11 Copyright (c) 2017 dunnhumby Germany GmbH. All rights reserved.
12 13 License:
14 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
15 Alternatively, this file may be distributed under the terms of the Tango
16 3-Clause BSD License (see LICENSE_BSD.txt for details).
17 18 *******************************************************************************/19 20 moduleocean.meta.codegen.Identifier;
21 22 importocean.meta.AliasSeq;
23 importocean.meta.codegen.CTFE;
24 importocean.meta.types.Qualifiers;
25 importocean.meta.types.Function;
26 27 version (unittest)
28 {
29 importocean.meta.types.Typedef;
30 }
31 32 /*******************************************************************************
33 34 Returns "name" (identifier) of a given symbol as string
35 36 When used with aggregate type field, will return only name of field itself,
37 without any qualification.
38 39 Params:
40 Sym = any symbol alias
41 42 *******************************************************************************/43 44 publictemplateidentifier ( aliasSym )
45 {
46 enumidentifier = __traits(identifier, Sym);
47 }
48 49 ///50 unittest51 {
52 classClassName { intfieldName; }
53 54 staticassert (identifier!(ClassName) == "ClassName");
55 assert (identifier!(ClassName) == "ClassName");
56 57 staticassert (identifier!(ClassName.fieldName) == "fieldName");
58 assert (identifier!(ClassName.fieldName) == "fieldName");
59 60 voidfuncName ( ) { }
61 62 staticassert (identifier!(funcName) == "funcName");
63 assert (identifier!(funcName) == "funcName");
64 65 extern(C) staticvoidfuncNameArgs ( inta, doubleb ) { }
66 67 staticassert (identifier!(funcNameArgs) == "funcNameArgs");
68 assert (identifier!(funcNameArgs) == "funcNameArgs");
69 }
70 71 /*******************************************************************************
72 73 Template to get the name of the ith member of a struct / class.
74 75 Used over plain `identifier` when iterating over aggregate fields with
76 `.tupleof` as compiler refuses to pass such field as template alias
77 parameter.
78 79 Params:
80 i = index of member to get
81 T = type of compound to get member name from
82 83 Returns:
84 name of the ith member
85 86 *******************************************************************************/87 88 publictemplatefieldIdentifier ( T, size_ti )
89 {
90 enumfieldIdentifier = identifier!(T.tupleof[i]);
91 }
92 93 unittest94 {
95 staticstructTestStruct96 {
97 inta;
98 doubleb;
99 }
100 101 staticassert (fieldIdentifier!(TestStruct, 0) == "a");
102 staticassert (fieldIdentifier!(TestStruct, 1) == "b");
103 104 assert (fieldIdentifier!(TestStruct, 0) == "a");
105 assert (fieldIdentifier!(TestStruct, 1) == "b");
106 107 structFoo { AliasSeq!(int, double, Object) fields; }
108 staticassert (fieldIdentifier!(Foo, 0) == "__fields_field_0");
109 }