root/trunk/ddl/Demangle.d

Revision 279, 3.6 kB (checked in by pragma, 2 years ago)

Dirty Update Please use r278 for something stable.

Line 
1 /+
2     Copyright (c) 2005-2006 Eric Anderton
3             
4     Permission is hereby granted, free of charge, to any person
5     obtaining a copy of this software and associated documentation
6     files (the "Software"), to deal in the Software without
7     restriction, including without limitation the rights to use,
8     copy, modify, merge, publish, distribute, sublicense, and/or
9     sell copies of the Software, and to permit persons to whom the
10     Software is furnished to do so, subject to the following
11     conditions:
12
13     The above copyright notice and this permission notice shall be
14     included in all copies or substantial portions of the Software.
15
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23     OTHER DEALINGS IN THE SOFTWARE.
24 +/
25 /**
26     Provides support for parsing and decoding D's name mangling syntax.
27     Wraps std.demangle from phobos.
28     
29     Authors: Eric Anderton
30     License: BSD Derivative (see source for details)
31     Copyright: 2005-2006 Eric Anderton
32 */
33 module ddl.Demangle;
34
35 private import etc.demangle;
36
37 debug private import ddl.Utils;
38
39 /**
40     The type of symbol that is represented by a given mangled name.
41     
42     Any ordinary type of symbol that doesn't match a D symbol, or a D special symbol
43     is merely of type 'PublicSymbol'.
44 */
45 enum DemangleType{
46     PublicSymbol,
47     PublicDSymbol,
48     ModuleInfo
49 }
50
51
52 /**
53     Parses a mangled D symbol and returns the equivalent D code to match the symbol.
54     
55     Params:
56         symbol = The mangled D symbol.
57         
58     Returns:
59         A D code representation of the symbol.
60 */
61 public char[] demangleSymbol(char[] symbol){
62     return demangle(symbol);
63 }
64
65 bool startsWith(char[] value,char[] test){
66     return value.length >= test.length && value[0..test.length] == test;
67 }
68
69 bool endsWith(char[] value,char[] test){
70     return value.length >= test.length && value[$-test.length .. $] == test;
71 }
72
73 /**
74     Parses a mangled D symbol and returns its DemangleType.
75     
76     Params:
77         symbol = The mangled D symbol.
78         
79     Returns:
80         The DemangleType for the symbol.
81 */
82 public DemangleType getDemangleType(char[] symbol){
83     if (symbol.endsWith("__ModuleInfoZ")) {
84         return DemangleType.ModuleInfo;
85     }
86     else if(symbol.startsWith("_D")){
87         return DemangleType.PublicDSymbol;
88     }
89     // no particular type, default the symbol to a 'public'
90     return DemangleType.PublicSymbol;
91 }
92
93 /**
94      Decomposes mangled D namespaces into an array of names.
95     
96      The array of namespaces is called a "namespace chain".
97     
98      Params:
99         mangled: (inout) a mangled D namespace.
100     
101      Return: Returns a namespace chain that is equivalent to the mangled input.
102 */
103 public char[][] parseNamespaceChain(inout char[] mangled){
104     char[][] chain;
105     uint ate;
106     uint len;
107
108     while(mangled.length > 0){
109         ate = 0;
110         len = 0;   
111         while(mangled[ate] >= '0' && mangled[ate] <= '9'){
112             len = (len*10) + (mangled[ate] - '0');
113             ate++;
114         }
115         if(ate == 0) break;
116         chain ~= mangled[ate..ate+len];
117         mangled = mangled[ate+len..$];
118     }
119     return chain;
120 }
121
122 /**
123     Combines a namespace chain into a dot ('.') separated namespace.
124     
125     Params:
126         chain: A namespace chain to convert.
127     
128     Return: A dot-sepearated version of the original chain.
129 */
130 public char[] toNamespace(char[][] chain){
131     char[] result = chain[0];
132     for(uint i=1; i<chain.length; i++){
133         result ~= "." ~ chain[i];
134     }
135     return result;
136 }
Note: See TracBrowser for help on using the browser.