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

  1. #ifndef __DEMANGLEH
  2. #define __DEMANGLEH
  3.  
  4. /********************************************************************/
  5. /*  <demangle.h> header file                                        */
  6. /*                                                                  */
  7. /*  Licensed Materials - Property of IBM                            */
  8. /*                                                                  */
  9. /*  IBM VisualAge C++ for OS/2, Version 3                           */
  10. /*  Copyright (C) International Business Machines Corp., 1991, 1995 */
  11. /*  All rights reserved                                             */
  12. /*                                                                  */
  13. /*  US Government Users Restricted Rights -                         */
  14. /*  Use, duplication, or disclosure restricted                      */
  15. /*  by GSA ADP Schedule Contract with IBM Corp.                     */
  16. /*                                                                  */
  17. /********************************************************************/
  18.  
  19. /*************************************************************************
  20.  
  21.     utilC++/filter/demangle.h, C++-util, C++.1.0
  22.  
  23.     demangle.h
  24.  
  25. This file contains the C and C++ interfaces to the C++ name demangler.
  26.  
  27. The library provides the function "Demangle" and a number of classes.
  28. "Demangle" is used to convert mangled C++ names to (pointers to) instances
  29. of the "Name" class (or some class derived from "Name"). Once such an object
  30. has been created, the user can find out certain characteristics of the name.
  31.  
  32. There are six subclasses of "Name", organized in the following way:
  33.  
  34.                           Name
  35.                        /  |  \  \
  36.             SpecialName  / \  \  ClassName
  37.                         /   \  LongName
  38.                        /     \
  39.                       /    FunctionName
  40.                      /           \
  41.                     /             \
  42.           MemberVarName         MemberFunctionName
  43.  
  44. The "SpecialName" class is for special compiler generated class objects,
  45. while the "ClassName" class is for names that are mangled class names.
  46. The "LongName" class is reserved for names that, when mangled, are "too
  47. long". "Too long" is system dependent, and determined by the compiler. See
  48. below.
  49.  
  50. The objects that the other subclasses represent should be self-evident.
  51.  
  52. The demangler will only demangle "special" names and names that are only
  53. class names when the "SpecialNames" and "ClassNames" flags, respectively,
  54. are supplied to the demangler in the "options" parameter. This parameter
  55. takes the default value of "RegularNames", and so it will by default
  56. demangle only regular names. "SpecialNames", "ClassNames" and "RegularNames"
  57. can be specified in any combination simply by "bit-or"ing them together.
  58.  
  59. One can also request that the text of the individual parameters of functions
  60. be kept around by specifying the option "ParameterText". It should be noted
  61. that this increases the amount of storage required to store a demangled name
  62. and can amount to alot when many names are being demangled and kept around.
  63.  
  64. One can also request that the text of the individual members of a qualifier
  65. list can be kept around by specifying the option "QualifierText". It should
  66. be noted that this increases the amount of storage required to store a
  67. demangled name. The "Qualifier" class is an auxiliary class that represents
  68. a member name's qualifier. Normally, only the text is available, but if
  69. QualifierText is specified as an option, the text of individual class names
  70. is available, both in full and without template arguments.
  71.  
  72. An important feature of the demangler is that, while it is a C++ program, it
  73. uses no features requiring the C++ runtime library, and hence it is not
  74. necessary to link libC.a to the program containing demangle.o. This affects
  75. the interface in only one way. Normally, the class "Name" would be a virtual
  76. class, but due to the lack of libC.a, pure virtual functions cannot be used.
  77. The user should always treat the Name class as though it is a virtual.
  78.  
  79. The most common operation is expected to be simply the retrieval of the
  80. string representation of the demangled name, and is accomplished by the
  81. virtual "Name" member "Text", which returns a pointer to a string. This
  82. string is part of the Name object; if a user wants to change it or have
  83. it exist beyond the lifetime of the object, s/he must make a copy of the
  84. string. There is also a "char *" conversion operator for "Name" (and its
  85. derived classes).
  86.  
  87. Other information about a demangled name is available. This is accomplished
  88. by first determining the actual kind of the given name, via the virtual
  89. method "Kind" of "Name", which returns a "NameKind" value (see below). Once
  90. this value is determined, the "Name *" can be cast to the appropriate
  91. derived class, and the methods of this class can then be called. These
  92. methods are defined below.
  93.  
  94. ***********************************************************************/
  95.  
  96. #include <stddef.h>
  97.  
  98. typedef enum { false = 0, False = 0, true  = 1, True  = 1 } Boolean;
  99.  
  100. typedef enum { VirtualName, MemberVar, Function, MemberFunction, Class,
  101.                Special, Long } NameKind;
  102. typedef enum { RegularNames = 0x1, ClassNames = 0x2, SpecialNames = 0x4,
  103.                ParameterText = 0x8, QualifierText = 0x10 } DemanglingOptions;
  104.  
  105. #pragma pack(4)
  106.  
  107. #ifdef __cplusplus
  108.  
  109. class CommonType;
  110. class TypeList;
  111. class Argument;
  112. class ArgumentList;
  113.  
  114. class Name;
  115.  
  116. /*
  117.  * Demangle. Given a valid C "name" and the address of a char pointer, this
  118.  * function creates a "Name" instance and returns its address. A valid C name
  119.  * is one starting with an "_" or letter followed by a number of letters, digits
  120.  * and "_"s. The name is assumed to start at the beginning of the string, but
  121.  * there may be trailing characters not part of the mangled name. A pointer
  122.  * into "name" at the first character not part of the mangled name is returned
  123.  * in "rest".
  124.  *     Demangle will return NULL when the text of the demangled name is the
  125.  * same as the text of the mangled name. Thus, when NULL is returned, the
  126.  * character string given as Demangle's first argument is in fact the
  127.  * demangled name, too.
  128.  */
  129.  
  130. Name *Demangle(char *name, char *&rest, unsigned long options = RegularNames);
  131.  
  132. class Name {
  133.     public:
  134.         virtual ~Name();
  135.         virtual NameKind Kind();
  136.  
  137.         virtual char *Text();
  138.         operator char *();
  139. };
  140.  
  141. // ClassName: representation of a demangled (possibly nested) class name
  142. /*
  143.  *     Kind: returns the kind of the name (Class)
  144.  *     Text: returns the text of the demangled name
  145.  *     operator Qualifier *: returns the associated qualifier
  146.  */
  147.  
  148. class Qualifier;
  149.  
  150. class ClassName: public Name {
  151.         friend Name *BufferedDemangle(char *, char *&, unsigned long = RegularNames);
  152.  
  153.         Qualifier *qualifier;
  154.  
  155.         ClassName(Qualifier *);
  156.     public:
  157.         ~ClassName();
  158.         virtual NameKind Kind();
  159.  
  160.         operator Qualifier *();
  161.  
  162.         char *Text();
  163. };
  164.  
  165. // SpecialName: representation of a demangled compiler-generated name
  166. /*
  167.  *     Kind: returns the kind of the name (Special)
  168.  *     Text: return the text of the demangled name
  169.  */
  170.  
  171. class SpecialName: public Name {
  172.         friend Name *BufferedDemangle(char *, char *&, unsigned long = RegularNames);
  173.  
  174.         char *text;
  175.  
  176.         SpecialName(char *);
  177.     public:
  178.         virtual ~SpecialName();
  179.  
  180.         virtual NameKind Kind();
  181.         virtual char *Text();
  182. };
  183.  
  184. // LongName: representation of a demangled long name
  185. /*
  186.  *     Kind: returns the kind of the name (LongName)
  187.  *     ProbableKind: returns the probable kind of the name, but if the
  188.  *           name is extremely long, the value might be wrong (e.g. a
  189.  *           member function name might be confused for a member var
  190.  *           name).
  191.  *     Text: return the text of the demangled name
  192.  */
  193.  
  194. class LongName: public Name {
  195.         friend Name *BufferedDemangle(char *, char *&, unsigned long = RegularNames);
  196.  
  197.         char *text;
  198.         NameKind probableKind;
  199.  
  200.         LongName(Name *);
  201.         LongName(char *);
  202.     public:
  203.         virtual ~LongName();
  204.  
  205.         virtual NameKind Kind();
  206.         virtual NameKind ProbableKind();
  207.         virtual char *Text();
  208. };
  209.  
  210. // MemberVarName: representation of a demangled static member variable name
  211. /*
  212.  *     Kind: returns the kind of the name (MemberVar)
  213.  *     VarName: returns the unqualified name of the member variable
  214.  *     Scope: returns the qualifier of the member variable
  215.  *     Text: return the text of the demangled name
  216.  *
  217.  *     IsConstant: returns whether the function is a constant function
  218.  *     IsVolatile: returns whether the function is a volatile function
  219.  *     IsStatic: returns whether the function is a static member function
  220.  */
  221.  
  222. class MemberVarName: public Name {
  223.         friend Name *BufferedDemangle(char *, char *&, unsigned long = RegularNames);
  224.  
  225.         char *text;
  226.         char *name;
  227.         Qualifier *qualifier;
  228.  
  229.         Boolean isConstant:8;
  230.         Boolean isStatic:8;
  231.         Boolean isVolatile:8;
  232.  
  233.         MemberVarName(char *, unsigned long, Qualifier *, Boolean, Boolean,
  234.                       Boolean);
  235.     public:
  236.         virtual ~MemberVarName();
  237.  
  238.         virtual NameKind Kind();
  239.         char *VarName();
  240.         Qualifier *Scope();
  241.         virtual char *Text();
  242.  
  243.         Boolean IsConstant();
  244.         Boolean IsStatic();
  245.         Boolean IsVolatile();
  246. };
  247.  
  248. // FunctionName: representation of a demangled function name
  249. /*
  250.  *     Kind: returns the kind of the name (Function)
  251.  *     RootName: returns the unqualified name of the function
  252.  *     Text: returns the text of the demangled name
  253.  *
  254.  * Further, if the option "ParameterText" was supplied to Demangle, the
  255.  * function "ParamDataKept" is True, and the following functions are
  256.  * useful:
  257.  *
  258.  *     nArguments: returns the number of arguments of the function
  259.  *     Arguments: returns the text of the function's argument string
  260.  *     Argument: returns the text of the i'th argument (numbered from 0) of
  261.  *       the function. If i < 0 or i >= n, where n is the number of arguments
  262.  *       of the function, NULL is returned.
  263.  */
  264.  
  265. class FunctionName: public Name {
  266.         friend Name *BufferedDemangle(char *, char *&, unsigned long = RegularNames);
  267.  
  268.     protected:
  269.         Boolean paramDataKept;
  270.         char *name;
  271.         TypeList *arguments;
  272.  
  273.         char *text;
  274.         FunctionName(char *, unsigned long, TypeList *, Boolean);
  275.  
  276.     public:
  277.         virtual ~FunctionName();
  278.  
  279.         virtual NameKind Kind();
  280.         char *RootName();
  281.         virtual char *Text();
  282.  
  283.         Boolean ParamDataKept();
  284.         long nArguments();
  285.         char *Arguments();
  286.         char *Argument(unsigned long);
  287. };
  288.  
  289. // MemberFunctionName: representation of a demangled member function name
  290. /*
  291.  *     Kind: returns the kind of the name (MemberFunction)
  292.  *     Scope: returns a pointer to the qualifier of the name
  293.  *     Text: returns the text of the demangled name
  294.  *
  295.  *     IsConstant: returns whether the function is a constant function
  296.  *     IsVolatile: returns whether the function is a volatile function
  297.  *     IsStatic: returns whether the function is a static member function
  298.  */
  299.  
  300. class MemberFunctionName: public FunctionName {
  301.         friend Name *BufferedDemangle(char *, char *&, unsigned long = RegularNames);
  302.  
  303.         Qualifier *qualifier;
  304.         Boolean isConstant: 8;
  305.         Boolean isStatic: 8;
  306.         Boolean isVolatile: 8;
  307.         Boolean isContravariant: 8;
  308.  
  309.         char *text;
  310.  
  311.         MemberFunctionName(char *, unsigned long, Qualifier *, TypeList *,
  312.                            Boolean, Boolean, Boolean, Boolean);
  313.     public:
  314.         virtual ~MemberFunctionName();
  315.  
  316.         virtual NameKind Kind();
  317.         Qualifier *Scope();
  318.         virtual char *Text();
  319.  
  320.         Boolean IsConstant();
  321.         Boolean IsStatic();
  322.         Boolean IsVolatile();
  323.         Boolean IsContravariant();
  324. };
  325.  
  326. // Qualifier: representation of a demangled (possibly nested) class name.
  327. /*
  328.  *     Text: returns the text of the qualifier
  329.  *
  330.  * Additionally, if the option "QualifierText" was specified, "ClassDataKept"
  331.  * is True, and the following members are useful:
  332.  *
  333.  *     NQualifiers: returns the number of segments in the qualifier name; i.e.,
  334.  *                  the number of levels the class name is nested, plus one.
  335.  *     operator[]: returns a pointer to the Qualifier::Class of the given
  336.  *                 class of the qualifier. These are numbered starting at zero
  337.  *                 on the left, and increasing to the right. Thus, in X::Y,
  338.  *                 "X" has number zero and "Y" number one.
  339.  *
  340.  * The nested class "Class" provides the information about one of the
  341.  * classes of the qualification. This information is available only if
  342.  * "ClassDataKept" is True. Its public members are:
  343.  *     Name: returns a pointer to the raw name of the class, excluding any
  344.  *           template arguments).
  345.  *     Text: returns a pointer to the full class name, including template
  346.  *           arguments.
  347.  */
  348.  
  349. class Qualifier {
  350.     public:
  351.         class Class {
  352.                 friend class Qualifier;
  353.                 friend Class *ValidClassName(char *, unsigned long &, Boolean);
  354.  
  355.                 char *name;
  356.                 char *text;
  357.             public:
  358.                 char *Name();
  359.                 char *Text();
  360.         };
  361.  
  362.     private:
  363.         friend Qualifier *ValidQualifier(char *, unsigned long &, Boolean);
  364.         friend class MemberVarName;
  365.         friend class MemberFunctionName;
  366.  
  367.         unsigned long nQNames;
  368.         Class **qualifiers;
  369.  
  370.         char *text;
  371.         Boolean classDataKept;
  372.  
  373.         Qualifier(Class **, unsigned long nQNames, Boolean keepClassData);
  374.     public:
  375.         ~Qualifier();
  376.  
  377.         virtual char *Text();
  378.         unsigned long nQualifiers();
  379.  
  380.         Boolean ClassDataKept();
  381.         Class *operator[](unsigned long qualifier);
  382. };
  383.  
  384. // inline function definitions
  385.  
  386. inline          Name::~Name() { }
  387. inline NameKind Name::Kind() { return VirtualName; }
  388. inline char *   Name::Text() { return (char *)NULL; }
  389. inline          Name::operator char *() { return Text(); }
  390.  
  391. inline unsigned long Qualifier::nQualifiers() { return nQNames; }
  392. inline char *   Qualifier::Class::Name() { return name; }
  393. inline char *   Qualifier::Class::Text() { return text; }
  394. inline Boolean  Qualifier::ClassDataKept() { return classDataKept; }
  395.  
  396. inline          ClassName::~ClassName() { delete qualifier; }
  397. inline NameKind ClassName::Kind() { return Class; }
  398. inline          ClassName::operator Qualifier *() { return qualifier; }
  399. inline char *   ClassName::Text() { return qualifier->Text(); }
  400.  
  401. inline          SpecialName::SpecialName(char *t) { text = t; }
  402. inline          SpecialName::~SpecialName() { delete text; }
  403. inline NameKind SpecialName::Kind() { return Special; }
  404. inline char *   SpecialName::Text() { return text; }
  405.  
  406. inline          LongName::~LongName() { delete text; }
  407. inline NameKind LongName::Kind() { return Long; }
  408. inline NameKind LongName::ProbableKind() { return probableKind; }
  409. inline char *   LongName::Text() { return text; }
  410.  
  411. inline NameKind MemberVarName::Kind() { return MemberVar; }
  412. inline char *   MemberVarName::VarName() { return name; }
  413. inline Qualifier *MemberVarName::Scope() { return qualifier; }
  414. inline char *   MemberVarName::Text() { return text; }
  415. inline Boolean  MemberVarName::IsConstant() { return (Boolean)isConstant; }
  416. inline Boolean  MemberVarName::IsStatic() { return (Boolean)isStatic; }
  417. inline Boolean  MemberVarName::IsVolatile() { return (Boolean)isVolatile; }
  418.  
  419. inline NameKind FunctionName::Kind() { return Function; }
  420. inline char *   FunctionName::RootName() { return name; }
  421. inline char *   FunctionName::Text() { return text; }
  422. inline Boolean  FunctionName::ParamDataKept() { return paramDataKept; }
  423.  
  424. inline NameKind MemberFunctionName::Kind() { return MemberFunction; }
  425. inline Qualifier *MemberFunctionName::Scope() { return qualifier; }
  426. inline char *   MemberFunctionName::Text() { return text; }
  427. inline Boolean  MemberFunctionName::IsConstant() { return (Boolean)isConstant; }
  428. inline Boolean  MemberFunctionName::IsStatic() { return (Boolean)isStatic; }
  429. inline Boolean  MemberFunctionName::IsVolatile() { return (Boolean)isVolatile; }
  430. inline Boolean  MemberFunctionName::IsContravariant() { return (Boolean)isContravariant; }
  431.  
  432. #else
  433.  
  434. /*
  435.  * The C Interface
  436.  */
  437.  
  438. /*
  439.  *     demangle. Given a valid C++ "name" and the address of a char pointer,
  440.  * this function creates a "Name" instance and returns its address. A valid C++
  441.  * name is one starting with an "_" or letter followed by a number of letters,
  442.  * digits and "_"s. The name is assumed to start at the beginning of the
  443.  * string, but there may be trailing characters not part of the mangled name.
  444.  * A pointer into "name" at the first character not part of the mangled name
  445.  * is returned in "rest".
  446.  */
  447.  
  448.     typedef struct Name *Name;
  449.  
  450.     Name *demangle(   char *name, char **rest, unsigned long options   );
  451.  
  452. /*
  453.  * Each of the following functions takes a pointer to a Name as its only
  454.  * parameter.
  455.  */
  456.  
  457.     NameKind kind(   Name *   );
  458.  
  459.     /* return the character representation of a given Name */
  460.     char *text(   Name *   );
  461.  
  462.     /* return the probable type of a given LongName-type Name */
  463.     NameKind probableKind(/* Name * */);
  464.  
  465.     /* return the actual name of a given Var- or MemberVar-type Name */
  466.     char *varName(   Name *   );
  467.  
  468.     /* return the qualifier text of the given Member-type Name */
  469.     char *qualifier(   Name *   );
  470.  
  471.     /* return the actual name of a given Function- or MemberFunction- */
  472.     /* type Name                                                      */
  473.     char *functionName(   Name *   );
  474.  
  475.  
  476.     /* returns whether the parameter information was maintained for a */
  477.     /* particular Function- or MemberFunction- type Name.             */
  478.     Boolean paramDataKept(/* Name * */);
  479.  
  480.     /* returns whether the qualifier information was maintained for a */
  481.     /* particular Member- type Name.                                  */
  482.     Boolean classDataKept(/* Name * */);
  483.  
  484.     /*
  485.      * The next three functions require that option "ParameterText" was given
  486.      * to Demangle.
  487.      */
  488.  
  489.     /* return the number of arguments of a given Function- or Member- */
  490.     /* Function type Name.                                            */
  491.     long nArguments(/* Name * */);
  492.  
  493.     /* return the text of the argument list of a given Function- or Member- */
  494.     /* Function- type Name. (char *)NULL is returned if the name wasn't     */
  495.     /* demangled with option ParameterText, and "" is returned if the arg-  */
  496.     /* ument list is empty.                                                 */
  497.     char *argumentsText(/* Name * */);
  498.  
  499.     /* return the text of the nth argument of a given Function- or Member- */
  500.     /* Function- type Name. (char *)NULL is returned if the name wasn't    */
  501.     /* demangled with option ParameterText, or the function doesn't have n */
  502.     /* arguments. The arguments of a function are numbered from 0.         */
  503.     char *argumentText(/* Name *, int n */);
  504.  
  505.     /*
  506.      * The next three functions require that option "QualifierText" was given
  507.      * to Demangle.
  508.      */
  509.  
  510.     /* return the number of qualifiers of the given Member- type Name */
  511.     unsigned long nQualifiers(/* Name * */);
  512.  
  513.     /* return the text of the nth qualifier of a given Member- type Name. */
  514.     /* (char *)NULL is returned if "n" is out of range. The qualifiers of */
  515.     /* a name are numbered from the left starting at zero.                */
  516.     char *qualifierText(/* Name *, unsigned long n */);
  517.  
  518.     /* return the text of the class name of the nth qualifier of a given    */
  519.     /* Member- type Name. (char *)NULL is returned if "n" is out of range.  */
  520.     /* This function will return a value different from the preceding func- */
  521.     /* tion only if the class is a template class. The qualifiers of a name */
  522.     /* are numbered from the left starting at zero.                         */
  523.     char *qualifierNameText(/* Name *, unsigned long n */);
  524.  
  525.  
  526.     /* is a Member-type Name constant? */
  527.     Boolean isConstant(/* Name * */);
  528.  
  529.     /* is a Member-type Name static? */
  530.     Boolean isStatic(/* Name * */);
  531.  
  532.     /* is a Member-type Name volatile? */
  533.     Boolean isVolatile(/* Name * */);
  534.  
  535.     /* is a MemberFunction-type Name a contravariant function? */
  536.     Boolean isContravariant(/* Name * */);
  537.  
  538.     /* delete the Name instance */
  539.     void erase(/* Name * */);
  540.  
  541. #endif
  542.  
  543. #pragma pack()
  544.  
  545. #endif
  546.