home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / SHOWCLAS.C < prev    next >
Text File  |  1996-05-16  |  21KB  |  523 lines

  1. /*****************************************************************************/
  2. /* File:                                                                     */
  3. /*                                                                           */
  4. /*   showclas.c                                                              */
  5. /*                                                                           */
  6. /* Description:                                                              */
  7. /*                                                                           */
  8. /*  Display c++ objects.                                                     */
  9. /*                                                                           */
  10. /*                                                                           */
  11. /* History:                                                                  */
  12. /*                                                                           */
  13. /*   03/21/96 Created.                                                       */
  14. /*                                                                           */
  15. /*****************************************************************************/
  16. #include "all.h"
  17. #include "demangle.h"
  18.  
  19. extern UINT VideoCols;
  20. extern UINT ExprAddr;                   /* Address value of expr             */
  21.                                         /* ( Set by ParseExpr )              */
  22.  
  23. static BOOL ViewMemFncs = TRUE;
  24.  
  25. void SetViewMemFncs( void ) {ViewMemFncs ^= 1;}
  26. BOOL GetViewMemFncs( void ) {return(ViewMemFncs);}
  27.  
  28. /*****************************************************************************/
  29. /* ShowClass()                                                               */
  30. /*                                                                           */
  31. /* Description:                                                              */
  32. /*                                                                           */
  33. /*   Show a c++ object.                                                      */
  34. /*                                                                           */
  35. /* Parameters:                                                               */
  36. /*                                                                           */
  37. /*   dfp       pointer to dfile node.                                        */
  38. /*   row       where to start the display.                                   */
  39. /*   rows      rows available for display.                                   */
  40. /*   skip      # of logical records to skip.                                 */
  41. /*                                                                           */
  42. /* Return:                                                                   */
  43. /*                                                                           */
  44. /*   TRUE                                                                    */
  45. /*   FALSE                                                                   */
  46. /*                                                                           */
  47. /*****************************************************************************/
  48. #define ISA_STRUCT 0x01
  49. UINT ShowClass(DFILE *dfp, UINT row, UINT rows, UINT skip)
  50. {
  51.  UINT      item;
  52.  UINT      rr;
  53.  Trec     *tp;
  54.  UINT      Nitems;
  55.  UINT      mid;
  56.  UINT      sfx;
  57.  UCHAR    *cp;
  58.  UCHAR    *pBuf;
  59.  UINT      baseaddr;
  60.  UCHAR     buffer[1024];         /* adjust this when you know max chars/line */
  61.  ULONG     memberoffs;                  /* offset of member within structure.*/
  62.  ULONG     membertype;                  /* type of the class member.         */
  63.  ULONG     itemtypeno;                  /* type list number.                 */
  64.  BOOL      IsStruct;
  65.  BOOL      IsMangled;
  66.  int       ColonColumn = 0;
  67.  
  68.  TD_CLASS    *pClass;
  69.  TD_TYPELIST *pClassItemList;
  70.  
  71.  mid      = dfp->mid;
  72.  sfx      = dfp->sfx;
  73.  baseaddr = dfp->baseaddr;
  74.  
  75.  /****************************************************************************/
  76.  /* - verify that this is a class record and that it has members.            */
  77.  /* - some classes are null.                                                 */
  78.  /****************************************************************************/
  79.  pClass = (TD_CLASS*)QbasetypeRec(mid, dfp->showtype);
  80.  if( (pClass == NULL)                ||
  81.      (pClass->RecType    != T_CLASS) ||
  82.      (pClass->NumMembers == 0)
  83.    )
  84.   return(FALSE);
  85.  
  86.  /****************************************************************************/
  87.  /* - format structures as in c.                                             */
  88.  /****************************************************************************/
  89.  IsStruct = FALSE;
  90.  if(pClass->TypeQual & ISA_STRUCT )
  91.   IsStruct = TRUE;
  92.  
  93.  /****************************************************************************/
  94.  /* - get the number of class members.                                       */
  95.  /* - quit if we were told to skip more than the number of class members.    */
  96.  /****************************************************************************/
  97.  Nitems = pClass->NumMembers;
  98.  if( Nitems <= skip )
  99.   return(FALSE);
  100.  
  101.  /****************************************************************************/
  102.  /* - get a ptr to the class item list.                                      */
  103.  /* - loop formatting each item.                                             */
  104.  /****************************************************************************/
  105.  pClassItemList = (TD_TYPELIST*)QtypeRec(mid, pClass->ItemListIndex);
  106.  
  107.  for( rr=0, item=skip+1;
  108.       (rr < rows) &&
  109.       (item <= Nitems);
  110.       ++rr, ++item
  111.     )
  112.  {
  113.   /***************************************************************************/
  114.   /* - init a display buffer for the class item.                             */
  115.   /* - put tabstops in for formatting purposes.                              */
  116.   /***************************************************************************/
  117.   InitClassItem( buffer, sizeof(buffer) );
  118.   PutInTabStops( buffer);
  119.  
  120.   pBuf = buffer;
  121.  
  122.   /***************************************************************************/
  123.   /* - get a ptr to the type record for this item.                           */
  124.   /* - we can have an item typeno == 0 because we added a "0" type index     */
  125.   /*   as the first item in the list when mapping base classes.              */
  126.   /*********************************************w*****************************/
  127.   itemtypeno = pClassItemList->TypeIndex[item-1];
  128.   tp         = NULL;
  129.  
  130.   if(itemtypeno != 0 )
  131.    tp = (Trec *)QtypeRec(mid, itemtypeno );
  132.  
  133.   /***************************************************************************/
  134.   /* - if not viewing member functions, then continue. don't format.         */
  135.   /***************************************************************************/
  136.   if( (IsStruct         == FALSE) &&
  137.       (GetViewMemFncs() == FALSE) &&
  138.        tp                         &&
  139.       (tp->RecType == T_MEMFNC)
  140.     )
  141.   {
  142.    rr--;
  143.    continue;
  144.   }
  145.  
  146.   /***************************************************************************/
  147.   /* - we want to display the expr name for the first display item.          */
  148.   /* - if this is really a struct, then display as in c.                     */
  149.   /* - if it's a class, put in the class name.                               */
  150.   /* - if it's a derived class, then proceed to format it's base class.      */
  151.   /* - if it's a base class, then we're done formatting this item.           */
  152.   /***************************************************************************/
  153.   if( (item==1) && dfp && dfp->expr )
  154.   {
  155.    strncpy( pBuf+1, dfp->expr, strlen(dfp->expr) );
  156.  
  157.    pBuf = NextTab(pBuf);
  158.  
  159.    if( IsStruct == FALSE )
  160.    {
  161.     pClass = (TD_CLASS*)QtypeRec(mid, dfp->showtype);
  162.  
  163.     strncpy( pBuf, pClass->Name, pClass->NameLen );
  164.  
  165.     if( dfp->DfpFlags.ClassType == DFP_DERIVED_CLASS )
  166.      pBuf += pClass->NameLen;
  167.    }
  168.   }
  169.  
  170.   /***************************************************************************/
  171.   /* - format member attributes, i.e. public, static, etc..                  */
  172.   /***************************************************************************/
  173.   if( tp && (IsStruct == FALSE) )
  174.   {
  175.    switch(tp->RecType)
  176.    {
  177.     case T_FRIEND:
  178.     {
  179.      FmtFriendAttrs( tp, pBuf + 4 );
  180.      *(pBuf + strlen(pBuf)) = ' ';
  181.      pBuf = NextTab(pBuf);
  182.     }
  183.     break;
  184.  
  185.     case T_MEMFNC:
  186.     {
  187.      FmtMemFncAttrs( tp, pBuf + 4 );
  188.      *(pBuf + strlen(pBuf)) = ' ';
  189.      pBuf = NextTab(pBuf);
  190.     }
  191.     break;
  192.  
  193.     case T_CLSMEM:
  194.     {
  195.      FmtClsMemAttrs( tp, pBuf + 4 );
  196.      *(pBuf + strlen(pBuf)) = ' ';
  197.      pBuf = NextTab(pBuf);
  198.     }
  199.     break;
  200.  
  201.     case T_CLSDEF:
  202.     {
  203.      FmtClsDefAttrs( tp, pBuf + 4 );
  204.      *(pBuf + strlen(pBuf)) = ' ';
  205.      pBuf = NextTab(pBuf);
  206.     }
  207.     break;
  208.  
  209.     case T_BSECLS:
  210.     {
  211.      int len;
  212.  
  213.      /************************************************************************/
  214.      /* - put in some formatting so that we can line up the multiple         */
  215.      /*   inheritance classes.                                               */
  216.      /************************************************************************/
  217.      if( item == 1 )
  218.       ColonColumn = pBuf - buffer;
  219.      else if( ColonColumn == 0 )
  220.       pBuf = NextTab(pBuf);
  221.      else
  222.       pBuf += ColonColumn;
  223.  
  224.      FmtBseClsAttrs( tp, pBuf );
  225.  
  226.      len            = strlen(pBuf);
  227.      *(pBuf + len)  = ' ';
  228.      pBuf          += len;
  229.     }
  230.     break;
  231.    }
  232.   }
  233.   else
  234.    pBuf = NextTab(pBuf);
  235.  
  236.   /***************************************************************************/
  237.   /* - format the item name and demangle it if need be.                      */
  238.   /* - a base class record will not have a name.                             */
  239.   /***************************************************************************/
  240.   cp = (UCHAR *)QClassItemList(mid, pClassItemList, NAME, item-1);
  241.   if( cp != NULL )
  242.   {
  243.    IsMangled = FALSE;
  244.    IsMangled = unmangle( pBuf, cp + 2 );
  245.    if( IsMangled == FALSE )
  246.    {
  247.     int len = *(USHORT*)cp;
  248.  
  249.     strncpy(pBuf, cp+2, len);
  250.    }
  251.    pBuf = NextTab(pBuf);
  252.   }
  253.  
  254.   /***************************************************************************/
  255.   /* - format class member records.                                          */
  256.   /* - format class def records.                                             */
  257.   /***************************************************************************/
  258.   if(  tp &&
  259.       ( (tp->RecType == T_CLSMEM) ||
  260.         (tp->RecType == T_CLSDEF)
  261.       )
  262.     )
  263.   {
  264.    /**************************************************************************/
  265.    /* - set the base address location for the object.                        */
  266.    /* - if this is a static member, then kill the baseaddr.                  */
  267.    /**************************************************************************/
  268.    baseaddr = dfp->baseaddr;
  269.  
  270.    if( tp->RecType == T_CLSMEM )
  271.    {
  272.     TD_CLSMEM *pClsMem;
  273.  
  274.     pClsMem = (TD_CLSMEM *)tp;
  275.  
  276.     if( pClsMem->TypeQual & CLSMEM_STATIC )
  277.      baseaddr = 0;
  278.    }
  279.  
  280.    /**************************************************************************/
  281.    /* - get the offset and type of this member within the object.            */
  282.    /* - for a static member, the offset will be a flat address.              */
  283.    /**************************************************************************/
  284.    memberoffs = QClassItemList(mid, pClassItemList, MEMBEROFFSET, item-1);
  285.    membertype = QClassItemList(mid, pClassItemList, MEMBERTYPE,   item-1);
  286.  
  287.    if( tp->RecType == T_CLSDEF )
  288.    {
  289.     /*************************************************************************/
  290.     /* - if this is a class def then simply append a suffix for typedefs     */
  291.     /*   and enums. These members do not occupy any storage.                 */
  292.     /*************************************************************************/
  293.     tp = (Trec*)QtypeRec(mid, membertype);
  294.     if( tp->RecType == T_TYPDEF )
  295.     {
  296.      strcpy(pBuf, "...typedef");
  297.      *(strchr(pBuf, '\0')) = ' ';
  298.     }
  299.     else if( tp->RecType == T_ENUM )
  300.     {
  301.      strcpy(pBuf, "...enum");
  302.      *(strchr(pBuf, '\0')) = ' ';
  303.     }
  304.     else
  305.      FormatDataItem( pBuf, baseaddr + memberoffs, mid, membertype, sfx );
  306.    }
  307.    else
  308.     FormatDataItem( pBuf, baseaddr + memberoffs, mid, membertype, sfx );
  309.   }
  310.   /***************************************************************************/
  311.   /* - chop off the buffer at the max screen width.                          */
  312.   /* - take out the tabstops that have not been written on.                  */
  313.   /***************************************************************************/
  314.   buffer[ VideoCols + 2 ] = 0;
  315.   PullOutTabStops( buffer );
  316.   putrc( row + rr, 0, buffer );
  317.  }
  318.  
  319.  /****************************************************************************/
  320.  /* - clear the remaining display lines.                                     */
  321.  /****************************************************************************/
  322.  if( rr < rows )
  323.    ClrScr( row + rr, row + rows - 1, vaStgExp );
  324.  return( TRUE );
  325. }
  326.  
  327. UINT ZapClass(DFILE *dfp,UINT row,UINT skip )
  328. {
  329. #if 0
  330.     TD_STRUCT *tp;                                                      /*813*/
  331.  
  332.     TD_TYPELIST *typelist;                                              /*813*/
  333.  
  334.     Trec *namelist;
  335.     UINT itemtype, mid = dfp->mid, sfx = dfp->sfx;
  336.     uchar *cp, *baseaddr = (uchar*) ExprAddr;
  337.  
  338.     if( (tp = (TD_STRUCT*)QbasetypeRec(mid, dfp->showtype))
  339.      && (tp->RecType == T_STRUCT)
  340.      && (tp->NumMembers) > skip                                         /*813*/
  341.      && (typelist = (TD_TYPELIST*)QtypeRec(mid,tp->TypeListIndex))      /*813*/
  342.      && (namelist = QtypeRec(mid, tp->NameListIndex))                   /*813*/
  343.      && (cp = (UCHAR*)QNameList(namelist,NAME,skip))                    /*813*/
  344.      && (itemtype = typelist->TypeIndex[skip] )                         /*813*/
  345.      )
  346.     {
  347.      return( (* PickKeyer( mid, itemtype))
  348.             (mid, itemtype, sfx, row, *cp + 2,
  349.             baseaddr + QNameList(namelist,MEMBEROFFSET,skip)  ) );   /*813215*/
  350.     }
  351.     return( FALSE );
  352. #endif
  353. }
  354.  
  355.  
  356. /*****************************************************************************/
  357. /* - insert tabstops into the buffer. i guess a tab stop is 0x09...don't     */
  358. /*   really know...it doesn't matter.                                        */
  359. /*****************************************************************************/
  360. #define FIRSTTAB 20
  361. #define TABSTOP  10
  362. #define TABCHAR  0x09
  363. void PutInTabStops( char *pbuf )
  364. {
  365.  char *cp = pbuf;
  366.  int   i,j;
  367.  
  368.  cp[FIRSTTAB-1] =  TABCHAR;
  369.  j              =  FIRSTTAB;
  370.  cp             = &cp[FIRSTTAB-1];
  371.  i = 0;
  372.  while(*cp++)
  373.  {
  374.   if( i == TABSTOP )
  375.   {
  376.    pbuf[j-1]=TABCHAR;
  377.    i = 0;
  378.   }
  379.   i++;
  380.   j++;
  381.  }
  382. }
  383.  
  384. /*****************************************************************************/
  385. /* - extract tabstops.                                                       */
  386. /*****************************************************************************/
  387. void PullOutTabStops( char *cp )
  388. {
  389.  for( ; *cp; cp++ )
  390.  {
  391.   if(*cp == TABCHAR)
  392.    *cp = ' ';
  393.  }
  394. }
  395.  
  396. /*****************************************************************************/
  397. /* - return a ptr to the next tabstop.                                       */
  398. /*****************************************************************************/
  399. char *NextTab( char *cp )
  400. {
  401.  while(*cp != TABCHAR) cp++;
  402.  return(cp);
  403. }
  404.  
  405. /*****************************************************************************/
  406. /* InitClassItem()                                                           */
  407. /*                                                                           */
  408. /* Description:                                                              */
  409. /*                                                                           */
  410. /*   Initialize a buffer for the display of a c++ class member.              */
  411. /*                                                                           */
  412. /*    --- ------------------------------ ---                                 */
  413. /*   |   |                              |   |                                */
  414. /*   |atr| blanks                       | 0 |                                */
  415. /*    --- ------------------------------ ---                                 */
  416. /*                                                                           */
  417. /*    atr is the display attribute for the class item.                       */
  418. /*                                                                           */
  419. /* Parameters:                                                               */
  420. /*                                                                           */
  421. /*   cp                                                                      */
  422. /*   bufsize                                                                 */
  423. /*                                                                           */
  424. /* Return:                                                                   */
  425. /*                                                                           */
  426. /* Assumptions:                                                              */
  427. /*                                                                           */
  428. /*****************************************************************************/
  429. void InitClassItem( UCHAR *cp, int bufsize )
  430. {
  431.  memset( cp, ' ' , bufsize );
  432.  
  433.  cp[0]           = Attrib(vaStgExp);
  434.  cp[bufsize-1]   = 0;
  435. }
  436.  
  437. /*****************************************************************************/
  438. /* unmabgle()                                                                */
  439. /*                                                                           */
  440. /* Description:                                                              */
  441. /*                                                                           */
  442. /*  Demangle a mangled name.                                                 */
  443. /*                                                                           */
  444. /* Parameters:                                                               */
  445. /*                                                                           */
  446. /*   pDeMangledName  ->to receiver of demangled name.                        */
  447. /*   pMangledName                                                            */
  448. /*                                                                           */
  449. /* Return:                                                                   */
  450. /*                                                                           */
  451. /*   IsMangled                                                               */
  452. /*                                                                           */
  453. /* Assumptions:                                                              */
  454. /*                                                                           */
  455. /*  pMangledName   != NULL                                                   */
  456. /*  pDeMangledName != NULL                                                   */
  457. /*                                                                           */
  458. /*****************************************************************************/
  459. BOOL unmangle( UCHAR *pDeMangledName, UCHAR *pMangledName )
  460. {
  461.  BOOL  IsMangled;
  462.  char *cp;
  463.  Name *pName;
  464.  char *pRest;
  465.  
  466.  IsMangled = FALSE;
  467.  pName = demangle( pMangledName, &pRest, RegularNames );
  468.  if( pName != NULL )
  469.  {
  470.   IsMangled = TRUE;
  471.   switch(kind(pName))
  472.   {
  473.    case MemberFunction:
  474.    case Function:
  475.    {
  476.     cp = text(pName);
  477.     strncpy(pDeMangledName, cp, strlen(cp) );
  478.    }
  479.    break;
  480.  
  481.    case MemberVar:
  482.     cp = varName(pName);
  483.     strncpy(pDeMangledName, cp, strlen(cp) );
  484.     break;
  485.  
  486.    default:
  487.     break;
  488.   }
  489.  }
  490.  return(IsMangled);
  491. }
  492.  
  493. /*****************************************************************************/
  494. /* - only count the non-member function lines if the member function         */
  495. /*   display is turned off.                                                  */
  496. /*****************************************************************************/
  497. UINT GetLinesNoMemFncs( UINT mid, Trec *tp )
  498. {
  499.  TD_CLASS    *pClass = (TD_CLASS*)tp;
  500.  TD_TYPELIST *pClassItemList;
  501.  UINT         Nitems;
  502.  UINT         NitemsNoFncs;
  503.  UINT         item;
  504.  ULONG        itemtypeno;
  505.  
  506.  pClassItemList = (TD_TYPELIST*)QtypeRec(mid, pClass->ItemListIndex);
  507.  Nitems         = pClass->NumMembers;
  508.  
  509.  NitemsNoFncs = 0;
  510.  for( item = 1; item <= Nitems; ++item )
  511.  {
  512.   itemtypeno = pClassItemList->TypeIndex[item-1];
  513.   tp         = NULL;
  514.  
  515.   if(itemtypeno != 0 )
  516.    tp = (Trec *)QtypeRec(mid, itemtypeno );
  517.  
  518.   if( tp && (tp->RecType != T_MEMFNC) )
  519.    NitemsNoFncs++;
  520.  }
  521.  return( NitemsNoFncs );
  522. }
  523.