Changeset 409

Show
Ignore:
Timestamp:
11/02/10 18:45:48 (2 years ago)
Author:
sean
Message:

http://d.puremagic.com/issues/show_bug.cgi?id=4852

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/core/demangle.d

    r386 r409  
    2222 
    2323 
    24 /** 
    25  * Demangles D mangled names.  If it is not a D mangled name, it returns its 
    26  * argument name. 
    27  * 
    28  * Params: 
    29  *  buf = The string to demangle. 
    30  *  dst = An optional destination buffer. 
    31  * 
    32  * Returns: 
    33  *  The demangled name or the original string if the name is not a mangled D 
    34  *  name. 
    35  */ 
    36 char[] demangle( const(char)[] buf, char[] dst = null ) 
     24private struct Demangle 
    3725{ 
    3826    // NOTE: This implementation currently only works with mangled function 
     
    5240    //       allocation during the course of a parsing run, this is still 
    5341    //       faster than assembling the result piecemeal. 
     42     
     43     
     44    enum AddType { yes, no } 
     45     
     46     
     47    this( const(char)[] buf_, char[] dst_ = null ) 
     48    { 
     49        this( buf_, AddType.no, dst_ ); 
     50    } 
     51     
     52     
     53    this( const(char)[] buf_, AddType addType_, char[] dst_ = null ) 
     54    { 
     55        buf     = buf_; 
     56        addType = addType_; 
     57        dst     = dst_; 
     58    } 
     59 
    5460 
    5561    enum minBufSize = 4000; 
    5662 
    57     size_t  pos = 0; 
    58     size_t  len = 0; 
     63 
     64    const(char)[]   buf     = null; 
     65    char[]          dst     = null; 
     66    size_t          pos     = 0; 
     67    size_t          len     = 0; 
     68    AddType         addType = AddType.no; 
     69     
    5970     
    6071    static class ParseException : Exception 
     
    6677    } 
    6778     
     79     
    6880    static class OverflowException : Exception 
    6981    { 
     
    7385        } 
    7486    } 
     87     
    7588     
    7689    static void error( string msg = "Invalid symbol" ) 
     
    8295    } 
    8396     
     97     
    8498    static void overflow( string msg = "Buffer overflow" ) 
    8599    { 
     
    89103    } 
    90104     
     105     
    91106    ////////////////////////////////////////////////////////////////////////// 
    92107    // Type Testing and Conversion 
    93108    ////////////////////////////////////////////////////////////////////////// 
    94109     
     110     
     111    static bool isAlpha( char val ) 
     112    { 
     113        return ('a' <= val && 'z' >= val) || 
     114               ('A' <= val && 'Z' >= val); 
     115    } 
     116     
     117     
    95118    static bool isDigit( char val ) 
    96119    { 
    97120        return '0' <= val && '9' >= val; 
    98121    } 
     122     
    99123     
    100124    static bool isHexDigit( char val ) 
     
    105129    } 
    106130     
    107     static bool isAlpha( char val ) 
    108     { 
    109         return ('a' <= val && 'z' >= val) || 
    110                ('A' <= val && 'Z' >= val); 
    111     } 
    112131     
    113132    static ubyte ascii2hex( char val ) 
     
    127146    } 
    128147     
     148     
    129149    ////////////////////////////////////////////////////////////////////////// 
    130150    // Data Output 
    131151    ////////////////////////////////////////////////////////////////////////// 
    132152     
     153     
    133154    static bool contains( const(char)[] a, const(char)[] b ) 
    134155    { 
     
    137158               b.ptr + b.length <= a.ptr + a.length; 
    138159    } 
     160     
    139161     
    140162    char[] shift( const(char)[] val ) 
     
    163185        return null; 
    164186    } 
     187     
    165188     
    166189    char[] append( const(char)[] val ) 
     
    185208    } 
    186209 
     210 
    187211    char[] put( const(char)[] val ) 
    188212    { 
     
    196220    } 
    197221     
     222     
    198223    void pad( const(char)[] val ) 
    199224    { 
     
    204229        } 
    205230    } 
     231 
    206232 
    207233    void silent( lazy void dg ) 
     
    212238    } 
    213239     
     240     
    214241    ////////////////////////////////////////////////////////////////////////// 
    215242    // Parsing Utility 
    216243    ////////////////////////////////////////////////////////////////////////// 
    217244 
     245 
    218246    char tok() 
    219247    { 
     
    223251    } 
    224252     
     253     
    225254    void test( char val ) 
    226255    { 
     
    229258    } 
    230259     
     260     
    231261    void next() 
    232262    { 
     
    235265    } 
    236266     
     267     
    237268    void match( char val ) 
    238269    { 
     
    241272    } 
    242273     
    243     void matchS( const(char)[] val ) 
     274     
     275    void match( const(char)[] val ) 
    244276    { 
    245277        foreach( e; val ) 
     
    250282    } 
    251283     
     284     
    252285    void eat( char val ) 
    253286    { 
     
    255288            next(); 
    256289    } 
     290     
    257291     
    258292    ////////////////////////////////////////////////////////////////////////// 
    259293    // Parsing Implementation 
    260294    ////////////////////////////////////////////////////////////////////////// 
     295 
    261296 
    262297    /* 
     
    285320    } 
    286321 
     322 
    287323    size_t decodeNumber() 
    288324    { 
     
    302338        return val; 
    303339    } 
     340 
    304341 
    305342    void parseReal() 
     
    354391    } 
    355392     
     393     
    356394    /* 
    357395    LName: 
     
    394432        pos += n; 
    395433    } 
     434     
    396435     
    397436    /* 
     
    851890        case 'T': // TypeTypedef (T LName) 
    852891            next(); 
    853             parseLName(); 
     892            parseQualifiedName(); 
    854893            return dst[beg .. len]; 
    855894        case 'D': // TypeDelegate (D TypeFunction) 
     
    9781017        } 
    9791018    } 
     1019     
    9801020     
    9811021    /* 
     
    10621102            } 
    10631103            put( "\"" ); 
    1064             if( 'a' != tok()
     1104            if( 'a' != t
    10651105                put( (cast(char*) &t)[0 .. 1] ); 
    10661106            return; 
     
    10731113        } 
    10741114    } 
     1115     
    10751116     
    10761117    /* 
     
    11071148                next(); 
    11081149                if( n ) put( ", " ); 
    1109                 parseLName(); 
     1150                parseQualifiedName(); 
    11101151                continue; 
    11111152            default: 
     
    11141155        } 
    11151156    } 
     1157     
    11161158     
    11171159    /* 
     
    11281170        auto n = decodeNumber(); 
    11291171        auto beg = pos; 
    1130         matchS( "__T" ); 
     1172        match( "__T" ); 
    11311173        parseLName(); 
    11321174        put( "!(" ); 
     
    11381180    } 
    11391181     
     1182     
    11401183    bool mayBeTemplateInstanceName() 
    11411184    { 
     
    11511194               pos < buf.length && 'T' == buf[pos++]; 
    11521195    } 
     1196     
    11531197     
    11541198    /* 
     
    11901234    } 
    11911235     
     1236     
    11921237    /* 
    11931238    QualifiedName: 
     
    12111256    } 
    12121257 
     1258 
    12131259    /* 
    12141260    MangledName: 
     
    12281274        if( 'M' == tok() ) 
    12291275            next(); // has 'this' pointer 
    1230         parseType( name ); 
    1231     } 
    1232      
    1233     while( true ) 
    1234     { 
    1235         try 
    1236         { 
    1237             debug(info) printf( "demangle(%.*s)\n", cast(int) buf.length, buf.ptr ); 
    1238             parseMangledName(); 
    1239             return dst[0 .. len]; 
    1240         } 
    1241         catch( OverflowException e ) 
    1242         { 
    1243             debug(trace) printf( "overflow... restarting\n" ); 
    1244             auto a = minBufSize; 
    1245             auto b = 2 * dst.length; 
    1246             auto newsz = a < b ? b : a; 
    1247             debug(info) printf( "growing dst to %lu bytes\n", newsz ); 
    1248             dst.length = newsz; 
    1249             pos = len = 0; 
    1250             continue; 
    1251         } 
    1252         catch( ParseException e ) 
    1253         { 
    1254             debug 
     1276        if( addType ) 
     1277            parseType( name ); 
     1278    } 
     1279 
     1280 
     1281    char[] opCall() 
     1282    { 
     1283        while( true ) 
     1284        { 
     1285            try 
    12551286            { 
    1256                 auto msg = e.toString; 
    1257                 printf( "error: %.*s\n", cast(int) msg.length, msg.ptr ); 
     1287                debug(info) printf( "demangle(%.*s)\n", cast(int) buf.length, buf.ptr ); 
     1288                parseMangledName(); 
     1289                return dst[0 .. len]; 
    12581290            } 
    1259             if( dst.length < buf.length ) 
    1260                 dst.length = buf.length; 
    1261             dst[0 .. buf.length] = buf[]; 
    1262             return dst[0 .. buf.length]; 
    1263         } 
    1264     } 
     1291            catch( OverflowException e ) 
     1292            { 
     1293                debug(trace) printf( "overflow... restarting\n" ); 
     1294                auto a = minBufSize; 
     1295                auto b = 2 * dst.length; 
     1296                auto newsz = a < b ? b : a; 
     1297                debug(info) printf( "growing dst to %lu bytes\n", newsz ); 
     1298                dst.length = newsz; 
     1299                pos = len = 0; 
     1300                continue; 
     1301            } 
     1302            catch( ParseException e ) 
     1303            { 
     1304                debug(info) 
     1305                { 
     1306                    auto msg = e.toString; 
     1307                    printf( "error: %.*s\n", cast(int) msg.length, msg.ptr ); 
     1308                } 
     1309                if( dst.length < buf.length ) 
     1310                    dst.length = buf.length; 
     1311                dst[0 .. buf.length] = buf[]; 
     1312                return dst[0 .. buf.length]; 
     1313            } 
     1314        } 
     1315    } 
     1316
     1317 
     1318 
     1319/** 
     1320 * Demangles D mangled names.  If it is not a D mangled name, it returns its 
     1321 * argument name. 
     1322 * 
     1323 * Params: 
     1324 *  buf = The string to demangle. 
     1325 *  dst = An optional destination buffer. 
     1326 * 
     1327 * Returns: 
     1328 *  The demangled name or the original string if the name is not a mangled D 
     1329 *  name. 
     1330 */ 
     1331char[] demangle( const(char)[] buf, char[] dst = null ) 
     1332
     1333    //return Demangle(buf, dst)(); 
     1334    auto d = Demangle(buf, dst); 
     1335    return d(); 
    12651336} 
    12661337 
     
    12771348        [ "_D6object6Object8opEqualsFC6ObjectZi", "int object.Object.opEquals(class Object)" ], 
    12781349        [ "_D4test2dgDFiYd", "double delegate(int, ...) test.dg" ], 
    1279         //[ "_D4test58__T9factorialVde67666666666666860140VG5aa5_68656c6c6fVPvnZ9factorialf", "float test.factorial!(double 4.2, char[5] \"hello\"c, void* null).factorial" ], 
    1280         //[ "_D4test101__T9factorialVde67666666666666860140Vrc9a999999999999d9014000000000000000c00040VG5aa5_68656c6c6fVPvnZ9factorialf", "float test.factorial!(double 4.2, cdouble 6.8+3i, char[5] \"hello\"c, void* null).factorial" ], 
     1350        [ "_D4test58__T9factorialVde67666666666666860140VG5aa5_68656c6c6fVPvnZ9factorialf", "float test.factorial!(double 4.2, char[5] \"hello\"c, void* null).factorial" ], 
     1351        [ "_D4test101__T9factorialVde67666666666666860140Vrc9a999999999999d9014000000000000000c00040VG5aa5_68656c6c6fVPvnZ9factorialf", "float test.factorial!(double 4.2, cdouble 6.8+3i, char[5] \"hello\"c, void* null).factorial" ], 
    12811352        [ "_D4test34__T3barVG3uw3_616263VG3wd3_646566Z1xi", "int test.bar!(wchar[3] \"abc\"w, dchar[3] \"def\"d).x" ], 
    12821353        [ "_D8demangle4testFLC6ObjectLDFLiZiZi", "int demangle.test(lazy class Object, lazy int delegate(lazy int))"], 
     
    12991370    } 
    13001371} 
     1372 
     1373 
     1374/* 
     1375 * 
     1376 */ 
     1377string decodeDmdString( char[] ln, ref int p ) 
     1378{ 
     1379    string s; 
     1380    uint zlen, zpos; 
     1381 
     1382    // decompress symbol 
     1383    while( p < ln.length ) 
     1384    { 
     1385        int ch = cast(ubyte) ln[p++]; 
     1386        if( (ch & 0xc0) == 0xc0 ) 
     1387        { 
     1388            zlen = (ch & 0x7) + 1; 
     1389            zpos = ((ch >> 3) & 7) + 1; // + zlen; 
     1390            if( zpos > s.length ) 
     1391                break; 
     1392            s ~= s[$ - zpos .. $ - zpos + zlen]; 
     1393        } 
     1394        else if( ch >= 0x80 ) 
     1395        { 
     1396            if( p >= ln.length ) 
     1397                break; 
     1398            int ch2 = cast(ubyte) ln[p++]; 
     1399            zlen = (ch2 & 0x7f) | ((ch & 0x38) << 4); 
     1400            if( p >= ln.length ) 
     1401                break; 
     1402            int ch3 = cast(ubyte) ln[p++]; 
     1403            zpos = (ch3 & 0x7f) | ((ch & 7) << 7); 
     1404            if( zpos > s.length ) 
     1405                break; 
     1406            s ~= s[$ - zpos .. $ - zpos + zlen]; 
     1407        } 
     1408        else if( Demangle.isAlpha(cast(char)ch) || Demangle.isDigit(cast(char)ch) || ch == '_' ) 
     1409            s ~= cast(char) ch; 
     1410        else 
     1411        { 
     1412            p--; 
     1413            break; 
     1414        } 
     1415    } 
     1416    return s; 
     1417}