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_function.c
27 * slang front-end compiler
32 #include "slang_compile.h"
33 #include "slang_mem.h"
35 /* slang_fixup_table */
38 slang_fixup_table_init(slang_fixup_table
* fix
)
45 slang_fixup_table_free(slang_fixup_table
* fix
)
47 _slang_free(fix
->table
);
48 slang_fixup_table_init(fix
);
52 * Add a new fixup address to the table.
55 slang_fixup_save(slang_fixup_table
*fixups
, GLuint address
)
57 fixups
->table
= (GLuint
*)
58 _slang_realloc(fixups
->table
,
59 fixups
->count
* sizeof(GLuint
),
60 (fixups
->count
+ 1) * sizeof(GLuint
));
61 if (fixups
->table
== NULL
)
63 fixups
->table
[fixups
->count
] = address
;
73 slang_function_construct(slang_function
* func
)
75 func
->kind
= SLANG_FUNC_ORDINARY
;
76 if (!slang_variable_construct(&func
->header
))
79 func
->parameters
= (slang_variable_scope
*)
80 _slang_alloc(sizeof(slang_variable_scope
));
81 if (func
->parameters
== NULL
) {
82 slang_variable_destruct(&func
->header
);
86 _slang_variable_scope_ctr(func
->parameters
);
87 func
->param_count
= 0;
90 slang_fixup_table_init(&func
->fixups
);
95 slang_function_destruct(slang_function
* func
)
97 slang_variable_destruct(&func
->header
);
98 slang_variable_scope_destruct(func
->parameters
);
99 _slang_free(func
->parameters
);
100 if (func
->body
!= NULL
) {
101 slang_operation_destruct(func
->body
);
102 _slang_free(func
->body
);
104 slang_fixup_table_free(&func
->fixups
);
108 * slang_function_scope
112 _slang_function_scope_ctr(slang_function_scope
* self
)
114 self
->functions
= NULL
;
115 self
->num_functions
= 0;
116 self
->outer_scope
= NULL
;
120 slang_function_scope_destruct(slang_function_scope
* scope
)
124 for (i
= 0; i
< scope
->num_functions
; i
++)
125 slang_function_destruct(scope
->functions
+ i
);
126 _slang_free(scope
->functions
);
131 * Does this function have a non-void return value?
134 _slang_function_has_return_value(const slang_function
*fun
)
136 return fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
;
141 * Search a list of functions for a particular function by name.
142 * \param funcs the list of functions to search
143 * \param a_name the name to search for
144 * \param all_scopes if non-zero, search containing scopes too.
145 * \return pointer to found function, or NULL.
148 slang_function_scope_find_by_name(slang_function_scope
* funcs
,
149 slang_atom a_name
, int all_scopes
)
153 for (i
= 0; i
< funcs
->num_functions
; i
++)
154 if (a_name
== funcs
->functions
[i
].header
.a_name
)
156 if (all_scopes
&& funcs
->outer_scope
!= NULL
)
157 return slang_function_scope_find_by_name(funcs
->outer_scope
, a_name
, 1);
163 * Search a list of functions for a particular function (for implementing
164 * function calls. Matching is done by first comparing the function's name,
165 * then the function's parameter list.
167 * \param funcs the list of functions to search
168 * \param fun the function to search for
169 * \param all_scopes if non-zero, search containing scopes too.
170 * \return pointer to found function, or NULL.
173 slang_function_scope_find(slang_function_scope
* funcs
, slang_function
* fun
,
178 for (i
= 0; i
< funcs
->num_functions
; i
++) {
179 slang_function
*f
= &funcs
->functions
[i
];
180 const GLuint haveRetValue
= 0;
182 = (f
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
);
187 printf("Compare name %s to %s (ret %u, %d, %d)\n",
188 (char *) fun->header.a_name, (char *) f->header.a_name,
190 fun->param_count, f->param_count);
193 if (fun
->header
.a_name
!= f
->header
.a_name
)
195 if (fun
->param_count
!= f
->param_count
)
197 for (j
= haveRetValue
; j
< fun
->param_count
; j
++) {
198 if (!slang_type_specifier_equal
199 (&fun
->parameters
->variables
[j
]->type
.specifier
,
200 &f
->parameters
->variables
[j
]->type
.specifier
))
203 if (j
== fun
->param_count
) {
205 printf("Found match\n");
211 printf("Not found\n");
213 if (all_scopes
&& funcs
->outer_scope
!= NULL
)
214 return slang_function_scope_find(funcs
->outer_scope
, fun
, 1);