glsl2: Move the Mesa IR codegen into mesa/shader/
[mesa.git] / src / glsl / 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 <new>
30
31 #include "symbol_table.h"
32 #include "ir.h"
33 #include "glsl_types.h"
34
35 /**
36 * Facade class for _mesa_symbol_table
37 *
38 * Wraps the existing \c _mesa_symbol_table data structure to enforce some
39 * type safe and some symbol table invariants.
40 */
41 class glsl_symbol_table {
42 private:
43 enum glsl_symbol_name_space {
44 glsl_variable_name_space = 0,
45 glsl_type_name_space = 1,
46 glsl_function_name_space = 2
47 };
48
49 static int
50 _glsl_symbol_table_destructor (glsl_symbol_table *table)
51 {
52 table->~glsl_symbol_table();
53
54 return 0;
55 }
56
57 public:
58 /* Callers of this talloc-based new need not call delete. It's
59 * easier to just talloc_free 'ctx' (or any of its ancestors). */
60 static void* operator new(size_t size, void *ctx)
61 {
62 void *table;
63
64 table = talloc_size(ctx, size);
65 assert(table != NULL);
66
67 talloc_set_destructor(table, (int (*)(void*)) _glsl_symbol_table_destructor);
68
69 return table;
70 }
71
72 /* If the user *does* call delete, that's OK, we will just
73 * talloc_free in that case. Here, C++ will have already called the
74 * destructor so tell talloc not to do that again. */
75 static void operator delete(void *table)
76 {
77 talloc_set_destructor(table, NULL);
78 talloc_free(table);
79 }
80
81 glsl_symbol_table()
82 {
83 table = _mesa_symbol_table_ctor();
84 }
85
86 ~glsl_symbol_table()
87 {
88 _mesa_symbol_table_dtor(table);
89 }
90
91 void push_scope()
92 {
93 _mesa_symbol_table_push_scope(table);
94 }
95
96 void pop_scope()
97 {
98 _mesa_symbol_table_pop_scope(table);
99 }
100
101 /**
102 * Determine whether a name was declared at the current scope
103 */
104 bool name_declared_this_scope(const char *name)
105 {
106 return _mesa_symbol_table_symbol_scope(table, -1, name) == 0;
107 }
108
109 /**
110 * \name Methods to add symbols to the table
111 *
112 * There is some temptation to rename all these functions to \c add_symbol
113 * or similar. However, this breaks symmetry with the getter functions and
114 * reduces the clarity of the intention of code that uses these methods.
115 */
116 /*@{*/
117 bool add_variable(const char *name, ir_variable *v)
118 {
119 return _mesa_symbol_table_add_symbol(table, glsl_variable_name_space,
120 name, v) == 0;
121 }
122
123 bool add_type(const char *name, const glsl_type *t)
124 {
125 return _mesa_symbol_table_add_symbol(table, glsl_type_name_space,
126 name, (void *) t) == 0;
127 }
128
129 bool add_function(const char *name, ir_function *f)
130 {
131 return _mesa_symbol_table_add_symbol(table, glsl_function_name_space,
132 name, f) == 0;
133 }
134 /*@}*/
135
136 /**
137 * \name Methods to get symbols from the table
138 */
139 /*@{*/
140 ir_variable *get_variable(const char *name)
141 {
142 return (ir_variable *)
143 _mesa_symbol_table_find_symbol(table, glsl_variable_name_space, name);
144 }
145
146 glsl_type *get_type(const char *name)
147 {
148 return (glsl_type *)
149 _mesa_symbol_table_find_symbol(table, glsl_type_name_space, name);
150 }
151
152 ir_function *get_function(const char *name)
153 {
154 return (ir_function *)
155 _mesa_symbol_table_find_symbol(table, glsl_function_name_space, name);
156 }
157 /*@}*/
158
159 private:
160 struct _mesa_symbol_table *table;
161 };
162
163 #endif /* GLSL_SYMBOL_TABLE */