Add overflow detection
[openscop.git] / source / names.c
blobc68f936a829d5fcdfff8693705c3a8868ee654ed
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/names.c **
6 **-----------------------------------------------------------------**
7 ** First version: 18/04/2011 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
67 #include <osl/macros.h>
68 #include <osl/strings.h>
69 #include <osl/names.h>
72 /*+***************************************************************************
73 * Structure display function *
74 *****************************************************************************/
77 /**
78 * osl_names_idump function:
79 * this function displays an osl_names_t structure (*names) into a
80 * file (file, possibly stdout) in a way that trends to be understandable. It
81 * includes an indentation level (level) in order to work with others
82 * idump functions.
83 * \param[in] file The file where the information has to be printed.
84 * \param[in] names The names structure whose information has to be printed.
85 * \param[in] level Number of spaces before printing, for each line.
87 void osl_names_idump(FILE * file, osl_names_p names, int level) {
88 int j;
90 // Go to the right level.
91 for (j = 0; j < level; j++)
92 fprintf(file, "|\t");
94 if (names != NULL)
95 fprintf(file, "+-- osl_names_t\n");
96 else
97 fprintf(file, "+-- NULL names\n");
99 if (names != NULL) {
100 // A blank line.
101 for (j = 0; j <= level+1; j++)
102 fprintf(file, "|\t");
103 fprintf(file, "\n");
105 // Print the various names.
106 osl_strings_idump(file, names->parameters, level + 1);
107 osl_strings_idump(file, names->iterators, level + 1);
108 osl_strings_idump(file, names->scatt_dims, level + 1);
109 osl_strings_idump(file, names->local_dims, level + 1);
110 osl_strings_idump(file, names->arrays, level + 1);
113 // The last line.
114 for (j = 0; j <= level; j++)
115 fprintf(file, "|\t");
116 fprintf(file, "\n");
121 * osl_names_dump function:
122 * this function prints the content of an osl_names_t structure
123 * (*names) into a file (file, possibly stdout).
124 * \param[in] file The file where the information has to be printed.
125 * \param[in] names The names structure whose information has to be printed.
127 void osl_names_dump(FILE * file, osl_names_p names) {
128 osl_names_idump(file, names, 0);
132 /*****************************************************************************
133 * Reading function *
134 *****************************************************************************/
137 /*+***************************************************************************
138 * Memory allocation/deallocation function *
139 *****************************************************************************/
143 * osl_names_malloc function:
144 * this function allocates the memory space for an osl_names_t
145 * structure and sets its fields with default values. Then it returns a
146 * pointer to the allocated space.
147 * \return A pointer to an empty names structure with fields set to
148 * default values.
150 osl_names_p osl_names_malloc() {
151 osl_names_p names;
153 OSL_malloc(names, osl_names_p, sizeof(osl_names_t));
154 names->parameters = NULL;
155 names->iterators = NULL;
156 names->scatt_dims = NULL;
157 names->local_dims = NULL;
158 names->arrays = NULL;
160 return names;
165 * osl_names_free function:
166 * This function frees the allocated memory for an osl_names_t
167 * structure. If the names are not character strings, it is the
168 * responsibility of the user to free each array of elements (including
169 * the array itself), this function will only free the osl_names_t shell.
170 * \param[in,out] names The pointer to the names structure we want to free.
172 void osl_names_free(osl_names_p names) {
173 if (names != NULL) {
174 osl_strings_free(names->parameters);
175 osl_strings_free(names->iterators);
176 osl_strings_free(names->scatt_dims);
177 osl_strings_free(names->local_dims);
178 osl_strings_free(names->arrays);
180 free(names);
185 /*+***************************************************************************
186 * Processing functions *
187 *****************************************************************************/
191 * osl_names_generate function:
192 * this function generates some names. For each kind of name it will generate
193 * a given number of names with a given prefix followed by a number.
194 * \param[in] parameter_prefix Prefix for parameter names.
195 * \param[in] nb_parameters Number of parameters names to generate.
196 * \param[in] iterator_prefix Prefix for iterator names.
197 * \param[in] nb_iterators Number of iterators names to generate.
198 * \param[in] scatt_dim_prefix Prefix for scattering dimension names.
199 * \param[in] nb_scatt_dims Number of scattering dim names to generate.
200 * \param[in] local_dim_prefix Prefix for local dimension names.
201 * \param[in] nb_local_dims Number of local dimension names to generate.
202 * \param[in] array_prefix Prefix for array names.
203 * \param[in] nb_arrays Number of array names to generate.
204 * \return A new names structure containing generated names.
206 osl_names_p osl_names_generate(
207 char * parameter_prefix, int nb_parameters,
208 char * iterator_prefix, int nb_iterators,
209 char * scatt_dim_prefix, int nb_scatt_dims,
210 char * local_dim_prefix, int nb_local_dims,
211 char * array_prefix, int nb_arrays) {
212 osl_names_p names = osl_names_malloc();
214 names->parameters= osl_strings_generate(parameter_prefix,nb_parameters);
215 names->iterators = osl_strings_generate(iterator_prefix, nb_iterators);
216 names->scatt_dims= osl_strings_generate(scatt_dim_prefix,nb_scatt_dims);
217 names->local_dims= osl_strings_generate(local_dim_prefix,nb_local_dims);
218 names->arrays = osl_strings_generate(array_prefix, nb_arrays);
220 return names;
224 * osl_names_clone function:
225 * this function builds and returns a "hard copy" (not a pointer copy) of an
226 * osl_names_t data structure provided as parameter.
227 * \param[in] names The pointer to the names structure we want to clone.
228 * \return A pointer to the clone of the names structure provided as parameter.
230 osl_names_p osl_names_clone(osl_names_p names) {
231 osl_names_p clone = NULL;
233 if (names != NULL) {
234 clone = osl_names_malloc();
235 clone->parameters = osl_strings_clone(names->parameters);
236 clone->iterators = osl_strings_clone(names->iterators);
237 clone->scatt_dims = osl_strings_clone(names->scatt_dims);
238 clone->local_dims = osl_strings_clone(names->local_dims);
239 clone->arrays = osl_strings_clone(names->arrays);
241 return clone;