Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ast.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. ast_expression(const char *identifier) :
  131. oper(ast_identifier)
  132. {
  133. subexpressions[0] = NULL;
  134. subexpressions[1] = NULL;
  135. subexpressions[2] = NULL;
  136. primary_expression.identifier = (char *) identifier;
  137. }
  138. static const char *operator_string(enum ast_operators op);
  139. virtual ir_instruction *hir(exec_list *instructions,
  140. struct _mesa_glsl_parse_state *state);
  141. virtual void print(void) const;
  142. enum ast_operators oper;
  143. ast_expression *subexpressions[3];
  144. union {
  145. char *identifier;
  146. int int_constant;
  147. float float_constant;
  148. unsigned uint_constant;
  149. int bool_constant;
  150. } primary_expression;
  151. /**
  152. * List of expressions for an \c ast_sequence.
  153. */
  154. struct simple_node expressions;
  155. };
  156. class ast_expression_bin : public ast_expression {
  157. public:
  158. ast_expression_bin(int oper, ast_expression *, ast_expression *);
  159. virtual void print(void) const;
  160. };
  161. /**
  162. * Number of possible operators for an ast_expression
  163. *
  164. * This is done as a define instead of as an additional value in the enum so
  165. * that the compiler won't generate spurious messages like "warning:
  166. * enumeration value ‘ast_num_operators’ not handled in switch"
  167. */
  168. #define AST_NUM_OPERATORS (ast_sequence + 1)
  169. class ast_compound_statement : public ast_node {
  170. public:
  171. ast_compound_statement(int new_scope, ast_node *statements);
  172. virtual void print(void) const;
  173. virtual ir_instruction *hir(exec_list *instructions,
  174. struct _mesa_glsl_parse_state *state);
  175. int new_scope;
  176. struct simple_node statements;
  177. };
  178. class ast_declaration : public ast_node {
  179. public:
  180. ast_declaration(char *identifier, int is_array, ast_expression *array_size,
  181. ast_expression *initializer);
  182. virtual void print(void) const;
  183. char *identifier;
  184. int is_array;
  185. ast_expression *array_size;
  186. ast_expression *initializer;
  187. };
  188. enum {
  189. ast_precision_high = 0, /**< Default precision. */
  190. ast_precision_medium,
  191. ast_precision_low
  192. };
  193. struct ast_type_qualifier {
  194. unsigned invariant:1;
  195. unsigned constant:1;
  196. unsigned attribute:1;
  197. unsigned varying:1;
  198. unsigned in:1;
  199. unsigned out:1;
  200. unsigned centroid:1;
  201. unsigned uniform:1;
  202. unsigned smooth:1;
  203. unsigned flat:1;
  204. unsigned noperspective:1;
  205. };
  206. class ast_struct_specifier : public ast_node {
  207. public:
  208. ast_struct_specifier(char *identifier, ast_node *declarator_list);
  209. virtual void print(void) const;
  210. char *name;
  211. struct simple_node declarations;
  212. };
  213. enum ast_types {
  214. ast_void,
  215. ast_float,
  216. ast_int,
  217. ast_uint,
  218. ast_bool,
  219. ast_vec2,
  220. ast_vec3,
  221. ast_vec4,
  222. ast_bvec2,
  223. ast_bvec3,
  224. ast_bvec4,
  225. ast_ivec2,
  226. ast_ivec3,
  227. ast_ivec4,
  228. ast_uvec2,
  229. ast_uvec3,
  230. ast_uvec4,
  231. ast_mat2,
  232. ast_mat2x3,
  233. ast_mat2x4,
  234. ast_mat3x2,
  235. ast_mat3,
  236. ast_mat3x4,
  237. ast_mat4x2,
  238. ast_mat4x3,
  239. ast_mat4,
  240. ast_sampler1d,
  241. ast_sampler2d,
  242. ast_sampler3d,
  243. ast_samplercube,
  244. ast_sampler1dshadow,
  245. ast_sampler2dshadow,
  246. ast_samplercubeshadow,
  247. ast_sampler1darray,
  248. ast_sampler2darray,
  249. ast_sampler1darrayshadow,
  250. ast_sampler2darrayshadow,
  251. ast_isampler1d,
  252. ast_isampler2d,
  253. ast_isampler3d,
  254. ast_isamplercube,
  255. ast_isampler1darray,
  256. ast_isampler2darray,
  257. ast_usampler1d,
  258. ast_usampler2d,
  259. ast_usampler3d,
  260. ast_usamplercube,
  261. ast_usampler1darray,
  262. ast_usampler2darray,
  263. ast_struct,
  264. ast_type_name
  265. };
  266. class ast_type_specifier : public ast_node {
  267. public:
  268. ast_type_specifier(int specifier);
  269. virtual void print(void) const;
  270. enum ast_types type_specifier;
  271. char *type_name;
  272. ast_struct_specifier *structure;
  273. int is_array;
  274. ast_expression *array_size;
  275. unsigned precision:2;
  276. };
  277. class ast_fully_specified_type : public ast_node {
  278. public:
  279. virtual void print(void) const;
  280. ast_type_qualifier qualifier;
  281. ast_type_specifier *specifier;
  282. };
  283. class ast_declarator_list : public ast_node {
  284. public:
  285. ast_declarator_list(ast_fully_specified_type *);
  286. virtual void print(void) const;
  287. virtual ir_instruction *hir(exec_list *instructions,
  288. struct _mesa_glsl_parse_state *state);
  289. ast_fully_specified_type *type;
  290. struct simple_node declarations;
  291. /**
  292. * Special flag for vertex shader "invariant" declarations.
  293. *
  294. * Vertex shaders can contain "invariant" variable redeclarations that do
  295. * not include a type. For example, "invariant gl_Position;". This flag
  296. * is used to note these cases when no type is specified.
  297. */
  298. int invariant;
  299. };
  300. class ast_parameter_declarator : public ast_node {
  301. public:
  302. virtual void print(void) const;
  303. virtual ir_instruction *hir(exec_list *instructions,
  304. struct _mesa_glsl_parse_state *state);
  305. ast_fully_specified_type *type;
  306. char *identifier;
  307. int is_array;
  308. ast_expression *array_size;
  309. };
  310. class ast_function : public ast_node {
  311. public:
  312. ast_function(void);
  313. virtual void print(void) const;
  314. ast_fully_specified_type *return_type;
  315. char *identifier;
  316. struct simple_node parameters;
  317. };
  318. class ast_declaration_statement : public ast_node {
  319. public:
  320. ast_declaration_statement(void);
  321. enum {
  322. ast_function,
  323. ast_declaration,
  324. ast_precision
  325. } mode;
  326. union {
  327. class ast_function *function;
  328. ast_declarator_list *declarator;
  329. ast_type_specifier *type;
  330. ast_node *node;
  331. } declaration;
  332. };
  333. class ast_expression_statement : public ast_node {
  334. public:
  335. ast_expression_statement(ast_expression *);
  336. virtual void print(void) const;
  337. virtual ir_instruction *hir(exec_list *instructions,
  338. struct _mesa_glsl_parse_state *state);
  339. ast_expression *expression;
  340. };
  341. class ast_case_label : public ast_node {
  342. public:
  343. /**
  344. * An expression of NULL means 'default'.
  345. */
  346. ast_expression *expression;
  347. };
  348. class ast_selection_statement : public ast_node {
  349. public:
  350. ast_selection_statement(ast_expression *condition,
  351. ast_node *then_statement,
  352. ast_node *else_statement);
  353. virtual void print(void) const;
  354. ast_expression *condition;
  355. ast_node *then_statement;
  356. ast_node *else_statement;
  357. };
  358. class ast_switch_statement : public ast_node {
  359. public:
  360. ast_expression *expression;
  361. struct simple_node statements;
  362. };
  363. class ast_iteration_statement : public ast_node {
  364. public:
  365. ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
  366. ast_expression *rest_expression, ast_node *body);
  367. virtual void print(void) const;
  368. enum ast_iteration_modes {
  369. ast_for,
  370. ast_while,
  371. ast_do_while
  372. } mode;
  373. ast_node *init_statement;
  374. ast_node *condition;
  375. ast_expression *rest_expression;
  376. ast_node *body;
  377. };
  378. class ast_jump_statement : public ast_node {
  379. public:
  380. ast_jump_statement(int mode, ast_expression *return_value);
  381. virtual void print(void) const;
  382. enum ast_jump_modes {
  383. ast_continue,
  384. ast_break,
  385. ast_return,
  386. ast_discard
  387. } mode;
  388. ast_expression *opt_return_value;
  389. };
  390. class ast_function_definition : public ast_node {
  391. public:
  392. virtual void print(void) const;
  393. virtual ir_instruction *hir(exec_list *instructions,
  394. struct _mesa_glsl_parse_state *state);
  395. ast_function *prototype;
  396. ast_compound_statement *body;
  397. };
  398. extern void
  399. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
  400. extern struct ir_instruction *
  401. _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
  402. exec_list *instructions,
  403. struct _mesa_glsl_parse_state *state);
  404. #endif /* AST_H */