Get it running for ARB_vertex_shader.
[mesa.git] / src / mesa / shader / slang / slang_compile_struct.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_compile_struct.c
27 * slang front-end compiler
28 * \author Michal Krol
29 */
30
31 #include "imports.h"
32 #include "slang_utility.h"
33 #include "slang_compile_variable.h"
34 #include "slang_compile_struct.h"
35
36 /* slang_struct_scope */
37
38 int slang_struct_scope_construct (slang_struct_scope *scope)
39 {
40 scope->structs = NULL;
41 scope->num_structs = 0;
42 scope->outer_scope = NULL;
43 return 1;
44 }
45
46 void slang_struct_scope_destruct (slang_struct_scope *scope)
47 {
48 unsigned int i;
49
50 for (i = 0; i < scope->num_structs; i++)
51 slang_struct_destruct (scope->structs + i);
52 slang_alloc_free (scope->structs);
53 /* do not free scope->outer_scope */
54 }
55
56 int slang_struct_scope_copy (slang_struct_scope *x, const slang_struct_scope *y)
57 {
58 slang_struct_scope z;
59 unsigned int i;
60
61 if (!slang_struct_scope_construct (&z))
62 return 0;
63 z.structs = (slang_struct *) slang_alloc_malloc (y->num_structs * sizeof (slang_struct));
64 if (z.structs == NULL)
65 {
66 slang_struct_scope_destruct (&z);
67 return 0;
68 }
69 for (z.num_structs = 0; z.num_structs < y->num_structs; z.num_structs++)
70 if (!slang_struct_construct (&z.structs[z.num_structs]))
71 {
72 slang_struct_scope_destruct (&z);
73 return 0;
74 }
75 for (i = 0; i < z.num_structs; i++)
76 if (!slang_struct_copy (&z.structs[i], &y->structs[i]))
77 {
78 slang_struct_scope_destruct (&z);
79 return 0;
80 }
81 z.outer_scope = y->outer_scope;
82 slang_struct_scope_destruct (x);
83 *x = z;
84 return 1;
85 }
86
87 slang_struct *slang_struct_scope_find (slang_struct_scope *stru, slang_atom a_name, int all_scopes)
88 {
89 unsigned int i;
90
91 for (i = 0; i < stru->num_structs; i++)
92 if (a_name == stru->structs[i].a_name)
93 return &stru->structs[i];
94 if (all_scopes && stru->outer_scope != NULL)
95 return slang_struct_scope_find (stru->outer_scope, a_name, 1);
96 return NULL;
97 }
98
99 /* slang_struct */
100
101 int slang_struct_construct (slang_struct *stru)
102 {
103 stru->a_name = SLANG_ATOM_NULL;
104 stru->fields = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));
105 if (stru->fields == NULL)
106 return 0;
107 if (!slang_variable_scope_construct (stru->fields))
108 {
109 slang_alloc_free (stru->fields);
110 return 0;
111 }
112 stru->structs = (slang_struct_scope *) slang_alloc_malloc (sizeof (slang_struct_scope));
113 if (stru->structs == NULL)
114 {
115 slang_variable_scope_destruct (stru->fields);
116 slang_alloc_free (stru->fields);
117 return 0;
118 }
119 if (!slang_struct_scope_construct (stru->structs))
120 {
121 slang_variable_scope_destruct (stru->fields);
122 slang_alloc_free (stru->fields);
123 slang_alloc_free (stru->structs);
124 return 0;
125 }
126 return 1;
127 }
128
129 void slang_struct_destruct (slang_struct *stru)
130 {
131 slang_variable_scope_destruct (stru->fields);
132 slang_alloc_free (stru->fields);
133 slang_struct_scope_destruct (stru->structs);
134 slang_alloc_free (stru->structs);
135 }
136
137 int slang_struct_copy (slang_struct *x, const slang_struct *y)
138 {
139 slang_struct z;
140
141 if (!slang_struct_construct (&z))
142 return 0;
143 z.a_name = y->a_name;
144 if (!slang_variable_scope_copy (z.fields, y->fields))
145 {
146 slang_struct_destruct (&z);
147 return 0;
148 }
149 if (!slang_struct_scope_copy (z.structs, y->structs))
150 {
151 slang_struct_destruct (&z);
152 return 0;
153 }
154 slang_struct_destruct (x);
155 *x = z;
156 return 1;
157 }
158
159 int slang_struct_equal (const slang_struct *x, const slang_struct *y)
160 {
161 unsigned int i;
162
163 if (x->fields->num_variables != y->fields->num_variables)
164 return 0;
165 for (i = 0; i < x->fields->num_variables; i++)
166 {
167 slang_variable *varx = &x->fields->variables[i];
168 slang_variable *vary = &y->fields->variables[i];
169
170 if (varx->a_name != vary->a_name)
171 return 0;
172 if (!slang_type_specifier_equal (&varx->type.specifier, &vary->type.specifier))
173 return 0;
174 if (varx->type.specifier.type == slang_spec_array)
175 {
176 /* TODO: compare array sizes */
177 }
178 }
179 return 1;
180 }
181