Logo Search packages:      
Sourcecode: mingw-w64 version File versions  Download package

m_token.h

/*
   Copyright (c) 2009, 2010 mingw-w64 project

   Contributing authors: Kai Tietz, Jonathan Yong

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/
#ifndef _M_TOKEN_HXX
#define _M_TOKEN_HXX

/**
 * Garbage collector elements.
 * Tracks allocated memory and points to the next element from the same context.
 * @see sGcCtx
 */
00032 typedef struct sGcElem {
00033   struct sGcElem *chain;        /**< Next element in chain. */
00034   size_t length;                /**< Size of allocated memory (excluding sGcElem and sGcCtx). */
00035   char dta[1];                  /**< Starting adress marker of requested memory. */
} sGcElem;

/**
 * Garbage collector context.
 * Tracks first and last of elements in gc context.
 * @see generate_gc()
 * @see release_gc()
 */
typedef struct sGcCtx {
00045   sGcElem *head;                /**< Pointer to first gc element in context.*/
00046   sGcElem *tail;                /**< Pointer to last gc element in context. */
} sGcCtx;

/**
 * Token "Kind" enumeration list.
 * @see gen_tok()
 * @see eMSToken
 * @see sMToken_base
 */
typedef enum eMToken {
  eMToken_none = 0,                /**< Token type: None.  */
  eMToken_value = 1,               /**< Token type: Value. */
  eMToken_name = 2,                /**< Token type: Name.  */
  eMToken_dim = 3,                 /**< Token type: Dim.   */
  eMToken_unary = 4,               /**< Token type: Unary  */
  eMToken_binary = 5,              /**< Token type: Binary */
  eMToken_MAX                      /**< Unused sentinel.   */
} eMToken;

/**
 * Token "Subkind" enumeration list.
 * Also used by internal function sprint_decl1() for printing.
 * @see gen_tok()
 * @see eMToken
 * @see sMToken_base
 */
typedef enum eMSToken {
  eMST_unmangled = 0,               /**< Name is unmagled. */
  eMST_nttp = 1,                    /**< Template name. */
  eMST_name = 2,                    /**< Decoded function name. */
  eMST_colon = 3,                   /**< Class member accessibility. */
  eMST_rtti = 4,                    /**< Runtime Type information name. */
  eMST_cv = 5,                      /**< Function call convention / data qualifiers / pointer. */
  eMST_vftable = 6,                 /**< Virtual Function Table. */
  eMST_vbtable = 7,                 /**< Virtual Base Table. */
  eMST_vcall = 8,                   /**< Virtual Function Call. */
  eMST_opname = 9,                  /**< Overloaded operator. */
  eMST_templargname = 10,           /**< Explicit template arg name. */
  eMST_type = 11,                   /**< Function return type. */
  eMST_dim,                         /**< Print array-like expression. @see eMToken_dim */
  eMST_val,                         /**< Print value expression. @see sMToken_val */
  eMST_gcarray, /* __gc[,,,,] */    /**< MSVC extenstion: "__gc" Managed C++ reference. */
  eMST_slashed,                     /**< MTOKEN_UNARY appended and prepended with "/". */
  eMST_array,                       /**< MTOKEN_UNARY enclosed by square brackets. */
  eMST_element,                     /**< MTOKEN_UNARY in an argument list. */
  eMST_template_argument_list,      /**< MTOKEN_UNARY in an argument list. */
  eMST_ltgt,                        /**< MTOKEN_UNARY enclosed by angular brackets. */
  eMST_frame,                       /**< MTOKEN_UNARY enclosed by curly brackets. */
  eMST_throw,                       /**< MTOKEN_UNARY prepended by "throw ". */
  eMST_rframe,                      /**< MTOKEN_UNARY enclosed by parentheses. */
  eMST_destructor,                  /**< MTOKEN_UNARY prepended with "~". */
  eMST_oper,                        /**< indicates that token an operand, prints from MTOKEN_UNARY. */
  eMST_colonarray, /* ::[] */       /**< Unused, to be removed. */
  eMST_lexical_frame,               /**< MTOKEN_UNARY enclosed by single quotes "'". */
  eMST_scope,                       /**< MTOKEN_UNARY, unenclosed. */
  eMST_udt_returning,               /**< User defined types (RTTI). */
  eMST_coloncolon,                  /**< "::" between MTOKEN_BINARY_LEFT and MTOKEN_BINARY_RIGHT. */
  eMST_assign,                      /**< "=" between MTOKEN_BINARY_LEFT and MTOKEN_BINARY_RIGHT and appended with "}". */
  eMST_templateparam,               /**< Explicit template. */
  eMST_nonetypetemplateparam,       /**< Non-explicit template. */
  eMST_exp,                         /**< dim 'e' (exponent) dim */
  eMST_combine,                     /**< Unary grouping. */
  eMST_ecsu,                        /**< Is an Enum/Class/Struct/Union */
  eMST_based                       /**< MSVC extension: "__based" Based addressing */
} eMSToken;

