Debian Codegraph @

gcc/cobol/symfind.cc

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of the Symas Corporation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "cobol-system.h"
33
34 #include "coretypes.h"
35 #include "tree.h"
36
37 #include "../../libgcobol/ec.h"
38 #include "../../libgcobol/common-defs.h"
39 #include "util.h"
40 #include "cbldiag.h"
41 #include "symbols.h"
42 #include "inspect.h"
43 #include "../../libgcobol/io.h"
44 #include "genapi.h"
45
46 extern int yydebug;
47
48 static bool
49 is_data_field( symbol_elem_t& e ) {
50 if( e.type != SymField ) return false;
51 const cbl_field_t *f = cbl_field_of(&e);
52 if( f->name[0] == '\0' ) return false;
53 if( is_filler(f) ) return false;
54
55 return f->type != FldForward;
56 }
57
58 class sym_name_t {
59 public: // TEMPORARY
60 const char *name;
61 size_t program, parent;
62 public:
63 explicit sym_name_t( const char name[] )
64 : name(name), program(0), parent(0) { assert(name[0] == '\0'); }
65 sym_name_t( size_t program, const char name[], size_t parent )
66 : name(name), program(program), parent(parent) {}
67
68 const char * c_str() const { return name; }
69
70 // Order by: Program, Name, Parent.
71 bool operator<( const sym_name_t& that ) const {
72 if( program == that.program ) {
73 int by_name = strcasecmp(name, that.name);
74 return by_name == 0? parent < that.parent : by_name < 0;
75 }
76 return program < that.program;
77 }
78 bool operator==( const char *name ) const {
79 return strcasecmp(this->name, name) == 0;
80 }
81
82 bool same_program( size_t program ) const {
83 return program == this->program;
84 }
85 };
86
87 typedef std::map< sym_name_t, std::vector<size_t> > symbol_map_t;
88
89
90 static symbol_map_t symbol_map;
91
92 typedef std::map <field_key_t, std::list<size_t> > field_keymap_t;
93 static field_keymap_t symbol_map2;
94
95 /*
96 * As each field is added to the symbol table, add its name and index
97 * to the name map. Initially the type is FldInvalid. Those are
98 * removed by symbols_update();
99 */
100 void
101 update_symbol_map2( const symbol_elem_t *e ) {
102 auto field = cbl_field_of(e);
103
104 if( ! field->is_typedef() ) {
105 switch( field->type ) {
106 case FldForward:
107 case FldLiteralN:
108 return;
109 case FldLiteralA:
110 if( ! field->is_key_name() ) return;
111 break;
112 default:
113 break;
114 }
115 }
116
117 field_key_t fk( e->program, field );
118 symbol_map2[fk].push_back(symbol_index(e));
119 }
120
121 /*
122 * Purge any field whose type is FldInvalid. Remove any names that do
123 * not map to any field.
124 */
125 void
126 finalize_symbol_map2() {
127 std::set<field_key_t> empties;
128
129 for( auto& elem : symbol_map2 ) {
130 auto& fields( elem.second );
131 fields.remove_if( []( auto isym ) {
132 const cbl_field_t *f = cbl_field_of(symbol_at(isym));
133 return f->type == FldInvalid;
134 } );
135 if( fields.empty() ) empties.insert(elem.first);
136 }
137
138 for( const auto& key : empties ) {
139 symbol_map2.erase(key);
140 }
141 }
142
143 static void
144 dump_symbol_map2( const field_key_t& key, const std::list<size_t>& candidates ) {
145 if( !yydebug ) return;
146 char *fields = NULL, sep[2] = "";
147
148 for( auto candidate : candidates ) {
149 char *tmp = fields;
150 fields = xasprintf("%s%s %3" GCC_PRISZ "u",
151 tmp? tmp : "", sep, (fmt_size_t)candidate);
152 sep[0] = ',';
153 free(tmp);
154 }
155
156 dbgmsg( "%s:%d: %3" GCC_PRISZ "u %s {%s}", __func__, __LINE__,
157 (fmt_size_t)key.program, key.name, fields );
158 free(fields);
159 }
160
161 void
162 dump_symbol_map2() {
163 int n = 0;
164 for( const auto& elem : symbol_map2 ) {
165 const field_key_t& key( elem.first );
166 const std::list<size_t>& candidates( elem.second);
167 if( key.program != 0 ) {
168 dump_symbol_map2( key, candidates );
169 n++;
170 }
171 }
172 dbgmsg("symbol_map2 has %d program elements", n);
173 }
174
175 static void
176 dump_symbol_map_value( const char name[], const symbol_map_t::value_type& value ) {
177 if( !yydebug ) return;
178 char *ancestry = NULL, sep[2] = "";
179 auto p = value.second.begin();
180
181 for( ; p != value.second.end(); p++ ) {
182 char *tmp = ancestry;
183 ancestry = xasprintf("%s%s %3" GCC_PRISZ "u",
184 tmp? tmp : "", sep, (fmt_size_t)*p);
185 sep[0] = ',';
186 free(tmp);
187 }
188
189 dbgmsg( "%s:%d: %s -> %-24s {%s }", __func__, __LINE__,
190 name, value.first.c_str(), ancestry );
191 free(ancestry);
192 }
193
194
195 static void
196 dump_symbol_map_value1( const symbol_map_t::value_type& value ) {
197 dump_symbol_map_value( "result", value );
198 }
199
200 static symbol_map_t::value_type
201 field_structure( symbol_elem_t& sym ) {
202 static const symbol_map_t::value_type
203 none( symbol_map_t::key_type( 0, "", 0 ), std::vector<size_t>() );
204
205 if( !is_data_field(sym) ) return none;
206
207 cbl_field_t *field = cbl_field_of(&sym);
208
209 symbol_map_t::key_type key( sym.program, field->name, field->parent );
210 symbol_map_t::value_type elem( key, std::vector<size_t>() );
211 symbol_map_t::mapped_type& v(elem.second);
212
213 for( v.push_back(field_index(field)); field->parent > 0; ) {
214 symbol_elem_t *par = symbol_at(field->parent);
215
216 if( SymFile == par->type ) {
217 v.push_back(field->parent);
218 break;
219 }
220 assert( SymField == par->type );
221 v.push_back(field->parent);
222
223 field = cbl_field_of(par);
224
225 // for C of R and B of A, where R redefines B, skip B: vector is [C, R, A].
226 cbl_field_t *redefined = symbol_redefines(field); // if R redefines B
227 if( redefined ) {
228 field = redefined; // We will use B's parent on next iteration
229 }
230 }
231
232 return elem;
233 }
234
235 void erase_symbol_map_fwds( size_t beg ) {
236 for( auto p = symbols_begin(beg); p < symbols_end(); p++ ) {
237 if( p->type != SymField ) continue;
238 const auto& field(*cbl_field_of(p));
239 if( field.type == FldForward ) {
240 symbol_map.erase( sym_name_t(p->program, field.name, field.parent) );
241 }
242 }
243 }
244
245 void
246 build_symbol_map() {
247 static size_t beg = 0;
248 size_t end = symbols_end() - symbols_begin();
249
250 if( beg == end ) return;
251 const size_t nsym = end - beg;
252
253 std::transform( symbols_begin(beg), symbols_end(),
254 std::inserter(symbol_map, symbol_map.begin()),
255 field_structure );
256 beg = end;
257
258 symbol_map.erase(sym_name_t(""));
259
260 if( yydebug ) {
261 dbgmsg( "%s:%d: " HOST_SIZE_T_PRINT_UNSIGNED " of "
262 HOST_SIZE_T_PRINT_UNSIGNED " symbols inserted into "
263 HOST_SIZE_T_PRINT_UNSIGNED " in symbol_map",
264 __func__, __LINE__, (fmt_size_t)nsym, (fmt_size_t)end,
265 (fmt_size_t)symbol_map.size() );
266 }
267 }
268
269 bool
270 update_symbol_map( symbol_elem_t *e ) {
271 auto output = symbol_map.insert(field_structure(*e));
272 return output.second;
273 }
274
275 class is_name {
276 const char *name;
277 public:
278 explicit is_name( const char *name ) : name(name) {}
279 bool operator()( const symbol_map_t::value_type& elem ) {
280 const bool tf = elem.first == name;
281 return tf;
282 }
283 protected:
284 void dump_key( const char tag[], const symbol_map_t::key_type& key ) const {
285 dbgmsg( "symbol_map key: %s { %3" GCC_PRISZ "u %3" GCC_PRISZ "u %s }",
286 tag, (fmt_size_t)key.program, (fmt_size_t)key.parent, key.name );
287 }
288 };
289
290 /*
291 * Construct a list of ancestors based on a set of candidate groups.
292 * Presented with an item, see if any group an ancestor. If so,
293 * replace the item's ancestry with the group's ancestry (thus
294 * shortening the chain). Otherwise, return an empty element.
295 */
296 class reduce_ancestry {
297 std::vector<symbol_map_t::mapped_type> candidates;
298 static symbol_map_t::mapped_type
299 candidates_only( const symbol_map_t::value_type& elem ) { return elem.second; }
300 public:
301 explicit reduce_ancestry( const symbol_map_t& groups )
302 : candidates( groups.size() )
303 {
304 std::transform( groups.begin(), groups.end(), candidates.begin(),
305 candidates_only );
306 }
307 symbol_map_t::value_type
308 reduce( const symbol_map_t::value_type& item ) {
309 static symbol_map_t::value_type none( "", std::vector<size_t>() );
310
311 auto ancestors = candidates.begin();
312 for( ; ancestors != candidates.end(); ancestors++ ) {
313 assert(!ancestors->empty()); // ancestry always starts with self
314 auto p = std::find( item.second.begin(), item.second.end(),
315 ancestors->front() );
316 if( p != item.second.end() ) {
317 // Preserve symbol's index at front of ancestor list.
318 symbol_map_t::mapped_type shorter(1 + ancestors->size());
319 auto p_l = shorter.begin();
320 *p_l = item.second.front();
321 shorter.insert( ++p_l, ancestors->begin(), ancestors->end() );
322 return make_pair(item.first, shorter);
323 }
324 }
325 return none;
326 }
327 symbol_map_t::value_type
328 operator()( symbol_map_t::value_type item ) { return reduce(item); }
329 };
330
331 class different_program {
332 size_t program;
333 public:
334 explicit different_program( size_t program ) : program(program) {}
335 bool operator()( const symbol_map_t::value_type& item ) const {
336 return ! item.first.same_program(program);
337 }
338 };
339
340 class in_scope {
341 size_t program;
342
343 static size_t prog_of( size_t program ) {
344 const cbl_label_t *L = cbl_label_of(symbol_at(program));
345 return L->parent;
346 }
347
348 public:
349 explicit in_scope( size_t program ) : program(program) {}
350
351 // A symbol is in scope if it's defined by this program or by an ancestor.
352 bool operator()( const symbol_map_t::value_type& item ) const {
353 const symbol_elem_t *e = symbol_at(item.second.front());
354 for( size_t prog = this->program; prog != 0; prog = prog_of(prog) ) {
355 if( e->program == prog ) return true;
356 }
357 return false;
358 }
359 };
360
361 /*
362 * For a field symbol and list of qualifier IN/OF names, see if the
363 * namelist matches the symbol's name and ancectors' names. Success
364 * is all names match within scope.
365 *
366 * All symbols local to the program are in scope. A containing
367 * program's symbol matches only if global_e is set on it or one of
368 * its ancestors.
369 */
370 static bool
371 name_has_names( const symbol_elem_t *e,
372 const std::list<const char *>& names, bool in_scope )
373 {
374 assert( ! names.empty() );
375 auto name = names.rbegin();
376
377 while( e && e->type == SymField ) {
378 auto field = cbl_field_of(e);
379 if( field->type == FldForward ) return false;
380
381 if( 0 == strcasecmp(field->name, *name) ) {
382 in_scope = in_scope || (field->attr & global_e);
383 if( ++name == names.rend() ) break;
384 }
385
386 // first name must match
387 if( name == names.rbegin() ) break;
388
389 // Do not chase redefines if we have an 01 record for an FD.
390 if( field->file ) {
391 e = symbol_at(field->file);
392 assert(1 == field->level);
393 assert(e->type == SymFile);
394 break;
395 }
396
397 /*
398 * If the current field redefines another, it is not a member of
399 * its parent, but of its grandparent, if any. Not a loop because
400 * REDEFINES cannot be chained.
401 */
402 cbl_field_t *parent = symbol_redefines(field);
403 if( parent ) {
404 field = parent;
405 assert( NULL == symbol_redefines(field) );
406 }
407
408 e = field->parent ? symbol_at(field->parent) : NULL;
409 }
410
411 if( e && e->type == SymFile ) {
412 // first name can be a filename
413 auto file = cbl_file_of(e);
414 if( 0 == strcasecmp(file->name, *name) ) name++;
415 }
416
417 return in_scope && name == names.rend();
418 }
419
420 size_t end_of_group( size_t igroup );
421
422 static std::vector<size_t>
423 symbol_match2( size_t program,
424 const std::list<const char *>& names, bool local = true )
425 {
426 std::vector<size_t> fields;
427
428 field_key_t key(program, names.back());
429
430 auto plist = symbol_map2.find(key);
431 if( plist != symbol_map2.end() ) {
432 for( auto candidate : plist->second ) {
433 const symbol_elem_t *e = symbol_at(candidate);
434 if( name_has_names( e, names, local ) ) {
435 fields.push_back( symbol_index(e) );
436 }
437 }
438 }
439
440 if( fields.empty() ){
441 if( program > 0 ) { // try containing program
442 program = cbl_label_of(symbol_at(program))->parent;
443 return symbol_match2( program, names, program == 0 );
444 }
445 }
446
447 if( yydebug ) {
448 char *ancestry = NULL;
449 const char *sep = "";
450 for( auto name : names ) {
451 char *partial = ancestry;
452 int asret = asprintf(&ancestry, "%s%s%s", partial? partial : "", sep, name);
453 assert(asret);
454 sep = " -> ";
455 assert(ancestry);
456 free(partial);
457 }
458
459 if( fields.empty() ) {
460 dbgmsg("%s: '%s' matches no fields", __func__, ancestry);
461 dump_symbol_map2();
462 } else {
463 char *fieldstr = NULL;
464 sep = "";
465 for( auto field : fields ) {
466 char *partial = fieldstr;
467 int asret = asprintf(&fieldstr, "%s%s" HOST_SIZE_T_PRINT_UNSIGNED,
468 partial? partial : "", sep, (fmt_size_t)field);
469 assert(asret);
470 sep = ", ";
471 assert(fieldstr);
472 free(partial);
473 }
474
475 dbgmsg("%s: '%s' matches " HOST_SIZE_T_PRINT_UNSIGNED " fields: {%s}",
476 __func__, ancestry, (fmt_size_t)fields.size(), fieldstr);
477 free(fieldstr);
478 }
479 free(ancestry);
480 }
481
482 return fields;
483 }
484
485 /*
486 * The names list is in top-down order, front-to-back. This function
487 * iterates backwards over the list, looking for the parent of N at
488 * N-1.
489 */
490 static symbol_map_t
491 symbol_match( size_t program, const std::list<const char *>& names ) {
492 auto matched = symbol_match2( program, names );
493 symbol_map_t output;
494
495 for( auto isym : matched ) {
496 auto e = symbol_at(isym);
497 auto f = cbl_field_of(e);
498
499 symbol_map_t::key_type key( e->program, f->name, f->parent );
500 auto p = symbol_map.find(key);
501 if( p == symbol_map.end() ) {
502 yyerror("%s is not defined", key.name);
503 continue;
504 }
505 auto inserted = output.insert(*p);
506 if( ! inserted.second ) {
507 error_msg_direct("%s is not a unique reference", key.name);
508 }
509 }
510 return output;
511 }
512
513 static const symbol_elem_t * symbol_field_alias_01;
514
515 const symbol_elem_t *
516 symbol_field_alias_begin() {
517 return symbol_field_alias_01 = symbol_field_current_record();
518 }
519 void
520 symbol_field_alias_end() {
521 symbol_field_alias_01 = NULL;
522 }
523
524 std::pair <symbol_elem_t *, bool>
525 symbol_find( size_t program, std::list<const char *> names ) {
526 symbol_map_t items = symbol_match(program, names);
527
528 if( symbol_field_alias_01 && items.size() != 1 ) {
529 symbol_map_t qualified;
530 size_t i01( symbol_index(symbol_field_alias_01) );
531 std::copy_if( items.begin(), items.end(),
532 std::inserter(qualified, qualified.begin()),
533 [i01]( auto item ) {
534 const std::vector<size_t>& ancestors(item.second);
535 return ancestors.back() == i01;
536 } );
537 items = qualified;
538 }
539
540 auto unique = items.size() == 1;
541
542 if( !unique ) {
543 if( items.empty() ) {
544 return std::pair<symbol_elem_t *, bool>(NULL, false);
545 }
546 if( yydebug ) {
547 dbgmsg( "%s:%d: '%s' has " HOST_SIZE_T_PRINT_UNSIGNED " possible matches",
548 __func__, __LINE__, names.back(), (fmt_size_t)items.size() );
549 std::for_each( items.begin(), items.end(), dump_symbol_map_value1 );
550 }
551 }
552
553 size_t isym = items.begin()->second.front();
554 auto output = std::make_pair(symbol_at(isym), unique);
555
556 assert( FldForward != field_at(isym)->type );
557
558 return output;
559 }
560
561 class in_group {
562 size_t group;
563 public:
564 explicit in_group( size_t group ) : group(group) {}
565
566 bool operator()( symbol_map_t::const_reference elem ) const {
567 return 0 < std::count( elem.second.begin(),
568 elem.second.end(), this->group );
569 }
570 };
571
572 symbol_elem_t *
573 symbol_find_of( size_t program, std::list<const char *> names, size_t group ) {
574 symbol_map_t input = symbol_match(program, names);
575
576 symbol_map_t items;
577 std::copy_if( input.begin(), input.end(),
578 std::inserter(items, items.begin()), in_group(group) );
579
580 if( items.size() == 1 ) {
581 size_t isym = items.begin()->second.front();
582 assert( FldForward != field_at(isym)->type );
583 return symbol_at(isym);
584 }
585
586 if( yydebug ) {
587 dbgmsg( "%s:%d: '%s' has " HOST_SIZE_T_PRINT_UNSIGNED " possible matches",
588 __func__, __LINE__, names.back(), (fmt_size_t)input.size() );
589 std::for_each( input.begin(), input.end(), dump_symbol_map_value1 );
590 }
591
592 return NULL;
593 }