1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity very high)
4 module dil.Enums;
5 
6 import common;
7 
8 /// Enumeration of storage classes.
9 enum StorageClass
10 {
11   None         = 0,
12   Abstract     = 1<<0,
13   Auto         = 1<<1,
14   Const        = 1<<2,
15   Deprecated   = 1<<3,
16   Extern       = 1<<4,
17   Final        = 1<<5,
18   Override     = 1<<6,
19   Scope        = 1<<7,
20   Static       = 1<<8,
21   Synchronized = 1<<9,
22   In           = 1<<10,
23   Out          = 1<<11,
24   Ref          = 1<<12,
25   Lazy         = 1<<13,
26   Variadic     = 1<<14,
27   // D2:
28   Immutable    = 1<<15,
29   Manifest     = 1<<16,
30   Nothrow      = 1<<17,
31   Pure         = 1<<18,
32   Shared       = 1<<19,
33   Gshared      = 1<<20,
34   Inout        = 1<<21,
35   // Attributes:
36   Disable      = 1<<22,
37   Property     = 1<<23,
38   Safe         = 1<<24,
39   System       = 1<<25,
40   Trusted      = 1<<26,
41 }
42 
43 /// Enumeration of protection attributes.
44 enum Protection
45 {
46   None,
47   Private/+   = 1+/,
48   Protected/+ = 1<<1+/,
49   Package/+   = 1<<2+/,
50   Public/+    = 1<<3+/,
51   Export/+    = 1<<4+/
52 }
53 
54 /// Enumeration of linkage types.
55 enum LinkageType
56 {
57   None,
58   C,
59   Cpp,
60   D,
61   Windows,
62   Pascal,
63   System
64 }
65 
66 /// Enumeration of variadic argument styles.
67 enum VariadicStyle
68 {
69   None, /// Not variadic.
70   C,    /// E.g.: void func(int x, ...)
71   D,    /// E.g.: void func(int[] xs...)
72 }
73 
74 /// Short aliases.
75 alias STC = StorageClass;
76 alias PROT = Protection; /// ditto
77 alias LINK = LinkageType; /// ditto
78 alias VARD = VariadicStyle; /// ditto
79 
80 /// Namespace for functions that return the string of an enum.
81 struct EnumString
82 {
83 static:
84 
85   /// List of Protection strings.
86   string[] prots =
87     ["","private","protected","package","public","export"];
88 
89   /// Returns the string for prot.
90   string opCall(Protection prot)
91   {
92     return prots[prot];
93   }
94 
95   /// List of StorageClass strings.
96   string[] stcs = [
97     "",
98     "abstract",
99     "auto",
100     "const",
101     "deprecated",
102     "extern",
103     "final",
104     "override",
105     "scope",
106     "static",
107     "synchronized",
108     "in",
109     "out",
110     "ref",
111     "lazy",
112     "variadic",
113     "immutable",
114     "manifest",
115     "nothrow",
116     "pure",
117     "shared",
118     "gshared",
119     "inout",
120     "@disable",
121     "@property",
122     "@safe",
123     "@system",
124     "@trusted",
125   ];
126 
127 
128   /// Returns the string of a storage class. Only one bit may be set.
129   string opCall(StorageClass stc)
130   {
131     import core.bitop : bsf;
132     size_t index = stc ? bsf(stc)+1 : 0;
133     assert(index < stcs.length);
134     return stcs[index];
135   }
136 
137   /// Returns the strings for stcs. Any number of bits may be set.
138   string[] all(StorageClass stcs)
139   {
140     string[] result;
141     for (auto i = StorageClass.max; i; i >>= 1)
142       if (stcs & i)
143         result ~= EnumString(i);
144     return result;
145   }
146 
147   /// List of LinkageType strings.
148   string[] ltypes = ["","C","C++","D","Windows","Pascal","System"];
149 
150   /// Returns the string for ltype.
151   string opCall(LinkageType ltype)
152   {
153     return ltypes[ltype];
154   }
155 }