1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/***************************************************************************
ns.c (libIDL namespace functions)
Copyright (C) 1998, 1999 Andrew T. Veliath
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: ns.c 445 2000-07-14 18:17:43Z kennard $
***************************************************************************/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "rename.h"
#include "util.h"
static int is_inheritance_conflict (IDL_tree p);
IDL_ns IDL_ns_new (void)
{
IDL_ns ns;
ns = g_new0 (struct _IDL_ns, 1);
if (ns == NULL) {
yyerror ("IDL_ns_new: memory exhausted");
return NULL;
}
IDL_NS (ns).global = IDL_gentree_new (IDL_ident_hash,
IDL_ident_equal,
IDL_ident_new (""));
IDL_NS (ns).file = IDL_NS (ns).current = IDL_NS (ns).global;
IDL_NS (ns).inhibits = g_hash_table_new (g_direct_hash, g_direct_equal);
IDL_NS (ns).filename_hash = g_hash_table_new (g_str_hash, g_str_equal);
return ns;
}
static void filename_hash_free (char *filename, IDL_fileinfo *fi)
{
g_free (fi->name);
g_free (fi);
}
void IDL_ns_free (IDL_ns ns)
{
assert (ns != NULL);
g_hash_table_foreach (IDL_NS (ns).inhibits, (GHFunc)IDL_tree_free, NULL);
g_hash_table_destroy (IDL_NS (ns).inhibits);
g_hash_table_foreach (IDL_NS (ns).filename_hash, (GHFunc) filename_hash_free, NULL);
g_hash_table_destroy (IDL_NS (ns).filename_hash);
IDL_tree_free (IDL_NS (ns).global);
g_free (ns);
}
#define IDL_NS_ASSERTS do { \
assert (ns != NULL); \
if (__IDL_is_parsing) { \
assert (IDL_NS (ns).global != NULL); \
assert (IDL_NS (ns).file != NULL); \
assert (IDL_NS (ns).current != NULL); \
assert (IDL_NODE_TYPE (IDL_NS (ns).global) == IDLN_GENTREE); \
assert (IDL_NODE_TYPE (IDL_NS (ns).file) == IDLN_GENTREE); \
assert (IDL_NODE_TYPE (IDL_NS (ns).current) == IDLN_GENTREE); \
} \
} while (0)
int IDL_ns_prefix (IDL_ns ns, const char *s)
{
char *r;
int l;
IDL_NS_ASSERTS;
if (s == NULL)
return FALSE;
if (*s == '"')
r = g_strdup (s + 1);
else
r = g_strdup (s);
l = strlen (r);
if (l && r[l - 1] == '"')
r[l - 1] = 0;
if (IDL_GENTREE (IDL_NS (ns).current)._cur_prefix)
g_free (IDL_GENTREE (IDL_NS (ns).current)._cur_prefix);
IDL_GENTREE (IDL_NS (ns).current)._cur_prefix = r;
return TRUE;
}
IDL_tree IDL_ns_resolve_this_scope_ident (IDL_ns ns, IDL_tree scope, IDL_tree ident)
{
IDL_tree p, q;
IDL_NS_ASSERTS;
p = scope;
while (p != NULL) {
q = IDL_ns_lookup_this_scope (ns, p, ident, NULL);
if (q != NULL)
return q;
p = IDL_NODE_UP (p);
}
return p;
}
IDL_tree IDL_ns_resolve_ident (IDL_ns ns, IDL_tree ident)
{
return IDL_ns_resolve_this_scope_ident (ns, IDL_NS (ns).current, ident);
}
IDL_tree IDL_ns_lookup_this_scope (IDL_ns ns, IDL_tree scope, IDL_tree ident, gboolean *conflict)
{
IDL_tree p, q;
IDL_NS_ASSERTS;
if (conflict)
*conflict = TRUE;
if (scope == NULL)
return NULL;
assert (IDL_NODE_TYPE (scope) == IDLN_GENTREE);
/* Search this namespace */
if (g_hash_table_lookup_extended (
IDL_GENTREE (scope).children, ident, NULL, (gpointer)&p)) {
assert (IDL_GENTREE (p).data != NULL);
assert (IDL_NODE_TYPE (IDL_GENTREE (p).data) == IDLN_IDENT);
return p;
}
/* If there are inherited namespaces, look in those before giving up */
q = IDL_GENTREE (scope)._import;
if (!q)
return NULL;
assert (IDL_NODE_TYPE (q) == IDLN_LIST);
for (; q != NULL; q = IDL_LIST (q).next) {
IDL_tree r;
assert (IDL_LIST (q).data != NULL);
assert (IDL_NODE_TYPE (IDL_LIST (q).data) == IDLN_IDENT);
assert (IDL_IDENT_TO_NS (IDL_LIST (q).data) != NULL);
assert (IDL_NODE_TYPE (IDL_IDENT_TO_NS (IDL_LIST (q).data)) == IDLN_GENTREE);
/* Search imported namespace scope q */
if (g_hash_table_lookup_extended (
IDL_GENTREE (IDL_IDENT_TO_NS (IDL_LIST (q).data)).children,
ident, NULL, (gpointer)&p)) {
assert (IDL_GENTREE (p).data != NULL);
assert (IDL_NODE_TYPE (IDL_GENTREE (p).data) == IDLN_IDENT);
/* This needs more work, it won't do full ambiguity detection */
if (conflict && !is_inheritance_conflict (p))
*conflict = FALSE;
return p;
}
/* Search up one level */
if (IDL_NODE_TYPE (IDL_NODE_UP (IDL_LIST (q).data)) == IDLN_INTERFACE &&
(r = IDL_ns_lookup_this_scope (
ns, IDL_IDENT_TO_NS (IDL_LIST (q).data), ident, conflict)))
return r;
}
return NULL;
}
IDL_tree IDL_ns_lookup_cur_scope (IDL_ns ns, IDL_tree ident, gboolean *conflict)
{
return IDL_ns_lookup_this_scope (ns, IDL_NS (ns).current, ident, conflict);
}
IDL_tree IDL_ns_place_new (IDL_ns ns, IDL_tree ident)
{
IDL_tree p, up_save;
gboolean does_conflict;
IDL_NS_ASSERTS;
p = IDL_ns_lookup_cur_scope (ns, ident, &does_conflict);
if (p != NULL && does_conflict)
return NULL;
/* The namespace tree is separate from the primary parse tree,
so keep the primary tree node's parent the same */
up_save = IDL_NODE_UP (ident);
p = IDL_gentree_chain_child (IDL_NS (ns).current, ident);
IDL_NODE_UP (ident) = up_save;
if (p == NULL)
return NULL;
assert (IDL_NODE_TYPE (p) == IDLN_GENTREE);
IDL_IDENT_TO_NS (ident) = p;
assert (IDL_NODE_UP (IDL_IDENT_TO_NS (ident)) == IDL_NS (ns).current);
/* Generate default repository ID */
IDL_IDENT_REPO_ID (ident) =
IDL_ns_ident_make_repo_id (__IDL_root_ns, p, NULL, NULL, NULL);
return p;
}
void IDL_ns_push_scope (IDL_ns ns, IDL_tree ns_ident)
{
IDL_NS_ASSERTS;
assert (IDL_NODE_TYPE (ns_ident) == IDLN_GENTREE);
assert (IDL_NODE_TYPE (IDL_GENTREE (ns_ident).data) == IDLN_IDENT);
assert (IDL_NS (ns).current == IDL_NODE_UP (ns_ident));
IDL_NS (ns).current = ns_ident;
}
void IDL_ns_pop_scope (IDL_ns ns)
{
IDL_NS_ASSERTS;
if (IDL_NODE_UP (IDL_NS (ns).current) != NULL)
IDL_NS (ns).current = IDL_NODE_UP (IDL_NS (ns).current);
}
IDL_tree IDL_ns_qualified_ident_new (IDL_tree nsid)
{
IDL_tree l = NULL, item;
while (nsid != NULL) {
if (IDL_GENTREE (nsid).data == NULL) {
nsid = IDL_NODE_UP (nsid);
continue;
}
assert (IDL_GENTREE (nsid).data != NULL);
assert (IDL_NODE_TYPE (IDL_GENTREE (nsid).data) == IDLN_IDENT);
item = IDL_list_new (IDL_ident_new (
g_strdup (IDL_IDENT (IDL_GENTREE (nsid).data).str)));
l = IDL_list_concat (item, l);
nsid = IDL_NODE_UP (nsid);
}
return l;
}
gchar *IDL_ns_ident_to_qstring (IDL_tree ns_ident, const char *join, int levels)
{
IDL_tree l, q;
int len, joinlen;
char *s;
int count = 0, start_level;
if (levels < 0 || levels > 64)
return NULL;
if (ns_ident == NULL)
return NULL;
if (IDL_NODE_TYPE (ns_ident) == IDLN_IDENT)
ns_ident = IDL_IDENT_TO_NS (ns_ident);
assert (IDL_NODE_TYPE (ns_ident) == IDLN_GENTREE);
l = IDL_ns_qualified_ident_new (ns_ident);
if (l == NULL)
return NULL;
if (join == NULL)
join = "";
joinlen = strlen (join);
for (len = 0, q = l; q != NULL; q = IDL_LIST (q).next) {
IDL_tree i = IDL_LIST (q).data;
assert (IDL_NODE_TYPE (q) == IDLN_LIST);
assert (IDL_NODE_TYPE (i) == IDLN_IDENT);
if (IDL_IDENT (i).str != NULL)
len += strlen (IDL_IDENT (i).str) + joinlen;
++count;
}
if (levels == 0)
start_level = 0;
else
start_level = count - levels;
assert (start_level >= 0 && start_level < count);
s = g_malloc (len + 1);
if (s == NULL) {
IDL_tree_free (l);
return NULL;
}
s[0] = '\0';
for (q = l; q != NULL; q = IDL_LIST (q).next) {
IDL_tree i = IDL_LIST (q).data;
if (start_level > 0) {
--start_level;
continue;
}
if (s[0] != '\0')
strcat (s, join);
strcat (s, IDL_IDENT (i).str);
}
IDL_tree_free (l);
return s;
}
int IDL_ns_scope_levels_from_here (IDL_ns ns, IDL_tree ident, IDL_tree parent)
{
IDL_tree p, scope_here, scope_ident;
int levels;
g_return_val_if_fail (ns != NULL, 1);
g_return_val_if_fail (ident != NULL, 1);
while (parent && !IDL_NODE_IS_SCOPED (parent))
parent = IDL_NODE_UP (parent);
if (parent == NULL)
return 1;
if ((scope_here = IDL_tree_get_scope (parent)) == NULL ||
(scope_ident = IDL_tree_get_scope (ident)) == NULL)
return 1;
assert (IDL_NODE_TYPE (scope_here) == IDLN_GENTREE);
assert (IDL_NODE_TYPE (scope_ident) == IDLN_GENTREE);
for (levels = 1; scope_ident;
++levels, scope_ident = IDL_NODE_UP (scope_ident)) {
p = IDL_ns_resolve_this_scope_ident (
ns, scope_here, IDL_GENTREE (scope_ident).data);
if (p == scope_ident)
return levels;
}
return 1;
}
/* If insertion was made, return true, else there was a collision */
static gboolean heap_insert_ident (IDL_tree interface_ident, GTree *heap, IDL_tree any)
{
IDL_tree p;
assert (any != NULL);
assert (heap != NULL);
if ((p = g_tree_lookup (heap, any))) {
char *newi;
char *i1, *i2;
char *what1 = "identifier", *what2 = what1;
char *who1, *who2;
IDL_tree q;
assert (IDL_NODE_TYPE (p) == IDLN_IDENT);
newi = IDL_ns_ident_to_qstring (IDL_IDENT_TO_NS (interface_ident), "::", 0);
i1 = IDL_ns_ident_to_qstring (IDL_IDENT_TO_NS (p), "::", 0);
i2 = IDL_ns_ident_to_qstring (IDL_IDENT_TO_NS (any), "::", 0);
q = p;
while (q && (IDL_NODE_TYPE (q) == IDLN_IDENT || IDL_NODE_TYPE (q) == IDLN_LIST))
q = IDL_NODE_UP (q);
assert (q != NULL);
IDL_tree_get_node_info (q, &what1, &who1);
q = any;
while (q && (IDL_NODE_TYPE (q) == IDLN_IDENT || IDL_NODE_TYPE (q) == IDLN_LIST))
q = IDL_NODE_UP (q);
assert (q != NULL);
IDL_tree_get_node_info (q, &what2, &who2);
yyerrorv ("Ambiguous inheritance in interface `%s' from %s `%s' and %s `%s'",
newi, what1, i1, what2, i2);
IDL_tree_error (p, "%s `%s' conflicts with", what1, i1);
IDL_tree_error (any, "%s `%s'", what2, i2);
g_free (newi); g_free (i1); g_free (i2);
return FALSE;
}
g_tree_insert (heap, any, any);
return TRUE;
}
static int is_visited_interface (GHashTable *visited_interfaces, IDL_tree scope)
{
assert (scope != NULL);
assert (IDL_NODE_TYPE (scope) == IDLN_GENTREE);
/* If already visited, do not visit again */
return g_hash_table_lookup_extended (visited_interfaces, scope, NULL, NULL);
}
static void mark_visited_interface (GHashTable *visited_interfaces, IDL_tree scope)
{
assert (scope != NULL);
assert (IDL_NODE_TYPE (scope) == IDLN_GENTREE);
g_hash_table_insert (visited_interfaces, scope, scope);
}
static int is_inheritance_conflict (IDL_tree p)
{
if (IDL_GENTREE (p).data == NULL)
return FALSE;
assert (IDL_NODE_TYPE (IDL_GENTREE (p).data) == IDLN_IDENT);
if (IDL_NODE_UP (IDL_GENTREE (p).data) == NULL)
return FALSE;
if (!(IDL_NODE_TYPE (IDL_NODE_UP (IDL_GENTREE (p).data)) == IDLN_OP_DCL ||
(IDL_NODE_UP (IDL_GENTREE (p).data) &&
IDL_NODE_TYPE (IDL_NODE_UP (IDL_NODE_UP (IDL_GENTREE (p).data))) == IDLN_ATTR_DCL)))
return FALSE;
return TRUE;
}
typedef struct {
IDL_tree interface_ident;
GTree *ident_heap;
int insert_conflict;
} InsertHeapData;
static void insert_heap_cb (IDL_tree ident, IDL_tree p, InsertHeapData *data)
{
if (!is_inheritance_conflict (p))
return;
if (!heap_insert_ident (
data->interface_ident, data->ident_heap, IDL_GENTREE (p).data))
data->insert_conflict = 1;
}
/* Return true if adds went okay */
static int IDL_ns_load_idents_to_tables (IDL_tree interface_ident, IDL_tree ident_scope,
GTree *ident_heap, GHashTable *visited_interfaces)
{
IDL_tree q, scope;
InsertHeapData data;
assert (ident_scope != NULL);
assert (IDL_NODE_TYPE (ident_scope) == IDLN_IDENT);
scope = IDL_IDENT_TO_NS (ident_scope);
if (!scope)
return TRUE;
assert (IDL_NODE_TYPE (scope) == IDLN_GENTREE);
assert (IDL_GENTREE (scope).data != NULL);
assert (IDL_NODE_TYPE (IDL_GENTREE (scope).data) == IDLN_IDENT);
assert (IDL_NODE_UP (IDL_GENTREE (scope).data) != NULL);
assert (IDL_NODE_TYPE (IDL_NODE_UP (IDL_GENTREE (scope).data)) == IDLN_INTERFACE);
if (is_visited_interface (visited_interfaces, scope))
return TRUE;
/* Search this namespace */
data.interface_ident = interface_ident;
data.ident_heap = ident_heap;
data.insert_conflict = 0;
g_hash_table_foreach (IDL_GENTREE (scope).children, (GHFunc)insert_heap_cb, &data);
/* If there are inherited namespaces, look in those before giving up */
q = IDL_GENTREE (scope)._import;
if (!q)
data.insert_conflict = 0;
else
assert (IDL_NODE_TYPE (q) == IDLN_LIST);
/* Add inherited namespace identifiers into heap */
for (; q != NULL; q = IDL_LIST (q).next) {
int r;
assert (IDL_LIST (q).data != NULL);
assert (IDL_NODE_TYPE (IDL_LIST (q).data) == IDLN_IDENT);
assert (IDL_IDENT_TO_NS (IDL_LIST (q).data) != NULL);
assert (IDL_NODE_TYPE (IDL_IDENT_TO_NS (IDL_LIST (q).data)) == IDLN_GENTREE);
assert (IDL_NODE_TYPE (IDL_NODE_UP (IDL_LIST (q).data)) == IDLN_INTERFACE);
if (!(r = IDL_ns_load_idents_to_tables (interface_ident, IDL_LIST (q).data,
ident_heap, visited_interfaces)))
data.insert_conflict = 1;
}
mark_visited_interface (visited_interfaces, scope);
return data.insert_conflict == 0;
}
int IDL_ns_check_for_ambiguous_inheritance (IDL_tree interface_ident, IDL_tree p)
{
/* We use a sorted heap to check for namespace collisions,
since we must do case-insensitive collision checks.
visited_interfaces is a hash of visited interface nodes, so
we only visit common ancestors once. */
GTree *ident_heap;
GHashTable *visited_interfaces;
int is_ambiguous = 0;
if (!p)
return 0;
ident_heap = g_tree_new (IDL_ident_cmp);
visited_interfaces = g_hash_table_new (g_direct_hash, g_direct_equal);
assert (IDL_NODE_TYPE (p) == IDLN_LIST);
for (; p; p = IDL_LIST (p).next) {
if (!IDL_ns_load_idents_to_tables (interface_ident, IDL_LIST (p).data,
ident_heap, visited_interfaces))
is_ambiguous = 1;
}
g_tree_destroy (ident_heap);
g_hash_table_destroy (visited_interfaces);
return is_ambiguous;
}
/*
* Local variables:
* mode: C
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*/
|