/**
 * Token base descriptor header.
 * Descibes the type of token being processed.
 */
00116 typedef struct sMToken_base {
00117   enum eMToken kind;            /**< Token kind. */
00118   enum eMSToken subkind;        /**< Token Subkind. */
00119   union uMToken *chain;         /**< Pointer to next token. NULL terminated. */
00120   int flags;                    /**< Token flags. */
} sMToken_base;

/**Sets the token kind, @a PT pointer to a base uMToken. */
#define MTOKEN_KIND(PT)       ((PT)->base.kind)

/**Sets the token subkind, @a PT pointer to a base uMToken. */
#define MTOKEN_SUBKIND(PT)    ((PT)->base.subkind)

/**Sets the pointer to the next token in the chain. */
#define MTOKEN_CHAIN(PT)      ((PT)->base.chain)

/**Sets flags in base descriptor. */
#define MTOKEN_FLAGS(PT)      ((PT)->base.flags)

#define MTOKEN_FLAGS_UDC    0x1 /**< Indicates a following "name" token for named struct/union/class. */
#define MTOKEN_FLAGS_NOTE   0x2 /**< Contains "note" name token.*/
#define MTOKEN_FLAGS_PTRREF 0x4 /**< Decoded fragment is a referrence. */
#define MTOKEN_FLAGS_ARRAY  0x8 /**< Decoded fragment has an array-like expression.*/

/**
 * "value" token.
 * Contains numerical expressions for decoded names.
 * @see sMToken_dim
 */
00145 typedef struct sMToken_value {
00146   sMToken_base base;            /**< Base descriptor header. */
00147   uint64_t value;               /**< Integer value. */
00148   uint64_t size : 5;            /**< Byte width of value. */
00149   uint64_t is_signed : 1;       /**< Value signed bit */
} sMToken_value;

/**Sets the token value. @a PT pointer to a value uMToken.*/
#define MTOKEN_VALUE(PT)      ((PT)->value.value)

/**Sets the signed bit on value token. @a PT pointer to a value uMToken.*/
#define MTOKEN_VALUE_SIGNED(PT)     ((PT)->value.is_signed)

/**Sets the byte width of value in value token. @a PT pointer to a value uMToken.*/
#define MTOKEN_VALUE_SIZE(PT) ((PT)->value.size)

/**
 * "name" token.
 * Contains text string expressions of the decoded fragment.
 */
00165 typedef struct sMToken_name {
00166   sMToken_base base;            /**< Base descriptor header. */
00167   char name[1];                 /**< Pointer to text string.*/
} sMToken_name;

/** Retrieve or set the name string, @a PT pointer to a name uMToken */
#define MTOKEN_NAME(PT)       ((PT)->name.name)

/**
 * "dim" token.
 * Contains array-like expressions in decoded names.
 */
00177 typedef struct sMToken_dim {
00178   sMToken_base base;            /**< Base descriptor header. */
00179   union uMToken *value;         /**< Pointer to value token. */
00180   union uMToken *non_tt_param;  /**< Pointer to C++ template name token. */
00181   int beNegate;                 /**< 1 for negative "values". */
} sMToken_dim;

/** Retrieve or set the value of a token, @a PT pointer to a value uMToken */
#define MTOKEN_DIM_VALUE(PT)  ((PT)->dim.value)

/** Retrieve or set the template of a token, @a PT pointer to a name uMToken */
#define MTOKEN_DIM_NTTP(PT)   ((PT)->dim.non_tt_param)

/** Retrieve or set negative bit on value token, @a PT pointer to an generic uMToken */
#define MTOKEN_DIM_NEGATE(PT) ((PT)->dim.beNegate)

/**
 * Unary node token.
 * Contains a pointer to a single generic leaf element.
 */
00197 typedef struct sMToken_Unary
{
00199   sMToken_base base;            /**< Base descriptor header. */
00200   union uMToken *unary;         /**< Pointer to leaf element. */
} sMToken_Unary;

/**Sets the leaf element on a unary token, @a PT pointer to a unary uMToken. */
#define MTOKEN_UNARY(PT)      ((PT)->unary.unary)

/** 
 * Binary node token.
 * Contains pointers to any 2 generic token instances as child node elements. 
 * May act as a connector for decoded C++ names.
 */
00211 typedef struct sMToken_binary {
00212   sMToken_base base;            /**< Base descriptor header. */
00213   union uMToken *left;          /**< Left node element. */
00214   union uMToken *right;         /**< Right node element. */
} sMToken_binary;

/**Sets the left node on binary token, @a PT pointer to a binary uMToken. */
#define MTOKEN_BINARY_LEFT(PT)            ((PT)->binary.left)

