preproc.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * \file libyasm/preproc.h
  3. * \brief YASM preprocessor module interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 Peter Johnson
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. * \endlicense
  29. */
  30. #ifndef YASM_PREPROC_H
  31. #define YASM_PREPROC_H
  32. #ifndef YASM_DOXYGEN
  33. /** Base #yasm_preproc structure. Must be present as the first element in any
  34. * #yasm_preproc implementation.
  35. */
  36. typedef struct yasm_preproc_base {
  37. /** #yasm_preproc_module implementation for this preprocessor. */
  38. const struct yasm_preproc_module *module;
  39. } yasm_preproc_base;
  40. #endif
  41. /** YASM preprocesor module interface. */
  42. typedef struct yasm_preproc_module {
  43. /** One-line description of the preprocessor. */
  44. const char *name;
  45. /** Keyword used to select preprocessor on the command line. */
  46. const char *keyword;
  47. /** Create preprocessor.
  48. * Module-level implementation of yasm_preproc_create().
  49. * Call yasm_preproc_create() instead of calling this function.
  50. *
  51. * \param in_filename initial starting filename, or "-" to read from
  52. * stdin
  53. * \param symtab symbol table (may be NULL if none)
  54. * \param lm line mapping repository
  55. * \param errwarns error/warnning set.
  56. * \return New preprocessor.
  57. *
  58. * \note Any preprocessor errors and warnings are stored into errwarns.
  59. */
  60. /*@only@*/ yasm_preproc * (*create) (const char *in_filename,
  61. yasm_symtab *symtab,
  62. yasm_linemap *lm,
  63. yasm_errwarns *errwarns);
  64. /** Module-level implementation of yasm_preproc_destroy().
  65. * Call yasm_preproc_destroy() instead of calling this function.
  66. */
  67. void (*destroy) (/*@only@*/ yasm_preproc *preproc);
  68. /** Module-level implementation of yasm_preproc_get_line().
  69. * Call yasm_preproc_get_line() instead of calling this function.
  70. */
  71. char * (*get_line) (yasm_preproc *preproc);
  72. /** Module-level implementation of yasm_preproc_get_included_file().
  73. * Call yasm_preproc_get_included_file() instead of calling this function.
  74. */
  75. size_t (*get_included_file) (yasm_preproc *preproc, /*@out@*/ char *buf,
  76. size_t max_size);
  77. /** Module-level implementation of yasm_preproc_add_include_file().
  78. * Call yasm_preproc_add_include_file() instead of calling this function.
  79. */
  80. void (*add_include_file) (yasm_preproc *preproc, const char *filename);
  81. /** Module-level implementation of yasm_preproc_predefine_macro().
  82. * Call yasm_preproc_predefine_macro() instead of calling this function.
  83. */
  84. void (*predefine_macro) (yasm_preproc *preproc, const char *macronameval);
  85. /** Module-level implementation of yasm_preproc_undefine_macro().
  86. * Call yasm_preproc_undefine_macro() instead of calling this function.
  87. */
  88. void (*undefine_macro) (yasm_preproc *preproc, const char *macroname);
  89. /** Module-level implementation of yasm_preproc_builtin_define().
  90. * Call yasm_preproc_builtin_define() instead of calling this function.
  91. */
  92. void (*define_builtin) (yasm_preproc *preproc, const char *macronameval);
  93. /** Module-level implementation of yasm_preproc_add_standard().
  94. * Call yasm_preproc_add_standard() instead of calling this function.
  95. */
  96. void (*add_standard) (yasm_preproc *preproc, const char **macros);
  97. } yasm_preproc_module;
  98. /** Initialize preprocessor.
  99. * The preprocessor needs access to the object format module to find out
  100. * any output format specific macros.
  101. * \param module preprocessor module
  102. * \param in_filename initial starting filename, or "-" to read from stdin
  103. * \param symtab symbol table (may be NULL if none)
  104. * \param lm line mapping repository
  105. * \param errwarns error/warning set
  106. * \return New preprocessor.
  107. * \note Errors/warnings are stored into errwarns.
  108. */
  109. /*@only@*/ yasm_preproc *yasm_preproc_create
  110. (yasm_preproc_module *module, const char *in_filename,
  111. yasm_symtab *symtab, yasm_linemap *lm, yasm_errwarns *errwarns);
  112. /** Cleans up any allocated preproc memory.
  113. * \param preproc preprocessor
  114. */
  115. void yasm_preproc_destroy(/*@only@*/ yasm_preproc *preproc);
  116. /** Gets a single line of preprocessed source code.
  117. * \param preproc preprocessor
  118. * \return Allocated line of code, without the trailing \n.
  119. */
  120. char *yasm_preproc_get_line(yasm_preproc *preproc);
  121. /** Get the next filename included by the source code.
  122. * \param preproc preprocessor
  123. * \param buf destination buffer for filename
  124. * \param max_size maximum number of bytes that can be returned in buf
  125. * \return Actual number of bytes returned in buf.
  126. */
  127. size_t yasm_preproc_get_included_file(yasm_preproc *preproc,
  128. /*@out@*/ char *buf, size_t max_size);
  129. /** Pre-include a file.
  130. * \param preproc preprocessor
  131. * \param filename filename
  132. */
  133. void yasm_preproc_add_include_file(yasm_preproc *preproc,
  134. const char *filename);
  135. /** Pre-define a macro.
  136. * \param preproc preprocessor
  137. * \param macronameval "name=value" string
  138. */
  139. void yasm_preproc_predefine_macro(yasm_preproc *preproc,
  140. const char *macronameval);
  141. /** Un-define a macro.
  142. * \param preproc preprocessor
  143. * \param macroname macro name
  144. */
  145. void yasm_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname);
  146. /** Define a builtin macro, preprocessed before the "standard" macros.
  147. * \param preproc preprocessor
  148. * \param macronameval "name=value" string
  149. */
  150. void yasm_preproc_define_builtin(yasm_preproc *preproc,
  151. const char *macronameval);
  152. /** Define additional standard macros, preprocessed after the builtins but
  153. * prior to any user-defined macros.
  154. * \param preproc preprocessor
  155. * \param macros NULL-terminated array of macro strings
  156. */
  157. void yasm_preproc_add_standard(yasm_preproc *preproc,
  158. const char **macros);
  159. #ifndef YASM_DOXYGEN
  160. /* Inline macro implementations for preproc functions */
  161. #define yasm_preproc_create(module, in_filename, symtab, lm, ews) \
  162. module->create(in_filename, symtab, lm, ews)
  163. #define yasm_preproc_destroy(preproc) \
  164. ((yasm_preproc_base *)preproc)->module->destroy(preproc)
  165. #define yasm_preproc_get_line(preproc) \
  166. ((yasm_preproc_base *)preproc)->module->get_line(preproc)
  167. #define yasm_preproc_get_included_file(preproc, buf, max_size) \
  168. ((yasm_preproc_base *)preproc)->module->get_included_file(preproc, buf, max_size)
  169. #define yasm_preproc_add_include_file(preproc, filename) \
  170. ((yasm_preproc_base *)preproc)->module->add_include_file(preproc, filename)
  171. #define yasm_preproc_predefine_macro(preproc, macronameval) \
  172. ((yasm_preproc_base *)preproc)->module->predefine_macro(preproc, \
  173. macronameval)
  174. #define yasm_preproc_undefine_macro(preproc, macroname) \
  175. ((yasm_preproc_base *)preproc)->module->undefine_macro(preproc, macroname)
  176. #define yasm_preproc_define_builtin(preproc, macronameval) \
  177. ((yasm_preproc_base *)preproc)->module->define_builtin(preproc, \
  178. macronameval)
  179. #define yasm_preproc_add_standard(preproc, macros) \
  180. ((yasm_preproc_base *)preproc)->module->add_standard(preproc, \
  181. macros)
  182. #endif
  183. #endif