slang: Check return value from new_instruction().
[mesa.git] / src / mesa / shader / slang / slang_compile_struct.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-2007 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 "main/imports.h"
32 #include "slang_mem.h"
33 #include "slang_compile.h"
34
35
36 GLvoid
37 _slang_struct_scope_ctr(slang_struct_scope * self)
38 {
39 self->structs = NULL;
40 self->num_structs = 0;
41 self->outer_scope = NULL;
42 }
43
44 void
45 slang_struct_scope_destruct(slang_struct_scope * scope)
46 {
47 GLuint i;
48
49 for (i = 0; i < scope->num_structs; i++)
50 slang_struct_destruct(scope->structs + i);
51 _slang_free(scope->structs);
52 /* do not free scope->outer_scope */
53 }
54
55 int
56 slang_struct_scope_copy(slang_struct_scope * x, const slang_struct_scope * y)
57 {
58 slang_struct_scope z;
59 GLuint i;
60
61 _slang_struct_scope_ctr(&z);
62 z.structs = (slang_struct *)
63 _slang_alloc(y->num_structs * sizeof(slang_struct));
64 if (z.structs == NULL) {
65 slang_struct_scope_destruct(&z);
66 return 0;
67 }
68 for (z.num_structs = 0; z.num_structs < y->num_structs; z.num_structs++)
69 if (!slang_struct_construct(&z.structs[z.num_structs])) {
70 slang_struct_scope_destruct(&z);
71 return 0;
72 }
73 for (i = 0; i < z.num_structs; i++)
74 if (!slang_struct_copy(&z.structs[i], &y->structs[i])) {
75 slang_struct_scope_destruct(&z);
76 return 0;
77 }
78 z.outer_scope = y->outer_scope;
79 slang_struct_scope_destruct(x);
80 *x = z;
81 return 1;
82 }
83
84 slang_struct *
85 slang_struct_scope_find(slang_struct_scope * stru, slang_atom a_name,
86 int all_scopes)
87 {
88 GLuint i;
89
90 for (i = 0; i < stru->num_structs; i++)
91 if (a_name == stru->structs[i].a_name)
92 return &stru->structs[i];
93 if (all_scopes && stru->outer_scope != NULL)
94 return slang_struct_scope_find(stru->outer_scope, a_name, 1);
95 return NULL;
96 }
97
98 /* slang_struct */
99
100 int
101 slang_struct_construct(slang_struct * stru)
102 {
103 stru->a_name = SLANG_ATOM_NULL;
104 stru->fields = (slang_variable_scope *)
105 _slang_alloc(sizeof(slang_variable_scope));
106 if (stru->fields == NULL)
107 return 0;
108 _slang_variable_scope_ctr(stru->fields);
109
110 stru->structs =
111 (slang_struct_scope *) _slang_alloc(sizeof(slang_struct_scope));
112 if (stru->structs == NULL) {
113 slang_variable_scope_destruct(stru->fields);
114 _slang_free(stru->fields);
115 return 0;
116 }
117 _slang_struct_scope_ctr(stru->structs);
118 stru->constructor = NULL;
119 return 1;
120 }
121
122 void
123 slang_struct_destruct(slang_struct * stru)
124 {
125 slang_variable_scope_destruct(stru->fields);
126 _slang_free(stru->fields);
127 slang_struct_scope_destruct(stru->structs);
128 _slang_free(stru->structs);
129 }
130
131 int
132 slang_struct_copy(slang_struct * x, const slang_struct * y)
133 {
134 slang_struct z;
135
136 if (!slang_struct_construct(&z))
137 return 0;
138 z.a_name = y->a_name;
139 if (!slang_variable_scope_copy(z.fields, y->fields)) {
140 slang_struct_destruct(&z);
141 return 0;
142 }
143 if (!slang_struct_scope_copy(z.structs, y->structs)) {
144 slang_struct_destruct(&z);
145 return 0;
146 }
147 slang_struct_destruct(x);
148 *x = z;
149 return 1;
150 }
151
152 int
153 slang_struct_equal(const slang_struct * x, const slang_struct * y)
154 {
155 GLuint i;
156
157 if (x->fields->num_variables != y->fields->num_variables)
158 return 0;
159
160 for (i = 0; i < x->fields->num_variables; i++) {
161 const slang_variable *varx = x->fields->variables[i];
162 const slang_variable *vary = y->fields->variables[i];
163
164 if (varx->a_name != vary->a_name)
165 return 0;
166 if (!slang_type_specifier_equal(&varx->type.specifier,
167 &vary->type.specifier))
168 return 0;
169 if (varx->type.specifier.type == SLANG_SPEC_ARRAY)
170 if (varx->array_len != vary->array_len)
171 return GL_FALSE;
172 }
173 return 1;
174 }