/**Sets the right node on binary token, @a PT pointer to a binary uMToken. */
#define MTOKEN_BINARY_RIGHT(PT)           ((PT)->binary.right)

/**
 * Generic token instances.
 * Type of token determined by base descriptor in members.
 * Base descriptor header available in all members through type punning.
 * @see gen_tok()
 */
00229 typedef union uMToken {
00230   sMToken_base base;        /**< Base descriptor header. */
00231   sMToken_value value;      /**< "value" token. */
00232   sMToken_name name;        /**< "name" token. */
00233   sMToken_dim dim;          /**< "dim" token */
00234   sMToken_Unary unary;      /**< Unary node token. */
00235   sMToken_binary binary;    /**< Binary node token. */
} uMToken;

/**
 * gen_tok constructs uMToken instances
 * Instances are destroyed with release_gc().
 * @param[in] gc Pointer to garbage collection context.
 * @param[in] kind Kind of token to construct
 * @param[in] subkind Subkind of token to construct
 * @param[in] addend Additional byte padding at the end.
 * @see release_gc()
 */
uMToken *gen_tok (sGcCtx *gc, enum eMToken kind, enum eMSToken subkind, size_t addend);

/**
 * Releases memory tracked by context.
 * @param[in] gc Garbage collection context to work on.
 * @see generate_gc()
 */
void release_gc (sGcCtx *gc);

/**
 * Constructs a garbage collection context token.
 * @return Pointer to context.
 * @see release_gc()
 */
sGcCtx *generate_gc (void);

/**
 * Chains uMTokens together.
 * @param[in] l uMtoken chain to link up with.
 * @param[in] add uMtoken to add to chain.
 * @return @a l unchanged
 */
uMToken *chain_tok (uMToken *l, uMToken *add);

/**
 * Dumps uMToken to a file descriptor for debugging.
 * @param[in] fp File descriptor to print the token to.
 * @param[in] p uMToken chain to print.
 */
void dump_tok (FILE *fp, uMToken *p);

/** 
 * Prints C++ name to file descriptor.
 * @param[in] fp Output file descriptor.
 * @param[in] p Token containing information about the C++ name.
 * @see decode_ms_name()
 */
void print_decl (FILE *fp, uMToken *p);

/** 
 * Get pointer to decoded C++ name string.
 * Use free() to release returned string.
 * @param[in] r C++ name token.
 * @return pointer to decoded C++ name string.
 * @see decode_ms_name()
 */
char *sprint_decl (uMToken *r);

/**
 * Constructs a "value" kind token.
 * @param[in] gc Pointer to garbage collection context.
 * @param[in] skind Token subkind.
 * @param[in] val Sets the value on token,
 * @param[in] is_signed Signed bit of \a val.
 * @param[in] size Width of \a val.
 * @return Pointer to value token.
 * @see sMToken_value
 */
uMToken *gen_value (sGcCtx *gc, enum eMSToken skind, uint64_t val, int is_signed, int size);

/**
 * Constructs a "name" kind token.
 * @param[in] gc Pointer to garbage collection context.
 * @param[in] skind Token subkind.
 * @param[in] name Pointer to name string.
 * @return Pointer to name token.
 * @see sMToken_name
 */
uMToken *gen_name (sGcCtx *gc, enum eMSToken skind, const char *name);

/**
 * Constructs a "dim" kind token.
 * @param[in] gc Pointer to garbage collection context.
 * @param[in] skind Token subkind.
 * @param[in] val Token numerical value.
 * @param[in] non_tt_param pointer to decoded C++ template name.
 * @param[in] fSigned Signedness of the numerical value.
 * @param[in] fNegate 1 for "val" is negative digit.
 * @return Pointer to dim token.
 * @see sMToken_dim
 */
uMToken *gen_dim (sGcCtx *gc, enum eMSToken skind, uint64_t val, const char *non_tt_param, int fSigned, int fNegate);

/**
 * Constructs a "unary" kind token.
 * @param[in] gc Pointer to garbage collection context.
 * @param[in] skind Token subkind.
 * @param[in] un Pointer to leaf element.
 * @return Pointer to a unary token.
 * @see sMToken_unary
 */
uMToken *gen_unary (sGcCtx *gc, enum eMSToken skind, uMToken *un);

/**
 * Generates a binary node token.
 * @param[in] gc Pointer to garbage collection context.
 * @param[in] skind Token subKind.
 * @param[in] l Left node element.
 * @param[in] r Right node element.
 * @return Pointer to binary token.
 * @see sMToken_binary
 */
uMToken *gen_binary (sGcCtx *gc, enum eMSToken skind, uMToken *l, uMToken *r);

#endif

Generated by  Doxygen 1.6.0   Back to index