Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2009 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #ifndef AST_H
  26. #define AST_H
  27. #include "main/simple_list.h"
  28. #include "list.h"
  29. #include "glsl_parser_extras.h"
  30. struct ir_instruction;
  31. struct _mesa_glsl_parse_state;
  32. struct YYLTYPE;
  33. class ast_node : public simple_node {
  34. public:
  35. virtual ~ast_node();
  36. virtual void print(void) const;
  37. virtual ir_rvalue *hir(exec_list *instructions,
  38. struct _mesa_glsl_parse_state *state);
  39. /**
  40. * Retrieve the source location of an AST node
  41. *
  42. * This function is primarily used to get the source position of an AST node
  43. * into a form that can be passed to \c _mesa_glsl_error.
  44. *
  45. * \sa _mesa_glsl_error, ast_node::set_location
  46. */
  47. struct YYLTYPE get_location(void) const
  48. {
  49. struct YYLTYPE locp;
  50. locp.source = this->location.source;
  51. locp.first_line = this->location.line;
  52. locp.first_column = this->location.column;
  53. locp.last_line = locp.first_line;
  54. locp.last_column = locp.first_column;
  55. return locp;
  56. }
  57. /**
  58. * Set the source location of an AST node from a parser location
  59. *
  60. * \sa ast_node::get_location
  61. */
  62. void set_location(const struct YYLTYPE *locp)
  63. {
  64. this->location.source = locp->source;
  65. this->location.line = locp->first_line;
  66. this->location.column = locp->first_column;
  67. }
  68. int type;
  69. struct {
  70. unsigned source;
  71. unsigned line;
  72. unsigned column;
  73. } location;
  74. protected:
  75. ast_node(void);
  76. };
  77. enum ast_operators {
  78. ast_assign,
  79. ast_plus, /**< Unary + operator. */
  80. ast_neg,
  81. ast_add,
  82. ast_sub,
  83. ast_mul,
  84. ast_div,
  85. ast_mod,
  86. ast_lshift,
  87. ast_rshift,
  88. ast_less,
  89. ast_greater,
  90. ast_lequal,
  91. ast_gequal,
  92. ast_equal,
  93. ast_nequal,
  94. ast_bit_and,
  95. ast_bit_xor,
  96. ast_bit_or,
  97. ast_bit_not,
  98. ast_logic_and,
  99. ast_logic_xor,
  100. ast_logic_or,
  101. ast_logic_not,
  102. ast_mul_assign,
  103. ast_div_assign,
  104. ast_mod_assign,
  105. ast_add_assign,
  106. ast_sub_assign,
  107. ast_ls_assign,
  108. ast_rs_assign,
  109. ast_and_assign,
  110. ast_xor_assign,
  111. ast_or_assign,
  112. ast_conditional,
  113. ast_pre_inc,
  114. ast_pre_dec,
  115. ast_post_inc,
  116. ast_post_dec,
  117. ast_field_selection,
  118. ast_array_index,
  119. ast_function_call,
  120. ast_identifier,
  121. ast_int_constant,
  122. ast_uint_constant,
  123. ast_float_constant,
  124. ast_bool_constant,
  125. ast_sequence
  126. };
  127. class ast_expression : public ast_node {
  128. public:
  129. ast_expression(int oper, ast_expression *,
  130. ast_expression *, ast_expression *);
  131. ast_expression(const char *identifier) :
  132. oper(ast_identifier)
  133. {
  134. subexpressions[0] = NULL;
  135. subexpressions[1] = NULL;
  136. subexpressions[2] = NULL;
  137. primary_expression.identifier = (char *) identifier;
  138. }
  139. static const char *operator_string(enum ast_operators op);
  140. virtual ir_rvalue *hir(exec_list *instructions,
  141. struct _mesa_glsl_parse_state *state);
  142. virtual void print(void) const;
  143. enum ast_operators oper;
  144. ast_expression *subexpressions[3];
  145. union {
  146. char *identifier;
  147. int int_constant;
  148. float float_constant;
  149. unsigned uint_constant;
  150. int bool_constant;
  151. } primary_expression;
  152. /**
  153. * List of expressions for an \c ast_sequence.
  154. */
  155. struct simple_node expressions;
  156. };
  157. class ast_expression_bin : public ast_expression {
  158. public:
  159. ast_expression_bin(int oper, ast_expression *, ast_expression *);
  160. virtual void print(void) const;
  161. };
  162. /**
  163. * Subclass of expressions for function calls
  164. */
  165. class ast_function_expression : public ast_expression {
  166. public:
  167. ast_function_expression(ast_expression *callee)
  168. : ast_expression(ast_function_call, callee,
  169. NULL, NULL),
  170. cons(false)
  171. {
  172. /* empty */
  173. }
  174. ast_function_expression(class ast_type_specifier *type)
  175. : ast_expression(ast_function_call, (ast_expression *) type,
  176. NULL, NULL),
  177. cons(true)
  178. {
  179. /* empty */
  180. }
  181. bool is_constructor() const
  182. {
  183. return cons;
  184. }
  185. virtual ir_rvalue *hir(exec_list *instructions,
  186. struct _mesa_glsl_parse_state *state);
  187. private:
  188. /**
  189. * Is this function call actually a constructor?
  190. */
  191. bool cons;
  192. };
  193. /**
  194. * Number of possible operators for an ast_expression
  195. *
  196. * This is done as a define instead of as an additional value in the enum so
  197. * that the compiler won't generate spurious messages like "warning:
  198. * enumeration value ‘ast_num_operators’ not handled in switch"
  199. */
  200. #define AST_NUM_OPERATORS (ast_sequence + 1)
  201. class ast_compound_statement : public ast_node {
  202. public:
  203. ast_compound_statement(int new_scope, ast_node *statements);
  204. virtual void print(void) const;
  205. virtual ir_rvalue *hir(exec_list *instructions,
  206. struct _mesa_glsl_parse_state *state);
  207. int new_scope;
  208. struct simple_node statements;
  209. };
  210. class ast_declaration : public ast_node {
  211. public:
  212. ast_declaration(char *identifier, int is_array, ast_expression *array_size,
  213. ast_expression *initializer);
  214. virtual void print(void) const;
  215. char *identifier;
  216. int is_array;
  217. ast_expression *array_size;
  218. ast_expression *initializer;
  219. };
  220. enum {
  221. ast_precision_high = 0, /**< Default precision. */
  222. ast_precision_medium,
  223. ast_precision_low
  224. };
  225. struct ast_type_qualifier {
  226. unsigned invariant:1;
  227. unsigned constant:1;
  228. unsigned attribute:1;
  229. unsigned varying:1;
  230. unsigned in:1;
  231. unsigned out:1;
  232. unsigned centroid:1;
  233. unsigned uniform:1;
  234. unsigned smooth:1;
  235. unsigned flat:1;
  236. unsigned noperspective:1;
  237. };
  238. class ast_struct_specifier : public ast_node {
  239. public:
  240. ast_struct_specifier(char *identifier, ast_node *declarator_list);
  241. virtual void print(void) const;
  242. char *name;
  243. struct simple_node declarations;
  244. };
  245. enum ast_types {
  246. ast_void,
  247. ast_float,
  248. ast_int,
  249. ast_uint,
  250. ast_bool,
  251. ast_vec2,
  252. ast_vec3,
  253. ast_vec4,
  254. ast_bvec2,
  255. ast_bvec3,
  256. ast_bvec4,
  257. ast_ivec2,
  258. ast_ivec3,
  259. ast_ivec4,
  260. ast_uvec2,
  261. ast_uvec3,
  262. ast_uvec4,
  263. ast_mat2,
  264. ast_mat2x3,
  265. ast_mat2x4,
  266. ast_mat3x2,
  267. ast_mat3,
  268. ast_mat3x4,
  269. ast_mat4x2,
  270. ast_mat4x3,
  271. ast_mat4,
  272. ast_sampler1d,
  273. ast_sampler2d,
  274. ast_sampler2drect,
  275. ast_sampler3d,
  276. ast_samplercube,
  277. ast_sampler1dshadow,
  278. ast_sampler2dshadow,
  279. ast_sampler2drectshadow,
  280. ast_samplercubeshadow,
  281. ast_sampler1darray,
  282. ast_sampler2darray,
  283. ast_sampler1darrayshadow,
  284. ast_sampler2darrayshadow,
  285. ast_isampler1d,
  286. ast_isampler2d,
  287. ast_isampler3d,
  288. ast_isamplercube,
  289. ast_isampler1darray,
  290. ast_isampler2darray,
  291. ast_usampler1d,
  292. ast_usampler2d,
  293. ast_usampler3d,
  294. ast_usamplercube,
  295. ast_usampler1darray,
  296. ast_usampler2darray,
  297. ast_struct,
  298. ast_type_name
  299. };
  300. class ast_type_specifier : public ast_node {
  301. public:
  302. ast_type_specifier(int specifier);
  303. /** Construct a type specifier from a type name */
  304. ast_type_specifier(const char *name)
  305. : type_specifier(ast_type_name), type_name(name), structure(NULL),
  306. is_array(false), array_size(NULL), precision(ast_precision_high)
  307. {
  308. /* empty */
  309. }
  310. /** Construct a type specifier from a structure definition */
  311. ast_type_specifier(ast_struct_specifier *s)
  312. : type_specifier(ast_struct), type_name(s->name), structure(s),
  313. is_array(false), array_size(NULL), precision(ast_precision_high)
  314. {
  315. /* empty */
  316. }
  317. const struct glsl_type *glsl_type(const char **name,
  318. struct _mesa_glsl_parse_state *state)
  319. const;
  320. virtual void print(void) const;
  321. enum ast_types type_specifier;
  322. const char *type_name;
  323. ast_struct_specifier *structure;
  324. int is_array;
  325. ast_expression *array_size;
  326. unsigned precision:2;
  327. };
  328. class ast_fully_specified_type : public ast_node {
  329. public:
  330. virtual void print(void) const;
  331. ast_type_qualifier qualifier;
  332. ast_type_specifier *specifier;
  333. };
  334. class ast_declarator_list : public ast_node {
  335. public:
  336. ast_declarator_list(ast_fully_specified_type *);
  337. virtual void print(void) const;
  338. virtual ir_rvalue *hir(exec_list *instructions,
  339. struct _mesa_glsl_parse_state *state);
  340. ast_fully_specified_type *type;
  341. struct simple_node declarations;
  342. /**
  343. * Special flag for vertex shader "invariant" declarations.
  344. *
  345. * Vertex shaders can contain "invariant" variable redeclarations that do
  346. * not include a type. For example, "invariant gl_Position;". This flag
  347. * is used to note these cases when no type is specified.
  348. */
  349. int invariant;
  350. };
  351. class ast_parameter_declarator : public ast_node {
  352. public:
  353. virtual void print(void) const;
  354. virtual ir_rvalue *hir(exec_list *instructions,
  355. struct _mesa_glsl_parse_state *state);
  356. ast_fully_specified_type *type;
  357. char *identifier;
  358. int is_array;
  359. ast_expression *array_size;
  360. };
  361. class ast_function : public ast_node {
  362. public:
  363. ast_function(void);
  364. virtual void print(void) const;
  365. virtual ir_rvalue *hir(exec_list *instructions,
  366. struct _mesa_glsl_parse_state *state);
  367. ast_fully_specified_type *return_type;
  368. char *identifier;
  369. struct simple_node parameters;
  370. private:
  371. /**
  372. * Is this prototype part of the function definition?
  373. *
  374. * Used by ast_function_definition::hir to process the parameters, etc.
  375. * of the function.
  376. *
  377. * \sa ::hir
  378. */
  379. bool is_definition;
  380. /**
  381. * Function signature corresponding to this function prototype instance
  382. *
  383. * Used by ast_function_definition::hir to process the parameters, etc.
  384. * of the function.
  385. *
  386. * \sa ::hir
  387. */
  388. class ir_function_signature *signature;
  389. friend class ast_function_definition;
  390. };
  391. class ast_declaration_statement : public ast_node {
  392. public:
  393. ast_declaration_statement(void);
  394. enum {
  395. ast_function,
  396. ast_declaration,
  397. ast_precision
  398. } mode;
  399. union {
  400. class ast_function *function;
  401. ast_declarator_list *declarator;
  402. ast_type_specifier *type;
  403. ast_node *node;
  404. } declaration;
  405. };
  406. class ast_expression_statement : public ast_node {
  407. public:
  408. ast_expression_statement(ast_expression *);
  409. virtual void print(void) const;
  410. virtual ir_rvalue *hir(exec_list *instructions,
  411. struct _mesa_glsl_parse_state *state);
  412. ast_expression *expression;
  413. };
  414. class ast_case_label : public ast_node {
  415. public:
  416. /**
  417. * An expression of NULL means 'default'.
  418. */
  419. ast_expression *expression;
  420. };
  421. class ast_selection_statement : public ast_node {
  422. public:
  423. ast_selection_statement(ast_expression *condition,
  424. ast_node *then_statement,
  425. ast_node *else_statement);
  426. virtual void print(void) const;
  427. virtual ir_rvalue *hir(exec_list *instructions,
  428. struct _mesa_glsl_parse_state *state);
  429. ast_expression *condition;
  430. ast_node *then_statement;
  431. ast_node *else_statement;
  432. };
  433. class ast_switch_statement : public ast_node {
  434. public:
  435. ast_expression *expression;
  436. struct simple_node statements;
  437. };
  438. class ast_iteration_statement : public ast_node {
  439. public:
  440. ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
  441. ast_expression *rest_expression, ast_node *body);
  442. virtual void print(void) const;
  443. enum ast_iteration_modes {
  444. ast_for,
  445. ast_while,
  446. ast_do_while
  447. } mode;
  448. ast_node *init_statement;
  449. ast_node *condition;
  450. ast_expression *rest_expression;
  451. ast_node *body;
  452. };
  453. class ast_jump_statement : public ast_node {
  454. public:
  455. ast_jump_statement(int mode, ast_expression *return_value);
  456. virtual void print(void) const;
  457. virtual ir_rvalue *hir(exec_list *instructions,
  458. struct _mesa_glsl_parse_state *state);
  459. enum ast_jump_modes {
  460. ast_continue,
  461. ast_break,
  462. ast_return,
  463. ast_discard
  464. } mode;
  465. ast_expression *opt_return_value;
  466. };
  467. class ast_function_definition : public ast_node {
  468. public:
  469. virtual void print(void) const;
  470. virtual ir_rvalue *hir(exec_list *instructions,
  471. struct _mesa_glsl_parse_state *state);
  472. ast_function *prototype;
  473. ast_compound_statement *body;
  474. };
  475. extern void
  476. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
  477. extern struct ir_rvalue *
  478. _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
  479. exec_list *instructions,
  480. struct _mesa_glsl_parse_state *state);
  481. #endif /* AST_H */