home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / smc / hash.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  11.1 KB  |  435 lines

  1. /*****************************************************************************/
  2. #ifndef _HASH_H_
  3. #define _HASH_H_
  4. /*****************************************************************************/
  5. #ifndef _ALLOC_H_
  6. #include "alloc.h"
  7. #endif
  8. /*****************************************************************************/
  9. #ifndef _TOKENS_H_
  10. #include "tokens.h"
  11. #endif
  12. /*****************************************************************************/
  13. #ifndef _TREEOPS_H_
  14. #include "treeops.h"
  15. #endif
  16. /*****************************************************************************/
  17.  
  18. const   unsigned    HASH_TABLE_SIZE =  256;     // recommended hash table size
  19.  
  20. /*****************************************************************************/
  21.  
  22. DEFMGMT class IdentRec
  23. {
  24. public:
  25.  
  26.     Ident           idNext;         // next identifier in this hash bucket
  27.  
  28.     SymDef          idSymDef;       // list of definitions, if any
  29.  
  30.     unsigned        idHash;         // hash value
  31.  
  32.     unsigned        idOwner;        // index of owning symbol table
  33.  
  34.     unsigned char   idToken;        // token# if the identifier is a keyword
  35.     unsigned char   idFlags;        // see IDF_XXXX below
  36.  
  37.     stringBuff      idSpelling() { assert(this); return idName; }
  38.     unsigned        idSpellLen() { assert(this); return idNlen; }
  39.  
  40.     unsigned short  idNlen;         // length of the identifier's name
  41.  
  42. #if MGDDATA
  43.     char         [] idName;         // the spelling follows
  44. #else
  45.     char            idName[];       // the spelling follows
  46. #endif
  47.  
  48. };
  49.  
  50. enum   IdentFlags
  51. {
  52.     IDF_WIDE_CHARS = 0x01,          // the identifier contains a non-ASCII char
  53.     IDF_MACRO      = 0x02,          // the identifier is defined as a macro?
  54.     IDF_HIDDEN     = 0x04,          // the identifier was invented by compiler
  55.     IDF_PREDEF     = 0x08,          // the identifier has some pre-defined meaning
  56.     IDF_STDVTP     = 0x10,          // the identifier denotes a std value type
  57.  
  58.     IDF_USED       = 0x80           // identifier referenced by source code
  59. };
  60.  
  61. /*****************************************************************************/
  62.  
  63. DEFMGMT class IdentListRec
  64. {
  65. public:
  66.  
  67.     IdentList       nlNext;
  68.     Ident           nlName;
  69. };
  70.  
  71. /*****************************************************************************/
  72.  
  73. struct  kwdDsc
  74. {
  75.     unsigned char   kdOper1;
  76.     unsigned char   kdOper2;
  77.     unsigned char   kdOper1prec;
  78.     unsigned char   kdOper2prec;
  79.  
  80.     unsigned        kdValue     :8;
  81.     unsigned        kdModifier  :8;
  82.     unsigned        kdAttribs   :8;
  83. };
  84.  
  85. /*****************************************************************************/
  86.  
  87. DEFMGMT class hashTab
  88. {
  89. public:
  90.  
  91.     /*************************************************************************/
  92.  
  93.     bool            hashInit(Compiler         comp,
  94.                              unsigned         count,
  95.                              unsigned         owner,
  96.                              norls_allocator *alloc);
  97.  
  98.     void            hashDone();
  99.     void            hashFree();
  100.  
  101.     /*************************************************************************/
  102.  
  103. private:
  104.  
  105.     norls_allocator hashMemAllocPriv;
  106.     norls_allocator*hashMemAlloc;
  107.     bool            hashMemAllocInit(Compiler comp, norls_allocator*alloc);
  108.  
  109.     void            hashMemAllocDone();
  110.     void            hashMemAllocFree();
  111.  
  112.     /*************************************************************************/
  113.  
  114.     Ident           tokenToIdTab[tkCount];
  115.  
  116. public:
  117.  
  118.     Ident           tokenToIdent(tokens tok)
  119.     {
  120.         assert(tok < arraylen(tokenToIdTab));
  121.  
  122.         return  tokenToIdTab[tok];
  123.     }
  124.  
  125.     /*************************************************************************/
  126.     /* The following members provide miscellaneous operations on identifiers */
  127.     /*************************************************************************/
  128.  
  129.     static
  130.     stringBuff      identSpelling(Ident id)
  131.     {
  132.         assert(id); return  id->idName;
  133.     }
  134.  
  135.     static
  136.     size_t          identSpellLen(Ident id)
  137.     {
  138.         assert(id); return  id->idNlen;
  139.     }
  140.  
  141.     static
  142.     tokens          tokenOfIdent (Ident id)
  143.     {
  144.         assert(id); return  (tokens)id->idToken;
  145.     }
  146.  
  147.     static
  148.     void            identSetTok  (Ident id, unsigned tokNum)
  149.     {
  150.         assert(id); id->idToken = tokNum; assert(id->idToken == tokNum);
  151.     }
  152.  
  153.     static
  154.     unsigned char   getIdentFlags (Ident id)
  155.     {
  156.         assert(id); return  id->idFlags;
  157.     }
  158.  
  159.     static
  160.     void            setIdentFlags (Ident id, unsigned char fl)
  161.     {
  162.         assert(id);         id->idFlags |= fl;
  163.     }
  164.  
  165.     static
  166.     unsigned        identHashVal (Ident id)
  167.     {
  168.         assert(id); return  id->idHash;
  169.     }
  170.  
  171.     static
  172.     SymDef          getIdentSymDef(Ident id)
  173.     {
  174.         assert(id); return  id->idSymDef;
  175.     }
  176.  
  177.     static
  178.     void            setIdentSymDef(Ident id, SymDef sym)
  179.     {
  180.         assert(id);         id->idSymDef = sym;
  181.     }
  182.  
  183.     static
  184.     void            hashMarkHidden(Ident id)
  185.     {
  186.         assert(id); id->idFlags |= IDF_HIDDEN;
  187.     }
  188.  
  189.     static
  190.     bool            hashIsIdHidden(Ident id)
  191.     {
  192.         assert(id); return  (id->idFlags & IDF_HIDDEN) != 0;
  193.     }
  194.  
  195.     /* The following is used by non-scanner hashes to store a 32-bit cookie */
  196.  
  197. #if 0
  198.  
  199.     static
  200.     unsigned        getIdentValue(Ident id)
  201.     {
  202.         assert(id); return  id->idOwner;
  203.     }
  204.  
  205.     static
  206.     void            setIdentValue(Ident id, unsigned val)
  207.     {
  208.         assert(id);         id->idOwner = val;
  209.     }
  210.  
  211. #endif
  212.  
  213.     /*************************************************************************/
  214.     /* The following members are related to the keyword descriptor tables    */
  215.     /*************************************************************************/
  216.  
  217. private:
  218.  
  219.     static
  220.     const   kwdDsc  hashKwdDescs[tkKwdCount];
  221.     static
  222.     const    char * hashKwdNames[tkKwdCount];
  223.     static
  224.     unsigned char   hashKwdNlens[tkKwdCount];
  225.     static
  226.     const    char * hashKwdNtab [tkKwdCount];
  227.  
  228.     static
  229.     const   kwdDsc* tokenDesc(tokens tok)
  230.     {
  231.         assert(tok <= tkKwdLast);
  232.         return  hashKwdDescs + tok;
  233.     }
  234.  
  235. public:
  236.  
  237.     const   char *  tokenName   (tokens  tok);
  238.     const   size_t  tokenNlen   (tokens  tok);
  239.  
  240.     bool            tokenIsBinop(tokens  tok, unsigned * precPtr,
  241.                                               treeOps  * operPtr);
  242.     bool            tokenIsUnop (tokens  tok, unsigned * precPtr,
  243.                                               treeOps  * operPtr);
  244.  
  245.     static
  246.     bool            tokenIsType (tokens  tok);
  247.     static
  248.     bool            tokenBegsTyp(tokens  tok);
  249.     static
  250.     bool            tokenOvlOper(tokens  tok);
  251.     static
  252.     unsigned        tokenIsMod  (tokens  tok);
  253.  
  254.     /*************************************************************************/
  255.  
  256. private:
  257.  
  258.     Ident      *    hashTable;
  259.     unsigned        hashCount;
  260.     unsigned        hashMask;
  261.     unsigned        hashIdCnt;
  262.     unsigned        hashOwner;
  263.  
  264. public:
  265.  
  266.     Ident      *    hashGetAddr() { return hashTable; }
  267.     unsigned        hashGetSize() { return hashCount; }
  268.  
  269.     Ident           hashNoName;
  270.  
  271.     /*************************************************************************/
  272.     /* The following members are used to compute hash functions, etc.        */
  273.     /*************************************************************************/
  274.  
  275. private:
  276.  
  277.     void            hashFuncInit(unsigned randSeed);
  278.  
  279. public:
  280.  
  281.     static
  282.     bool            hashHasWideChars(const char *name)
  283.     {
  284.         return  false; // (strchr(name, '\\') != NULL);
  285.     }
  286.  
  287.     static
  288.     bool            hashStringCompare(const char *s1, const char *s2);
  289.  
  290.     static
  291.     unsigned        hashComputeHashVal(const char *name);
  292.  
  293.     static
  294.     unsigned        hashComputeHashVal(const char *name, size_t nlen);
  295.  
  296.     Ident           hashString(const char *name)
  297.     {
  298.         return  lookupName(name, strlen(name), hashComputeHashVal(name), true);
  299.     }
  300.  
  301.     Ident           lookupString(const char *name)
  302.     {
  303.         return  lookupName(name, strlen(name), hashComputeHashVal(name), false);
  304.     }
  305.  
  306.     Ident           hashName    (const char * name,
  307.                                  unsigned     hash,
  308.                                  unsigned     nlen,
  309.                                  bool         wide);
  310.  
  311.     Ident           lookupName  (const char *    name,
  312.                                  size_t          nlen,
  313.                                  unsigned        hval,
  314.                                  bool            add = false);
  315. };
  316.  
  317. /*---------------------------------------------------------------------------*/
  318.  
  319. inline
  320. bool                hashTab::tokenIsBinop(tokens tok, unsigned * precPtr,
  321.                                                       treeOps  * operPtr)
  322. {
  323.     if  (tok <= tkKwdLast)
  324.     {
  325.         const kwdDsc *  tokDesc = tokenDesc(tok);
  326.  
  327.         *precPtr =          tokDesc->kdOper2prec;
  328.         *operPtr = (treeOps)tokDesc->kdOper2;
  329.  
  330.         return  true;
  331.     }
  332.     else
  333.     {
  334.         return  false;
  335.     }
  336. }
  337.  
  338. inline
  339. bool                hashTab::tokenIsUnop (tokens tok, unsigned * precPtr,
  340.                                                       treeOps  * operPtr)
  341. {
  342.     if  (tok <= tkKwdLast)
  343.     {
  344.         const kwdDsc *  tokDesc = tokenDesc(tok);
  345.  
  346.         *precPtr =          tokDesc->kdOper1prec;
  347.         *operPtr = (treeOps)tokDesc->kdOper1;
  348.  
  349.         return  true;
  350.     }
  351.     else
  352.     {
  353.         return  false;
  354.     }
  355. }
  356.  
  357. inline
  358. bool                hashTab::tokenIsType (tokens tok)
  359. {
  360.     if  (tok <= tkKwdLast)
  361.     {
  362.         const kwdDsc *  tokDesc = tokenDesc(tok);
  363.  
  364.         return  (bool)((tokDesc->kdAttribs & 1) != 0);
  365.     }
  366.     else
  367.     {
  368.         return  false;
  369.     }
  370. }
  371.  
  372. inline
  373. bool                hashTab::tokenBegsTyp(tokens tok)
  374. {
  375.     if  (tok <= tkKwdLast)
  376.     {
  377.         const kwdDsc *  tokDesc = tokenDesc(tok);
  378.  
  379.         return  (bool)((tokDesc->kdAttribs & 2) != 0);
  380.     }
  381.     else
  382.     {
  383.         return  false;
  384.     }
  385. }
  386.  
  387. inline
  388. bool                hashTab::tokenOvlOper(tokens tok)
  389. {
  390.     if  (tok <= tkKwdLast)
  391.     {
  392.         const kwdDsc *  tokDesc = tokenDesc(tok);
  393.  
  394.         return  (bool)((tokDesc->kdAttribs & 4) != 0);
  395.     }
  396.     else
  397.     {
  398.         return  false;
  399.     }
  400. }
  401.  
  402. inline
  403. unsigned            hashTab::tokenIsMod(tokens tok)
  404. {
  405.     if  (tok <= tkKwdLast)
  406.     {
  407.         const kwdDsc *  tokDesc = tokenDesc(tok);
  408.  
  409.         if  (tokDesc->kdModifier)
  410.             return  1 << tokDesc->kdModifier;
  411.     }
  412.  
  413.     return  0;
  414. }
  415.  
  416. inline
  417. const   char *      hashTab::tokenName(tokens tok)
  418. {
  419.     assert(tok != tkNone && tok <= tkKwdLast);
  420.  
  421.     return  hashKwdNames[tok];
  422. }
  423.  
  424. inline
  425. const   size_t      hashTab::tokenNlen(tokens tok)
  426. {
  427.     assert(tok != tkNone && tok <= tkKwdLast);
  428.  
  429.     return  hashKwdNlens[tok];
  430. }
  431.  
  432. /*****************************************************************************/
  433. #endif
  434. /*****************************************************************************/
  435.