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.

glsl_symbol_table.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2010 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 GLSL_SYMBOL_TABLE
  26. #define GLSL_SYMBOL_TABLE
  27. #include "symbol_table.h"
  28. #include "ir.h"
  29. #include "glsl_types.h"
  30. /**
  31. * Facade class for _mesa_symbol_table
  32. *
  33. * Wraps the existing \c _mesa_symbol_table data structure to enforce some
  34. * type safe and some symbol table invariants.
  35. */
  36. class glsl_symbol_table {
  37. private:
  38. enum glsl_symbol_name_space {
  39. glsl_variable_name_space = 0,
  40. glsl_type_name_space = 1,
  41. glsl_function_name_space = 2
  42. };
  43. public:
  44. glsl_symbol_table()
  45. {
  46. table = _mesa_symbol_table_ctor();
  47. }
  48. ~glsl_symbol_table()
  49. {
  50. _mesa_symbol_table_dtor(table);
  51. }
  52. void push_scope()
  53. {
  54. _mesa_symbol_table_push_scope(table);
  55. }
  56. void pop_scope()
  57. {
  58. _mesa_symbol_table_pop_scope(table);
  59. }
  60. /**
  61. * Determine whether a name was declared at the current scope
  62. */
  63. bool name_declared_this_scope(const char *name)
  64. {
  65. return _mesa_symbol_table_symbol_scope(table, -1, name) == 0;
  66. }
  67. /**
  68. * \name Methods to add symbols to the table
  69. *
  70. * There is some temptation to rename all these functions to \c add_symbol
  71. * or similar. However, this breaks symmetry with the getter functions and
  72. * reduces the clarity of the intention of code that uses these methods.
  73. */
  74. /*@{*/
  75. bool add_variable(const char *name, ir_variable *v)
  76. {
  77. return _mesa_symbol_table_add_symbol(table, glsl_variable_name_space,
  78. name, v) == 0;
  79. }
  80. bool add_type(const char *name, const glsl_type *t)
  81. {
  82. return _mesa_symbol_table_add_symbol(table, glsl_type_name_space,
  83. name, (void *) t) == 0;
  84. }
  85. bool add_function(const char *name, ir_function *f)
  86. {
  87. return _mesa_symbol_table_add_symbol(table, glsl_function_name_space,
  88. name, f) == 0;
  89. }
  90. /*@}*/
  91. /**
  92. * \name Methods to get symbols from the table
  93. */
  94. /*@{*/
  95. ir_variable *get_variable(const char *name)
  96. {
  97. return (ir_variable *)
  98. _mesa_symbol_table_find_symbol(table, glsl_variable_name_space, name);
  99. }
  100. glsl_type *get_type(const char *name)
  101. {
  102. return (glsl_type *)
  103. _mesa_symbol_table_find_symbol(table, glsl_type_name_space, name);
  104. }
  105. ir_function *get_function(const char *name)
  106. {
  107. return (ir_function *)
  108. _mesa_symbol_table_find_symbol(table, glsl_function_name_space, name);
  109. }
  110. /*@}*/
  111. private:
  112. struct _mesa_symbol_table *table;
  113. };
  114. #endif /* GLSL_SYMBOL_TABLE */