Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ast.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright © 2009 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. #pragma once
  24. #ifndef AST_H
  25. #define AST_H
  26. #include "main/simple_list.h"
  27. #include "list.h"
  28. #include "glsl_parser_extras.h"
  29. struct ir_instruction;
  30. struct _mesa_glsl_parse_state;
  31. struct YYLTYPE;
  32. class ast_node : public simple_node {
  33. public:
  34. virtual ~ast_node();
  35. virtual void print(void) const;
  36. virtual ir_instruction *hir(exec_list *instructions,
  37. struct _mesa_glsl_parse_state *state);
  38. /**
  39. * Retrieve the source location of an AST node
  40. *
  41. * This function is primarily used to get the source position of an AST node
  42. * into a form that can be passed to \c _mesa_glsl_error.
  43. *
  44. * \sa _mesa_glsl_error, ast_node::set_location
  45. */
  46. struct YYLTYPE get_location(void) const
  47. {
  48. struct YYLTYPE locp;
  49. locp.source = this->location.source;
  50. locp.first_line = this->location.line;
  51. locp.first_column = this->location.column;
  52. locp.last_line = locp.first_line;
  53. locp.last_column = locp.first_column;
  54. return locp;
  55. }
  56. /**
  57. * Set the source location of an AST node from a parser location
  58. *
  59. * \sa ast_node::get_location
  60. */
  61. void set_location(const struct YYLTYPE *locp)
  62. {
  63. this->location.source = locp->source;
  64. this->location.line = locp->first_line;
  65. this->location.column = locp->first_column;
  66. }
  67. int type;
  68. struct {
  69. unsigned source;
  70. unsigned line;
  71. unsigned column;
  72. } location;
  73. protected:
  74. ast_node(void);
  75. };
  76. enum ast_operators {
  77. ast_assign,
  78. ast_plus, /**< Unary + operator. */
  79. ast_neg,
  80. ast_add,
  81. ast_sub,
  82. ast_mul,
  83. ast_div,
  84. ast_mod,
  85. ast_lshift,
  86. ast_rshift,
  87. ast_less,
  88. ast_greater,
  89. ast_lequal,
  90. ast_gequal,
  91. ast_equal,
  92. ast_nequal,
  93. ast_bit_and,
  94. ast_bit_xor,
  95. ast_bit_or,
  96. ast_bit_not,
  97. ast_logic_and,
  98. ast_logic_xor,
  99. ast_logic_or,
  100. ast_logic_not,
  101. ast_mul_assign,
  102. ast_div_assign,
  103. ast_mod_assign,
  104. ast_add_assign,
  105. ast_sub_assign,
  106. ast_ls_assign,
  107. ast_rs_assign,
  108. ast_and_assign,
  109. ast_xor_assign,
  110. ast_or_assign,
  111. ast_conditional,
  112. ast_pre_inc,
  113. ast_pre_dec,
  114. ast_post_inc,
  115. ast_post_dec,
  116. ast_field_selection,
  117. ast_array_index,
  118. ast_function_call,
  119. ast_identifier,
  120. ast_int_constant,
  121. ast_uint_constant,
  122. ast_float_constant,
  123. ast_bool_constant,
  124. ast_sequence
  125. };
  126. class ast_expression : public ast_node {
  127. public:
  128. ast_expression(int oper, ast_expression *,
  129. ast_expression *, ast_expression *);
  130. static const char *operator_string(enum ast_operators op);
  131. virtual ir_instruction *hir(exec_list *instructions,
  132. struct _mesa_glsl_parse_state *state);
  133. virtual void print(void) const;
  134. enum ast_operators oper;
  135. ast_expression *subexpressions[3];
  136. union {
  137. char *identifier;
  138. int int_constant;
  139. float float_constant;
  140. unsigned uint_constant;
  141. int bool_constant;
  142. } primary_expression;
  143. /**
  144. * List of expressions for an \c ast_sequence.
  145. */
  146. struct simple_node expressions;
  147. };
  148. class ast_expression_bin : public ast_expression {
  149. public:
  150. ast_expression_bin(int oper, ast_expression *, ast_expression *);
  151. virtual void print(void) const;
  152. };
  153. /**
  154. * Number of possible operators for an ast_expression
  155. *
  156. * This is done as a define instead of as an additional value in the enum so
  157. * that the compiler won't generate spurious messages like "warning:
  158. * enumeration value ‘ast_num_operators’ not handled in switch"
  159. */
  160. #define AST_NUM_OPERATORS (ast_sequence + 1)
  161. class ast_compound_statement : public ast_node {
  162. public:
  163. ast_compound_statement(int new_scope, ast_node *statements);
  164. virtual void print(void) const;
  165. virtual ir_instruction *hir(exec_list *instructions,
  166. struct _mesa_glsl_parse_state *state);
  167. int new_scope;
  168. struct simple_node statements;
  169. };
  170. class ast_declaration : public ast_node {
  171. public:
  172. ast_declaration(char *identifier, int is_array, ast_expression *array_size,
  173. ast_expression *initializer);
  174. virtual void print(void) const;
  175. char *identifier;
  176. int is_array;
  177. ast_expression *array_size;
  178. ast_expression *initializer;
  179. };
  180. enum {
  181. ast_precision_high = 0, /**< Default precision. */
  182. ast_precision_medium,
  183. ast_precision_low
  184. };
  185. struct ast_type_qualifier {
  186. unsigned invariant:1;
  187. unsigned constant:1;
  188. unsigned attribute:1;
  189. unsigned varying:1;
  190. unsigned in:1;
  191. unsigned out:1;
  192. unsigned centroid:1;
  193. unsigned uniform:1;
  194. unsigned smooth:1;
  195. unsigned flat:1;
  196. unsigned noperspective:1;
  197. };
  198. class ast_struct_specifier : public ast_node {
  199. public:
  200. ast_struct_specifier(char *identifier, ast_node *declarator_list);
  201. virtual void print(void) const;
  202. char *name;
  203. struct simple_node declarations;
  204. };
  205. enum ast_types {
  206. ast_void,
  207. ast_float,
  208. ast_int,
  209. ast_uint,
  210. ast_bool,
  211. ast_vec2,
  212. ast_vec3,
  213. ast_vec4,
  214. ast_bvec2,
  215. ast_bvec3,
  216. ast_bvec4,
  217. ast_ivec2,
  218. ast_ivec3,
  219. ast_ivec4,
  220. ast_uvec2,
  221. ast_uvec3,
  222. ast_uvec4,
  223. ast_mat2,
  224. ast_mat2x3,
  225. ast_mat2x4,
  226. ast_mat3x2,
  227. ast_mat3,
  228. ast_mat3x4,
  229. ast_mat4x2,
  230. ast_mat4x3,
  231. ast_mat4,
  232. ast_sampler1d,
  233. ast_sampler2d,
  234. ast_sampler3d,
  235. ast_samplercube,
  236. ast_sampler1dshadow,
  237. ast_sampler2dshadow,
  238. ast_samplercubeshadow,
  239. ast_sampler1darray,
  240. ast_sampler2darray,
  241. ast_sampler1darrayshadow,
  242. ast_sampler2darrayshadow,
  243. ast_isampler1d,
  244. ast_isampler2d,
  245. ast_isampler3d,
  246. ast_isamplercube,
  247. ast_isampler1darray,
  248. ast_isampler2darray,
  249. ast_usampler1d,
  250. ast_usampler2d,
  251. ast_usampler3d,
  252. ast_usamplercube,
  253. ast_usampler1darray,
  254. ast_usampler2darray,
  255. ast_struct,
  256. ast_type_name
  257. };
  258. class ast_type_specifier : public ast_node {
  259. public:
  260. ast_type_specifier(int specifier);
  261. virtual void print(void) const;
  262. enum ast_types type_specifier;
  263. char *type_name;
  264. ast_struct_specifier *structure;
  265. int is_array;
  266. ast_expression *array_size;
  267. unsigned precision:2;
  268. };
  269. class ast_fully_specified_type : public ast_node {
  270. public:
  271. virtual void print(void) const;
  272. ast_type_qualifier qualifier;
  273. ast_type_specifier *specifier;
  274. };
  275. class ast_declarator_list : public ast_node {
  276. public:
  277. ast_declarator_list(ast_fully_specified_type *);
  278. virtual void print(void) const;
  279. virtual ir_instruction *hir(exec_list *instructions,
  280. struct _mesa_glsl_parse_state *state);
  281. ast_fully_specified_type *type;
  282. struct simple_node declarations;
  283. /**
  284. * Special flag for vertex shader "invariant" declarations.
  285. *
  286. * Vertex shaders can contain "invariant" variable redeclarations that do
  287. * not include a type. For example, "invariant gl_Position;". This flag
  288. * is used to note these cases when no type is specified.
  289. */
  290. int invariant;
  291. };
  292. class ast_parameter_declarator : public ast_node {
  293. public:
  294. virtual void print(void) const;
  295. virtual ir_instruction *hir(exec_list *instructions,
  296. struct _mesa_glsl_parse_state *state);
  297. ast_fully_specified_type *type;
  298. char *identifier;
  299. int is_array;
  300. ast_expression *array_size;
  301. };
  302. class ast_function : public ast_node {
  303. public:
  304. ast_function(void);
  305. virtual void print(void) const;
  306. ast_fully_specified_type *return_type;
  307. char *identifier;
  308. struct simple_node parameters;
  309. };
  310. class ast_declaration_statement : public ast_node {
  311. public:
  312. ast_declaration_statement(void);
  313. enum {
  314. ast_function,
  315. ast_declaration,
  316. ast_precision
  317. } mode;
  318. union {
  319. class ast_function *function;
  320. ast_declarator_list *declarator;
  321. ast_type_specifier *type;
  322. ast_node *node;
  323. } declaration;
  324. };
  325. class ast_expression_statement : public ast_node {
  326. public:
  327. ast_expression_statement(ast_expression *);
  328. virtual void print(void) const;
  329. virtual ir_instruction *hir(exec_list *instructions,
  330. struct _mesa_glsl_parse_state *state);
  331. ast_expression *expression;
  332. };
  333. class ast_case_label : public ast_node {
  334. public:
  335. /**
  336. * An expression of NULL means 'default'.
  337. */
  338. ast_expression *expression;
  339. };
  340. class ast_selection_statement : public ast_node {
  341. public:
  342. ast_selection_statement(ast_expression *condition,
  343. ast_node *then_statement,
  344. ast_node *else_statement);
  345. virtual void print(void) const;
  346. ast_expression *condition;
  347. ast_node *then_statement;
  348. ast_node *else_statement;
  349. };
  350. class ast_switch_statement : public ast_node {
  351. public:
  352. ast_expression *expression;
  353. struct simple_node statements;
  354. };
  355. class ast_iteration_statement : public ast_node {
  356. public:
  357. ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
  358. ast_expression *rest_expression, ast_node *body);
  359. virtual void print(void) const;
  360. enum ast_iteration_modes {
  361. ast_for,
  362. ast_while,
  363. ast_do_while
  364. } mode;
  365. ast_node *init_statement;
  366. ast_node *condition;
  367. ast_expression *rest_expression;
  368. ast_node *body;
  369. };
  370. class ast_jump_statement : public ast_node {
  371. public:
  372. ast_jump_statement(int mode, ast_expression *return_value);
  373. virtual void print(void) const;
  374. enum ast_jump_modes {
  375. ast_continue,
  376. ast_break,
  377. ast_return,
  378. ast_discard
  379. } mode;
  380. ast_expression *opt_return_value;
  381. };
  382. class ast_function_definition : public ast_node {
  383. public:
  384. virtual void print(void) const;
  385. virtual ir_instruction *hir(exec_list *instructions,
  386. struct _mesa_glsl_parse_state *state);
  387. ast_function *prototype;
  388. ast_compound_statement *body;
  389. };
  390. extern struct ir_instruction *
  391. _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
  392. exec_list *instructions,
  393. struct _mesa_glsl_parse_state *state);
  394. #endif /* AST_H */