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