glcpp.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef GLCPP_H
  24. #define GLCPP_H
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include "../ralloc.h"
  28. #include "program/hash_table.h"
  29. #define yyscan_t void*
  30. /* Some data types used for parser values. */
  31. typedef struct string_node {
  32. const char *str;
  33. struct string_node *next;
  34. } string_node_t;
  35. typedef struct string_list {
  36. string_node_t *head;
  37. string_node_t *tail;
  38. } string_list_t;
  39. typedef struct token token_t;
  40. typedef struct token_list token_list_t;
  41. typedef union YYSTYPE
  42. {
  43. // Could be int, but results in some bugs with parsing of #version directives
  44. // in Apple LLVM Compiler 4.2 when building for 32 bit.
  45. int ival;
  46. char *str;
  47. string_list_t *string_list;
  48. token_t *token;
  49. token_list_t *token_list;
  50. } YYSTYPE;
  51. # define YYSTYPE_IS_TRIVIAL 1
  52. # define YYSTYPE_IS_DECLARED 1
  53. typedef struct YYLTYPE {
  54. int first_line;
  55. int first_column;
  56. int last_line;
  57. int last_column;
  58. unsigned source;
  59. } YYLTYPE;
  60. # define YYLTYPE_IS_DECLARED 1
  61. # define YYLTYPE_IS_TRIVIAL 1
  62. # define YYLLOC_DEFAULT(Current, Rhs, N) \
  63. do { \
  64. if (N) \
  65. { \
  66. (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
  67. (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
  68. (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
  69. (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
  70. } \
  71. else \
  72. { \
  73. (Current).first_line = (Current).last_line = \
  74. YYRHSLOC(Rhs, 0).last_line; \
  75. (Current).first_column = (Current).last_column = \
  76. YYRHSLOC(Rhs, 0).last_column; \
  77. } \
  78. (Current).source = 0; \
  79. } while (0)
  80. struct token {
  81. int type;
  82. YYSTYPE value;
  83. YYLTYPE location;
  84. };
  85. typedef struct token_node {
  86. token_t *token;
  87. struct token_node *next;
  88. } token_node_t;
  89. struct token_list {
  90. token_node_t *head;
  91. token_node_t *tail;
  92. token_node_t *non_space_tail;
  93. };
  94. typedef struct argument_node {
  95. token_list_t *argument;
  96. struct argument_node *next;
  97. } argument_node_t;
  98. typedef struct argument_list {
  99. argument_node_t *head;
  100. argument_node_t *tail;
  101. } argument_list_t;
  102. typedef struct glcpp_parser glcpp_parser_t;
  103. typedef enum {
  104. TOKEN_CLASS_IDENTIFIER,
  105. TOKEN_CLASS_IDENTIFIER_FINALIZED,
  106. TOKEN_CLASS_FUNC_MACRO,
  107. TOKEN_CLASS_OBJ_MACRO
  108. } token_class_t;
  109. token_class_t
  110. glcpp_parser_classify_token (glcpp_parser_t *parser,
  111. const char *identifier,
  112. int *parameter_index);
  113. typedef struct {
  114. int is_function;
  115. string_list_t *parameters;
  116. const char *identifier;
  117. token_list_t *replacements;
  118. } macro_t;
  119. typedef struct expansion_node {
  120. macro_t *macro;
  121. token_node_t *replacements;
  122. struct expansion_node *next;
  123. } expansion_node_t;
  124. typedef enum skip_type {
  125. SKIP_NO_SKIP,
  126. SKIP_TO_ELSE,
  127. SKIP_TO_ENDIF
  128. } skip_type_t;
  129. typedef struct skip_node {
  130. skip_type_t type;
  131. YYLTYPE loc; /* location of the initial #if/#elif/... */
  132. struct skip_node *next;
  133. } skip_node_t;
  134. typedef struct active_list {
  135. const char *identifier;
  136. token_node_t *marker;
  137. struct active_list *next;
  138. } active_list_t;
  139. struct glcpp_parser {
  140. yyscan_t scanner;
  141. struct hash_table *defines;
  142. active_list_t *active;
  143. int lexing_if;
  144. int space_tokens;
  145. int newline_as_space;
  146. int in_control_line;
  147. int paren_count;
  148. skip_node_t *skip_stack;
  149. token_list_t *lex_from_list;
  150. token_node_t *lex_from_node;
  151. char *output;
  152. char *info_log;
  153. size_t output_length;
  154. size_t info_log_length;
  155. int error;
  156. bool has_new_line_number;
  157. int new_line_number;
  158. bool has_new_source_number;
  159. int new_source_number;
  160. };
  161. struct gl_extensions;
  162. glcpp_parser_t *
  163. glcpp_parser_create (const struct gl_extensions *extensions, int api);
  164. int
  165. glcpp_parser_parse (glcpp_parser_t *parser);
  166. void
  167. glcpp_parser_destroy (glcpp_parser_t *parser);
  168. int
  169. glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
  170. const struct gl_extensions *extensions, int api);
  171. /* Functions for writing to the info log */
  172. void
  173. glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
  174. void
  175. glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
  176. /* Generated by glcpp-lex.l to glcpp-lex.c */
  177. int
  178. glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
  179. void
  180. glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
  181. int
  182. glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
  183. int
  184. glcpp_lex_destroy (yyscan_t scanner);
  185. /* Generated by glcpp-parse.y to glcpp-parse.c */
  186. int
  187. yyparse (glcpp_parser_t *parser);
  188. #endif