2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file slang_compile_struct.c
27 * slang front-end compiler
32 #include "slang_compile.h"
39 _slang_struct_scope_ctr (slang_struct_scope
*self
)
42 self
->num_structs
= 0;
43 self
->outer_scope
= NULL
;
46 void slang_struct_scope_destruct (slang_struct_scope
*scope
)
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 */
56 int slang_struct_scope_copy (slang_struct_scope
*x
, const slang_struct_scope
*y
)
61 _slang_struct_scope_ctr (&z
);
62 z
.structs
= (slang_struct
*) slang_alloc_malloc (y
->num_structs
* sizeof (slang_struct
));
63 if (z
.structs
== NULL
)
65 slang_struct_scope_destruct (&z
);
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
]))
71 slang_struct_scope_destruct (&z
);
74 for (i
= 0; i
< z
.num_structs
; i
++)
75 if (!slang_struct_copy (&z
.structs
[i
], &y
->structs
[i
]))
77 slang_struct_scope_destruct (&z
);
80 z
.outer_scope
= y
->outer_scope
;
81 slang_struct_scope_destruct (x
);
86 slang_struct
*slang_struct_scope_find (slang_struct_scope
*stru
, slang_atom a_name
, int all_scopes
)
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);
100 int slang_struct_construct (slang_struct
*stru
)
102 stru
->a_name
= SLANG_ATOM_NULL
;
103 stru
->fields
= (slang_variable_scope
*) slang_alloc_malloc (sizeof (slang_variable_scope
));
104 if (stru
->fields
== NULL
)
106 _slang_variable_scope_ctr (stru
->fields
);
107 stru
->structs
= (slang_struct_scope
*) slang_alloc_malloc (sizeof (slang_struct_scope
));
108 if (stru
->structs
== NULL
)
110 slang_variable_scope_destruct (stru
->fields
);
111 slang_alloc_free (stru
->fields
);
114 _slang_struct_scope_ctr (stru
->structs
);
118 void slang_struct_destruct (slang_struct
*stru
)
120 slang_variable_scope_destruct (stru
->fields
);
121 slang_alloc_free (stru
->fields
);
122 slang_struct_scope_destruct (stru
->structs
);
123 slang_alloc_free (stru
->structs
);
126 int slang_struct_copy (slang_struct
*x
, const slang_struct
*y
)
130 if (!slang_struct_construct (&z
))
132 z
.a_name
= y
->a_name
;
133 if (!slang_variable_scope_copy (z
.fields
, y
->fields
))
135 slang_struct_destruct (&z
);
138 if (!slang_struct_scope_copy (z
.structs
, y
->structs
))
140 slang_struct_destruct (&z
);
143 slang_struct_destruct (x
);
148 int slang_struct_equal (const slang_struct
*x
, const slang_struct
*y
)
152 if (x
->fields
->num_variables
!= y
->fields
->num_variables
)
154 for (i
= 0; i
< x
->fields
->num_variables
; i
++)
156 slang_variable
*varx
= x
->fields
->variables
[i
];
157 slang_variable
*vary
= y
->fields
->variables
[i
];
159 if (varx
->a_name
!= vary
->a_name
)
161 if (!slang_type_specifier_equal (&varx
->type
.specifier
, &vary
->type
.specifier
))
163 if (varx
->type
.specifier
.type
== SLANG_SPEC_ARRAY
)
164 if (varx
->array_len
!= vary
->array_len
)