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