PIPS
naming.c
Go to the documentation of this file.
1 /*
2 
3  $Id$
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 
28 /* Functions using simultaneously pipsdbm, which is based on strings,
29  and ri-util, which contains basic methods for the objects of the
30  internal representation.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 
38 #include "linear.h"
39 
40 #include "genC.h"
41 #include "misc.h"
42 #include "ri.h"
43 #include "properties.h"
44 #include "ri-util.h"
45 #include "workspace-util.h"
46 
47 /* Based on entity_user_name, but preserves scoping information when
48  needed to avoid ambiguity between two or more variables with the
49  same name.
50 
51  A variable or function name can be used for an external or static
52  entity in the compilation unit, it can be used as function
53  parameter, and then declared within the function at any scope
54  level.
55 
56  This function cannot directly be used in an interprocedural setting
57  as it is only related to one module.
58 
59  It is assumed that no new string is allocated, but a pointer to an
60  existing string is returned.
61 
62  The current implementation is extremely ineffective, especially for
63  a very unlikely, although mystifying, problem.
64 
65  FI: this function does not seem to be used in Libs or Passes. Its
66 implementation is based on module_entities(), which is not in ri-util...
67 */
69 {
70  const char* uan = entity_user_name(e);
71 
72  /* No problem in Fortran */
73  if(c_module_p(m)) {
74  list conflicts = module_entities(m);
75 
76  FOREACH(ENTITY, v, conflicts){
77  if(v!=e) {
78  const char* vn = entity_user_name(v);
79  if(same_string_p(uan,vn)) {
80  uan = entity_local_name(e);
81  break;
82  }
83  }
84  }
85  gen_free_list(conflicts);
86  }
87 
88  return (uan);
89 }
90 
92 {
94 
96 }
97 
98 /* In interprocedural context, returns the shortest non-ambiguous name
99  for a variable. If it is local to the current module, use the user
100  name. If not return entity_name(), which is not fully satisfying
101  for C variables because it includes scope information.
102 
103  Note also that this function assumes the existence of a current module.
104 
105  FI: why is this function in preprocessor and not in ri-util?
106 */
107 static const char* entity_more_or_less_minimal_name(entity e, bool strict_p)
108 {
110  const char* mln = module_local_name(m);
111  const char* emn = string_undefined;
112  string cun = string_undefined; // compilation unit name
113  entity cu = entity_undefined; // compilation unit
114  list cudl = list_undefined; // compilation unit declaration list
116 
117  if(c_module_p(m) && !compilation_unit_p(mln)) {
118  /* in pipsmake library...*/
119  string compilation_unit_of_module(const char*);
120  cun = compilation_unit_of_module(mln);
123  free(cun);
124  }
125 
126  pips_assert("some current entity", !entity_undefined_p(m));
127 
128  /* gen_in_list_p() would/should be sufficient... */
129  if (strcmp(mln, entity_module_name(e)) == 0
130  || gen_in_list_p(e, mdl)) {
131  /* The variable is declared in the current module */
132  //return global_name_to_user_name(entity_name(e));
133  if(strict_p)
135  else
136  emn = entity_user_name(e);
137  }
138  else if (!list_undefined_p(cudl) && gen_in_list_p(e, cudl)) {
139  /* The variable is declared in the compilation unit of the current
140  module */
141  //return global_name_to_user_name(entity_name(e));
142  if(strict_p)
144  else
145  emn = entity_user_name(e);
146  }
147  else if (entity_field_p(e)) {
148  /* The variable is a union or struct field. No need to
149  disambiguate. */
150  if(strict_p)
152  else
153  emn = entity_user_name(e);
154  }
155  else if (entity_formal_p(e)) {
156  /* Formal parameters should always be unambiguous? */
157  if(strict_p)
159  else
160  emn = entity_user_name(e);
161  }
162  else if (strstr(entity_module_name(e), DUMMY_PARAMETER_PREFIX)) {
163  /* The variable is a dummy parameter entity, used in a function
164  declaration */
165  if(strict_p)
166  emn = entity_local_name(e);
167  else {
168  /* In analysis results, let's know when dummy parameters are
169  used... */
170  //emn = strdup(entity_local_name(e));
171  emn = strdup(entity_name(e));
172  }
173  }
174  else if (strcmp(TOP_LEVEL_MODULE_NAME, entity_module_name(e)) == 0) {
175  /* The variable is a ??? */
176  if(strict_p)
177  emn = entity_local_name(e);
178  else
179  emn = strdup(entity_local_name(e));
180  }
181  else if (strcmp(REGIONS_MODULE_NAME, entity_module_name(e)) == 0) {
182  /* The variable is a PHI entity */
183  if(strict_p)
184  emn = entity_local_name(e);
185  else
186  emn = strdup(entity_local_name(e));
187  }
188  else if (strcmp(POINTS_TO_MODULE_NAME, entity_module_name(e)) == 0) {
189  /* The variable is a stub entity for formal and global pointers */
190  if(strict_p)
191  emn = entity_local_name(e);
192  else
193  emn = strdup(entity_local_name(e));
194  }
195  else {
196  /* must be used to prettyprint interprocedural information... */
197  //entity om = local_name_to_top_level_entity(mln);
198  //emn = entity_interprocedural_unambiguous_user_name(e, om);
199  if(strict_p)
200  emn = entity_name(e);
201  else
202  emn = strdup(entity_name(e));
203  }
204 
205  return emn;
206 }
207 
208 /* Do preserve scope informations
209 
210  This function does not allocate a new string, which implies to keep
211  the scope information in the name of extraprocedural variables and
212  functions.
213  */
215 {
216  return entity_more_or_less_minimal_name(e, true);
217 }
218 
219 /* Do not preserve scope information
220 
221  A new string is allocated.
222  */
224 {
225  return entity_more_or_less_minimal_name(e, false);
226 }
227 
229 {
231 }
232 
233 /**
234  * very simple conversion from string to expression
235  * only handles entities and numeric values at the time being
236  */
238 {
239  if(empty_string_p(s)) return entity_undefined;
240 
241  /* try float conversion */
242  string endptr;
243  const char *module_name=module_local_name(module);
244  long int l = strtol(s,&endptr,10);
245  if(!*endptr) {
246  if(l>=0)
247  return int_to_entity(l);
248  else /* no negative integer entity in pips */
249  return entity_undefined;
250  }
251  float f = strtof(s,&endptr);
252  if(!*endptr) return float_to_entity(f);
253 
254  entity candidate = entity_undefined;
255  /* first find all relevant entities */
257  {
258  /* this an heuristic to find the one with a suiting scope
259  * error prone*/
261  if(entity_undefined_p(candidate) ||
262  strlen(entity_name(candidate)) > strlen(entity_name(e)))
263  candidate=e;
264  }
265  /* try at the compilation unit level */
266  if(entity_undefined_p(candidate))
268  /* try at top level */
269  if(entity_undefined_p(candidate))
270  candidate=FindEntity(TOP_LEVEL_MODULE_NAME,s);
271  return candidate;
272 }
273 
274 /* split a string using @p seed as separator
275  * and call string_to_entity on each chunk */
276 list string_to_entities(const char * str, const char * seed, entity module) {
277  list strings = strsplit(str,seed);
278  list entities = NIL;
279  FOREACH(STRING,s,strings) {
281  if(!entity_undefined_p(e)) {
282  entities = CONS(ENTITY,
283  e,
284  entities);
285  }
286  }
287  gen_map(free,strings);
288  gen_free_list(strings);
289  return gen_nreverse(entities);
290 
291 }
void print_homogeneous_arguments(list args, const char *variable_name(entity))
Functions dealing with entity lists.
Definition: arguments.c:54
entity float_to_entity(float c)
Definition: constant.c:430
entity int_to_entity(_int c)
Definition: constant.c:453
string compilation_unit_of_module(const char *)
The output is undefined if the module is referenced but not defined in the workspace,...
Definition: module.c:350
bool compilation_unit_p(const char *module_name)
The names of PIPS entities carry information about their nature.
Definition: entity_names.c:56
bool empty_string_p(const char *s)
Definition: entity_names.c:239
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
#define STRING(x)
Definition: genC.h:87
void free(void *)
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
#define list_undefined_p(c)
Return if a list is undefined.
Definition: newgen_list.h:75
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
void gen_map(gen_iter_func_t fp, const list l)
Definition: list.c:172
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
bool gen_in_list_p(const void *vo, const list lx)
tell whether vo belongs to lx
Definition: list.c:734
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define list_undefined
Undefined list definition :-)
Definition: newgen_list.h:69
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define DUMMY_PARAMETER_PREFIX
For dmmmy parameters in functions declarations.
Definition: naming-local.h:93
#define TOP_LEVEL_MODULE_NAME
Module containing the global variables in Fortran and C.
Definition: naming-local.h:101
#define POINTS_TO_MODULE_NAME
Module containing stub variables used to initialize intraprocedural points-to in C.
Definition: naming-local.h:104
const char * entity_module_unambiguous_user_name(entity e, entity m)
Functions using simultaneously pipsdbm, which is based on strings, and ri-util, which contains basic ...
Definition: naming.c:68
entity string_to_entity(const char *s, entity module)
very simple conversion from string to expression only handles entities and numeric values at the time...
Definition: naming.c:237
const char * entity_unambiguous_user_name(entity e)
Definition: naming.c:91
static const char * entity_more_or_less_minimal_name(entity e, bool strict_p)
In interprocedural context, returns the shortest non-ambiguous name for a variable.
Definition: naming.c:107
const char * entity_minimal_user_name(entity e)
Do not preserve scope information.
Definition: naming.c:223
list string_to_entities(const char *str, const char *seed, entity module)
split a string using seed as separator and call string_to_entity on each chunk
Definition: naming.c:276
const char * entity_minimal_name(entity e)
Do preserve scope informations.
Definition: naming.c:214
void print_arguments(list args)
Definition: naming.c:228
list strsplit(const char *, const char *)
Definition: string.c:318
#define same_string_p(s1, s2)
#define string_undefined
Definition: newgen_types.h:40
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
static char * module
Definition: pips.c:74
#define REGIONS_MODULE_NAME
Already defined.
#define entity_declarations(e)
MISC: newgen shorthands.
const char * entity_user_name(entity e)
Since entity_local_name may contain PIPS special characters such as prefixes (label,...
Definition: entity.c:487
entity FindEntity(const char *package, const char *name)
Retrieve an entity from its package/module name and its local name.
Definition: entity.c:1503
const char * entity_local_name(entity e)
entity_local_name modified so that it does not core when used in vect_fprint, since someone thought t...
Definition: entity.c:453
bool entity_formal_p(entity p)
is p a formal parameter?
Definition: entity.c:1935
bool c_module_p(entity m)
Test if a module "m" is written in C.
Definition: entity.c:2777
bool entity_field_p(entity e)
e is the field of a structure
Definition: entity.c:857
const char * module_local_name(entity e)
Returns the module local user name.
Definition: entity.c:582
const char * entity_module_name(entity e)
See comments about module_name().
Definition: entity.c:1092
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define code_declarations(x)
Definition: ri.h:784
#define entity_undefined_p(x)
Definition: ri.h:2762
#define entity_undefined
Definition: ri.h:2761
#define entity_name(x)
Definition: ri.h:2790
#define value_code(x)
Definition: ri.h:3067
#define entity_initial(x)
Definition: ri.h:2796
char * strdup()
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
list module_entities(entity m)
Return a list of all variables and functions accessible somewhere in a module.
Definition: module.c:85